• Type of the Children props

    Type of the Children props

    Usually, a component’s children (this.props.children) is an array of components:

    1. var GenericWrapper = React.createClass({
    2. componentDidMount: function() {
    3. console.log(Array.isArray(this.props.children)); // => true
    4. },
    5. render: function() {
    6. return <div />;
    7. }
    8. });
    9. ReactDOM.render(
    10. <GenericWrapper><span/><span/><span/></GenericWrapper>,
    11. mountNode
    12. );

    However, when there is only a single child, this.props.children will be the single child component itself without the array wrapper. This saves an array allocation.

    1. var GenericWrapper = React.createClass({
    2. componentDidMount: function() {
    3. console.log(Array.isArray(this.props.children)); // => false
    4. // warning: yields 5 for length of the string 'hello', not 1 for the
    5. // length of the non-existent array wrapper!
    6. console.log(this.props.children.length);
    7. },
    8. render: function() {
    9. return <div />;
    10. }
    11. });
    12. ReactDOM.render(<GenericWrapper>hello</GenericWrapper>, mountNode);

    To make this.props.children easy to deal with, we’ve provided the React.Children utilities.