react-state-management-architect
Architect-level state strategy for React apps — deciding between local, lifted, URL, server-cache and global state, and choosing among TanStack Query, SWR, Zustand, Redux Toolkit, Jotai and Context. Use when state is being duplicated, synced by effects, or when adding a store is on the table.
- License 许可
- MIT
- 最近更新
- 2026-11-13
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- react-state-management-architect
- license
- MIT
- metadata
- { "crusor-path": "website-architect/frontend-development-architect/react-architect/react-state-management-architect", "crusor-domain": "website-architect", "crusor-origin": "original", "crusor-sources": "react.dev/learn/managing-state; tanstack.com/query; zustand docs; redux-toolkit docs; vercel-labs/agent-skills@063bee94 (react-best-practices)", "crusor-triggers": "react state, client state, state ownership, zustand, jotai, context vs store, url state, app state, server state ownership", "crusor-verified-on": "2026-10-30" }
- description
- Architect-level state strategy for React apps — deciding between local, lifted, URL, server-cache and global state, and choosing among TanStack Query, SWR, Zustand, Redux Toolkit, Jotai and Context. Use when state is being duplicated, synced by effects, or when adding a store is on the table.
- compatibility
- React 18+; framework-agnostic
03 SKILL.md 正文
React State Management (decision skill)
There is no "React state library" problem. There are five kinds of state, and most architecture failures come from putting one kind in another kind's home.
When to use
- Before adding any store, context or query client.
- When a value needs
useEffectto stay in sync with another value. - When two screens disagree about the same entity.
- When the team cannot answer "who owns this state?"
When NOT to use
- A value used by one component: it is
useState, decision over. - A styling or rendering change with no new state.
Decision 1 — The five homes
| Kind | What it is | Home |
|---|---|---|
| Local UI | open/closed, hover, draft text | useState in the owning component |
| Lifted | shared by siblings | nearest common parent |
| URL | filters, tab, page, selected id, query | router search params |
| Server | anything the backend is the source of truth for | TanStack Query / SWR / RSC |
| Global client | cross-feature, session-lifetime UI or drafts | Zustand / Redux Toolkit / Jotai |
Decision rule: server data is a cache, not state. If the server owns it, a client store must not also own it.
Decision 2 — Choosing the library
| Need | Use | Why not the others |
|---|---|---|
| Server data, caching, revalidation | TanStack Query (or SWR if minimal) | a global store gives you no invalidation, dedupe or staleness model |
| Small global client state, minimal ceremony | Zustand | Redux's ceremony buys nothing at this size |
| Large app, devtools/audit, event-sourced updates | Redux Toolkit | Zustand gives no action log or enforced reducer boundaries |
| Many independent atoms, fine-grained subscriptions | Jotai | one big store over-renders or needs manual selector discipline |
| Values that rarely change (theme, locale, auth user) | Context | context re-renders every consumer on change |
Two libraries maximum. A codebase with a query client, a store and a context holding the same entity has three sources of truth and zero owners.
Decision 3 — Boundaries
- Query owns the entity lifecycle (fetch, cache, invalidate). Components read from the query cache; mutations invalidate tags/keys.
- Store owns client-only facts: selected rows, unsaved draft, panel layout, wizard step. It must be serialisable and must not contain server payloads.
- URL owns anything shareable — if refreshing should preserve it, it belongs in the URL, not the store.
- Context owns dependency injection (theme, i18n, client instances), not application data.
Rules that prevent the usual rewrite
- Derive, don't sync. If B is computed from A, compute it during render.
useEffectmirroring is the signature of a missing derivation. - One writer per fact. A fact written by two layers will eventually disagree.
- No fetching in components when a query hook exists. Otherwise you get duplicate requests and per-component loading states.
- Select narrowly. Subscribe to the field, not the object; shallow-compare only when the shape is truly stable.
- Normalise only when there is a real need (same entity in several views, or updates that must propagate). Otherwise keep server data in the query cache untouched.
- Store shape is part of the public contract. Renaming a slice is a migration, not a refactor.
Failure modes
- Query cache + Zustand + local state holding the same list — three copies, one refresh path, guaranteed drift.
- A global store created for a modal's open flag.
- Context provider at the root holding a rapidly changing value; every consumer re-renders on every keystroke.
- Optimistic update with no rollback path.
- Storing derived booleans (
isValid,hasChanges) instead of the inputs.
Anti-patterns
useEffect(() => setLocal(serverValue), [serverValue]).useState+useEffectfetch in a codebase that already has a query client.- Redux for a form; the form library already owns that state.
- Passing setters four levels down instead of lifting, or lifting when the value belongs in the URL.
- A store whose actions call
fetchdirectly, bypassing the cache layer.
Verification receipts
- A refresh of the page preserves everything that lives in the URL.
- A mutation invalidates and refetches exactly the affected query keys (log the keys, do not assume).
- Render-count check: typing in an input does not re-render unrelated screens.
- Devtools/state dump shows one copy of each entity.
- Tests: the query hook's loading/error/empty paths, not just success.
04 同仓库更多 Skills
interaction-patterns-architect
Architect-level interaction design in code — choosing established patterns over invented ones, feedback and latency behaviour, destructive-action safeguards, navigation and overlay conventions, and the state model behind every interactive surface. Use when a screen has to behave, not just look.
design-system-ops-architect
Architect-level operation of a design system in code — token pipelines and versioning, component API and variant policy, the catalogue as the contract, adoption metrics, and the governance that stops a system from forking into three. Use when a component library must survive several teams.
design-handoff-architect
Architect-level design handoff — what must be specified before implementation starts, tokens over raw values, responsive and content-limit contracts, state matrices, and the review loop that catches drift between design and code. Use when a design is about to become code.
content-microcopy-architect
Architect-level UI content — where strings live, voice and terminology consistency, error and empty-state copy as part of the interface, translation-ready authoring, and the content model a product needs before it multiplies screens. Use when the words in the interface matter as much as the pixels.
ui-ux-implementation-architect
Architect-level direction for turning a design into working UI — the handoff contract, component ownership versus screen-level layout, interaction and state conventions, design QA gates, and the review habits that keep an implementation faithful without frozen pixels. Use before any specific UI implementation skill.
test-strategy-architect
Architect-level test strategy — allocating assertions across unit, integration, contract and end-to-end layers, designing for testability through dependency boundaries, fixture and data strategy, and the flake and coverage policies that keep a suite trustworthy. Use when defining how a codebase is tested.