【发布时间】:2017-06-06 18:09:45
【问题描述】:
我正在尝试确定将子组件作为父组件的道具传递的惯用/规范方式。我找不到这方面的好例子,并且正在努力确定这是否与 this.props.children.... 假设我有一个像这样的简单 React 组件:
const Clazz = React.createClass({
render: function(){
return (
<this.props.A/>
<this.props.B/>
{this.props.children}
)
}
});
假设我要渲染这个组件,并像这样传递两个子组件:
const A = React.createClass({
render: () => 'a';
})
const B = React.createClass({
render: () => 'b';
})
ReactDOM.render(<Clazz A={A} B={B} />, document.getElementById('root'));
如您所见,我想通过 props 将子组件传递给组件 C。我从来没有理解过 - 这与使用 this.props.children 有什么不同吗?我在这里所做的是否足够好? 将孩子作为道具传递的惯用方式是什么,这与: this.props.children 有什么关系?几个月后,我仍然不明白this.props.children 的含义。我假设 this.props.children 是由 React 设置的,并且我不应该设置该值,但我不确定。
【问题讨论】: