Infinite query

useInfiniteQuery keeps a list of pages for a cursor API. initialPageParam is the first cursor, getNextPageParam reads the next one from the last page and returns undefined to stop, and fetchNextPage loads one more page.

Minimal example

ts
useInfiniteQuery({
  queryKey: ["orders"],
  initialPageParam: "",
  queryFn: ({ pageParam }) => fetchOrders(api, pageParam),
  getNextPageParam: page => page.next_cursor || undefined,
});

Learn this concept

Lesson 4.8 · TanStack Query caching

Used in exercises

Exercise 4.10 · Restaurant list screen Exercise 4.17 · Orders tab and order detail screens
Prerequisite conceptsQuery key
RelatedFlatList