Skip to main content
Infinite queries are useful for implementing “load more” and infinite scrolling features. They allow you to fetch pages of data progressively as the user scrolls or clicks a button.

Basic Usage

Use useInfiniteQuery to fetch paginated data. The hook manages pages of data and provides methods to fetch more:

Required Options

1

initialPageParam

The initial page parameter passed to the query function. This is required and determines the starting point for your infinite query.
2

getNextPageParam

Function that receives the last page and returns the next page parameter. Return undefined or null when there are no more pages.

Bidirectional Infinite Queries

Fetch data in both directions by implementing both next and previous page parameters:
Both getPreviousPageParam and getNextPageParam receive the full list of pages and page parameters as additional arguments, allowing you to calculate the next parameter based on all loaded data.

Infinite Scroll Implementation

Combine useInfiniteQuery with the Intersection Observer API for automatic infinite scrolling:
Attach the ref to a sentinel element (like a button or div) at the bottom of your list. When it becomes visible, inView will be true and trigger the next page fetch.

Limiting Maximum Pages

Control memory usage by limiting the number of cached pages:
When using maxPages, older pages will be removed from the cache as new pages are fetched. This helps prevent memory issues with very long lists.

Data Structure

The data object contains two properties:
  • pages: An array of page data objects
  • pageParams: An array of page parameters used to fetch each page

Query Function Context

The query function receives a context object with the page parameter:

Refetching Pages

By default, refetching an infinite query will refetch all pages. You can customize this:
The refetchPage function receives the page data and index, allowing you to selectively refetch specific pages.