tanstack-query-best-practices

Standardize TanStack Query data-fetching and server-state patterns in React applications.

1|1|Updated Apr 18, 2026
One-click install
npx skills add https://github.com/rodrigobranas/formacao_ia_arq_t1_4 --skill tanstack-query-best-practices-rodrigobranas
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: tanstack-query-best-practices
Source: https://github.com/rodrigobranas/formacao_ia_arq_t1_4/tree/main/mastra-rag/.agents/skills/tanstack-query-best-practices
Command: npx skills add https://github.com/rodrigobranas/formacao_ia_arq_t1_4 --skill tanstack-query-best-practices-rodrigobranas

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

TanStack Query best practices for data fetching, caching, mutations, and server-state management in React applications.

Core Features & Use Cases

  • Standardize data-fetching patterns, caching strategies, and error handling.
  • Provide guidance for SSR/SSG integration, optimistic updates, and cache invalidation.
  • Apply consistent rules across projects to reduce bugs and improve user experience.

Quick Start

Review the guidelines and implement the recommended patterns in your React data-fetching code.

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 for consistent cache invalidation in React?

TanStack Query keys should be structured hierarchically as arrays to enable predictable cache invalidation in React. Standardizing query key arrays allows targeted invalidation of specific data-fetching entries without clearing unrelated server-state caches.

What is the best way to implement optimistic updates with TanStack Query mutations?

Optimistic updates in TanStack Query mutations involve temporarily updating the cache before the server response arrives. This pattern requires configuring mutation logic to roll back the cache automatically if the server request fails, ensuring robust error handling.

How does TanStack Query handle server-state caching differently from local React state?

TanStack Query manages server-state caching by automatically fetching, updating, and invalidating cached data in the background. This differs from local React state by synchronizing asynchronous server data directly within the caching layer rather than manually managing component state.

Can I integrate TanStack Query caching strategies with React SSR and SSG?

TanStack Query caching strategies integrate with React SSR and SSG by prefetching data on the server and dehydrating the cache. The client then hydrates this server-state to prevent redundant data-fetching and ensure immediate data availability.

Why does my TanStack Query cache return stale data after a mutation?

Stale data after a mutation occurs when TanStack Query cache invalidation rules are not triggered for the relevant query keys. Applying structured invalidation patterns immediately after successful mutations forces the cache to re-fetch the updated server-state.