Use when adding new error messages to React, or seeing "unknown error code" warnings.
npx skills add DeckardGer/tanstack-agent-skills --skill "tanstack-query-best-practices"
Install specific skill from multi-skill repository
# Description
TanStack Query (React Query) best practices for data fetching, caching, mutations, and server state management. Activate when building data-driven React applications with server state.
# SKILL.md
name: tanstack-query-best-practices
description: TanStack Query (React Query) best practices for data fetching, caching, mutations, and server state management. Activate when building data-driven React applications with server state.
TanStack Query Best Practices
Comprehensive guidelines for implementing TanStack Query (React Query) patterns in React applications. These rules optimize data fetching, caching, mutations, and server state synchronization.
When to Apply
- Creating new data fetching logic
- Setting up query configurations
- Implementing mutations and optimistic updates
- Configuring caching strategies
- Integrating with SSR/SSG
- Refactoring existing data fetching code
Rule Categories by Priority
| Priority | Category | Rules | Impact |
|---|---|---|---|
| CRITICAL | Query Keys | 5 rules | Prevents cache bugs and data inconsistencies |
| CRITICAL | Caching | 5 rules | Optimizes performance and data freshness |
| HIGH | Mutations | 6 rules | Ensures data integrity and UI consistency |
| HIGH | Error Handling | 3 rules | Prevents poor user experiences |
| MEDIUM | Prefetching | 4 rules | Improves perceived performance |
| MEDIUM | Parallel Queries | 2 rules | Enables dynamic parallel fetching |
| MEDIUM | Infinite Queries | 3 rules | Prevents pagination bugs |
| MEDIUM | SSR Integration | 4 rules | Enables proper hydration |
| LOW | Performance | 4 rules | Reduces unnecessary re-renders |
| LOW | Offline Support | 2 rules | Enables offline-first patterns |
Quick Reference
Query Keys (Prefix: qk-)
qk-array-structureβ Always use arrays for query keysqk-include-dependenciesβ Include all variables the query depends onqk-hierarchical-organizationβ Organize keys hierarchically (entity β id β filters)qk-factory-patternβ Use query key factories for complex applicationsqk-serializableβ Ensure all key parts are JSON-serializable
Caching (Prefix: cache-)
cache-stale-timeβ Set appropriate staleTime based on data volatilitycache-gc-timeβ Configure gcTime for inactive query retentioncache-defaultsβ Set sensible defaults at QueryClient levelcache-invalidationβ Use targeted invalidation over broad patternscache-placeholder-vs-initialβ Understand placeholder vs initial data differences
Mutations (Prefix: mut-)
mut-invalidate-queriesβ Always invalidate related queries after mutationsmut-optimistic-updatesβ Implement optimistic updates for responsive UImut-rollback-contextβ Provide rollback context from onMutatemut-error-handlingβ Handle mutation errors gracefullymut-loading-statesβ Use isPending for mutation loading statesmut-mutation-stateβ Use useMutationState for cross-component tracking
Error Handling (Prefix: err-)
err-error-boundariesβ Use error boundaries with useQueryErrorResetBoundaryerr-retry-configβ Configure retry logic appropriatelyerr-fallback-dataβ Provide fallback data when appropriate
Prefetching (Prefix: pf-)
pf-intent-prefetchβ Prefetch on user intent (hover, focus)pf-route-prefetchβ Prefetch data during route transitionspf-stale-time-configβ Set staleTime when prefetchingpf-ensure-query-dataβ Use ensureQueryData for conditional prefetching
Infinite Queries (Prefix: inf-)
inf-page-paramsβ Always provide getNextPageParaminf-loading-guardsβ Check isFetchingNextPage before fetching moreinf-max-pagesβ Consider maxPages for large datasets
SSR Integration (Prefix: ssr-)
ssr-dehydrationβ Use dehydrate/hydrate pattern for SSRssr-client-per-requestβ Create QueryClient per requestssr-stale-time-serverβ Set higher staleTime on serverssr-hydration-boundaryβ Wrap with HydrationBoundary
Parallel Queries (Prefix: parallel-)
parallel-use-queriesβ Use useQueries for dynamic parallel queriesquery-cancellationβ Implement query cancellation properly
Performance (Prefix: perf-)
perf-select-transformβ Use select to transform/filter dataperf-structural-sharingβ Leverage structural sharingperf-notify-change-propsβ Limit re-renders with notifyOnChangePropsperf-placeholder-dataβ Use placeholderData for instant UI
Offline Support (Prefix: offline-)
network-modeβ Configure network mode for offline supportpersist-queriesβ Configure query persistence for offline support
How to Use
Each rule file in the rules/ directory contains:
1. Explanation β Why this pattern matters
2. Bad Example β Anti-pattern to avoid
3. Good Example β Recommended implementation
4. Context β When to apply or skip this rule
Full Reference
See individual rule files in rules/ directory for detailed guidance and code examples.
# Supported AI Coding Agents
This skill is compatible with the SKILL.md standard and works with all major AI coding agents:
Learn more about the SKILL.md standard and how to use these skills with your preferred AI coding agent.