Antd tree defaultExpandAll problem

when antd tree sets defaultExpandAll to true, it expands by default the first time it is initialized. When the tree is collapsed, the window is closed and the window is opened again, the tree remains collapsed. Then I use expandedKeys to control the nodes that need to be expanded, but clicking on the nodes set by expandedKeys cannot be collapsed. May I ask how to solve the appeal problem? Thank you!

 <Tree
    checkable
    onCheck={this.onCheck}
    defaultExpandAll={expandAllState}
    checkedKeys={checkedkey}
    loadData={this.onLoadData}
    expandedKeys={expandedKeys}
  >
   {this.renderTreeNodes(roles)}
  </Tree>
Mar.06,2021

https://github.com/ant-design. look here, this is the best solution


after you set expandedKeys , the Tree component becomes a controlled component.
when you open the window again, you need to set expandedKeys to open it all.
in addition, the defaultExpandAll attribute is loaded only when the component is rendered first, and this value no longer works when it is mount.


you need to write an OnTreeExpand method to change his key value

Menu