Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 41 ยท Redux Toolkit

configureStore in Redux Toolkit

Learn configureStore options: reducer, middleware with getDefaultMiddleware, devTools, preloadedState and enhancers. See the default middleware and dev checks work in live stand-ins.

configureStore is Redux Toolkit's replacement for createStore. In one call it creates the store, combines your reducers, attaches the thunk middleware, adds development checks, and connects the Redux DevTools. For most apps the only option you ever pass is reducer.

configureStore comes from @reduxjs/toolkit, so real calls below are read-only. The runnable examples use a small stand-in that reproduces the parts we can show: the reducer map, a middleware pipeline with a thunk, and development checks.

The basic call

JavaScript
// needs @reduxjs/toolkit (read-only here)
import { configureStore } from '@reduxjs/toolkit';
import todosReducer from '../features/todos/todosSlice';
import filtersReducer from '../features/filters/filtersSlice';

export const store = configureStore({
  reducer: {
    todos: todosReducer,
    filters: filtersReducer,
  },
});

If reducer is an object, Toolkit passes it to combineReducers for you (see Combining Reducers). If it is a function, it is used as the root reducer as it is, which is handy when you wrap it (for the logout reset, say). The returned store has the usual getState, dispatch and subscribe.

Options at a glance

Option Purpose
reducer An object of slice reducers, or one root reducer function. Required.
middleware A callback (getDefaultMiddleware) => array to customise the middleware list.
devTools true, false or an options object; defaults to on in development.
preloadedState Initial state, e.g. restored from storage or server-rendered HTML.
enhancers A callback (getDefaultEnhancers) => array to add store enhancers.

The default middleware

getDefaultMiddleware() returns the standard list. In development it contains three things; in production only the first:

  1. Thunk: lets you dispatch functions for async logic (covered later in the course).
  2. Immutability check: throws if state is mutated between dispatches.
  3. Serializability check: warns when an action or the state holds a non-serializable value.

The checks warn in the console when they take long (more than 32 ms), because on a large state they cost time. They are removed in production builds.

Adding your own middleware

In Redux Toolkit 2 the middleware option is a callback. Never write middleware: [logger] โ€” that would replace the defaults and lose thunk and the checks. Use the callback and .concat() (add at the end) or .prepend() (add at the start):

JavaScript
import { configureStore } from '@reduxjs/toolkit';
import { logger } from './middleware/logger';

const store = configureStore({
  reducer: rootReducer,
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware().concat(logger),
});

// customise the defaults themselves
const store2 = configureStore({
  reducer: rootReducer,
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: { ignoredActions: ['upload/fileSelected'] },
      immutableCheck: false, // turn one check off (not recommended)
      thunk: true,
    }),
});

The order matters: each middleware wraps the next one. .prepend() puts yours before the defaults, so it sees the raw action first. The next example uses a stand-in configureStore with a middleware pipeline, so you can see the order of calls and a thunk at work.

JS Middleware order and a thunk

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

No output.

Notice that the thunk is intercepted by the thunk middleware, which sits before the loggers in the list (the defaults come first), so the loggers never see the function itself, only the plain actions it dispatches. The middleware chapter later in the course goes further.

Development checks in action

The immutability and serializability checks are middleware, too. This stand-in shows what they catch:

JS What the development checks catch

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

No output.

When you see these messages in a real app, read them as friendly bug reports: they point to the path of the mutated value or the key of the bad action.

devTools, preloadedState and enhancers

JavaScript
const store = configureStore({
  reducer: rootReducer,
  devTools: process.env.NODE_ENV !== 'production', // the default behaviour
  preloadedState: loadSavedState(),                // must match the reducer shape
});

devTools: false removes the connection; an object (name, maxAge, trace, actionSanitizer...) customises it, as in Redux DevTools and Time Travel. The enhancers option is a callback too: (getDefaultEnhancers) => getDefaultEnhancers().concat(myEnhancer). You will rarely need it.

Typing the store

In a TypeScript project, export two types from the store file and use them everywhere:

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

RootState is inferred from your reducers; AppDispatch includes thunk support. See Organizing Redux Code for the typed hooks that use them.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does configureStore do when reducer is an object?

    An object of slice reducers is combined automatically. A function is used as the root reducer directly.
  2. 2. How should you add a custom middleware in Redux Toolkit 2?

    The middleware option is a callback. Passing a plain array would drop thunk and the development checks.
  3. 3. Which is NOT part of the default middleware in development?

    Toolkit does not add a logger. You can add one with concat; the DevTools show the action log.
  4. 4. What does the immutability check do?

    It detects in-place mutation, the most common Redux bug, and names the path of the change.

Summary

  • configureStore({ reducer }) builds a store with thunk, DevTools and development checks.
  • reducer can be an object of slice reducers or a single root reducer function.
  • Customise middleware only through the callback and concat or prepend, never with a bare array.
  • The immutability and serializability checks exist only in development.
  • devTools, preloadedState and enhancers handle debugging, restored state and rare store extensions.

Next: createSlice