前提:
当左侧切换重新加载数据的时候,tree已展开的字段 三角图标会消息
代码如下:
<Tree
showIcon
onSelect={onSelectTree}
loadData={onLoadData}
treeData={treeData}
/>
解决如下
添加key,每次数据加载回来,刷新key即可刷新tree
// tree标识key
const [uuid, setUuid] = useState<string>(createUUID());
// 获取列表
const getCatalog = async (id: string) => {
const { msg, status, desc } = await getList({ id });
setUuid(createUUID());//每次获取列表刷新key
if (status < 20000) {
.......
} else {
message.warning(desc);
}
};
<Tree
key={uuid}
showIcon
onSelect={onSelectTree}
loadData={onLoadData}
treeData={treeData}
/>
UUID创建如下:
// 生成唯一id
export const createUUID = () => {
let d = new Date().getTime();
const uuid = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, (c: any) => {
const r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16);
});
return uuid;
};