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.

`useLocalStorage` should sync with localStorage

See original GitHub issue

Clear and concise description of the problem

I am using the card code component which lets you change the code language ts or js. I use useLocalStorage 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

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:5
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
harmyderomancommented, Oct 24, 2022

@nolde don’t be upset. Vueuse already has a solution for you. Just use createGlobalState. Like this

// store.js
import { createGlobalState, useStorage } from '@vueuse/core'

export const useGlobalState = createGlobalState(
  () => useStorage('vueuse-local-storage', 'initialValue'),
)
1reaction
noldecommented, Oct 25, 2022

@harmyderoman Ooooh that’s interesting. I’ll take a look into that! Thanks a lot mate!

Read more comments on GitHub >

github_iconTop Results From Across the Web

useLocalStorage React Hook - useHooks
Sync state to local storage so that it persists through a page refresh. Usage is similar to useState except we pass in a...
Read more >
Keeping React State and localStorage in sync - Thomas Derleth
One common use case that naturally arises is to keep React state and local storage in sync. Though we're building client-side applications, we ......
Read more >
React sync with local storage and update dynamically
You need to mutate the previous state of the array. Try as the following: setStoredValue(prevState => [...prevState, valueToStore]);.
Read more >
Sync to localStorage with React useReducer Hook
I guess we could've created our own useLocalStorage . But anyway, this is still better than having all of this code within the...
Read more >
Using Local Storage in React with Your Own Custom ...
Much like the useState hook, our useLocalStorage hook will return a 2-element array. The first element will be the variable and the second...
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