[英]How to receive childs of childs?
我想跳過一個子封裝以直接訪問孫子。
this.props.children.map((x) => ...);
上面的示例用於循環父級的子級……但是我如何遍歷子級的子級?
我試圖訪問孩子的孩子,但這給了我一個未定義的異常。
this.props.children.map((x) => return x.children);
this.props.children
不是數組。 您應該在應用地圖轉換之前對其進行轉換。
let children = React.Children.toArray(this.props.children)
children.map((x) => return x.props.children);
您還必須訪問兒童道具才能訪問其兒童。
這對你有用:
const level2Child = this.props.children.map(child => {
return child.props.children;
});
代替:
this.props.children.map((x) => return x.children);
用:
this.props.children.map((x) => return x.props.children);
您很難獲得解決您問題的人員的確切代碼。 相反,我建議您采用“遞歸”的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.