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

Answer from Ido on Stack Overflow
🌐
GitHub
github.com › mui › material-ui › issues › 18432
[TreeView] Expand all nodes · Issue #18432 · mui/material-ui
November 18, 2019 - I have tried feeding the "defaultExpanded" prop a new list that has all of the nodes in it but it doesn't seem to cause the nodes to expand as I had expected. 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).
Author   ogmiosnetworks
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

reactjs - Material-ui - Treeview Expand all - Stack Overflow
Using material-ui/lab 4.0.0-alpha.27 - TreeView Component I have a treeview with a couple of hundred nodes. I have added a textfield before the treview that the user can use to search/filter the t... More on stackoverflow.com
🌐 stackoverflow.com
How to get a treeview to remain expanded
Forum Thread - Treeview - How to get a treeview to remain expanded. Browse other users' questions about our React - EJ 2 Treeview components. More on syncfusion.com
🌐 syncfusion.com
5
5
May 21, 2020
How to set expanded prop for material ui TreeView component conditionally
I have a material-ui TextField and TreeView components one below the other. Initially I want the tree view to be collapsed and user should be able to manually expand it. But when the user types some text in the text field, I want the nodes that have similar text to be expanded. I have the code to search for text in the tree and get the node ids for matching nodes. There is a prop called expanded that allows ... More on stackoverflow.com
🌐 stackoverflow.com
reactjs - Expand using nodeId to the desired node in material ui TreeView when tree view reload - Stack Overflow
In this example when treeView reloads it should be expanded to nodeId 9 and only expands the parent of nodeId 9. should not expand all nodes. import TreeView from '@material-ui/lab/TreeView'; import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; import ChevronRightIcon from ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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.
🌐
Stack Overflow
stackoverflow.com › questions › 58840396 › material-ui-treeview-expand-all
reactjs - Material-ui - Treeview Expand all - Stack Overflow
I have tried feeding the "defaultExpanded" prop a new list that has all of the nodes in it but it doesn't seem to cause the nodes to expand as I had expected. 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).
🌐
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.
🌐
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 have validated your requirements in TreeView component. We can expand the TreeView component all nodes using expandAll method. We can use this expandAll method after loading the all tree node in the DOM.
🌐
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
Find elsewhere
🌐
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);
🌐
MUI
mui.com › x › react-tree-view › simple-tree-view › items
Simple Tree View - Items - MUI X
Mouse or keyboard interactions will not expand or collapse disabled items.
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › react-mui-treeview-api
React MUI TreeView API - GeeksforGeeks
July 23, 2025 - import './App.css' import * as React from 'react' import TreeView from '@mui/lab/TreeView' import TreeItem from '@mui/lab/TreeItem' import ExpandMoreIcon from '@mui/icons-material/ExpandMore' import ChevronRightIcon from '@mui/icons-material/ChevronRight' import Box from '@mui/material/Box' import Button from '@mui/material/Button' function App() { const [expanded, setExpanded] = React.useState([]) const [selected, setSelected] = React.useState([]) const handleToggle = (event, nodeIds) => { setExpanded(nodeIds) } const handleSelect = (event, nodeIds) => { setSelected(nodeIds) } const handleExpandClick = () => { setExpanded((oldExpanded) => oldExpanded.length === 0 ?
🌐
Mui
v5-0-6.mui.com › components › tree-view
Tree View React component - MUI
Tree views can be used to represent a file system navigator displaying folders and files, an item representing a folder can be expanded to reveal the contents of the folder, which may be files, folders, or both. ... <TreeView aria-label="file system navigator" defaultCollapseIcon={<ExpandMoreIcon />} defaultExpandIcon={<ChevronRightIcon />} sx={{ height: 240, flexGrow: 1, maxWidth: 400, overflowY: 'auto' }} > <TreeItem nodeId="1" label="Applications"> <TreeItem nodeId="2" label="Calendar" /> </TreeItem> <TreeItem nodeId="5" label="Documents"> <TreeItem nodeId="10" label="OSS" /> <TreeItem nodeId="6" label="MUI"> <TreeItem nodeId="8" label="index.js" /> </TreeItem> </TreeItem> </TreeView>
🌐
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
🌐
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
🌐
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. I missed it. I've spend a great deal of time trying to get the defaultExpanded working as I need to show all items. This is a challenge since using useState in a recursive function appears to be problematic. I'm going to guess others need the TreeView to start fully expanded as I do.
Author   sdpollack
🌐
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!
🌐
MUI
mui.com › x › react-tree-view
Tree View React component - MUI X
The Tree View components let users navigate hierarchical lists of data with nested levels that can be expanded and collapsed.
🌐
MUI
mui.com › x › react-tree-view › simple-tree-view › customization
Simple Tree View - Customization - MUI X
Use the collapseIcon slot, the expandIcon slot and the defaultEndIcon prop to customize SimpleTreeView icons. The demo below shows how to add icons using both an existing icon library, such as Material Icons, and creating an icon from scratch using Material UI's SvgIcon.
🌐
HomieDevs
homiedevs.com › example › material-ui-treeview-expand-all
material-ui treeview expand all code example
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> void Button_Command(Object sender, CommandEventArgs e) { switch(e.CommandName) { case "Expand": LinksTreeView.ExpandAll(); break; case "Collapse": LinksTreeView.CollapseAll(); break; default: // Do nothing. break; } } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>TreeView ExpandAll and CollapseAll Example</title> </head> <body> <form id="form1" runat="server"> <h3>TreeView ExpandAll and
🌐
MUI
mui.com › x › react-tree-view › rich-tree-view › expansion
Rich Tree View - Expansion - MUI X
<div> <Button onClick={handleExpandClick}> {expandedItems.length === 0 ? 'Expand all' : 'Collapse all'} </Button> </div> <Box sx={{ minHeight: 352, minWidth: 250 }}> <RichTreeView items={MUI_X_PRODUCTS} expandedItems={expandedItems} onExpandedItemsChange={handleExpandedItemsChange} /> </Box>