الانتقال إلى المحتوى
FrontendState Management· 7 دقائق للقراءة

Not All State Belongs in a Global Store

A store makes data easy to reach from anywhere. It also keeps it alive for the whole session, which local state never needed.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • React 19
محتويات المقال

A shop's admin app keeps all of its state in one store, so there's one place to look. Then a support ticket arrives. An admin opened "Edit price" for a blue mug, typed 12, got distracted and closed the dialog. An hour later she opened the same dialog for a teapot, and it already said 12. She saved without looking, and the teapot now costs 12. Nothing crashed. The dialog's draft was a single priceDraft field in the store, and the store had kept it for as long as the tab was open.

The common misconception

The belief: if all state lives in one global store, the app gets simpler. Any component can read anything without passing props, there's one place to debug, and nobody has to decide where a value belongs.

Part of it is true, which is why good teams adopt it. A store really does end prop drilling for data that many distant components need, like the signed-in user or the cart, and Redux's own FAQ notes that some developers keep every piece of data there. But the same FAQ says there's no single right answer and that using local component state is fine. What the belief misses is that moving a value into a store changes more than where it's read from. It changes who owns the value and how long it lives.

What actually happens

Every piece of state has an owner and a lifetime

React's docs ask you to choose, for each unique piece of state, the component that owns it. They call that a and point out that it doesn't mean all state lives in one place. Ownership also sets the lifetime. React keeps a component's state while it renders at the same position in the tree, and destroys it when the component is removed. Close the dialog and a useState draft is gone; open it for another product with a different key and it starts fresh.

A store has no idea a dialog closed. Its values live as long as the app, so each piece of local state you move there becomes something you must reset by hand, at the right moment, on every path that leaves the screen. The mug's 12 survived because nobody wrote that reset.

One field for every instance

A component can appear many times on a page: two date pickers, a menu per table column, a dialog per row. With useState, each instance gets its own copy. A store field is one value for the whole app, so two filter menus that read the same filterMenuOpen flag open and close together, unless you key the store by instance and clean those entries up yourself. Local state does that bookkeeping for free.

Every keystroke becomes an app-wide update

A draft changes on every keystroke. In a store, each one turns into an update of app-wide state, visible to every part of the app, when only one input cares. Redux's FAQ says most form state doesn't need to be in the store, since it's probably not shared, and that dispatching an action on every keystroke probably isn't worthwhile.

TSX PriceDialog.tsx
// One draft for the whole app, alive until the tab closesexport function PriceDialog({ product }: { product: Product }) {  const draft = useAppStore((s) => s.priceDraft);  const setDraft = useAppStore((s) => s.setPriceDraft);  return (    <div role="dialog">      <input value={draft} onChange={(e) => setDraft(e.target.value)} />      <button onClick={() => savePrice(product.id, Number(draft))}>        Save      </button>    </div>  );}

The fix is mostly deletion. The draft becomes useState inside the dialog, starting from the product's current price. The key gives each product its own fresh draft, and closing the dialog throws the draft away, with no reset code anywhere. Only the saved price leaves the component.

Sometimes you do want a draft to outlive its screen. React's docs use a chat app where each recipient keeps an unsent message: they lift the drafts to a parent, one per recipient, or save them to localStorage. That's a lifetime you chose, keyed by the thing it belongs to, not one shared field that happens to survive.

Pick the narrowest owner

A short ladder covers most state. Stop at the first step that fits:

  1. Can it be calculated from props or other state? Then it isn't state at all: is computed during render.
  2. Does only one component use it? Keep it in useState right there.
  3. Do a few nearby components need it? to their closest common parent.
  4. Do many distant components need it? Use context or a store. Redux's FAQ asks whether other parts of the app care, whether the same data drives several components, and whether you want to cache it or restore past states.
  5. Does the server own it? Then you hold a cached copy, which has different problems; a later article in this path covers it.
Kind of stateExampleWhere it lives
A value you can calculateCart total, filtered listComputed during render
One component's UIAn open menu, an input draftuseState in that component
Shared by nearby componentsWhich accordion panel is openTheir closest common parent
Read across the appSigned-in user, theme, cartContext or a store
Owned by the serverProducts, ordersA data-fetching cache

How to apply it in your project

  1. List who reads each store fieldFor every field in your store, search for the components that read it. A field used by one component, or by one screen, is a candidate to move down.
  2. Move single-owner state into componentsTurn it into useState or useReducer where it's used, give the component a key when it should reset per item, and delete the reset code the store needed.
  3. Delete stored copies of derived dataReplace stored totals, counts and filtered lists with calculations during render or in the function that reads them.
  4. Treat server data separatelyMark the fields that are API responses. They need caching and refetching more than a global field, and the next articles in this path cover them.

Key takeaways

  • A store makes data easy to reach, not easy to own: it ends prop drilling but keeps every value alive all session.
  • Local state disappears with its component and resets with a new key; store state must be reset by hand.
  • Choose the narrowest owner: calculate it, keep it local, lift it to a common parent, then use context or a store.
  • Keep app-wide client data like the user or the cart in a store; keep drafts, toggles and derived values out of it.

📚 المصادر

  1. Choosing the State Structure — React
  2. Sharing State Between Components — React
  3. Preserving and Resetting State — React
  4. Redux FAQ: Organizing State — Redux

التعليقات (0)