I want to add a conditional route in my component, so if a user is logged in I return the component, and if not I want to navigate to the login page.
When I do that nothing is displayed on my page. How to manage it correctly?
import { Route } from 'react-router-dom';
import { UserContext } from '../UserContext';
import Login from './Login';
import React, { useContext } from 'react'
function ShoppingList() {
const { user } = useContext(UserContext);
if (user) {
return (
..... //content
);
}
else {
return (
<div>
<Route path='/login' component={Login} />
</div>
);
}
}
export default ShoppingList;
I tried also Navigate but I got an error and the same thing with the history.