带有反应路由器的分层路由

Hierarchical routes with react router

如果我想要一个路线层次结构,将活动状态应用于我的导航,但其中某些级别的导航是纯逻辑的。

例如,如果我这样做

<Route component={App} path="/" >
        <Route path="example" component={empty}>
            <Route path="ex1" component={ex1} />
            <Route path="ex2" component={ex2} />
            <IndexRedirect to="ex1" />
        </Route>
 </Route>

我的导航节点示例正确应用了活动状态,但我必须创建一个空组件,它只呈现子项。

或者我可以展平路线,这意味着我不再需要创建一个空的组件,但我不再获得活动的导航状态。

有什么好的解决办法吗? (我使用的是最新的 react-router v2.0.0rc5)

你可以只省略空组件。我会把你的例子写成:

<Route path="/" component={App} >
  <Route path="example">
    <IndexRedirect to="ex1" />
    <Route path="ex1" component={ex1} />
    <Route path="ex2" component={ex2} />
  </Route>
</Route>

这会被认为是地道的。