ReactJS匹配道具未传递给组件

时间:2018-06-07 10:29:50

标签: javascript reactjs

我有这样的路线

Dashboard.js

<Route path="/dashboard/:user_id" render={(props) =>  <Profile user={this.props.user} {...this.props}/>}/>

正如您所看到的,我将Dashboard组件的道具传递给Profile组件,当您传递道具时反应文档,匹配对象也被传递我想要访问{{1}来自match

的对象

但访问Profile

时,我在个人资料组件上获得了undefined
match.params

当我检查react dev工具时,componentDidMount(){ console.log(this.props.match.params) // Undefined } 组件确实没有收到匹配对象,但当我在react dev工具中检查路由时,Profile有当前参数

来自React Dev工具

match.params行给了我这个

<Route/>

正如您所见,匹配存在

虽然Props path: "/dashboard/:user_id" render: render() State match: {…} isExact: true params: {…} user_id: "5af13d60f3957c11e46510ad" path: "/dashboard/:user_id" url: "/dashboard/5af13d60f3957c11e46510ad" 组件没有参数

Profile

1 个答案:

答案 0 :(得分:1)

在Profile组件中尝试将{... this.props}更改为{... props}

相关问题