reactjsmaterial-uicreate-react-app

React Material Treeview, Tree node not expanding on click when using expanded property


MUI Treeview (v6) component is not expanding on manual click when using expanded property, not sure why it's not expanding

<TreeView
  defaultCollapseIcon={<MinusSquare />}
  expanded={nodeIds}
  selected={selected}
  defaultExpandIcon={<PlusSquare />}
  defaultEndIcon={<CloseSquare />}
>
  {GetTreeItemsFromData(props.treeItems)}
</TreeView>

Solution

  • Based on the docs of the TreeView API from the Material-UI, to expand a node, you need to pass in the node IDs as an array into either expanded or defaultExpanded.

    So if you can't expand a node, perhaps you can try to see if nodeIds is an array in the state first, then see if you properly trigger a re-render when you update the state. If not, then probably you have not yet set up this state properly.

    Reference: TreeView API