this
is defined in the render
method, but not in an eventHandler that calls an arrow function.
I did a fresh clean slate of npx create-react-app projectName
and am reproducing an unexpected issue I'm encountering in a personal project.
import React from 'react';
class RemoteSubmitButton extends React.Component {
onSignUpClick = () => {
debugger; // "this" is undefined
};
render() {
debugger; // "this" is defined
return (
<button
type="button"
onClick={this.onSignUpClick}
>
Submit
</button>
);
}
}
export default RemoteSubmitButton;
Whenever I reference this
inside of an arrow function, I expect the context of the calling method (the render
method) to get carried over. Placing a few debuggers I see that this
is defined in the render method, but not within the onSignUpClick
method.