const {
fetchNextPage,
fetchPreviousPage,
hasNextPage,
hasPreviousPage,
isFetchingNextPage,
isFetchingPreviousPage,
promise,
...result
} = useInfiniteQuery({
queryKey,
queryFn: ({ pageParam }) => fetchPage(pageParam),
initialPageParam: 1,
...options,
getNextPageParam: (lastPage, allPages, lastPageParam, allPageParams) =>
lastPage.nextCursor,
getPreviousPageParam: (firstPage, allPages, firstPageParam, allPageParams) =>
firstPage.prevCursor,
})
Options
The options for useInfiniteQuery are identical to the useQuery hook with the addition of the following:
queryFn: (context: QueryFunctionContext) => Promise<TData>
defaultQueryFninitialPageParam: TPageParam
getNextPageParam: (lastPage, allPages, lastPageParam, allPageParams) => TPageParam | undefined | null
undefined or null to indicate there is no next page available.getPreviousPageParam: (firstPage, allPages, firstPageParam, allPageParams) => TPageParam | undefined | null
undefined or nullto indicate there is no previous page available.maxPages: number | undefined
undefined or equals 0, the number of pages is unlimitedundefinedgetNextPageParam and getPreviousPageParam must be properly defined if maxPages value is greater than 0 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[]
data.pageParams: unknown[]
isFetchingNextPage: boolean
true while fetching the next page with fetchNextPage.isFetchingPreviousPage: boolean
true while fetching the previous page with fetchPreviousPage.fetchNextPage: (options?: FetchNextPageOptions) => Promise<UseInfiniteQueryResult>
options.cancelRefetch: boolean if set to true, calling fetchNextPage repeatedly will invoke queryFn every time, whether the previous
invocation has resolved or not. Also, the result from previous invocations will be ignored. If set to false, calling fetchNextPage
repeatedly won't have any effect until the first invocation has resolved. Default is true.fetchPreviousPage: (options?: FetchPreviousPageOptions) => Promise<UseInfiniteQueryResult>
options.cancelRefetch: boolean same as for fetchNextPage.hasNextPage: boolean
true if there is a next page to be fetched (known via the getNextPageParam option).hasPreviousPage: boolean
true if there is a previous page to be fetched (known via the getPreviousPageParam option).isFetchNextPageError: boolean
true if the query failed while fetching the next page.isFetchPreviousPageError: boolean
true if the query failed while fetching the previous page.isRefetching: boolean
true whenever a background refetch is in-flight, which does not include initial pending or fetching of next or previous pageisFetching && !isPending && !isFetchingNextPage && !isFetchingPreviousPageisRefetchError: boolean
true if the query failed while refetching a page.promise: Promise<TData>
React.use() to fetch dataexperimental_prefetchInRender feature flag to be enabled on the QueryClient.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.
