const { data, dataUpdatedAt, error, errorUpdatedAt, failureCount, failureReason, fetchStatus, isError, isFetched, isFetchedAfterMount, isFetching, isInitialLoading, isLoading, isLoadingError, isPaused, isPending, isPlaceholderData, isRefetchError, isRefetching, isStale, isSuccess, promise, refetch, status,} = useQuery( { queryKey, queryFn, gcTime, enabled, networkMode, initialData, initialDataUpdatedAt, meta, notifyOnChangeProps, placeholderData, queryKeyHashFn, refetchInterval, refetchIntervalInBackground, refetchOnMount, refetchOnReconnect, refetchOnWindowFocus, retry, retryOnMount, retryDelay, select, staleTime, structuralSharing, throwOnError, }, queryClient,)
Parameter1 (Options)
queryKey: unknown[]
enabled
is not set to false
).queryFn: (context: QueryFunctionContext) => Promise<TData>
undefined
.enabled: boolean | (query: Query) => boolean
false
to disable this query from automatically running.networkMode: 'online' | 'always' | 'offlineFirst
'online'
retry: boolean | number | (failureCount: number, error: TError) => boolean
false
, failed queries will not retry by default.true
, failed queries will retry infinitely.number
, e.g. 3
, failed queries will retry until the failed query count meets that number.3
on the client and 0
on the serverretryOnMount: boolean
false
, the query will not be retried on mount if it contains an error. Defaults to true
.retryDelay: number | (retryAttempt: number, error: TError) => number
retryAttempt
integer and the actual Error and returns the delay to apply before the next attempt in milliseconds.attempt => Math.min(attempt > 1 ? 2 ** attempt * 1000 : 1000, 30 * 1000)
applies exponential backoff.attempt => attempt * 1000
applies linear backoff.staleTime: number | ((query: Query) => number)
0
Infinity
, the data will never be considered stalestaleTime
.gcTime: number | Infinity
5 * 60 * 1000
(5 minutes) or Infinity
during SSRInfinity
, will disable garbage collectionqueryKeyHashFn: (queryKey: QueryKey) => string
queryKey
to a string.refetchInterval: number | false | ((query: Query) => number | false | undefined)
refetchIntervalInBackground: boolean
true
, queries that are set to continuously refetch with a refetchInterval
will continue to refetch while their tab/window is in the backgroundrefetchOnMount: boolean | "always" | ((query: Query) => boolean | "always")
true
true
, the query will refetch on mount if the data is stale.false
, the query will not refetch on mount."always"
, the query will always refetch on mount.refetchOnWindowFocus: boolean | "always" | ((query: Query) => boolean | "always")
true
true
, the query will refetch on window focus if the data is stale.false
, the query will not refetch on window focus."always"
, the query will always refetch on window focus.refetchOnReconnect: boolean | "always" | ((query: Query) => boolean | "always")
true
true
, the query will refetch on reconnect if the data is stale.false
, the query will not refetch on reconnect."always"
, the query will always refetch on reconnect.notifyOnChangeProps: string[] | "all" | (() => string[] | "all" | undefined)
['data', 'error']
for example, the component will only re-render when the data
or error
properties change."all"
, the component will opt-out of smart tracking and re-render whenever a query is updated.select: (data: TData) => unknown
data
value, but does not affect what gets stored in the query cache.select
function will only run if data
changed, or if the reference to the select
function itself changes. To optimize, wrap the function in useCallback
.initialData: TData | () => TData
staleTime
has been set.initialData
is persisted to the cacheinitialDataUpdatedAt: number | (() => number | undefined)
initialData
itself was last updated.placeholderData: TData | (previousValue: TData | undefined; previousQuery: Query | undefined,) => TData
pending
state.placeholderData
is not persisted to the cacheplaceholderData
, as a first argument you will receive previously watched query data if available, and the second argument will be the complete previousQuery instance.structuralSharing: boolean | (oldData: unknown | undefined, newData: unknown) => unknown)
true
false
, structural sharing between query results will be disabled.throwOnError: undefined | boolean | (error: TError, query: Query) => boolean
throwOnError
value, which is undefined
true
if you want errors to be thrown in the render phase and propagate to the nearest error boundaryfalse
to disable suspense
's default behavior of throwing errors to the error boundary.true
) or return the error as state (false
)meta: Record<string, unknown>
query
is available, and is also part of the QueryFunctionContext
provided to the queryFn
.Parameter2 (QueryClient)
queryClient?: QueryClient
,
Returns
status: QueryStatus
pending
if there's no cached data and no query attempt was finished yet.error
if the query attempt resulted in an error. The corresponding error
property has the error received from the attempted fetchsuccess
if the query has received a response with no errors and is ready to display its data. The corresponding data
property on the query is the data received from the successful fetch or if the query's enabled
property is set to false
and has not been fetched yet data
is the first initialData
supplied to the query on initialization.isPending: boolean
status
variable above, provided for convenience.isSuccess: boolean
status
variable above, provided for convenience.isError: boolean
status
variable above, provided for convenience.isLoadingError: boolean
true
if the query failed while fetching for the first time.isRefetchError: boolean
true
if the query failed while refetching.data: TData
undefined
.dataUpdatedAt: number
status
as "success"
.error: null | TError
null
errorUpdatedAt: number
status
as "error"
.isStale: boolean
true
if the data in the cache is invalidated or if the data is older than the given staleTime
.isPlaceholderData: boolean
true
if the data shown is the placeholder data.isFetched: boolean
true
if the query has been fetched.isFetchedAfterMount: boolean
true
if the query has been fetched after the component mounted.fetchStatus: FetchStatus
fetching
: Is true
whenever the queryFn is executing, which includes initial pending
as well as background refetches.paused
: The query wanted to fetch, but has been paused
.idle
: The query is not fetching.isFetching: boolean
fetchStatus
variable above, provided for convenience.isPaused: boolean
fetchStatus
variable above, provided for convenience.isRefetching: boolean
true
whenever a background refetch is in-flight, which does not include initial pending
isFetching && !isPending
isLoading: boolean
true
whenever the first fetch for a query is in-flightisFetching && isPending
isInitialLoading: boolean
isLoading
, will be removed in the next major version.failureCount: number
0
when the query succeeds.failureReason: null | TError
null
when the query succeeds.errorUpdateCount: number
refetch: (options: { throwOnError: boolean, cancelRefetch: boolean }) => Promise<UseQueryResult>
throwOnError: true
optioncancelRefetch?: boolean
true
false
, no refetch will be made if there is already a request running.promise: Promise<TData>
experimental_prefetchInRender
feature flag to be enabled on the QueryClient
.“This course is the best way to learn how to use React Query in real-world applications.”—Tanner LinsleyCheck it out