0

I would like to create a <textarea> that behaves like console.log.

class Console extends Component {
...
    async componentDidMount() {
        for (var i = 1; i < 10; i++) {
           setTimeout(() => {this.setState({message: 'updateMessage' + millis()}, 1000);
        }
    }
    render() {
       return <textarea>{this.state.messsage}</textarea>
    }
}

I expected this code to update the UI in realtime. The issue is that componentDidMount waits until all the setTimeout's functions are executed in the loop then renders everything all at once.

Is there something I am misunderstanding? Thanks

1
  • 1
    that's working as intended since you all the set time out resolve at the same time. Commented Jan 3, 2021 at 21:08

1 Answer 1

2

Your setTimeout are all resolved at the same time, and your messages are not in array, hence it's overwritten each time.

If I use a delay promise, instead of set timeout, then the state will be updated accordingly.

const delay = async () => {
  await new Promise((resolve) => {
    setTimeout(resolve, 1000)
})
}

Example: codesandbox

class Console extends Component {
...
    async componentDidMount() {
        for (var i = 1; i < 10; i++) {
           this.setState({message: 'updateMessage' + millis()})
           await delay()
        }
    }
    render() {
       return <textarea>{this.state.messsage}</textarea>
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.