Skip to main content
Infinite queries are perfect for implementing infinite scrolling, “load more” buttons, and other patterns where you need to fetch data in pages or chunks.

Basic Usage

Use useInfiniteQuery (React) or createInfiniteQuery (Solid/Vue) to implement infinite data loading:

Required Options

initialPageParam

The initial page param to use when fetching the first page:

getNextPageParam

Function that receives the last page and all pages/pageParams, and returns the next page param:
Return undefined when there are no more pages to indicate the end of data.

getPreviousPageParam (Optional)

Function to determine the previous page param for bi-directional infinite queries:

Data Structure

The data object has a specific structure for infinite queries:
Example:

Fetching Pages

fetchNextPage

Fetch the next page of data:

fetchPreviousPage

Fetch the previous page (for bi-directional scrolling):

Limiting Pages with maxPages

Limit the number of pages stored in memory to optimize performance:
When maxPages is set, older pages will be removed from the cache as new pages are fetched. When fetching in the forward direction, the oldest pages are removed first. When fetching backward, the newest pages are removed.

Refetching Pages

By default, refetching an infinite query will refetch all pages. You can customize this:

Bi-directional Infinite Lists

Example of a chat-like interface with both directions:

Status Flags

Infinite queries provide specialized status flags:
  • isFetchingNextPage: true while fetching the next page
  • isFetchingPreviousPage: true while fetching the previous page
  • isFetchNextPageError: true if fetching next page failed
  • isFetchPreviousPageError: true if fetching previous page failed
  • hasNextPage: true if there is a next page to fetch
  • hasPreviousPage: true if there is a previous page to fetch

Cursor vs Offset Pagination

Offset-based

Cursor-based pagination is generally more reliable for real-time data since it’s not affected by insertions/deletions in the dataset.

Flattening Pages

If you need a flat array of items instead of pages:

Next Steps