简体   繁体   English

如何接收孩子的孩子?

[英]How to receive childs of childs?

I want to skip one child encapsulation to get access to the grand-child directly.我想跳过一个子封装以直接访问孙子。

this.props.children.map((x) => ...);

The example above is used to loop over the childs of a parent... But how do i iterate over the childs of childs ?上面的示例用于循环父级的子级……但是我如何遍历子级的子级?

I tried to access the childs of childs, but this gave me a undefined exception.我试图访问孩子的孩子,但这给了我一个未定义的异常。

this.props.children.map((x) => return x.children);

this.props.children is not an array. this.props.children不是数组。 you should transform it before apply map transformation.您应该在应用地图转换之前对其进行转换。

let children = React.Children.toArray(this.props.children)
children.map((x) => return x.props.children);

Also you have to access children props to access to its children.您还必须访问儿童道具才能访问其儿童。

This will work for you:这对你有用:

const level2Child = this.props.children.map(child => {
        return child.props.children;
});

instead of:代替:

this.props.children.map((x) => return x.children);

use:用:

this.props.children.map((x) => return x.props.children);

It will be hard for you to get exact code of people which solve your problem.您很难获得解决您问题的人员的确切代码。 Rather I would suggest you to take approach of 'recursion'.相反,我建议您采用“递归”的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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