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