Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cookies.remove('abc') not working in reactJs

import Cookies from 'universal-cookie';

const cookies = new Cookies();
cookies.remove('abc');

console.log(cookies.getAll());

It is still printing my abc cookie.

like image 650
user10916649 Avatar asked Feb 25 '19 07:02

user10916649


People also ask

How do I delete cookies in react JS?

Cookies can be removed in React. js by using the following methods: By using cookies. remove() in the react-cookie library.

Can I use cookies in react?

By default, when you fetch your URL, React native sets the cookie. To see cookies and make sure that you can use the https://www.npmjs.com/package/react-native-cookie package.

How do I use http only cookies in react?

In this example, I will use axios to make such API call in the frontend. Note that every API call's request header will contain our httpOnly cookie and it's content - we can confirm this by opening chrome dev tools' network tab, make the API call, then check the "Request Headers" for the "Cookie"...


2 Answers

Cookies need to have both path and domain appended to them to be removed. Try this:

cookies.remove("abc", {path: "/", domain: ".example.com"})  
like image 126
Jeff Klink Avatar answered Sep 30 '22 10:09

Jeff Klink


May be you need to do something like

cookies.remove('abc', { path: '/' });

More info here

like image 39
Sid Avatar answered Sep 30 '22 08:09

Sid