Reputation: 91
I want to get the tree of my rendered React Components as I have a scenario where I have to convert the React Components to an object to store it as it is.
Please see below for the example of React tree Structure.
React tree structure as in React Dev Tools
Upvotes: 8
Views: 4039
Reputation: 32158
You can take a look at React Test Renderer.
provides a React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM or a native mobile environment.
and then create a JSON reepresentation of your tree
const instance = TestRenderer.create(<MyComponent />);
instance.toJSON();
Upvotes: 2