What problem does it solve?
TanStack Query Best Practices reduces cache bugs, redundant network requests, and UI inconsistencies caused by improper query key design, misconfigured caching, and incorrect mutation handling in React applications using TanStack Query.
Core Features & Use Cases
- Query Key Discipline: Enforce array-based, hierarchical keys that include all dependencies and remain JSON-serializable to enable precise invalidation and avoid cache collisions.
- Caching Strategies: Recommendations for staleTime, gcTime, default options, placeholderData vs initialData, and persistence for offline-first or fast-refresh experiences.
- Mutations & Optimistic Updates: Patterns for onMutate snapshotting, rollback context, setQueryData updates, invalidate vs update trade-offs, and useMutationState for cross-component tracking.
- SSR, Prefetching & Parallelism: Dehydrate/hydrate guidance, per-request QueryClient creation, intent-based prefetching, useQueries for dynamic parallel fetching, and infinite query pagination rules.
- Performance & Resilience: Use select for transformations, structural sharing to limit re-renders, proper query cancellation with AbortSignal, and error boundary strategies with useQueryErrorResetBoundary.
Use case: Apply these rules when building dashboards, paginated feeds, detail pages, or any data-driven UI that requires consistent server state and predictable cache behavior.
Quick Start
Review and standardize your query keys as arrays including all dependencies, configure sensible staleTime and gcTime values, add targeted invalidation and optimistic updates for mutations, and ensure fetch functions accept AbortSignal.