Last active
December 20, 2021 06:05
-
-
Save mrcoles/f82827d4ff42f4ec0c5b4d008ddd55eb to your computer and use it in GitHub Desktop.
A simple hook for running an IntersectionObserver inside a React FunctionComponent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useRef } from 'react'; | |
export type OnIntersect = (elt: HTMLElement) => void; | |
/** | |
* Hook for running an IntersectionObserver. | |
* @param onIntersect this is used in the deps of useEffect, you can | |
* employ useCallback to prevent it from running | |
* after every render | |
* @param args | |
* @returns an HTMLElement `ref`, make sure to set it to a component | |
*/ | |
export const useIntersectionObserver = ( | |
onIntersect: OnIntersect, | |
{ | |
threshold = 0, | |
root = null, | |
rootMargin = undefined, | |
}: IntersectionObserverInit = {} | |
) => { | |
const ref = useRef<HTMLElement>(); | |
let observer: IntersectionObserver; | |
useEffect(() => { | |
if (window.IntersectionObserver) { | |
const callback: IntersectionObserverCallback = (entries) => { | |
entries.forEach((entry) => { | |
if (entry.isIntersecting) { | |
onIntersect(entry.target as HTMLElement); | |
observer.unobserve(entry.target); | |
} | |
}); | |
}; | |
const args = { threshold, root, rootMargin }; | |
observer = new IntersectionObserver(callback, args); | |
observer.observe(ref.current); | |
return () => observer.disconnect(); | |
} | |
}, [onIntersect, JSON.stringify(threshold), root, rootMargin]); | |
return ref; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: