I have this class:
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>
);
}
}
I have a problem with User component which not read a props. Before I tried display route without <Switch>
like this:
<Route path="" component={} />
and after click component was loaded correctly and display props but after refresh website it display me error 404. After implement Switch, it absolutely broken. Nothing display (only error with console log)
// 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 User component import React from 'react';
const User = (props) => {
return (
<div>
<p>User id: {props.match.params.id}</p>
</div>
);
}
export default User;
Any idea how I can solve my problem?