我有这样的路线
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
答案 0 :(得分:1)
在Profile组件中尝试将{... this.props}更改为{... props}