question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`useCookie` state is stored independently

See original GitHub issue

Environment

  • Operating System: Darwin
  • Node Version: v14.17.3
  • Nuxt Version: 3.0.0
  • Package Manager: yarn@3.1.1
  • Bundler: Vite
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

<template>
  <div>
    <h1>
      {{ cookie2 }}
    </h1>
    <button @click="cookie1 = (Math.random() * 100).toFixed()">
      Change cookie
    </button>
  </div>
</template>

<script setup>
const cookie1 = useCookie('test')
const cookie2 = useCookie('test')
</script>

Describe the bug

Multiple invocations of useCookie are not synced, and so setting one will not trigger a change to others. This is not a bug per-se, but it’s inconsistent compared to useState, for example…

Additional context

No response

Logs

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:6
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

7reactions
boboldehampsinkcommented, Jul 21, 2022

This is the composable I created:

import { useCookie, useState, watch } from '#imports';

// Expires in a year
const expires = new Date();
expires.setTime(expires.getTime() + (365 * 24 * 60 * 60 * 1000));

// Make cookie use state across app
export default function useStatefulCookie(name) {
  const cookie = useCookie(name, { expires });
  const state = useState(name, () => cookie.value);

  watch(state, () => { cookie.value = state.value; }, { deep: true });

  return state;
}
4reactions
joshwcorbettcommented, Oct 27, 2022

Would be awesome to have useCookie behave similarly to useState but persisted in cookies

Read more comments on GitHub >

github_iconTop Results From Across the Web

Store state in cookies with use-cookie-state React hook
Store state in cookies with use-cookie-state React hook ... Sometimes it becomes necessary to keep the state of the application between reloads ...
Read more >
Cookies vs. sessions - Stack Overflow
The concept is storing persistent data across page loads for a web visitor. Cookies store it directly on the client. Sessions use a...
Read more >
Cookie Store API - GitHub Pages
Cookie Store. A cookie store is normatively defined for user agents by Cookies: HTTP State Management Mechanism §User Agent Requirements. When ...
Read more >
How To Manage State with Hooks on React Components
Each of these can be stored in state using the above Hook. ... With Hooks, state objects are completely independent of each other, ......
Read more >
IIE Website Cookie Policy - Institute of International Education
We use cookie technology on our Website, which allows us to ... you do not want cookies to be stored, you may turn...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found