简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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