如果侧栏组件为空,react-router v4会阻止渲染父级

时间:2017-07-01 09:36:40

标签: reactjs react-router

我使用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进行检查,但这不起作用。这有什么解决方案吗?

感谢您的回答。

0 个答案:

没有答案