Hello, am, as many struggling with the “this” keyword. I understand, that it is pointing one level up (like outside a function). Also, that it is pointing to the word, that is to the left of the dot.
But it still is not intuitive for me. Like as in the following example:
import React from 'react'
class Counter extends React.Component {
constructor() {
super()
this.state = {
counter: 0
}
this.handleIncrement = this.handleIncrement.bind(this)
}
handleIncrement() {
this.setState({
counter: this.state.counter += 1
})
}
render() {
return (
<div>
<div>{this.state.counter}</div>
<hr />
<button type="button" onClick={this.handleIncrement}>+</button>
</div>
)
}
}
I would really appreciate, if someone could (like in paint) add some arrows to the “this” keyword and explain to me, where is what pointing to.
Thanks in advance!