Create a variable to keep track of the current page number
index.ts
let currentPage: number = 1;
29 chars
2 lines
Add an event listener to the window that checks if the user has scrolled to the bottom of the page
index.ts
window.addEventListener('scroll', () => {
if (window.innerHeight + window.scrollY >= document.body.offsetHeight) {
// user has scrolled to the bottom of the page }
});
176 chars
6 lines
Inside the event listener, increment the currentPage variable and make an API call to fetch the next set of data
index.ts
window.addEventListener('scroll', () => {
if (window.innerHeight + window.scrollY >= document.body.offsetHeight) {
currentPage++;
fetchData();
}
});
functionfetchData() {
// make an API call to fetch data for the current page}
244 chars
11 lines
Append the fetched data to the current list of items
index.ts
functionfetchData() {
// make an API call to fetch data for the current page fetch(`https://exampleapi.com/data?page=${currentPage}`)
.then(response => response.json())
.then(data => {
// append the new data to the current list of items items.push(...data);
});
}
293 chars
10 lines
Update the view to display the new items
index.ts
functionfetchData() {
// make an API call to fetch data for the current page fetch(`https://exampleapi.com/data?page=${currentPage}`)
.then(response => response.json())
.then(data => {
// append the new data to the current list of items items.push(...data);
// update the view to display the new items renderItems();
});
}
functionrenderItems() {
// update the view to display the current list of items}