Query Basics
A query requires two things:- A unique query key - An array-based identifier for the query
- A query function - A function that returns a Promise that resolves data or throws an error
Query States
A query can be in one of the following states at any given moment:Status States
Queries have astatus field that indicates the current state:
pending- The query has no data yet and is currently fetchingerror- The query encountered an errorsuccess- The query was successful and data is available
Fetch Status
In addition to thestatus field, queries also have a fetchStatus that provides additional granularity:
fetching- The query is currently fetchingpaused- The query wanted to fetch, but it is paused (usually due to being offline)idle- The query is not doing anything at the moment
The
fetchStatus is independent of the status. A query can be in success status but still have fetching fetchStatus when performing a background refetch.Derived Boolean States
For convenience, the query result also includes derived boolean flags:isPending- Equivalent tostatus === 'pending'isSuccess- Equivalent tostatus === 'success'isError- Equivalent tostatus === 'error'isFetching- Equivalent tofetchStatus === 'fetching'isPaused- Equivalent tofetchStatus === 'paused'isLoading- Equivalent toisPending && isFetching(first load with no data)isRefetching- Equivalent toisFetching && !isPending(background refetch)
queryObserver.ts:560-589:
Query Lifecycle
1. Query Observer Creation
When you useuseQuery, a QueryObserver is created that subscribes to changes in the query’s state. From queryObserver.ts:71-89:
2. Subscription
When the observer gets its first subscriber, it checks if it should fetch on mount (queryObserver.ts:95-107):
3. Data Fetching
When a fetch is executed, the query transitions through states. The fetch state is defined inquery.ts:690-709:
4. Result Updates
As the query state changes, the observer computes new results and notifies listeners only when tracked properties change (queryObserver.ts:641-697).
Stale Queries
A query is considered “stale” when it’s old enough that it should be refetched. The staleness is determined by:- The
staleTimeoption (defaults to0, meaning data is immediately stale) - Whether the query has been invalidated
query.ts:308-323:
Background Refetching
Queries automatically refetch in the background under several conditions:- New instances of the query mount
- Window is refocused (controlled by
refetchOnWindowFocus) - Network is reconnected (controlled by
refetchOnReconnect) - Refetch interval is configured (via
refetchInterval)
Enabled Queries
Queries can be disabled using theenabled option. This is useful for dependent queries:
Initial Data
You can provide initial data for a query, which will be used immediately while the query fetches in the background:Placeholder Data
Placeholder data allows you to show fake data while the real data is loading. UnlikeinitialData, placeholder data is not persisted to the cache:
Query Function Context
Query functions receive aQueryFunctionContext object with useful properties:
queryKey- The query keysignal- An AbortSignal for cancellationmeta- Optional metadatapageParam- For infinite queries
types.ts:138-165, the context is defined as:
Type Safety
TanStack Query provides full type safety for your queries:Error Handling
When a query function throws an error, the query enters theerror state:
By default, TanStack Query will retry failed queries 3 times with exponential backoff before setting the query to an
error state.