import React, { Component } from 'react';
class SayHello extends React.Component {
constructor(props) {
super(props);
this.state = { message: 'Hello!' };
// This line is important!
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
console.log(this.state.message);
}
render() {
return <button onClick={this.handleClick}>Say hello</button>;
}
}
export default SayHello;`
why do we need to bind ?? I understand the context but wont this
will refer to the instance, can someone explain with an example in a non-react way or in simple JS class.