Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

logout in react js

/*
|==========================================================
| How to logout in react js and redirect to login page
|==========================================================
*/
import React from 'react';
import { Link, useNavigate } from 'react-router-dom';

function Navbar() {

    const navigate = useNavigate();

    // HANDLE LOGOUT EVENT
    const logout = (e) => {
        e.preventDefault();
        console.log('Logout');

        // CLEAR DATA FROM STORAGE
        localStorage.clear();
        sessionStorage.clear();

        navigate("/");
    }

    return (
        <nav className="sb-topnav navbar navbar-expand navbar-dark bg-dark">
            <Link 
              className="dropdown-item" 
              to="#" 
              onClick={logout}
			>
             Logout
			</Link>
        </nav>
    )
}

export default Navbar;
Comment

PREVIOUS NEXT
Code Example
:: jquery add attribute 
::  
:: Reset reCAPTCHA js 
::  
::  
Javascript ::  
::  
:: class onclick jqery 
::  
::  
::  
::  
::  
Javascript ::  
:: node version not specified in package.json 
:: where to add "type": "module" in the package.json 
::  
::  
:: how to add a right click listener javascript 
Javascript ::  
Javascript :: vimeo id regex 
::  
Javascript :: javascript password generator example 
Javascript ::  
Javascript ::  
::  
::  
Javascript ::  
Javascript ::  
::  
ADD CONTENT
Topic
Content
Source link
Name
7+6 =