Skip to main content
Queries are the foundation of TanStack Query. A query is a declarative dependency on an asynchronous source of data that is tied to a unique key. Queries are used to fetch, cache, and update data from your server.

Query Basics

A query requires two things:
  1. A unique query key - An array-based identifier for the query
  2. 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 a status field that indicates the current state:
  • pending - The query has no data yet and is currently fetching
  • error - The query encountered an error
  • success - The query was successful and data is available

Fetch Status

In addition to the status field, queries also have a fetchStatus that provides additional granularity:
  • fetching - The query is currently fetching
  • paused - 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 to status === 'pending'
  • isSuccess - Equivalent to status === 'success'
  • isError - Equivalent to status === 'error'
  • isFetching - Equivalent to fetchStatus === 'fetching'
  • isPaused - Equivalent to fetchStatus === 'paused'
  • isLoading - Equivalent to isPending && isFetching (first load with no data)
  • isRefetching - Equivalent to isFetching && !isPending (background refetch)
These flags are derived from the observer’s state computation in queryObserver.ts:560-589:

Query Lifecycle

1. Query Observer Creation

When you use useQuery, 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 in query.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 staleTime option (defaults to 0, meaning data is immediately stale)
  • Whether the query has been invalidated
From query.ts:308-323:
Set staleTime to control how long data is considered fresh. For data that doesn’t change often, use a higher staleTime to reduce unnecessary refetches.

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 the enabled 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. Unlike initialData, placeholder data is not persisted to the cache:
Placeholder data is not cached and will be replaced by actual data when it arrives. Use initialData if you want the data to be persisted to the cache.

Query Function Context

Query functions receive a QueryFunctionContext object with useful properties:
  • queryKey - The query key
  • signal - An AbortSignal for cancellation
  • meta - Optional metadata
  • pageParam - For infinite queries
From 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 the error state:
By default, TanStack Query will retry failed queries 3 times with exponential backoff before setting the query to an error state.