1const {
2 fetchNextPage,
3 fetchPreviousPage,
4 hasNextPage,
5 hasPreviousPage,
6 isFetchingNextPage,
7 isFetchingPreviousPage,
8 ...result
9} = useInfiniteQuery({
10 queryKey,
11 queryFn: ({ pageParam }) => fetchPage(pageParam),
12 initialPageParam: 1,
13 ...options,
14 getNextPageParam: (lastPage, allPages, lastPageParam, allPageParams) =>
15 lastPage.nextCursor,
16 getPreviousPageParam: (firstPage, allPages, firstPageParam, allPageParams) =>
17 firstPage.prevCursor,
18})
Options
The options for useInfiniteQuery
are identical to the useQuery
hook with the addition of the following:
queryFn: (context: QueryFunctionContext) => Promise<TData>
- Required, but only if no default query function has been defined
defaultQueryFn
- The function that the query will use to request data.
- Receives a QueryFunctionContext
- Must return a promise that will either resolve data or throw an error.
- Required, but only if no default query function has been defined
initialPageParam: TPageParam
- Required
- The default page param to use when fetching the first page.
getNextPageParam: (lastPage, allPages, lastPageParam, allPageParams) => TPageParam | undefined | null
- Required
- When new data is received for this query, this function receives both the last page of the infinite list of data and the full array of all pages, as well as pageParam information.
- It should return a single variable that will be passed as the last optional parameter to your query function.
- Return
undefined
ornull
to indicate there is no next page available.
getPreviousPageParam: (firstPage, allPages, firstPageParam, allPageParams) => TPageParam | undefined | null
- When new data is received for this query, this function receives both the first page of the infinite list of data and the full array of all pages, as well as pageParam information.
- It should return a single variable that will be passed as the last optional parameter to your query function.
- Return
undefined
ornull
to indicate there is no previous page available.
maxPages: number | undefined
- The maximum number of pages to store in the infinite query data.
- When the maximum number of pages is reached, fetching a new page will result in the removal of either the first or last page from the pages array, depending on the specified direction.
- If
undefined
or equals0
, the number of pages is unlimited - Default value is
undefined
getNextPageParam
andgetPreviousPageParam
must be properly defined ifmaxPages
value is greater than0
to allow fetching a page in both directions when needed.
Returns
The returned properties for useInfiniteQuery
are identical to the useQuery
hook, with the addition of the following properties and a small difference in isRefetching
and isRefetchError
:
data.pages: TData[]
- Array containing all pages.
data.pageParams: unknown[]
- Array containing all page params.
isFetchingNextPage: boolean
- Will be
true
while fetching the next page withfetchNextPage
.
- Will be
isFetchingPreviousPage: boolean
- Will be
true
while fetching the previous page withfetchPreviousPage
.
- Will be
fetchNextPage: (options?: FetchNextPageOptions) => Promise<UseInfiniteQueryResult>
- This function allows you to fetch the next "page" of results.
options.cancelRefetch: boolean
if set totrue
, callingfetchNextPage
repeatedly will invokequeryFn
every time, whether the previous invocation has resolved or not. Also, the result from previous invocations will be ignored. If set tofalse
, callingfetchNextPage
repeatedly won't have any effect until the first invocation has resolved. Default istrue
.
fetchPreviousPage: (options?: FetchPreviousPageOptions) => Promise<UseInfiniteQueryResult>
- This function allows you to fetch the previous "page" of results.
options.cancelRefetch: boolean
same as forfetchNextPage
.
hasNextPage: boolean
- Will be
true
if there is a next page to be fetched (known via thegetNextPageParam
option).
- Will be
hasPreviousPage: boolean
- Will be
true
if there is a previous page to be fetched (known via thegetPreviousPageParam
option).
- Will be
isFetchNextPageError: boolean
- Will be
true
if the query failed while fetching the next page.
- Will be
isFetchPreviousPageError: boolean
- Will be
true
if the query failed while fetching the previous page.
- Will be
isRefetching: boolean
- Will be
true
whenever a background refetch is in-flight, which does not include initialpending
or fetching of next or previous page - Is the same as
isFetching && !isPending && !isFetchingNextPage && !isFetchingPreviousPage
- Will be
isRefetchError: boolean
- Will be
true
if the query failed while refetching a page.
- Will be
Keep in mind that imperative fetch calls, such as fetchNextPage
, may interfere with the default refetch behaviour, resulting in outdated data. Make sure to call these functions only in response to user actions, or add conditions like hasNextPage && !isFetching
.