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 --yesCustomize in the configurator
Packages it adds
| Package | Version | Type |
|---|---|---|
@reduxjs/toolkit | ^2.15.0 | runtime |
react-redux | ^9.3.1 | runtime |
New files
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 "./index";
export const useAppDispatch = useDispatch.withTypes<AppDispatch>();
export const useAppSelector = useSelector.withTypes<RootState>();
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
+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
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
.gitignoreREADME.mdindex.htmlpackage.jsonsrc/App.tsxsrc/components/Counter.tsxsrc/index.csssrc/main.tsxsrc/store/counterSlice.tssrc/store/hooks.tssrc/store/index.tstsconfig.jsonvite.config.ts