Skip to content
Advertisement

React treeview from JSON array

I am making a treeview from JSON using react. So far I have made a collapsible tree using this example data:

JavaScript

But this is one object. I want to get JSON array of objects as input and generate treeview from that but I am unable to understand where to change the code.

Here’s my render function:

JavaScript

How can I change the code to work with the whole array and not just one object?

Advertisement

Answer

Recursion is fun!

JavaScript

Demo: https://codesandbox.io/s/01kl2xmo40

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement