Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 15, 2022 12:47 am GMT

help useRef within useEffect

my Goal: Is to create a simple effect that prints out a string one letter at a time.

example: string = "TEXT". it should print T E X T

Whats currently being printed is: T X T undefined

my code:

`const [placeholder, setPlaceholder] = useState('');
const
string = 'TEXT',
index = useRef(0);

useEffect(() => {
function showChar() {
setPlaceholder(prev => prev + string[index.current]);
console.log(index.current)
index.current++;
}
if (index.current < string.length) {
let addChar = setInterval(showChar, 500);
return () => clearInterval(addChar);
}
}, [placeholder]);

return (


{placeholder}

)

export default App;`


Original Link: https://dev.to/bay_area_ninja/help-useref-within-useeffect-468o

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To