# Infinite Scroll

## What is Infinite Scroll

The Infinite Scroll component calls an action to be performed when the user scrolls a specified distance from the bottom or top of the page. This is what you see when you quickly scroll Facebook or Instagram feed, and the page loads new posts, while you see a loader spinning on the bottom of the screen

This is useful when you have huge amount of data, but you only fetch a small chunk at a time for a faster app experience, or saving data for users.

![](https://3608702775-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LsQWILyBp_9nWmIJ7TG%2F-LubTLqzGbCYwp0EQus9%2F-LubTiaY9W9bhQcn7iK0%2Fassets_-La-kqVz2Uq1Ka0VUWZT_-LakSMU813nkhUH2Rw3o_-LakW-uETercKpcaMnnL_Screen%20Shot%202019-03-24%20at%209.52.31%20PM.png?alt=media\&token=09a3c56f-7d31-49ed-81e1-1c53e7755b7e)

### Infinite Scroll in Ionic 4 Full App

The infinite scroll is located in `src/app/pages/addons/infinite`&#x20;

To use this functionality, you put the `ion-infinite-scroll` at the bottom of `ion-content`in your page

```
  <ion-infinite-scroll #infiniteScroll (ionInfinite)="loadData($event)">
    <ion-infinite-scroll-content loadingSpinner="bubbles" loadingText="Loading more data...">
    </ion-infinite-scroll-content>
  </ion-infinite-scroll>
```

`ion-infinite-scroll-content` contains the elements which will show when the page is loading new content. You can put text, images or GIFs here as per your app design.&#x20;

### Importing&#x20;

You can include infinite scroll functionality by importing it in your `page.ts`

```
import { IonInfiniteScroll } from '@ionic/angular';
```

and&#x20;

```
  @ViewChild(IonInfiniteScroll) infiniteScroll: IonInfiniteScroll;
```

### Loading more data

`(ionInfinite)="loadData($event)"` is basically an `ionInfinite` listener implemented, which fires on a scroll event when you reach the bottom of the page on scrolling. It then calls a function `loadData` (or any function you want), and you can implement the proper logic to fetch more data.&#x20;

In Full App, we fetch more data from an API, with 5000ms delay

```
this.util.infiniteData().subscribe((data:Array<any>) => {
      data.forEach(element => {
        this.loadedData.unshift(element)
      });
      event.target.complete();

      // App logic to determine if all data is loaded
      // and disable the infinite scroll
      if (this.loadedData.length > 30) {
        this.util.presentToast('No more data available', true, 'bottom', 1500);
        event.target.disabled = true;
      }
    })
```

`event.target.complete();` tells the app that the infinite scroll action (loading more data) is completed, and the loader can be hidden.&#x20;

You should disable the Infinite Scroll functionality once all the data has been fetched. This will require a custom logic on your end. For demo purpose, we stop fetching more data when the list item count exceeds 30.

![](https://3608702775-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LsQWILyBp_9nWmIJ7TG%2F-LubTLqzGbCYwp0EQus9%2F-LubTlMgEIWDbQUPxg9p%2Fassets_-La-kqVz2Uq1Ka0VUWZT_-LakSMU813nkhUH2Rw3o_-LakYOqEKocLCo5jUjK2_Screen%20Shot%202019-03-24%20at%2010.03.33%20PM.png?alt=media\&token=4dbf6ab1-b18c-41cf-87ff-075c0abdfa22)
