Last active
February 13, 2016 00:53
-
-
Save ryanflorence/5e96fafe4e4edc186945 to your computer and use it in GitHub Desktop.
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
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