Skip to main content
The InfiniteQueryObserver extends QueryObserver to provide additional functionality for infinite queries (pagination). It powers hooks like useInfiniteQuery.

Constructor

Creates a new InfiniteQueryObserver instance.
QueryClient
required
The QueryClient instance to use.
InfiniteQueryObserverOptions
required
Options for the infinite query observer.

Example

Methods

InfiniteQueryObserver inherits all methods from QueryObserver and adds the following:

fetchNextPage

Fetches the next page of data.
FetchNextPageOptions
Options for fetching the next page.
Promise
Returns a promise that resolves with the infinite query result.

Example

fetchPreviousPage

Fetches the previous page of data.
FetchPreviousPageOptions
Options for fetching the previous page.
Promise
Returns a promise that resolves with the infinite query result.

Example

subscribe

Subscribes to the observer. Same as QueryObserver but with InfiniteQueryObserverResult.
(result: InfiniteQueryObserverResult<TData, TError>) => void
required
Function called when the query result changes.
function
Returns an unsubscribe function.

Example

getCurrentResult

Returns the current infinite query result.
InfiniteQueryObserverResult
Returns the current infinite query result.

setOptions

Updates the observer options. Automatically sets the infinite query behavior.
InfiniteQueryObserverOptions
required
New options for the observer.

getOptimisticResult

Returns an optimistic infinite query result.

InfiniteData Structure

The data returned by an infinite query has a special structure:

Example

Usage Example

Here’s a complete example:

Differences from QueryObserver

  1. Data Structure: Returns InfiniteData<TData, TPageParam> instead of TData
  2. Additional Methods: fetchNextPage() and fetchPreviousPage()
  3. Additional Result Properties: hasNextPage, hasPreviousPage, isFetchingNextPage, etc.
  4. Required Options: Requires initialPageParam, getNextPageParam
  5. Automatic Behavior: Automatically applies infinite query behavior

Page Management

You can control the number of pages kept in memory using the maxPages option:
When maxPages is set, the oldest pages will be removed as new pages are fetched.