🌐
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.
🌐
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;
🌐
GitHub
github.com › mui › material-ui › issues › 18092
TreeView: Provide prop so that all items are expanded by default · Issue #18092 · mui/material-ui
My initial implementations delivered the defaultExpanded array after the TreeView was rendered so it had no effect. ... 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 ...
🌐
GitHub
github.com › mui › material-ui › issues › 18432
[TreeView] Expand all nodes · Issue #18432 · mui/material-ui
May 13, 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
🌐
Telerik
telerik.com › components › treeview › expanding items › expanding all items
React TreeView Expanding Items Expanding All Items - KendoReact
To enable the expand-all mode for its items, set the expanded field of each TreeView node to true. To enable the collapse-all mode for its items, set the expanded field of each TreeView node to a falsy value.
🌐
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®
January 28, 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.
🌐
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 - mui / mui-x Public · There was an error while loading. Please reload this page. Notifications · You must be signed in to change notification settings · Fork 1.8k · Star 5.6k · New issueCopy link · New issueCopy link · Closed · Closed · [tree view] Provide prop so that all items are expanded by default#14509 · Copy link · Labels · discussionscope: tree viewChanges related to the tree view. This includes TreeView, TreeItem.Changes related to the tree view.
Author   sdpollack
🌐
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
An update of your code, that shows Node ID 9 and expands all its parents only, by default. 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 />} defaultExpan
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.

🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › react-mui-treeview-api
React MUI TreeView API - GeeksforGeeks
July 23, 2025 - sx(Array<func/object/bool> func/object): The system prop allows defining system overrides as well as additional CSS styles. CSS Rules: root: It is the style applied to the root element. Its global class name is .MuiTreeView-root. Syntax: Create the TreeView as follows: <TreeView defaultCollapseIcon={<ExpandMoreIcon />} defaultExpandIcon={<ChevronRightIcon />}> <TreeItem nodeId="1" label="Applications"> <TreeItem nodeId="2" label="Calendar" /> </TreeItem> </TreeView> Installing and Creating React app and adding the MUI dependencies.
Find elsewhere
🌐
MUI
mui.com › x › react-tree-view › tree-item-customization
Tree Item Customization - MUI X
The demo below shows how to introduce a new element that expands and collapses the TreeItem. ... <RichTreeView items={MUI_X_PRODUCTS} defaultExpandedItems={['grid']} slots={{ item: CustomTreeItem }} />
🌐
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).
🌐
GitHub
github.com › mui › mui-x › issues › 9960
[TreeView] Expand control at TreeItem level · Issue #9960 · mui/mui-x
February 7, 2020 - Both of these features can be solved by adding defaultExpanded and expanded props to TreeItem itself, that would override whatever dictated by the root TreeView. If this is not technically possible, it would be geat to at least have a ...
Author   OoDeLally
🌐
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> ... Expansion is controlled when its parent manages it by providing a expandedItems prop. Expansion is uncontrolled when it is managed by the component's own internal state. This state can be initialized using the defaultExpandedItems prop.
🌐
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.
🌐
Telerik
telerik.com › forums › treeview-expand-all-nodes-by-default
TreeView-expand all nodes by default in KendoReact | Telerik Forums
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.
🌐
GitHub
github.com › mui › mui-x › issues › 11850
[Treeview] Expand all parent nodes when adding child node id to the defaultExpanded property · Issue #11850 · mui/mui-x
October 3, 2023 - mui / mui-x Public · There was an error while loading. Please reload this page. Notifications · You must be signed in to change notification settings · Fork 1.7k · Star 5.5k · New issueCopy link · New issueCopy link · Closed · Closed · [Treeview] Expand all parent nodes when adding child node id to the defaultExpanded property#11850 ·
Author   doorman02
Top answer
1 of 1
2

The TreeView is "uncontrolled" by default meaning that the component will handle the state for you, and you as a consumer get whatever default behavior MUI set. In order to achieve what you want you'll need to make the TreeView a "controlled" component. There is an example of using the controlled component here: https://mui.com/components/tree-view/#controlled-tree-view

The example on the MUI page is doing more than just handling the expanding/collapsing of nodes. In the simplest form you need to explicitly handle the "expanded" prop that gets passed into the TreeView yourself.

const Tree = () => {
  // nodes "1", "1.1", "2" will start expanded
  const [expanded,setExpanded] = useState(["1","1.2","2"]);

  const createHandler = (id) => () => {
    // if node was collapsed, add to expanded list
    if(!expanded.includes(id)) {
      setExpanded([...expanded,id]);
    } else {
      // remove clicked node, and children of clicked node from expanded list
      setExpanded(expanded.filter(i => i !== id && !i.startsWith(`${id}.`));
    }
  };

  return (
    <TreeView expanded={expanded}>
      <TreeItem nodeId="1" label="A" onClick={createHandler("1")}>
        <TreeItem nodeId="1.1" label="B" />
        <TreeItem nodeId="1.2" label="C" onClick={createHandler("1.2")}>
          <TreeItem nodeId="1.2.1" label="D" />
        </TreeItem>
      </TreeItem>
      <TreeItem nodeId="2" label="E" onClick={createHandler("2")}>
        <TreeItem nodeId="2.1" label="F" />
      </TreeItem>
    </TreeView>
  );
}

Note: there be some mistakes in the above example, I'm unable to run it at the moment to verify correctness.

🌐
GitHub
github.com › mui › material-ui › issues › 17705
TreeView - Expand TreeItem manually · Issue #17705 · mui/material-ui
June 12, 2018 - 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. 👍React with 👍2oliviertassinari and Andrew-Stubbs ... scope: tree viewChanges related to the tree view. This includes TreeView, TreeItem.Changes related to the tree view.
Author   pimmee
🌐
DevExtreme
js.devexpress.com › Documentation › Guide › UI_Components › TreeView › Expand_and_Collapse_Nodes
Expand and Collapse Nodes - DevExtreme - DevExpress
TreeView · How To · Expand and Collapse Nodes · Documentation · UI Components · TreeView · How To · Expand and Collapse Nodes · v25.2 · v25.2 v25.1 v24.2 v24.1 v23.2 v23.1 v22.2 v22.1 v21.2 v21.1 v20.2 v20.1 v19.2 · The page you are viewing does not exist in version 19.2.