I'm trying out the new React v0.13 ES6 component classes and I've run into a problem. When I try to have a click handler within the class call a function the component has received through props it throws an error saying that this
isn't defined.
import React, { Component } from 'react';
class MyComp extends Component {
render() {
return (
<div>
<input type='button' value='Fetch Data' onClick={this.handleClick} />
</div>
);
}
handleClick(evt) {
// ...do stuff
this.props.fetch(); // throws error: `cannot read property 'this' of undefined`
}
}
I double-checked and I can access the props from within the render
function so I know that they exist in the place that they should.
When I use the old React.createClass
syntax the same code works fine so I'm wondering if I missed something. I know I can fix this easily by just binding this
to the handleClick
method in the render function but I'd rather not if it's something simple.