React.js > useEffect와 useRef를 이용해 FadeIn 애니메이션 효과 만들기
import React, { useEffect, useRef } from "react";
import ReactDOM from "react-dom";
const useFadeIn = (duration = 1, delay = 0) => {
if (typeof duration !== "number" || typeof delay !== "number") {
return;
}
const element = useRef();
useEffect(() => {
if (element.current) {
const { current } = element;
current.style.transition = `opacity ${duration}s ease-in-out ${delay}s`;
current.style.opacity = 1;
}
}, []);
return { ref: element, style: { opacity: 0 } };
};
const App = () => {
const fadeInH1 = useFadeIn(1, 2);
const fadeInP = useFadeIn(3, 4);
return (
<div>
<h1 {...fadeInH1}>Hello</h1>
<p {...fadeInP}>Nice to meet you!</p>
</div>
);
};
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);