@raury

Почему componentDidMount() не зацикливается?

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;


Ведь componentDidMount() выполняется после рендера и изменяет состояние, после чего все должно повторяться по кругу.
  • Вопрос задан
  • 95 просмотров
Решения вопроса 1
LinkVP
@LinkVP
Frontend, React, React Native, Flutter
Ререндер происходит всего 2 раза.

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.


Читайте документацию внимательней.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы