react-hooks icon indicating copy to clipboard operation
react-hooks copied to clipboard

`useLocalStorage` not updating if many components watch the same key

Open merelinguist opened this issue 4 years ago • 4 comments

Description

Hey! Thanks for the awesome project. I’m having a problem where multiple calls to useLocalStorage('my-key') from different components are not staying in sync as the value changes.

Reproduction

I’ve reproduced it in this sandbox:

https://codesandbox.io/s/distracted-bash-x9k10?file=/pages/index.js

Expected behavior

If my localStorage value changes, all my components watching that value pick up the change.

Actual behavior

The hook seems to be initializing from localStorage into individual instances of useState and then never reading from localStorage again.

merelinguist avatar Jun 14 '20 22:06 merelinguist

Thank you for reporting!

I omitted storage watching intentionally for performance reasons. However, this seems to be a great use-case, so it should be implemented.

I’ll give it a shot during the weekend. Please keep me reminded if I would forget about it 😅

kripod avatar Jun 18 '20 22:06 kripod

@kripod Thank you so much!

merelinguist avatar Jun 20 '20 11:06 merelinguist

Unfortunately, I cannot allocate time for this task. There are some hard stuff I have to cope with, and I decided to work on other OSS projects of mine, as I find them more interesting.

kripod avatar Jul 17 '20 19:07 kripod

is it something already solved? I can reproduce the same issue, can you indicate how you intentionally omitted the storage watching?

guifeliper avatar Dec 01 '22 10:12 guifeliper