New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useLocalStorage
should sync with localStorage
#1504
Comments
I am facing the similar issue. The problem should be the |
If possible you should share the ref in a separate composable in this case (e.g. export function usePreferredLanguage() {
return {
lang: useStorage('preferredCodeLanguage', 'ts')
}
} then in your component const { lang } = usePreferredLanguage(); |
Ran into this myself today. Glad to see it's already on github! |
Do we have a solution for this? |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
👀 |
I have been experiencing this with vue3 as well... if you reload the page, value loads correctly, but never seems to track value until next reload. |
I'm not sure if this is possible. The problem is in the storage listeners, as it is written in the MDN docs, they will not work on the same page, only on different tabs. |
@harmyderoman You're correct. I was not aware of that. Probably would be good to add a disclaimer to the doc. I totally expected it to be fully reactive within the context of the same page. Guess I am out of luck. |
@nolde don't be upset. Vueuse already has a solution for you. Just use createGlobalState. Like this
|
@harmyderoman Ooooh that's interesting. I'll take a look into that! Thanks a lot mate! |
Personally, I do monkey patching on For people who want to try this method:
function hookBefore(target: any, methodName: string, hookFn: Function) {
const original = target[methodName]
target[methodName] = function (...args: any[]) {
hookFn(...args)
return original.apply(this, args)
}
}
function tryPatchLocalStorage() {
// @ts-expect-error mark it as patched
if (localStorage.setItem.__patched__) return
// @ts-expect-error mark it as patched
localStorage.setItem.__patched__ = true
hookBefore(localStorage, 'setItem', (key: string, newValue: string) => {
const oldValue = localStorage.getItem(key)
const event = new StorageEvent('storage', {
key,
newValue,
oldValue,
storageArea: localStorage,
})
nextTick(() => window.dispatchEvent(event))
})
hookBefore(localStorage, 'removeItem', (key: string) => {
const oldValue = localStorage.getItem(key)
const event = new StorageEvent('storage', {
key,
newValue: undefined,
oldValue,
storageArea: localStorage,
})
nextTick(() => window.dispatchEvent(event))
})
} |
Fixed by 800f74f |
Seems problem is not solved yet |
Clear and concise description of the problem
I am using the card code component which lets you change the code language
ts
orjs
. I useuseLocalStorage
in my card component to get and set language to show in the card.However, If I change the language from one card then it doesn't get reflected in other cards until I refresh or come back from another route.
Playground
Suggested solution
useLocalStorage
can accept a arg to allow sync between instances.Alternative
None
Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: