Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 41 ยท Core Concepts

The Redux Store

Understand the Redux store API: getState, dispatch, subscribe, preloaded state and replaceReducer. Learn the subscription rules, why createStore is deprecated and how configureStore replaces it.

The store is the object that holds your state and ties everything together. It has a tiny API, only a handful of methods, and learning it fully takes ten minutes. Everything else in Redux (React bindings, Toolkit, middleware, the DevTools) is built on those methods.

A Redux app has exactly one store. It holds the whole state tree, calls your root reducer to compute updates, and notifies subscribers after each one.

The store API

Method What it does
store.getState() Returns the current state tree (the value last returned by the root reducer).
store.dispatch(action) Sends an action through the reducer, saves the new state, then notifies subscribers. Returns the action.
store.subscribe(listener) Registers a function to call after every dispatch. Returns an unsubscribe function.
store.replaceReducer(next) Swaps the root reducer. Used for code splitting and hot reloading.

That is all. Notice there is no setState and no way to read a subscriber's argument: the listener receives nothing, and you call getState() yourself.

Creating a store

The original way is createStore(reducer, preloadedState?, enhancer?) from the redux package. In Redux 5 it is marked deprecated in favour of configureStore from Redux Toolkit; it still works, and a legacy_createStore alias exists to hide the strikethrough in editors. This lesson uses it because it shows the machinery without anything in the way.

JavaScript
// needs the redux package (read-only here)
import { createStore } from 'redux';

const store = createStore(rootReducer);
// or with state restored from somewhere (e.g. server-rendered HTML):
const store2 = createStore(rootReducer, { counter: { value: 5 } });

The second argument, preloaded state, replaces the initial state a reducer would otherwise compute. With combineReducers its keys must match the reducers. With configureStore you pass it as preloadedState: {...}.

Run a store that follows these rules. createStore is the stand-in; reducers and calls are exactly what you would write with the real package.

JS The four store methods

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

No output.

Subscription rules

subscribe has a few rules worth knowing, because they explain odd bugs:

  • The listener runs after every dispatched action, even when the state is identical. To react only to specific changes, compare values yourself (or use selectors in React-Redux).
  • The list of listeners is a snapshot taken at the start of each dispatch. A listener added or removed during a dispatch takes effect on the next dispatch, not the current one.
  • Listeners may call dispatch, but be careful: it can loop forever. Guard it with a condition.
  • A reducer must never call dispatch. Redux throws Reducers may not dispatch actions.
  • Unsubscribe when done. Forgetting to unsubscribe leaks listeners (React-Redux does this for you).
JS Subscription edge cases

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

No output.

One store, many pieces of state

New Redux users ask whether they should create several stores. The answer is no: one store per app. You divide the state with reducers (the root reducer is built from several smaller ones, as in Reducers in Depth), not with several stores. One store means one consistent state, one DevTools timeline, one Provider.

Exceptions are rare (separate micro frontends on one page, for example, may each own a store).

From createStore to configureStore

Modern projects create the store with Redux Toolkit:

JavaScript
// needs @reduxjs/toolkit (read-only here)
import { configureStore } from '@reduxjs/toolkit';

export const store = configureStore({
  reducer: { counter: counterReducer, todos: todosReducer },
  preloadedState: undefined, // optional
});

// the type of the whole state, for TypeScript projects
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;

The returned store has the same four methods. Extra behaviour (thunk middleware, DevTools, development checks) is wired in for you. Redux Toolkit covers the options.

Middleware and enhancers: a preview

createStore's third argument and configureStore's middleware and enhancers options let you wrap or extend the store. Middleware intercepts each dispatched action before it reaches the reducer, which is how thunks, logging and analytics work; an enhancer can wrap the store itself (the DevTools use one). You will not need either yet; know only that they extend dispatch, not the reducer.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does store.subscribe(listener) return?

    subscribe returns an unsubscribe function. Call it to stop receiving notifications.
  2. 2. When is a subscribed listener called?

    The store notifies after every dispatch. Comparing old and new values is up to the subscriber or the selector.
  3. 3. What happens if a reducer calls store.dispatch?

    Dispatching inside a reducer is forbidden; side effects and further dispatches belong outside reducers.
  4. 4. How many stores should a typical Redux app have?

    Use a single store and divide the state with combined reducers.

Summary

  • A store has four methods: getState, dispatch, subscribe and replaceReducer.
  • subscribe returns an unsubscribe function; listeners run after every dispatch and use a snapshot per dispatch.
  • Reducers must never dispatch; listeners may, with care.
  • createStore is deprecated in Redux 5; configureStore from Redux Toolkit is the standard replacement with the same store API.
  • Use one store and split state by reducers, not by stores.

Next: Combining Reducers and State Shape