I wanted to understand the basic difference between how the below syntax works.
// Syntax 1
class Component extends React.Component {
constructor(props) {
super(props)
this.handleClick = this.handleClick.bind(this)
}
handleClick() {
// ...
}
}
// Syntax 2
handleClick = () => {
console.log('this is:', this)
}
<button onClick={this.handleClick}>
{'Click me'}
</button>
While syntax 1 needs an explicit binding to be specified inside constructor. However, it seems like with syntax 2, that is not needed. How is syntax 2 able to achieve the binding automatically ?
I assume this understanding/syntax can be extended for any framework including React, Angular, Ember, etc