I got a list based on I render list items I want to track which elements overflows container (especially index of first elements that overflew) in order to do this I wanted to use react-intersection-observer library.
My problem:
const List = ({data}) => {
const { ref, inView, entry } = useInView({
threshold: 0,
root: ????
});
return (
<div>{data.map(i=><ListItem />)</div>
)
}
My solution which does not work.
const wrapperRef = useRef(null);
const { ref, inView, entry } = useInView({
threshold: 0,
root: wrapperRef
});
return (
<div ref={wrapperRef}>{data.map(i=><div ref={ref}><ListItem /></div>)</div>
)
Failed to construct 'IntersectionObserver': Failed to read the 'root' property from 'IntersectionObserverInit': The provided value is not of type '(Document or Element)'.
My question if my approach is correct and if yes how to set root?
The react-intersection-observer docs mention to use the
ref
it gives you fromuseInView
. No need to create a new one. Alsoroot
defaults to the viewport of the entire document which is probably what you want.If you don't want root to be the document viewport but instead a container, you can give
useInView
aroot
ref:There's many good examples here as well.