DEV Community

Cover image for Infinite scrolling in React with intersection observer

Infinite scrolling in React with intersection observer

Yogini Bende on September 02, 2021

Hello folks, Few days back I came across the use-case of infinite scroll in React. For this, I used Intersection Observer and found different way...
Collapse
 
udittakkar profile image
Udit Takkar

Why have to used this ref={setLastElement} ? isn't it supposed to be ref={observer} ?

Collapse
 
rakshit profile image
Rakshit

Read about callback refs for this.

Collapse
 
jai_type profile image
Jai Sandhu

This is great, do you have a solution for infinite scroll + virtualisation? That's the holy grail I haven't been able to implement well

Collapse
 
galelmalah profile image
Gal Elmalah • Edited

A lot of React libs doing that out there Jai Sandhu

Collapse
 
jai_type profile image
Jai Sandhu

Haven't found a good one that caters for different height cells and infinite scrolling

Thread Thread
 
globalroo profile image
Andy

Hi Jai. This solution has an interesting take on different height cells in a virtualised list. dev.to/miketalbot/react-virtual-wi....

Thread Thread
 
jai_type profile image
Jai Sandhu

Oooh wow thanks that looks awesome!

Collapse
 
akshay1502 profile image
Akshay Shinde

Your arcticle helped a lot. Thanks for indetailed explanation. I have some suggestion.
There are 2 ways:

  1. either add observer for the last element
  2. add a empty div below your last ele, and set observer on it.

with 1st we need calculate the last ele, with 2nd it becomes much cleaner and simpler.
Adding YT link for those who want video explanation.
https://www.youtube.com/watch?v=8kLOvs1prEM&t=3s

Collapse
 
fires3as0n profile image
fires3as0n

Why do you use

const observer = useRef(...);

to store a reference to an observer?

useRef(value)

is nothing but

useMemo(() =>({current: value}), []);

It is a convenience hook often used to store auto-updated refs to an elements (with its return value passed as a ref prop), or in rare cases when we need to update persistent state without causing component to re-render (by manually updating .current value)

Here you are just storing the reference, so it is more intuitive and less confusing to use

const observer = useMemo(() => new Observer(...), [])

instead.

Also since you are still using ref but update it manually using "callback ref", the presence of useRef() in the code makes it harder to understand especially to a novice.

Collapse
 
nguyencqchi profile image
Chi Nguyen

This is great. Do you have any solution for combining infinite scroll and scroll into view? I'm trying to implement into my project, which has a navigation that I can click to go to a certain section and update the active section on my navigation, and also update my navigation whenever I scroll to a section

Collapse
 
willaiem profile image
Damian Żygadło

for this hint you can make me louda

Collapse
 
mathrubootamfan profile image
C

the page Number is incrementing even when the already intersected element comes down. how can we resolve this issue?

Collapse
 
amantiwari1 profile image
Aman Tiwari

use this library to save time :)
react-query.tanstack.com/reference...