const {
fetchNextPage,
fetchPreviousPage,
hasNextPage,
hasPreviousPage,
isFetchingNextPage,
isFetchingPreviousPage,
...result
} = useInfiniteQuery({
queryKey,
queryFn: ({ pageParam = 1 }) => fetchPage(pageParam),
...options,
getNextPageParam: (lastPage, allPages) => lastPage.nextCursor,
getPreviousPageParam: (firstPage, allPages) => firstPage.prevCursor,
})Options
The options for useInfiniteQuery are identical to the useQuery hook with the addition of the following:
queryFn: (context: QueryFunctionContext) => Promise<TData>
getNextPageParam: (lastPage, allPages) => unknown | undefined
getPreviousPageParam: (firstPage, allPages) => unknown | undefined
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.
It should return a single variable that will be passed as the last optional parameter to your query function.
Return undefined to indicate there is no previous page available.
Returns
The returned properties for useInfiniteQuery are identical to the useQuery hook, with the addition of the following and a small difference in isRefetching:
data.pages: TData[]
data.pageParams: unknown[]
isFetchingNextPage: boolean
isFetchingPreviousPage: boolean
fetchNextPage: (options?: FetchNextPageOptions) => Promise<UseInfiniteQueryResult>
fetchPreviousPage: (options?: FetchPreviousPageOptions) => Promise<UseInfiniteQueryResult>
hasNextPage: boolean
hasPreviousPage: boolean
isRefetching: boolean
Is true whenever a background refetch is in-flight, which does not include initial loading or fetching of next or previous page
Is the same as isFetching && !isLoading && !isFetchingNextPage && !isFetchingPreviousPage
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.