Next.js + Redux Toolkit
Central store with slices and typed hooks. This page shows exactly what pickstack adds to a Next.js project for Redux Toolkit: the packages, the new files and the changes to existing ones. Every combination is installed, built, linted and tested in CI.
$ npm create pickstack@latest my-app -- --framework next --modules redux-toolkit --yesCustomize in the configurator
Packages it adds
| Package | Version | Type |
|---|---|---|
@reduxjs/toolkit | ^2.15.0 | runtime |
react-redux | ^9.3.1 | runtime |
New files
"use client";
import { type ReactNode, useState } from "react";
import { Provider } from "react-redux";
import { type AppStore, makeStore } from "../lib/store";
export default function StoreProvider({ children }: { children: ReactNode }) {
const [store] = useState<AppStore>(makeStore);
return <Provider store={store}>{children}</Provider>;
}
import { createSlice } from "@reduxjs/toolkit";
interface CounterState {
value: number;
}
const initialState: CounterState = { value: 0 };
const counterSlice = createSlice({
name: "counter",
initialState,
reducers: {
increment: (state) => {
state.value += 1;
},
decrement: (state) => {
state.value -= 1;
},
},
});
export const { increment, decrement } = counterSlice.actions;
export default counterSlice.reducer;
import { useDispatch, useSelector } from "react-redux";
import type { AppDispatch, RootState } from "./store";
export const useAppDispatch = useDispatch.withTypes<AppDispatch>();
export const useAppSelector = useSelector.withTypes<RootState>();
import { configureStore } from "@reduxjs/toolkit";
import counterReducer from "./counterSlice";
export const makeStore = () =>
configureStore({
reducer: {
counter: counterReducer,
},
});
export type AppStore = ReturnType<typeof makeStore>;
export type RootState = ReturnType<AppStore["getState"]>;
export type AppDispatch = AppStore["dispatch"];
Changes to existing files
⋯ 2 unchanged lines <section className="page"> <h1 className="title">About</h1> <p className="text">+ Built with Next.js (App Router), React 19, TypeScript, Redux Toolkit.− Built with Next.js (App Router), React 19, TypeScript. </p> </section> );⋯ 2 unchanged lines
import type { Metadata } from "next"; import Link from "next/link";+import StoreProvider from "./StoreProvider"; import "./globals.css"; export const metadata: Metadata = {⋯ 5 unchanged lines return ( <html lang="en"> <body>+ <StoreProvider>+ <main className="app">+ <nav className="nav">+ <Link className="link" href="/">+ Home+ </Link>+ <Link className="link" href="/about">+ About+ </Link>+ </nav>+ {children}+ </main>+ </StoreProvider>− <main className="app">− <nav className="nav">− <Link className="link" href="/">− Home− </Link>− <Link className="link" href="/about">− About− </Link>− </nav>− {children}− </main> </body> </html> );⋯ 2 unchanged lines
"use client"; +import { decrement, increment } from "../lib/counterSlice";+import { useAppDispatch, useAppSelector } from "../lib/hooks";−import { useState } from "react"; export default function Counter() {+ const count = useAppSelector((state) => state.counter.value);+ const dispatch = useAppDispatch();− const [count, setCount] = useState(0); return ( <div className="card">⋯ 2 unchanged lines <button type="button" className="btn"+ onClick={() => dispatch(decrement())}− onClick={() => setCount((n) => n - 1)} > -1 </button> <button type="button" className="btn"+ onClick={() => dispatch(increment())}− onClick={() => setCount((n) => n + 1)} > +1 </button>⋯ 5 unchanged lines
Project files
.gitignoreREADME.mdapp/StoreProvider.tsxapp/about/page.tsxapp/globals.cssapp/layout.tsxapp/page.tsxcomponents/Counter.tsxlib/counterSlice.tslib/hooks.tslib/store.tsnext.config.tspackage.jsontsconfig.json