tanstack-query
TanStack Query (React Query) v5 best practices for data fetching, caching, mutations, and server state management. Use when building data-driven React applications, setting up query configurations, implementing mutations/optimistic updates, configuring caching strategies, integrating with SSR, or fixing v4→v5 migration errors.
Best use case
tanstack-query is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
TanStack Query (React Query) v5 best practices for data fetching, caching, mutations, and server state management. Use when building data-driven React applications, setting up query configurations, implementing mutations/optimistic updates, configuring caching strategies, integrating with SSR, or fixing v4→v5 migration errors.
Teams using tanstack-query should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/tanstack-query/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How tanstack-query Compares
| Feature / Agent | tanstack-query | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
TanStack Query (React Query) v5 best practices for data fetching, caching, mutations, and server state management. Use when building data-driven React applications, setting up query configurations, implementing mutations/optimistic updates, configuring caching strategies, integrating with SSR, or fixing v4→v5 migration errors.
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
SKILL.md Source
# TanStack Query v5
**Version**: @tanstack/react-query@5.90.x
**Requires**: React 18.0+, TypeScript 4.7+
## v5 New Features
- **useMutationState** — cross-component mutation tracking without prop drilling
- **Simplified optimistic updates** — via `variables` from pending mutations, no cache manipulation needed
- **throwOnError** — renamed from `useErrorBoundary`
- **networkMode** — offline/PWA support (`online` | `always` | `offlineFirst`)
- **useQueries with combine** — merge parallel query results into single object
- **infiniteQueryOptions** — type-safe factory for infinite queries (parallel to `queryOptions`)
- **maxPages** — limit pages in cache for infinite queries (requires bi-directional pagination)
- **Mutation callback signature change (v5.89+)** — `onError`/`onSuccess`/`onSettled` now receive 4 params (added `onMutateResult`)
## Quick Setup
```bash
npm install @tanstack/react-query@latest @tanstack/react-query-devtools@latest
```
```tsx
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
const queryClient = new QueryClient({
defaultOptions: {
queries: {
staleTime: 1000 * 60 * 5, // 5 min
gcTime: 1000 * 60 * 60, // 1 hour
refetchOnWindowFocus: false,
},
},
})
function App() {
return (
<QueryClientProvider client={queryClient}>
<YourApp />
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
)
}
```
### Unified Devtools (Recommended with Multiple TanStack Libraries)
If using Query + Router (or other TanStack libraries), use the unified `TanStackDevtools` shell instead of individual devtools components:
```bash
npm install -D @tanstack/react-devtools
```
```tsx
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { ReactQueryDevtoolsPanel } from '@tanstack/react-query-devtools'
import { TanStackDevtools } from '@tanstack/react-devtools'
function App() {
return (
<QueryClientProvider client={queryClient}>
<YourApp />
<TanStackDevtools
config={{ position: 'bottom-right' }}
plugins={[
{ name: 'TanStack Query', render: <ReactQueryDevtoolsPanel /> },
// Add more plugins: Router, etc.
]}
/>
</QueryClientProvider>
)
}
```
Use `*Panel` variants (`ReactQueryDevtoolsPanel`, `TanStackRouterDevtoolsPanel`) when embedding inside `TanStackDevtools`.
```tsx
import { useQuery, useMutation, useQueryClient, queryOptions } from '@tanstack/react-query'
const todosQueryOptions = queryOptions({
queryKey: ['todos'],
queryFn: async () => {
const res = await fetch('/api/todos')
if (!res.ok) throw new Error('Failed to fetch')
return res.json()
},
})
function useTodos() {
return useQuery(todosQueryOptions)
}
function useAddTodo() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async (newTodo: { title: string }) => {
const res = await fetch('/api/todos', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(newTodo),
})
if (!res.ok) throw new Error('Failed to add')
return res.json()
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['todos'] })
},
})
}
```
## Rule Categories
| Priority | Category | Rule File | Impact |
|----------|----------|-----------|--------|
| CRITICAL | Query Keys | `rules/qk-query-keys.md` | Prevents cache bugs and data inconsistencies |
| CRITICAL | Caching | `rules/cache-configuration.md` | Optimizes performance and data freshness |
| HIGH | Invalidation | `rules/cache-invalidation.md` | Ensures stale data is properly refreshed |
| HIGH | Mutations | `rules/mut-basics.md` | Ensures data integrity after writes |
| HIGH | Optimistic Updates | `rules/mut-optimistic-updates.md` | Responsive UI during mutations |
| HIGH | Error Handling | `rules/err-error-handling.md` | Prevents poor user experiences |
| MEDIUM | Prefetching | `rules/pf-prefetching.md` | Improves perceived performance |
| MEDIUM | Infinite Queries | `rules/inf-infinite-queries.md` | Prevents pagination bugs |
| MEDIUM | SSR/Hydration | `rules/ssr-hydration.md` | Enables proper server rendering |
| MEDIUM | Parallel Queries | `rules/parallel-queries.md` | Dynamic parallel fetching |
| LOW | Performance | `rules/perf-optimization.md` | Reduces unnecessary re-renders |
| LOW | Offline Support | `rules/offline-support.md` | Enables offline-first patterns |
## Critical Rules
### Always Do
- **Object syntax for all hooks**: `useQuery({ queryKey, queryFn, ...options })`
- **Array query keys**: `['todos']`, `['todos', id]`, `['todos', { filter }]`
- **Throw errors in queryFn**: `if (!res.ok) throw new Error('Failed')`
- **isPending for initial loading**: `if (isPending) return <Loading />`
- **Invalidate after mutations**: `onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] })`
- **queryOptions factory**: reuse across `useQuery`, `useSuspenseQuery`, `prefetchQuery`
- **gcTime (not cacheTime)**: renamed in v5
### Never Do
- **v4 array/function syntax**: `useQuery(['todos'], fetchTodos)` — removed in v5
- **Query callbacks**: `onSuccess`/`onError`/`onSettled` removed from queries (still work in mutations) — use `useEffect` instead
- **isLoading for "no data yet"**: meaning changed in v5 — use `isPending`
- **enabled with useSuspenseQuery**: not available — use conditional rendering
- **keepPreviousData**: removed — use `placeholderData: keepPreviousData`
- **refetch() for changed parameters**: include params in queryKey instead, query auto-refetches
## v4→v5 Migration Cheatsheet
| v4 | v5 | Notes |
|----|-----|-------|
| `useQuery(['key'], fn, opts)` | `useQuery({ queryKey, queryFn, ...opts })` | Object syntax only |
| `cacheTime` | `gcTime` | Renamed |
| `isLoading` (no data) | `isPending` | `isLoading` = `isPending && isFetching` |
| `keepPreviousData: true` | `placeholderData: keepPreviousData` | Import `keepPreviousData` helper |
| `useErrorBoundary` | `throwOnError` | Renamed |
| `onSuccess/onError/onSettled` on queries | Removed | Use `useEffect` for side effects |
| `pageParam = 0` default | `initialPageParam: 0` | Required for infinite queries |
| `status: 'loading'` | `status: 'pending'` | Renamed |
| `onError(err, vars, ctx)` | `onError(err, vars, onMutateResult, ctx)` | v5.89+ added 4th param |
## Known Issues (v5.90.x)
- **Streaming SSR hydration mismatch** — `void prefetchQuery` + `useSuspenseQuery` with conditional `isFetching` render causes hydration errors. Workaround: `await` prefetch or don't render based on `fetchStatus`
- **useQuery hydration error with prefetching** — `useQuery` + server prefetch can mismatch `isLoading` between server/client. Use `useSuspenseQuery` instead
- **refetchOnMount ignored for errored queries** — errors are always stale. Use `retryOnMount: false` in addition to `refetchOnMount: false`
- **useMutationState types** — `mutation.state.variables` typed as `unknown` due to fuzzy matching. Cast explicitly in `select` callback
- **invalidateQueries only refetches active queries** — use `refetchType: 'all'` to include inactive queries
- **Readonly query keys break in v5.90.8** — fixed in v5.90.9+
## Key Patterns
```tsx
// Dependent queries (B waits for A)
const { data: user } = useQuery({ queryKey: ['user', id], queryFn: () => fetchUser(id) })
const { data: posts } = useQuery({
queryKey: ['posts', user?.id],
queryFn: () => fetchPosts(user!.id),
enabled: !!user,
})
// Parallel queries
const results = useQueries({
queries: ids.map(id => ({ queryKey: ['item', id], queryFn: () => fetchItem(id) })),
combine: (results) => ({ data: results.map(r => r.data), pending: results.some(r => r.isPending) }),
})
// Prefetch on hover
const handleHover = () => queryClient.prefetchQuery({ queryKey: ['item', id], queryFn: () => fetchItem(id) })
// Infinite scroll
useInfiniteQuery({
queryKey: ['posts'],
queryFn: ({ pageParam }) => fetchPosts(pageParam),
initialPageParam: 0,
getNextPageParam: (lastPage) => lastPage.nextCursor,
})
// Query cancellation
queryFn: async ({ signal }) => {
const res = await fetch(`/api/search?q=${query}`, { signal })
return res.json()
}
// Data transformation
useQuery({ queryKey: ['todos'], queryFn: fetchTodos, select: (data) => data.filter(t => t.completed) })
```Related Skills
tanstack-virtual
TanStack Virtual headless virtualization for React. Use when rendering large lists (100+ items), implementing virtual scroll, building infinite scroll feeds, virtualizing grids or tables, using window-level scrolling, or implementing masonry/lane layouts with @tanstack/react-virtual. Triggers on: useVirtualizer, useWindowVirtualizer, virtual list, virtual scroll, list virtualization.
tanstack-pacer
TanStack Pacer best practices for execution control in React — debouncing, throttling, rate limiting, queuing, and batching. Use when implementing search inputs, scroll handlers, API rate limits, task queues, bulk operations, or any scenario requiring controlled execution timing with reactive state.
tanstack-hotkeys
Guide for implementing keyboard shortcuts in React using @tanstack/react-hotkeys. Use when building hotkey/shortcut features, registering keyboard shortcuts, handling key sequences, recording custom shortcuts, tracking held keys, or formatting hotkeys for display in React applications.
solid
Apply SOLID principles to write flexible, maintainable, and testable code. Use when designing classes, interfaces, and module boundaries. Covers Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion with practical TypeScript examples and detection heuristics.
denji
Manage SVG icons as framework components using Denji CLI. Use when the user needs to add, remove, list, export, import, or manage SVG icons in React, Preact, Solid, Qwik, Vue, or Svelte projects. Triggers include requests to "add an icon", "set up icons", "manage SVG icons", "remove an icon", "list icons", "export icons", "import icons", "dry-run icon add", or any task involving Iconify icons as framework components.
zod
Zod 4 — TypeScript-first schema validation with static type inference. Use when writing Zod schemas, validating data, defining types with Zod, parsing input, creating form validation schemas, defining API request/response schemas, working with z.object, z.string, z.number, z.enum, z.array, z.union, z.discriminatedUnion, z.file, z.jwt, z.email, z.uuid, z.url, z.codec, z.toJSONSchema, z.fromJSONSchema, z.int, z.stringbool, z.templateLiteral, z.record, z.partialRecord, or any other Zod API. Also use when migrating from Zod 3 to Zod 4, or when the user's package.json shows zod@^4. CRITICAL: Always use Zod 4 APIs. Never use deprecated Zod 3 patterns unless user explicitly requests Zod 3 compatibility.
web-design-guidelines
Review UI code for Web Interface Guidelines compliance. Use when asked to "review my UI", "check accessibility", "audit design", "review UX", or "check my site against best practices".
vercel-react-best-practices
React and Next.js performance optimization guidelines from Vercel Engineering. This skill should be used when writing, reviewing, or refactoring React/Next.js code to ensure optimal performance patterns. Triggers on tasks involving React components, Next.js pages, data fetching, bundle optimization, or performance improvements.
vercel-composition-patterns
React composition patterns that scale. Use when refactoring components with boolean prop proliferation, building flexible component libraries, or designing reusable APIs. Triggers on tasks involving compound components, render props, context providers, or component architecture. Includes React 19 API changes.
turborepo
Turborepo monorepo build system guidance. Triggers on: turbo.json, task pipelines, dependsOn, caching, remote cache, the "turbo" CLI, --filter, --affected, CI optimization, environment variables, internal packages, monorepo structure/best practices, and boundaries. Use when user: configures tasks/workflows/pipelines, creates packages, sets up monorepo, shares code between apps, runs changed/affected packages, debugs cache, or has apps/packages directories.
skill-creator
Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Claude's capabilities with specialized knowledge, workflows, or tool integrations.
seo-audit
When the user wants to audit, review, or diagnose SEO issues on their site. Also use when the user mentions "SEO audit," "technical SEO," "why am I not ranking," "SEO issues," "on-page SEO," "meta tags review," or "SEO health check." For building pages at scale to target keywords, see programmatic-seo. For adding structured data, see schema-markup.