Error on Use State to Toggle an Element

Tell us what’s happening:
I put this code but it returns “Cannot read property ‘visibility’ of undefined”

Where is my error ?

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 = ({
          visibility: !this.state.visibility
        })
  }
        
  // 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 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

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

this.setState = ({

That equal sign it not supposed to be there :wink:

ooh, what a silly :slight_smile:

1 Like