Sub topic 1
When you need global state, and when you don't
This chapter helps you decide whether a piece of state belongs in a global store such as Redux Toolkit (RTK), or somewhere simpler. It comes first because most Redux problems in real apps come from putting too much into the store, not from using Redux incorrectly.
Prerequisites
This chapter assumes you already know:
- JavaScript fundamentals, including ES modules, destructuring and spread syntax.
- React function components, props, and the
useStateanduseEffecthooks. - The basic idea of immutability (not changing a value in place) and pure functions (the same input always gives the same output).
- How to run a React project with npm.
You do not need to know Redux yet. Later chapters introduce the store, slices and hooks.
What "global state" means
State is data that changes over time and affects what the user sees. React gives each component its own state through useState. That state is local: only the component that owns it, and the components it passes it to as props, can see it.
Global state is state that many unrelated parts of the app need to read or change. It lives outside any one component. In Redux it lives in a single object called the store, and any component can read from it.
Global state is not a different kind of data. It is a placement decision. The same piece of data, such as a selected filter, could be local in one app and global in another.
The four places state can live
Before reaching for a store, check the cheaper options in order.
| Place | Good for | Example |
|---|---|---|
| Local component state (`useState`) | Data used by one component | Whether a dropdown is open |
| Lifted state (parent component) | Data shared by a few nearby components | Selected tab shared by a tab bar and a panel |
| URL | Data that should survive reloads and be shareable | Current page, search query, selected item id |
| Global store (Redux) | Client data many distant components need, with complex updates | Shopping cart, signed-in user, app-wide filters |
A fifth category is server data, which is data that is owned by a backend and only cached in the browser. It has its own tools, covered below and in the RTK Query chapter.
A decision flow
Work through these questions for each piece of state.
Treat this as a default order, not a law. If lifting state forces you to pass props through six layers of components that do not use them, that is a sign the state may deserve a wider scope.
Signs you need global state
Redux is worth its cost when several of these are true:
- Many components in different parts of the tree need the same data.
- The data changes often, and the update logic is non-trivial (for example, adding an item to a cart, applying a coupon and recalculating totals).
- You need to know why and when state changed, for debugging or auditing.
- State must be read or updated from outside React, such as from a timer, a WebSocket handler or a utility module.
- Several different events should change the same state in a consistent way.
- The app is large enough that a team benefits from one predictable pattern for updates.
The last two points are where Redux gives the most. Every change goes through a described event called an action, so you can log, replay and inspect changes with Redux DevTools. You will meet actions in the next chapter.
Signs you do not need it
Redux is probably the wrong tool when:
- The state is only used by one component or a small branch of the tree.
- The state is form input that is only submitted once. Local state or a form library is simpler.
- The state is a UI detail, such as hover, focus, or whether a modal is open in one place.
- The data comes from an API. It needs caching, loading flags and refetching, not hand-written reducers.
- The app is small and the only reason is that "real apps use Redux".
Adding a store has a cost. You write more files, learn more concepts, and add another place where bugs can hide. If the benefit is small, the cost is not worth paying.
Server state versus client state
The most common mistake is storing fetched API data in Redux as if it were client state. The two behave differently.
| Row | Client state | Server state |
|---|---|---|
| Owner | Your app | A backend |
| Source of truth | The browser | The server |
| Can become out of date | No | Yes, other users may change it |
| Needs caching, refetching, deduplication | No | Yes |
| Typical tools | `useState`, Redux slices | RTK Query, other fetching libraries |
If you store server data in a slice by hand, you must also write the code for loading flags, error handling, refetching, and avoiding duplicate requests. That is a lot of repeated work, and RTK Query solves it for you. A later chapter covers it. A good rule is to use Redux slices for client state and a fetching cache for server state.
Alternatives to Redux
Redux is not the only global option. Knowing the others helps you choose.
- React context. Context passes a value down the tree without props. It is built in and fine for rarely changing values such as a theme or the current locale. It is not a state manager. When a context value changes, every component that reads that context re-renders, and context has no built-in tools for updates, debugging or derived data.
useReducerplus context. This gives you the reducer pattern without a library. It works for a contained feature, but you must build your own selectors and tooling.- Other state libraries. Zustand, Jotai and similar libraries offer smaller APIs. They are reasonable choices. Redux Toolkit's strengths are its conventions, its DevTools and its built-in solutions for async logic and data fetching.
A worked example: choosing placement
Imagine a shopping app with these pieces of state. Here is where each one would reasonably go.
| State | Placement | Reason |
|---|---|---|
| Is the "size guide" popup open | `useState` in the product page | Only one component cares |
| Text typed in the search box before submit | `useState` | Not needed elsewhere until submitted |
| Submitted search query and page number | URL | Users can bookmark and share it |
| Product list from the API | RTK Query | Server state that needs caching |
| Items in the cart | Redux slice | Header badge, cart page and checkout all read it, with many update rules |
| Signed-in user profile | Redux slice or a query cache | Many distant components need it |
Notice that only some of this state ends up in Redux. A healthy Redux app does not put everything in the store. Redux's own documentation says you decide which state to keep there, and that it is fine to mix global state with local component state.
Common mistakes
Watch for these patterns:
- Putting everything in the store. Each field you add is more code to maintain. Start local and move state up only when you feel the pain.
- Copying props into the store. If a value already comes from a parent, a second copy in the store can fall out of sync.
- Mirroring server data by hand. Use a data fetching tool instead.
- Storing values that can be computed. If a total can be calculated from the cart items, derive it with a selector instead of storing it. You will learn selectors in a later chapter.
- Using Redux because of re-render worries. Measure first. Many apps never have a performance problem that a store would fix.
What the rest of the tutorial covers
If you decide a piece of state is global, the next chapters show how to build it. You will learn the Redux model, write slices, configure a store, connect React components, derive data, and handle async work and server data. The goal is not to use Redux everywhere. It is to use it where it earns its place.
Exercise
A recipe app has these pieces of state: (1) whether the "share" menu on one recipe card is open, (2) the list of recipes loaded from an API, (3) the list of ingredients the user has added to a shopping list, shown in the header badge, a sidebar and a checkout page, and (4) the current search term, which users want to bookmark. For each, choose the best place: local state, URL, server cache, or Redux slice, and give one reason.
Show answer
(1) Local state with useState, because only one card uses it. (2) A server cache such as RTK Query, because it is server-owned data that needs caching, loading flags and refetching. (3) A Redux slice, because several distant components read it and it has several update rules such as add, remove and clear. (4) The URL, because it must survive reloads and be shareable through a link.
Exercise
A teammate says: "Our theme (light or dark) changes about once a week per user. Let's put it in Redux because it is global." Give one argument for and one against, and say which you would choose for a small app.
Show answer
For: many components across the app need it, and a store gives one place to read and change it, with DevTools to inspect changes. Against: it changes rarely and has trivial update logic, so React context (or a single piece of local state at the root) does the job with no extra library and no extra concepts. For a small app, context is the better choice, since the extra cost of a store is not repaid by this one value. Moving it into Redux later is easy if the app grows.