ReactEasy

What is a Store?

In Redux, the store is the single object that holds the whole application state and exposes three core methods: getState() to read it, dispatch(action) to request changes, and subscribe(listener) to be notified of updates. With Redux Toolkit you create it with configureStore, and React components access it through <Provider> and hooks.

What the store does

  • Holds the current state tree.
  • Runs dispatched actions through middleware, then through the root reducer to produce the next state.
  • Notifies subscribers after each dispatch (React Redux uses useSyncExternalStore internally to subscribe safely).

Creating one

configureStore({ reducer: { cart: cartReducer, user: userReducer } }) combines slice reducers, adds the thunk middleware and DevTools, and enables development checks.

In React

  • Wrap the app in <Provider store={store}>.
  • useSelector((state) => state.cart.items) reads a slice.
  • useDispatch() returns dispatch.

Good practices

  • One store per app (per request on the server, to avoid sharing state between users in SSR).
  • Keep the state serializable (no class instances, functions or promises) so DevTools and persistence work.
  • Normalize collections (ids + entities) with createEntityAdapter.
  • Do not put server cache data you could manage with RTK Query or TanStack Query into hand-written slices.

The word "store" elsewhere

Zustand, Jotai and other libraries also call their containers stores; the idea is the same: state outside components with a subscription API.

Code example

tsx
1import { configureStore, createSlice } from "@reduxjs/toolkit";
2
3const themeSlice = createSlice({
4 name: "theme",
5 initialState: { mode: "light" as "light" | "dark" },
6 reducers: { toggled: (s) => void (s.mode = s.mode === "light" ? "dark" : "light") },
7});
8
9export const store = configureStore({
10 reducer: { theme: themeSlice.reducer },
11});
12
13// The three core store methods
14const unsubscribe = store.subscribe(() => {
15 console.log("state changed:", store.getState().theme.mode);
16});
17
18store.dispatch(themeSlice.actions.toggled()); // logs "dark"
19console.log(store.getState()); // { theme: { mode: "dark" } }
20unsubscribe();
21
22// In React:
23// <Provider store={store}><App /></Provider>
24// const mode = useSelector((s: RootState) => s.theme.mode);

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app