I was reading the page on React's docs about State and Lifecycle:
https://reactjs.org/docs/state-and-lifecycle.html
And I don't understand why you can pass a function to "setState" to make state updates synchronous.
In other words, according to the docs:
this.setState({ counter: this.state.counter + this.props.increment, });
Might update the counter when it's executed... or it might not. But this:
this.setState((state, props) => ({ counter: state.counter + props.increment }));
Will always update the counter.
Why? What's the difference between both? What does React do internally in that case? Does it execute the internal function first and then store the result to update the state later, when it's better for performance? Or does it update the state inmediately? And in that case, is that a good practice for performance? Or does it do something completely different?