I am new to react js and try to create basic login UI, after user login the header of the UI change automatically. For testing purpose, I just want to remove the login option from the header, later, I may add more options such as logout and others. Here is my code
import { useState,useEffect } from 'react';
import Container from 'react-bootstrap/Container';
import Nav from 'react-bootstrap/Nav';
import Navbar from 'react-bootstrap/Navbar';
const Header = () => {
const [localtoken, setLocaltoken] = useState(null);
useEffect(() => {
const jwt_token= localStorage.getItem('jwt_token');
if (jwt_token) {
setLocaltoken(jwt_token);
}},[]);
console.log(localtoken)
if (!localtoken){
console.log(localtoken)
return (
<Navbar bg="primary" variant="dark">
<Container>
<Navbar.Brand href="home">Navbar</Navbar.Brand>
<Nav>
<Nav.Link href="home">Home</Nav.Link>
<Nav.Link href="login">Login</Nav.Link>
<Nav.Link href="register">Register</Nav.Link>
</Nav>
</Container>
</Navbar>)}
else {
return (
<Navbar bg="primary" variant="dark">
<Container>
<Navbar.Brand href="home">Navbar</Navbar.Brand>
<Nav>
<Nav.Link href="home">Home</Nav.Link>
<Nav.Link href="register">Register</Nav.Link>
</Nav>
</Container>
</Navbar>)
}
}
export default Header
The problem for me is after I login, the header still show "Home Login Register", I have to click either "Home" or "Login" again to make the header change to the required status.


