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 fromQueryObserver 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
- Data Structure: Returns
InfiniteData<TData, TPageParam>instead ofTData - Additional Methods:
fetchNextPage()andfetchPreviousPage() - Additional Result Properties:
hasNextPage,hasPreviousPage,isFetchingNextPage, etc. - Required Options: Requires
initialPageParam,getNextPageParam - Automatic Behavior: Automatically applies infinite query behavior
Page Management
You can control the number of pages kept in memory using themaxPages option:
maxPages is set, the oldest pages will be removed as new pages are fetched.