我使用react-router v4
制作这样的侧边栏组件:
<ParentComponent>
{routes.map((route, index) => (
<Route
key={index}
path={route.path}
exact={route.exact}
component={route.sidebar}
/>
))}
</ParentComponent>
路线是
[
{
path: '/',
exact: true,
sidebar: HomeSideBar,
mobileSidebar: HomeSideBar,
component: HomePage,
},{
path: '/profile',
exact: true,
mobileSidebar: HomeSideBar,
component: ProfilePage,
}, {
path: '/*',
component: NotFound,
},
]
我尝试不渲染父<ParentComponent>
以防我在路由中没有sidebar
,这意味着如果地图中的component={route.sidebar}
不存在,{{1没有渲染。
我试图通过<ParentComponent>
或routes.length
进行检查,但这不起作用。这有什么解决方案吗?
感谢您的回答。