Skip to main content
The QueryClient is the central class in TanStack Query that manages all query and mutation caches. It provides methods to fetch, cache, and synchronize server state.

Constructor

Creates a new QueryClient instance.
QueryClientConfig
Configuration options for the QueryClient

Example

Methods

fetchQuery

Fetches a query and returns a promise with the data. If the query exists and the data is not stale, it will return the cached data.
FetchQueryOptions
required
Options for fetching the query
Promise
Returns a promise that resolves with the query data.

Example

prefetchQuery

Prefetches a query and caches the result. Unlike fetchQuery, it does not return the data and does not throw errors.
FetchQueryOptions
required
Same options as fetchQuery.
Promise
Returns a promise that resolves when the query is prefetched.

Example

fetchInfiniteQuery

Fetches an infinite query and returns a promise with the infinite data structure.
FetchInfiniteQueryOptions
required
Options for fetching the infinite query
Promise
Returns a promise that resolves with the infinite query data.

prefetchInfiniteQuery

Prefetches an infinite query and caches the result.

getQueryData

Returns the cached data for a query. This is a synchronous, non-reactive way to read data.
QueryKey
required
The query key to get data for.
Data
Returns the cached data or undefined if not found.

Example

Do not use this inside components, as it won’t receive updates. Use useQuery instead.

setQueryData

Updates the cached data for a query. Can accept a value or an updater function.
QueryKey
required
The query key to update data for.
TQueryFnData | ((oldData: TQueryFnData | undefined) => TQueryFnData)
required
New data value or updater function.
SetDataOptions
Additional options for setting data.
Data
Returns the updated data.

Example

getQueriesData

Returns the cached data for multiple queries matching the provided filters.
QueryFilters
required
Filters to match queries.
Array
Returns an array of [queryKey, data] tuples.

setQueriesData

Updates the cached data for multiple queries matching the provided filters.

getQueryState

Returns the query state (data, status, timestamps, etc.) for a query.
QueryKey
required
The query key to get state for.
QueryState
Returns the query state or undefined if not found.

ensureQueryData

Ensures that query data is available. If data is not cached, it will fetch it.
EnsureQueryDataOptions
required
Options including queryKey, queryFn, and optional revalidateIfStale.
Promise
Returns a promise that resolves with the query data (either cached or freshly fetched).

ensureInfiniteQueryData

Ensures that infinite query data is available.

invalidateQueries

Marks queries as stale and optionally refetches them.
InvalidateQueryFilters
Filters to match queries to invalidate.
InvalidateOptions
Additional options for invalidation.
Promise
Returns a promise that resolves when invalidation is complete.

Example

refetchQueries

Refetches queries matching the provided filters.
RefetchQueryFilters
Filters to match queries to refetch.
RefetchOptions
Additional options for refetching.
Promise
Returns a promise that resolves when refetching is complete.

Example

cancelQueries

Cancels ongoing queries matching the provided filters.
QueryFilters
Filters to match queries to cancel.
CancelOptions
Options for cancellation.
Promise
Returns a promise that resolves when cancellation is complete.

Example

resetQueries

Resets queries to their initial state and optionally refetches them.
QueryFilters
Filters to match queries to reset.
ResetOptions
Options for resetting.
Promise
Returns a promise that resolves when reset is complete.

removeQueries

Removes queries from the cache matching the provided filters.
QueryFilters
Filters to match queries to remove.

Example

isFetching

Returns the number of queries currently fetching.
QueryFilters
Filters to match queries.
number
The number of queries currently fetching.

Example

isMutating

Returns the number of mutations currently executing.
MutationFilters
Filters to match mutations.
number
The number of mutations currently executing.

getQueryCache

Returns the QueryCache instance used by this client.
QueryCache
The QueryCache instance.

getMutationCache

Returns the MutationCache instance used by this client.
MutationCache
The MutationCache instance.

getDefaultOptions

Returns the default options for this client.
DefaultOptions
The default options.

setDefaultOptions

Sets the default options for this client.
DefaultOptions
required
The new default options.

setQueryDefaults

Sets default options for queries matching a specific query key.
QueryKey
required
The query key to set defaults for.
Partial<QueryObserverOptions>
required
The default options for this query key.

Example

getQueryDefaults

Gets the default options for queries matching a specific query key.
QueryKey
required
The query key to get defaults for.
object
The default options for this query key.

setMutationDefaults

Sets default options for mutations matching a specific mutation key.
MutationKey
required
The mutation key to set defaults for.
Partial<MutationObserverOptions>
required
The default options for this mutation key.

getMutationDefaults

Gets the default options for mutations matching a specific mutation key.

mount

Mounts the QueryClient, setting up focus and online event listeners.

unmount

Unmounts the QueryClient, removing focus and online event listeners.

clear

Clears all caches (queries and mutations).

Example

resumePausedMutations

Resumes all paused mutations.
Promise
Returns a promise that resolves when all paused mutations have resumed.