Skip to content

Instantly share code, notes, and snippets.

@dstevensio
Created January 7, 2016 18:37
Show Gist options
  • Save dstevensio/5340e61d9f740d905ec3 to your computer and use it in GitHub Desktop.
Save dstevensio/5340e61d9f740d905ec3 to your computer and use it in GitHub Desktop.
Binding in React with React.Component use
class MyComponent extends React.Component {
constructor(props) {
super(props);
this._myHandlerFunc = this._myHandlerFunc.bind(this);
}
_myHandlerFunc() {
// do something
}
render() {
return <p onClick={this._myHandlerFunc}>that's like your opinion, man</p>;
}
}
@johnman
Copy link

johnman commented Jan 8, 2016

You could also try _myHandlerFunc = ()=> {
// do something
}

And then you don't need to remember to add that binding to the constructor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment