When you need global state

Intermediate · 14 min

A decision framework for server state, genuinely global client state, and state that should just stay local — before reaching for a store.

Before thisContext, and avoiding prop drillinguseReducer for complex stateCaching, invalidation, and mutations

Web Dev Simplified · 14 min

When you need global state

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Web Dev SimplifiedReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Server state — anything that originates from an API and can change outside your app — belongs in a query cache like TanStack Query, not in a global store, because a store doesn't know when the server's copy has gone stale.
  • Genuinely global client state is state that many unrelated parts of the tree need and that doesn't belong to any one component: theme, auth session, a multi-step wizard's in-progress answers.
  • Most state that looks like it needs to be global is actually just state that needs to be lifted to a shared ancestor and passed down, or left local to the one component that uses it.
  • Context plus useReducer can model global client state without a third-party library, but every value in a context re-renders every consumer on each change, which makes it a poor fit for state that updates frequently.
  • The question to ask before reaching for a store is not 'could multiple components use this' but 'do multiple, distant components actually need this right now, and does it change often enough that re-render cost matters'.
  • Defaulting to a global store for everything recreates, at the state-management layer, the exact problem a dedicated data-fetching library solves at the data layer: one shared blob that's hard to reason about and easy to make stale.

Three kinds of state, not one

The previous three lessons dealt with one specific kind of state: data that lives on a server and that your app merely reflects. That’s server state, and TanStack Query’s cache is the right home for it — it already handles staleness, refetching, and invalidation, and putting the same data in a global store would mean solving those problems twice, badly.

But “state” in a React app is not one thing, and the mistake this lesson is about is treating every piece of it the same way. There are really three categories:

  1. Server state. Owned by the backend; your component just displays a cached, possibly-stale copy. Covered by the last three lessons.
  2. Local or lifted state. Owned by one component, or shared by a small group of nearby components via useState/useReducer and a common ancestor, exactly as you’ve been doing since the state-and-events group of this roadmap.
  3. Global client state. Owned by the application as a whole, read from many unrelated places, and not derived from a server response at all — theme, the current authenticated session, the in-progress answers of a multi-step wizard that spans several routes.

Only the third category is what this lesson means by “global state,” and it’s the smallest of the three in most real apps.

The test that actually matters

It’s tempting to reach for a global store the moment you think “something else might need this.” Almost anything might, eventually. That question overpredicts.

A better pair of questions:

  • Does this genuinely belong to more than one distant part of the tree right now — not hypothetically, but in the component tree as it exists today?
  • Does it change often enough that re-render cost is a real concern, or is it set once and read rarely (like a feature flag)?

A shopping cart’s contents, read by a header badge, a cart drawer, and a checkout page that don’t share a close ancestor: that’s global. A single <SearchBox>’s current input text, even though in principle a parent could want to know it: usually not global — lift it to the nearest shared ancestor if a sibling needs it, and leave it local otherwise.

// Not global state — this is one form's business.
function SearchBox() {
  const [query, setQuery] = useState("");
  return <input value={query} onChange={(e) => setQuery(e.target.value)} />;
}

// Genuinely global — read by unrelated, distant components.
const ThemeContext = createContext("light");

function App() {
  const [theme, setTheme] = useState("light");
  return (
    <ThemeContext.Provider value={theme}>
      <Header />
      <Sidebar />
      <Main />
    </ThemeContext.Provider>
  );
}

Context can model this — with a real limitation

You already have a tool for global client state: Context, usually paired with useReducer for anything more structured than a single value, as covered earlier in this roadmap. For state that’s set rarely and read widely — theme, locale, a feature flag — that combination is often enough, and reaching for a third-party library would be overkill.

The limitation is re-renders. A context has one value, and every component that calls useContext on it re-renders whenever that value changes, with no way to subscribe to just a slice of it. That’s fine for a theme that flips twice a session. It’s a real problem for, say, a shared cursor position that updates sixty times a second — every consumer re-renders on every update, whether it cares about that particular change or not.

That limitation is exactly what the next two lessons address: Zustand and Redux Toolkit both let components subscribe to a slice of global state and only re-render when that slice changes, which Context cannot do on its own.

What the video covers

Web Dev Simplified’s video builds almost exactly this decision tree live: prop drilling versus global state versus Context versus a reducer versus state that belongs in the URL, landing on a dedicated “When To Use State Management Libraries” chapter around the 11-minute mark. Its central claim — that most apps reach for a state management library far earlier than they need to — is the same claim this lesson makes. It doesn’t separate out “server state” as its own named category the way this lesson does; treat the server-state material from the earlier lessons in this group as the missing third leg of the framework it presents.

Where this fits

The next lesson is Zustand and minimal state stores, which picks up exactly where Context’s re-render limitation leaves off.

Check yourself

  1. 01A component fetches a list of orders from an API and stores it in a global Zustand store so other components can read it. What's the problem with this, according to the framework in this lesson?

    The list of orders is server state, not client state — it can go stale the moment something else changes it on the server, and a plain global store has no mechanism to know that or refetch it. That data belongs in a query cache like TanStack Query, which already solves staleness, not in a store built for client-owned state.

  2. 02What is an example of state that is genuinely global client state, as opposed to state that merely looks like it might be needed elsewhere?

    The current theme (light or dark) or the logged-in user's session are good examples — they are read by components scattered across the entire tree with no single natural owner, and they aren't derived from a server response that a query cache already tracks. A single form's input value, by contrast, usually belongs to that form alone.

  3. 03Why is Context a poor fit for state that changes on every keystroke, even though it can technically hold any value?

    Every consumer of a context re-renders whenever the context's value changes, with no built-in way to subscribe to only part of it. State that updates frequently, held in a context, causes every component reading that context to re-render on every update, which is the re-render cost this lesson warns about.

  4. 04Before adding a global store, what question should you ask instead of 'could more than one component use this'?

    Whether multiple, meaningfully distant components actually need this value right now, and whether it changes often enough that the re-render cost of a shared store matters — because almost any piece of state could theoretically be read elsewhere, but most state is used by one component or a small, nearby group, where lifting state up or keeping it local is simpler and cheaper.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark