useIntersectionObserver

React

  • animation
  • observer

2023-05-07 17:51

โœ๐Ÿป Table Of Contents

useIntersectionObserver

hook/useIntersectionObserver.js

import { useEffect, useRef, useState } from 'react'

const useIntersectionObsever = (targetRef) => {

  useEffect(() => {
    const target = targetRef.current;
    if (!target) return;

    const options = {
      root: null,
      rootMargin: "0px",
      threshold: 0.5,
    };

    const observer = new IntersectionObserver((entries) => {
      entries.forEach((entry) => {
        if (entry.isIntersecting) {
          target.style.opacity = 1;
          target.style.transitionDuration = `1s`;
        } else {
          target.style.opacity = 0;
        }
      });
    }, options);

    observer.observe(target);

    return () => {
      observer.unobserve(target);
    };
  }, [targetRef]);

}
export default useIntersectionObsever
javascript

์‚ฌ์šฉ

const targetRef = useRef(null);

useIntersectionObsever(targetRef);

return (
	...
	<div ref={targetRef}></div>
)
javascript