
Not All State Belongs in a Global Store
- كاتب موثّق
- React 19
محتويات المقال
priceDraft field in the store, and the store had kept it for as long as the tab was open.
The common misconception
What actually happens
Every piece of state has an owner and a lifetime
useState draft is gone; open it for another product with a different key and it starts fresh.
One field for every instance
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
// 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> );}import { useState } from "react";// Rendered as <PriceDialog key={product.id} product={product} />export function PriceDialog({ product }: { product: Product }) { const [draft, setDraft] = useState(String(product.price)); return ( <div role="dialog"> <input value={draft} onChange={(e) => setDraft(e.target.value)} /> <button onClick={() => savePrice(product.id, Number(draft))}> Save </button> </div> );}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.
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
Can it be calculated from props or other state? Then it isn't state at all: is computed during render. Does only one component use it? Keep it in useStateright there.Do a few nearby components need it? to their closest common parent. 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. Does the server own it? Then you hold a cached copy, which has different problems; a later article in this path covers it.
useState in that component | ||
How to apply it in your project
List who reads each store field For 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. Move single-owner state into components Turn it into useStateoruseReducerwhere it's used, give the component akeywhen it should reset per item, and delete the reset code the store needed.Delete stored copies of derived data Replace stored totals, counts and filtered lists with calculations during render or in the function that reads them. Treat server data separately Mark 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.
التعليقات (0)