tanstack-query-best-practices

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

Updated Aug 15, 2025
One-click install
npx skills add https://github.com/yehezkieldio/topaz --skill tanstack-query-best-practices-yehezkieldio
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: tanstack-query-best-practices
Source: https://github.com/yehezkieldio/topaz/tree/main/.agents/skills/tanstack-query-best-practices
Command: npx skills add https://github.com/yehezkieldio/topaz --skill tanstack-query-best-practices-yehezkieldio

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? React applications often suffer from cache inconsistencies, stale data, unnecessary network requests, and poorly handled mutations when server state is managed ad hoc. This Skill provides a structured rule set for TanStack Query that prevents common bugs in query keys, caching, mutations, and SSR hydration. ## Core Features & Use Cases - Query Key Standards: Enforces array-based, hierarchical, serializable query keys with factory patterns for type-safe invalidation. - Caching & Mutation Rules: Covers staleTime/gcTime tuning, targeted invalidation, optimistic updates with rollback, and cross-component mutation tracking via useMutationState. - Advanced Patterns: Includes guidance on infinite queries, parallel fetching with useQueries, SSR dehydration/hydration, prefetching on user intent, and offline persistence. - Use Case: When building a paginated product list with optimistic cart updates in a Next.js app, apply the rules to structure query keys hierarchically, prefetch on hover, and roll back cache changes on mutation errors. ## Quick Start Ask the AI to review your React data fetching code against TanStack Query best practices and fix any query key, caching, or mutation issues it finds.

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, then ID, then filters or sub-resources, such as ['todos', 5, 'comments']. 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?

Cancel outgoing queries in onMutate, snapshot the previous cache, apply the change with setQueryData, and return the snapshot as context. Roll back in onError using that context, then invalidate in onSettled to resync 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. Prefetch queries on the server with a per-request QueryClient, pass dehydrate(queryClient) into a HydrationBoundary, and consume the data client-side with useSuspenseQuery. Set a nonzero staleTime on the server to avoid an immediate client refetch.

Why is my React Query data stale after a mutation?

The mutation likely did not invalidate the affected queries. In onSuccess, call queryClient.invalidateQueries with the specific query keys impacted, including lists, detail views, and any cross-entity queries that display the changed data.

When should I use placeholderData versus initialData?

Use placeholderData for temporary previews that should not persist to cache, such as showing list data while a detail query fetches. Use initialData for authoritative data like SSR results, paired with initialDataUpdatedAt so stale calculations are correct.