Answer the question
In order to leave comments, you need to log in
Why is the updated state not visible in the console?
Why does console.log componentDidMoun
give updated data inside, but console.log
at the end it gives 122
? I don’t understand why it gives out old, not updated data, because it is triggered by the .onmessage event every 2 seconds. I will be grateful for help.
constructor(props) {
super(props);
this.state = { messages: [122] };
};
componentDidMount() {
this.connection = new WebSocket('ws://reactchart:8080');
this.connection.onmessage = responce => {
this.setState({
messages : responce.data
});
console.log(this.state.messages);
};
this.connection.onmessage = responce => console.log(this.state.messages);
....
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question