Skip to content

Commit

Permalink
Add custom hook useIntersectionObserver
Browse files Browse the repository at this point in the history
  • Loading branch information
alphacoder-mp3 committed Sep 14, 2024
1 parent 43998e3 commit fad195f
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions hooks/useIntersectionObserver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useEffect, useRef, MutableRefObject } from 'react';

export function useIntersectionObserver(
callback: () => void
): MutableRefObject<IntersectionObserver | null> {
const observerRef = useRef<IntersectionObserver | null>(null);

useEffect(() => {
observerRef.current = new IntersectionObserver(entries => {
if (entries[0].isIntersecting) {
callback();
}
});

return () => observerRef.current?.disconnect();
}, [callback]);

return observerRef;
}

0 comments on commit fad195f

Please sign in to comment.