I tried to call onClick
on the button by doing
<button>Click Me</button>{.onClick(handleClick)}
but it gave an error in the console saying there’s a build error.
I also tried it like this:
const button =
<button>Click Me</button>;
{button.onClick(handleClick}
but that too had the same result.
So what am I doing wrong?
My 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>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/84.0.4147.125 Safari/537.36 Edg/84.0.522.59
.
Challenge: Bind ‘this’ to a Class Method
Link to the challenge: