vkorobtsev-nextJs/app/about/components/quote/useIntersectionObserver.js

34 lines
683 B
JavaScript

// useIntersectionObserver.js
import { useEffect, useState, useRef } from "react";
export function useIntersectionObserver() {
const [isIntersecting, setIsIntersecting] = useState(false);
const ref = useRef(null);
useEffect(() => {
const observer = new IntersectionObserver(
([entry]) => {
setIsIntersecting(entry.isIntersecting);
},
{
root: null,
rootMargin: "0px",
threshold: 0.1,
}
);
const currentRef = ref.current; // Store the ref value in a variable
if (currentRef) {
observer.observe(currentRef);
}
return () => {
if (currentRef) {
observer.unobserve(currentRef);
}
};
}, []);
return [ref, isIntersecting];
}