Sub topic 1
When you need global state, and when you don't
Prerequisites
This chapter assumes you know JavaScript modules, destructuring and spread syntax. It also assumes you can write React function components with props and the useState and useEffect hooks. You should understand immutability and pure functions at a basic level. No Redux knowledge is needed.
The problem global state solves
Most React state belongs in the component that uses it. State starts to hurt when many distant components need the same data. The usual first fix is to lift state up to a common parent and pass it down through props. That works until the props pass through components that never use them. This is called prop drilling.
Global state is a single place, outside the component tree, that any component can read from and write to. Redux Toolkit (RTK) is the standard way to build such a place with Redux.
Where state can live
Different kinds of state have different homes. Choosing the right one is the main skill in this chapter.
| Kind of state | Example | Usual home |
|---|---|---|
| Local UI state | Whether a dropdown is open | `useState` in the component |
| Shared by a few nearby components | A form's field values | Lifted state in a common parent |
| Rarely changing, widely read | Theme, current locale | React context |
| Server data | A list of todos from an API | A cache such as RTK Query |
| Client state shared across the app | Shopping cart, multi-step wizard progress | A global store such as Redux |
Server data deserves a note. It is owned by the server, and your app only holds a copy that can go stale. Storing it by hand in a global store means you must write loading flags, error handling and refetch logic yourself. Later chapters show how RTK Query handles this for you.
Signs you need a global store
Consider Redux when several of these are true:
- Many components in different parts of the tree need the same state.
- The state changes often, and many components must react to each change.
- The rules for updating the state are non-trivial and you want them in one place.
- You want to see a history of what happened, for example to debug with Redux DevTools.
- The app is large enough that several developers need a predictable structure.
Signs you don't
Skip Redux, or delay it, when these are true:
- The state is used by one component or a small subtree.
- The state is a copy of server data. Use a data-fetching tool first.
- The value changes rarely and is only passed down. Context is enough.
- The app is small and prop passing is still readable.
A common mistake is putting every piece of state in the store "to be consistent". Form inputs and open/closed flags usually do not need to be global. Keeping them local makes components easier to reuse and test.
Context is not a replacement for a store
Context and Redux are often confused. Context is a way to pass a value down without props. It does not manage updates or give you a place to put update logic. Every component that reads a context value re-renders when that value changes. React-Redux lets components subscribe to just the slice of state they select, so they re-render only when that slice changes.
A decision flow
Worked example
Take a shop app with these pieces of state:
- A product list loaded from
/api/products. - Whether the filter sidebar is open.
- The items in the cart, shown in the header, on the product page and at checkout.
- The current theme.
The product list is server data, so it goes in a cache. The sidebar flag is used by one component, so useState is enough. The theme rarely changes, so context works. The cart is read in three places and updated by several actions, such as adding, removing and changing quantity, so it is a good fit for Redux. Only one of the four pieces ends up in the store.
Exercise
A page shows a list of orders fetched from an API, a "show archived" checkbox that only affects that list's component, and a logged-in user's permissions that ten components check and that change through several actions. Which of these belong in a Redux store?
Show answer
Only the permissions. The orders are server data and fit a cache such as RTK Query. The checkbox is local UI state, so useState is enough. The permissions are read widely and updated by several actions, which is the case a global store handles well.