Defined in: packages/query-core/src/queryObserver.ts:56
A QueryObserver watches a single query in the QueryCache and computes a QueryObserverResult from its state, recomputing and notifying subscribers whenever the underlying query (or the observer's options) changes. It is the primitive that framework adapters (e.g. useQuery) build their hooks on top of, but it can also be used directly to observe and switch between queries outside of any framework.
const observer = new QueryObserver(queryClient, {
queryKey: ['posts'],
queryFn: fetchPosts,
})
const unsubscribe = observer.subscribe((result) => {
console.log(result.data)
})TQueryFnData = unknown
TError = DefaultError
TData = TQueryFnData
TQueryData = TQueryFnData
TQueryKey extends QueryKey = QueryKey
new QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>(client: QueryClient, options: QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>): QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>;Defined in: packages/query-core/src/queryObserver.ts:86
QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>
Subscribable<QueryObserverListener<TData, TError>>.constructoroptions: QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>;Defined in: packages/query-core/src/queryObserver.ts:88
destroy(): void;Defined in: packages/query-core/src/queryObserver.ts:162
Stops observing the current query: clears all listeners, cancels the stale and refetch-interval timers, and removes this observer from the query it was observing.
void
fetchOptimistic(options: QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>): Promise<QueryObserverResult<TData, TError>>;Defined in: packages/query-core/src/queryObserver.ts:407
Fetches a query defined by the given options without affecting this observer's own tracked query or result, and returns a promise that resolves with the QueryObserverResult for that fetch. This is useful for prefetching data that another observer (e.g. a query about to be navigated to) will need, ahead of time.
QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
The observer options of the query to fetch.
Promise<QueryObserverResult<TData, TError>>
A promise that resolves with the result for the fetched query.
const result = await observer.fetchOptimistic({
queryKey: ['posts', 2],
queryFn: () => fetchPost(2),
})
console.log(result.data)getCurrentQuery(): Query<TQueryFnData, TError, TQueryData, TQueryKey>;Defined in: packages/query-core/src/queryObserver.ts:366
Returns the Query instance this observer is currently observing.
Query<TQueryFnData, TError, TQueryData, TQueryKey>
The observed query.
getCurrentResult(): QueryObserverResult<TData, TError>;Defined in: packages/query-core/src/queryObserver.ts:325
Returns the most recently computed QueryObserverResult for the observed query. This is a point-in-time read; to be notified of updates as they happen, subscribe to the observer instead (its inherited subscribe method).
QueryObserverResult<TData, TError>
The current result.
const result = observer.getCurrentResult()
console.log(result.status, result.data)getOptimisticResult(options: DefaultedQueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>): QueryObserverResult<TData, TError>;Defined in: packages/query-core/src/queryObserver.ts:276
Computes the result the observer would produce for the given (already-defaulted) options right now, building the underlying Query if it doesn't exist yet, without waiting for a subscription callback. Called by framework adapters on every render (e.g. useQuery) so the returned value is available synchronously, ahead of setOptions triggering an actual fetch.
DefaultedQueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
The defaulted observer options to compute the result for.
QueryObserverResult<TData, TError>
The result for the given options.
hasListeners(): boolean;Defined in: packages/query-core/src/subscribable.ts:43
Returns true while at least one listener is registered, false once they have all unsubscribed.
boolean
true if at least one listener is registered.
Subscribable.hasListenersrefetch(options: RefetchOptions): Promise<QueryObserverResult<TData, TError>>;Defined in: packages/query-core/src/queryObserver.ts:382
Refetches the observed query and returns a promise that resolves with the resulting QueryObserverResult.
RefetchOptions = {}
Set cancelRefetch to false to keep a running fetch instead of cancelling it, and throwOnError to true to reject when the refetch fails.
Promise<QueryObserverResult<TData, TError>>
A promise that resolves with the result after the refetch.
const result = await observer.refetch({ cancelRefetch: false })
console.log(result.data)setOptions(options: QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>): void;Defined in: packages/query-core/src/queryObserver.ts:184
Updates the observer's options. This will re-resolve the query being observed (switching to a different query if the queryKey changed), trigger a fetch if the new options require one and the observer has subscribers, recompute the current result, and reschedule the stale and refetch-interval timers as needed.
QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
The new observer options. They are defaulted with QueryClient#defaultQueryOptions before being applied.
void
observer.setOptions({ queryKey: ['posts', 1], queryFn: () => fetchPost(1) })
// later: switch to a different query, reusing the same observer
observer.setOptions({ queryKey: ['posts', 2], queryFn: () => fetchPost(2) })shouldFetchOnReconnect(): boolean;Defined in: packages/query-core/src/queryObserver.ts:135
Returns whether the observed query is currently stale and configured (via the refetchOnReconnect option) to refetch when the network reconnects.
boolean
true if the observer should refetch the query on reconnect.
shouldFetchOnWindowFocus(): boolean;Defined in: packages/query-core/src/queryObserver.ts:149
Returns whether the observed query is currently stale and configured (via the refetchOnWindowFocus option) to refetch when the window regains focus.
boolean
true if the observer should refetch the query on window focus.
subscribe(listener: QueryObserverListener): () => void;Defined in: packages/query-core/src/subscribable.ts:28
Registers a listener to be called on every update this object notifies about. Returns a function that removes the listener again — call it to stop listening. The base class never drops a listener on its own, though some subclasses clear all of theirs in destroy().
QueryObserverListener
Called on each update, with whatever the subclass passes to its subscribers.
A function that removes the listener.
(): void;void
const unsubscribe = subscribable.subscribe(() => {
// react to the update
})
unsubscribe()Subscribable.subscribetrackProp(key:
| "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"): void;Defined in: packages/query-core/src/queryObserver.ts:358
Records that the given QueryObserverResult property was read, so a subsequent update only notifies this observer if a tracked property actually changed. Normally called indirectly via QueryObserver#trackResult's proxy; exposed directly for adapters that track property access themselves (e.g. through their own reactivity system) instead of via the proxy.
The name of the property that was read.
"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"
void
trackResult(result: QueryObserverResult<TData, TError>, onPropTracked?: (key:
| "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") => void): QueryObserverResult<TData, TError>;Defined in: packages/query-core/src/queryObserver.ts:338
Wraps a QueryObserverResult in a Proxy that records which properties are read, via QueryObserver#trackProp (and an optional onPropTracked callback). Used by framework adapters when notifyOnChangeProps is not set, to implement its default "only re-render on properties you actually read" behavior.
QueryObserverResult<TData, TError>
The result to wrap.
(key: | "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") => void
Called with the name of each property that is read.
QueryObserverResult<TData, TError>
A proxy of result that tracks property reads.
updateResult(): void;Defined in: packages/query-core/src/queryObserver.ts:745
Recomputes and stores the current result from the current query/options, notifying listeners if it changed. Framework adapters call this right after subscribing to make sure no query update was missed in the gap between creating the observer and subscribing to it.
void