React: Use State to Toggle and Element Error

Tell us what’s happening:
Hello, what I enter the code for the React challenge, even if I use the solution code I get this error:
SyntaxError: unknown: Unexpected token (12:8)

10 | }
11 | // Change code below this line

12 | this.setState(state => ({
| ^
13 | visibility: !state.visibility
14 | }));
15 | // Change code above this line

Also earlier when I just start putting in the bind section I got this:
“Build error, open your browser console to learn more.”
Thanks for your help.

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
  this.setState(state => ({
    visibility: !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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60.

Challenge: Use State to Toggle an Element

Link to the challenge:

this is the mistake in the code.

you tried to bind before you defined the function so it says “Cannot read property ‘bind’ of undefined”