tanstack-query-best-practices

Provide TanStack Query best practices for React server state management.

Updated Apr 7, 2026
One-click install
npx skills add https://github.com/rally19/goform --skill tanstack-query-best-practices-rally19
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: tanstack-query-best-practices
Source: https://github.com/rally19/goform/tree/main/.qwen/skills/tanstack-query-best-practices
Command: npx skills add https://github.com/rally19/goform --skill tanstack-query-best-practices-rally19

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Managing server state in React apps is error-prone: inconsistent query keys, improper caching, incorrect mutation handling, and poor SSR/hydration lead to stale UI, excessive network requests, and subtle bugs. These guidelines consolidate proven patterns to prevent cache collisions, ensure correct invalidation, and keep UI consistent during optimistic updates and offline scenarios.

Core Features & Use Cases

  • Query Key Strategy: Always use array keys, include dependencies, enforce serializable key parts, and organize keys hierarchically with factories for large apps.
  • Caching & Freshness: Practical staleTime and gcTime recommendations, placeholder vs initial data guidance, and persistence strategies for offline-first apps.
  • Mutations & Optimistic Updates: Standardized onMutate/onError/onSettled patterns, rollback context, useMutationState usage, and comprehensive invalidation approaches.
  • Advanced Patterns: Prefetching on intent, infinite pagination rules, parallel queries with useQueries, query cancellation with AbortSignal, SSR dehydrate/hydrate patterns, and error boundary integration.
  • Use Cases: Dashboards with frequently revisited data, detail pages with pre-seeded SSR data, large lists with infinite scrolling, and offline-capable mobile apps.

Quick Start

Audit your React codebase for useQuery/useMutation patterns and apply the rules to standardize query keys, set appropriate staleTime/gcTime, add optimistic update rollbacks, and implement SSR hydration and persistence where needed.

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 TanStack Query keys to prevent cache collisions in React?

TanStack Query keys should always use array formats with included dependencies and serializable parts. Organize keys hierarchically using factory functions for large apps to prevent cache collisions and ensure correct invalidation.

What's the best way to implement optimistic updates and rollbacks with useMutation?

Optimistic updates in useMutation require standardized onMutate, onError, and onSettled patterns. Capture rollback context during onMutate and use it in onError to revert changes if the mutation fails, keeping UI consistent.

How do I configure staleTime and gcTime for React server state caching?

Configure staleTime and gcTime based on data refresh needs and revisit frequency. Use placeholder versus initial data appropriately and apply persistence strategies for offline-first apps to reduce cache bugs.

Does TanStack Query work with SSR and prefetching in React applications?

TanStack Query supports SSR through dehydrate and hydrate patterns for pre-seeded detail pages. Implement prefetching on user intent, parallel queries with useQueries, and query cancellation with AbortSignal for advanced React patterns.

How do I handle infinite scrolling and pagination with TanStack Query?

Infinite pagination in TanStack Query follows specific rules for loading large lists. Combine infinite scrolling with proper query key strategies and invalidation approaches to maintain consistent cache state across pages.

When should I not use optimistic updates for React mutations?

Avoid optimistic updates when mutation failure rates are high or rollback complexity outweighs UI benefits. Rely on standard mutation handling with comprehensive invalidation and error boundary integration to maintain consistency.