I want to create a text where's another letter is added to the last one, like in an old computer. But the letters keeps replacing one another instead of adding to one another:
const App = () => {
const [text, setText] = React.useState(``);
React.useEffect(() => {
[`x`,`y`].forEach((letter, index) => {
setTimeout(()=> {
// old: setText(text => text + letter)
setText(text => text + letter) // new: printing each letter twice
}, 500 * index)
})
}, [])
return (text)
}
ReactDOM.createRoot(document.getElementById(`root`))
.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.development.js"></script>
<div id="root"></div>
What's the way to fix it?
Thanks!