examples/vue/load-more-infinite-scroll-nuxt4
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Vue Query Load-More Infinite Scroll Example (Nuxt 4)#
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 Nuxt 4.
How it works#
app/services/projects/queries.tsdefinesprojectsQueries.infinite(), aninfiniteQueryOptionsfactory withinitialPageParam: 0andgetPreviousPageParamandgetNextPageParamreading the page'spreviousIdandnextId.app/pages/index.vuereads it withuseInfiniteQueryand renders every page indata.pages. The "Load Newer" button callsfetchNextPage, and "Load Older" callsfetchPreviousPage.- In
app/pages/index.vue,useElementVisibilityfrom@vueuse/corewatches the "Load Newer" button, and awatchEffectcallsfetchNextPagewhen the button is visible,hasNextPageistrueand no next page is being fetched. app/plugins/vue-query.tsis a Nuxt plugin that creates aQueryClientand installsVueQueryPlugin. On the server it storesdehydrate(queryClient)inuseState('vue-query')onapp:rendered, and on the client it callshydratewith that state onapp:created.server/api/projects.tsis a Nitro handler that returns 5 projects per cursor after 1 second, withnextIdandpreviousIdset tonullpast cursor 10 and -10.
To run this example:
npm installnpm run dev