I'm new to React, going through the tutorial on the React website. In the step by step tutorial, there is a section which explains that you should not rely on the value of state for calculating the next state, because state updates are asynchronous. Instead, they explain, you should use a version of setState that accepts a function to which the previous state is passed as an argument.
However, in the practical tutorial, the following code appears:
handleClick(i) {
const squares = this.state.squares.slice();
squares[i] = this.state.xIsNext ? 'X' : 'O';
this.setState({
squares: squares,
xIsNext: !this.state.xIsNext,
});
}
in which the setState call passes an object that relies on a value from this.state. Why is this code ok and why would one not have to worry about asynchronous setState execution in this case? I'm trying to understand when exactly I can rely directly on the value of this.state in my React code.
Thanks in advance!