React Custom Hooks로 scroll animation 만들기 FadeIn편 | 수줍은 동그래 블로그

PHOTO EMBED

Mon Sep 05 2022 05:37:21 GMT+0000 (Coordinated Universal Time)

Saved by @wumeenna #javascript

const useScrollFadeIn = () => {
  const dom = useRef();
  
  const handleScroll = useCallback(([entry]) => {
      const { current } = dom;
    	
    	if(entry.isIntersecting) {
        // 원하는 이벤트를 추가 할 것
      }
    }, []);
  
  useEffect(() => {
    let observer;
    const { current } = dom;
    
    if (current) {
      observer = new IntersectionObserver(handleScroll, { threshold: 0.7 });
      observer.observe(current);
      
      return () => observer && observer.disconnect();
    };
  }, [handleScroll]);
  
    return {
    ref: dom,
  };
}
content_copyCOPY

https://shylog.com/react-custom-hooks-scroll-animation-fadein/