Render sandpack when it in view

This commit is contained in:
Defectink
2022-04-14 14:17:19 +08:00
parent 859fd847f1
commit b9049bfe19
3 changed files with 36 additions and 11 deletions

25
lib/hooks/useInView.ts Normal file
View 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;