繁体   English   中英

如何接收孩子的孩子?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM