useInfiniteQuery

· abundance's blog


 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:

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:

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.