examples/react/load-more-infinite-scroll-nextjs-pages-router
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack React Query Load-More Infinite Scroll Example (Next.js Pages Router)#
An infinite list that loads the next page when a "Load more" button is clicked or when the end of the list scrolls into view.
Built with Next.js (Pages Router).
How it works#
src/services/projects/queries.tsdefinesprojectsQueries.infinite(), aninfiniteQueryOptionsfactory keyed by['projects', 'infinite']withinitialPageParam: 0, andgetPreviousPageParamandgetNextPageParamreadingpreviousIdandnextIdfrom the page.src/pages/index.tsxpasses it touseInfiniteQuery. The "Load Newer" button has areffromuseInView, and an effect callsfetchNextPagewhen the button scrolls into view whilehasNextPageistrue. "Load Older" callsfetchPreviousPage.src/pages/about.tsxis a second page to navigate to and back from, so the cached pages are shown again on return.src/pages/api/projects.tsis a Next.js API route that returns 5 projects per page for acursorbetween -10 and 10, withnextIdandpreviousId, after a 1-second delay.
To run this example:
npm installnpm run dev