728x90
반응형
useFadeIn
서서히 나타나는 이벤트를 hooks로 만들어보기
useFadeIn 예시
3초 뒤 Hello, 15초 뒤 lorem ipsum...이 화면에 뜸
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(5, 10);
return (
<div className="App">
<h1 {...fadeInH1}>Hello</h1>
<p {...fadeInP}>lorem ipsum ...</p>
</div>
);
};
반응형
'프로그래밍 > React' 카테고리의 다른 글
[React] React Hooks (9) - useFullscreen (0) | 2023.02.06 |
---|---|
[React] React Hooks (8) - useScroll (0) | 2023.02.03 |
[React] React Hooks (6) - useBeforeLeave (0) | 2023.02.01 |
[React] React Hooks (5) - useRef (2) | 2023.01.31 |
[React] React Hooks (4.1) - useEffect 활용법 (0) | 2023.01.30 |