# ion-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. The expression assigned to the `ionInfinite` event is called when the user reaches that defined distance. When this expression has finished any and all tasks, it should call the `complete()` method on the infinite scroll instance. ## Infinite Scroll Content The `ion-infinite-scroll` component has the infinite scroll logic. It requires a child component in order to display content. Ionic uses its `ion-infinite-scroll-content` component by default. This component displays the infinite scroll and changes the look depending on the infinite scroll's state. It displays a spinner that looks best based on the platform the user is on. However, the default spinner can be changed and text can be added by setting properties on the `ion-infinite-scroll-content` component. ## Custom Content Separating the `ion-infinite-scroll` and `ion-infinite-scroll-content` components allows developers to create their own content components, if desired. This content can contain anything, from an SVG element to elements with unique CSS animations. ## Usage with Virtual Scroll Infinite scroll requires a scroll container to function. When using a virtual scrolling solution, you will need to disable scrolling on the `ion-content` and indicate which element container is responsible for the scroll container with the `.ion-content-scroll-host` class target. ```html ``` ## Interfaces ### InfiniteScrollCustomEvent While not required, this interface can be used in place of the `CustomEvent` interface for stronger typing with Ionic events emitted from this component. ```typescript interface InfiniteScrollCustomEvent extends CustomEvent { target: HTMLIonInfiniteScrollElement; } ``` ## Usage ### Angular ```html Toggle Infinite Scroll ``` ```typescript import { Component, ViewChild } from '@angular/core'; import { IonInfiniteScroll } from '@ionic/angular'; @Component({ selector: 'infinite-scroll-example', templateUrl: 'infinite-scroll-example.html', styleUrls: ['./infinite-scroll-example.css'] }) export class InfiniteScrollExample { @ViewChild(IonInfiniteScroll) infiniteScroll: IonInfiniteScroll; constructor() {} loadData(event) { setTimeout(() => { console.log('Done'); event.target.complete(); // App logic to determine if all data is loaded // and disable the infinite scroll if (data.length === 1000) { event.target.disabled = true; } }, 500); } toggleInfiniteScroll() { this.infiniteScroll.disabled = !this.infiniteScroll.disabled; } } ``` ### Javascript ```html Toggle Infinite Scroll ``` ```javascript const infiniteScroll = document.getElementById('infinite-scroll'); infiniteScroll.addEventListener('ionInfinite', function(event) { setTimeout(function() { console.log('Done'); event.target.complete(); // App logic to determine if all data is loaded // and disable the infinite scroll if (data.length === 1000) { event.target.disabled = true; } }, 500); }); function toggleInfiniteScroll() { infiniteScroll.disabled = !infiniteScroll.disabled; } ``` ### React ```tsx import { IonButton, IonContent, IonHeader, IonInfiniteScroll, IonInfiniteScrollContent, IonItem, IonLabel, IonList, IonPage, IonTitle, IonToolbar, useIonViewWillEnter } from '@ionic/react'; import { useState } from 'react'; const InfiniteScrollExample: React.FC = () => { const [data, setData] = useState([]); const [isInfiniteDisabled, setInfiniteDisabled] = useState(false); const pushData = () => { const max = data.length + 20; const min = max - 20; const newData = []; for (let i = min; i < max; i++) { newData.push('Item' + i); } setData([ ...data, ...newData ]); } const loadData = (ev: any) => { setTimeout(() => { pushData(); console.log('Loaded data'); ev.target.complete(); if (data.length === 1000) { setInfiniteDisabled(true); } }, 500); } useIonViewWillEnter(() => { pushData(); }); return ( Blank Blank setInfiniteDisabled(!isInfiniteDisabled)} expand="block"> Toggle Infinite Scroll {data.map((item, index) => { return ( {item} ) })} ); }; export default InfiniteScrollExample; ``` ### Stencil ```tsx import { Component, State, h } from '@stencil/core'; @Component({ tag: 'infinite-scroll-example', styleUrl: 'infinite-scroll-example.css' }) export class InfiniteScrollExample { private infiniteScroll: HTMLIonInfiniteScrollElement; @State() data = []; componentWillLoad() { this.pushData(); } pushData() { const max = this.data.length + 20; const min = max - 20; for (var i = min; i < max; i++) { this.data.push('Item ' + i); } // Stencil does not re-render when pushing to an array // so create a new copy of the array // https://stenciljs.com/docs/reactive-data#handling-arrays-and-objects this.data = [ ...this.data ]; } loadData(ev) { setTimeout(() => { this.pushData(); console.log('Loaded data'); ev.target.complete(); // App logic to determine if all data is loaded // and disable the infinite scroll if (this.data.length === 1000) { ev.target.disabled = true; } }, 500); } toggleInfiniteScroll() { this.infiniteScroll.disabled = !this.infiniteScroll.disabled; } render() { return [ this.toggleInfiniteScroll()} expand="block"> Toggle Infinite Scroll {this.data.map(item => {item} )} this.infiniteScroll = el} onIonInfinite={(ev) => this.loadData(ev)}> ]; } } ``` ### Vue ```html ``` ## Properties | Property | Attribute | Description | Type | Default | | ----------- | ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------- | ---------- | | `disabled` | `disabled` | If `true`, the infinite scroll will be hidden and scroll event listeners will be removed. Set this to true to disable the infinite scroll from actively trying to receive new data while scrolling. This is useful when it is known that there is no more data that can be added, and the infinite scroll is no longer needed. | `boolean` | `false` | | `position` | `position` | The position of the infinite scroll element. The value can be either `top` or `bottom`. | `"bottom" \| "top"` | `'bottom'` | | `threshold` | `threshold` | The threshold distance from the bottom of the content to call the `infinite` output event when scrolled. The threshold value can be either a percent, or in pixels. For example, use the value of `10%` for the `infinite` output event to get called when the user has scrolled 10% from the bottom of the page. Use the value `100px` when the scroll is within 100 pixels from the bottom of the page. | `string` | `'15%'` | ## Events | Event | Description | Type | | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------- | | `ionInfinite` | Emitted when the scroll reaches the threshold distance. From within your infinite handler, you must call the infinite scroll's `complete()` method when your async operation has completed. | `CustomEvent` | ## Methods ### `complete() => Promise` Call `complete()` within the `ionInfinite` output event handler when your async operation has completed. For example, the `loading` state is while the app is performing an asynchronous operation, such as receiving more data from an AJAX request to add more items to a data list. Once the data has been received and UI updated, you then call this method to signify that the loading has completed. This method will change the infinite scroll's state from `loading` to `enabled`. #### Returns Type: `Promise` ---------------------------------------------- *Built with [StencilJS](https://stenciljs.com/)*