const routes = [
{
path: "/",
element: <App />,
loader: authLoader,
children: [
{
errorElement: <ErrorPage />,
children: [
{
index: true,
title: "系统概况",
icon: <DashboardOutlined />,
element: <Dashboard />,
},
{
path: "stations/-7",
title: "运维管理",
icon: <ConsoleSqlOutlined />,
element: <Operation />,
children: [
{
path: "/stations/-7",
title: "巡检记录",
element: <AccountCenter />,
},
],
},
{
path: "stations/0",
title: "运维管理",
icon: <ConsoleSqlOutlined />,
element: <Operation />,
},
{
path: "form",
title: "表单页",
icon: <EditOutlined />,
element: <FormPage />,
},
{
path: "table",
title: "列表页",
icon: <TableOutlined />,
element: <TablePage />,
},
{
path: "detail",
title: "详情页",
icon: <BarsOutlined />,
element: <DetailPage />,
},
{
path: "account",
title: "个人页",
icon: <UserOutlined />,
children: [
{
path: "/account/center",
title: "个人中心",
element: <AccountCenter />,
},
{
path: "/account/settings",
title: "个人设置",
element: <AccountSettings />,
},
],
},
// {
// path: "*",
// element: <Navigate to="/" replace={true} />,
// },
],
},
],
},
{
path: "/login",
element: <LoginPage />,
},
];
// console.log(routes);
export { routes };
export default createBrowserRouter(routes);
这是我的路由表,现在是静态写的,如何根据后端返回的数据动态渲染路由表?在这里面不能使用hook, 我在别的组件可以这样获取到数据
import { useMenuStore } from "@stores/index";
const { menuInfo } = useMenuStore();
在路由表里面写过函数,使用hook获取数据,报错
警告:钩子调用无效。钩子只能在函数组件的主体内部调用。这可能是由于以下原因之一:
1.你可能有不匹配的React版本和渲染器(如React DOM)
2.你可能违反了钩子规则
3.同一个应用程序中可能有多个React副本
有没有其他的解决方案,能动态渲染就行,数据如下,树形结构的rows:[
{
"mid": null,
"name": null,
"link": null,
"path": "/stations/0",
"order": 999,
"parent": null,
"valid": null,
"mark": null,
"icon": "ant-design",
"style": null,
"id": 1,
"parentId": 0,
"key": "module_1",
"parentKey": null,
"text": "环境数据",
"children": [
{
"mid": null,
"name": null,
"link": null,
"path": "/stations/0",
"order": 999,
"parent": null,
"valid": null,
"mark": null,
"icon": "align-left",
"style": null,
"id": 4,
"parentId": 1,
"key": "module_4",
"parentKey": "module_1",
"text": "数据管理",
"children": [
{
"mid": null,
"name": null,
"link": null,
"path": "/stations/0",
"order": 992,
"parent": null,
"valid": null,
"mark": null,
"icon": "book",
"style": null,
"id": 11,
"parentId": 4,
"key": "module_11",
"parentKey": "module_4",
"text": "实时数据",
"children": []
},