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 fires
setIntersecting(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 unmount
return [isIntersecting, reset]
}
export default useNear