tanstack-query-best-practices

Prescribe TanStack Query best practices for React data fetching and caching.

Updated Apr 4, 2026
One-click install
npx skills add https://github.com/TaherMustansir1929/zainy-water-v3 --skill tanstack-query-best-practices-tahermustansir1929
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: tanstack-query-best-practices
Source: https://github.com/TaherMustansir1929/zainy-water-v3/tree/main/.agents/skills/tanstack-query-best-practices
Command: npx skills add https://github.com/TaherMustansir1929/zainy-water-v3 --skill tanstack-query-best-practices-tahermustansir1929

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides a concise, opinionated set of best-practice rules and actionable examples to prevent common bugs and performance issues when using TanStack Query (React Query) in React applications. It reduces cache inconsistencies, excessive network requests, incorrect mutation handling, and SSR hydration problems by standardizing query keys, caching policies, mutation flows, and offline persistence strategies.

Core Features & Use Cases

  • Query Key Discipline: guidance on array-structured, hierarchical, serializable keys and query key factories to avoid cache collisions and enable precise invalidation.
  • Caching & Performance: recommendations for staleTime, gcTime, placeholderData vs initialData, structural sharing, and select-based transforms to minimize refetches and re-renders.
  • Mutations & Optimistic Updates: patterns for onMutate snapshots, rollback context, targeted invalidation, and useMutationState for cross-component tracking.
  • SSR, Prefetching & Offline: dehydrate/hydrate patterns, intent-based prefetching, networkMode options, and query persistence strategies for offline-first apps.
  • Use Case: Audit or implement a data-fetching layer in a React/TanStack Query codebase to improve UX, reduce unnecessary requests, and eliminate stale or inconsistent UI states.

Quick Start

Review my React project's data fetching code and return prioritized, actionable fixes and code examples to align with TanStack Query best practices.

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 avoid cache collisions?

Prevent cache collisions in React Query by structuring keys as hierarchical, serializable arrays using query key factories. This standardized approach enables precise invalidation and ensures consistent data fetching across your React application.

What's the best way to handle optimistic updates and rollback in React Query?

Handle optimistic updates in React Query by implementing onMutate snapshots to capture rollback context. This pattern ensures mutation consistency by enabling targeted invalidation and cross-component state tracking during data modifications.

When should I configure staleTime vs gcTime in TanStack Query?

Configure staleTime in TanStack Query to control when cached data is considered stale and needs refetching, while gcTime dictates when inactive query data is garbage collected. Balancing these caching policies minimizes unnecessary network requests and re-renders.

How does TanStack Query SSR hydration work for React applications?

TanStack Query SSR hydration uses dehydrate and hydrate patterns to transfer server-fetched cache state to the React client. Applying these patterns prevents hydration mismatches and ensures production-ready rendering with immediately available data.

Why is my React Query mutation causing inconsistent UI states?

React Query mutations cause inconsistent UI states when lacking proper onMutate rollback context and targeted cache invalidation. Implementing standardized mutation flows with optimistic updates and useMutationState tracking resolves these data inconsistencies.

Can I use React Query for offline persistence and prefetching?

React Query supports offline persistence through networkMode options and intent-based prefetching strategies. These patterns allow your React application to cache data efficiently, prefetch queries before navigation, and maintain functionality during network interruptions.