Redux & Redux Toolkit Course
Redux & Redux Toolkit
/
Advanced

Normalized State

Definition

A database-like state structure where each item is stored uniquely under its ID in a dictionary object, and arrays of IDs are used to indicate ordering.

Explain Like I'm New

Deeply nested arrays are a nightmare to update in Redux. If you want to update Comment 4 on Post 3 by User 2, writing the immutable spread operators is horrific. Normalization flattens everything. You just do `state.comments['comment4'].text = 'New Text'`.

Real World Example

Using `createEntityAdapter` to automatically normalize incoming array data from an API into a dictionary of `{ ids: [], entities: {} }`.

Common Use Cases

  • •Managing relational data
  • •Instant O(1) item lookups

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • What two keys are typically present in a normalized state slice?

intermediate

  • Why does normalizing data improve React rendering performance?

Flash Cards

Question

Two keys?

Click to reveal answer
Answer

`ids` (An array of strings for sorting) and `entities` (A dictionary object holding the actual data).

Question

Improve rendering?

Click to reveal answer
Answer

If a child component only needs one specific item, it can connect directly to Redux via `useSelector(state => state.items.entities[id])`. When other items in the list change, this specific child component does NOT re-render because its specific item didn't change.