Skip to content
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

New feature -> watch localstorage?? #27

Closed
intergalactic-overlords opened this issue Oct 4, 2019 · 1 comment · Fixed by #30
Closed

New feature -> watch localstorage?? #27

intergalactic-overlords opened this issue Oct 4, 2019 · 1 comment · Fixed by #30
Labels
Hacktoberfest https://hacktoberfest.digitalocean.com/

Comments

@intergalactic-overlords
Copy link

Hi, I was using your module (thanks by the way :-) ) and I encountered a problem. I was in a situation where one component was updating localstorage and another one was 'watching' it. And if localstorage was updated, the watching component needed to rerender with the new data.
But the way the hook is written, it doesn't 'watch' localstorage.

So in my project, I modified your hook and added the following effect:

  useEffect(() => {
    const checkValue = localStorage.getItem(key)
    if (checkValue  !== value) {
      setValue(checkValue )
    }
  });

This way, the state is updated when localstorage changes, and each component using the hook rerenders with the new data. For me it works perfectly, but perhaps you might see a downside (besides added complexity and extra calls to localStorage).
It could also be hidden behind an option.

So anyhow, do want you want with it :-) but I just wanted to let you know what I've done to solve my problem :-)

@dance2die
Copy link
Owner

dance2die commented Oct 4, 2019

Thank you for the nice suggestion/find @intergalactic-overlords 👍

Could you do a PR for the code & add a test for it?
(https://hacktoberfest.digitalocean.com 😉)
because that does make the value in sync.

@dance2die dance2die added Hacktoberfest https://hacktoberfest.digitalocean.com/ and removed Hacktoberfest https://hacktoberfest.digitalocean.com/ labels Oct 4, 2019
dance2die added a commit that referenced this issue Nov 24, 2019
Watch changes on storage and change state (closes #27)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hacktoberfest https://hacktoberfest.digitalocean.com/
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants