Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 41 ยท Getting Started

Setting Up a Redux Project

Install Redux Toolkit and React-Redux, create a store, add a slice and connect it to a React app with Vite. Includes a recommended folder layout and a runnable wiring demo.

Setting up Redux today takes a few minutes and four small pieces: install the packages, create a store, write a slice, and wrap your app in a Provider. This lesson walks through each piece in a real Vite + React project, then shows the same wiring as a runnable stand-in so you can see how the parts connect.

The commands and imports in the read-only blocks below belong in your own project on your computer. Our editor cannot install npm packages, so the live examples use hand-written stand-ins (labelled in comments) with the same shape as configureStore and createSlice.

Step 1: create a project and install

If you do not have a React project yet, create one with Vite, then install the two packages you need.

Terminal
# needs Node.js 20 or newer (read-only here)
npm create vite@latest my-redux-app -- --template react
cd my-redux-app
npm install @reduxjs/toolkit react-redux
  • @reduxjs/toolkit is the package you will write most code with. It depends on and re-exports the core redux package, so you rarely install redux yourself.
  • react-redux connects a store to React components.
  • For a TypeScript project use the react-ts template; both packages ship their own types, so there is no @types package to add.

You can also start from the official Redux templates (they include a counter example, TypeScript and a working store), but building the pieces by hand once is the best way to learn them.

Redux does not force a structure, but most teams use feature folders:

Text
src/
  app/
    store.js          // creates the store
  features/
    counter/
      counterSlice.js // reducer + actions + selectors for one feature
      Counter.jsx     // the component that uses them
  main.jsx            // renders <Provider>

Everything about "counter" lives together. Adding a feature means adding a folder and one line in store.js. Organizing Redux Code goes deeper on layout and naming.

Step 3: write a slice

A slice is the Redux logic for one part of the state. It holds the initial state, the reducer functions and the generated action creators.

JavaScript
// src/features/counter/counterSlice.js (needs @reduxjs/toolkit)
import { createSlice } from '@reduxjs/toolkit';

const counterSlice = createSlice({
  name: 'counter',
  initialState: { value: 0 },
  reducers: {
    incremented(state) {
      state.value += 1;
    },
    decremented(state) {
      state.value -= 1;
    },
  },
});

export const { incremented, decremented } = counterSlice.actions;
export default counterSlice.reducer;

Step 4: create the store

JavaScript
// src/app/store.js (needs @reduxjs/toolkit)
import { configureStore } from '@reduxjs/toolkit';
import counterReducer from '../features/counter/counterSlice';

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

The keys of the reducer object become the keys of the state: store.getState() returns { counter: { value: 0 } }. configureStore also turns on the Redux DevTools, adds the thunk middleware, and in development checks for accidental mutation and non-serializable values. Chapter 3 takes it apart.

Step 5: provide the store to React

JSX
// src/main.jsx (needs react-redux)
import { createRoot } from 'react-dom/client';
import { Provider } from 'react-redux';
import { store } from './app/store';
import App from './App';

createRoot(document.getElementById('root')).render(
  <Provider store={store}>
    <App />
  </Provider>
);

Every component below Provider can now read the store with useSelector and send actions with useDispatch, as shown in Redux with React. Run npm run dev and install the Redux DevTools browser extension: you will see each action as you click.

Seeing the wiring run

The first example below recreates steps 3 to 5 without React. configureStore and createSlice here are small stand-ins, but the files-as-comments structure mirrors a real project.

JS Slice, store and state shape

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Look at the action object printed at the end: {"type":"counter/incremented"}. The slice name and the reducer name were combined to build the type string, and no one wrote it by hand.

Common setup mistakes

Most first-time errors come from wiring, not from Redux itself. Try the failures on purpose:

JS Typical wiring mistakes

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Remember the rule: put slice.reducer (the default export) into the store, never the slice object. And note that the key you choose in configureStore is the key under which components will find the data.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which two packages do you normally install for a React app using Redux?

    Redux Toolkit is the recommended way to write Redux logic and react-redux connects the store to React. Toolkit already includes redux, thunk and Immer.
  2. 2. With configureStore({ reducer: { cart: cartReducer } }), what does store.getState() return?

    The keys of the reducer object become the keys of the root state, so the state is { cart: ... }.
  3. 3. Where must the Provider be placed?

    Provider puts the store into React context, so it must wrap every component that uses useSelector or useDispatch.
  4. 4. What do you put into configureStore's reducer map for a slice?

    A slice object bundles name, actions and reducer. The store needs the reducer function, which is the slice's default export.

Summary

  • Install @reduxjs/toolkit and react-redux; Toolkit includes the core Redux package.
  • Use feature folders: each folder holds a slice and the components that use it.
  • configureStore({ reducer: { key: sliceReducer } }) builds the store; the keys define the state shape.
  • Wrap the app in <Provider store={store}>.
  • Install the Redux DevTools extension to watch actions while you develop.

Next: Your First Redux App