Skip to main content

useInfiniteQuery

The useInfiniteQuery 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

type
The type of data returned by the query function for a single page
type
default:"DefaultError"
The type of error that can be thrown by the query function
type
default:"InfiniteData<TQueryFnData>"
The type of data returned by the select function (if provided)
type
default:"QueryKey"
The type of the query key
type
default:"unknown"
The type of the page parameter used for pagination

Parameters

UseInfiniteQueryOptions
required
Configuration options for the infinite query. Extends UseQueryOptions with additional infinite-specific options.
TQueryKey
required
A unique key for the query. Must be an array.
(context: QueryFunctionContext<TQueryKey, TPageParam>) => Promise<TQueryFnData>
required
The function that fetches a single page of data. Receives pageParam in the context.
TPageParam
required
The default page parameter to use for the initial page
(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.
(firstPage: TQueryFnData, allPages: TQueryFnData[], firstPageParam: TPageParam, allPageParams: TPageParam[]) => TPageParam | undefined | null
Function that returns the previous page parameter for bi-directional infinite queries
number
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.
boolean
default:"true"
Set to false to disable automatic query execution
number | ((query: Query) => number)
default:"0"
Time in milliseconds after data is considered stale
number
default:"300000"
Time in milliseconds that unused/inactive cache data remains in memory
boolean | 'always'
default:"true"
If set to true, the query will refetch on window focus if the data is stale
boolean | 'always'
default:"true"
If set to true, the query will refetch on mount if the data is stale
boolean | 'always'
default:"true"
If set to true, the query will refetch on reconnect if the data is stale
boolean | number | (failureCount: number, error: TError) => boolean
default:"3"
Number of retry attempts or function to determine if a request should be retried
(data: InfiniteData<TQueryFnData>) => TData
Function to transform or select a part of the data returned by the query function
boolean
default:"true"
Set this to false to unsubscribe this observer from updates to the query cache
QueryClient
Optional QueryClient instance to use. If not provided, the client from the nearest QueryClientProvider will be used.

Returns

Returns all properties from useQuery plus the following:
InfiniteData<TQueryFnData>
The aggregated data from all pages:
(options?: FetchNextPageOptions) => Promise<UseInfiniteQueryResult>
Function to fetch the next page of data. Options:
  • cancelRefetch?: boolean - Cancel any ongoing refetch
(options?: FetchPreviousPageOptions) => Promise<UseInfiniteQueryResult>
Function to fetch the previous page of data
boolean
Will be true if there is a next page to fetch (i.e., getNextPageParam returned a value other than undefined or null)
boolean
Will be true if there is a previous page to fetch
boolean
Will be true while fetching the next page
boolean
Will be true while fetching the previous page
boolean
Will be true whenever a fetch is in progress (including background refetches and next/previous page fetches)

Examples

Basic Usage

With Type Safety

Bi-directional Infinite Query

Infinite Scroll with Intersection Observer

With Data Transformation

Source

Implementation: useInfiniteQuery.ts:72