diff --git a/src/components/reusable/TypewritterText/TypewritterText.tsx b/src/components/reusable/TypewritterText/TypewritterText.tsx index 25fe53d..02647af 100644 --- a/src/components/reusable/TypewritterText/TypewritterText.tsx +++ b/src/components/reusable/TypewritterText/TypewritterText.tsx @@ -1,4 +1,4 @@ -import React, { useCallback, useEffect, useRef, useState } from 'react' +import React, { useEffect, useRef, useState } from 'react' import './TypewritterText.scss'; interface Props { @@ -9,9 +9,10 @@ interface Props { export default function TypewriterText({ texts, color }: Props) { const [currentText, setCurrentText] = useState(texts[0]); const currentTextRef = useRef(texts[0]); + const intervalRef = useRef(); - const changeText = useCallback(() => { - setInterval(() => { + const changeText = () => { + intervalRef.current = setInterval(() => { const nextTextIndex = texts.indexOf(currentTextRef.current) + 1; if (nextTextIndex > texts.length - 1) { @@ -23,11 +24,16 @@ export default function TypewriterText({ texts, color }: Props) { currentTextRef.current = texts[nextTextIndex] } }, 3000) - }, [texts]) + } useEffect(() => { changeText(); - }, [changeText]); + return () => { + if (intervalRef.current) { + clearInterval(intervalRef.current); + } + }; + }, [texts]); return (