I have the following code:
class App extends Component {
constructor(){
super()
//this.buttonOneClick = this.buttonOneClick.bind(this);
}
buttonOneClick() {
const { setTestData } = this.props;
setTestData("test");
}
render() {
const { testData } = this.props;
return (
<div className="App">
<h1>Test App</h1>
<div>{testData}</div>
<button onClick={this.buttonOneClick}>Test</button>
</div>
);
}
}
The component App is exported with react-redux's connect method to map the setTestData
-function to the props (thats where setTestData
is coming from).
When I set the buttonOneClick()
handler without biding the component context to it (see the outcommented line in my code in constructor()
) I get the following error:
Cannot read property 'props' of undefined
I understand that props wouldn't be defined since the handler is executed in the global context which doesn't know about props but how I understand the error it implies that this
itself is not defined which doesn't make sense to me. Why is this the case? Shouldn't this
always be defined in any execution context?
Thanks in advance