英文:
RouteChildrenProps is not an exported member
问题
我遇到了下面指向 'RouteChildrenProps' 的错误:
我只是尝试完成一个教程,但在这里卡住了。这是完整的代码:
import React from 'react';
import { Route, RouteChildrenProps, Routes } from 'react-router';
import routes from './config/routes';
export interface IApplicationProps {}
const Application: React.FunctionComponent<IApplicationProps> = (props) => {
return (
<Routes>
{routes.map((route, index) => {
return <Route
key={index}
exact={route.exact}
path={route.path}
render={(routeProps: RouteChildrenProps<any>) =>
<route.component {...routeProps} />} />;
}
)}
</Routes>
);
};
export default Application;
教程本身没有遇到这个问题。接下来我可以尝试什么?
英文:
I am getting the below error pointing to 'RouteChildrenProps':
I am just trying to get through a tutorial but I got stuck here. Here is the full code:
import React from 'react';
import { Route, RouteChildrenProps, Routes } from 'react-router';
import routes from './config/routes';
export interface IApplicationProps {}
const Application: React.FunctionComponent<IApplicationProps> = (props) => {
return (
<Routes>
{routes.map((route, index) => {
return <Route
key={index}
exact={route.exact}
path={route.path}
render={(routeProps: RouteChildrenProps<any>) =>
<route.component {...routeProps} />} />;
}
)}
</Routes>
);
};
export default Application;
The tutorial itself did not encounter this issue. What can I try next?
答案1
得分: 1
根据react-router插件,它导出的成员如下。它不包含 RouteChildrenProps
,这意味着您无法使用该属性。请检查您使用的 react-router 版本(在此答案中,我引用的是 react-router v6.11.2)。有时此属性可能已被弃用。
react-router/packages/react-router/index.ts
// 公开 react-router 的公共 API
export type {
ActionFunction,
ActionFunctionArgs,
AwaitProps,
Blocker as unstable_Blocker,
BlockerFunction as unstable_BlockerFunction,
DataRouteMatch,
DataRouteObject,
Fetcher,
Hash,
IndexRouteObject,
IndexRouteProps,
JsonFunction,
LazyRouteFunction,
LayoutRouteProps,
LoaderFunction,
LoaderFunctionArgs,
Location,
MemoryRouterProps,
NavigateFunction,
NavigateOptions,
NavigateProps,
Navigation,
Navigator,
NonIndexRouteObject,
OutletProps,
Params,
ParamParseKey,
Path,
PathMatch,
Pathname,
PathPattern,
PathRouteProps,
RedirectFunction,
RelativeRoutingType,
RouteMatch,
RouteObject,
RouteProps,
RouterProps,
RouterProviderProps,
RoutesProps,
Search,
ShouldRevalidateFunction,
To,
};
如果您想在应用程序中实现一个简单的路由,可以按照下面的机制进行操作(请记住,我使用了 TypeScript 方法):
import React from "react";
import { Navigate, RouterProvider, createBrowserRouter } from "react-router-dom";
import { Status } from "./interfaces/IAuth";
import CardPage from "./pages/CardPage";
import HomePage from "./pages/HomePage";
import ListPage from "./pages/ListPage";
import LoginPage from "./pages/Login";
import RegisterPage from "./pages/Register";
const protectedRouter = createBrowserRouter(
[
{
path: "/",
element: <HomePage />
},
{
path: "/list-component",
element: <ListPage />
},
{
path: "/card-page",
element: <CardPage />
},
],
{ basename: '/react-fundamentals' }
);
const publicRouter = createBrowserRouter(
[
{
path: "/",
element: <LoginPage />
},
{
path: "/register",
element: <RegisterPage />
},
{
path: "/home",
element: <HomePage />
},
{
path: "*",
element: <Navigate to="/" replace />
}
],
{ basename: '/react-fundamentals' }
);
const AppRouter: React.FC<{ status?: Status }> = ({ status }) => {
const selectedConfig = (status === 'authenticated') ? protectedRouter : publicRouter;
return (
<React.StrictMode>
<RouterProvider router={selectedConfig} />
</React.StrictMode>
)
}
export default AppRouter;
英文:
According to the react-router plugin, it's exported members as bellows. It does not contain RouteChildrenProps
, It means you are unable to use that property. Please check whether you are using which version of react-router you are using (in this answer I'm referring react-router v6.11.2). Sometimes now this property can be deprecated.
react-router/packages/react-router/index.ts
// Expose react-router public API
export type {
ActionFunction,
ActionFunctionArgs,
AwaitProps,
Blocker as unstable_Blocker,
BlockerFunction as unstable_BlockerFunction,
DataRouteMatch,
DataRouteObject,
Fetcher,
Hash,
IndexRouteObject,
IndexRouteProps,
JsonFunction,
LazyRouteFunction,
LayoutRouteProps,
LoaderFunction,
LoaderFunctionArgs,
Location,
MemoryRouterProps,
NavigateFunction,
NavigateOptions,
NavigateProps,
Navigation,
Navigator,
NonIndexRouteObject,
OutletProps,
Params,
ParamParseKey,
Path,
PathMatch,
Pathname,
PathPattern,
PathRouteProps,
RedirectFunction,
RelativeRoutingType,
RouteMatch,
RouteObject,
RouteProps,
RouterProps,
RouterProviderProps,
RoutesProps,
Search,
ShouldRevalidateFunction,
To,
};
If you want to implement a simple route in your application you can follow the bellow mechanism (remember for this I have used the typescript approach)
import React from "react";
import { Navigate, RouterProvider, createBrowserRouter } from "react-router-dom";
import { Status } from "./interfaces/IAuth";
import CardPage from "./pages/CardPage";
import HomePage from "./pages/HomePage";
import ListPage from "./pages/ListPage";
import LoginPage from "./pages/Login";
import RegisterPage from "./pages/Register";
const protectedRouter = createBrowserRouter(
[
{
path: "/",
element: <HomePage />
},
{
path: "/list-component",
element: <ListPage />
},
{
path: "/card-page",
element: <CardPage />
},
],
{ basename: '/react-fundamentals' }
);
const publicRouter = createBrowserRouter(
[
{
path: "/",
element: <LoginPage />
},
{
path: "/register",
element: <RegisterPage />
},
{
path: "/home",
element: <HomePage />
},
{
path: "*",
element: <Navigate to="/" replace />
}
],
{ basename: '/react-fundamentals' }
);
const AppRouter: React.FC<{ status?: Status }> = ({ status }) => {
const selectedConfig = (status === 'authenticated') ? protectedRouter : publicRouter;
return (
<React.StrictMode>
<RouterProvider router={selectedConfig} />
</React.StrictMode>
)
}
export default AppRouter;
答案2
得分: 1
React-Router v6 移除了路由属性,这些属性在 v4/5 版本中存在。Route
组件的 API 从 v4/5 版本变化很大到 v6 版本。现在没有路由属性,也没有 exact
属性,因为现在路由总是精确匹配,所有路由内容都渲染在单个 element
属性上,该属性接受 React.ReactNode
,例如 JSX,作为值。
import React from 'react';
import { Route, Routes } from 'react-router';
import routes from './config/routes';
export interface IApplicationProps {}
const Application: React.FunctionComponent<IApplicationProps> = (props) => {
return (
<Routes>
{routes.map((route) => {
const Component = route.component;
return (
<Route
key={route.path}
path={route.path}
element={<Component />}
/>
);
})}
</Routes>
);
};
如果任何路由组件需要访问之前通过属性传递的内容,它们应该使用提供的 React hooks:useNavigate
用于替代 useHistory
的 navigate
函数,useParams
用于路由路径参数,useLocation
用于 location
对象等。
英文:
React-Router v6 removed route props, these are a v4/5 export. The Route
component API changed significantly from v4/5 to v6. There are no route props, no exact
prop since routes are now always exactly matched, and all routed content is rendered on a single element
prop taking a React.ReactNode
, e.g. JSX, value.
import React from 'react';
import { Route, Routes } from 'react-router';
import routes from './config/routes';
export interface IApplicationProps {}
const Application: React.FunctionComponent<IApplicationProps> = (props) => {
return (
<Routes>
{routes.map((route) => {
const Component = route.component;
return (
<Route
key={route.path}
path={route.path}
element={<Component />}
/>
);
})}
</Routes>
);
};
If any of the routed component need to access what was previously passed via props, they should use the provided React hooks: useNavigate
for navigate
function that replaced useHistory
, useParams
for route path parameters, useLocation
for the location
object, etc.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论