RouteChildrenProps 不是一个导出的成员。

huangapple go评论67阅读模式
英文:

RouteChildrenProps is not an exported member

问题

我遇到了下面指向 'RouteChildrenProps' 的错误:

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':

RouteChildrenProps 不是一个导出的成员。

I am just trying to get through a tutorial but I got stuck here. Here is the full code:

import React from &#39;react&#39;;
import { Route, RouteChildrenProps, Routes } from &#39;react-router&#39;;
import routes from &#39;./config/routes&#39;;

export interface IApplicationProps {}

const Application: React.FunctionComponent&lt;IApplicationProps&gt; = (props) =&gt; {
  return (
    &lt;Routes&gt;
      {routes.map((route, index) =&gt; {
        return &lt;Route 
          key={index} 
          exact={route.exact} 
          path={route.path} 
          render={(routeProps: RouteChildrenProps&lt;any&gt;) =&gt;
            &lt;route.component {...routeProps} /&gt;} /&gt;;
          }
      )}
    &lt;/Routes&gt;
  );
};

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 &quot;react&quot;;
import { Navigate, RouterProvider, createBrowserRouter } from &quot;react-router-dom&quot;;
import { Status } from &quot;./interfaces/IAuth&quot;;
import CardPage from &quot;./pages/CardPage&quot;;
import HomePage from &quot;./pages/HomePage&quot;;
import ListPage from &quot;./pages/ListPage&quot;;
import LoginPage from &quot;./pages/Login&quot;;
import RegisterPage from &quot;./pages/Register&quot;;
const protectedRouter = createBrowserRouter(
[
{
path: &quot;/&quot;,
element: &lt;HomePage /&gt;
},
{
path: &quot;/list-component&quot;,
element: &lt;ListPage /&gt;
},
{
path: &quot;/card-page&quot;,
element: &lt;CardPage /&gt;
},
],
{ basename: &#39;/react-fundamentals&#39; }
);
const publicRouter = createBrowserRouter(
[
{
path: &quot;/&quot;,
element: &lt;LoginPage /&gt;
},
{
path: &quot;/register&quot;,
element: &lt;RegisterPage /&gt;
},
{
path: &quot;/home&quot;,
element: &lt;HomePage /&gt;
},
{
path: &quot;*&quot;,
element: &lt;Navigate to=&quot;/&quot; replace /&gt;
}
],
{ basename: &#39;/react-fundamentals&#39; }
);
const AppRouter: React.FC&lt;{ status?: Status }&gt; = ({ status }) =&gt; {
const selectedConfig = (status === &#39;authenticated&#39;) ? protectedRouter : publicRouter;
return (
&lt;React.StrictMode&gt;
&lt;RouterProvider router={selectedConfig} /&gt;
&lt;/React.StrictMode&gt;
)
}
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 用于替代 useHistorynavigate 函数,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 &#39;react&#39;;
import { Route, Routes } from &#39;react-router&#39;;
import routes from &#39;./config/routes&#39;;

export interface IApplicationProps {}

const Application: React.FunctionComponent&lt;IApplicationProps&gt; = (props) =&gt; {
  return (
    &lt;Routes&gt;
      {routes.map((route) =&gt; {
        const Component = route.component;
        return (
          &lt;Route 
            key={route.path} 
            path={route.path} 
            element={&lt;Component /&gt;}
          /&gt;
        );
      })}
    &lt;/Routes&gt;
  );
};

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.

huangapple
  • 本文由 发表于 2023年6月1日 22:27:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定