tanstack-query

Implement server-state caching, fetching, and synchronization with TanStack Query v5.

Updated Mar 31, 2026
One-click install
npx skills add https://github.com/sonth87/Redprint --skill tanstack-query-sonth87
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: tanstack-query
Source: https://github.com/sonth87/Redprint/tree/main/.agents/skills/tanstack-query
Command: npx skills add https://github.com/sonth87/Redprint --skill tanstack-query-sonth87

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires @tanstack/react-query, @tanstack/react-query-devtools.

What problem does it solve? Managing server state in React applications requires handling caching, background refetching, pagination, and optimistic updates manually, which leads to duplicated boilerplate and stale data bugs. ## Core Features & Use Cases - Declarative Data Fetching: Use useQuery with hierarchical query keys for automatic caching, deduplication, and background refetching. - Mutations & Optimistic Updates: Apply useMutation with onMutate rollback patterns to update the UI instantly and sync with the server. - Advanced Patterns: Covers infinite queries, dependent queries, prefetching, Suspense integration, SSR hydration, and testing setup. - Use Case: When building a dashboard that lists paginated todos with add/edit actions, use this Skill to wire up useQuery for the list, useMutation with optimistic updates for edits, and invalidateQueries to keep everything in sync. ## Quick Start Set up TanStack Query in my React app with a QueryClientProvider and write a useQuery hook that fetches and caches a list of todos.

Frequently Asked Questions about tanstack-query

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

FAQPage Schema
How do I set up TanStack Query in a React app?▼

Install @tanstack/react-query, create a QueryClient with defaultOptions like staleTime and retry, then wrap your app in QueryClientProvider. Optionally add ReactQueryDevtools for debugging cache state.

How to do optimistic updates with useMutation in React Query?▼

In onMutate, cancel outgoing queries with cancelQueries, snapshot the previous cache via getQueryData, then setQueryData with the new value. Roll back in onError using the returned context and invalidate in onSettled.

What is the difference between staleTime and gcTime in TanStack Query?▼

staleTime controls how long data is considered fresh before refetching, defaulting to 0. gcTime controls how long unused data stays in cache before garbage collection, defaulting to 5 minutes. gcTime should exceed staleTime.

Does TanStack Query support infinite scrolling and pagination?▼

Yes, useInfiniteQuery handles cursor-based infinite scrolling with getNextPageParam and fetchNextPage. For numbered pagination, use useQuery with placeholderData set to the previous data to avoid loading flicker.

Why is my React Query data not refetching after a mutation?▼

Queries only refetch when invalidated or stale. Call queryClient.invalidateQueries with the matching query key prefix in the mutation's onSuccess or onSettled callback to mark cached data stale and trigger refetch.

Can I use TanStack Query with Next.js server-side rendering?▼

Yes, prefetch queries on the server with a fresh QueryClient, then pass dehydrate(queryClient) to HydrationBoundary on the client. This hydrates the cache so useQuery resolves immediately without a client-side fetch.