I have XML parsed as JSON. I want to build a React component tree by traversing the JSON and calling React.createElement
on each node. The third argument to React.createElement
is an array of child React elements. This means that I have to walk down the tree to the leaf nodes, create those React elements first, and then walk back up each branch.
Simple, recursive iteration over a tree structure is simple enough. I'm unsure how to say "okay, now you're at the leaf node, go back up". Are there techniques for this?
Sample data:
{
"section":{
"attrs":{
"class":"foo",
"data-foo":"foo"
},
"#name":"section",
"children":[
{
"attrs":{
"class":"region-1"
},
"#name":"p",
"children":[
{
"attrs":{
"data-children":"true"
},
"#name":"span"
}
],
"span":[
{
"attrs":{
"data-children":"true"
}
}
]
},
{
"attrs":{
"class":"second"
},
"#name":"div"
}
],
"p":[
{
"attrs":{
"class":"region-1"
},
"children":[
{
"attrs":{
"data-children":"true"
},
"#name":"span"
}
],
"span":[
{
"attrs":{
"data-children":"true"
}
}
]
}
],
"div":[
{
"attrs":{
"class":"second"
}
}
]
}
}