DEV Community

Cover image for Futuristic Infinite Scrolling in React and JavaScript

Futuristic Infinite Scrolling in React and JavaScript

Dhairya Nadapara on September 23, 2021

There are multiple use cases in the modern UX when we are required to use infinite scrolling. Previously, devs are using the height of the viewport...
Collapse
 
joolucas_silveirasilva profile image
João Lucas Silveira Silva

We are in 2021, please stop writing class components

Collapse
 
ortonomy profile image
🅖🅡🅔🅖🅞🅡🅨 🅞🅡🅣🅞🅝

Lol, right? Doesn’t really lend weight to the hyperbolic title of ‘futuristic’ now, does it?

Collapse
 
vndre profile image
Andre

this.state = {
arr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
};

My heart omg 😱

Also the IntersectObserver API was introduced on chrome since 2016...

Collapse
 
dhairyanadapara profile image
Dhairya Nadapara • Edited

Agree with you @vndre it been there for a long time. But people still doesn't know about this. I have searched for infinite scrolling react component on npm and haven't found any which is community maintained. Thanks for suggestion, I will try to improve quality of my sample code to be more realistic with api call. Thanks again :)

Collapse
 
vndre profile image
Andre

What about github.com/thebuilder/react-inters... ?

I believe a hook approach would make things easier.

Thread Thread
 
dhairyanadapara profile image
Dhairya Nadapara

Yes, I have seen the react-infinite-intersection but, I was confused because they have very few download on npm.

There are two reasons why I'm more inclined towards the class components:

  1. I'm more inclined towards the object oriented programming. So, I get more comfortable with class components and they are easy to understand for me
  2. Some times before I have to add some feature in a functional components and it was developed by someone in organization. At the beginning there was no issue but as the component complexity increases the issues were getting worse. It's easy to write but it's easy to make mistakes too.

For creating small components like UI framework with independent components or stateless components, I use functional components but for components which are going be complex with lots of features, I prefer class components.

Thanks :)

Collapse
 
vetras profile image
vetras

The div id="loading" never leaves the page. It is just pushed further down.

This is weird ... but OK I guess the main point is using the
IntersectionObserver and looking at the value of the intersectionRatio to make decisions.

That same principle can be applied to other things as you mention at the top. Cool.

Collapse
 
dhairyanadapara profile image
Dhairya Nadapara • Edited

Yes @vetras , usually web-app's shows the loading at bottom of the div in case of infinite scrolling. Other way I thought of was set target to last element of the list, but in that case we have to change the observer target when list updates. If you know any other way please share, it will be helpful. Thanks :)

Collapse
 
vetras profile image
vetras

What I usually see when we have DOM elements we want to show or hide dynamically is to have a display property on them (not just shove them outside the view 🙏 ).

For example:

  • on vue.js:
<p v-if=isLoading > loading... please wait </p>
Enter fullscreen mode Exit fullscreen mode
  • on react:
render() {
  return this.isLoading && (<p>loading ... please wait</p>);
Enter fullscreen mode Exit fullscreen mode

(full stack dev here, FYI, so my FE well...)

Thread Thread
 
dhairyanadapara profile image
Dhairya Nadapara

Oh yes, I completely missed this. Let me think of this how it will be achieved as target element is required to pass the in observe method.

Collapse
 
rexgalilae profile image
RexGalilae

This article doesn't make any attempts to be easy to understand.

It uses class components to try doing that even if it achieves the opposite and the explanations are very lackluster at best.

Just a glorified code dump

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
iskurbanov profile image
iskurbanov

Don't have anything nice to say? Don't say it :)