Infinite Scroll in React

From the TanStack Query cheat sheet ยท Infinite Queries ยท verified Jul 2026

Infinite Scroll

Loading data progressively with useInfiniteQuery

typescript
import { useInfiniteQuery } from '@tanstack/react-query'

function InfiniteList() {
  const {
    data,
    fetchNextPage,
    hasNextPage,
    isFetchingNextPage,
    isLoading,
    isError
  } = useInfiniteQuery({
    queryKey: ['posts'],
    queryFn: ({ pageParam }) => fetchPosts(pageParam),
    initialPageParam: 0,
    getNextPageParam: (lastPage, pages) => lastPage.nextCursor,
  })

  if (isLoading) return 'Loading...'
  if (isError) return 'Error!'

  return (
    <div>
      {data.pages.map((page, i) => (
        <div key={i}>
          {page.posts.map(post => (
            <div key={post.id}>{post.title}</div>
          ))}
        </div>
      ))}
      <button
        onClick={() => fetchNextPage()}
        disabled={!hasNextPage || isFetchingNextPage}
      >
        {isFetchingNextPage
          ? 'Loading more...'
          : hasNextPage
          ? 'Load More'
          : 'Nothing more to load'}
      </button>
    </div>
  )
}
๐Ÿ’ก Use intersection observer for automatic loading
โšก getNextPageParam determines pagination logic
๐Ÿ“Œ pages array contains all loaded pages in order
๐ŸŸข Provide manual load button as fallback
infinitepaginationscroll
Back to the full TanStack Query cheat sheet