react-query
Skill1 starsChanged 5 months agoMITView on GitHub
Manages server state on the Next.js 16 frontend with TanStack Query v5 — structured array query keys, staleTime/gcTime tuning, useQuery/useMutation with onSuccess invalidateQueries by key prefix, and the server-state vs client-state boundary. Use when fetching or caching API data, wiring a query-key factory, invalidating after a POST/PATCH/DELETE, fixing stale UI after a mutation, or deciding what belongs in Query vs Zustand. Not for the tenant-switch procedure itself and what to purge on switch (see tenant-session-switch), persisted UI/client state (see zustand-state), or typing the API payload shapes (see drf-zod-contract).
2 other projects use a version of this.
Adopted by
Most recently taken up by
- jgamaraalv/delivery-looptheir own version, first seen Jul 4, 2026
- Mindrally/skillstheir own version, first seen Jan 23, 2026
Other versions2 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 2
- Mindrally/skills★ 266same name
- jgamaraalv/delivery-loop★ 4same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository49 other files it gives its agents.
- accessibilitySkill
- ai-evalsSkill
- ai-integrationSkill
- architecture-decisionsSkill
- audit-loggingSkill
- aws-servicesSkill
- browser-e2e-testingSkill
- celery-tasksSkill
- changelogSkill
- ci-cdSkill
- codebase-guideSkill
- code-reviewSkill
- commit-messageSkill
- cookie-auth-csrfSkill
- dashboard-data-designSkill
- db-concurrencySkill
- debugSkill
- dependency-auditSkill
- deploy-awsSkill
- design-systemSkill
- drf-apiSkill
- drf-zod-contractSkill
- frontend-perfSkill
- git-workflowSkill
- i18n-rtlSkill
- incident-responseSkill
- migrationsSkill
- money-decimalSkill
- multi-tenancySkill
- nextjs-moduleSkill
- observabilitySkill
- perf-reviewSkill
- production-readinessSkill
- product-ux-designSkill
- rbac-permissionsSkill
- security-reviewSkill
- skill-authoringSkill
- tailwind-shadcnSkill
- tenant-session-switchSkill
- threat-modelingSkill
- verifySkill
- version-checkSkill
- visual-ui-designSkill
- webhook-handlerSkill
- websockets-channelsSkill
- write-testsSkill
- zod-formsSkill
- zustand-stateSkill
- example-skillSkill
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 11
Do not copy fetched rows into Zustand or useState.
- Very specific: line 23
After a mutation, invalidate by key prefix in
onSuccess: Don't manually patch the cache for the common case.
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
React Query (server state with TanStack Query v5)
When to use
Any component that reads or writes data owned by the Django/DRF backend. Server
state is asynchronous, shared, and can go stale under you — that is Query's job.
Do not copy fetched rows into Zustand or useState; mirror-storing server
data is the top source of stale-UI bugs on this stack.
Pattern
Three rules:
- Query keys are structured arrays from a factory, never ad-hoc strings.
A key is
['tenant', tenantId, 'invoices', 'list', filters]— a stable, serializable description of what the data is, which under a per-tenant API includes whose it is. Prefix-matching then powers targeted invalidation. staleTimesets how long data is trusted;gcTimehow long an unused cache lingers. Pick per resource — not the defaults everywhere.- After a mutation, invalidate by key prefix in
onSuccess. Don't manually patch the cache for the common case; let the refetch reconcile with the server.
Centralize keys in a factory, read with useQuery, write with useMutation,
then invalidate by prefix in onSuccess:
// features/invoices/keys.ts — tenant is part of the key, not just a header
export const invoiceKeys = {
all: (t: string) => ['tenant', t, 'invoices'] as const,
lists: (t: string) => [...invoiceKeys.all(t), 'list'] as const,
list: (t: string, filters: InvoiceFilters) => [...invoiceKeys.lists(t), filters] as const,
detail: (t: string, id: number) => [...invoiceKeys.all(t), 'detail', id] as const,
};
// Read (v5 single-object signature; no-data status is `isPending`)
const tenantId = useUI((s) => s.activeEntrepriseId); // see `zustand-state`
const { data, isPending, isError } = useQuery({
queryKey: invoiceKeys.list(tenantId, filters),
queryFn: () => fetchInvoices(filters), // returns a Zod-parsed payload
enabled: !!tenantId,
staleTime: 30_000, // 30s: trust the list before refetching
gcTime: 5 * 60_000, // keep unused cache 5min for fast back-nav
});
// Write, then prefix-invalidate so every matching list/detail refetches
const qc = useQueryClient();
const { mutate } = useMutation({
mutationFn: (body: InvoiceInput) => createInvoice(body),
onSuccess: () => qc.invalidateQueries({ queryKey: invoiceKeys.all(tenantId) }),
});
// mutate(values) — call from the form's submit handlerThe server-state vs client-state boundary
| Belongs in React Query | Belongs in Zustand / local state |
| API rows, lists, details | Sidebar open, active tab, theme |
| Anything the server owns | Draft form input before submit |
| Derived-from-fetched values | Selected row id / UI filters* |
*Filters live in client state, then feed into the query key — the key is the one place client and server state meet. Never duplicate the fetched rows.
Switching tenants
A tenant switch changes which rows every key means, so it runs as an ordered
sequence — set the store, cancelQueries, remove the old tenant's entries,
resubscribe sockets, let observers refetch. The order is the safety property; see
tenant-session-switch for the full procedure and tenant-aware persistence.
Adapt to your repo
Rename invoices/invoiceKeys and the accessor (/api/invoices/) per resource,
and keep one keys file per feature. Tune staleTime to how fast the data changes
(near-real-time dashboards → low or 0; reference lists → minutes). Wrap the app
once in a QueryClientProvider in a client boundary under the App Router. Make
queryFn return the Zod-validated shape (see drf-zod-contract).
Gotchas
- v5 renamed
isLoading→isPendingfor the no-data state,cacheTime→gcTime, and takes a single object — positionaluseQuery(key, fn)no longer exists. invalidateQueriesprefix-matches, so{ queryKey: ['invoices'] }catches every list and detail; scoping too narrowly leaves stale sibling views.staleTime: 0(the default) refetches on every mount/focus — deliberate, but chatty for stable data. Set it up rather than leaving it implicit.- Reading
dataoff a mirrored Zustand copy defeats invalidation — the refetch updates the cache, not your snapshot. Read straight fromuseQuery. - Cookie-JWT auth means the browser sends the HttpOnly cookie automatically, but
credentials: 'include'alone only covers reads — mutatingmutationFncalls also need theX-CSRFTokenheader. Go through the shared client (seenextjs-module) rather than hand-rollingfetchin aqueryFn. - Security-critical: the tenant belongs IN the query key, not only in the request
header. Two tenants otherwise share the cache entry
['invoices','list',filters], and after a tenant switch the cache happily serves the previous tenant's rows — a cross-tenant leak your backend never sees. Scope every key under['tenant', tenantId, ...]so two tenants can never collide in one entry (the purge on switch istenant-session-switch's job). Client-side isolation is defence-in-depth, not the boundary — the server still enforces it (seemulti-tenancy,rbac-permissions).
See also
nextjs-moduletenant-session-switchzustand-statedrf-zod-contract
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

