Skip to main content

Overview

The InfiniteQueryObserver extends QueryObserver to provide support for infinite/paginated queries. It adds methods for fetching next and previous pages and tracks pagination state.

Constructor

QueryClient
required
The QueryClient instance
InfiniteQueryObserverOptions
required
Options for the infinite query observer
Example:

Methods

InfiniteQueryObserver inherits all methods from QueryObserver and adds:

fetchNextPage()

Fetches the next page of data.
FetchNextPageOptions
Returns: Promise resolving to the infinite query result Example:

fetchPreviousPage()

Fetches the previous page of data.
FetchPreviousPageOptions
Returns: Promise resolving to the infinite query result Example:

subscribe()

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

getCurrentResult()

Returns the current infinite query result without subscribing.
Returns: Current infinite query result
object
Extends all properties from QueryObserverResult and adds:

Type Parameters

  • TQueryFnData - The type of data returned by the query function for a single page
  • TError - The type of error that can be thrown (defaults to DefaultError)
  • TData - The type of data after selection/transformation
  • TQueryKey - The type of the query key
  • TPageParam - The type of the page parameter

Usage Example

Infinite Data Structure

The data returned by an infinite query has a specific structure:
Example:

Page Parameter Functions

The getNextPageParam and getPreviousPageParam functions determine if there are more pages to fetch:
Returning undefined or null from these functions sets hasNextPage or hasPreviousPage to false.