when I use this code :
html : <body>
<button>Click me</button>
<script type="module" src="1.js"></script>
</body>
JS : const btn = document.querySelector('button')
class Events {
constructor() {
console.log(this)
btn.addEventListener('click', this.handleClick)
}
handleClick() {
console.log(this)
}
}
const event = new Events()
"this" = button element
But when I use this code in react:
class Event extends Component {
constructor(props) {
super(props)
this.state = {}
}
handleClick() {
console.log(this)
}
render() {
return (
<div>
<button onClick={this.handleClick}>Click me</button>
</div>
)
}
}
"this" = undefined why 'this' in the javascript event handler is different than react event handler?