Skip to content

Instantly share code, notes, and snippets.

@tolumide-ng
Last active January 20, 2021 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tolumide-ng/fff16f5b0dad53165c034fc3b9e6a11f to your computer and use it in GitHub Desktop.
Save tolumide-ng/fff16f5b0dad53165c034fc3b9e6a11f to your computer and use it in GitHub Desktop.
import * as React from "react";
interface entryDef {
isIntersecting: Boolean;
intersectionRatio: number;
}
interface IntersectionObserverDef {
ancestorElem?: Element,
}
export const useIntersectionObserver = (props: IntersectionObserverDef) => {
const [converge, setConverge] = React.useState(false);
React.useEffect(() => {}, [converge]);
function executeJob(entry: Array<entryDef>){
entries.forEach((entry) => {
if(entry.isIntersecting){
setConverge(true);
}
})
}
function createScrollObserver() {
const options = {
threshold: 1,
root: props.ancestorElem ?? null;
rootMargin: "0px"
}
return new IntersectionObserver(executeJob, options);
}
return {
createScrollObserver,
converge,
setConverge,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment