Capítulo 62 de 80

Chapter 62: useQuery

Core Idea

The full option/return surface behind everything covered in the Guides chapters — most options here are explained in depth elsewhere (Important Defaults, Network Mode, Render Optimizations); this chapter is the compact reference for looking up an exact name, type, or default.

Key Concepts

  • Signature: useQuery(options, queryClient?) — second argument is an optional explicit QueryClient override (defaults to the nearest QueryClientProvider's).
  • Required options: queryKey (array); queryFn (unless a default query function is registered).
  • Options already covered in depth elsewhere: enabled, networkMode, retry/retryOnMount/retryDelay, staleTime, gcTime, refetchInterval/refetchIntervalInBackground, refetchOnMount/refetchOnWindowFocus/refetchOnReconnect, notifyOnChangeProps, select, initialData/initialDataUpdatedAt, placeholderData, structuralSharing, throwOnError, meta.
  • queryKeyHashFn: override how a queryKey is hashed to a cache-lookup string — rarely needed, an escape hatch for exotic key shapes the default JSON-based hash can't handle correctly.
  • subscribed (default true): set false to mount a useQuery instance that doesn't subscribe to the cache at all — it won't trigger queryFn itself and won't react to updates from elsewhere. A narrow tool for cases needing the hook's shape without its normal reactivity.

Reference Tables

Return fieldMeaning
status'pending' | 'error' | 'success'
isPending/isSuccess/isErrorDerived booleans from status
isLoadingErrorFailed on the first fetch
isRefetchErrorFailed on a background refetch (had prior data)
dataLast successfully resolved data (undefined by default)
dataUpdatedAt / errorUpdatedAtTimestamp of most recent success/error
isStaleInvalidated, or older than staleTime
isPlaceholderDataCurrently showing placeholderData, not real data
isFetched / isFetchedAfterMountHas ever fetched / has fetched since this mount
fetchStatus'fetching' | 'paused' | 'idle'
isFetching / isPausedDerived from fetchStatus
isRefetchingisFetching && !isPending — background fetch, not initial
isLoadingisFetching && isPending — the actual first-fetch spinner flag
isInitialLoadingDeprecated alias of isLoading
isEnabledWhether this observer is currently enabled
failureCount / failureReasonCurrent retry attempt count / latest error during retries
errorUpdateCountTotal error count across the query's lifetime
refetch(options)Manual refetch trigger; { throwOnError, cancelRefetch }

Key Takeaways

  1. isLoading (not isPending) is the correct flag for "show a spinner because this is genuinely fetching for the first time" — isPending alone is also true for disabled/lazy queries doing nothing.
  2. isRefetching vs. isFetching: use isRefetching specifically to distinguish a background update from the initial fetch.
  3. refetch()'s errors are only logged by default — pass throwOnError: true explicitly if a manual refetch failure should propagate.

Connects To

  • Queries: the conceptual walkthrough of status/fetchStatus this reference formalizes.
  • Query Retries, Network Mode, Render Optimizations: full guides for the options only summarized here.