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

useLocalStorage not updating if many components watch the same key

Open TylerR909 opened this issue 6 years ago • 12 comments

Note: I'm already working on a PR for this.

What is the current behavior? Multiple calls to useLocalStorage('myKey') from different components are not staying in sync as the value changes.

Steps to reproduce it and if possible a minimal demo of the problem.

Seems that all the Sandboxes are failing to install react-use right now but slap this somewhere and the problem will become apparent:

import useLocalStorage from 'react-use/lib/useLocalStorage';

const Increment = () => {
  const [count, setCount] = useLocalStorage('count', 0);
  return (<h1 onClick={() => setCount(count+1)}>Increment</h1>);
}

const Decrement = () => {
  const [count, setCount] = useLocalStorage('count', 0);
  return (<h1 onClick={() => setCount(count-1)}>Decrement</h1>);
}

const App = () => (
  <>
    <Increment />
    <Decrement />
  </>
)

Basically it's initializing from localStorage into individual instances of useState and then never reading from localStorage again, so if it's changed out from under you... (1) you never see the update and (2) if you update it you clobber whatever was there before.

What is the expected behavior?

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

TylerR909 avatar Nov 20 '19 08:11 TylerR909

Any news on this issue? The problem is still here. Could I help making this fix released? Here is a sandbox reproducing the problem: https://codesandbox.io/s/peaceful-hopper-l3ksy?file=/src/App.tsx

nicgirault avatar Apr 20 '20 06:04 nicgirault

Looks like it got merged into v14.0 which isn't on master yet. But is also 75 commits ahead, 121 commits behind master.

Also the unit test that specifically checks against this ticket is commented out. https://github.com/streamich/react-use/blob/v14.0/tests/useLocalStorage.test.ts#L86

TylerR909 avatar Apr 20 '20 18:04 TylerR909

Dang. I was hoping to replace a use of https://github.com/astoilkov/use-local-storage-state with this. Guess I won't be doing that.

gimbo avatar Mar 29 '21 19:03 gimbo

If anyone interested it is implemented in upcoming hook of @react-hookz/web react-hookz/web#43

xobotyi avatar May 15 '21 15:05 xobotyi

Nice one @xobotyi — when I saw your comment at first (not knowing who you were) I thought "oh dear, somebody with yet another hook library promoting it here instead of contributing" but then I looked into it and saw what you're doing with react-hooks/web and your long history with react-use, and your reasons for leaving/starting afresh there. I'll definitely give react-hooks a try, and I expect I'll be eagerly watching its growth. Unfortunately my JS/TS expertise is more "consumer" than "contributor", else I'd get involved — hope you get the support you need!

gimbo avatar May 17 '21 11:05 gimbo

This hook remains unusable except in simple cases.

bertho-zero avatar Jan 25 '22 10:01 bertho-zero

There are still issues with this hook not syncing correct values when watching the same key in multiple components

dsgriffin avatar Jul 05 '23 08:07 dsgriffin

Here looking at this in 2023 and it's still not resolved. Having to use https://github.com/astoilkov/use-local-storage-state instead. Please fix this issue or remove the functionality.

warbler9000 avatar Oct 20 '23 16:10 warbler9000

This hook is still broken, there's no getItem anywhere in the source, so it never syncs with the localStorage values.

alexgorbatchev avatar Nov 28 '23 21:11 alexgorbatchev