递归地将嵌套对象输入到 React 功能组件的 return 语句内的树视图组件中
Recursively enter nested object into tree view component inside return statement of a React functional component
我有一个分层对象。看起来像这样:
{
id: 74,
parentId: null,
children: [
{
id: 62,
parentId: 74,
children: [{ id: 56, parentId: 62 }, { id: 63, parentId: 62 }],
},
{
id: 86,
parentId: 74,
children: [
{
id: 80,
parentId: 86,
children: [{ id: 81, parentId: 80 }, { id: 76, parentId: 80 }],
},
{ id: 87, parentId: 86 },
],
},
],
};
我想使用从 https://mui.com/material-ui/react-tree-view/ 获得的树视图模块在我的前端显示它
树视图的语法是这样的:
return (
<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>
);
}
如何遍历我的对象并将相应的值输入到此树语法中的父项和相应的子树项中?它应该是这样的:
<TreeItem nodeId="74" label="74">
<TreeItem nodeId="62" label="62">
<TreeItem nodeId="52" label="52"/>
<TreeItem />
.
.
.
<TreeItem />
你可以这样做
const renderTree = tree => {
return <TreeItem key={tree.id} nodeId={tree.id} label={tree.id}>
{tree.children && tree.children.map(renderTree)}
</TreeItem>
}
return (
<TreeView
aria-label="file system navigator"
defaultCollapseIcon={<ExpandMoreIcon />}
defaultExpandIcon={<ChevronRightIcon />}
sx={{ height: 240, flexGrow: 1, maxWidth: 400, overflowY: 'auto' }}
>
{renderTree(tree)}
</TreeView>
);
我有一个分层对象。看起来像这样:
{
id: 74,
parentId: null,
children: [
{
id: 62,
parentId: 74,
children: [{ id: 56, parentId: 62 }, { id: 63, parentId: 62 }],
},
{
id: 86,
parentId: 74,
children: [
{
id: 80,
parentId: 86,
children: [{ id: 81, parentId: 80 }, { id: 76, parentId: 80 }],
},
{ id: 87, parentId: 86 },
],
},
],
};
我想使用从 https://mui.com/material-ui/react-tree-view/ 获得的树视图模块在我的前端显示它 树视图的语法是这样的:
return (
<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>
);
}
如何遍历我的对象并将相应的值输入到此树语法中的父项和相应的子树项中?它应该是这样的:
<TreeItem nodeId="74" label="74">
<TreeItem nodeId="62" label="62">
<TreeItem nodeId="52" label="52"/>
<TreeItem />
.
.
.
<TreeItem />
你可以这样做
const renderTree = tree => {
return <TreeItem key={tree.id} nodeId={tree.id} label={tree.id}>
{tree.children && tree.children.map(renderTree)}
</TreeItem>
}
return (
<TreeView
aria-label="file system navigator"
defaultCollapseIcon={<ExpandMoreIcon />}
defaultExpandIcon={<ChevronRightIcon />}
sx={{ height: 240, flexGrow: 1, maxWidth: 400, overflowY: 'auto' }}
>
{renderTree(tree)}
</TreeView>
);