Redux Toolkit for complex apps

Intermediate · 21 min

When Redux still earns its complexity, RTK's createSlice and configureStore, and why it replaced hand-written Redux boilerplate.

Before thisWhen you need global stateuseReducer for complex stateZustand and minimal state stores

Dave Gray · 21 min

Redux Toolkit for complex apps

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Dave GrayReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Redux still earns its complexity on large teams with complex, interrelated derived state and a real need for strict conventions, time-travel debugging, and enforced structure across many contributors — not as a default choice for every app.
  • createSlice generates an action creator and an action type automatically for every reducer function you write, removing the hand-written action-type constants and switch statements that classic Redux required.
  • configureStore replaces the hand-assembled createStore plus manually wired middleware of classic Redux, setting up Redux DevTools and sensible middleware (like a check for accidental state mutation) by default.
  • Redux Toolkit lets you 'mutate' state directly inside a slice's reducer functions, because it uses Immer internally to turn those mutations into a correct, immutable update behind the scenes.
  • connect() and manually written action creators were the standard way to wire Redux into components for years; RTK plus hooks like useSelector and useDispatch replaced nearly all of that boilerplate without changing Redux's core single-store, action-based model.

Redux is a bigger commitment than Zustand on purpose

The previous lesson closed on Zustand’s deliberate lack of structure: no enforced conventions, no required middleware, minimal ceremony. That’s the right tradeoff for a cart or a theme toggle. It stops being the right tradeoff once a team gets large enough, or the state gets interrelated enough, that lack of enforced structure becomes its own problem — ten contributors each inventing their own pattern for updating a shared store is worse than one strict, shared convention, even if that convention has more boilerplate.

That’s the case for Redux, specifically: large teams, state with a lot of derived, cross-cutting logic (a dashboard where dozens of UI pieces depend on overlapping slices of the same data), and a real need for tooling like time-travel debugging — stepping backward through every state change that happened, which Redux’s strict action-based model makes possible in a way an ad-hoc store generally doesn’t.

It is not the case for most apps, including most apps with meaningful global state. If the previous lesson’s decision framework says you need global client state at all, Zustand or Context usually covers it; Redux is for when that state’s complexity and your team’s size have both grown past what minimal tooling can keep consistent.

What Redux Toolkit actually replaced

“Redux” used to mean, at minimum: string constants for action types, hand-written action creator functions that returned { type, payload } objects, a switch statement in every reducer dispatching on those strings, and manual, careful object-spreading to avoid ever mutating state directly. All of that was boilerplate required by Redux’s core idea — a single store, updated only by dispatching plain action objects to pure reducer functions — rather than being optional.

Redux Toolkit (RTK) is the now-standard way to write Redux that keeps that core idea and removes nearly all of the boilerplate around it:

import { createSlice, configureStore } from "@reduxjs/toolkit";

const cartSlice = createSlice({
  name: "cart",
  initialState: { items: [] },
  reducers: {
    addItem: (state, action) => {
      state.items.push(action.payload); // looks like a mutation
    },
    removeItem: (state, action) => {
      state.items = state.items.filter((i) => i.id !== action.payload);
    },
  },
});

export const { addItem, removeItem } = cartSlice.actions;

const store = configureStore({
  reducer: { cart: cartSlice.reducer },
});

createSlice takes a name, an initial state, and an object of reducer functions, and generates the action type strings and action creator functions for you — cartSlice.actions.addItem(product) returns the action object you used to write by hand. configureStore replaces the classic createStore plus manual middleware wiring, enabling Redux DevTools and a development-only check for accidental state mutation out of the box.

The mutation that isn’t

state.items.push(action.payload) looks like exactly the kind of direct mutation Redux has always forbidden. It’s allowed here because RTK’s reducers run through Immer, a library that lets you write mutating-looking code against a special proxy object, then produces a correct, brand-new, immutable state object from the changes you made — without you writing a single spread operator. Outside a createSlice reducer, Redux’s “never mutate state” rule still applies exactly as before; this is a sanctioned exception scoped to reducer functions, not a change to Redux’s underlying model.

Using the store from a component

Reading and updating state inside a component uses hooks, not the connect() higher-order component that classic Redux relied on before hooks existed:

import { useSelector, useDispatch } from "react-redux";
import { addItem } from "./cartSlice";

function CartBadge() {
  const itemCount = useSelector((state) => state.cart.items.length);
  return <span className="badge">{itemCount}</span>;
}

function AddToCartButton({ product }) {
  const dispatch = useDispatch();
  return (
    <button onClick={() => dispatch(addItem(product))}>Add to cart</button>
  );
}

useSelector should look familiar — it’s the same selector idea as Zustand’s hook from the previous lesson, and it has the same re-render benefit: a component only re-renders when the specific slice it selects changes. The difference is architectural, not about re-renders: Redux enforces one store, reducers as the only way to change state, and a strict separation between dispatching an action and the reducer that handles it, which is exactly the rigidity a large team benefits from and a small app usually doesn’t need.

What the video covers

Dave Gray’s tutorial opens with an explicit “Redux vs Redux Toolkit” comparison around the 1:11 mark, then builds configureStore, createSlice, and dispatching actions step by step — a clean, concrete demonstration of exactly the boilerplate reduction described above. It doesn’t make the case for when Redux is worth choosing over something lighter like Zustand in the first place; that framing is this lesson’s own addition, reinforced by the Theo video on the global-state lesson earlier in this group if you want another angle on it.

Where this fits

This is the last lesson in the data-fetching-and-state-management group. The next group moves into Suspense and concurrent rendering, starting with Suspense for data fetching, which builds directly on the query cache from earlier in this group.

Check yourself

  1. 01According to this lesson, what kind of situation makes Redux's extra complexity worth it over a simpler store like Zustand?

    A large team working across many contributors, with complex and deeply interrelated derived state, where strict conventions and strong dev-tooling like time-travel debugging genuinely pay for themselves — not a small or medium app, where that structure is mostly overhead.

  2. 02What does createSlice generate for you that classic Redux required you to write by hand?

    It generates the action creators and action type strings automatically from the reducer functions you define, removing the separate action-type constants and the switch statement that dispatched on them in hand-written Redux.

  3. 03Inside a createSlice reducer, you write state.count += 1. Why doesn't this break Redux's rule that state must never be mutated directly?

    Redux Toolkit uses Immer internally to intercept that code and translate it into a correct, immutable update under the hood, so the reducer can be written in a direct, mutating style while the actual state object Redux stores is still replaced immutably, as Redux requires.

  4. 04What problem did connect() and hand-written action creators solve in classic Redux, and what replaced them?

    connect() was how a class or function component subscribed to the store and received dispatch, since hooks didn't exist yet; hand-written action creators were functions that returned the right action object for a given action type. Redux Toolkit's createSlice generates the action creators automatically, and the useSelector and useDispatch hooks replaced connect() as the way components read from and dispatch to the store.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark