Skip to content

Commit

Permalink
Bug: 타이핑 효과 적용시 2번째 글씨 누락 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
s0zzang committed Nov 21, 2024
1 parent a30e057 commit 1127c81
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/hooks/useTyping.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import { useState, useEffect } from 'react';

const TypingEffect = ({ text, container }: { text: string; container: HTMLDivElement }) => {
const [displayedText, setDisplayedText] = useState('');
const [index, setIndex] = useState(0);

useEffect(() => {
let index = 0;
const interval = setInterval(() => {
if (index < text.length - 1) {
if (index < text.length) {
setDisplayedText((prev) => prev + text[index]);
index++;
container.scrollTo({ top: container.scrollHeight - container.clientHeight, behavior: 'smooth' });
setIndex((prev) => prev + 1);
container.scrollTo({ top: container.scrollHeight, behavior: 'smooth' });
} else {
clearInterval(interval);
clearInterval(interval); // 타이핑이 끝나면 정지
}
}, 50);
console.log(container.scrollHeight);
return () => clearInterval(interval);
}, [text]);

return () => clearInterval(interval); // 컴포넌트 언마운트 시 인터벌 클리어
}, [text, index, container]);

return <p>{displayedText}</p>;
};
Expand Down

0 comments on commit 1127c81

Please sign in to comment.