Why no parentheses here:
toggleVisibility() {
this.setState(state => {
if (state.visibility === true) {
return { visibility: false };
} else {
return { visibility: true };
}
});
But here there are:
increment() {
this.setState(state => ({
count: state.count + 1
}));
Thanks,
Andrej
Accoding to MDN
An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the this, arguments, super, or new.target keywords. Arrow function...
It looks like you “Parenthesize the body of a function to return an object literal expression”.
1 Like
ilenia
June 19, 2020, 5:08pm
3
the first one, you have explicit return, so the graph parenthesis are of the function body
the second one you have implicit return and the {}
are of an object, if you don’t put the round parenthesis around the object, the graph parenthesis are considered those of the function body, and as such name: "value"
is wrong sintax loose inside a function
Why does this code not work:
reset() {
this.setState(state => ({count: 0}))
}
And this code does:
reset() {
this.setState({
count: 0
});
I fixed it like this:
reset() {
this.setState(state => ({count: state.count - state.count}))
}
But is this the only way?
ilenia
June 19, 2020, 6:17pm
6
here you do not have a function…
state.count - state.count
makes 0, you can just have count: 0
inside that object instead of count: state.count - state.count