If I click a button, it calls handleClick
which increases the value of the button by 1 from 0.
I have made the handleClick
code work in two different scenarios. I understand the first, I don't seem to grasp the second one.
I wrote this one. And it works.
handleClick = () => {
this.setState({count: this.state.count + 1})
}
The tutorial shows this one and it works too.
handleClick = () => {
this.setState(({count}) => ({
count: count + 1
}))
}
If I change the code to this, this works too.
handleClick = () => {
this.setState(() => ({
count: this.state.count + 1
}))
}
What is the advantage of using the second body of code when it does the same thing but is more complex than the first one?
I understand I am returning an object literal which is basically what I wrote instead of generating it in the first code body. But why am I passing {count}
as a parameter when I can keep the parameter empty and just use this.state.count
in the body?