react-use/src/useIntersection.ts
2020-02-04 07:12:19 +05:30

31 lines
930 B
TypeScript

/* eslint-disable */
import { RefObject, useEffect, useState } from 'react';
const useIntersection = (
ref: RefObject<HTMLElement>,
options: IntersectionObserverInit
): IntersectionObserverEntry | null => {
const [intersectionObserverEntry, setIntersectionObserverEntry] = useState<IntersectionObserverEntry | null>(null);
useEffect(() => {
if (ref.current && typeof IntersectionObserver === 'function') {
const handler = (entries: IntersectionObserverEntry[]) => {
setIntersectionObserverEntry(entries[0]);
};
const observer = new IntersectionObserver(handler, options);
observer.observe(ref.current);
return () => {
setIntersectionObserverEntry(null);
observer.disconnect();
};
}
return () => {};
}, [ref.current, options.threshold, options.root, options.rootMargin]);
return intersectionObserverEntry;
};
export default useIntersection;