Skip to main content

Overview

The QueriesObserver allows you to observe multiple queries at once and optionally combine their results. It’s the foundation for framework-specific hooks like useQueries in React.

Constructor

QueryClient
required
The QueryClient instance
Array<QueryObserverOptions>
required
Array of query options
QueriesObserverOptions<TCombinedResult>
Example:

Methods

subscribe()

Subscribes to updates from all queries.
(results: Array<QueryObserverResult>) => void
required
Callback function that receives an array of query results
Returns: Function to unsubscribe Example:

getCurrentResult()

Returns the current results from all queries without subscribing.
Returns: Array of current query results Example:

setQueries()

Updates the queries being observed.
Array<QueryObserverOptions>
required
New array of query options
QueriesObserverOptions<TCombinedResult>
New observer options
Example:

getQueries()

Returns the underlying Query instances.
Returns: Array of Query instances

getObservers()

Returns the underlying QueryObserver instances.
Returns: Array of QueryObserver instances

getOptimisticResult()

Returns optimistic results and a combine function.
Returns: Tuple of [rawResults, combineFunction, trackFunction]

destroy()

Destroys the observer and cleans up all query observers.
Example:

Combining Results

You can provide a combine function to transform the array of results into any shape:

Type Parameters

  • TCombinedResult - The type of the combined result (defaults to Array<QueryObserverResult>)

Usage Example

Dynamic Queries

You can dynamically add or remove queries:

Result Array Structure

The results array maintains the same order as the queries array:
When using the combine option, the combined result is memoized and only recomputed when the individual query results change.