簡體   English   中英

如何使子組件的子組件具有第一級子代可以使用的引用?

[英]How make child components of a child component have refs that the first-level child can use?

如果我具有以下JSX,則Childrefs為空:

<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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM