Skip to content

Instantly share code, notes, and snippets.

@bloodyowl
Created June 21, 2015 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bloodyowl/de2ac8f7b0c9801112f7 to your computer and use it in GitHub Desktop.
Save bloodyowl/de2ac8f7b0c9801112f7 to your computer and use it in GitHub Desktop.
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