ReactJS application with a sequence of datacards.
Each datacard has a video that plays in it as well as some other text data.
There are a few hundred of these. Performance is slow... The videos don't play smooth, CPU use skyrockets, the network use goes crazy because we are now downloading 300 videos simultaneously, etc. ... but only about 12 or so are on the screen at any given point in time.
I would like to only load and play the video when the dataccard is on the screen. (or, ideally, when it's 'near' the screen)
So, ever-so-wonderful stackoverflow provided me with this hook: React - check if element is visible in DOM
It is supposed to check if the element is visible .. and then let me react based on that. (ie: I can drop the video, etc)
Unfortunately, when I implement the code as written in that answer:
export function useOnScreen(ref) {
const [isIntersecting, setIntersecting] = useState(false)
const observer = new IntersectionObserver(
([entry]) => setIntersecting(entry.isIntersecting)
)
useEffect(() => {
observer.observe(ref.current)
// Remove the observer as soon as the component is unmounted
return () => { observer.disconnect() }
}, [])
return isIntersecting
}
and implement the hook in this way:
const ref = useRef()
const isVisible = useOnScreen(ref);
....
return <div ref={ref}>
{isVisible ? "I'm on the screen" : "I'm not"}
// actual content of datacard here
</div>
... performance slows to an absolute crawl. I suspect that the process of checking if a datacard is on the screen is taxing.
In the referenced answer, I see someone suggest memo-izing this hook. I'm unsure how to do that and came here for advice.
Alternately, if there is a better way to tell if a react component is on (or near) the screen, that would be great.