34
loading...
This website collects cookies to deliver better user experience
// Defining config
let config = {
root: document.querySelector('#scrollArea'),
rootMargin: '0px',
threshold: 1.0
}
// What action needs to be taken
let callback = () => {
// Here you specify what needs to be done
// More on this later
}
// Creating an observer object
let observer = new IntersectionObserver(callback, config)
null
is passed....
const App = () => {
// Create an observer object
const observer = useRef(null);
observer.current = new IntersectionObserver(
(entries) => {
// entries is of type array
entries.forEach((entry) => {
// this will tell if the element is visible or not
if (!entry.isIntersecting) {
return;
}
// Do something
})
}, {
root: document.querySelector("#App"),
rootMargin: "0px",
threshold: 0.3,
});
...
// ComponentDidMount, Make sure to observe the element after render
useEffect(() => {
observer.current.observe(document.querySelector("#observe-this"));
}, []);
...
return (
<div className="App">
...
{/* When this div is visible, fetch new data */}
<div id="observe-this"></div>
...
</div>
);
};
...