Hello everyone. I solved this challenge a few minutes ago following the instructions about the if/else statement that returns something different based on the boolean value of this.state.display. However, I came up with a different idea. Instead of using the return statement two times, I declared a const variable named message and when the value of this.state.display is true the message is “Displayed!” and when the value is false it becomes empty like this “”. Then I insert the variable in the h1
tag with curly braces as shown below. Is it considered a good practice or should it be avoided?? Thank you in advance.
My code:
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
display: true
}
this.toggleDisplay = this.toggleDisplay.bind(this);
}
toggleDisplay() {
this.setState({
display: !this.state.display
});
}
render() {
// change code below this line
const message;
if(this.state.display){
message = "Displayed!";
}else{
message = "";
}
return (
<div>
<button onClick={this.toggleDisplay}>Toggle Display</button>
<h1>{message}</h1>
</div>
);
}
};