I have a little bit complicated problem (I think so). I am using a React-router-dom v4. I wanted to implement private routes. I have a login page where I call API to authenticate and then when it is successful I redirect my application to another page. Till now everything works just fine. The problem starts when I try to redirect my application to another page under the private route.
here is my NavigationContainer render method (there is a Redux connected which supplies me the data about userData
state:
render() {
const { userData } = this.props
return(
<div style={{height: '100%', width: '100%'}}>
<Switch>
<Route exact path="/" component={LoginScreen} />
<PrivateRoute path='/dashboard' authenticated={userData.isLoggedIn} component={PrivateRouteContainer} props={this.state} />
<Route component={NotFoundPage} />
</Switch>
</div>
)
}
my PrivateRoute implementation looks like:
const PrivateRoute = ({ component: Component, authenticated, ...rest }) => {
return(<Route {...rest} render={(props) => authenticated === true
? <Component {...props} />
: <Redirect to='/' />
} />)
}
And finally my PrivateRouteContainer is implemented like this:
export default class PrivateRoute extends Component {
onMenuClick = (path) => {
this.props.history.push('/dashboard' + path)
}
render() {
return(
<div>
<NavigationBar onMenuClick={this.onMenuClick} />
<Switch>
<Route exact path='/dashboard' component={MainPage} />
<Route path='/dashboard/loadFile' component={LoadFile} />
</Switch>
</div>
)
}
}
The only way to force the app to redirect to the path: dashboard/loadFile
is to change something in the project and hot reload it. Otherwise, there is no possibility to force it to render the route.
Could someone suggest me something? I do not know where the problem is.