2016-09-20 8 views
0

私はReactを使い始めています。図書館の構成要素では、私はしばしばprops.childrenが必要とされていることを知っています(また、child.keyが必要です)。チュートリアルでは、子ノードをReact.createElement(tag, [props], [children])コールに入れることができます。React - props.childrenをいつ使用するのか、React.createElementに要素を追加するのはいつですか?

だからいつ使うのですか?

答えて

0

これらは同じコインの表裏である:

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>{this.props.summary}</summary> 
     {this.props.children} 
    <details>); 
    } 
}); 

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

// Final "HTML" 
<details> 
    <summary>Here is where we *use* children</summary> 
    <span>Some more details</span> 
</details> 
関連する問題