Sander Bakker
Sander Bakker

Reputation: 35

Logout update NavBar React.js

I'm trying to update my NavBar upon logging out (clearing the session). This is the code I wrote:

App.js:

class App extends Component {
  constructor(props){
    super(props);
    this.state = {};  
  }
  componentWillMount(){
      if(sessionStorage.getItem('access_token') != null && sessionStorage.getItem('id_token') != null){
          this.setState({loggedIn: true}); 
      } 
  }
  render() {
    return (
        <BrowserRouter>
          <div>
            <title>Webshop</title> 
            <NavBar/>
            <Switch>
                    {/*Routes need to be include in App.js otherwise root can't find the paths*/}
                    <Route exact path='/' component={Home}/>
                    <Route exact path='/categories' component={Categories}/>
                    <Route exact path='/login' component={Login}/>
                    <Route exact path='/register' component={Register}/>
                    {this.state.loggedIn == true ? <Route exact path='/logout' component={Logout}/> : null}
                    <Route render={function(){
                        return (<NotFound/>); 
                    }}/>
                </Switch>
              <Footer/>
          </div>  
        </BrowserRouter>
    );
  }
}

NavBar.js:

class NavBar extends Component {
    constructor(props) {
        super(props);
        this.toggle = this.toggle.bind(this);
        this.state = {
          isOpen: false
        };
    }

    componentWillMount(){
        if(sessionStorage.getItem('access_token') != null && sessionStorage.getItem('id_token') != null){
            this.setState({loggedIn: true}); 
        } 
        else{
            this.setState({loggedIn: false}); 
        }
    }

    toggle() {
        this.setState({
          isOpen: !this.state.isOpen
        });
    }

    render(){
        return(
            <div>
                <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css'/>
                <Navbar color="faded" light expand="md">
                    <NavLink className='navbar-brand' exact to='/'>
                        <img src={logo} alt='Brand' width='35px' height='35px'/>
                    </NavLink>
                    <NavbarToggler onClick={this.toggle} />
                    <Collapse isOpen={this.state.isOpen} navbar>
                        <Nav className="mr-auto" navbar>
                            <NavItem>
                                <NavLink className='nav-link' exact to='/categories'>
                                    Categories
                                </NavLink>
                            </NavItem>
                        </Nav>
                        <Nav className='mx-auto' navbar>
                            <Form inline>
                                <FormGroup>
                                    <Input size='sm' type="text" name="search" placeholder="Search" />
                                </FormGroup>
                                <Button size='sm'><i className='fa fa-search'></i></Button>
                            </Form>
                        </Nav>
                        <Nav className="ml-auto" navbar>

                            <NavItem>
                                <NavLink className='nav-link' exact to='/cart'>
                                    <i className='fa fa-shopping-cart'></i>
                                </NavLink>
                            </NavItem>

                            {(this.state.loggedIn) ?
                            <NavItem>
                                <NavLink className='nav-link' exact to='/profile'>
                                    <i className='fa fa-user'></i> 
                                </NavLink>
                            </NavItem>
                            : null }

                            {(this.state.loggedIn == true) ?

                            <NavItem>
                                <NavLink className='nav-link' exact to='/logout'>
                                    <i className='fa fa-sign-out'></i>
                                </NavLink>
                            </NavItem>
                            : 
                            <NavItem>
                                <NavLink className='nav-link' exact to='/login'>
                                    <i className='fa fa-sign-in'></i>
                                </NavLink>
                            </NavItem>
                            }
                        </Nav>
                    </Collapse>
                </Navbar>
            </div>
        );
    }
}

Logout.js:

class Logout extends Component{
    componentDidMount(){
        sessionStorage.clear(); 

    }

    render(){
        return(
            <div>
                Now loggedOut; 
            </div>
        )
    }
}

The code does the following, upon login it stores the access_token & id_token in the sessionStorage. Then updates the NavBar by redirecting to the homepage, now the NavBar contains the logout button because the state is updated to loggedIn. Now when I logout, I clear the session so the id_token & access_token are no longer avaiable. This should trigger the state to update loggedIn to false & rerender the NavBar component but somehow it doesn't. (When I refresh the page the NavBar does update)

Could anyone help me out?

Upvotes: 2

Views: 6161

Answers (1)

Scott
Scott

Reputation: 3763

In your App.js, I recommend you maintain the state there as a single source of truth, then pass along this.state.loggedIn to <NavBar/> like so:

<NavBar loggedIn={this.state.loggedIn} />

In addition to that, add a function to toggle the loggedIn state in App.js, like so:

logOut() { this.setState({loggedIn: false}); }

And pass along that function like so:

<NavBar loggedIn={this.state.loggedIn} logOut={this.logOut} />

This way, you can respond to this.props.loggedIn in Navbar.js and keep your logic for logging out (and also logIn if you please) in one place in App.js

Upvotes: 1

Related Questions