TanStack
API Reference

useQuery

Overview

ts
function useQuery<TQueryFnData, TError, TData, TQueryKey>(options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: QueryClient): DefinedUseQueryResult<TData, TError>;
function useQuery<TQueryFnData, TError, TData, TQueryKey>(options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: QueryClient): UseQueryResult<TData, TError>;
function useQuery<TQueryFnData, TError, TData, TQueryKey>(options: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: QueryClient): UseQueryResult<TData, TError>;

See also: Parameters · Returns

Call Signature

ts
function useQuery<TQueryFnData, TError, TData, TQueryKey>(options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: QueryClient): DefinedUseQueryResult<TData, TError>;

Defined in: packages/react-query/src/useQuery.ts:49

This overload is selected when initialData is set, so the resulting data is never undefined (unless a select changes TData to include undefined).

Type Parameters

TQueryFnData

TQueryFnData = unknown

TError

TError = Error

TData

TData = TQueryFnData

TQueryKey

TQueryKey extends readonly unknown[] = readonly unknown[]

Parameters

options

DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>

The DefinedInitialDataOptions to use — everything you can pass to useQuery, with initialData set.

queryClient?

QueryClient

Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.

Returns

DefinedUseQueryResult<TData, TError>

The current query result, typed so that status is success — or error if a fetch attempt fails while keeping the existing data (status never resolves to pending in this overload's type, since initialData guarantees data upfront). isSuccess/isError are derived booleans for convenience.

See

queryOptions to share these options between useQuery and imperative APIs like queryClient.query.

Example

tsx
import { useQuery } from '@tanstack/react-query'

function Posts() {
  // `data` is `Post[]`, never `undefined`, thanks to `initialData` — even if a refetch fails,
  // so the list stays visible alongside the error.
  const { data, isError, error } = useQuery({
    queryKey: ['posts'],
    queryFn: fetchPosts,
    initialData: [],
  })

  return (
    <div>
      {isError ? <span>Error: {error.message}</span> : null}
      <ul>
        {data.map((post) => <li key={post.id}>{post.title}</li>)}
      </ul>
    </div>
  )
}

Call Signature

ts
function useQuery<TQueryFnData, TError, TData, TQueryKey>(options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: QueryClient): UseQueryResult<TData, TError>;

Defined in: packages/react-query/src/useQuery.ts:116

This overload is selected when initialData is omitted or may be undefined, so the resulting data can be undefined.

Type Parameters

TQueryFnData

TQueryFnData = unknown

TError

TError = Error

TData

TData = TQueryFnData

TQueryKey

TQueryKey extends readonly unknown[] = readonly unknown[]

Parameters

options

UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>

The UndefinedInitialDataOptions to use — everything you can pass to useQuery.

queryClient?

QueryClient

Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.

Returns

UseQueryResult<TData, TError>

The current query result. status is pending if there is no cached data to display, error if the last fetch attempt failed, or success if the query has data to display. isPending/isSuccess/isError are derived booleans for convenience.

See

queryOptions to share these options between useQuery and imperative APIs like queryClient.query.

Examples

tsx
import { useQuery } from '@tanstack/react-query'

function Posts() {
  const { status, data, error, isFetching } = useQuery({
    queryKey: ['posts'],
    queryFn: fetchPosts,
  })

  if (status === 'pending') return 'Loading...'
  if (status === 'error') return <span>Error: {error.message}</span>

  return (
    <div>
      <ul>
        {data.map((post) => (
          <li key={post.id}>{post.title}</li>
        ))}
      </ul>
      <div>{isFetching ? 'Background Updating...' : ' '}</div>
    </div>
  )
}

The same query, checking isPending/isError instead of status — pick whichever reads better to you:

tsx
import { useQuery } from '@tanstack/react-query'

function Posts() {
  const { isPending, isError, data, error } = useQuery({
    queryKey: ['posts'],
    queryFn: fetchPosts,
  })

  if (isPending) return 'Loading...'
  if (isError) return <span>Error: {error.message}</span>

  return (
    <ul>
      {data.map((post) => <li key={post.id}>{post.title}</li>)}
    </ul>
  )
}

Call Signature

ts
function useQuery<TQueryFnData, TError, TData, TQueryKey>(options: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: QueryClient): UseQueryResult<TData, TError>;

Defined in: packages/react-query/src/useQuery.ts:281

Fallback overload for options whose initialData presence isn't statically known — for example, an object typed as UseQueryOptions rather than an object literal. Prefer one of the other overloads when possible, since they infer whether data can be undefined from initialData directly.

Type Parameters

TQueryFnData

TQueryFnData = unknown

TError

TError = Error

TData

TData = TQueryFnData

TQueryKey

TQueryKey extends readonly unknown[] = readonly unknown[]

Parameters

options

UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>

The UseQueryOptions to use — everything you can pass to useQuery.

queryClient?

QueryClient

Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.

Returns

UseQueryResult<TData, TError>

The current query result. status is pending if there is no cached data to display, error if the last fetch attempt failed, or success if the query has data to display. isPending/isSuccess/isError are derived booleans for convenience.

See

queryOptions to share these options between useQuery and imperative APIs like queryClient.query.

Examples

tsx
import { useQuery } from '@tanstack/react-query'

function Posts() {
  const { status, data, error, isFetching } = useQuery({
    queryKey: ['posts'],
    queryFn: fetchPosts,
  })

  if (status === 'pending') return 'Loading...'
  if (status === 'error') return <span>Error: {error.message}</span>

  return (
    <div>
      <ul>
        {data.map((post) => (
          <li key={post.id}>{post.title}</li>
        ))}
      </ul>
      <div>{isFetching ? 'Background Updating...' : ' '}</div>
    </div>
  )
}

select derives whatever data a component needs from the cached value, without changing what's actually stored in the cache — the cache still holds the full Post[], but data here is a number:

tsx
import { useQuery } from '@tanstack/react-query'

function PostCount() {
  const { data, isPending, isError, error } = useQuery({
    queryKey: ['posts'],
    queryFn: fetchPosts,
    select: (posts) => posts.length,
  })

  if (isPending) return 'Loading...'
  if (isError) return <span>Error: {error.message}</span>

  return <span>{data} posts</span>
}

A dependent query, only enabled once postId is set — use isLoading, not isPending, so the loading state doesn't show while the query is disabled:

tsx
import { useQuery } from '@tanstack/react-query'

function Post({ postId }: { postId: number | undefined }) {
  const { data, isLoading, isError, error } = useQuery({
    queryKey: ['post', postId],
    queryFn: () => fetchPost(postId!),
    enabled: postId != null,
  })

  if (postId == null) return 'Select a post'
  if (isLoading) return 'Loading...'
  if (isError) return <span>Error: {error.message}</span>

  return <h1>{data?.title}</h1>
}

The same dependent query, type safe: skipToken disables the query without needing the non-null assertion above, since queryFn is only ever called when postId is defined. refetch doesn't work while queryFn is skipToken — use enabled: false instead if you need to trigger the query manually:

tsx
import { skipToken, useQuery } from '@tanstack/react-query'

function Post({ postId }: { postId: number | undefined }) {
  const { data, isLoading, isError, error } = useQuery({
    queryKey: ['post', postId],
    queryFn: postId != null ? () => fetchPost(postId) : skipToken,
  })

  if (postId == null) return 'Select a post'
  if (isLoading) return 'Loading...'
  if (isError) return <span>Error: {error.message}</span>

  return <h1>{data?.title}</h1>
}

Seeding a detail query from an already-cached list, to skip the loading state. initialDataUpdatedAt carries over the list's own fetch time, so that if you set a staleTime, it's measured from when the list was fetched rather than from now:

tsx
import { useQuery, useQueryClient } from '@tanstack/react-query'

function Post({ postId }: { postId: number }) {
  const queryClient = useQueryClient()

  const { data, isError, error } = useQuery({
    queryKey: ['post', postId],
    queryFn: () => fetchPost(postId),
    initialData: () =>
      queryClient
        .getQueryData<Array<Post>>(['posts'])
        ?.find((post) => post.id === postId),
    initialDataUpdatedAt: () =>
      queryClient.getQueryState(['posts'])?.dataUpdatedAt,
  })

  if (isError) return <span>Error: {error.message}</span>

  return <h1>{data?.title}</h1>
}

Paginated data, keeping the previous page's data visible while the next page loads:

tsx
import { keepPreviousData, useQuery } from '@tanstack/react-query'
import { useState } from 'react'

function Posts() {
  const [page, setPage] = useState(0)

  const { data, isPlaceholderData, isError, error } = useQuery({
    queryKey: ['posts', page],
    queryFn: () => fetchPosts(page),
    placeholderData: keepPreviousData,
  })

  if (isError) return <span>Error: {error.message}</span>

  return (
    <div>
      <ul>
        {data?.map((post) => <li key={post.id}>{post.title}</li>)}
      </ul>
      <button
        disabled={isPlaceholderData}
        onClick={() => setPage((old) => old + 1)}
      >
        Next Page
      </button>
    </div>
  )
}

Parameters

options

UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>

The UseQueryOptions to use — everything you can pass to useQuery.

options properties

PropertyTypeDefault valueDescription
enabled?| false | true | ((query: Query<TQueryFnData, TError, TQueryFnData, TQueryKey>) => boolean)trueSet this to false or a function that returns false to disable automatic refetching when the query mounts or changes query keys. To refetch the query, use the refetch method returned from the useQuery instance. Accepts a boolean or function that returns a boolean.
gcTime?numberundefinedThe 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. When different garbage collection times are specified, the longest one will be used. Setting it to Infinity will disable garbage collection. Defaults to 5 * 60 * 1000 (5 minutes), or Infinity during SSR. Note: the maximum allowed time is about 24 days, imposed by setTimeout's 32-bit signed integer delay — see timeoutManager.setTimeoutProvider for a workaround.
initialData?TQueryFnData | (() => TQueryFnData | undefined)undefinedIf set, this value will be used as the initial data for the query cache (as long as the query hasn't been created or cached yet). If set to a function, the function will be called once during the shared/root query initialization, and be expected to synchronously return the initial data. Initial data is considered stale by default unless a staleTime has been set. initialData is persisted to the cache.
initialDataUpdatedAt?number | (() => number | undefined)undefinedIf set, this value will be used as the time (in milliseconds) of when the initialData itself was last updated.
maxPages?numberundefinedMaximum number of pages to store in the data of an infinite query.
meta?Record<string, unknown>undefinedAdditional payload to be stored on each query. Use this property to pass information that can be used in other places.
networkMode?"online" | "always" | "offlineFirst"'online'Controls whether a query is allowed to run based on the current network connectivity. See Network Mode for more information.
notifyOnChangeProps?| ( | "error" | "data" | "isError" | "isPending" | "isLoading" | "isLoadingError" | "isRefetchError" | "isSuccess" | "isPlaceholderData" | "status" | "dataUpdatedAt" | "errorUpdatedAt" | "failureCount" | "failureReason" | "errorUpdateCount" | "isFetched" | "isFetchedAfterMount" | "isFetching" | "isInitialLoading" | "isPaused" | "isRefetching" | "isStale" | "isEnabled" | "refetch" | "fetchStatus" | "fetchNextPage" | "fetchPreviousPage" | "hasNextPage" | "hasPreviousPage" | "isFetchNextPageError" | "isFetchingNextPage" | "isFetchPreviousPageError" | "isFetchingPreviousPage")[] | "all" | (() => | "all" | ( | "error" | "data" | "isError" | "isPending" | "isLoading" | "isLoadingError" | "isRefetchError" | "isSuccess" | "isPlaceholderData" | "status" | "dataUpdatedAt" | "errorUpdatedAt" | "failureCount" | "failureReason" | "errorUpdateCount" | "isFetched" | "isFetchedAfterMount" | "isFetching" | "isInitialLoading" | "isPaused" | "isRefetching" | "isStale" | "isEnabled" | "refetch" | "fetchStatus" | "fetchNextPage" | "fetchPreviousPage" | "hasNextPage" | "hasPreviousPage" | "isFetchNextPageError" | "isFetchingNextPage" | "isFetchPreviousPageError" | "isFetchingPreviousPage")[] | undefined)undefinedIf set, the component will only re-render if any of the listed properties change. When set to ['data', 'error'], the component will only re-render when the data or error properties change. When set to 'all', the component will re-render whenever a query is updated. When set to a function, the function will be executed to compute the list of properties. Defaults to undefined, in which case property access is tracked automatically, and the component only re-renders when one of the tracked properties changes.
persister?(queryFn: (context: object) => TQueryFnData | Promise<TQueryFnData>, context: object, query: Query) => TQueryFnData | Promise<TQueryFnData>undefinedThis option can be used to persist the result of a query to an external storage, bypassing the need to actually call the queryFn. Useful for persisting a query's data across e.g. server/client boundaries.
placeholderData?| NonFunctionGuard<TQueryFnData> | ((previousData: NonFunctionGuard<TQueryFnData> | undefined, previousQuery: | Query<NonFunctionGuard<TQueryFnData>, TError, NonFunctionGuard<TQueryFnData>, TQueryKey> | undefined) => NonFunctionGuard<TQueryFnData> | undefined)undefinedIf set, this value will be used as the placeholder data for this particular query observer while the query is still in the loading data and no initialData has been provided.
queryFn?| typeof skipToken | ((context: object) => TQueryFnData | Promise<TQueryFnData>)undefinedThe function that the query will use to request data. Required, unless a default query function has been set via queryClient.setQueryDefaults or queryClient.setDefaultOptions. Receives a QueryFunctionContext. Must return a promise that will either resolve data or throw an error. The data cannot be undefined.
queryHash?stringundefinedThe hashed form of queryKey, computed with queryKeyHashFn (or the default hashing function otherwise). Used as the actual cache key internally.
queryKeyTQueryKey & objectundefinedThe query key to use for this query. The query key will be hashed into a stable hash. See Query Keys for more information. The query will automatically update when this key changes (as long as enabled is not set to false).
queryKeyHashFn?(queryKey: TQueryKey) => stringundefinedIf specified, this function is used to hash the queryKey to a string.
refetchInterval?| number | false | ((query: Query<TQueryFnData, TError, TQueryFnData, TQueryKey>) => number | false | undefined)falseIf set to a number, the query will continuously refetch at this frequency in milliseconds. If set to a function, the function will be executed with the latest data and query to compute a frequency
refetchIntervalInBackground?booleanfalseIf set to true, the query will continue to refetch while their tab/window is in the background.
refetchOnMount?| boolean | "always" | ((query: Query<TQueryFnData, TError, TQueryFnData, TQueryKey>) => boolean | "always")trueIf set to true, the query will refetch on mount if the data is stale. If set to false, will disable additional instances of a query to trigger background refetch. If set to 'always', the query will always refetch on mount (except when staleTime: 'static' is used). If set to a function, the function will be executed with the latest data and query to compute the value
refetchOnReconnect?| boolean | "always" | ((query: Query<TQueryFnData, TError, TQueryFnData, TQueryKey>) => boolean | "always")undefinedIf set to true, the query will refetch on reconnect if the data is stale. If set to false, the query will not refetch on reconnect. If set to 'always', the query will always refetch on reconnect (except when staleTime: 'static' is used). If set to a function, the function will be executed with the latest data and query to compute the value. Defaults to true unless networkMode is 'always'.
refetchOnWindowFocus?| boolean | "always" | ((query: Query<TQueryFnData, TError, TQueryFnData, TQueryKey>) => boolean | "always")trueIf set to true, the query will refetch on window focus if the data is stale. If set to false, the query will not refetch on window focus. If set to 'always', the query will always refetch on window focus (except when staleTime: 'static' is used). If set to a function, the function will be executed with the latest data and query to compute the value.
retry?| number | false | true | ((failureCount: number, error: TError) => boolean)undefinedIf false, failed queries will not retry by default. If true, failed queries will retry infinitely. If set to an integer number, e.g. 3, failed queries will retry until the failed query count meets that number. If set to a function (failureCount, error) => boolean failed queries will retry until the function returns false. Defaults to 3 on the client and 0 on the server.
retryDelay?number | ((failureCount: number, error: TError) => number)undefinedThis function receives a retryAttempt integer and the actual Error and returns the delay to apply before the next attempt in milliseconds. A function like attempt => Math.min(attempt > 1 ? 2 ** attempt * 1000 : 1000, 30 * 1000) applies exponential backoff. A function like attempt => attempt * 1000 applies linear backoff. Defaults to a function that applies exponential backoff, capped at 30 seconds.
retryOnMount?| false | true | ((query: Query<TQueryFnData, TError, TQueryFnData, TQueryKey>) => boolean)trueIf set to false, the query will not be retried on mount if it contains an error. If set to a function, the function will be executed with the query to compute the value.
select?(data: TQueryFnData) => TDataundefinedThis option can be used to transform or select a part of the data returned by the query function. It affects the returned data value, but does not affect what gets stored in the query cache. The select function will only run if data changed, or if the reference to the select function itself changes. To optimize, memoize the function so its reference stays stable across calls.
staleTime?| number | "static" | ((query: Query<TQueryFnData, TError, TQueryFnData, TQueryKey>) => number | "static")0The time in milliseconds after data is considered stale. If set to Infinity, the data will never be considered stale. If set to 'static', the data will never be considered stale. If set to a function, the function will be executed with the query to compute a staleTime.
structuralSharing?boolean | ((oldData: unknown, newData: unknown) => unknown)trueSet this to false to disable structural sharing between query results. Set this to a function which accepts the old and new data and returns resolved data of the same type to implement custom structural sharing logic.
subscribed?booleantrueSet this to false to unsubscribe this observer from updates to the query cache.
throwOnError?| false | true | ((error: TError, query: Query<TQueryFnData, TError, TQueryFnData, TQueryKey>) => boolean)falseWhether errors should be thrown instead of setting the error property. If set to true or suspense is true, all errors will be thrown to the error boundary. If set to false and suspense is false, errors are returned as state. If set to a function, it will be passed the error and the query, and it should return a boolean indicating whether to show the error in an error boundary (true) or return the error as state (false).

queryClient?

QueryClient

Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.

Returns

UseQueryResult<TData, TError>

The current query result. status is pending if there is no cached data to display, error if the last fetch attempt failed, or success if the query has data to display. isPending/isSuccess/isError are derived booleans for convenience.

Result properties

PropertyTypeDescription
dataTData | undefinedThe last successfully resolved data for the query.
dataUpdatedAtnumberThe timestamp for when the query most recently returned the status as "success".
errorTError | nullThe error object for the query, if an error was thrown. - Defaults to null.
errorUpdateCountnumberThe sum of all errors.
errorUpdatedAtnumberThe timestamp for when the query most recently returned the status as "error".
failureCountnumberThe failure count for the query. - Incremented every time the query fails. - Reset to 0 when the query succeeds.
failureReasonTError | nullThe failure reason for the query retry. - Reset to null when the query succeeds.
fetchStatus"fetching" | "paused" | "idle"The fetch status of the query. - fetching: Is true whenever the queryFn is executing, which includes initial pending as well as background refetch. - paused: The query wanted to fetch, but has been paused. - idle: The query is not fetching. - See Network Mode for more information.
isEnabledbooleantrue if this observer is enabled, false otherwise.
isErrorbooleanA derived boolean from the status variable, provided for convenience. - true if the query attempt resulted in an error.
isFetchedbooleanWill be true if the query has been fetched.
isFetchedAfterMountbooleanWill be true if the query has been fetched after the component mounted. - This property can be used to not show any previously cached data.
isFetchingbooleanA derived boolean from the fetchStatus variable, provided for convenience. - true whenever the queryFn is executing, which includes initial pending as well as background refetch.
isInitialLoadingbooleanDeprecated isInitialLoading is being deprecated in favor of isLoading and will be removed in the next major version.
isLoadingbooleanIs true whenever the first fetch for a query is in-flight. - Is the same as isFetching && isPending.
isLoadingErrorbooleanWill be true if the query failed while fetching for the first time.
isPausedbooleanA derived boolean from the fetchStatus variable, provided for convenience. - The query wanted to fetch, but has been paused.
isPendingbooleanWill be pending if there's no cached data and no query attempt was finished yet.
isPlaceholderDatabooleanWill be true if the data shown is the placeholder data.
isRefetchErrorbooleanWill be true if the query failed while refetching.
isRefetchingbooleanIs true whenever a background refetch is in-flight, which does not include initial pending. - Is the same as isFetching && !isPending.
isStalebooleanWill be true if the data in the cache is invalidated or if the data is older than the given staleTime.
isSuccessbooleanA derived boolean from the status variable, provided for convenience. - true if the query has received a response with no errors and is ready to display its data.
refetch(options?: RefetchOptions) => Promise<QueryObserverResult<TData, TError>>A function to manually refetch the query.
status"error" | "pending" | "success"The status of the query. - Will be: - pending if there's no cached data and no query attempt was finished yet. - error if the query attempt resulted in an error. - success if the query has received a response with no errors and is ready to display its data.