2017-03-09 111 views
0

我如何能实现maybeGoToBar这样 - 这取决于名字 - 我显示Foo组件或Bar组件不改变网址如何重定向阵营路由器不改变网址

function maybeGoToBar(nextState, replace, callback) { 
    const name = nextState.params.name; 
    if (name === "Bob") { 
    // do something to route to /bar 
    // but keep showing /bob/foo as the current location 
    // in the browser address bar 
    } 
    callback(); 
} 

<Route path=":name/foo" onEnter={maybeGoToBar} component={Foo} > 
<Route path="bar" component={Bar} > 
+0

对我来说听起来像你不应该使用路由器来做到这一点。你应该在你的组件中存储一个状态变量(如果需要,它的初始值可以从url中获取),你应该根据状态选择要显示的孩子。 – caesay

+0

[React路由器没有改变URL]可能的重复(http://stackoverflow.com/questions/39721812/react-router-without-changing-url) – Gajus

回答

0

您需要使用记忆历史。

Link

+0

我读了这个选项有不利的一面,初始URL(用于打开页面)不由路由器处理。 – mnieber