
const {data,error,failureCount,isError,isFetchedAfterMount,isFetching,isIdle,isLoading,isPreviousData,isStale,isSuccess,refetch,remove,status,} = useQuery(queryKey, queryFn?, {cacheTime,enabled,initialData,isDataEqual,keepPreviousData,notifyOnStatusChange,onError,onSettled,onSuccess,queryFnParamsFilter,queryKeyHashFn,refetchInterval,refetchIntervalInBackground,refetchOnMount,refetchOnReconnect,refetchOnWindowFocus,retry,retryDelay,staleTime,structuralSharing,suspense,useErrorBoundary,})// or using the object syntaxconst result = useQuery({queryKey,queryFn,enabled,})
Options
queryKey: string | unknown[]enabled is not set to false).queryFn: (...params: unknown[]) => Promise<TData>enabled: booleanfalse to disable this query from automatically running.retry: boolean | number | (failureCount: number, error: TError) => booleanfalse, 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.retryDelay: (retryAttempt: number) => numberretryAttempt integer 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 | InfinityInfinity, the data will never be considered stalecacheTime: number | InfinityInfinity, will disable garbage collectionrefetchInterval: false | numberrefetchIntervalInBackground: booleantrue, queries that are set to continuously refetch with a refetchInterval will continue to refetch while their tab/window is in the backgroundrefetchOnMount: boolean | "always"truetrue, 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"truetrue, 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"truetrue, 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.notifyOnStatusChange: booleanfalse to only re-render when there are changes to data or error.true.onSuccess: (data: TData) => voidonError: (error: TError) => voidonSettled: (data?: TData, error?: TError) => voidselect: (data: TData) => unknownsuspense: booleantrue to enable suspense mode.true, useQuery will suspend when status === 'loading'true, useQuery will throw runtime errors when status === 'error'initialData: unknown | () => unknownstaleTime has been set.keepPreviousData: booleanfalsedata will be kept when fetching new data because the query key changed.queryFnParamsFilter: (...params: unknown[]) => unknown[]queryFn.queryFnParamsFilter: params => params.slice(1).structuralSharing: booleantruefalse, structural sharing between query results will be disabled.Returns
status: Stringidle if the query is idle. This only happens if a query is initialized with enabled: false and no initial data is available.loading if the query is in a "hard" loading state. This means there is no cached data and the query is currently fetching, eg isFetching === trueerror 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 is in manual mode and has not been fetched yet data is the first initialData supplied to the query on initialization.isIdle: booleanstatus variable above, provided for convenience.isLoading: booleanstatus variable above, provided for convenience.isSuccess: booleanstatus variable above, provided for convenience.isError: booleanstatus variable above, provided for convenience.data: TDataundefined.error: null | TErrornullisStale: booleantrue if the data in the cache is invalidated or if the data is older than the given staleTime.isPreviousData: booleantrue when keepPreviousData is set and data from the previous query is returned.isFetchedAfterMount: booleantrue if the query has been fetched after the component mounted.isFetching: booleantrue so long as manual is set to falsetrue if the query is currently fetching, including background fetching.failureCount: number0 when the query succeeds.refetch: (options: { throwOnError: boolean }) => Promise<UseQueryResult>throwOnError: true optionremove: () => voidThe latest TanStack news, articles, and resources, sent to your inbox.