Probably you didn't set the callback when you try to control it. (Same as TextField's value and onChange)
TreeView API document here
You can find there the callback onNodeToggle
Set it will fix this problem.

Answer from keikai on Stack Overflow
Top answer
1 of 3
7

You can achieve that using TreeView expanded prop.
The code below expands the TreeItem with id "1" on mount.

import React, {useEffect, useState} from 'react';
import TreeView from '@material-ui/lab/TreeView';
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
import ChevronRightIcon from '@material-ui/icons/ChevronRight';
import TreeItem from '@material-ui/lab/TreeItem';

export default function FileSystemNavigator() {
  const [expanded, setExpanded] = useState([]);

  useEffect(() => {
    setExpanded(["1"])
  },[])

  return (
    <TreeView
      defaultCollapseIcon={<ExpandMoreIcon />}
      defaultExpandIcon={<ChevronRightIcon />}
      expanded={expanded}
    >
      <TreeItem nodeId="1" label="Applications"    
>
        <TreeItem nodeId="2" label="Calendar" />
        <TreeItem nodeId="3" label="Chrome" />
        <TreeItem nodeId="4" label="Webstorm" />
      </TreeItem>
      <TreeItem nodeId="5" label="Documents"
>
        <TreeItem nodeId="6" label="Material-UI">
          <TreeItem nodeId="7" label="src">
            <TreeItem nodeId="8" label="index.js" />
            <TreeItem nodeId="9" label="tree-view.js" />
          </TreeItem>
        </TreeItem>
      </TreeItem>
    </TreeView>
  );
}

Code Sandbox

2 of 3
4

This answer is for Riham Nour Question in the comment section of this answer (won't be able to reply in comments due to less reputation).

what if I only want the selected parent TreeItem to be selected while collapsing the others? like when I select node 1, then node 6 collapses ?

Just add the nodeId of the selected nodes then you can easily be able to get the desired outcome. You can go through the code Code SandBox.

PS: I hope I understood the question correctly and Sorry I added this in the answer section. Please let me know if there is any better way to communicate the answer to Riham.

Discussions

TreeView - Removing expand/collapse icon when there are no children
hi, did you ever find a solution to this? I am looking to implement this too right now :) More on reddit.com
🌐 r/AskProgramming
1
1
January 11, 2020
reactjs - Expand using nodeId to the desired node in material ui TreeView when tree view reload - Stack Overflow
import TreeView from '@material-ui/lab/TreeView'; import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; import ChevronRightIcon from '@material-ui/icons/ChevronRight'; import TreeItem from '@material-ui/lab/TreeItem'; export default function FileSystemNavigator() { const [expanded, ... More on stackoverflow.com
🌐 stackoverflow.com
[tree view] Provide prop so that all items are expanded by default
I have searched the issues of this repository and believe that this is not a duplicate. Summary 💡 I would appreciate an easier way to have all children expanded by default. Seems like the current &... More on github.com
🌐 github.com
5
October 29, 2019
TreeView - Expand TreeItem manually
I'm wondering if there is a way to open TreeItem manually? If I have a node "Add item" with a child node "Items", is there a way to expand "Items" once a new item ... More on github.com
🌐 github.com
5
October 4, 2019
🌐
GitHub
github.com › mui › material-ui › issues › 18432
[TreeView] Expand all nodes · Issue #18432 · mui/material-ui
November 18, 2019 - The defaultExpanded prop only seems to be respected when the tree initially draws. I am currently working around this by looking for collapsed nodes and firing click events for them to force them to open but that is causing issues (the textfield looses focus and the keyboard hides and the treeview jumps around). Is there a recommended way to dynamically expand all of the nodes in a TreeView?
Author   ogmiosnetworks
🌐
MUI
mui.com › x › react-tree-view › simple-tree-view › expansion
Simple Tree View - Expansion - MUI X
The expansion is controlled when its parent manages it by providing a expandedItems prop. The expansion is uncontrolled when it's managed by the component's own internal state. This state can be initialized using the defaultExpandedItems prop.
🌐
Reddit
reddit.com › r/askprogramming › treeview - removing expand/collapse icon when there are no children
r/AskProgramming on Reddit: TreeView - Removing expand/collapse icon when there are no children
January 11, 2020 -

I'm building a TreeView in `react` with the Treeview component from Material UI: https://material-ui.com/components/tree-view/

I have created the component below which fetches data when a node is expanded. Furthermore, the tree is build so each node that have children also is a tree of MyTreeItem
, but I have one question:

When I reach a point where there are no more children, I want to remove/hide the expand/collapse icon. How can i achieve this?

import TreeView from "@material-ui/lab/TreeView";
import ExpandMoreIcon from "@material-ui/icons/ExpandMore";
import ChevronRightIcon from "@material-ui/icons/ChevronRight";
import TreeItem from "@material-ui/lab/TreeItem";
const { useState, useCallback } = React;

export default function MyTreeItem(props) {
  const [childNodes, setChildNodes] = useState(null);
  const [expanded, setExpanded] = React.useState([]);

  function fetchChildNodes(id) {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve({
          children: [
            {
              id: "2",
              name: "Calendar"
            },
            {
              id: "3",
              name: "Settings"
            },
            {
              id: "4",
              name: "Music"
            }
          ]
        });
      }, 1000);
    });
  }

