2017-07-06 49 views
1

我有以下兩條路線:/items/items/buy如何防止與react-router v4匹配兩條路由?

每條路徑對應於單個視圖中的一個選項卡。兩條路線均以精確的道具呈現,但仍然有兩個標籤在導航至/items/buy時被標記爲有效。

我已經嘗試過使用withRouter,但我注意到將/items更改爲/items/sell可以解決問題,但我不想擁有那條路線。

我明白rrv4是匹配我的路線/items的第一部分和其他路線太/items/buy,但我想,如果我使用exact這不應該發生。任何線索爲什麼發生這種情況?

啊我忘了說我已經在使用Switch了。

感謝您的幫助!

回答

1

問題是我用LinkContainer包裝了一個NavItem,並且LinkContainer沒有一個確切的道具。添加確切的道具解決了問題!

4

您需要將您的路線放在<Switch>組件中。交換機將只呈現匹配的第一條路由。

import {Route, Switch} from 'react-router-dom'; 

<Switch> 
    <Route exact path="/" component={Main} /> 
    <Route exact path="/items" component={SomeComponent} /> 
    <Route exact path="/items/buy" component={SomeOtherComponent} /> 
    <Route component={NotFound} /> 
</Switch> 
+0

我已經在使用Switch:/ – Whee