There is something off about toggleVisibility. It is hard to understand what is happening in its setState function.
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
visibility: false
};
// Change code below this line
this.toggleVisibility = this.toggleVisibility.bind(this);
// Change code above this line
}
// Change code below this line
toggleVisibility = (state, prop) => setState(
{this.state.visibility ? false : true}
);
// 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>
);
}
}
}
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36 Edg/89.0.774.50
.
Challenge: Use State to Toggle an Element
Link to the challenge: