react-hooks-bank
Version:
A collection of **powerful, reusable custom React hooks** for complex, non-trivial interactions that go beyond React’s native features.
19 lines (18 loc) • 649 B
JavaScript
import { useEffect, useRef, useState } from "react";
export function useIntersectionObserver(options) {
if (options === void 0) { options = {}; }
var ref = useRef(null);
var _a = useState(null), entry = _a[0], setEntry = _a[1];
useEffect(function () {
var node = ref.current;
if (!node)
return;
var observer = new IntersectionObserver(function (_a) {
var entry = _a[0];
return setEntry(entry);
}, options);
observer.observe(node);
return function () { return observer.disconnect(); };
}, [ref.current]);
return { ref: ref, entry: entry };
}