Recently I've been using Intersection Observers in React (16.4+) as follows:
const myRef = React.useRef(null);
React.useLayoutEffect(() => {
const observer = new IntersectionObserver(
doWork,
{ rootMargin: "0px 0px 0px 0px" }
);
observer.observe(myRef.current)
}, [])
Generally in React we're told to always return a cleanup function from our effect hooks if those hooks are setting up functions like window.addEventListener
:
return () => window.removeEventListener('event', cb); // more cleanup
Should I be unobserve
'ing myRef.current
in a similar way? Or is that not strictly required in the same way it is for window event listeners?
I haven't found anything in the w3c spec regarding this, I've also been performance profiling my components and I haven't seen any negative results of not explicitly observing either.
Is there any consensus on this?