由于在 react 中,state 更新是一个异步操作的行为,所以不要依赖于 this.props 和 this.state 去计算下一个 state 的值; 举个例子:
// Wrong
this.setState({
counter: this.state.counter + this.props.increment
});
// Correct
this.setState((prevState, props) => ({
counter: prevState.counter + props.increment
}));