This content originally appeared on DEV Community and was authored by Nelson Adonis Hernandez
For this example we will use the Intersection Observer API
Component in svelte
<script>
import axios from "axios";
import { infiniteScroll } from "../../functions/infiniteScroll";
let pageNumber = 0;
let characters = [];
let Loading = false;
const fetchData = async () => {
try {
Loading = true;
const response = await axios({
method: "GET",
url: "https://rickandmortyapi.com/api/character/",
params: { page: pageNumber },
});
// SAVE NEW RESULTS
characters = [
...new Set([...characters, ...response.data.results.map((b) => b)]),
];
Loading = false;
} catch (error) {
console.log(error);
}
};
const load = () => {
setTimeout(() => {
pageNumber = pageNumber + 1;
fetchData();
}, 300); // WE WAITED A FEW SECONDS
};
// REACTIVE DECLARATIONS
let elementRef = null;
$: {
if (elementRef) {
infiniteScroll({ fetch: load, element: elementRef });
}
}
</script>
<div class="box-shawdow">
{#each characters as character}
<div class="grid-games">
<div class="box-game">
{character.name}
</div>
</div>
{/each}
{#if Loading}
<h1>Cargando...</h1>
{/if}
<!-- ELEMENT OBSERVER -->
{#if Loading === false}
<li bind:this={elementRef}>Cargando</li>
{/if}
</div>
Function of Intersection Observer
export const infiniteScroll = ({ fetch, element }) => {
if (element) {
const observer = new IntersectionObserver(
(entries) => {
const first = entries[0];
if (first.isIntersecting) {
console.log("Is Intersecting");
fetch();
}
},
{ threshold: 1 }
);
observer.observe(element);//Element of DOM
}
};
This content originally appeared on DEV Community and was authored by Nelson Adonis Hernandez
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.

APA
MLA
Nelson Adonis Hernandez | Sciencx (2021-09-07T18:14:27+00:00) How to create Infinite Scroll with Svelte. Retrieved from https://www.scien.cx/2021/09/07/how-to-create-infinite-scroll-with-svelte/
" » How to create Infinite Scroll with Svelte." Nelson Adonis Hernandez | Sciencx - Tuesday September 7, 2021, https://www.scien.cx/2021/09/07/how-to-create-infinite-scroll-with-svelte/
HARVARDNelson Adonis Hernandez | Sciencx Tuesday September 7, 2021 » How to create Infinite Scroll with Svelte., viewed ,<https://www.scien.cx/2021/09/07/how-to-create-infinite-scroll-with-svelte/>
VANCOUVERNelson Adonis Hernandez | Sciencx - » How to create Infinite Scroll with Svelte. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/07/how-to-create-infinite-scroll-with-svelte/
CHICAGO" » How to create Infinite Scroll with Svelte." Nelson Adonis Hernandez | Sciencx - Accessed . https://www.scien.cx/2021/09/07/how-to-create-infinite-scroll-with-svelte/
IEEE" » How to create Infinite Scroll with Svelte." Nelson Adonis Hernandez | Sciencx [Online]. Available: https://www.scien.cx/2021/09/07/how-to-create-infinite-scroll-with-svelte/. [Accessed: ]
rf:citation » How to create Infinite Scroll with Svelte | Nelson Adonis Hernandez | Sciencx | https://www.scien.cx/2021/09/07/how-to-create-infinite-scroll-with-svelte/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.