Boyang Boyang - 1 year ago 51
React JSX Question

React - when to use props.children and when to add element to React.createElement?

I'm getting started with React. In library components I often see

being required prop (also required
). In tutorials I see you can put children nodes into
React.createElement(tag, [props], [children])

So when to use which?

Answer Source

They are two sides to the same coin:

var MyComponent = React.createClass({
  propTypes: {
    summary: React.PropTypes.string,
    // Children are what we accept, and we display them in our <details>
    children: React.PropTypes.element
  render: function() {
    return (<details>

  {summary: 'Here is where we *use* children'},
  React.createElement('span', {}, 'Some more details')

// Final "HTML"
  <summary>Here is where we *use* children</summary>
  <span>Some more details</span>