Why
Because x++
expression first returns the value of x
then it increments it, while ++x
first increments it, then returns that incremented value.
Just for experimentation purposes, you can verify this by using the preincrement like this
// Don't do this, it's just to explain why it works.
addCount () {
this.setState({count: ++this.state.count})
}
Then it'll do what you want, however it's not idiotmatic, because it does mutate directly this.state.count
.
Idiomatic way
In ReactJS you don't want to mutate this.state.count
directly, so your first example is more idiomatic:
addCount () {
this.setState({count: this.state.count + 1})
}
References
MDN postfix/prefix increment operator ++