C
C
copal2016-03-29 22:44:18
JavaScript
copal, 2016-03-29 22:44:18

Is it possible to get all nested children?

<Parent>
    <ALevel>
        <BLevel></BLevel>
    </ALevel>
</Parent>

Is it possible to Parentget and ALeveland BLevel?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
N
Nikita Gushchin, 2016-03-30
@copal

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
}

A
Anton Izmailov, 2016-03-29
@WapGeaR

Receive in what form?

P
prishelec, 2016-03-29
@prishelec

Through recursion. There are many examples.

L
lem_prod, 2016-03-30
@lem_prod

var pChilpren = document.querySelectorAll('Parent *');

or
var parent = document.getElementsByTagName('Parent'); //в случае если только один парент, вообще лучше через id 
var child = parent.querySelectorAll('*');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question