Sub topic 8
Choosing what goes in Redux and shaping the state
This chapter assumes you can write a slice with createSlice, read state with useSelector, and know why global state exists. Here you decide what belongs in the store and how to lay it out.
Not everything belongs in Redux
Putting all state in Redux is a common mistake. Each value you add means more actions, more reducers, and more places to read. Ask these questions in order:
- Do many distant components need this value?
- Does other logic need to react to changes in it?
- Do you want to inspect it, replay it, or restore it later?
- Is it a copy of data that lives on a server?
Yes to the first three points toward Redux. Yes to the fourth points toward RTK Query, which caches server data for you.
| State | Best home |
|---|---|
| Text in a form field being typed | `useState` in the component |
| Whether a dropdown is open | `useState` in the component |
| Logged-in user, theme, cart contents | Redux |
| Data fetched from an API | RTK Query, or a thunk plus a slice |
| Current page and filters | URL, or Redux if other logic reacts to them |
Shaping the state
Once a value belongs in Redux, its shape matters. Three rules cover most cases.
Store minimal data and derive the rest
Keep the source of truth in state and compute everything else with selectors. Storing both todos and completedCount invites the two to drift apart. Store todos only, and derive the count.
Keep state serializable
Store plain objects, arrays, strings, numbers, booleans and null. Avoid class instances, functions, Map, Set, and Date objects. Store dates as ISO strings. configureStore warns in development when it finds non-serializable values, because they break DevTools and persistence.
Normalize collections
Nested data is hard to update. Changing one comment inside a post inside a user means rebuilding three levels. Store each kind of item in its own lookup keyed by id, and refer to other items by id.
// Nested: updating one comment means digging through the tree
const nested = {
posts: [
{ id: 'p1', title: 'Hello', comments: [{ id: 'c1', text: 'Nice' }] },
],
}
// Normalized: each item is found directly by id
const normalized = {
posts: {
byId: { p1: { id: 'p1', title: 'Hello', commentIds: ['c1'] } },
allIds: ['p1'],
},
comments: {
byId: { c1: { id: 'c1', text: 'Nice' } },
allIds: ['c1'],
},
}With the normalized shape, editing a comment touches one entry. Looking up an item is a property access instead of a search through an array. A later chapter shows createEntityAdapter, which generates this structure and its reducers.
Organize by feature
Give each feature its own slice and name the slice after the feature, such as cart or auth. Model events rather than setters: an action called itemAddedToCart describes what happened, and the reducer decides how state changes. This keeps logic in reducers instead of scattered across components.
Exercise
A shopping app has these values: the text in a search box being typed, the list of products from /api/products, and the items in the user's cart. Say where each should live, and name one derived value you would not store.
Show answer
The search box text belongs in useState because only one component uses it. The product list is server data, so use RTK Query. The cart contents belong in a Redux slice because many components read and change them. The cart total (or item count) should not be stored. Compute it from the cart items with a selector.