TanStack Query
Framework-agnostic server state management library providing useQuery, useMutation, and useInfiniteQuery hooks with stale-while-revalidate caching, QueryClient cache invalidation, and optimistic updates for React, Vue, Svelte, and Angular.
Score Breakdown
⚙ Agent Friendliness
🔒 Security
No security surface of its own — security depends entirely on the underlying fetch implementation. Cache data lives in memory; sensitive data should not be cached longer than necessary.
⚡ Reliability
Best When
Building React or Vue applications that fetch data from REST or GraphQL APIs and need declarative caching, background sync, and optimistic UI without manual fetch state management.
Avoid When
Your data is entirely local/client-side with no server synchronization, or you're in a server-only Node.js context where a simple fetch wrapper is more appropriate.
Use Cases
- • Fetch and cache API data in a React component with useQuery, getting automatic background refetching and loading/error state management
- • Invalidate and refetch related queries after a mutation using queryClient.invalidateQueries to keep UI in sync after data changes
- • Implement infinite scroll pagination with useInfiniteQuery fetching the next page cursor from the previous response automatically
- • Apply optimistic updates with useMutation's onMutate callback to update the UI instantly before server confirmation, with rollback on error
- • Prefetch critical data server-side using QueryClient.prefetchQuery and dehydrate/hydrate for SSR frameworks like Next.js
Not For
- • Managing purely client-side UI state (modals, form state, theme) — use Zustand or useState instead
- • Applications with no server data fetching where a simpler fetch + useState pattern would suffice
- • Teams requiring Flux/Redux-style unidirectional data flow with time-travel debugging for all application state
Interface
Authentication
TanStack Query has no auth layer — it is a data-fetching wrapper. Authentication headers and credentials are passed through the queryFn fetch function, which is entirely user-controlled.
Pricing
MIT licensed open source. TanStack offers paid courses and templates but the library itself is free.
Agent Metadata
Known Gotchas
- ⚠ v5 removed the onSuccess/onError/onSettled callbacks from useQuery — code generated for v4 will have TypeScript errors and silent runtime issues on v5
- ⚠ Query keys must be serializable arrays; using non-serializable objects (class instances, functions) as query keys causes cache misses and duplicate fetches that are hard to debug
- ⚠ staleTime defaults to 0 meaning every component mount triggers a background refetch; agents generating query code should set appropriate staleTime to avoid hammering APIs
- ⚠ queryClient.invalidateQueries() with no arguments invalidates ALL queries — always pass a specific queryKey filter to avoid cascading refetches
- ⚠ useQuery does not run on the server in SSR by default; prefetchQuery + dehydrate/hydrate pattern is required for Next.js App Router and other SSR setups
Alternatives
Full Evaluation Report
Detailed scoring breakdown, competitive positioning, security analysis, and improvement recommendations for TanStack Query.
Scores are editorial opinions as of 2026-03-06.