React Tree Graph — Render Your Data as a Tree Using SVG

Search for a command to run...

No comments yet. Be the first to comment.
Bun, the hyper-fast JavaScript runtime known for its incredible speed and Zig-based architecture, is being completely rewritten in Rust. For a project that has over 22 million monthly downloads and ea

Next.js 16.3 is almost here, and it's packed with a ton of improvements and let's see them in this post 1.Instant Navigation For a while now, there’s been a valid, lingering debateServer Components vs

It’s official. React has merged its long-awaited Rust port of the React Compiler (formerly known as React Forget) into the main repository. What started as an experimental research project by Joseph S

If you follow modern technology trends, you have likely been led to believe that artificial intelligence is entirely built on Python. Every tutorial, machine learning framework documentation, and open

Just when the JavaScript ecosystem was catching its breath after last week’s TanStack Router compromise, the Mini Shai-Hulud supply chain worm has continued its relentless march. Developed by the thre

React Tree graph is a component which renders data as a tree using SVG
Installing React Tree Graph Component
Customizing Tree Graph
Tree Graph Click Events
Custom Path props
Tree view animation
Let’s begin by creating a React app using the command below
create-react-app tree-view cd tree-view npm start
If you would like to use TypeScript for your React application, then use the command below
create-react-app tree-view --typescript cd tree-view npm start
Now we will install the react-tree-graph component using the below command
npm install react-tree-graph --save
If you are using TypeScript, then you need to add the types for the component. Unfortunately, the react-tree-graph doesn’t have the types to install so we need to declare a module like below
Create a new folder named types in src->types and then create a new file named AllTypes.d.ts and add the below code.
declare module 'react-tree-graph';
Click Save and then go to tsconfig.json and add the below code in compiler options
{
"compilerOptions": {
.......
"typeRoots": ["./src/types"]
},
....
}
Now we will add the code in App.tsx. First, replace the default code available in App.tsx with the below code
Run the project using the command npm start and your output will be like below

Now we will customize the tree graph by adding more data and custom colors for the paths.
Create a file named data.ts and add the below code
In App.tsx replace the old code with the new one like below
Replace App.css and create new styles and add the below code
Now run the project to see the following output

Now we will customize the tree graph by showing the text in below for the parent node **Color** and increase the icon size using **nodeRadius**
In data.ts add textProps: {x: -25, y: 25} for all the nodes and children and in App.tsx add the props for Tree component like below
Now run the project and you could see the output

If we want to do some operation while clicking a child in the tree, we can do it using gProps.onClick as the following code shows
And your output be:
If you would like to have custom styles for your paths you can do it using the path props by setting the class name and passing the value and in CSS define the class name like below
And in data.ts add, a new property called pathProps and define a className in pathProps as shown below
pathProps: {className: 'red'}
Save and run the project. Your output will be like

If you want your tree graph with animation simply add animated in the <Tree /> component.
click Save and run the project you will see the animation like below
Congratulations you have created tree graph in react with custom styles and animation. Explore more with the plugin. Happy coding :)
react-tree-graph
Generates a tree graph from datawww.npmjs.com