pickstack

React + Redux Toolkit

Central store with slices and typed hooks. This page shows exactly what pickstack adds to a React 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 react --modules redux-toolkit --yes
Customize in the configurator

Packages it adds

PackageVersionType
@reduxjs/toolkit^2.15.0runtime
react-redux^9.3.1runtime

New files

src/store/counterSlice.ts
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;
src/store/hooks.ts
import { useDispatch, useSelector } from "react-redux";
import type { AppDispatch, RootState } from "./index";

export const useAppDispatch = useDispatch.withTypes<AppDispatch>();
export const useAppSelector = useSelector.withTypes<RootState>();
src/store/index.ts
import { configureStore } from "@reduxjs/toolkit";
import counterReducer from "./counterSlice";

export const store = configureStore({
  reducer: {
    counter: counterReducer,
  },
});

export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;

Changes to existing files

src/components/Counter.tsx (changed)
+import { decrement, increment } from "../store/counterSlice";+import { useAppDispatch, useAppSelector } from "../store/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
src/main.tsx (changed)
 import { StrictMode } from "react"; import { createRoot } from "react-dom/client";+import { Provider } from "react-redux"; import App from "./App";+import { store } from "./store"; import "./index.css";   createRoot(document.getElementById("root")!).render(   <StrictMode>+    <Provider store={store}>+      <App />+    </Provider>−    <App />   </StrictMode>, );  

Project files

Related templates