react-query-best-practices
Skill2 starsChanged 4 months agoMITView on GitHub
React Query v4 (TanStack Query) best practices, patterns, and troubleshooting. Use when working with useQuery, useMutation, query invalidation, caching, WebSocket integration, or any async state management in React. Based on TkDodo's comprehensive blog series.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- bibektimilsina00/runmycrewtheir own version, first seen May 19, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- bibektimilsina00/runmycrew★ 1same 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 repository8 other files it gives its agents.
- skills/feature-workflow/AGENTS.mdAGENTS.md
- skills/react-query-best-practices/AGENTS.mdAGENTS.md
- smc-crt-analysisAGENTS.md
- skills/validate-pr/AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- feature-workflowSkill
- smc-crt-analysisSkill
- validate-prSkill
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 2 are distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 107
Server state != Client state: - never mix them in global state managers
- Distinctive: line 113
Read individual rule files for detailed explanations and code examples:
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 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 Best Practices
Important: This guide targets React Query v4. Some patterns may differ in v5.
Comprehensive guide for React Query v4 (TanStack Query) based on TkDodo's authoritative blog series. Contains 24 rules across 7 categories, prioritized by impact.
When to Apply
Reference these guidelines when:
- Implementing new queries or mutations
- Integrating WebSockets with React Query
- Setting up query invalidation patterns
- Debugging React Query behavior
- Optimizing render performance
- TypeScript integration questions
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
| 1 | Query Keys & Patterns | CRITICAL | query- |
| 2 | Mutations & Updates | CRITICAL | mutation- |
| 3 | Caching Strategy | HIGH | cache- |
| 4 | WebSocket Integration | HIGH | websocket- |
| 5 | TypeScript Integration | MEDIUM | typescript- |
| 6 | Testing Patterns | MEDIUM | testing- |
| 7 | Common Pitfalls | MEDIUM | troubleshoot- |
| 8 | Migration to v5 | HIGH | migration- |
Quick Reference
1. Query Keys & Patterns (CRITICAL)
query-keys-as-dependencies- Include all queryFn params in queryKeyquery-key-factory- Use factory pattern for consistent key generationquery-select-transforms- Use select option for data transformationsquery-status-check-order- Check data first, then error, then loadingquery-tracked-properties- Only destructure properties you usequery-placeholder-vs-initial- Know when to use each approachquery-dependent-enabled- Use enabled option for dependent queries
2. Mutations & Updates (CRITICAL)
mutation-prefer-mutate- Use mutate() with callbacks over mutateAsync()mutation-invalidation- Invalidate queries after mutationsmutation-direct-cache-update- Update cache directly when appropriatemutation-optimistic-updates- Show success immediately, rollback on failuremutation-callback-separation- Query logic in hook, UI effects in component
3. Caching Strategy (HIGH)
cache-stale-time- Set appropriate staleTime for your domaincache-refetch-triggers- Keep refetch triggers enabled in production
4. WebSocket Integration (HIGH)
websocket-event-invalidation- Use events to trigger invalidationwebsocket-stale-time-infinity- Set staleTime: Infinity for WS-managed datawebsocket-reconnection- Invalidate stale queries on reconnect
5. TypeScript Integration (MEDIUM)
typescript-infer-dont-specify- Let TypeScript infer, type the queryFntypescript-zod-validation- Use Zod for runtime validation
6. Testing Patterns (MEDIUM)
testing-fresh-client- Create fresh QueryClient per testtesting-msw-mocking- Use MSW for network mocking
7. Common Pitfalls (MEDIUM)
troubleshoot-copy-to-state- Never copy query data to local statetroubleshoot-missing-key-deps- Include all dependencies in query keytroubleshoot-fetch-not-reject- Handle HTTP errors with fetch
8. Migration to v5 (HIGH)
migration-single-signature- All hooks now take a single object argumentmigration-status-pending- status 'loading' → 'pending'; isLoading semantics changedmigration-cache-time-to-gc-time- cacheTime renamed to gcTimemigration-keep-previous-data- keepPreviousData → placeholderData with identity fnmigration-query-callbacks-removed- onSuccess/onError/onSettled removed from useQuerymigration-suspense-hooks- New useSuspenseQuery, useSuspenseInfiniteQuery hooksmigration-throw-on-error- useErrorBoundary renamed to throwOnErrormigration-remove-method- query.remove() removed; use queryClient.removeQueries()migration-initial-page-param- initialPageParam now required for infinite queriesmigration-refetch-page-to-max-pages- refetchPage replaced by maxPagesmigration-hydration-boundary- Hydrate component renamed to HydrationBoundarymigration-refetch-interval-callback- refetchInterval callback now receives only querymigration-context-to-query-client- context prop removed; pass queryClient directlymigration-misc-breaking-changes- React 18 min, server retry=0, hashKey, isDataEqual, etc.
Core Mental Model
- React Query is NOT a data fetching library - it's an async state manager
- Server state != Client state - never mix them in global state managers
- Stale-while-revalidate - show cached data immediately, fetch in background
- Query keys are dependencies - include all variables used in queryFn
How to Use
Read individual rule files for detailed explanations and code examples:
rules/query-key-factory.md
rules/mutation-invalidation.md
rules/websocket-event-invalidation.mdEach rule file contains:
- Brief explanation of why it matters
- Incorrect code example
- Correct code example
Full Compiled Document
For the complete guide with all rules expanded: AGENTS.md
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.

