Created
October 19, 2023 17:00
-
-
Save pedrogardim/ab6505bfd4281ea97f7da3f01142ef7a to your computer and use it in GitHub Desktop.
Typing text effect React hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from "react"; | |
export default function useTypingText(originalText: string, delay = 50) { | |
const [letterCount, setLetterCount] = useState(0); | |
useEffect(() => { | |
const interval = setInterval(() => { | |
setLetterCount((prev) => { | |
if (prev === originalText.length) { | |
clearInterval(interval); | |
} | |
return prev + 1; | |
}); | |
}, delay); | |
return () => clearInterval(interval); | |
}, []); | |
return originalText.slice(0, letterCount); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment