examples/preact/load-more-infinite-scroll-preact-iso
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Preact Query Load-More Infinite Scroll Example (preact-iso)#
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 preact-iso.
How it works#
src/services/projects/queries.tsdefinesprojectsQueries.infinite(), aninfiniteQueryOptionsfactory withinitialPageParam: 0andgetNextPageParamandgetPreviousPageParamthat read the page'snextIdandpreviousId.src/Example.tsxpasses it touseInfiniteQuery. AnIntersectionObserverwatches the "Load Newer" button and callsfetchNextPagewhen it scrolls into view whilehasNextPageistrue, and the buttons callfetchNextPageandfetchPreviousPagedirectly.src/App.tsxsets uppreact-isoroutes for/and/about. Going to the about page and back renders the loaded pages from the cache.src/mocks.tsis an MSW handler for/api/projectsthat returns 5 projects per cursor after a 1-second delay.nextIdisnullonce the cursor reaches 10, andpreviousIdisnullonce it reaches -10.
To run this example:
npm installnpm run dev