Skip to content

Skills for Claude Code CLI such as full stack dev Cloudflare, React, Tailwind v4, and AI integrations.

skill/tanstack-query: Phase 2 knowledge-gap audit (68.3% reduction)

Jeza6d1885cae56
diff --git a/skills/tanstack-query/SKILL.md b/skills/tanstack-query/SKILL.md
index d07f0eb8..ab01d96c 100644
--- a/skills/tanstack-query/SKILL.md
+++ b/skills/tanstack-query/SKILL.md
@@ -9,576 +9,91 @@ license: MIT
# TanStack Query (React Query) v5
-**Status**: Production Ready ✅
-**Last Updated**: 2025-10-22
-**Dependencies**: React 18.0+, TypeScript 4.7+ (recommended)
-**Latest Versions**: @tanstack/[email protected], @tanstack/[email protected]
+**Last Updated**: 2025-11-28
+**Versions**: @tanstack/[email protected], @tanstack/[email protected]
+**Requires**: React 18.0+ (useSyncExternalStore), TypeScript 4.7+ (recommended)
---
-## Quick Start (5 Minutes)
-
-### 1. Install Dependencies
+## Quick Setup
```bash
npm install @tanstack/react-query@latest
npm install -D @tanstack/react-query-devtools@latest
```
-**Why this matters:**
-- TanStack Query v5 requires React 18+ (uses useSyncExternalStore)
-- DevTools are essential for debugging queries and mutations
-- v5 has breaking changes from v4 - use latest for all fixes
-
-### 2. Set Up QueryClient Provider
+### Step 2: Provider + Config
```tsx
-// src/main.tsx or src/index.tsx
-import { StrictMode } from 'react'
-import { createRoot } from 'react-dom/client'
+// src/main.tsx
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
-import App from './App'
-// Create a client
const queryClient = new QueryClient({
defaultOptions: {
queries: {
- staleTime: 1000 * 60 * 5, // 5 minutes
- gcTime: 1000 * 60 * 60, // 1 hour (formerly cacheTime)
- retry: 1,
- refetchOnWindowFocus: false,
- },
- },
-})
-
-createRoot(document.getElementById('root')!).render(
- <StrictMode>
- <QueryClientProvider client={queryClient}>
- <App />
- <ReactQueryDevtools initialIsOpen={false} />
- </QueryClientProvider>
- </StrictMode>
-)
-```
-
-**CRITICAL:**
-- Wrap entire app with `QueryClientProvider`
-- Configure `staleTime` to avoid excessive refetches (default is 0)
-- Use `gcTime` (not `cacheTime` - renamed in v5)
-- DevTools should be inside provider
-
-### 3. Create First Query
-
-```tsx
-// src/hooks/useTodos.ts
-import { useQuery } from '@tanstack/react-query'
-
-type Todo = {
- id: number
- title: string
- completed: boolean
-}
-
-async function fetchTodos(): Promise<Todo[]> {
- const response = await fetch('/api/todos')
- if (!response.ok) {
- throw new Error('Failed to fetch todos')
- }
- return response.json()
-}
-
-export function useTodos() {
- return useQuery({
- queryKey: ['todos'],
- queryFn: fetchTodos,
- })
-}
-
-// Usage in component:
-function TodoList() {
- const { data, isPending, isError, error } = useTodos()
-
- if (isPending) return <div>Loading...</div>
- if (isError) return <div>Error: {error.message}</div>
-
- return (
- <ul>
- {data.map(todo => (
- <li key={todo.id}>{todo.title}</li>
- ))}
- </ul>
- )
-}
-```
-
-**CRITICAL:**
-- v5 requires object syntax: `useQuery({ queryKey, queryFn })`
-- Use `isPending` (not `isLoading` - that now means "pending AND fetching")
-- Always throw errors in queryFn for proper error handling
-- QueryKey should be array for consistent cache keys
-
-### 4. Create First Mutation
-
-```tsx
-// src/hooks/useAddTodo.ts
-import { useMutation, useQueryClient } from '@tanstack/react-query'
-
-type NewTodo = {
- title: string
-}
-
-async function addTodo(newTodo: NewTodo) {
- const response = await fetch('/api/todos', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify(newTodo),
- })
- if (!response.ok) throw new Error('Failed to add todo')
- return response.json()
-}
-
-export function useAddTodo() {
- const queryClient = useQueryClient()
-
- return useMutation({
- mutationFn: addTodo,
- onSuccess: () => {
- // Invalidate and refetch todos
- queryClient.invalidateQueries({ queryKey: ['todos'] })
- },
- })
-}
-
-// Usage in component:
-function AddTodoForm() {
- const { mutate, isPending } = useAddTodo()
-
- const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
- e.preventDefault()
- const formData = new FormData(e.currentTarget)
- mutate({ title: formData.get('title') as string })
- }
-
- return (
- <form onSubmit={handleSubmit}>
- <input name="title" required />
- <button type="submit" disabled={isPending}>
- {isPending ? 'Adding...' : 'Add Todo'}
- </button>
- </form>
- )
-}
-```
-
-**Why this works:**
-- Mutations use callbacks (`onSuccess`, `onError`, `onSettled`) - queries don't
-- `invalidateQueries` triggers background refetch
-- Mutations don't cache by default (correct behavior)
-
----
-
-## The 7-Step Setup Process
-
-### Step 1: Install Dependencies
-
-```bash
-# Core library (required)
-npm install @tanstack/react-query
-
-# DevTools (highly recommended for development)
-npm install -D @tanstack/react-query-devtools
-
-# Optional: ESLint plugin for best practices
-npm install -D @tanstack/eslint-plugin-query
-```
-
-**Package roles:**
-- `@tanstack/react-query` - Core React hooks and QueryClient
-- `@tanstack/react-query-devtools` - Visual debugger (dev only, tree-shakeable)
-- `@tanstack/eslint-plugin-query` - Catches common mistakes
-
-**Version requirements:**
-- React 18.0 or higher (uses `useSyncExternalStore`)
-- TypeScript 4.7+ for best type inference (optional but recommended)
-
-### Step 2: Configure QueryClient
-
-```tsx
-// src/lib/query-client.ts
-import { QueryClient } from '@tanstack/react-query'
-
-export const queryClient = new QueryClient({
- defaultOptions: {
- queries: {
- // How long data is considered fresh (won't refetch during this time)
- staleTime: 1000 * 60 * 5, // 5 minutes
-
- // How long inactive data stays in cache before garbage collection
+ staleTime: 1000 * 60 * 5, // 5 min
gcTime: 1000 * 60 * 60, // 1 hour (v5: renamed from cacheTime)
-
- // Retry failed requests (0 on server, 3 on client by default)
- retry: (failureCount, error) => {
- if (error instanceof Response && error.status === 404) return false
- return failureCount < 3
- },
-
- // Refetch on window focus (can be annoying during dev)
refetchOnWindowFocus: false,
-
- // Refetch on network reconnect
- refetchOnReconnect: true,
-
- // Refetch on component mount if data is stale
- refetchOnMount: true,
- },
- mutations: {
- // Retry mutations on failure (usually don't want this)
- retry: 0,
},
},
})
-```
-
-**Key configuration decisions:**
-**staleTime vs gcTime:**
-- `staleTime`: How long until data is considered "stale" and might refetch
- - `0` (default): Data is immediately stale, refetches on mount/focus
- - `1000 * 60 * 5`: Data fresh for 5 min, no refetch during this time
- - `Infinity`: Data never stale, manual invalidation only
-- `gcTime`: How long unused data stays in cache
- - `1000 * 60 * 5` (default): 5 minutes
- - `Infinity`: Never garbage collect (memory leak risk)
-
-**When to refetch:**
-- `refetchOnWindowFocus: true` - Good for frequently changing data (stock prices)
-- `refetchOnWindowFocus: false` - Good for stable data or during development
-- `refetchOnMount: true` - Ensures fresh data when component mounts
-- `refetchOnReconnect: true` - Refetch after network reconnect
-
-### Step 3: Wrap App with Provider
-
-```tsx
-// src/main.tsx
-import { StrictMode } from 'react'
-import { createRoot } from 'react-dom/client'
-import { QueryClientProvider } from '@tanstack/react-query'
-import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
-import { queryClient } from './lib/query-client'
-import App from './App'
-
-createRoot(document.getElementById('root')!).render(
- <StrictMode>
- <QueryClientProvider client={queryClient}>
- <App />
- <ReactQueryDevtools
- initialIsOpen={false}
- buttonPosition="bottom-right"
- />
- </QueryClientProvider>
- </StrictMode>
-)
+<QueryClientProvider client={queryClient}>
+ <App />
+ <ReactQueryDevtools initialIsOpen={false} />
+</QueryClientProvider>
```
-**Provider placement:**
-- Must wrap all components that use TanStack Query hooks
-- DevTools must be inside provider
-- Only one QueryClient instance for entire app
-
-**DevTools configuration:**
-- `initialIsOpen={false}` - Collapsed by default
-- `buttonPosition="bottom-right"` - Where to show toggle button
-- Automatically removed in production builds (tree-shaken)
-
-### Step 4: Create Custom Query Hooks
-
-**Pattern: Reusable Query Hooks**
+### Step 3: Query + Mutation Hooks
```tsx
-// src/api/todos.ts - API functions
-export type Todo = {
- id: number
- title: string
- completed: boolean
-}
-
-export async function fetchTodos(): Promise<Todo[]> {
- const response = await fetch('/api/todos')
- if (!response.ok) {
- throw new Error(`Failed to fetch todos: ${response.statusText}`)
- }
- return response.json()
-}
-
-export async function fetchTodoById(id: number): Promise<Todo> {
- const response = await fetch(`/api/todos/${id}`)
- if (!response.ok) {
- throw new Error(`Failed to fetch todo ${id}: ${response.statusText}`)
- }
- return response.json()
-}
-
-// src/hooks/useTodos.ts - Query hooks
-import { useQuery, queryOptions } from '@tanstack/react-query'
-import { fetchTodos, fetchTodoById } from '../api/todos'
+// src/hooks/useTodos.ts
+import { useQuery, useMutation, useQueryClient, queryOptions } from '@tanstack/react-query'
-// Query options factory (v5 pattern for reusability)
+// Query options factory (v5 pattern)
export const todosQueryOptions = queryOptions({
queryKey: ['todos'],
- queryFn: fetchTodos,
- staleTime: 1000 * 60, // 1 minute
+ queryFn: async () => {
+ const res = await fetch('/api/todos')
+ if (!res.ok) throw new Error('Failed to fetch')
+ return res.json()
+ },
})
export function useTodos() {
return useQuery(todosQueryOptions)
}
-export function useTodo(id: number) {
- return useQuery({
- queryKey: ['todos', id],
- queryFn: () => fetchTodoById(id),
- enabled: !!id, // Only fetch if id is truthy
- })
-}
-```
-
-**Why use queryOptions factory:**
-- Type inference works perfectly
-- Reusable across `useQuery`, `useSuspenseQuery`, `prefetchQuery`
-- Consistent queryKey and queryFn everywhere
-- Easier to test and maintain
-
-**Query key structure:**
-- `['todos']` - List of all todos
-- `['todos', id]` - Single todo detail
-- `['todos', 'filters', { status: 'completed' }]` - Filtered list
-- More specific keys are subsets (invalidating `['todos']` invalidates all)
-
-### Step 5: Implement Mutations with Optimistic Updates
-
-```tsx
-// src/hooks/useTodoMutations.ts
-import { useMutation, useQueryClient } from '@tanstack/react-query'
-import type { Todo } from '../api/todos'
-
-type AddTodoInput = {
- title: string
-}
-
-type UpdateTodoInput = {
- id: number
- completed: boolean
-}
-
export function useAddTodo() {
const queryClient = useQueryClient()
-
return useMutation({
- mutationFn: async (newTodo: AddTodoInput) => {
- const response = await fetch('/api/todos', {
+ mutationFn: async (newTodo) => {
+ const res = await fetch('/api/todos', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(newTodo),
})
- if (!response.ok) throw new Error('Failed to add todo')
- return response.json()
+ if (!res.ok) throw new Error('Failed to add')
+ return res.json()
},
-
- // Optimistic update
- onMutate: async (newTodo) => {
- // Cancel outgoing refetches
- await queryClient.cancelQueries({ queryKey: ['todos'] })
-
- // Snapshot previous value
- const previousTodos = queryClient.getQueryData<Todo[]>(['todos'])
-
- // Optimistically update
- queryClient.setQueryData<Todo[]>(['todos'], (old = []) => [
- ...old,
- { id: Date.now(), ...newTodo, completed: false },
- ])
-
- // Return context with snapshot
- return { previousTodos }
- },
-
- // Rollback on error
- onError: (err, newTodo, context) => {
- queryClient.setQueryData(['todos'], context?.previousTodos)
- },
-
- // Always refetch after error or success
- onSettled: () => {
- queryClient.invalidateQueries({ queryKey: ['todos'] })
- },
- })
-}
-
-export function useUpdateTodo() {
- const queryClient = useQueryClient()
-
- return useMutation({
- mutationFn: async ({ id, completed }: UpdateTodoInput) => {
- const response = await fetch(`/api/todos/${id}`, {
- method: 'PATCH',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ completed }),
- })
- if (!response.ok) throw new Error('Failed to update todo')
- return response.json()
- },
-
- onSuccess: (updatedTodo) => {
- // Update the specific todo in cache
- queryClient.setQueryData<Todo>(['todos', updatedTodo.id], updatedTodo)
-
- // Invalidate list to refetch
+ onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['todos'] })
},
})
}
-export function useDeleteTodo() {
- const queryClient = useQueryClient()
-
- return useMutation({
- mutationFn: async (id: number) => {
- const response = await fetch(`/api/todos/${id}`, { method: 'DELETE' })
- if (!response.ok) throw new Error('Failed to delete todo')
- },
-
- onSuccess: (_, deletedId) => {
- queryClient.setQueryData<Todo[]>(['todos'], (old = []) =>
- old.filter(todo => todo.id !== deletedId)
- )
- },
- })
-}
-```
-
-**Optimistic update pattern:**
-1. `onMutate`: Cancel queries, snapshot old data, update cache optimistically
-2. `onError`: Rollback to snapshot if mutation fails
-3. `onSettled`: Refetch to ensure cache matches server
-
-**When to use:**
-- Immediate UI feedback for better UX
-- Low-risk mutations (todo toggle, like button)
-- Avoid for critical data (payments, account settings)
-
-### Step 6: Set Up DevTools
-
-```tsx
-// Already set up in main.tsx, but here are advanced options:
-
-import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
-
-<ReactQueryDevtools
- initialIsOpen={false}
- buttonPosition="bottom-right"
- position="bottom"
-
- // Custom toggle button
- toggleButtonProps={{
- style: { marginBottom: '4rem' },
- }}
-
- // Custom panel styles
- panelProps={{
- style: { height: '400px' },
- }}
-
- // Only show in dev (already tree-shaken in production)
- // But can add explicit check:
- // {import.meta.env.DEV && <ReactQueryDevtools />}
-/>
-```
-
-**DevTools features:**
-- View all queries and their states
-- See query cache contents
-- Manually refetch queries
-- View mutations in flight
-- Inspect query dependencies
-- Export state for debugging
-
-### Step 7: Configure Error Boundaries
-
-```tsx
-// src/components/ErrorBoundary.tsx
-import { Component, type ReactNode } from 'react'
-import { QueryErrorResetBoundary, useQueryErrorResetBoundary } from '@tanstack/react-query'
-
-type Props = { children: ReactNode }
-type State = { hasError: boolean }
-
-class ErrorBoundaryClass extends Component<Props, State> {
- constructor(props: Props) {
- super(props)
- this.state = { hasError: false }
- }
-
- static getDerivedStateFromError() {
- return { hasError: true }
- }
-
- render() {
- if (this.state.hasError) {
- return (
- <div>
- <h2>Something went wrong</h2>
- <button onClick={() => this.setState({ hasError: false })}>
- Try again
- </button>
- </div>
- )
- }
- return this.props.children
- }
-}
-
-// Wrapper with TanStack Query error reset
-export function ErrorBoundary({ children }: Props) {
- return (
- <QueryErrorResetBoundary>
- {({ reset }) => (
- <ErrorBoundaryClass onReset={reset}>
- {children}
- </ErrorBoundaryClass>
- )}
- </QueryErrorResetBoundary>
- )
-}
-
-// Usage with throwOnError option:
-function useTodosWithErrorBoundary() {
- return useQuery({
- queryKey: ['todos'],
- queryFn: fetchTodos,
- throwOnError: true, // Throw errors to error boundary
- })
-}
+// Usage:
+function TodoList() {
+ const { data, isPending, isError, error } = useTodos()
+ const { mutate } = useAddTodo()
-// Or conditional:
-function useTodosConditionalError() {
- return useQuery({
- queryKey: ['todos'],
- queryFn: fetchTodos,
- throwOnError: (error, query) => {
- // Only throw server errors, handle network errors locally
- return error instanceof Response && error.status >= 500
- },
- })
+ if (isPending) return <div>Loading...</div>
+ if (isError) return <div>Error: {error.message}</div>
+ return <ul>{data.map(todo => <li key={todo.id}>{todo.title}</li>)}</ul>
}
```
-**Error handling strategies:**
-- Local handling: Use `isError` and `error` from query
-- Global handling: Use error boundaries with `throwOnError`
-- Mixed: Conditional `throwOnError` function
-- Centralized: Use `QueryCache` global error handlers
-
---
## Critical Rules
@@ -935,651 +450,54 @@ const { error } = useQuery({
---
-## Configuration Files Reference
-
-### package.json (Full Example)
-
-```json
-{
- "name": "my-app",
- "version": "1.0.0",
- "type": "module",
- "scripts": {
- "dev": "vite",
- "build": "tsc && vite build",
- "preview": "vite preview"
- },
- "dependencies": {
- "react": "^18.3.1",
- "react-dom": "^18.3.1",
- "@tanstack/react-query": "^5.90.5"
- },
- "devDependencies": {
- "@tanstack/react-query-devtools": "^5.90.2",
- "@tanstack/eslint-plugin-query": "^5.90.2",
- "@types/react": "^18.3.12",
- "@types/react-dom": "^18.3.1",
- "@vitejs/plugin-react": "^4.3.4",
- "typescript": "^5.6.3",
- "vite": "^6.0.1"
- }
-}
-```
-
-**Why these versions:**
-- React 18.3.1 - Required for useSyncExternalStore
-- TanStack Query 5.90.5 - Latest stable with all v5 fixes
-- DevTools 5.90.2 - Matched to query version
-- TypeScript 5.6.3 - Best type inference for query types
-
-### tsconfig.json (TypeScript Configuration)
-
-```json
-{
- "compilerOptions": {
- "target": "ES2020",
- "useDefineForClassFields": true,
- "lib": ["ES2020", "DOM", "DOM.Iterable"],
- "module": "ESNext",
- "skipLibCheck": true,
-
- /* Bundler mode */
- "moduleResolution": "bundler",
- "allowImportingTsExtensions": true,
- "isolatedModules": true,
- "moduleDetection": "force",
- "noEmit": true,
- "jsx": "react-jsx",
-
- /* Linting */
- "strict": true,
- "noUnusedLocals": true,
- "noUnusedParameters": true,
- "noFallthroughCasesInSwitch": true,
-
- /* TanStack Query specific */
- "esModuleInterop": true,
- "resolveJsonModule": true
- },
- "include": ["src"]
-}
-```
-
-### .eslintrc.cjs (ESLint Configuration with TanStack Query Plugin)
-
-```javascript
-module.exports = {
- root: true,
- env: { browser: true, es2020: true },
- extends: [
- 'eslint:recommended',
- 'plugin:@typescript-eslint/recommended',
- 'plugin:react-hooks/recommended',
- 'plugin:@tanstack/eslint-plugin-query/recommended',
- ],
- ignorePatterns: ['dist', '.eslintrc.cjs'],
- parser: '@typescript-eslint/parser',
- plugins: ['react-refresh', '@tanstack/query'],
- rules: {
- 'react-refresh/only-export-components': [
- 'warn',
- { allowConstantExport: true },
- ],
- },
-}
-```
-
-**ESLint plugin catches:**
-- Query keys as references instead of inline
-- Missing queryFn
-- Using v4 patterns in v5
-- Incorrect dependencies in useEffect
-
----
-
-## Common Patterns
-
-### Pattern 1: Dependent Queries
-
-```tsx
-// Fetch user, then fetch user's posts
-function UserPosts({ userId }: { userId: number }) {
- const { data: user } = useQuery({
- queryKey: ['users', userId],
- queryFn: () => fetchUser(userId),
- })
-
- const { data: posts } = useQuery({
- queryKey: ['users', userId, 'posts'],
- queryFn: () => fetchUserPosts(userId),
- enabled: !!user, // Only fetch posts after user is loaded
- })
-
- if (!user) return <div>Loading user...</div>
- if (!posts) return <div>Loading posts...</div>
-
- return (
- <div>
- <h1>{user.name}</h1>
- <ul>
- {posts.map(post => (
- <li key={post.id}>{post.title}</li>
- ))}
- </ul>
- </div>
- )
-}
-```
-
-**When to use**: Query B depends on data from Query A
-
-### Pattern 2: Parallel Queries with useQueries
-
-```tsx
-// Fetch multiple todos in parallel
-function TodoDetails({ ids }: { ids: number[] }) {
- const results = useQueries({
- queries: ids.map(id => ({
- queryKey: ['todos', id],
- queryFn: () => fetchTodo(id),
- })),
- })
-
- const isLoading = results.some(result => result.isPending)
- const isError = results.some(result => result.isError)
-
- if (isLoading) return <div>Loading...</div>
- if (isError) return <div>Error loading todos</div>
-
- return (
- <ul>
- {results.map((result, i) => (
- <li key={ids[i]}>{result.data?.title}</li>
- ))}
- </ul>
- )
-}
-```
-
-**When to use**: Fetch multiple independent queries in parallel
-
-### Pattern 3: Prefetching
-
-```tsx
-import { useQueryClient } from '@tanstack/react-query'
-import { todosQueryOptions } from './hooks/useTodos'
-
-function TodoListWithPrefetch() {
- const queryClient = useQueryClient()
- const { data: todos } = useTodos()
-
- const prefetchTodo = (id: number) => {
- queryClient.prefetchQuery({
- queryKey: ['todos', id],
- queryFn: () => fetchTodo(id),
- staleTime: 1000 * 60 * 5, // 5 minutes
- })
- }
-
- return (
- <ul>
- {todos?.map(todo => (
- <li
- key={todo.id}
- onMouseEnter={() => prefetchTodo(todo.id)}
- >
- <Link to={`/todos/${todo.id}`}>{todo.title}</Link>
- </li>
- ))}
- </ul>
- )
-}
-```
-
-**When to use**: Preload data before user navigates (on hover, on mount)
-
-### Pattern 4: Infinite Scroll
-
-```tsx
-import { useInfiniteQuery } from '@tanstack/react-query'
-import { useEffect, useRef } from 'react'
-
-type Page = {
- data: Todo[]
- nextCursor: number | null
-}
-
-async function fetchTodosPage({ pageParam }: { pageParam: number }): Promise<Page> {
- const response = await fetch(`/api/todos?cursor=${pageParam}&limit=20`)
- return response.json()
-}
-
-function InfiniteTodoList() {
- const {
- data,
- fetchNextPage,
- hasNextPage,
- isFetchingNextPage,
- } = useInfiniteQuery({
- queryKey: ['todos', 'infinite'],
- queryFn: fetchTodosPage,
- initialPageParam: 0,
- getNextPageParam: (lastPage) => lastPage.nextCursor,
- })
-
- const loadMoreRef = useRef<HTMLDivElement>(null)
-
- useEffect(() => {
- const observer = new IntersectionObserver(
- (entries) => {
- if (entries[0].isIntersecting && hasNextPage) {
- fetchNextPage()
- }
- },
- { threshold: 0.1 }
- )
-
- if (loadMoreRef.current) {
- observer.observe(loadMoreRef.current)
- }
-
- return () => observer.disconnect()
- }, [fetchNextPage, hasNextPage])
-
- return (
- <div>
- {data?.pages.map((page, i) => (
- <div key={i}>
- {page.data.map(todo => (
- <div key={todo.id}>{todo.title}</div>
- ))}
- </div>
- ))}
-
- <div ref={loadMoreRef}>
- {isFetchingNextPage && <div>Loading more...</div>}
- </div>
- </div>
- )
-}
-```
-
-**When to use**: Paginated lists with infinite scroll
-
-### Pattern 5: Query Cancellation
-
-```tsx
-function SearchTodos() {
- const [search, setSearch] = useState('')
-
- const { data } = useQuery({
- queryKey: ['todos', 'search', search],
- queryFn: async ({ signal }) => {
- const response = await fetch(`/api/todos?q=${search}`, { signal })
- return response.json()
- },
- enabled: search.length > 2, // Only search if 3+ characters
- })
-
- return (
- <div>
- <input
- value={search}
- onChange={e => setSearch(e.target.value)}
- placeholder="Search todos..."
- />
- {data && (
- <ul>
- {data.map(todo => (
- <li key={todo.id}>{todo.title}</li>
- ))}
- </ul>
- )}
- </div>
- )
-}
-```
-
-**How it works:**
-- When queryKey changes, previous query is automatically cancelled
-- Pass `signal` to fetch for proper cleanup
-- Browser aborts pending fetch requests
-
----
-
-## Using Bundled Resources
-
-### Templates (templates/)
-
-Complete, copy-ready code examples:
-
-- `package.json` - Dependencies with exact versions
-- `query-client-config.ts` - QueryClient setup with best practices
-- `provider-setup.tsx` - App wrapper with QueryClientProvider
-- `use-query-basic.tsx` - Basic useQuery hook pattern
-- `use-mutation-basic.tsx` - Basic useMutation hook
-- `use-mutation-optimistic.tsx` - Optimistic update pattern
-- `use-infinite-query.tsx` - Infinite scroll pattern
-- `custom-hooks-pattern.tsx` - Reusable query hooks with queryOptions
-- `error-boundary.tsx` - Error boundary with query reset
-- `devtools-setup.tsx` - DevTools configuration
-
-**Example Usage:**
-```bash
-# Copy query client config
-cp ~/.claude/skills/tanstack-query/templates/query-client-config.ts src/lib/
-
-# Copy provider setup
-cp ~/.claude/skills/tanstack-query/templates/provider-setup.tsx src/main.tsx
-```
-
-### References (references/)
-
-Deep-dive documentation loaded when needed:
-
-- `v4-to-v5-migration.md` - Complete v4 → v5 migration guide
-- `best-practices.md` - Request waterfalls, caching strategies, performance
-- `common-patterns.md` - Reusable queries, optimistic updates, infinite scroll
-- `typescript-patterns.md` - Type safety, generics, type inference
-- `testing.md` - Testing with MSW, React Testing Library
-- `top-errors.md` - All 8+ errors with solutions
-
-**When Claude should load these:**
-- `v4-to-v5-migration.md` - When migrating existing React Query v4 project
-- `best-practices.md` - When optimizing performance or avoiding waterfalls
-- `common-patterns.md` - When implementing specific features (infinite scroll, etc.)
-- `typescript-patterns.md` - When dealing with TypeScript errors or type inference
-- `testing.md` - When writing tests for components using TanStack Query
-- `top-errors.md` - When encountering errors not covered in main SKILL.md
-
----
-
-## Advanced Topics
-
-### Data Transformations with select
-
-```tsx
-// Only subscribe to specific slice of data
-function TodoCount() {
- const { data: count } = useQuery({
- queryKey: ['todos'],
- queryFn: fetchTodos,
- select: (data) => data.length, // Only re-render when count changes
- })
-
- return <div>Total todos: {count}</div>
-}
-
-// Transform data shape
-function CompletedTodoTitles() {
- const { data: titles } = useQuery({
- queryKey: ['todos'],
- queryFn: fetchTodos,
- select: (data) =>
- data
- .filter(todo => todo.completed)
- .map(todo => todo.title),
- })
-
- return (
- <ul>
- {titles?.map((title, i) => (
- <li key={i}>{title}</li>
- ))}
- </ul>
- )
-}
-```
-
-**Benefits:**
-- Component only re-renders when selected data changes
-- Reduces memory usage (less data stored in component state)
-- Keeps query cache unchanged (other components get full data)
-
-### Request Waterfalls (Anti-Pattern)
-
-```tsx
-// ❌ BAD: Sequential waterfalls
-function BadUserProfile({ userId }: { userId: number }) {
- const { data: user } = useQuery({
- queryKey: ['users', userId],
- queryFn: () => fetchUser(userId),
- })
-
- const { data: posts } = useQuery({
- queryKey: ['posts', user?.id],
- queryFn: () => fetchPosts(user!.id),
- enabled: !!user,
- })
-
- const { data: comments } = useQuery({
- queryKey: ['comments', posts?.[0]?.id],
- queryFn: () => fetchComments(posts![0].id),
- enabled: !!posts && posts.length > 0,
- })
-
- // Each query waits for previous one = slow!
-}
-
-// ✅ GOOD: Fetch in parallel when possible
-function GoodUserProfile({ userId }: { userId: number }) {
- const { data: user } = useQuery({
- queryKey: ['users', userId],
- queryFn: () => fetchUser(userId),
- })
-
- // Fetch posts AND comments in parallel
- const { data: posts } = useQuery({
- queryKey: ['posts', userId],
- queryFn: () => fetchPosts(userId), // Don't wait for user
- })
-
- const { data: comments } = useQuery({
- queryKey: ['comments', userId],
- queryFn: () => fetchUserComments(userId), // Don't wait for posts
- })
-
- // All 3 queries run in parallel = fast!
-}
-```
-
-### Server State vs Client State
+## Key Patterns
+**Dependent Queries** (Query B waits for Query A):
```tsx
-// ❌ Don't use TanStack Query for client-only state
-const { data: isModalOpen, setData: setIsModalOpen } = useMutation(...)
-
-// ✅ Use useState for client state
-const [isModalOpen, setIsModalOpen] = useState(false)
-
-// ✅ Use TanStack Query for server state
-const { data: todos } = useQuery({
- queryKey: ['todos'],
- queryFn: fetchTodos,
+const { data: posts } = useQuery({
+ queryKey: ['users', userId, 'posts'],
+ queryFn: () => fetchUserPosts(userId),
+ enabled: !!user, // Wait for user
})
```
-**Rule of thumb:**
-- Server state: Use TanStack Query (data from API)
-- Client state: Use useState/useReducer (local UI state)
-- Global client state: Use Zustand/Context (theme, auth token)
-
----
-
-## Dependencies
-
-**Required**:
-- `@tanstack/[email protected]` - Core library
-- `[email protected]+` - Uses useSyncExternalStore hook
-- `[email protected]+` - React DOM renderer
-
-**Recommended**:
-- `@tanstack/[email protected]` - Visual debugger (dev only)
-- `@tanstack/[email protected]` - ESLint rules for best practices
-- `[email protected]+` - For type safety and inference
-
-**Optional**:
-- `@tanstack/query-sync-storage-persister` - Persist cache to localStorage
-- `@tanstack/query-async-storage-persister` - Persist to AsyncStorage (React Native)
-
----
-
-## Official Documentation
-
-- **TanStack Query Docs**: https://tanstack.com/query/latest
-- **React Integration**: https://tanstack.com/query/latest/docs/framework/react/overview
-- **v5 Migration Guide**: https://tanstack.com/query/latest/docs/framework/react/guides/migrating-to-v5
-- **API Reference**: https://tanstack.com/query/latest/docs/framework/react/reference/useQuery
-- **Context7 Library ID**: `/websites/tanstack_query`
-- **GitHub Repository**: https://github.com/TanStack/query
-- **Discord Community**: https://tlinz.com/discord
-
----
-
-## Package Versions (Verified 2025-10-22)
-
-```json
-{
- "dependencies": {
- "@tanstack/react-query": "^5.90.5"
- },
- "devDependencies": {
- "@tanstack/react-query-devtools": "^5.90.2",
- "@tanstack/eslint-plugin-query": "^5.90.2"
- }
-}
-```
-
-**Verification:**
-- `npm view @tanstack/react-query version` → 5.90.5
-- `npm view @tanstack/react-query-devtools version` → 5.90.2
-- Last checked: 2025-10-22
-
----
-
-## Production Example
-
-This skill is based on production patterns used in:
-- **Build Time**: ~6 hours research + development
-- **Errors Prevented**: 8 (all documented v5 migration issues)
-- **Token Efficiency**: ~65% savings vs manual setup
-- **Validation**: ✅ All patterns tested with TypeScript strict mode
-
----
-
-## Troubleshooting
-
-### Problem: "useQuery is not a function" or type errors
-**Solution**: Ensure you're using v5 object syntax:
+**Parallel Queries** (fetch multiple at once):
```tsx
-// ✅ Correct:
-useQuery({ queryKey: ['todos'], queryFn: fetchTodos })
-
-// ❌ Wrong (v4 syntax):
-useQuery(['todos'], fetchTodos)
-```
-
-### Problem: Callbacks (onSuccess, onError) not working on queries
-**Solution**: Removed in v5. Use `useEffect` or move to mutations:
-```tsx
-// ✅ For queries:
-const { data } = useQuery({ queryKey: ['todos'], queryFn: fetchTodos })
-useEffect(() => {
- if (data) {
- // Handle success
- }
-}, [data])
-
-// ✅ For mutations (still work):
-useMutation({
- mutationFn: addTodo,
- onSuccess: () => { /* ... */ },
+const results = useQueries({
+ queries: ids.map(id => ({ queryKey: ['todos', id], queryFn: () => fetchTodo(id) })),
})
```
-### Problem: isLoading always false even during initial load
-**Solution**: Use `isPending` instead:
-```tsx
-const { isPending, isLoading, isFetching } = useQuery(...)
-// isPending = no data yet
-// isLoading = isPending && isFetching
-// isFetching = any fetch in progress
-```
-
-### Problem: cacheTime option not recognized
-**Solution**: Renamed to `gcTime` in v5:
-```tsx
-gcTime: 1000 * 60 * 60 // 1 hour
-```
-
-### Problem: useSuspenseQuery with enabled option gives type error
-**Solution**: `enabled` not available with suspense. Use conditional rendering:
-```tsx
-{id && <TodoComponent id={id} />}
-```
-
-### Problem: Data not refetching after mutation
-**Solution**: Invalidate queries in `onSuccess`:
+**Prefetching** (preload on hover):
```tsx
-onSuccess: () => {
- queryClient.invalidateQueries({ queryKey: ['todos'] })
-}
+queryClient.prefetchQuery({ queryKey: ['todo', id], queryFn: () => fetchTodo(id) })
```
-### Problem: Infinite query requires initialPageParam
-**Solution**: Always provide `initialPageParam` in v5:
+**Infinite Scroll** (useInfiniteQuery):
```tsx
useInfiniteQuery({
- queryKey: ['projects'],
- queryFn: ({ pageParam }) => fetchProjects(pageParam),
- initialPageParam: 0, // Required
+ queryKey: ['todos', 'infinite'],
+ queryFn: ({ pageParam }) => fetchTodosPage(pageParam),
+ initialPageParam: 0, // Required in v5
getNextPageParam: (lastPage) => lastPage.nextCursor,
})
```
-### Problem: keepPreviousData not working
-**Solution**: Replaced with `placeholderData`:
+**Query Cancellation** (auto-cancel on queryKey change):
```tsx
-import { keepPreviousData } from '@tanstack/react-query'
-
-useQuery({
- queryKey: ['todos', page],
- queryFn: () => fetchTodos(page),
- placeholderData: keepPreviousData,
-})
+queryFn: async ({ signal }) => {
+ const res = await fetch(`/api/todos?q=${search}`, { signal })
+ return res.json()
+}
```
----
+**Data Transformation** (select):
+```tsx
+select: (data) => data.filter(todo => todo.completed)
+```
-## Complete Setup Checklist
-
-Use this checklist to verify your setup:
-
-- [ ] Installed @tanstack/[email protected]+
-- [ ] Installed @tanstack/react-query-devtools (dev dependency)
-- [ ] Created QueryClient with configured defaults
-- [ ] Wrapped app with QueryClientProvider
-- [ ] Added ReactQueryDevtools component
-- [ ] Created first query using object syntax
-- [ ] Tested isPending and error states
-- [ ] Created first mutation with onSuccess handler
-- [ ] Set up query invalidation after mutations
-- [ ] Configured staleTime and gcTime appropriately
-- [ ] Using array queryKey consistently
-- [ ] Throwing errors in queryFn
-- [ ] No v4 syntax (function overloads)
-- [ ] No query callbacks (onSuccess, onError on queries)
-- [ ] Using isPending (not isLoading) for initial load
-- [ ] DevTools working in development
-- [ ] TypeScript types working correctly
-- [ ] Production build succeeds
+**Avoid Request Waterfalls**: Fetch in parallel when possible (don't chain queries unless truly dependent)
---
-**Questions? Issues?**
-
-1. Check `references/top-errors.md` for complete error solutions
-2. Verify all steps in the setup process
-3. Check official docs: https://tanstack.com/query/latest
-4. Ensure using v5 syntax (object syntax, gcTime, isPending)
-5. Join Discord: https://tlinz.com/discord
+**Official Docs**: https://tanstack.com/query/latest | **v5 Migration**: https://tanstack.com/query/latest/docs/framework/react/guides/migrating-to-v5 | **GitHub**: https://github.com/TanStack/query | **Context7**: `/websites/tanstack_query`