Rev 5193 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import { useEffect, useState } from 'react'const useNear = (ref, rootMargin = '0px') => {const [isIntersecting, setIntersecting] = useState(false)const reset = () => setIntersecting(false)useEffect(() => {const observer = new IntersectionObserver(([entry]) => {// Update our state when observer callback firessetIntersecting(entry.isIntersecting)},{rootMargin})if (ref.current) {observer.observe(ref.current)}return () => {observer.unobserve(ref.current)}}, [isIntersecting, ref]) // Empty array ensures that effect is only run on mount and unmountreturn [isIntersecting, reset]}export default useNear