Infinite scroll techniques in React
Infinite scrolling is a web design technique that loads content continuously as the user scrolls down the page, eliminating the need for pagination.
Content is often loaded asynchronously by making a request to the server. Often times this can improve the user experience on a website.
But not always. Sometimes it’s terrible.
Infinite scrolling technically requires adding a
scroll event listener to the window object or a certain div. This determines when the scroll has reached the bottom of the div and then performs actions accordingly.
In this tutorial, I will explain two methods of implementing infinite scroll in React:
- The first method describes implementing everything from the ground up
- The second method uses an already available infinite scroll library/component
A basic understanding of React is needed to follow through this tutorial.
Implementing from the ground up
As mentioned earlier, infinite scroll is about attaching event listeners to DOM elements while watching for when the scrollbar hits the bottom of the div.
Look at the render function of this Component below:
Say you want to load more
li items into the
ul tag each time the div with the class
App gets to the end of the div, how do you tackle this problem?
First, notice that there is a reference to the div called
myscroll which makes it possible to access the element in React using
Declare an initial state in the constructor:
Here, you declared two states:
loading. The items hold the number of items available to be shown as
li tags while the loading state will show when the infinite loader is fetching more items.
Next, create a function that renders all items:
This function loops through the number of items present and create a
li tag showing the item number.
Now, update your render function to display these items:
What you have is a normal component that shows a couple
li's showing the items number. How do you add infinite scroll to this component? remember the
myscroll? You get to use it now. Define the
In the method above, a scroll listener was added to the
myscroll ref which references the div being targeted. Here you used the
scrollTop property of the element to get the scroll position (which is relative to the top of the window) and then added it to the
clientHeight property (the height of the document).
Next, a check is made to see if the sum of those two properties is greater than or equal to the height of the scrollbar. If the assumption is true, then the bottom of the div has been reached. A new function called
loadMore (Which will be created next) is then fired.
Here is what the
loadMore function looks like:
In this method, the
loading is first set to
true, so the loading div is shown. Next, a timeout function is called, after which the items are increased by
20 and the
loading state is set back to false.
The reason for the timeout, however, is to cause a little delay. In your application, you probably want to make a
axios call to your server and then change state.
But no matter your use case, the concept remains the same.
Here is what the final component should look like:
Using an infinite scroll library
While the first method showed how relatively easy it is to implement infinite scroll in React applications, you might not be so content to implement event listeners yourself.
You might also just want a solution that works without all the fuss because you’re kind of lazy (or, more likely, pressed for time).
That’s okay, I’ve got you covered.
For this purpose, you can use
React-infinite-scroller , available here. Here’s an example of how it can be used:
Looking at the code above, notice it looks similar to the previous method? What are the main differences of this code from the one in the previous method?
- Here, there is no event listener being attached
- Here, no reference to the div was made, as it wasn’t needed
- No loading state was defined. Instead, there is
hasMoreItemswhich is used to tell the Infinite scroll component to detach the event listener
- An alteration to the
loadMorefunction, which sets the
hasMoreItemsstate to false once the items his 200
And there you have it, two different methods that allow you to implement infinite scroll in React applications.
For those of you who want to get involved in writing the event listeners yourself, you have seen how simple and easy it is.
For those of you who do not want to get involved in attaching the event listeners, there’s a method that will work for you too.
Have any comments or observations? That’s what the comment section is for.
Plug: LogRocket, a DVR for web apps
LogRocket is a frontend logging tool that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.