R
R
raury2020-02-01 17:23:37
React
raury, 2020-02-01 17:23:37

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;


After all, componentDidMount () is executed after rendering and changes the state, after which everything should repeat in a circle.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LinkVP, 2020-02-01
@raury

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.

Read the documentation carefully.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question