Today we will explore how to use the intersection observer API in React with some examples.
Mozilla web documentation describes the intersection observer API as:
allows code to register a callback function that runs whenever an element they want to monitor enters or leaves another element (or the viewport), or when the value by which the two intersect changes of a requested amount. This way, sites no longer need to do anything on the main thread to observe this type of intersection of elements, and the browser is free to optimize intersection management as it sees fit.
In short, it allows us to detect when a certain element is visible in the viewport, this only happens when the element meets the desired intersection ratio.
As you can see, if you scroll down the page the intersection ratio will increase until it reaches the projected limit and at that moment the function that executes a callback is triggered.
const observer = new IntersectionObserver(callbackFunction, options) observer.observer(elementToObserver)
The intersection observer constructor object needs two arguments:
That's all, we're ready to see some action, but first, we need to know what each option means, the options argument is an object with the following values:
const options = { root: null, rootMargin: "0px", threshold: 1 }
Now we will see an implementation of the intersection observer API in React.
const observer = new IntersectionObserver(callbackFunction, options) observer.observer(elementToObserver)
const options = { root: null, rootMargin: "0px", threshold: 1 }
Remember, this is just a basic implementation and there are several ways to do this.
Now we will implement the same code we did previously, but separating all the logic in a nu hook called useElementOnScreen.
const containerRef = useRef(null) const [isVisible, setIsVisible] = useState(false) const callbackFunction = (entries) => { const [entry] = entries setIsVisible(entry.isIntersecting) } const options = { root: null, rootMargin: "0px", threshold: 1.0 } useEffect(() => { const observer = new IntersectionObserver(callbackFunction, options) if (containerRef.current) observer.observe(containerRef.current) return () => { if (containerRef.current) observer.unobserve(containerRef.current) } }, [containerRef, options]) return ( <div className="app"> <div className="isVisible">{isVisible ? "IN VIEWPORT" : "NOT IN VIEWPORT"}</div> <div className="section"></div> <div className="box" ref={containerRef}>Observe me</div> </div> )
<div className="box" ref={containerRef}>Observe me</div>
1- Import the newly created hook into our component.
2 - Initialize it with the options object.
3 - This is how we finish.
Congratulations, we have successfully used the intersection observer API and even created a hook for it!
Intersection Observer using React, originally written by producthackers
Thank you for reading this article. I hope I can provide you with some useful information. If so, I would be very happy if you would recommend this post and click the ♥ button so more people can see this.
The above is the detailed content of How to use the Intersection Observer in React. For more information, please follow other related articles on the PHP Chinese website!