UNPKG

react-use

Version:
22 lines (21 loc) 817 B
import { useEffect, useState } from 'react'; var useIntersection = function (ref, options) { var _a = useState(null), intersectionObserverEntry = _a[0], setIntersectionObserverEntry = _a[1]; useEffect(function () { if (ref.current) { var handler = function (entries) { setIntersectionObserverEntry(entries[0]); }; var observer_1 = new IntersectionObserver(handler, options); observer_1.observe(ref.current); return function () { if (ref.current) { observer_1.disconnect(); } }; } return function () { }; }, [ref, options.threshold, options.root, options.rootMargin]); return intersectionObserverEntry; }; export default useIntersection;