我正在从Reaction路由v5迁移到Reaction路由v6.新版本的主要更改之一是path属性不接受array.

因此,使用如下所示的原始代码:

<Route path=["/a-route", "/another-route", "/another-more", "/even-other">
   <VeryBigChildren>....</VeryBigChildren>
<Route/>

它必须被重构为类似如下的东西:

<Route path="/a-route" element={<VeryBigChildren>...</VeryBigChildren>} />
<Route path="/another-route" element={<VeryBigChildren>...</VeryBigChildren>} />
<Route path="/another-more" element={<VeryBigChildren>...</VeryBigChildren>} />
<Route path="/even-other" element={<VeryBigChildren>...</VeryBigChildren>} />

我认为这是肮脏的,难以阅读和难以维护.

所以我创建了一个小组件:

export const MultipleRoute = ({ path, children }) => (
  <>
    {path.map((routePath) => (
      <Route key={routePath} path={routePath} element={<>{children}</>} />
    ))}
  </>
);

为了能够将原始代码重构为:

<MultipleRoute path=["/a-route", "/another-route", "/another-more", "/even-other">
   <VeryBigChildren>....</VeryBigChildren>
<MultipleRoute/>

但是,REACT-ROUTER报告<MultipleRoute>不是<RouteS>组件的有效子项:

enter image description here

那么,是否有机会创建有效的路由子组件?

推荐答案

MultipleRoute呈现的路由必须包装在Routes组件中,而MultipleRoute必须由Route组件本身呈现.只有Route个组件可以是Route个组件的有效子组件,因此MultipleRoute将需要获取您希望在每条路径上呈现的JSX.

示例:

<Route
  path="/*" // <-- Splat "*" is necessary for descendent routes to render
  element={(
    <MultipleRoute
      path={[
        "/a-route",
        "/another-route",
        "/another-more",
        "/even-other"
      ]}
      element={<VeryBigChildren />}
    />
  )}
/>
export const MultipleRoute = ({ path, element }) => (
  <Routes>
    {path.map((routePath) => (
      <Route key={routePath} path={routePath} element={element} />
    ))}
  </Routes>
);

虽然它is你的意见,只是渲染4路由直接是"讨厌的,难以阅读,难以维护",相比上述代码,我不同意.这是大约两倍多的代码行来完成更少的.

这4条路由直接进行比较:

<Route path="/a-route" element={<VeryBigChildren/ >} />
<Route path="/another-route" element={<VeryBigChildren />} />
<Route path="/another-more" element={<VeryBigChildren />} />
<Route path="/even-other" element={<VeryBigChildren />} />

使用类似于上面的"多路由"组件的限制是,您只能 for each 要匹配的路径指定一个"/*"根级别的路由.如果您想要"分组"路由以呈现不同的组件,则需要将它们嵌套在其他根级路由下.

示例:

<Route
  path="/*"
  element={(
    <MultipleRoute
      path={[...]}
      element={<RootChildren />}
    />
  )}
/>
<Route
  path="/foo/*"
  element={(
    <MultipleRoute
      path={[...]}
      element={<FooChildren />}
    />
  )}
/>

对于这种复杂性,最好只实现普通的派生路由.

示例:

<Route path="/*" element={<RootChildren />} />
<Route path="/foo/*" element={<FooChildren />} />
const RootChildren = () => (
  <Routes>
    <Route path="/a-route" element={<VeryBigChildren/ >} />
    <Route path="/another-route" element={<VeryBigChildren />} />
    <Route path="/another-more" element={<VeryBigChildren />} />
    <Route path="/even-other" element={<VeryBigChildren />} />
  </Routes>
);
const FooChildren = () => (
  <Routes>
    ....
  </Routes>
);

MultipleRoute这个分量根本不是必需的.

Reactjs相关问答推荐

从另一个组件更改组件中const的状态

为什么这个'Suspense'子组件在挂起promise解决后会丢失它的状态,而不是呈现?<>

在Reaction中的第一次装载时,Use Effect返回空数组

试图从Amazon-Cogito-Identity-js模拟CogitoUser.authateUser,并将其获取为未定义的S

如何动态地改变<; Select >;React和Tailwind元素?

React 在第一次渲染时为 null

FabricJS反序列化问题

在 golang 服务器中刷新或直接 url 路径时响应 404

React 无效的钩子调用:如何避免嵌套钩子?

useMemo 依赖项的行为

ReactJS中使用setState方法时,Context内部的State未进行更新

如何覆盖自定义 MUI 手风琴的样式(分隔线和折叠图标)

如何到达类组件中的状态?

如何在 nextjs 中单击按钮时动态导入和渲染组件?

有没有办法根据 Rechart 中的 activeDot 更改值?

list 组件未按预期运行

如何使用react 路由将 url 参数限制为一组特定的值?

找不到元素 - 这是参考问题吗?

单击按钮时获取react 表中每一行的属性

bootstrap 导航栏切换器在 reactjs 中不起作用