Please note this question is not talking about previous versions of react as in this 2017 question Reactjs: In JavaScript, class methods are not bound by default
In reactjs docs on Handling events, they mention the following for this example
In JavaScript, class methods are not bound by default. If you forget to bind this.handleClick and pass it to onClick, this will be undefined when the function is actually called.
I think the bold statement is technically incorrect. Consider the following example:
class component2 {
prop = 3;
foo() {
console.log(this.prop);
}
}
var obj = new component2();
obj.foo();
<button id="one">Print prop</button>
As you can see I didn't have to bind this
. This contradicts react18 documentation and also contradicts react2017 answer