mirror of
https://github.com/DefectingCat/DefectingCat.github.io
synced 2025-07-15 16:51:37 +00:00
Render sandpack when it in view
This commit is contained in:
25
lib/hooks/useInView.ts
Normal file
25
lib/hooks/useInView.ts
Normal file
@ -0,0 +1,25 @@
|
||||
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;
|
Reference in New Issue
Block a user