Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 41 ยท Redux Toolkit

Why Redux Toolkit?

See why Redux Toolkit is the official way to write Redux. Compare hand-written Redux with createSlice, tour what RTK includes and learn what runs in our editor and what needs the real package.

Everything you wrote in the last chapter works, and it is real Redux. It is also verbose. For every feature you wrote action type strings, action creators, a switch with careful spread copies, and a store with several lines of setup. Redux Toolkit (RTK) was created by the Redux team to take away that repetition and to make the right choices by default.

Today the Redux documentation tells everyone to use Redux Toolkit. Plain createStore is deprecated, and Toolkit is not a different library: it is Redux plus a set of helpers, written by the same maintainers.

Real vs stand-in. @reduxjs/toolkit cannot be loaded in our editor, so the Toolkit code in this chapter is either read-only or runs on short stand-ins (labelled in comments) for createSlice, createAction and configureStore. The stand-ins copy the real API shapes and behaviour; real Toolkit adds Immer, development checks and TypeScript support.

The problems Toolkit solves

Early Redux users complained about three things:

  1. "Configuring a Redux store is too complicated." DevTools, middleware and enhancers each needed manual wiring.
  2. "I have to add a lot of packages to get Redux to do anything useful." Async logic, immutable updates and selectors were all separate libraries.
  3. "Redux requires too much boilerplate code." Action constants, creators and switch statements for a one-line change.

Toolkit's answer, in order: configureStore, a curated bundle of tools, and createSlice.

What is in the box

Setup and logic

configureStore A store with good defaults.
createSlice State, reducers and actions together.
Immer Write "mutating" code safely.
1 / 4

This chapter covers configureStore, createSlice, Immer, createAction and createReducer. The async tools and RTK Query come later in the course.

Before and after

Here is a counter with an incrementByAmount action, written two ways. First, hand-written Redux:

JavaScript
// Hand-written Redux: 3 constants, 3 creators, 1 switch
const INCREMENTED = 'counter/incremented';
const DECREMENTED = 'counter/decremented';
const AMOUNT_ADDED = 'counter/amountAdded';

export const incremented = () => ({ type: INCREMENTED });
export const decremented = () => ({ type: DECREMENTED });
export const amountAdded = (n) => ({ type: AMOUNT_ADDED, payload: n });

export default function counterReducer(state = { value: 0 }, action) {
  switch (action.type) {
    case INCREMENTED: return { ...state, value: state.value + 1 };
    case DECREMENTED: return { ...state, value: state.value - 1 };
    case AMOUNT_ADDED: return { ...state, value: state.value + action.payload };
    default: return state;
  }
}

The same feature with createSlice (read-only, needs @reduxjs/toolkit):

JavaScript
import { createSlice } from '@reduxjs/toolkit';

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

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

No constants, no creators, no switch, no default, no spreads. The generated action types are counter/incremented and friends, identical to the hand-written ones, so the two versions can be swapped without changing the rest of the app. The next example proves it with stand-ins:

JS Hand-written and slice reducers behave the same

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

No output.

Safer by default

configureStore adds development-only checks that catch the two most common Redux bugs:

  • Immutability check: throws if state is mutated between dispatches, in a reducer or elsewhere.
  • Serializability check: warns about functions, class instances, promises and other non-serializable values in actions or state.

It also adds the thunk middleware and connects the Redux DevTools. These checks cost performance, so they are disabled in production builds.

The whole picture

flowchart LR
    CS["createSlice"] -->|"slice.reducer"| CF["configureStore"]
    CS -->|"slice.actions"| UI["Components"]
    CF -->|"store"| PR["Provider"]
    PR --> UI
    CF --- MW["Thunk + dev checks + DevTools"]

A slice produces a reducer for the store and action creators for components. The store arrives with sensible middleware already attached.

Is Toolkit "real Redux"?

Yes. Toolkit's output is a standard Redux store; every Redux feature, extension and DevTools tool still works. You can adopt it gradually: configureStore accepts old hand-written reducers, and createSlice reducers can sit next to them. Everything you learned in Core Concepts still applies. Toolkit just writes the repetitive parts.

A second honest point: Toolkit does not make your design decisions for you. You still choose the state shape, name events well and keep reducers pure.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is Redux Toolkit?

    RTK is made by the Redux maintainers and is the recommended way to use Redux. Its stores are standard Redux stores.
  2. 2. What does createSlice generate for you?

    createSlice returns slice.reducer and slice.actions; types are named sliceName/reducerName.
  3. 3. Which development checks does configureStore enable?

    It adds middleware that detects state mutation and non-serializable values in development.
  4. 4. Can hand-written reducers be used together with createSlice reducers?

    A slice reducer is an ordinary Redux reducer, so migration can be gradual.

Summary

  • Redux Toolkit is the official, recommended way to write Redux; it is built on the same core.
  • It fixes three complaints: complicated store setup, extra packages and boilerplate.
  • createSlice replaces action types, creators and the switch; configureStore adds thunk, DevTools and dev checks.
  • Output is a normal Redux store, so adoption can be gradual.
  • In our editor Toolkit code is read-only or runs on labelled stand-ins.

Next: configureStore