Prerequisites
Before you begin, make sure you have:- Node.js 18+ or Bun installed
- A JavaScript or TypeScript project set up
- Basic knowledge of React, Vue, Svelte, Solid, or Angular
Installation
Install TanStack Query for your framework:Basic Example
Here’s a complete working example using React Query to fetch data from an API:Understand what's happening
Query Key:
['repoData'] is a unique identifier for this query. TanStack Query uses it for caching.Query Function: The async function that fetches your data. It can be any async operation.Query States:isPending:truewhen the query is loading for the first timeerror: Contains error object if the query failsdata: Contains the fetched data when successfulisFetching:truewhenever data is being fetched (including background refetches)
Key Features
Automatic Caching
TanStack Query automatically caches your data. If you navigate away and come back, the cached data is shown instantly while fresh data is fetched in the background.Automatic Refetching
Queries automatically refetch in the background when:- The window regains focus
- The network reconnects
- A configured refetch interval elapses
staleTime, refetchOnWindowFocus, and refetchInterval.
Mutations
UseuseMutation to modify data on your server:
Configuration Options
Customize query behavior with options:Next Steps
Essential Concepts
Learn about query keys, query functions, and caching strategies
Framework Guides
Deep dive into framework-specific usage and patterns
TypeScript
Learn how to use TanStack Query with TypeScript for full type safety
DevTools
Set up the DevTools to debug and inspect your queries