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
options InfiniteQueryObserverOptions
required
Options for the infinite query observer queryFn QueryFunction<TQueryFnData, TQueryKey, TPageParam>
required
Function that fetches a page of data. Receives pageParam in context.
The initial page parameter
getNextPageParam (lastPage: TQueryFnData, allPages: TQueryFnData[], lastPageParam: TPageParam, allPageParams: TPageParam[]) => TPageParam | undefined | null
required
Function to get the next page parameter. Return undefined or null if there are no more pages.
getPreviousPageParam (firstPage: TQueryFnData, allPages: TQueryFnData[], firstPageParam: TPageParam, allPageParams: TPageParam[]) => TPageParam | undefined | null
Function to get the previous page parameter. Return undefined or null if there are no previous pages.
Maximum number of pages to store in the data
Example:
Methods
InfiniteQueryObserver inherits all methods from QueryObserver and adds:
fetchNextPage()
Fetches the next page of data.
Cancel currently running request before fetching next page. Defaults to true.
Throw error instead of returning it in result. Defaults to false.
Returns: Promise resolving to the infinite query result
Example:
fetchPreviousPage()
Fetches the previous page of data.
Cancel currently running request before fetching previous page. Defaults to true.
Throw error instead of returning it in result. Defaults to false.
Returns: Promise resolving to the infinite query result
Example:
subscribe()
Subscribes to infinite query updates.
listener (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
InfiniteQueryObserverResult Extends all properties from QueryObserverResult and adds: Show additional properties
data InfiniteData<TData, TPageParam>
The infinite data object containing pages and pageParams Array of page parameters corresponding to each page
Is true if there is a next page to fetch
Is true if there is a previous page to fetch
Is true while fetching the next page
Is true while fetching the previous page
Is true if the query failed while fetching the next page
Is true if the query failed while fetching the previous page
fetchNextPage (options?: FetchNextPageOptions) => Promise<InfiniteQueryObserverResult>
Function to fetch the next page
fetchPreviousPage (options?: FetchPreviousPageOptions) => Promise<InfiniteQueryObserverResult>
Function to fetch the previous page
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.