tanstack-query-best-practices

Implement TanStack Query patterns for caching, mutations, and server state in React applications.

3|Updated Apr 20, 2026
One-click install
npx skills add https://github.com/langgenius/due-date-hq-jwl --skill tanstack-query-best-practices-langgenius
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: tanstack-query-best-practices
Source: https://github.com/langgenius/due-date-hq-jwl/tree/main/.agents/skills/tanstack-query-best-practices
Command: npx skills add https://github.com/langgenius/due-date-hq-jwl --skill tanstack-query-best-practices-langgenius

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? React developers frequently introduce cache bugs, stale data, unnecessary network requests, and inconsistent UI when managing server state with TanStack Query. This Skill provides a prioritized rule set that prevents common mistakes in query keys, caching, mutations, error handling, prefetching, and SSR integration. ## Core Features & Use Cases - Prioritized Rule Library: 38 rules organized by priority (CRITICAL to LOW) across 10 categories including query keys, caching, mutations, error handling, infinite queries, and SSR. - Bad vs Good Examples: Each rule file shows an anti-pattern to avoid alongside a recommended implementation with TypeScript code. - Use Case: When building a paginated product list with optimistic updates, activate this Skill to get correct query key hierarchy, getNextPageParam configuration, and rollback-safe optimistic mutation patterns. ## Quick Start Review my React component that fetches data with TanStack Query and apply the best practice rules to fix caching and mutation issues.

Frequently Asked Questions about tanstack-query-best-practices

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I structure query keys in TanStack Query?

Always use arrays organized hierarchically from general to specific: entity type first, then ID, then filters or sub-resources. Include every variable the query function depends on, and use query key factories for consistency in larger applications.

How do I implement optimistic updates with React Query mutations?

Cancel outgoing queries in onMutate, snapshot the previous cache, update it with setQueryData, and return the snapshot as context. Roll back in onError using that context, then invalidate queries in onSettled to sync with the server.

What is the difference between staleTime and gcTime in TanStack Query?

staleTime controls how long fetched data is considered fresh before background refetching, while gcTime controls how long inactive query data stays in cache before garbage collection. Set staleTime based on data volatility and gcTime based on how often users revisit the data.

Does TanStack Query support server-side rendering with Next.js?

Yes, use the dehydrate/hydrate pattern: prefetch queries into a new QueryClient on the server, pass dehydrate(queryClient) to HydrationBoundary, and consume the data with useSuspenseQuery on the client. Create a fresh QueryClient per request to avoid sharing data between users.

Why does my React Query cache show stale data after a mutation?

The mutation likely did not invalidate all affected queries. In onSuccess, invalidate every query key whose data could change, including lists, detail views, counts, and cross-entity queries, using hierarchical keys for precise targeting.

When should I use placeholderData versus initialData?

Use placeholderData for temporary previews like list-item data shown while fetching details, since it never persists to cache. Use initialData for authoritative data such as SSR-fetched content, since it is cached and respects staleTime.