Render with an If/Else Condition1

Tell us what’s happening:

i am not able to understand what is going wrong really

Your code so far


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
      if(this.state.display===true){
    return (
       <div>
         <button onClick={this.toggleDisplay}>Toggle Display</button>
         <h1>Displayed!</h1>
       </div>
    );
      }
      else {
        return({
        <div>
        <button onClick={this.toggleDisplay}>Toggle Display</button>
        <div>
        })
      }
  }
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/react/render-with-an-ifelse-condition/

Two things here.

In your else statement, you don’t need a curly bracket inside return. That means you are returning an object.

Also you need to make sure that you are closing off your div with </div>

1 Like

thanks for your help it worked