useInfiniteQuery
TheuseInfiniteQuery hook is used for fetching data that is paginated or loaded incrementally (infinite scroll). It extends useQuery with additional functionality for managing multiple pages of data.
Import
Signature
Type Parameters
The type of data returned by the query function for a single page
The type of error that can be thrown by the query function
The type of data returned by the select function (if provided)
The type of the query key
The type of the page parameter used for pagination
Parameters
Configuration options for the infinite query. Extends
UseQueryOptions with additional infinite-specific options.A unique key for the query. Must be an array.
The function that fetches a single page of data. Receives
pageParam in the context.The default page parameter to use for the initial page
getNextPageParam
(lastPage: TQueryFnData, allPages: TQueryFnData[], lastPageParam: TPageParam, allPageParams: TPageParam[]) => TPageParam | undefined | null
required
Function that returns the next page parameter. Return
undefined or null to indicate there are no more pages.getPreviousPageParam
(firstPage: TQueryFnData, allPages: TQueryFnData[], firstPageParam: TPageParam, allPageParams: TPageParam[]) => TPageParam | undefined | null
Function that returns the previous page parameter for bi-directional infinite queries
Maximum number of pages to store in the query data at once. When the maximum is reached, fetching a new page will remove either the first or last page from the data, depending on the direction of the fetch.
Set to
false to disable automatic query executionTime in milliseconds after data is considered stale
Time in milliseconds that unused/inactive cache data remains in memory
If set to
true, the query will refetch on window focus if the data is staleIf set to
true, the query will refetch on mount if the data is staleIf set to
true, the query will refetch on reconnect if the data is staleNumber of retry attempts or function to determine if a request should be retried
Function to transform or select a part of the data returned by the query function
Set this to
false to unsubscribe this observer from updates to the query cacheOptional QueryClient instance to use. If not provided, the client from the nearest
QueryClientProvider will be used.Returns
Returns all properties fromuseQuery plus the following:
The aggregated data from all pages:
Function to fetch the next page of data. Options:
cancelRefetch?: boolean- Cancel any ongoing refetch
Function to fetch the previous page of data
Will be
true if there is a next page to fetch (i.e., getNextPageParam returned a value other than undefined or null)Will be
true if there is a previous page to fetchWill be
true while fetching the next pageWill be
true while fetching the previous pageWill be
true whenever a fetch is in progress (including background refetches and next/previous page fetches)