Queries
client.query binds an endpoint to the cache. The result is one object you use three ways: as a hook, as a source of TanStack query options, and as a typed handle on the cached data. This page covers all three.
The examples use userQuery from the quick start and one more query:
The hook
use takes the endpoint's arguments. They are optional when the endpoint requires none.
The arguments are the cache identity: two components calling use with the same arguments share one entry and one request. use returns TanStack's useQuery result unchanged.
Options
The second argument accepts every useQuery option except queryKey and queryFn, which the query owns.
select derives a value from the cached data and changes the type of data with it:
Waiting for an input
When an argument is not known yet, pass TanStack's skipToken in place of the arguments. The query stays idle and the arguments stay fully typed, with no placeholder values.
Suspense
useSuspense is useSuspenseQuery for the endpoint. The component suspends while loading and throws to the nearest error boundary on failure, so data is always defined.
It takes the same second argument as use, minus the options useSuspenseQuery does not have. In particular there is no enabled and no skipToken: a suspense query always fetches, so render the component conditionally instead.
Two useSuspense calls in one component run one after the other. To load in parallel, use useSuspenseQueries with query options. What the error boundary receives is covered in Errors.
Query options
Calling the query itself returns a TanStack options object with the key and the query function filled in. Use it wherever TanStack takes options: useQueries, useSuspenseQueries, useQuery, queryClient.query.
The call takes the same second argument as use:
Status and headers
withResponse: true makes a query deliver { data, response }, where response is { status, statusText, headers } of the HTTP reply. Use it when a header matters to the UI: a total count, an ETag, a rate limit.
headers is a plain record with lower-cased names, so the cached value can be dehydrated and persisted. Failures are not wrapped; they stay in error. A withResponse query has its own cache entries, separate from the same endpoint declared without it.
Cache helpers
Each helper takes the QueryClient first and the query's arguments second, and is typed from the endpoint.
fetch, prefetch and ensure are built on queryClient.query(). To give one of them TanStack options such as staleTime, call that method yourself with the query options. Their use on a server is covered in SSR and prefetching.
POST queries
A query is any endpoint whose result you want cached, whatever its method. A search that sends its criteria in the body is still a read:
The body is part of the cache key, so each distinct search has its own entry.
Related: Infinite queries, Keys and invalidation.