useStorage reactivity between components not working
See original GitHub issueDescribe the bug
If two components share the same local storage they do not respond reactive if one of them changes the value of the binding. After a page reload they are in sync. But if the value is changed again they are not reactive as before.
Reproduction
System Info
System:
OS: macOS 12.3.1
CPU: (8) arm64 Apple M1
Memory: 143.25 MB / 16.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.13.0 - /usr/local/bin/node
Yarn: 1.22.17 - ~/.yarn/bin/yarn
npm: 8.1.0 - /usr/local/bin/npm
Browsers:
Brave Browser: 101.1.38.109
Chrome: 101.0.4951.54
Firefox: 98.0.2
Safari: 15.4
npmPackages:
@vueuse/core: ^8.4.2 => 8.4.2
vue: ^3.2.33 => 3.2.33
Used Package Manager
yarn
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a VueUse issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion.
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:7 (2 by maintainers)
Top Results From Across the Web
useStorage - VueUse
Reactive LocalStorage/SessionStorage ... When using with Nuxt 3, this functions will NOT be auto imported in favor of Nitro's built-in useStorage() .
Read more >vue.js - start useStore() from a composable after a click event
Composables are primarily supposed to be used when component ... Since the use of a composable causes problem, it needs to be justified....
Read more >How to Make localStorage Reactive in Vue - CSS-Tricks
Let's answer that that question, and while we're at it, make Vue reactivity work with localStorage . If we were to run the...
Read more >use-reactive-storage - npm
If you are using React 15, use-reactive-storage will still work, but it requires that you wrap your functional components with the useStorage ......
Read more >Using localStorage with React Hooks - LogRocket Blog
Learn how using localStorage with React Hooks can persist user information in browser storage and share logic between multiple components.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for your reply. Regarding the watcher: If I remove the value from localStorage it gets immediately updated, so isn’t there a watcher?
Also, if you open this link in two separate windows the changes are visible immediately with localStorage acting as a single source of truth: https://vueuse.org/core/usestorage/#demo
This is why I do not understand that it does not work in my case.
I think this makes sense. You are essentially initialising two refs in two different components. They are both retrieving their initial value from storage, but they are then independent of each other.
If we unpack
useStorage
, what you essentially have here is:When
foo
changes in the above, it will update the locally stored value. However,bar
does not have any kind of watcher on localStorage. It won’t be updated until the component is initialised again, andinitialValue
is retrieved again.In your use case, you need to actually use the same ref in both components. Do this in the usual Vue way by passing it as a prop, injecting it, or exporting the ref from a composable .js/.ts file.