UNPKG

hooks-me

Version:

<div align="center"> <h1>hooks-me</h1> <div>React useful hooks.</div>

24 lines (23 loc) 792 B
import { useEffect, useState } from "react"; var useIsVisible = function (ref, rootMargin) { if (rootMargin === void 0) { rootMargin = "0px"; } var _a = useState(false), isVisible = _a[0], setIsVisible = _a[1]; useEffect(function () { if (ref.current == null) { return; } var observer = new IntersectionObserver(function (_a) { var entry = _a[0]; return setIsVisible(entry.isIntersecting); }, { rootMargin: rootMargin }); observer.observe(ref.current); return function () { if (ref.current == null) { return; } observer.unobserve(ref.current); }; }, [ref.current, rootMargin]); return isVisible; }; export default useIsVisible;