2017-04-24 98 views
3

我使用的反應 - 路由器終極版安裝了與反應路由器,終極版訪問PARAMS

npm install --save [email protected] 

實現像這樣(https://github.com/ReactTraining/react-router/tree/master/packages/react-router-redux

<Route path='/resource/:id' component={Resource}/> 

我試圖訪問容器中的id參數,如下所示:

const mapStateToProps = (state, ownProps) => { 
    console.log(ownProps) 
    return {...state.resource, id: ownProps.params.id} 
} 

如react-router-redux文檔所示。

我收到一個錯誤,指出ownProps.params是未定義的。所以這個工程:

const mapStateToProps = (state, ownProps) => { 
    return {...state.resource, id: ownProps.match.params.id} 
} 

當我登錄ownProps,但是,我發現,ownProps.match.params.id包含我需要的ID。

這是執行改變還是我執行路線錯誤?由於

+0

你能鏈接到react-router-redux文檔嗎? '路線'一直都是通過三個道具,'match','location'和'history'。 – Li357

+0

https://github.com/reactjs/react-router-redux: 功能mapStateToProps(狀態,ownProps){ 返回{ ID:ownProps.params.id, 濾波器:ownProps.location.query.filter }; } – zoopz

回答

5

npm install --save [email protected]

有了上面的命令,你可能已經安裝了react-router-redux 5的alpha版本和你正在使用它與反應路由器V4。

正如他們在https://github.com/reactjs/react-router-redux自述中提到的,react-router-redux版本5目前正在積極開發中somewhere else

這回購爲反應路由器-終極版4.x的,這是唯一與 反應路由器2.x和3.x兼容

的下一個版本反應路由器-終極版將5.0.0並且將與 兼容react-router 4.x.目前正在積極開發 over there

因此,您所引用的文檔對您所使用的版本無效。

您的實施過程中沒有任何問題,您可以通過ownProps的match繼續訪問params