TanStack
Overview
The TanStack ecosystem provides type-safe client libraries for React: Query for declarative data fetching and caching, Router for fully typed routing with search parameters, Table for headless data tables with sorting/filtering/pagination, and Virtual for rendering large lists efficiently.
Instructions
- When fetching data, use
useQuery() with hierarchical query keys (e.g., ["users", userId, "posts"]) and configure staleTime based on freshness needs (0 for real-time, 5 minutes for dashboards, Infinity for static data).
- When performing mutations, use
useMutation() with onSuccess for cache invalidation via queryClient.invalidateQueries(), and onMutate for optimistic updates with rollback.
- When building tables, use TanStack Table's headless approach with typed column definitions, and combine with
@tanstack/react-virtual for datasets with 10,000+ rows.
- When routing, use TanStack Router for fully typed route parameters and search params with Zod validation, file-based routes with automatic type generation, and route-level data loading.
- When handling pagination, use
useInfiniteQuery() for infinite scroll or cursor-based patterns, and server-side pagination in TanStack Table.
- When prefetching, use
queryClient.prefetchQuery() for anticipated navigation and useSuspenseQuery() for React Suspense integration.
- When virtualizing lists, use
@tanstack/react-virtual with estimateSize for scroll position prediction and support for dynamic, variable-height items.
Examples
Example 1: Build a data dashboard with Query and Table
User request: "Create a dashboard with server-paginated data table and real-time stats"
Actions:
- Set up TanStack Query with appropriate
staleTime and refetch intervals for stats
- Define TanStack Table with typed columns, server-side sorting and pagination
- Implement filter controls with column filters and global search
- Add optimistic updates for inline row editing with mutation rollback
Output: A dashboard with efficient data fetching, server-managed table pagination, and instant edit feedback.
Example 2: Add type-safe routing with data prefetching
User request: "Set up TanStack Router with typed search parameters and data preloading"
Actions:
- Define routes with typed parameters and Zod-validated search params
- Add route loaders for data fetching with built-in caching
- Configure
<Link> components with type-checked params and search params
- Enable prefetching on hover for instant navigation
Output: A fully typed routing layer where invalid params cause TypeScript errors at compile time.
Guidelines
- Use query key factories for consistent cache keys:
const userKeys = { all: ["users"], detail: (id) => ["users", id] }.
- Set
staleTime based on data freshness needs: 0 for real-time, 5 minutes for dashboards, Infinity for static data.
- Always define
onError for mutations; silent failures confuse users.
- Use
placeholderData instead of initialData for loading states; placeholder does not write to cache.
- Use TanStack Table with
@tanstack/react-virtual for large datasets; do not render thousands of DOM nodes.
- Keep query functions pure: they receive
queryKey and return data with no side effects.
- Use
queryClient.invalidateQueries() after mutations instead of manual cache updates for simplicity.
1---2name: tanstack3description: Assists with building React applications using the TanStack ecosystem: Query for server state management, Router for type-safe routing, Table for headless data tables, and Virtual for list virtualization. Trigger words: tanstack, react query, tanstack query, tanstack table, tanstack router, useQuery, useMutation.4license: Apache-2.05---67# TanStack89## Overview1011The TanStack ecosystem provides type-safe client libraries for React: Query for declarative data fetching and caching, Router for fully typed routing with search parameters, Table for headless data tables with sorting/filtering/pagination, and Virtual for rendering large lists efficiently.1213## Instructions1415- When fetching data, use `useQuery()` with hierarchical query keys (e.g., `["users", userId, "posts"]`) and configure `staleTime` based on freshness needs (0 for real-time, 5 minutes for dashboards, Infinity for static data).16- When performing mutations, use `useMutation()` with `onSuccess` for cache invalidation via `queryClient.invalidateQueries()`, and `onMutate` for optimistic updates with rollback.17- When building tables, use TanStack Table's headless approach with typed column definitions, and combine with `@tanstack/react-virtual` for datasets with 10,000+ rows.18- When routing, use TanStack Router for fully typed route parameters and search params with Zod validation, file-based routes with automatic type generation, and route-level data loading.19- When handling pagination, use `useInfiniteQuery()` for infinite scroll or cursor-based patterns, and server-side pagination in TanStack Table.20- When prefetching, use `queryClient.prefetchQuery()` for anticipated navigation and `useSuspenseQuery()` for React Suspense integration.21- When virtualizing lists, use `@tanstack/react-virtual` with `estimateSize` for scroll position prediction and support for dynamic, variable-height items.2223## Examples2425### Example 1: Build a data dashboard with Query and Table2627**User request:** "Create a dashboard with server-paginated data table and real-time stats"2829**Actions:**301. Set up TanStack Query with appropriate `staleTime` and refetch intervals for stats312. Define TanStack Table with typed columns, server-side sorting and pagination323. Implement filter controls with column filters and global search334. Add optimistic updates for inline row editing with mutation rollback3435**Output:** A dashboard with efficient data fetching, server-managed table pagination, and instant edit feedback.3637### Example 2: Add type-safe routing with data prefetching3839**User request:** "Set up TanStack Router with typed search parameters and data preloading"4041**Actions:**421. Define routes with typed parameters and Zod-validated search params432. Add route loaders for data fetching with built-in caching443. Configure `<Link>` components with type-checked params and search params454. Enable prefetching on hover for instant navigation4647**Output:** A fully typed routing layer where invalid params cause TypeScript errors at compile time.4849## Guidelines5051- Use query key factories for consistent cache keys: `const userKeys = { all: ["users"], detail: (id) => ["users", id] }`.52- Set `staleTime` based on data freshness needs: 0 for real-time, 5 minutes for dashboards, Infinity for static data.53- Always define `onError` for mutations; silent failures confuse users.54- Use `placeholderData` instead of `initialData` for loading states; placeholder does not write to cache.55- Use TanStack Table with `@tanstack/react-virtual` for large datasets; do not render thousands of DOM nodes.56- Keep query functions pure: they receive `queryKey` and return data with no side effects.57- Use `queryClient.invalidateQueries()` after mutations instead of manual cache updates for simplicity.