This commit is contained in:
focp212@naver.com
2025-10-17 17:23:32 +09:00
parent ec16b6ec11
commit dc6d80eb81
8 changed files with 190 additions and 36 deletions

View File

@@ -0,0 +1,33 @@
import { useEffect, useState } from "react";
interface useIntersectionObserverProps {
root?: null;
rootMargin?: string;
threshold?: number;
onIntersect: IntersectionObserverCallback;
}
const useIntersectionObserver = ({
root,
rootMargin = '0px',
threshold = 0,
onIntersect
}: useIntersectionObserverProps) => {
const [target, setTarget] = useState<HTMLElement | null | undefined>(null);
useEffect(() => {
if (!target) return;
const observer: IntersectionObserver = new IntersectionObserver(
onIntersect,
{ root, rootMargin, threshold }
);
observer.observe(target);
return () => observer.unobserve(target);
}, [onIntersect, root, rootMargin, target, threshold]);
return { setTarget };
};
export default useIntersectionObserver;