tanstack-query-architect
Architect-level TanStack Query (React Query) guidance — server-state ownership versus client state, query key design, cache lifetime and invalidation strategy, mutation and optimistic-update contracts, and the cache bugs that surface only in production. Use when a React app fetches remote data.
- License 许可
- MIT
- 最近更新
- 2026-11-13
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- tanstack-query-architect
- license
- MIT
- metadata
- { "crusor-path": "website-architect/frontend-development-architect/react-architect/tanstack-query-architect", "crusor-domain": "website-architect", "crusor-origin": "original", "crusor-sources": "tanstack.com/query/latest/docs/framework/react/overview", "crusor-triggers": "tanstack query, react query, usequery, usemutation, server state, invalidation", "crusor-verified-on": "2026-10-30" }
- description
- Architect-level TanStack Query (React Query) guidance — server-state ownership versus client state, query key design, cache lifetime and invalidation strategy, mutation and optimistic-update contracts, and the cache bugs that surface only in production. Use when a React app fetches remote data.
- compatibility
- TanStack Query v5; React 18+
03 SKILL.md 正文
TanStack Query Architect
Server state is a cache with an owner, not application state. Everything here answers one question: who is allowed to say a cached value is stale?
When to use
- A React app fetches or mutates remote data.
- Data is duplicated across components, refetched on every mount, or goes stale silently after a write.
- Choosing between query cache, global store, and component state.
When NOT to use
- Client-only state (modal open, form draft) — that is not server state.
- Single fetch with no reuse — a plain
fetchin a loader is enough.
Decision 1 — State ownership
| State | Home |
|---|---|
| remote records, lists, pagination | TanStack Query |
| UI state (open, selected tab) | component state |
| app-wide client state (theme, session flags) | small store or context |
| form drafts | local/form library |
Rule: if it came from the network, it does not belong in Redux/Zustand. Two owners = two truths = the bug you cannot reproduce.
Decision 2 — Query keys are an API
- Keys are arrays built by one factory:
['orders', {status, page}]. - Every variable that changes the response appears in the key. Miss one and you serve the wrong cache entry.
- Keys are shared with invalidation — inventing ad-hoc strings breaks the link.
enabledgates dependent queries;selectderives without re-fetching.
Decision 3 — Freshness
| Data | staleTime |
|---|---|
| user profile, settings | minutes |
| lists that change on writes | 0–30 s + invalidation |
| reference data (countries, plans) | hours |
| live dashboards | short poll or push |
- Defaults are set once at the provider, not per hook.
- Pagination/infinite lists: keep previous data to avoid layout collapse.
- Retries: bounded, with backoff; never retry a 4xx.
Decision 4 — Mutations
- Every mutation declares what it invalidates — the write path owns cache correctness, not the read path.
- Optimistic updates require a rollback path and an id strategy for new rows.
onSuccessinvalidates; do not hand-patch caches unless you can name why.- Concurrency: last-write-wins is a decision, not an accident.
Decision 5 — Server rendering
- Prefetch on the server/loader, dehydrate, hydrate on the client; keys must be identical on both sides.
- Never let the client refetch immediately after hydration (set
staleTime).
Failure modes at scale
- The same data fetched by three hooks with three keys → three truths.
staleTime: Infinityset globally, then "data doesn't update".- Optimistic update without rollback → phantom records after a failed write.
- Query in a component that mounts 50 times → request storm.
- Cache never garbage collected because the key holds a new object each render.
Verification receipts
- Key factory reviewed; every response variable present in the key.
- Mutation → invalidation map documented per write.
- Network tab: one request per logical read, no duplicates on mount.
- Failed-write path: UI reverts, error surfaced.
- SSR hydration: no double fetch, same data on both sides.
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.