Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 41 ยท Core Concepts

Combining Reducers and State Shape

Learn how combineReducers builds a root reducer, how each slice sees only its own state, how to design a normalized state shape and how to reset the whole store on logout.

A real app has more than one kind of state: a cart, a user, filters, notifications. Putting all that logic in a single reducer would make it huge. Redux's answer is combineReducers: you write one small reducer per area, and Redux combines them into one root reducer whose output is an object with one key per area.

This lesson explains what combineReducers does (so you can reason about Redux Toolkit's configureStore, which uses it behind the scenes), then moves on to the second half of the problem: how to shape the state so it stays easy to update and read.

What combineReducers does

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

const rootReducer = combineReducers({
  cart: cartReducer,
  user: userReducer,
  filters: filtersReducer,
});
// state shape: { cart: ..., user: ..., filters: ... }

The keys you choose become the keys of the state. When an action arrives, the combined reducer:

  1. calls every child reducer with its own slice of state (state.cart goes to cartReducer, and so on) and the action;
  2. collects the results into a new object;
  3. returns the old object if every child returned the same reference, otherwise the new one.

Step 3 is a performance feature: unrelated actions do not create a new root state. It also means each child reducer sees only its slice, which keeps them small and independent.

JS A combineReducers stand-in

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

No output.

The real combineReducers also checks, in development, that you did not pass state keys with no matching reducer (it warns about "unexpected keys" and ignores them) and that every reducer returns a non-undefined value for the init action. The error message above imitates that.

Choosing the keys and the shape

The state shape is a design decision, much like database tables. A few guidelines:

  • Organise by feature (domain), not by data type. Prefer { auth, cart, products } to { objects, arrays, booleans }.
  • Keep it as flat as you can. Deep nesting makes updates and selectors painful.
  • Store each fact once. Duplicate copies drift apart.
  • Keep UI state and server data apart. Put "is the cart drawer open" in a ui or cart slice and keep records you fetched in their own slice.
  • Derive the rest. Totals, counts and filtered lists are computed with selectors.

Normalising collections

When your state holds many records that refer to each other (posts with authors, orders with products), nested data causes trouble: the same author appears in ten posts, and changing the author's name means changing ten places. The fix is to normalise: store records in a lookup object by id, and refer to them by id.

JavaScript
// nested (hard to update)
{ posts: [{ id: 1, title: 'Hi', author: { id: 7, name: 'Ada' } }, ...] }

// normalised (each fact once)
{
  posts:   { byId: { 1: { id: 1, title: 'Hi', authorId: 7 } }, ids: [1] },
  authors: { byId: { 7: { id: 7, name: 'Ada' } },              ids: [7] },
}

Updating one author or one post is now a single, shallow change, and finding a record by id is a direct lookup instead of a scan. Redux Toolkit ships createEntityAdapter to generate exactly this ids + entities structure with ready-made reducers and selectors.

JS Normalising nested data

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

No output.

Notice that both of Ada's posts show the new name, though we changed exactly one object.

One action, many reducers

Because the combined reducer sends every action to every child, several slices can react to the same action. This keeps related changes together and the component code short.

JS A single logout action resets several slices

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

No output.

The wrapper trick works because passing undefined for a slice makes its reducer return its initial state. It is the standard way to reset a store and avoid leaking one user's data to the next.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. With combineReducers({ cart, user }), what does the cart reducer receive as its state?

    Each child reducer is called with its own slice and the action. It never sees its siblings.
  2. 2. What does the combined reducer return when every child returns the same reference?

    If nothing changed, the old root object is returned, so unrelated actions do not create new state.
  3. 3. Why normalise records into byId and ids?

    Normalised state avoids duplicates, so one change updates every place that refers to the record.
  4. 4. How can one action change several slices?

    Every reducer receives every action, so any of them may react to the same type.

Summary

  • combineReducers builds a root reducer: each child gets only its slice, and the keys define the state shape.
  • If no child changes, the previous root state object is returned.
  • Design the shape by feature, keep it flat, store each fact once and derive the rest with selectors.
  • Normalise related records into byId and ids (Redux Toolkit's createEntityAdapter automates it).
  • Several reducers can respond to the same action; wrap the root reducer to reset state on logout.

Next: Why Redux Toolkit?