Search code examples
reactjscookiesreduxfont-awesomejs-cookie

How to get js-cookies?


I used redux and I want to fetch my API using the cookies item like this:

import cookies from "js-cookies";

api/get_liste/${cookies.getItem("moncentre")}

when I run my app, ${cookies.getItem("moncentre")} is null.

My codesandbox: https://codesandbox.io/s/ecstatic-rubin-tf56r?file=/src/App.js:157-212

How can I fix it ?


Solution

  • So the problem here is, you never set a cookie in your app and then try to get the cookie that never exists so it will return null since there is no value for it in the browser cookies.

    In order to set a cookie, you should do this (according to the package provider):

    cookies.setItem('_code_sandbox_key', 'Cookie did set.')
    

    The other thing that you facing is the warning that you get in your dev tools, so according to this doc, this console warning is not an error or an actual problem — Chrome is just spreading the word about this new standard to increase developer adoption. To fix this you should add SameSite="none Secure" (and then clear all your cookies with the dev tool) to let the browser know the script is not related to your site and that it is a third-party library that you are using.

    <script src="https://kit.fontawesome.com/a076d05399.js" SameSite="none Secure"></script>
    

    NOTE: You can follow this thread for more information.