【发布时间】:2022-10-20 22:38:00
【问题描述】:
我有一个关于输入 React Router 的问题。 我已经声明了一个接口 IRoute 来输入路由
import Event from "../pages/Event";
import Login from "../pages/Login";
export enum RouteNames {
LOGIN = "./login",
EVENT = "/",
}
export interface IRoute {
path: string;
element: React.ComponentType;
}
export const publicRoutes: IRoute[] = [
{ path: RouteNames.LOGIN, element: Login },
];
export const privateRoutes: IRoute[] = [
{ path: RouteNames.EVENT, element: Event },
];
然后在 AppRouter 组件中,我想遍历 privateRoutes 和 publicRoutes:
const AppRouter: FC = () => {
const isAuth = true;
return (
<div>
{isAuth ? (
<Routes>
{privateRoutes.map((route) => (
<Route path={route.path} element={route.element} />
))}
</Routes>
) : (
<Routes>
{publicRoutes.map((route) => (
<Route path={route.path} element={route.element} /> // throws an error here
))}</Routes>
)}
</div>
我收到以下错误:
Type 'ComponentType<{}>' is not assignable to type 'ReactNode'.
Type 'ComponentClass<{}, any>' is not assignable to type 'ReactNode'.ts(2322)
components.d.ts(52, 5): The expected type comes from property 'element' which is declared here on type 'IntrinsicAttributes & (PathRouteProps | LayoutRouteProps | IndexRouteProps)'
谁能帮我在这里正确输入所有内容?谢谢!
【问题讨论】:
-
你能分享
publicRoutes和privateRoutes数组吗? -
他们在第一个sn-p
标签: reactjs typescript react-router