score:0

Accepted answer

since it is a react hook, you can't use usecookies outside a react component function: to access a cookie, you'll need to read it from document.cookie, or install another package, like cookie.

if you're only using the one cookie, you can probably get away by using w3school's cookie example, (which i've turned into an npm package):

function getcookie(cname) {
  let name = cname + "=";
  let decodedcookie = decodeuricomponent(document.cookie);
  let ca = decodedcookie.split(';');
  for(let i = 0; i <ca.length; i++) {
    let c = ca[i];
    while (c.charat(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexof(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

then just do:

const cookie: string = getcookie('mytoken');

Related Query

More Query from same tag