Pra*_*are 1 reactjs react-router async-components react-router-dom
我正在尝试使用createBrowserRouterReact Router v6 中的函数延迟加载路由元素,但我不断收到此错误:`位置“/admin/reports/enrollees”处的匹配叶路由没有元素或组件。这意味着默认情况下它将呈现空值,从而导致“空”页面。这是我的路线文件:
export default createBrowserRouter([
{
path: '/admin/reports',
children: [
{
path: '',
element: <Index />,
},
{
path: 'enrollees',
lazy: () => import('../../components/Reports/Enrollees'),
},
{
path: 'facilities',
lazy: () => import('../../components/Reports/Facilities'),
}
],
}
])
Run Code Online (Sandbox Code Playgroud)
我一开始尝试这样做:
export default createBrowserRouter([
{
path: '/admin/reports',
children: [
{
path: '',
element: <Index />,
},
{
path: 'enrollees',
element: lazy(() => import('../../components/Reports/Enrollees')),
},
{
path: 'facilities',
element: lazy(() => import('../../components/Reports/Facilities')),
}
],
}
])
Run Code Online (Sandbox Code Playgroud)
但我得到了错误:Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it.
小智 9
我遇到了同样的问题,我是这样解决的
{
path: "dashboard",
lazy: async () => {let { Dashboard } = await import("./pages/Dashboard")
return { Component: Dashboard }},
}
Run Code Online (Sandbox Code Playgroud)
这个奇怪的问题困扰了我几个小时,但我终于弄清楚了问题所在
首先确保您的组件名称是Component,然后使用命名导出而不是默认导出
例如。YourComponent.jsx
export function Component(){
return <div>something</div>
}
Run Code Online (Sandbox Code Playgroud)
路线对象
{
path: 'your-component',
lazy: () => import('./YourComponent'),
}
Run Code Online (Sandbox Code Playgroud)
如果您想保留组件名称并使用默认导出,您可以这样做
例如。YourComponent.jsx
export default function YourComponent(){
return <div>something</div>
}
Run Code Online (Sandbox Code Playgroud)
路线对象
{
path: 'your-component',
async lazy() {
let YourComponent = await import("./YourComponent");
return { Component: YourComponent.default };
},
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5381 次 |
| 最近记录: |