examples/react/pagination-nextjs-pages-router
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack React Query Pagination Example (Next.js Pages Router)#
Paginated projects with placeholderData: keepPreviousData, so the current page stays visible while the next one loads. The next page is prefetched.
Built with Next.js (Pages Router).
How it works#
src/services/projects/queries.tsdefinesprojectsQueries.list(page), aqueryOptionsfactory keyed by['projects', 'list', page]with a 5-secondstaleTime.src/pages/index.tsxpasses it touseQuerywithplaceholderData: keepPreviousData. While the next page loads,datastill holds the previous page andisPlaceholderDataistrue, which disables the "Next Page" button.- When a page arrives with
hasMore: true, the next page is fetched ahead of time withqueryClient.query(projectsQueries.list(page + 1)). src/pages/api/projects.tsis a Next.js API route that returns 10 projects per page after a 1-second delay.
To run this example:
npm installnpm run dev