Home > Software engineering >  How to remove a cookiei from a browser in react?
How to remove a cookiei from a browser in react?

Time:07-25

I am trying to remove the token cookie from the browser when logging out but with my current code that doesnt seem to be working. Also the Cookie.get() function is returning an empty object when a cookie is still visible in the browser. I have tried looking at other solutions but nothing seem to be working.

import { useNavigate } from "react-router-dom";
import history from "../../history";
import Cookie from "js-cookie";

export default function Tool() {
  const navigate = useNavigate();

  function logout() {
    localStorage.clear();
    Cookie.remove("token");
    console.log(Cookie.get(), "Oooooooooooooo");
    navigate("/login");
  }

  return (
    <div >
      <div className="topButtons">
        <button onClick={() => logout()}>LogOut</button>
      </div>
    </div>
  );
}

CodePudding user response:

Try remove all cookies first: document.cookie = ''

CodePudding user response:

Using the remove method from library

Cookies.remove('name')
// Removing a nonexistent cookie neither raises any exception nor returns any value.

  • Related