Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 41 ยท Patterns and Performance

Designing and Normalizing State Shape

Learn how to design Redux state: what belongs in the store, how to normalize nested API data into lookup tables, avoid duplication, and keep derived data out of state.

A reducer is only as clear as the state it manages. A good shape makes updates one-line changes and makes selectors simple. A bad shape forces deep spreads, duplicated data and bugs where the same user shows two different names in two places. This lesson gives you rules for deciding what goes in the store and how to arrange it.

What belongs in Redux?

Not everything. A useful test: would several distant parts of the app need it, or must it survive unmounting, or should it be inspectable and replayable? Then Redux fits.

  • Good fits: the signed-in user, cart contents, data from the server that many screens use, app-wide filters.
  • Poor fits: the text of a half-typed input, whether a dropdown is open, hover state, anything one component owns. Use useState for these.
  • Server data: prefer RTK Query's cache over copying responses into slices by hand.

Redux does not require all state to be in the store. The questions to ask are "who needs it?" and "does it need to outlive the component?".

Rules of thumb for shape

  1. Store the minimum. Keep the facts, derive everything else (see selectors). Do not store fullName next to firstName and lastName, or completedCount next to the list.
  2. Keep it serializable. Plain objects, arrays, strings, numbers, booleans, null. No Date (store an ISO string or a timestamp), Map, Set, class instances, promises or functions. Serializable state is what makes DevTools, persistence and server rendering work.
  3. Avoid nesting that mirrors the UI. The state is a database, not a component tree.
  4. Do not duplicate the same entity. If two places hold a copy of one user, the copies drift apart.
  5. Treat loading and error status as state, next to the data they describe.

The problem with nested data

Typical API responses are nested: a post contains its author and its comments, and each comment contains its author. Storing that as received means the same author object appears many times.

Updating an author's name then requires walking the whole tree. In a normalized shape, the author appears once.

flowchart LR
  subgraph nested["Nested (as received)"]
    P1["post 1"] --> A1["author: Ann"]
    P1 --> C1["comment: author Ann"]
    P2["post 2"] --> A2["author: Ann"]
  end
  subgraph norm["Normalized"]
    PE["posts.entities"] --> UE["users.entities"]
    CE["comments.entities"] --> UE
  end

Normalized data stores each entity once and refers to others by id.

Normalizing by hand

Normalization means splitting nested data into one lookup table per entity type and replacing nested objects with their ids. Libraries such as normalizr automate this, but the algorithm is short enough to write yourself.

JS Normalize a nested response

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

No output.

After renaming Ann once, the post author and her comment both show the new name, because there is only one copy.

Updating normalized state

With a lookup table, an update needs only the id. Compare the two approaches for "mark todo 7 done":

JS Array scan vs lookup

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

No output.

Keeping the ids array untouched (same reference) means a list component that selects only ids does not re-render when one item's content changes.

Handling relations

  • One-to-many: the parent stores child ids (commentIds) or each child stores postId. Pick the side you query most.
  • Many-to-many: a separate "join" table or id arrays on both sides.
  • Optional: store null or omit the id, and have the selector handle missing entries (a deleted author).
  • Cascading deletes are your responsibility: when a post is removed, a reducer must remove its comments (use extraReducers to react to the same action in several slices).

Slices and the shape of the whole store

Split state by domain, not by component: auth, cart, products, ui. Each slice owns its data and exports its selectors. Cross-slice reads happen in selectors; cross-slice writes happen by several slices handling the same action. Keep UI flags (ui.sidebarOpen) separate from domain data so features can change independently.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which value is a bad candidate for Redux state?

    Date objects are not serializable; store a string or a timestamp.
  2. 2. What is the main benefit of normalizing nested data?

    Single source of truth for each entity prevents copies from drifting.
  3. 3. Where should the number of completed todos live?

    Derived data is computed, not stored.
  4. 4. Which data should usually NOT go in the Redux store?

    Local, short-lived UI state belongs to the component.

Summary

  • Put shared, long-lived, replayable data in Redux; keep local UI state in components.
  • Store the minimum, keep it serializable, and derive the rest with selectors.
  • Normalize nested data into one lookup table per entity type plus id lists.
  • Relations are ids; cascades are your reducers' job.
  • Organize slices by domain and keep UI flags apart from domain data.

Next: Immutable updates and Immer