tanstack-query

Fetch and manage server state in React 18+ with TanStack Query.

Updated Aug 15, 2025
One-click install
npx skills add https://github.com/gesmith0606/nfl_data_engineering --skill tanstack-query-gesmith0606
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: tanstack-query
Source: https://github.com/gesmith0606/nfl_data_engineering/tree/main/web/frontend/.agents/skills/tanstack-query
Command: npx skills add https://github.com/gesmith0606/nfl_data_engineering --skill tanstack-query-gesmith0606

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Modern React applications need a consistent, resilient way to fetch, cache, and update server state without cluttering components with loading and error boilerplate. This Skill documents patterns for using TanStack Query v5 to provide Suspense-based data loading, predictable cache behavior, and reliable mutation flows so UI remains fast and consistent.

Core Features & Use Cases

  • Suspense-first queries: Adopt Suspense boundaries for simpler components and consistent loading UX using useSuspenseQuery.
  • Legacy loading control: Use useQuery when component-level loading or error states are required.
  • Mutations and optimistic updates: Patterns for onMutate, rollback on error, and post-mutation invalidation.
  • Cache management: Invalidation, manual updates, prefetching, persistence, gcTime/staleTime tuning, and deduplication strategies.
  • Advanced flows: Dependent queries, parallel queries, infinite pagination, query cancellation with abort signals, and integration with centralized API service layers.
  • Error handling and tooling: Combine ErrorBoundary with Suspense and use Devtools for debugging cache state.

Quick Start

Use the tanstack-query patterns to fetch a list of posts with Suspense and prefetch the selected post on hover while keeping cache updates optimistic after a create operation.

Frequently Asked Questions about tanstack-query

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fetch data with React Suspense using TanStack Query?

Fetch data with React Suspense using TanStack Query by implementing useSuspenseQuery to centralize data fetching. This pattern provides cache-first behaviors and simplifies components by relying on Suspense boundaries for consistent loading UX.

What is the best way to handle optimistic updates and cache invalidation in React?

Handle optimistic updates and cache invalidation in React by applying mutation patterns with onMutate, rolling back on error, and executing post-mutation invalidation to keep the UI fast and consistent.

How do I configure staleTime and gcTime for cache management in React applications?

Configure staleTime and gcTime for cache management in React applications by tuning query key factories and retry policies. This approach ensures predictable cache behavior, automatic deduplication, and efficient garbage collection.

Can I use useQuery for component-level loading states instead of Suspense?

You can use useQuery for component-level loading states instead of Suspense when your React UI requires manual control over loading and error handling rather than relying on Suspense boundaries.

How do I implement infinite scroll and prefetching with React data fetching?

Implement infinite scroll and prefetching with React data fetching by utilizing TanStack Query's infinite pagination patterns and hover-based prefetch triggers to maintain a cache-first user experience.

Why use query key factories for centralized API services in React 18?

Use query key factories for centralized API services in React 18 to structure cache management predictably, enabling precise invalidation, dependent queries, and abortable fetch integration without cluttering components.