简体   繁体   English

为什么 setState() 在重复调用期间不响应更新 state?

[英]Why isn't setState() in react updating state during recurrent calls?

I've started learning react using the official documentation came across this "React may batch multiple setState() calls into a single update for performance. Because this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state."我已经开始使用官方文档学习反应,遇到了这个“React 可能将多个 setState() 调用批处理到单个更新中以提高性能。因为 this.props 和 this.state 可能会异步更新,所以你不应该依赖它们的值计算下一个 state。” Upon practicing, i came across this problem, which i assume is in some way tied to this, but i still don't understand why snippet(1) doesn't work and snippet(2) does.在练习时,我遇到了这个问题,我认为这在某种程度上与此有关,但我仍然不明白为什么片段(1)不起作用而片段(2)起作用。

   //  code-snippet(1)

    import React from "react";
    import ReactDOM from 'react-dom';
    import "./styles.css";
    class Count extends React.Component {
      constructor(props) {
        super(props);
        this.state = {count : 0};
      }
      increment() {
        var newCount = state.count + 1;
        this.setState({count: newCount});
      }
      increment5() {
        this.increment();
        this.increment();
        this.increment();
        this.increment();
        this.increment();
      }
      render() {
        return (
        <div>
          <h1>Count - {this.state.count}</h1>
          <button onClick={() => this.increment5()}>Increment</button>
        </div>
        );
      }
    }
     ReactDOM.render(<Count />, document.getElementById("root"));


//   code-snippet(2)

     import React from "react";
        import ReactDOM from 'react-dom';
        import "./styles.css";
        class Count extends React.Component {
          constructor(props) {
            super(props);
            this.state = {count : 0};
          }
          increment() {
            this.setState(function(state) {
              var newCount = state.count + 1;
              return {count : newCount};
            });
          }
          increment5() {
            this.increment();
            this.increment();
            this.increment();
            this.increment();
            this.increment();
          }
          render() {
            return (
            <div>
              <h1>Count - {this.state.count}</h1>
              <button onClick={() => this.increment5()}>Increment</button>
            </div>
            );
          }
        }
         ReactDOM.render(<Count />, document.getElementById("root"));

I think if you just add "this" in the increment method of your first snippet it would work perfectly:我认为如果您只是在第一个代码段的增量方法中添加“this”,它将完美地工作:

 increment() {
    var newCount = this.state.count + 1; //you forgot the this keyword on this line
    this.setState({count: newCount});
  }

Since setState works in an asynchronous way.由于 setState 以异步方式工作。 That means after calling setState the this.state variable is not immediately changed.这意味着在调用 setState 后 this.state 变量不会立即更改。 so if you want to perform an action immediately after setting state the state may have the older value so that second state update may not regarde the previous state value. so if you want to perform an action immediately after setting state the state may have the older value so that second state update may not regarde the previous state value.

As in the second script the callback function will always use the last state value that's why the second approach is working without any issue.与第二个脚本一样,回调 function 将始终使用最后一个 state 值,这就是为什么第二种方法可以正常工作的原因。

And also you are missing the this keyword in this keyword in the first snipped.而且你在第一个剪断的this关键字中错过了这个关键字。

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

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