Skip to main content

Overview

The QueryObserver class is used to subscribe to a query and receive reactive updates when the query’s state changes. It’s the foundation for framework-specific hooks like useQuery in React.

Constructor

QueryClient
required
The QueryClient instance
QueryObserverOptions
required
Options for the query observer
Example:

Methods

subscribe()

Subscribes to query updates.
(result: QueryObserverResult<TData, TError>) => void
required
Callback function that receives query results
Returns: Function to unsubscribe Example:

getCurrentResult()

Returns the current result without subscribing.
Returns: Current query result
object

setOptions()

Updates the observer’s options.
QueryObserverOptions
required
New options for the observer
Example:

getOptimisticResult()

Returns an optimistic result based on the provided options, without triggering a fetch.
Returns: Optimistic query result
This method is useful for framework integrations that need to compute what the result would be before actually subscribing.

refetch()

Manually refetches the query.
RefetchOptions
Returns: Promise resolving to the query result Example:

fetchOptimistic()

Fetches a query with new options without subscribing.
Returns: Promise resolving to the query result

getCurrentQuery()

Returns the underlying Query instance.
Returns: The Query instance

trackResult()

Returns a proxied result that tracks which properties are accessed.
This is used internally for optimizing re-renders by only triggering updates when tracked properties change.

destroy()

Destroys the observer and unsubscribes from the query.
Example:

Properties

options

The current options for the observer.

Type Parameters

  • TQueryFnData - The type of data returned by the query function
  • TError - The type of error that can be thrown (defaults to DefaultError)
  • TData - The type of data after selection/transformation
  • TQueryData - The type of data stored in the cache
  • TQueryKey - The type of the query key

Usage Example