Use State to Toggle an Element React

Tell us what’s happening:

Hi guys, I’m having difficulties debugging this. Can someone help me with it.

Thank you!

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(){
  if (this.state.visibility=true){
    this.setState = ({
      visibility: false
    });
}
else{
   this.setState = ({
      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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/react/use-state-to-toggle-an-element

The debug difficult is due to the missing curly braces after your else statement ( two are missing).
I would add a couple of things:

  • if (this.state.visibility=true)

    in js = is assignment, == is loose equality, === is stricly equality

  • this.setState = ({ […] })

    This.setState is a method, that equal sign shouldn’t be there ^^

3 Likes

I learnt something new today. Thank you!!

1 Like

Works like this :
:smile:

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({
visibility: !this.state.visibility
})
}

// change code above this line
render() {
if (this.state.visibility) {
return (


Click Me

Now you see me!



);
} else {
return (

Click Me

);
}
}
};
6 Likes

As @MARTAdip pointed out the easiest solution is using the not operator “!”

3 Likes

Try rc-if-else

npm install --save rc-if-else
import React from 'react';
import { If } from 'rc-if-else';

class CommentBox extends Component {
    constructor() {
        super();
        this.state = {
            showForm: false
        }
    }
  
    render() {
        const showReplyForm = () => {
            this.setState({showForm: true});
        };
    
        return (
            <div>
                <div>
                    <p>Some comment</p>
                    <a onClick={showReplyForm}>Post a reply to this comment</a>
                </div>
                <If condition={this.state.showStatus}>
                    <ReplyForm />
                </If>
            </div>
        )
    }
}

Oh! it was so simple and i struggled for 2 hours to solve. thanks @MARTAdip for your help

Hi KingKongg, you have not yet passed anonymous functions inside this.setState

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() {
if(this.state.visibility){
this.setState((state, props) =>
({visibility: false})
)
} else {
this.setState((state, props)=>(
{visibility: true}
))
}
}
// change code above this line

render() {
if (this.state.visibility) {
return (


Click Me

Now you see me!



);
} else {
return (

Click Me

);
}
}
};

It worked for me!!