Basic Usage
UseuseInfiniteQuery to fetch paginated data. The hook manages pages of data and provides methods to fetch more:
Required Options
1
initialPageParam
The initial page parameter passed to the query function. This is required and determines the starting point for your infinite query.
2
getNextPageParam
Function that receives the last page and returns the next page parameter. Return
undefined or null when there are no more pages.Bidirectional Infinite Queries
Fetch data in both directions by implementing both next and previous page parameters:Both
getPreviousPageParam and getNextPageParam receive the full list of pages and page parameters as additional arguments, allowing you to calculate the next parameter based on all loaded data.Infinite Scroll Implementation
CombineuseInfiniteQuery with the Intersection Observer API for automatic infinite scrolling:
Limiting Maximum Pages
Control memory usage by limiting the number of cached pages:Data Structure
Thedata object contains two properties:
pages: An array of page data objectspageParams: An array of page parameters used to fetch each page
Query Function Context
The query function receives a context object with the page parameter:Refetching Pages
By default, refetching an infinite query will refetch all pages. You can customize this:The
refetchPage function receives the page data and index, allowing you to selectively refetch specific pages.