首页 文章

在react-router中隐藏父组件

提问于
浏览
1

在react路由器中有一种方法可以有子路由但不显示父组件吗?我'm trying to display breadcrumbs in my application so I switched to having child routes but I don't想要以'master/detail'类型布局的方式显示父组件 . 我只是希望子组件显示为好像它自己导航到但是看起来如果你不在父组件中的某处包含 {this.props.children} 那么子组件永远不会被渲染 .

我试过的一个黑客就是看是否填充了 this.props.children 并且没有在渲染中显示页面的其余部分

if(this.props.children) {
   return (this.props.children) 
} else { 
  return /**rest of render method**/
}

但这感觉就像一个糟糕的解决方案 .

1 回答

  • 1

    我正在尝试这样做,但似乎 <IndexRoute> 可以帮助你,因为这可以让你区分你在 /portal/portal/feature 做的事情 .

    <Route path="/portal" component={ComponentContainingBreadcrumbs}>
      <IndexRoute component={ComponentWithRestofRenderMethodFromYourExample} />
      <Route path="child1" component={Child1} />
      <Route path="child2" component={Child2} />
    </Route>
    

相关问题