【发布时间】:2019-05-19 07:40:46
【问题描述】:
我有一个问题,我以正确的方式通过props (IMO),但我仍然有这个错误:
我的 App.js 中有这些代码行
这个正在工作:
<Route path="/users/:username"
render={(props) => <Profile isAuthenticated={this.state.isAuthenticated} currentUser={this.state.currentUser} {...props} />}>
</Route>
但是这个不起作用,在渲染“Ride”时出现错误
<Route path="/ride/:username"
render={(props) => <Ride isAuthenticated={this.state.isAuthenticated} currentUser={this.state.currentUser} {...props} />}>
</Route>
错误: 类型错误:无法读取未定义的属性“参数”
43 | }
44 |
45 | componentDidMount() {
> 46 | const username = this.props.match.params.username;
47 | ^ this.loadUserProfile(username);
48 | }
49 |
即使代码在内部(组件),它们也是一样的......
【问题讨论】:
-
检查你的 props,错误提示
match没有params属性,请问console.log(this.props)你得到了什么?
标签: reactjs react-router