
const {data,dataUpdatedAt,error,errorUpdatedAt,failureCount,isError,isFetched,isFetchedAfterMount,isFetching,isPaused,isLoading,isLoadingError,isPlaceholderData,isPreviousData,isRefetchError,isRefetching,isStale,isSuccess,refetch,remove,status,fetchStatus,} = useQuery(queryKey, queryFn?, {cacheTime,enabled,networkMode,initialData,initialDataUpdatedAt,isDataEqual,keepPreviousData,meta,notifyOnChangeProps,onError,onSettled,onSuccess,placeholderData,queryKeyHashFn,refetchInterval,refetchIntervalInBackground,refetchOnMount,refetchOnReconnect,refetchOnWindowFocus,retry,retryOnMount,retryDelay,select,staleTime,structuralSharing,suspense,useErrorBoundary,})// or using the object syntaxconst result = useQuery({queryKey,queryFn,enabled,})
Options
queryKey: unknown[]enabled is not set to false).queryFn: (context: QueryFunctionContext) => Promise<TData>undefined.enabled: booleanfalse to disable this query from automatically running.networkMode: 'online' | 'always' | 'offlineFirst'online'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.retryOnMount: booleanfalse, the query will not be retried on mount if it contains an error. Defaults to true.retryDelay: number | (retryAttempt: number, error: TError) => numberretryAttempt 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 | Infinity0Infinity, the data will never be considered stalecacheTime: number | Infinity5 * 60 * 1000 (5 minutes) or Infinity during SSRInfinity, will disable garbage collectionqueryKeyHashFn: (queryKey: QueryKey) => stringqueryKey to a string.refetchInterval: number | false | ((data: TData | undefined, query: Query) => number | false)refetchIntervalInBackground: 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.notifyOnChangeProps: string[] | "all"['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.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: TData | () => TDatastaleTime has been set.initialData is persisted to the cacheinitialDataUpdatedAt: number | (() => number | undefined)initialData itself was last updated.placeholderData: TData | () => TDataloading data and no initialData has been provided.placeholderData is not persisted to the cachekeepPreviousData: booleanfalsedata will be kept when fetching new data because the query key changed.structuralSharing: booleantruefalse, structural sharing between query results will be disabled.useErrorBoundary: undefined | boolean | (error: TError, query: Query) => booleanuseErrorBoundary value, which is undefinedtrue 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.context?: React.Context<QueryClient | undefined>defaultContext will be used.Returns
status: Stringloading 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's enabled property is set to false and has not been fetched yet data is the first initialData supplied to the query on initialization.isLoading: booleanstatus variable above, provided for convenience.isSuccess: booleanstatus variable above, provided for convenience.isError: booleanstatus variable above, provided for convenience.isLoadingError: booleantrue if the query failed while fetching for the first time.isRefetchError: booleantrue if the query failed while refetching.data: TDataundefined.dataUpdatedAt: numberstatus as "success".error: null | TErrornullerrorUpdatedAt: numberstatus as "error".isStale: booleantrue if the data in the cache is invalidated or if the data is older than the given staleTime.isPlaceholderData: booleantrue if the data shown is the placeholder data.isPreviousData: booleantrue when keepPreviousData is set and data from the previous query is returned.isFetched: booleantrue if the query has been fetched.isFetchedAfterMount: booleantrue if the query has been fetched after the component mounted.fetchStatus: FetchStatusfetching: Is true whenever the queryFn is executing, which includes initial loading as well as background refetches.paused: The query wanted to fetch, but has been paused.idle: The query is not fetching.isFetching: booleanfetchStatus variable above, provided for convenience.isPaused: booleanfetchStatus variable above, provided for convenience.isRefetching: booleantrue whenever a background refetch is in-flight, which does not include initial loadingisFetching && !isLoadingfailureCount: number0 when the query succeeds.refetch: (options: { throwOnError: boolean, cancelRefetch: boolean }) => Promise<UseQueryResult>throwOnError: true optioncancelRefetch?: booleantruefalse, no refetch will be made if there is already a request running.remove: () => voidThe best JavaScript newsletter! Delivered every Monday to over 76,000 devs.