tanstack-query-best-practices

Organize TanStack Query patterns for fetching, caching, and mutating server state in React apps.

5|Updated Feb 6, 2022
One-click install
npx skills add https://github.com/christofferbergj/dotfiles --skill tanstack-query-best-practices-christofferbergj
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: tanstack-query-best-practices
Source: https://github.com/christofferbergj/dotfiles/tree/main/.agents/skills/tanstack-query-best-practices
Command: npx skills add https://github.com/christofferbergj/dotfiles --skill tanstack-query-best-practices-christofferbergj

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

TanStack Query best practices provide a structured, battle-tested approach to fetching, caching, and mutating server state in React apps, reducing boilerplate and errors.

Core Features & Use Cases

  • Guidance on consistent query keys, caching strategies, and targeted invalidation.
  • Mutation handling with optimistic updates and error boundaries.
  • SSR/ hydration patterns and scalable organization for large apps.
  • Real-world usage patterns across various data-fetching scenarios.

Quick Start

Provide a concise, starter-focused plan outlining TanStack Query best practices for a React project.

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 for TanStack Query caching and invalidation?

TanStack Query caching uses consistent query key arrays to enable dependency tracking and targeted invalidation. Organizing keys hierarchically ensures predictable cache management and safe, testable mutation patterns across React components.

How do I handle mutations with optimistic updates in React Query?

Handling mutations with optimistic updates involves applying proven patterns to update the UI immediately while managing server state rollback on error boundaries. This approach reduces boilerplate and ensures robust data handling during failed requests.

What are the best practices for SSR hydration with TanStack Query?

SSR hydration patterns for TanStack Query involve dehydrating server state and rehydrating it on the client to prevent fetch waterfalls. Applying these structured patterns ensures reliable server-state initialization and scalable organization for large React apps.

When should I use targeted query invalidation instead of refetching?

Targeted query invalidation should be used when you need to selectively refresh specific cached server state after mutations, rather than triggering broad refetches. This precise invalidation strategy prevents redundant network requests and maintains data consistency across components.

Can I use TanStack Query best practices for large-scale React applications?

TanStack Query best practices are designed for large-scale React applications, providing scalable organization for server state. The catalog of rules ensures consistent query keys, robust caching strategies, and testable data-fetching patterns across complex component hierarchies.