Created
December 18, 2021 08:40
-
-
Save 0xLDev/084a39ef463439276211ace00550318c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { useEffect, useState } from 'react'; | |
export const useAnimatedText = (text, delayMs) => { | |
const [currentPos, setCurrentPos] = useState(0); | |
useEffect(() => { | |
const intervalHandle = setInterval(() => { | |
setCurrentPos((pos) => { | |
const isLast = pos === text.length - 1; | |
return isLast ? 0 : pos + 1; | |
}); | |
}, delayMs); | |
return () => { | |
clearInterval(intervalHandle); | |
}; | |
}, [text, delayMs]); | |
return text.substring(0, currentPos); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment