How To Add TreeView Component In React Application

Introduction

 
In this article, we will learn how to add TreeView component in React applications. A tree view is used to display a hierarchical list of data. In this demo we use React material UI Treeview component. Material UI is one of the most popular UI frameworks developed by Google. The Material UI library is designed for faster, easier and more developer-friendly user interface development. Now Material-UI is supported by all major browsers and platforms.
 
You can check my previous article in which we discussed ReactJS and its basic components from the below-mentioned links.
Prerequisites

Create ReactJS project

 
Let's first create a React application using the following command.
  1. npx create-react-app platform   

Install Material-UI

 
Now install Material-UI by using the following command.
  1. npm install @material-ui/core --save    
Now, right-click on the "src" folder and add a new component named 'Treeview.js'
 
First let’s add the following reference in this component.
  1. import React from 'react';  
  2. import TreeView from '@material-ui/lab/TreeView';  
  3. import ExpandMoreIcon from '@material-ui/icons/ExpandMore';  
  4. import ChevronRightIcon from '@material-ui/icons/ChevronRight';  
  5. import TreeItem from '@material-ui/lab/TreeItem';  
  6. import AppBar from '@material-ui/core/AppBar';  
  7. import Toolbar from '@material-ui/core/Toolbar';  
 Add the following code in Treeview component.
  1. import React from 'react';  
  2. import TreeView from '@material-ui/lab/TreeView';  
  3. import ExpandMoreIcon from '@material-ui/icons/ExpandMore';  
  4. import ChevronRightIcon from '@material-ui/icons/ChevronRight';  
  5. import TreeItem from '@material-ui/lab/TreeItem';  
  6. import AppBar from '@material-ui/core/AppBar';  
  7. import Toolbar from '@material-ui/core/Toolbar';  
  8.   
  9.   
  10. export default function Treeview() {  
  11.    
  12.   return (  
  13.     <>  
  14.     <AppBar position="static">  
  15.     <Toolbar style={{ 'paddingLeft'"600px" }}>  
  16.     Tree View Component in React Application   
  17. </Toolbar>  
  18. </AppBar>  
  19.     <TreeView  
  20.       defaultCollapseIcon={<ExpandMoreIcon />}  
  21.       defaultExpandIcon={<ChevronRightIcon />}  
  22.     >  
  23.       <TreeItem nodeId="1" label="Music">  
  24.         <TreeItem nodeId="2" label="Audio">  
  25.             <TreeItem nodeId="3" label="Hindi Music" />  
  26.             <TreeItem nodeId="4" label="English Music" />  
  27.         </TreeItem>  
  28.         <TreeItem nodeId="5" label="Video">  
  29.             <TreeItem nodeId="6" label="Hindi Music" />  
  30.             <TreeItem nodeId="7" label="English Music" />  
  31.         </TreeItem>  
  32.       </TreeItem>  
  33.     </TreeView>  
  34.     </>  
  35.   );  
  36. }  
 Now open app.js file and add the following code. 
  1. import React from 'react';  
  2. import logo from './logo.svg';  
  3. import './App.css';  
  4. import Treeview from './Treeview'  
  5. function App() {  
  6.   return (  
  7.     <div className="App">  
  8.       <Treeview/>  
  9.     </div>  
  10.   );  
  11. }  
  12.   
  13. export default App;  
Now run the project  by using 'npm start' and check the result.
 
How To Add TreeView Component In React Application
Now let's add another component 'Treeviewdemo.js' file. In this component we add divider in treeview component. Open Treeviewdemo.js and add the following code.
  1. import React from 'react';  
  2. import { makeStyles } from '@material-ui/core/styles';  
  3. import List from '@material-ui/core/List';  
  4. import ListItem from '@material-ui/core/ListItem';  
  5. import ListItemText from '@material-ui/core/ListItemText';  
  6. import Divider from '@material-ui/core/Divider';  
  7. import TreeView from '@material-ui/lab/TreeView';  
  8. import ExpandMoreIcon from '@material-ui/icons/ExpandMore';  
  9. import ChevronRightIcon from '@material-ui/icons/ChevronRight';  
  10. import TreeItem from '@material-ui/lab/TreeItem';  
  11. import AppBar from '@material-ui/core/AppBar';  
  12. import Toolbar from '@material-ui/core/Toolbar';  
  13.   
  14. export default function Treeviewdemo() {  
  15.   
  16.   return (  
  17.           <>  
  18.         <AppBar position="static">  
  19.         <Toolbar style={{ 'paddingLeft'"600px" }}>  
  20.         Tree View Component in React Application   
  21.         </Toolbar>  
  22.         </AppBar>  
  23.        <List component="nav"  aria-label="mailbox folders">  
  24.       <ListItem button>  
  25.       <TreeView  
  26.       defaultCollapseIcon={<ExpandMoreIcon />}  
  27.       defaultExpandIcon={<ChevronRightIcon />}  
  28.       >  
  29.       <TreeItem nodeId="1" label="Music">  
  30.         <TreeItem nodeId="2" label="Audio">  
  31.             <TreeItem nodeId="3" label="Hindi Music" />  
  32.             <TreeItem nodeId="4" label="English Music" />  
  33.         </TreeItem>  
  34.         <TreeItem nodeId="5" label="Video">  
  35.             <TreeItem nodeId="6" label="Hindi Music" />  
  36.             <TreeItem nodeId="7" label="English Music" />  
  37.         </TreeItem>  
  38.       </TreeItem>  
  39.     </TreeView>  
  40.       </ListItem>  
  41.       <Divider />  
  42.       <ListItem button divider>  
  43.       <TreeView  
  44.       defaultCollapseIcon={<ExpandMoreIcon />}  
  45.       defaultExpandIcon={<ChevronRightIcon />}  
  46.       >  
  47.       <TreeItem nodeId="1" label="Demo">  
  48.         <TreeItem nodeId="2" label="Audio">  
  49.             <TreeItem nodeId="3" label="Hindi Music" />  
  50.             <TreeItem nodeId="4" label="English Music" />  
  51.         </TreeItem>  
  52.         <TreeItem nodeId="5" label="Video">  
  53.             <TreeItem nodeId="6" label="Hindi Music" />  
  54.             <TreeItem nodeId="7" label="English Music" />  
  55.         </TreeItem>  
  56.       </TreeItem>  
  57.     </TreeView>  
  58.       </ListItem>  
  59.       <ListItem button>  
  60.         <ListItemText primary="Demo" />  
  61.       </ListItem>  
  62.       <Divider light />  
  63.        
  64.     </List>  
  65.     </>  
  66.   );  
  67. }  
Run the project by using 'npm start' and check the result.
 
How To Add TreeView Component In React Application

Summary

 
In this article, we learned how we add create functionality in React applications using React material UI.