189
rated 0 times
[
190]
[
1]
/ answers: 1 / hits: 45360
/ 8 Years ago, sat, january 14, 2017, 12:00:00
I just started learning React and JavaScript.
While going through the tutorial, I got to this example code of a component, which creates a toggle button.
This is part of the code:
class Toggle extends React.Component {
constructor(props) {
super(props);
this.state = {isToggleOn: true};
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState(prevState => ({ // prevState?
isToggleOn: !prevState.isToggleOn
}));
}
2 things that are bugging me here:
- Where did the
prevState
argument come from?
I don't see anything likevar prevState = this.state;
before calling it, and still, it works. - The syntax of the arrow function: why the parentheses after the arrow?
Why doesn't the usualarg => { statement; }
syntax work here?
Sorry for the newbie questions...
More From » reactjs