Hey, I am having a problem understanding this challenge. it’s unclear for me for many reasons
first of all what is meant by state updates may be asynchronous ?
and why React may batch multiple setState()
calls into a single update. ?
why react may call setState multiple times when doing a single update?
This means you can’t rely on the previous value of this.state
or this.props
when calculating the next value. So, you should not use code like this: and does this means that when calling the previous value it will already be updated?
Your code so far
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
visibility: false
};
// Change code below this line
// Change code above this line
}
// Change code below this line
// Change code above this line
render() {
if (this.state.visibility) {
return (
<div>
<button onClick={this.toggleVisibility}>Click Me</button>
<h1>Now you see me!</h1>
</div>
);
} else {
return (
<div>
<button onClick={this.toggleVisibility}>Click Me</button>
</div>
);
}
}
}
Challenge: React - Use State to Toggle an Element
Link to the challenge: