react-use
Version:
Collection of React Hooks
24 lines (23 loc) • 900 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = require("react");
var useIntersection = function (ref, options) {
var _a = react_1.useState(null), intersectionObserverEntry = _a[0], setIntersectionObserverEntry = _a[1];
react_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;
};
exports.default = useIntersection;