Here's an example of creating an infinitely scrollable page in Svelte 5. We'll create a simple component that fetches data from an API and appends it to the list as the user scrolls down.
In this example, we're using the onMount lifecycle method to fetch the initial data when the component mounts. We're also using the window.addEventListener method to listen for scroll events and fetch more data when the user scrolls down.
The fetchItems function fetches data from the API and appends it to the items array. We're using the hasNextPage variable to keep track of whether there are more pages to fetch.
The handleScroll function checks if the user has scrolled down to the bottom of the page and if there are more pages to fetch. If both conditions are true, it calls the fetchItems function to fetch more data.
Note that this is just a basic example, and you may need to modify it to fit your specific use case. You may also want to consider using a library like svelte-infinite-scroll to handle the infinite scrolling logic for you.
Example usage:
svelte
<script>
import InfiniteScroll from './InfiniteScroll.svelte';
</script>
<InfiniteScroll />
This will render the infinitely scrollable page with the data fetched from the API.