Skip to content

Instantly share code, notes, and snippets.

@0xLDev
Created December 18, 2021 08:40
Show Gist options
  • Save 0xLDev/084a39ef463439276211ace00550318c to your computer and use it in GitHub Desktop.
Save 0xLDev/084a39ef463439276211ace00550318c to your computer and use it in GitHub Desktop.
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