I was refreshing my react skills and there is one confusing thing…
...
//in constructor method is bound to this
this.increment = this.increment.bind(this);
...
//method itself
increment() {
this.setState(state => ({
count: state.count + 1
}));
Now, here is my question:
Why do we use state=>({}) instead of state=>{}
I tried to find some examples in mdn docs , but no info.
jenovs
March 6, 2021, 3:13pm
2
From Arrow function expressions - JavaScript | MDN
This is because the code inside braces ({}) is parsed as a sequence of statements (i.e. foo is treated like a label, not a key in an object literal).
You must wrap the object literal in parentheses
3 Likes
Ah, I am ashamed that I missed that. Thanks.
system
Closed
September 5, 2021, 3:20am
4
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.