I am learning React and I came across event handlers. In React, it is recommended to bind the function to this, before using it as an event handler. However, I did not bind it and I am still getting the desired output. Below is my code:
import React, { Component } from 'react';
class Experiment extends Component {
constructor(props){
super(props);
}
clickEvent(e){
e.preventDefault();
document.getElementById('change').innerHTML="This text changed";
}
render(){
return(
<div>
<p id='change'>This paragraph will change</p>
<button onClick={this.clickEvent}>Press me</button>
</div>
);
}
}
export default Experiment;
As you can see, I haven't bound clickEvent() to this, yet the onClick event works without a hitch. Why is that? I assumed that I would have gotten an undefined error or something else