What I'm trying to accomplish is to have my App component redirect to the "/login" route if the state's uid is null. The redirect works correctly and will always redirect you to "/login" if there is a null uid, however the problem is it doesn't stop redirecting once you're on the "/login" route.
The error being thrown is:
Warning: You tried to redirect to the same route you're currently on: "/login"
One last constraint is I need the uid to be null until after a login event fires from the LoginScreen component. So the user needs to be able to view the "/login" route with a null uid in the top-level App component.
My code is as follows:
class App extends Component {
constructor() {
super();
this.state = {
uid: null
}
}
render() {
// Redirect to login if no user
if (this.state.uid == null) {
return (
<Router>
<Redirect to="/login" />
</Router>
);
}
return (
<Router>
<div>
<Route path="/login" component={LoginScreen} />
<Route path="/dashboard" component={DashboardScreen} />
<Route path="/profile" component={ProfileScreen} />
</div>
</Router>
);
}
}
I hope I explained the situation well. Thanks in advance!