Sunstrike527
Sunstrike527

Reputation: 607

How to get js-cookie

I have a code for setting a cookie from js-cookie

Cookies.set("currentCity", selected ? JSON.stringify(selected.city) : "", {
            domain: `.${process.env.DOMAIN}`,
})

When I try to get cookie on change of currentCity variable

    useEffect(() => {
    console.log('All Cookies' ,Cookies.get())
  }, [currentCity])

I have the following in console

_pk_id.10.1fff: "7f704b7a9d967f1e.1577256464.0.1577265589.."

Also when I didnt set a Cookie (just removed Cookies.set)

I have the same in console.log on get Cookie

_pk_id.10.1fff: "7f704b7a9d967f1e.1577256464.0.1577265589.."

What am I doing wrong ? Is _pk_id.10.1fff the same Cookie which I have set at first time ?

Upvotes: 0

Views: 273

Answers (1)

Midani Rachdi
Midani Rachdi

Reputation: 23

You are setting the cookie value to currentCity everytime without clearing its values (in case you didn't close your browser) , perhaps removing the cookie first might solve your problem.

Here's a suggestion :


const domain = { domain: `.${process.env.DOMAIN}` };
if (selected === true) {
  if (Cookies.get("currentCity")) {
    Cookies.remove("currentCity");
    Cookies.set("currentCity", JSON.stringify(selected.city),domain);
  }
} else {
  Cookies.set("currentCity", "", domain);
}

Upvotes: 1

Related Questions