Open In App

ReactJS Blueprint Tree Component

BlueprintJS is a React-based UI toolkit for the web. This library is very optimized and popular for building interfaces that are complex data-dense for desktop applications. Tree Component provides a way for users to display tree-structured data. It is used to show hierarchical data. We can use the following approach in ReactJS to use the ReactJS Blueprint Tree Component.

Tree Props:



TreeNode Props:

Creating React Application And Installing Module:



Step 1: Create a React application using the following command:

npx create-react-app foldername

Step 2: After creating your project folder i.e. foldername, move to it using the following command:

cd foldername

Step 3: After creating the ReactJS application, Install the required module using the following command:

npm install @blueprintjs/core

Project Structure: It will look like the following.

Project Structure

Example 1: Now write down the following code in the App.js file. Here we have demonstrated the Tree Component with the Icon element applied to it.




import React from 'react'
import '@blueprintjs/core/lib/css/blueprint.css';
import { Tree, Classes } from "@blueprintjs/core";
 
function App() {
 
    // Sample Tree Data
    const sampleData = [
        {
            id: 0,
            hasCaret: true,
            icon: "folder-close",
            label: "Folders"
        },
        {
            id: 1,
            hasCaret: true,
            icon: "user",
            label: "Profile"
        },
        {
            id: 2,
            hasCaret: true,
            icon: "folder-close",
            label: "Documents"
        },
    ];
 
    return (
        <div style={{
            display: 'block', width: 700, padding: 30
        }}>
            <h4>ReactJS Blueprint Tree Component</h4>
            <Tree
                contents={sampleData}
                className={Classes.ELEVATION_0}
            />
        </div>
    );
}
 
export default App;

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Example 2: Now write down the following code in the App.js file. Here,  we have demonstrated the Tree Component without the Icon element applied to it.




import React from 'react'
import '@blueprintjs/core/lib/css/blueprint.css';
import { Tree, Classes } from "@blueprintjs/core";
 
function App() {
 
    // Sample Tree Data
    const sampleData = [
        { id: 0, label: "Songs" },
        { id: 1, label: "Pictures" },
        { id: 2, label: "Calendar" },
        { id: 4, label: "Album" },
        { id: 5, label: "Themes" },
    ];
 
    return (
        <div style={{
            display: 'block', width: 300, padding: 30
        }}>
            <h4>ReactJS Blueprint Tree Component</h4>
            <Tree
                contents={sampleData}
                className={Classes.ELEVATION_0}
            />
        </div>
    );
}
 
export default App;

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Example 3: Now write down the following code in the App.js file. Here, Here we have demonstrated the Nested Tree Component which is expanded on click of the tree node.




import React from 'react'
import '@blueprintjs/core/lib/css/blueprint.css';
import { Tree, Classes } from "@blueprintjs/core";
 
function App() {
 
    const [isOpen, setIsOpen] = React.useState(false)
 
    // Sample Tree Data
    const sampleData = [
        {
            id: 5, label: "Click to expand Folders",
            isExpanded: isOpen,
            childNodes: [
                {
                    id: 0,
                    icon: "folder-close",
                    label: "Folders"
                },
                {
                    id: 1,
                    icon: "user",
                    label: "Profile"
                },
                {
                    id: 2,
                    icon: "folder-close",
                    label: "Documents"
                }
            ]
        },
    ];
 
    return (
        <div style={{
            display: 'block', width: 300, padding: 30
        }}>
            <h4>ReactJS Blueprint Tree Component</h4>
            <Tree
                contents={sampleData}
                className={Classes.ELEVATION_0}
                onNodeClick={()=>setIsOpen(true)}
            />
        </div>
    );
}
 
export default App;

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Reference: https://blueprintjs.com/docs/#core/components/tree


Article Tags :