Skip to content

Instantly share code, notes, and snippets.

@pedrogardim
Created October 19, 2023 17:00
Show Gist options
  • Save pedrogardim/ab6505bfd4281ea97f7da3f01142ef7a to your computer and use it in GitHub Desktop.
Save pedrogardim/ab6505bfd4281ea97f7da3f01142ef7a to your computer and use it in GitHub Desktop.
Typing text effect React hook
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