是否有將道具傳遞給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