Introduction
What is infinite scroll ?
Infinite scrolling is a web-design technique that loads content continuously as the user scrolls down the page, eliminating the need for pagination.
Some sites where you can see usage of infinity scroll are for example: Twitter, 9gag etc...
What are we going to build
I know nothing to fancy looking, but you can improve and style it, so it looks better, this is just a basic example and introducting a concept
Prerequisites
- This tutorial assumes that you have working knowledge of React
- We are going to Use React Hooks
- Before we get started, ensure that you have Node, Yarn, or npm installed in your environment.
- Have a Web browser offcourse
Getting started
npx create-react-app infiniteScroll
Once you have finished creating the project folder you can cd into it, and run it:
cd infiniteScroll
npm start
This will run the app in development mode and you can view it in the browser using the link http://localhost:3000/.
It will look like this:
Component Setup
Create new Infinite scroll component and paste following code inside it:
import React, { useState } from 'react';
// styling post container
const divStyle = {
color: 'blue',
height: '250px',
textAlign: 'center',
padding: '5px 10px',
background: '#eee',
marginTop: '15px'
};
// styling container wrapper
const containerStyle = {
maxWidth: '1280px',
margin: '0 auto',
}
const InfiniteScroll = () => {
// initialize list of posts
const [postList, setPostList] = useState({
list: [1,2,3,4]
});
return (<div className="container" style={containerStyle}>
<div className="post-list">
{
postList.list.map((post, index) => {
return (<div key={index}
className="post"
style={divStyle}>
<h2> {post } </h2>
</div>)
})
}
<div className="loading">
<h2>Load More</h2>
</div>
</div>
</div>)
}
export default InfiniteScroll;
Your page will now look like this:
Adding infinite scroll
For this we would use Interaction Observer API
Intersection Observer is a really awesome JavaScript API that simplifies scroll-based events in JavaScript. Rather than constantly checking the distance from the top, Intersection Observer watches when an element enters or exits the viewport.
We will use interaction Observer to watch when user enters specific point and then load more posts.
- First we will import **useRef* and useEffect hook from React and attach them to Load More div*
- then will register IntersectionObserver on Load More div when component is mounted
- add new state variable page, that will track on what page we currently are. To simulate more real life example how we would do it when connecting with backend
- the last step when page is update, simply just load more posts
Here is a complete code:
import React, { useEffect, useState, useRef } from 'react';
const divStyle = {
color: 'blue',
height: '250px',
textAlign: 'center',
padding: '5px 10px',
background: '#eee',
marginTop: '15px'
};
const containerStyle = {
maxWidth: '1280px',
margin: '0 auto',
}
const InfiniteScroll = () => {
const [postList, setPostList] = useState({
list: [1,2,3,4]
});
// tracking on which page we currently are
const [page, setPage] = useState(1);
// add loader refrence
const loader = useRef(null);
useEffect(() => {
var options = {
root: null,
rootMargin: "20px",
threshold: 1.0
};
// initialize IntersectionObserver
// and attaching to Load More div
const observer = new IntersectionObserver(handleObserver, options);
if (loader.current) {
observer.observe(loader.current)
}
}, []);
useEffect(() => {
// here we simulate adding new posts to List
const newList = postList.list.concat([1,1,1,1]);
setPostList({
list: newList
})
}, [page])
// here we handle what happens when user scrolls to Load More div
// in this case we just update page variable
const handleObserver = (entities) => {
const target = entities[0];
if (target.isIntersecting) {
setPage((page) => page + 1)
}
}
return (<div className="container" style={containerStyle}>
<div className="post-list">
{
postList.list.map((post, index) => {
return (<div key={index} className="post" style={divStyle}>
<h2> {post } </h2>
</div>)
})
}
<!-- Add Ref to Load More div -->
<div className="loading" ref={loader}>
<h2>Load More</h2>
</div>
</div>
</div>)
}
export default InfiniteScroll;
This is my first post on Dev.to Thank you for reading :)
If you liked this post, you can find more by:
Following me on Twitter:
Top comments (20)
Here is a cleaner version of the code above that compliance with Eslint.
There is a problem with the code . It'll add infinite number of elements without being triggered by scrolling to the end of the list.
Be carefull... that
postList.list
as dependencies will make your component enter an infinite loop of rerenders. It is better to access this property via an arrow function.Also,
handleObserver
is better to be insde the useEffect function and you should clear (observer.unobserve) the listener on a callback funtion. ;)Nice, Kakarot. I will for sure try it out ✨
Thank you for reading, and yes please go try it out :)
You wrote
ContactPage
function and exportInfiniteScroll
! Should review the code and fixhaha yes, I think he has tried to copy-paste and forget to change the name of function 😁
Updated thank you, Yup you are right left over while copying from VS code :D
How would I turn off this observer when certain conditions are met, for example if some state changes (ie, I make it to the last set of items/last page)? I've tried observer.disconnect inside the useEffect hook, but it does not seem to work.
Thanks good article I will try this out.
Reading y is unnecessary in handleObserver.
Yes you are right
Thank you!
Nice article! But please add the javascript tag to the code-blocks for syntax highlights
why dont you have syntax highlighting?
Thank you very much, brother