Skip to content

Instantly share code, notes, and snippets.

@kerrickstaley
Created September 18, 2016 20:39
Show Gist options
  • Save kerrickstaley/4c7200885cb56eef25ad73aab501df33 to your computer and use it in GitHub Desktop.
Save kerrickstaley/4c7200885cb56eef25ad73aab501df33 to your computer and use it in GitHub Desktop.
Binding onClick to this in react.js
class Canvas extends Component {
constructor(props) {
super(props);
var onClick;
if (this.props.onClick) {
onClick = this.props.onClick.bind(this);
}
this.state = {
onClick: onClick,
};
this.doFoo = this.doFoo.bind(this);
}
render() {
return (
<div className="Canvas" onClick={ this.state.onClick }>
</div>
);
}
doFoo(bar) {}
}
function doFooOnClick() {
this.doFoo(42);
}
ReactDOM.render(
<Canvas onClick={ addWidgetOnClick }/>,
document.getElementById('root');
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment