2

I have a React app. I have the following react hook to get cookies using react-cookie.

const [cookies] = useCookies(["basket"]);

I already successfully get the cookie basket. Is there anyway for me to invoke a fresh update on the cookie "basket" if it was changed independently from this react app? I.e. a function for me to call to get the latest cookie value?

kyle
  • 21
  • 2
  • So you want to call a function as soon as there's a change in cookie value? – AdityaSrivast May 13 '20 at 12:01
  • No, whenever I want to use the cookie throughout the code base, I want the latest update of that cookie. Common sense would tell me to just reuse `X = useCookies(["basket"]);` but they become invalid hook calls in the areas I want to use the updated cookie. – kyle May 13 '20 at 12:09
  • You can get the value of cookie using `cookies.basket` in your case. However I'm unsure about the issue you are facing with the invalid hook calls. If maybe you can give some reproducible example? – AdityaSrivast May 13 '20 at 12:29

1 Answers1

0

a quick fix, first remove the cookie first, then update it

  1. cookies.remove('basket')
  2. cookies.set('basket', updated_cookie)
rubimbura brian
  • 619
  • 6
  • 10