install
source · Clone the upstream repo
git clone https://github.com/ComeOnOliver/skillshub
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ComeOnOliver/skillshub "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/pproenca/dot-skills/tanstack-query" ~/.claude/skills/comeonoliver-skillshub-tanstack-query-8ef488 && rm -rf "$T"
manifest:
skills/pproenca/dot-skills/tanstack-query/SKILL.mdsource content
TanStack Query Best Practices
Comprehensive performance optimization guide for TanStack Query v5 applications. Contains 40 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
When to Apply
Reference these guidelines when:
- Writing new queries, mutations, or data fetching logic
- Implementing caching strategies (staleTime, gcTime)
- Reviewing code for performance issues or request waterfalls
- Refactoring existing TanStack Query code
- Implementing infinite queries, Suspense, or optimistic updates
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Query Key Structure | CRITICAL | |
| 2 | Caching Configuration | CRITICAL | |
| 3 | Mutation Patterns | HIGH | |
| 4 | Prefetching & Waterfalls | HIGH | |
| 5 | Infinite Queries | MEDIUM | |
| 6 | Suspense Integration | MEDIUM | |
| 7 | Error & Retry Handling | MEDIUM | |
| 8 | Render Optimization | LOW-MEDIUM | |
Quick Reference
1. Query Key Structure (CRITICAL)
- Use centralized query key factoriestquery-key-factories
- Structure keys from generic to specifictquery-hierarchical-keys
- Always use array query keystquery-always-arrays
- Use serializable objects in keystquery-serializable-objects
- Use queryOptions for type-safe sharingtquery-options-pattern
- Colocate query keys with featurestquery-colocate-keys
2. Caching Configuration (CRITICAL)
- Understand staleTime vs gcTimecache-staletime-gctime
- Configure global defaults appropriatelycache-global-defaults
- Use placeholderData vs initialData correctlycache-placeholder-vs-initial
- Invalidate with precisioncache-invalidation-precision
- Control automatic refetch triggerscache-refetch-triggers
- Use enabled for conditional queriescache-enabled-option
3. Mutation Patterns (HIGH)
- Implement optimistic updates with rollbackmutation-optimistic-updates
- Invalidate in onSettled, not onSuccessmutation-invalidate-onsettled
- Cancel queries before optimistic updatesmutation-cancel-queries
- Use setQueryData for immediate cache updatesmutation-setquerydata
- Avoid parallel mutations on same datamutation-avoid-parallel
4. Prefetching & Waterfalls (HIGH)
- Avoid request waterfallsprefetch-avoid-waterfalls
- Prefetch on hover for perceived speedprefetch-on-hover
- Prefetch dependent data in queryFnprefetch-in-queryfn
- Prefetch in Server Componentsprefetch-server-components
- Flatten API to reduce waterfallsprefetch-flatten-api
5. Infinite Queries (MEDIUM)
- Limit infinite query pages with maxPagesinfinite-max-pages
- Flatten pages for renderinginfinite-flatten-pages
- Understand infinite query refetch behaviorinfinite-refetch-behavior
- Handle infinite query loading states correctlyinfinite-loading-states
6. Suspense Integration (MEDIUM)
- Use Suspense hooks for simpler loading statessuspense-use-suspense-hooks
- Always pair Suspense with Error Boundariessuspense-error-boundaries
- Combine Suspense queries with useSuspenseQueriessuspense-parallel-queries
- Place Suspense boundaries strategicallysuspense-boundaries-placement
7. Error & Retry Handling (MEDIUM)
- Configure retry with exponential backofferror-retry-config
- Use conditional retry based on error typeerror-conditional-retry
- Use global error handler for common errorserror-global-handler
- Display errors appropriatelyerror-display-patterns
- Use throwOnError with Error Boundarieserror-throw-on-error
8. Render Optimization (LOW-MEDIUM)
- Memoize select functionsrender-select-memoize
- Use select to derive data and reduce re-rendersrender-select-derived
- Use notifyOnChangeProps to limit re-rendersrender-notify-props
- Understand structural sharingrender-structural-sharing
- Avoid destructuring all propertiesrender-tracked-props
How to Use
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Reference files:
references/{prefix}-{slug}.md
Each reference file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and references
Related Skills
- For generating type-safe query hooks, see
skillorval - For mocking API responses in tests, see
skilltest-msw - For React 19 data fetching patterns, see
skillreact-19
Full Compiled Document
For the complete guide with all rules expanded:
AGENTS.md