由于以下原因,所有React Router的拳头都让我很困惑 . 如果其他人有同样的感觉,这可能对他们有用,或者如果他们不这样做,他们的回答对我有帮助:)
1)使用反应路由器V4的标准方法是什么
-
使用Switch?这对V4来说还算不错吗?
-
在一个js文件中使用Route config作为集合?
-
使用parent component中指定的路线?
2)如果我的应用程序有多个大模块,我如何根据模块指定路由并将它们组合在根索引文件中?这是开始的正确方法吗?
我有这样写的路由配置, how can I configure notfound(404) logic If I have my route config in this fashion, I want it to redirect to homepage if no route found.
const routes = [
{
path: '/',
component: Layout,
routes: [{
path: '/users',
component: Users,
exact: true
}, {
path: '/users/:id',
component: ViewUser
}, {
path: '/addUser',
component: AddUser
}
]
}, { component: Layout }
];
const RouteWithSubRoutes = (route) => (
<Route path={route.path} exact={route.exact} render={props => (
<route.component {...props} routes={route.routes} />
)} />
);
export const renderChildren = (childRoutes) => {
if (childRoutes) {
return childRoutes.map((route, i) => {
return <RouteWithSubRoutes key={i} {...route} />;
});
}
};
export const RouteConfig = () => {
return (
<Router>
<div>
{renderChildren(routes)}
</div>
</Router>
);
};