typescript 类型“LoadableComponent< unknown>”不能赋给类型“ReactNode”

kmpatx3s  于 2023-01-18  发布在  TypeScript
关注(0)|答案(1)|浏览(237)

我正在尝试将react-router-dom v5迁移到v6。

// react-router-dom v5
<Route exact path="/" component={router.Home} />

// react-router-dom v6
<Route path="/*" element={router.Home} /> // error

得到了这个错误
Type 'LoadableComponent<unknown>' is not assignable to type 'ReactNode'. Type 'ComponentClass<ExtraComponentProps, any> & LoadableComponentMethods<unknown>' is not assignable to type 'ReactNode'.
我该怎么修呢?
下面是我代码示例
routes.ts

import loadable from '@loadable/component';

export const Home = loadable(() => import('./pages/Home'));

App.tsx

import * as router from 'src/routes';

<Route path="/*" element={router.Home} /> // error

package.json

//devDependency
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.10",
"@types/react-router-dom": "^5.3.3",
"@types/loadable__component": "^5.13.4",

//dependency
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.6.2",
"@loadable/component": "^5.15.2",
qacovj5a

qacovj5a1#

代替

<Route path="/*" element={router.Home} /> // error

用途

const {Home} = router;
...
<Route path="/*" element={<Home />} />

相关问题