0

I am fetching a JSON file and want to make a visual tree representation of it.

The file itself is quite large, therefore what I want is using a loop to only display the very first keys that are found in an object (in that iteration). A key that has child nodes is displayed with an arrow next to it.

I have a TreeItem component:

<TreeView>
 <TreeItem nodeId="1" label="First Key" />
 <TreeItem nodeId="2" label="Second Key">
   <TreeItem nodeId="3" label="First Child" />
   <TreeItem nodeId="4" label="Second Child" />
   <TreeItem nodeId="5" label="Third Child" />
 </TreeItem>
<TreeView>

Is there a way I can embed the child TreeItem components if I know that the second key has children?

2 Answers 2

1

It'll be reasonable to start using react-virtualized - the library was created for such cases

Sign up to request clarification or add additional context in comments.

Comments

0

If you are not planning to use any library and want to do it in the plain react way, I would suggest rendering the inner blocks using the ternary operator.

This way you will be rendering only the parent elements first and as soon as you click on the element, based on some flag/condition, you can render the inner one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.