Last active
March 9, 2023 23:08
-
-
Save timurcatakli/b9a7a718cbb23f7434f09dc3830a12db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useMemo, useState } from "react"; | |
function useIsInViewPort(ref) { | |
const [isVisible, setIsVisible] = useState(false); | |
const observer = useMemo(() => { | |
return new IntersectionObserver( | |
([entry]) => { | |
setIsVisible(entry.isIntersecting); | |
}, | |
{ threshold: 0.5 }, | |
); | |
}, [ref]); | |
useEffect(() => { | |
observer.observe(ref.current); | |
return () => { | |
observer.unobserve(ref.current); | |
}; | |
}, [ref, observer]); | |
return isVisible; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment