Answer the question
In order to leave comments, you need to log in
Why is componentDidMount() not looping?
import React, {Component} from 'react';
class Clock extends Component {
constructor(props) {
super(props);
this.state = {
vall: 0};
}
componentDidMount() {
this.setState({val: 5})
}
render() {
return (
<div>
<h1>Привет, мир!</h1>
</div>
);
}
}
export default Clock;
Answer the question
In order to leave comments, you need to log in
The rerender happens only 2 times.
You may call setState() immediately in componentDidMount(). It will trigger an extra rendering, but it will happen before the browser updates the screen. This guarantees that even though the render() will be called twice in this case , the user won't see the intermediate state.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question