Basic Query
To subscribe to a query in your components, use theuseQuery hook with at least:
- A unique key for the query
- A function that returns a promise
Query Results
The query result contains all information about the query state:The
status field indicates the data state (pending/error/success), while fetchStatus indicates the fetch operation state (fetching/paused/idle).Query Options
Stale Time
The time in milliseconds after data is considered stale. Defaults to0.
GC Time (formerly Cache Time)
The time in milliseconds that unused/inactive cache data remains in memory. Defaults to 5 minutes.Retry
Control how queries retry on failure:Retry Delay
Customize the delay between retries:Initial Data
Provide initial data to prevent loading states:Queries with
initialData start in success status immediately.Placeholder Data
Show temporary data while the query loads:Enabled Queries
Control when queries run:Refetching
- Manual Refetch
- On Window Focus
- On Reconnect
- On Mount
Polling with Refetch Interval
Automatically refetch at regular intervals:Select Data
Transform or select a portion of query data:Dependent Queries
Queries that depend on previous queries:Parallel Queries
Multiple queries in the same component run in parallel:Dynamic Parallel Queries with useQueries
For a variable number of queries:Query Cancellation
Queries support cancellation via AbortSignal:When a query is cancelled (e.g., component unmounts), TanStack Query automatically calls
abort() on the signal.Error Handling
- Per Query
- Global Default
- With Error Boundary