是否有将道具传递给React中的后代组件的推荐模式?反应:将道具传递给后代
下面,我通过道具callback
的子组件:
Master = React.createClass({
render: function() {
return (
<div>
<ListComp items={this.props.items} callback={this.handleClick} />
</div>
);
}
});
ListComp = React.createClass({
render: function() {
this.props.items.forEach(function(item) {
items.push(<ItemView item={item} callback={this.props.callback} />);
}, this);
return (
<ul>{items}</ul>
);
}
});
然后是callback
道具被流传下来的后代组成:
ItemComp = React.createClass({
render: function() {
return (
<li><a onClick={this.handleClick} href="#">Link</a></li>
);
},
handleClick: function(e) {
e.preventDefault();
this.props.callback();
}
});
是否正确传递道具两次这样下去,还是应该以某种方式引用它的继承?
我看到了一个文档的方法transferPropsTo
,并从记录它看起来像我可以从后代通过this.props.__owner__.props
得到callback
但是那些两双下划线让我觉得我不应该。
我看到通过回调两次没有问题。它需要多一点打字,但它明确而易于理解。 – NVI