0
我開始使用React。在圖書館組件中,我經常看到props.children
是必需的道具(也需要child.key
)。在教程中,我看到您可以將子節點放入React.createElement(tag, [props], [children])
調用中。React - 何時使用props.children以及何時將元素添加到React.createElement?
那麼什麼時候用哪個?
我開始使用React。在圖書館組件中,我經常看到props.children
是必需的道具(也需要child.key
)。在教程中,我看到您可以將子節點放入React.createElement(tag, [props], [children])
調用中。React - 何時使用props.children以及何時將元素添加到React.createElement?
那麼什麼時候用哪個?
他們雙方是同一枚硬幣:
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>