Skip to main content
Queries are the foundation of TanStack Query. They represent a declarative dependency on an asynchronous source of data that is tied to a unique key.

Basic Query

To subscribe to a query in your components, use the useQuery 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 to 0.
Set staleTime: Infinity for data that never needs to be refetched, or use the special 'static' value for compile-time static data.

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:
placeholderData is never persisted to the cache. For persisted initial data, use initialData.

Enabled Queries

Control when queries run:

Refetching

Polling with Refetch Interval

Automatically refetch at regular intervals:

Select Data

Transform or select a portion of query data:
The select function is only called when data exists and is memoized, so it only runs when the data changes.

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

Query Meta

Attach metadata to queries:
Access meta in global callbacks: