Skip to main content

useQuery

The useQuery hook is the primary way to fetch and cache data in TanStack Query. It subscribes to a query and returns the current state of the query.

Import

Signature

Type Parameters

type
default:"unknown"
The type of data returned by the query function
type
default:"DefaultError"
The type of error that can be thrown by the query function
type
default:"TQueryFnData"
The type of data returned by the select function (if provided), otherwise same as TQueryFnData
type
default:"QueryKey"
The type of the query key

Parameters

UseQueryOptions
required
Configuration options for the query
TQueryKey
required
A unique key for the query. Must be an array.
QueryFunction<TQueryFnData, TQueryKey>
The function that will be called to fetch data. Receives a QueryFunctionContext object.
boolean
default:"true"
Set to false to disable automatic query execution
number | ((query: Query) => number)
default:"0"
Time in milliseconds after data is considered stale. Set to Infinity to disable automatic refetching.
number
default:"300000"
Time in milliseconds that unused/inactive cache data remains in memory. When a query’s cache becomes unused or inactive, that cache data will be garbage collected after this duration.
number | false | ((query: Query) => number | false)
default:"false"
If set to a number, the query will continuously refetch at this frequency in milliseconds
boolean
default:"false"
If set to true, the query will continue to refetch while the window is in the background
boolean | 'always' | ((query: Query) => boolean | 'always')
default:"true"
If set to true, the query will refetch on window focus if the data is stale
boolean | 'always' | ((query: Query) => boolean | 'always')
default:"true"
If set to true, the query will refetch on mount if the data is stale
boolean | 'always' | ((query: Query) => boolean | 'always')
default:"true"
If set to true, the query will refetch on reconnect if the data is stale
boolean | number | (failureCount: number, error: TError) => boolean
default:"3"
Number of retry attempts or function to determine if a request should be retried
number | (retryAttempt: number, error: TError) => number
Function that receives a retry attempt number and returns the delay to apply before the next attempt
(data: TQueryFnData) => TData
Function to transform or select a part of the data returned by the query function
TQueryFnData | () => TQueryFnData
If set, this value will be used as the initial data for the query (as long as the query hasn’t been created or cached yet)
number | (() => number | undefined)
If set, this value will be used as the time (in milliseconds) of when the initialData itself was last updated
TQueryFnData | (previousData: TData | undefined, previousQuery: Query | undefined) => TQueryFnData
If set, this value will be used as the placeholder data for this particular query observer while the query is still in the pending state
boolean | (oldData: unknown, newData: unknown) => unknown
default:"true"
Set this to false to disable structural sharing between query results
boolean | (error: TError, query: Query) => boolean
default:"false"
Set this to true if you want errors to be thrown in the render phase and propagate to the nearest error boundary
'online' | 'always' | 'offlineFirst'
default:"'online'"
Controls when the query function is allowed to execute
Array<keyof UseQueryResult> | 'all'
If set, the component will only re-render when one of the listed properties change
Record<string, unknown>
Optional metadata that can be used by query client plugins
boolean
default:"true"
Set this to false to unsubscribe this observer from updates to the query cache
QueryClient
Optional QueryClient instance to use. If not provided, the client from the nearest QueryClientProvider will be used.

Returns

TData
The data returned by the query function (or undefined if the query hasn’t successfully fetched yet)
TError | null
The error object for the query, if an error occurred
'pending' | 'error' | 'success'
The status of the query:
  • pending: The query has no data yet
  • error: The query encountered an error
  • success: The query was successful and data is available
'fetching' | 'paused' | 'idle'
The fetch status of the query:
  • fetching: The query is currently fetching
  • paused: The query wanted to fetch, but it is paused
  • idle: The query is not fetching
boolean
Derived from status. Will be true if the query is in pending status
boolean
Derived from status. Will be true if the query is in success status
boolean
Derived from status. Will be true if the query is in error status
boolean
Derived from fetchStatus. Will be true whenever the query is fetching (including background refetching)
boolean
Shorthand for isPending && isFetching - useful for showing a loading spinner on initial load
boolean
Will be true if the query failed while fetching for the first time
boolean
Will be true if the query failed while refetching
boolean
Will be true if the data in the cache is invalidated or if the data is older than the given staleTime
boolean
Will be true if the data shown is placeholder data
(options?: { throwOnError?: boolean, cancelRefetch?: boolean }) => Promise<UseQueryResult>
Function to manually refetch the query
number
Timestamp of when the data was last updated
number
Timestamp of when the error was last updated
number
The failure count for the query
TError | null
The failure reason for the query retry

Examples

Basic Usage

With Type Safety

With Data Transformation

Conditional Queries

With Stale Time

Source

Implementation: useQuery.ts:50