我正在从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>
组件的有效子项:
那么,是否有机会创建有效的路由子组件?