2017-04-24 103 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