It seems that React components change the behavior of how this is assigned in DOM event handlers, but I cannot find any documentation that details this behavior.
For example, when using an object's method as a DOM event handler with vanilla JS, the this context remains as the object:
function SomeClass() {}
SomeClass.prototype.showThis = function() { console.log(this) };
let o = new SomeClass();
<button onclick="o.showThis()">Show "this"</button>
React, however, changes this behavior such that the this context becomes undefined:
function SomeClass() {}
SomeClass.prototype.showThis = function() { console.log(this) };
let o = new SomeClass();
function App() {
return (
<button onClick={o.showThis}>Show this</button>
);
}
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
Where is the documentation that explains this? (I can surmise why this may occur, but would like to know where this is covered in the React docs.)