Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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;
 
PREVIOUS NEXT
Tagged: #logout #react #js
ADD COMMENT
Topic
Name
4+6 =