Created
June 21, 2015 15:29
-
-
Save bloodyowl/de2ac8f7b0c9801112f7 to your computer and use it in GitHub Desktop.
using the ES7 bind syntax with react components
This file contains hidden or 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
import React, {Component, PropTypes} from "react" | |
class MyComponent extends Component { | |
static propTypes = { | |
onClick: PropTypes.func, | |
} | |
handleClick(event) { | |
const {onClick} = this.props | |
if(onClick) { | |
onClick(event) | |
} | |
} | |
render() { | |
const {children} = this.props | |
return ( | |
<button onClick={::this.handleClick}> | |
{/* here, the `handleClick` method is bound to `this`, | |
which is MyComponent instance. */} | |
{children} | |
</button> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment