Tell us what’s happening:
I am trying to get the else - if statement replaced by the ternary operator and I do not know why I can’t get this logic to work
Your code so far
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() {
this.setState(state => {
(state.visibility ? {visibility: false} : {visibility: 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; rv:105.0) Gecko/20100101 Firefox/105.0
Challenge: React - Use State to Toggle an Element
Link to the challenge: