Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/components/reusable/TypewritterText/TypewritterText.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useRef, useState } from 'react'
import React, { useEffect, useRef, useState } from 'react'
import './TypewritterText.scss';

interface Props {
Expand All @@ -9,9 +9,10 @@ interface Props {
export default function TypewriterText({ texts, color }: Props) {
const [currentText, setCurrentText] = useState<string>(texts[0]);
const currentTextRef = useRef<string>(texts[0]);
const intervalRef = useRef<NodeJS.Timeout>();

const changeText = useCallback(() => {
setInterval(() => {
const changeText = () => {
intervalRef.current = setInterval(() => {
const nextTextIndex = texts.indexOf(currentTextRef.current) + 1;

if (nextTextIndex > texts.length - 1) {
Expand All @@ -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 (
<div className='typewritter'>
Expand Down