This is a very simple question but I am in the learning process and after reading around I could not find a good explanation to this, in the code below: What is the purpose of the line:
this.buttonClicked = this.buttonClicked.bind(this);
If I comment it, the program is still working. Most likely with some side effects but I don't know them yet...
class test extends React.Component {
constructor(props){
super(props)
//this.buttonClicked = this.buttonClicked.bind(this);
}
buttonClicked() {
alert("thank you!")
}
render() {
return (
<div>
<h2>{this.props.text}</h2>
<button onClick={this.buttonClicked}>click me!</button>
</div>
)
}
}
thisin the callback will refer to your component instancealert(this.props.text)instead.