const handleChange = (event, nodes) => {
    const expandingNodes = nodes.filter(x => !expanded.includes(x));
    setExpanded(nodes);
    if (expandingNodes[0]) {
      const childId = expandingNodes[0];
      fetchChildNodes(childId).then(
        result =>
          result.children
            ? setChildNodes(
                result.children.map(node => (
                  <MyTreeItem key={node.uuid} {...node} action={props.action} />
                ))
              )
            : console.log("No children") // How do I remeove the expand/collapse icon?
      );
    }
  };

  return (
    <TreeView
      defaultCollapseIcon={<ExpandMoreIcon />}
      defaultExpandIcon={<ChevronRightIcon />}
      expanded={expanded}
      onNodeToggle={handleChange}
    >
      {/*The node below should act as the root node for now */}
      <TreeItem nodeId={props.id} label={props.name}>
        {childNodes || [<div key="stub" />]}
      </TreeItem>
    </TreeView>
  );
}
const rootElement = document.getElementById("root");
ReactDOM.render(<MyTreeItem id="1" name="Applications" />, rootElement);
🌐
GitHub
github.com › mui › material-ui › issues › 18092
TreeView: Provide prop so that all items are expanded by default · Issue #18092 · mui/material-ui
October 29, 2019 - I can't believe is 2022 and the MUI still does not provide a way to have the treeview items expanded by default or successfully draw treeview lines appropriately. Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment ... TreeView, TreeItem. This is the name of the generic UI component, not the React module!
🌐
Stack Overflow
stackoverflow.com › questions › 69988012 › expand-using-nodeid-to-the-desired-node-in-material-ui-treeview-when-tree-view-r
reactjs - Expand using nodeId to the desired node in material ui TreeView when tree view reload - Stack Overflow
import * as React from 'react'; import TreeView from '@mui/lab/TreeView'; import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; import ChevronRightIcon from '@mui/icons-material/ChevronRight'; import TreeItem from '@mui/lab/TreeItem'; // import {useState, useEffect} from 'react'; export default function FileSystemNavigator() { // const [expanded, setExpanded] = useState([]); // useEffect(() => { // setExpanded(["1"]) // },[]) return ( <TreeView defaultCollapseIcon={<ExpandMoreIcon />} defaultExpandIcon={<ChevronRightIcon />} defaultExpanded={['5','6','7']} // expanded={expanded} > <Tree
🌐
GitHub
github.com › mui › mui-x › issues › 14509
[tree view] Provide prop so that all items are expanded by default · Issue #14509 · mui/mui-x
October 29, 2019 - Then 'defaultExpanded' would be available and could be implemented as the default 'true' or 'false' default expanded state for all child TreeItems. If there is already a way to accomplish this, it should be better described in the documentation.
Author   sdpollack
Find elsewhere
🌐
GitHub
github.com › mui › material-ui › issues › 17705
TreeView - Expand TreeItem manually · Issue #17705 · mui/material-ui
October 4, 2019 - If I have a node "Add item" with a child node "Items", is there a way to expand "Items" once a new item is added? What is the recommended way of achieving what I want? Here is a Codesandbox that describe the functionality I'm looking for. ... scope: tree viewChanges related to the tree view. This includes TreeView, TreeItem.Changes related to the tree view.
Author   pimmee
🌐
Telerik
telerik.com › components › treeview › expanding items › expanding all items
React TreeView Expanding Items Expanding All Items - KendoReact
While the TreeView does not provide a built-in feature for expanding and collapsing all its items at once, the component supports such an implementation.
🌐
Syncfusion
syncfusion.com › forums › 154461 › how-to-get-a-treeview-to-remain-expanded
How to get a treeview to remain... | React - EJ 2 Forums | Syncfusion®
May 21, 2020 - We were unable to reproduce it from our end with your provided information. TreeView component will remain expanded even after page refresh, when setting expanded field as true in the datasource property.
🌐
Stack Overflow
stackoverflow.com › questions › 58840396 › material-ui-treeview-expand-all
reactjs - Material-ui - Treeview Expand all - Stack Overflow
I am currently working around this ... treeview jumps around). I need something a bit smoother. ... You need to use the expanded prop, instead of defaultExpanded....
🌐
Stack Overflow
stackoverflow.com › questions › 70899118 › how-to-make-a-treeview-expanded-by-default
How to make a TreeView expanded by default?
nodes.children.map((node) => renderTree(node)) : null} </CustomTreeItem> ); switch (data.state) { case apiStates.ERROR: return <p>ERROR: {data.error || "General error"}</p>; case apiStates.SUCCESS: const batimentsTreeViews = data.data.map((bat) => { var b = []; for (const key in bat) { b = { id: key, ...bat[key], }; } return renderTree(b); }); handleExpandAll(data.data.length); return ( <TreeView aria-label="controlled" defaultCollapseIcon={<ExpandMoreIcon />} defaultExpandIcon={<ChevronRightIcon />} expanded={expanded} selected={props.selectedId} onNodeToggle={handleToggle} onNodeSelect={handleSelect} sx={{ flexGrow: 1, width: "100%", maxWidth: "none", overflowY: "auto", }} > {batimentsTreeViews} </TreeView> ); default: return <LoadingDiv />; } } export default TreeNavigation;
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › react-mui-treeview-api
React MUI TreeView API - GeeksforGeeks
July 23, 2025 - defaultExpandIcon(node): It is the default icon used to expand it.
🌐
GitHub
github.com › mui › material-ui › issues › 19953
[TreeView] Expand/collapse node only when clicking on expand/collapse icon · Issue #19953 · mui/material-ui
March 3, 2020 - I have searched the issues of this repository and believe that this is not a duplicate. Summary 💡 When I click on a tree item that has children, the node expands, wherever I click on the line. While this behavior might be ok for most use...
Author   LaurianeDPX
🌐
Lib4dev
lib4dev.in › info › fiffty › react-treeview-mui › 69370609
lib4dev.in
best javascript data visualization library,download python machine learning library,android ui component library tutorial
🌐
C# Corner
c-sharpcorner.com › article › how-to-add-treeview-component-in-react-application
How To Add TreeView Component In React Application
February 12, 2020 - import TreeView from '@material-ui/lab/TreeView'; import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; import ChevronRightIcon from '@material-ui/icons/ChevronRight'; import TreeItem from '@material-ui/lab/TreeItem'; import AppBar from '@material-ui/core/AppBar'; import Toolbar from '@material-ui/core/Toolbar'; export default function Treeview() { return ( <> <AppBar position="static"> <Toolbar style={{ 'paddingLeft': "600px" }}> Tree View Component in React Application ·
🌐
MUI
mui.com › x › react-tree-view › tree-item-customization
Tree Item Customization - MUI X
By default, a TreeItem is expanded when the user clicks on its contents. You can change the expansion trigger using the expansionTrigger prop on the iconContainer.
🌐
GitHub
github.com › mui › material-ui › issues › 24369
Customized tree view - Material UI issue · Issue #24369 · mui/material-ui
January 11, 2021 - I have created a tree using "Customized tree view - material UI". However, I'm facing a challenge: defaultEndIcon state is getting lost on accordion collapse and then expand: Selected defaultEndIco...
Author   ayupur
🌐
GitHub
github.com › mui-org › material-ui › issues › 22024
[TreeView] only to expand or collapse (onNodeToggle) when clicking on Icon but not select any particular node (onNodeSelect) · Issue #15467 · mui/mui-x
August 1, 2020 - mui/material-ui#22846mui/material-ui#22846 · Closed · [TreeView] only to expand or collapse (onNodeToggle) when clicking on Icon but not select any particular node (onNodeSelect)#15467 · mui/material-ui#22846 · Copy link · Assignees · Labels · designThis is about UI or UX design, please involve a designer.This is about UI or UX design, please involve a designer.scope: tree viewChanges related to the tree view.
Author   flora8984461