React中的this.props.children
2018-07-05 本文已影响0人
小龙虾Julian
1、this.props
对象的属性与组件的属性是一一对应的,但是有一个例外就是this.props.children
属性,它表示组件的所有子节点:
var NotesList = React.createClass({
render: function() {
return (
<ol>
{
React.Children.map(this.props.children, function (child) {
return <li>{child}</li>;
})
}
</ol>
);
}
});
ReactDOM.render(
<NotesList>
<span>hello</span>
<span>world</span>
</NotesList>,
document.body
);
2、上面代码的 NoteList 组件有两个 span 子节点,它们都可以通过 this.props.children
读取,运行结果如下:
