简体   繁体   English

状态 ReactJS 和渲染方法

[英]State ReactJS and render method

I'm studying chapter State and lifecycle in reactJS with Clock class and I don't understand why I can re-render my variables "myFirstNumber, mySecondNumber, myWord" which are not states when I use this code in CodePen:我正在研究带有 Clock 类的reactJS 中的章节状态和生命周期,我不明白为什么我可以重新渲染我的变量“myFirstNumber、mySecondNumber、myWord”,当我在 CodePen 中使用此代码时,这些变量不是状态:

class Clock extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      date: new Date(),
    };
    this.mySecondNumber = 0;
  }

  componentDidMount() {
    this.myFirstNumber = 0;
    this.myWord = "Start";
    this.timerID = setInterval(
      () => this.tick(),
      1000
    );

  }

  componentWillUnmount() {
    clearInterval(this.timerID);
  }

  tick() {
    this.setState({
      date: new Date(),

    });
        this.myFirstNumber += 1;
       this.mySecondNumber += 1;
    if (this.myFirstNumber ===5) {
      this.myWord = "Finish";
    }
  }

  render() {
    return (
      <div>
        <h1>Hello, world!</h1>
        <h2>It is {this.state.date.toLocaleTimeString()}.</h2>
        <h2>myFirstNumber from ComponentDidMount: {this.myFirstNumber}</h2>
        <h2>mySecondNumber from constructor: {this.mySecondNumber}</h2>
        <h2>myWord: {this.myWord}</h2>
      </div>
    );
  }
}

ReactDOM.render(
  <Clock />,
  document.getElementById('root')
);

The render method render all DOM each seconde for my variables which are not states ? render 方法每秒为我的不是状态的变量渲染所有 DOM ?

当您运行tick()setState部分会触发重新渲染,它将处理所有内容,无论状态与否。

In componentDidMount() you schedule interval which triggers tick() function every second.componentDidMount()您安排时间间隔, tick()触发一次tick()函数。 Inside this function you update component's state by invoking setState() and passing current time as a new state value.在此函数中,您可以通过调用setState()并将当前时间作为新状态值传递来更新组件的状态。 Moreover, you modify some local variables of the class.此外,您修改了类的一些局部变量。 When arguments passed to setState() are processed by JS engine, what happens asynchronously, not immediately after setState() is called, the component updates its state.当传递给setState()参数被 JS 引擎处理时,会异步发生,而不是在setState()被调用后立即更新组件的状态。

Then the render() function is called by the framework.然后render()函数被框架调用。 The render() function returns output which reflects the current values of all variables requested inside render() function. render()函数返回的输出反映了render()函数内部请求的所有变量的当前值。 If you didn't call setState() inside tick() method then you wouldn't see any changes even though you modify myFirstNumber and other variables after each second.如果您没有在tick()方法中调用setState()那么即使您myFirstNumber修改myFirstNumber和其他变量,您也不会看到任何更改。

When you call this.setState function, the DOM gets rerendered.当你调用 this.setState 函数时,DOM 被重新渲染。 Here the DOM is getting updated every second when this.state.date gets changed through this.setState, showing new values.当 this.state.date 通过 this.setState 改变时,DOM 每秒更新一次,显示新值。 However, if you keep date outside of state, you can see that the DOM no longer rerenders, same result if you want to change "myWord" or "firstNumber" without changing anything in state.但是,如果您将日期保留在状态之外,您可以看到 DOM 不再重新渲染,如果您想更改“myWord”或“firstNumber”而不更改状态中的任何内容,则结果相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM