我想跳过一个子封装以直接访问孙子。
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);
此外,您还必须访问儿童道具才能访问其孩子。