tanstack-query-best-practices

Implements TanStack Query patterns for data fetching, caching, mutations, and server state in React applications.

Updated Sep 17, 2026
One-click install
npx skills add https://github.com/juanjaragavi/utilities-manager --skill tanstack-query-best-practices-juanjaragavi
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: tanstack-query-best-practices
Source: https://github.com/juanjaragavi/utilities-manager/tree/main/.agents/skills/tanstack-query-best-practices
Command: npx skills add https://github.com/juanjaragavi/utilities-manager --skill tanstack-query-best-practices-juanjaragavi

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? React applications often suffer from inconsistent data fetching, stale caches, duplicate requests, and poorly handled mutations. This Skill provides a structured rule set for TanStack Query (React Query) so server state is managed correctly across query keys, caching, mutations, error handling, prefetching, infinite queries, SSR, and offline support. ## Core Features & Use Cases - Prioritized Rule Library: 38 rules organized by priority (CRITICAL to LOW) across ten categories including query keys, caching, mutations, error handling, and SSR integration. - Bad vs Good Examples: Each rule file shows an anti-pattern alongside the recommended implementation with TypeScript code samples. - Configuration Guidance: Concrete recommended values for staleTime, gcTime, retry logic, and network modes based on data volatility. - Use Case: When building a paginated product list in React, apply the infinite query rules to configure getNextPageParam, guard fetchNextPage with isFetching checks, and use keepPreviousData so the UI stays stable during page transitions. ## Quick Start Ask the assistant to review your React data fetching code using the TanStack Query best practices rules and fix any 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 configure staleTime and gcTime in TanStack Query?▼

Set staleTime based on data volatility: 0 for real-time data, 30s-1min for notifications, 5min for user profiles, and Infinity for static content. Configure gcTime (default 5 minutes) to control how long inactive queries stay cached, using longer values for frequently revisited routes.

How to implement optimistic updates with React Query mutations?▼

Use the onMutate callback to cancel outgoing queries, snapshot the previous cache with getQueryData, and apply the new state with setQueryData. Return the snapshot as context, then restore it in onError and call invalidateQueries in onSettled to sync with the server.

What is the difference between placeholderData and initialData?▼

initialData is persisted to the cache and treated as real fetched data with staleTime applied, making it suitable for SSR. placeholderData is temporary, never cached, always triggers a background fetch, and sets isPlaceholderData to true, making it ideal for pagination and preview content.

Does TanStack Query support server-side rendering and hydration?▼

Yes, TanStack Query supports SSR through the dehydrate/hydrate pattern with HydrationBoundary. Create a QueryClient per request on the server, set a higher staleTime to avoid double-fetching after hydration, and avoid gcTime of 0 to prevent hydration issues.

Why does my infinite query fetch duplicate pages?▼

Duplicate page fetches happen when fetchNextPage is triggered without checking isFetching or isFetchingNextPage first. Guard scroll handlers and buttons with these flags, and ensure getNextPageParam returns undefined when no more pages exist.

When should I not retry failed queries in React Query?▼

Avoid retrying client errors like 404 and 403 since they will not resolve on retry. Use a custom retry function that returns false for 4xx status codes and limits retries for server errors, and keep mutation retry at 0 by default.