[英]How make child components of a child component have refs that the first-level child can use?
如果我具有以下JSX,則Child
的refs
為空:
<Parent>
<Child ref="first">
<GrandChild ref="parentSeesThisNotChild" />
</Child>
</Parent>
我希望Child
組件在其refs
獲得GrandChild
。 我怎么做?
您將必須修改Child
組件代碼:
const Child = React.createClass({
render() {
const childrenWithProps = React.Children.map(this.props.children, (child) => {
return React.cloneElement(child, {
ref: child.ref,
});
});
return (
<div>
<h1>Child</h1>
{childrenWithProps}
</div>
)
},
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.