Answer the question
In order to leave comments, you need to log in
Is it possible to get all nested children?
<Parent>
<ALevel>
<BLevel></BLevel>
</ALevel>
</Parent>
Parent
get and ALevel
and BLevel
?
Answer the question
In order to leave comments, you need to log in
There is an API for working with children: https://facebook.github.io/react/docs/top-level-ap... . Since children are just props, iterate through the tree recursively and collect elements:
function getChildren(children) {
let elements = []
if (!children) {
return elements
}
React.Children.forEach(children, child => {
elements = [ ...elements, child, ...getChildren(child.props.children) ]
})
return elements
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question