我有这堂课:
export default class Panel extends PureComponent {
constructor(props){
super(props);
this.state = {
loading: true,
url: '/' +this.props.lang+ '/panel',
about: '/' +this.props.lang+ '/panel' + '/about',
user: '/' +this.props.lang+ '/panel' + '/user/1',
userPath: '/' +this.props.lang+ '/panel' + '/user/:id'
}
}
componentDidMount(){
this.setState({
loading: false
});
console.log(this.state.user);
}
onResize(width){
var totalWidth = width + 250 + "px";
var contentWidth = width + "px";
document.querySelector('.panel__slideContainer').style.width = totalWidth;
document.querySelector('.mainContainer').style.width = contentWidth;
}
render() {
if(this.state.loading === true){
return <p>Loading...</p>
}
return (
<div className="container panel__container">
<Router>
<ReactResizeDetector handleWidth onResize={this.onResize} />
<div className="panel__slideContainer">
<nav className="panel__sideMenu">
<ul>
<li>
<NavLink exact activeClassName="active" to={this.state.url}>Home</NavLink>
</li>
<li>
<NavLink activeClassName="active" to={this.state.about}><Trans>change password</Trans></NavLink>
</li>
<li>
<NavLink activeClassName="active" to="/pl/panel/user/1">User</NavLink>
</li>
</ul>
</nav>
<div className="mainContainer">
<Nav />
<div>
<Switch>
<Route exact path={this.state.url}>
<Home />
</Route>
<Route path={this.state.about} >
<About />
</Route>
<Route path="/pl/panel/user/:id">
<User />
</Route>
</Switch>
</div>
</div>
</div>
</Router>
</div>
);
}
}
我对未读取道具的用户组件有疑问。在我尝试显示路线之前没有<Switch>
这样的:
<Route path="" component={} />
并且在单击组件正确加载并显示道具后,但在刷新网站后它显示错误 404。实施 Switch 后,它绝对坏了。没有显示(只有控制台日志错误)
// This is the error:
> Cannot read property 'params' of undefined
> backend.js:6 The above error occurred in the <User> component:
> Uncaught TypeError: Cannot read property 'params' of undefined
@Edit 用户组件从 'react' 导入 React;
const User = (props) => {
return (
<div>
<p>User id: {props.match.params.id}</p>
</div>
);
}
export default User;
知道如何解决我的问题吗?