Why does this.handleClick not need ()?

Tell us what’s happening:
Why does this.handleClick not need () in the button tags

  **Your code so far**

class MyComponent extends React.Component {
constructor(props) {
  super(props);
  this.state = {
    text: "Hello"
  };
  // Change code below this line
  this.handleClick = this.handleClick.bind(this)
  // Change code above this line
}
handleClick() {
  this.setState({
    text: "You clicked!"
  });
}
render() {
  return (
    <div>
      { /* Change code below this line */ }
      <button onClick = {this.handleClick}>Click Me</button>
      { /* Change code above this line */ }
      <h1>{this.state.text}</h1>
    </div>
  );
}
};
  **Your browser information:**

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

Challenge: Bind ‘this’ to a Class Method

Link to the challenge:

Hello @birdmaster , Welcome to the forum.

When using class based components in React, you need to bind the event handler methods to the component instance.

Read more on why here

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.