Created
September 18, 2016 20:39
-
-
Save kerrickstaley/4c7200885cb56eef25ad73aab501df33 to your computer and use it in GitHub Desktop.
Binding onClick to this in react.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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