mirror of
https://github.com/DefectingCat/DefectingCat.github.io
synced 2025-07-16 09:11:38 +00:00
26 lines
579 B
TypeScript
26 lines
579 B
TypeScript
import { useEffect, useRef, useState } from 'react';
|
|
|
|
const useInView = () => {
|
|
const ref = useRef(null);
|
|
const [inView, setInView] = useState(false);
|
|
|
|
useEffect(() => {
|
|
const observer = new IntersectionObserver((entries, observer) => {
|
|
entries.forEach((entry) => {
|
|
if (entry.isIntersecting) {
|
|
if (ref.current) {
|
|
setInView(true);
|
|
observer.unobserve(ref.current);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
ref.current && observer.observe(ref.current);
|
|
}, []);
|
|
|
|
return { ref, inView };
|
|
};
|
|
|
|
export default useInView;
|