Capítulo 64 de 80

Chapter 64: useInfiniteQuery

Core Idea

Superset of useQuery's options/returns — the reference companion to the Infinite Queries guide, listing every field precisely (including a return-shape difference in isRefetching that excludes next/previous-page fetches, not just the initial one).

Key Concepts

  • Extra required options beyond useQuery: initialPageParam (seed value for the first page); getNextPageParam(lastPage, allPages, lastPageParam, allPageParams) (returns the next pageParam, or undefined/null for "no more pages" — required). getPreviousPageParam has the mirrored signature for bi-directional lists, optional unless bi-directional.
  • maxPages: caps stored/refetched pages — when the cap is hit, fetching a new page evicts the oldest page from the opposite end (requires both getNextPageParam and getPreviousPageParam properly defined once maxPages > 0, so eviction/refetch can work in both directions).
  • Return shape additions: data.pages/data.pageParams (arrays, same length, paired by index); fetchNextPage/fetchPreviousPage (trigger loading more, both accept { cancelRefetch } — default true, meaning repeated calls always invoke queryFn again and ignore earlier in-flight results; false makes repeated calls no-ops until the first resolves); hasNextPage/hasPreviousPage; isFetchingNextPage/isFetchingPreviousPage; isFetchNextPageError/isFetchPreviousPageError.
  • isRefetching redefinition: for infinite queries, isRefetching excludes both the initial fetch and next/previous-page fetches — isFetching && !isPending && !isFetchingNextPage && !isFetchingPreviousPage. This is stricter than the plain useQuery definition.
  • Imperative-call caution: calling fetchNextPage/fetchPreviousPage can interfere with the query's normal background-refetch behavior if not gated — guard calls with conditions like hasNextPage && !isFetching, and trigger them only from real user actions (scroll-end, button click), not arbitrarily.

Reference Tables

Return field (infinite-specific)Meaning
data.pages / data.pageParamsParallel arrays of fetched page data and their params
fetchNextPage(opts) / fetchPreviousPage(opts)Load more; cancelRefetch (default true) controls overlap behavior
hasNextPage / hasPreviousPageWhether getNextPageParam/getPreviousPageParam returned a usable value
isFetchingNextPage / isFetchingPreviousPageCurrently loading more in that direction
isFetchNextPageError / isFetchPreviousPageErrorThat direction's most recent load-more attempt failed

Key Takeaways

  1. getNextPageParam is mandatory (unlike useQuery, which needs nothing infinite-specific) — an infinite query without it won't type-check or run correctly.
  2. Remember isRefetching here is stricter than in useQuery — it's specifically "background refetch, not initial load, not next/previous page load."
  3. Always gate imperative fetchNextPage/fetchPreviousPage calls on hasNextPage/hasPreviousPage and !isFetching to avoid interfering with the library's own refetch scheduling.

Connects To

  • Infinite Queries: the full conceptual guide this chapter is the reference companion to.
  • useQuery: the base option/return shape this hook extends.