Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Last active February 13, 2016 00:53
Show Gist options
  • Save ryanflorence/5e96fafe4e4edc186945 to your computer and use it in GitHub Desktop.
Save ryanflorence/5e96fafe4e4edc186945 to your computer and use it in GitHub Desktop.
const IDGenerator = React.createClass({
childContextTypes: {
generateId: React.PropTypes.func
},
getChildContext() {
return {
generateId: () => {
return ++this.id
}
}
},
componentWillMount() {
this.id = 0
},
render() {
return React.Children.only(this.props.children)
}
})
const SomeForm = React.createClass({
contextTypes: {
generateId: React.PropTypes.func
},
render() {
const generateId = this.context.generateId || Math.random()
const id = this.context.generateId()
// ...
}
})
// usage
<IDGenerator>
<SomeForm/>
</IDGenerator>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment