Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using the ES7 bind syntax with react components
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