chauncey
2/12/2016 - 1:28 AM

gistfile1.js

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>