I don't understand the behavior of react's setState.


                {this.state.count}
                <button onClick={this.add}>click</button>
            </div>
        );
    }
}

export default App;

according to official documents, repeating setState count: this.state.count + 1 is only added once

but this problem does not exist with count: PPthis.state.count.

can someone tell me what"s going on

Sep.10,2021

React doesn't update this.state.count until the component is re-rendered

so every time this.setState ({count: this.state.count + 1}) is executed, this.state.count is 0, and the final result of setState is 1; while this.setState ({count: PPthis.state.count}) each time this.state.count increases by + 1, and the final result of setState is the result of this.state.count .


PPthis.state.count

this code is equivalent to

this.state.count = this.state.count + 1
Menu