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

useLocalStorage not updating if many components watch the same key #785

Open
TylerR909 opened this issue Nov 20, 2019 · 9 comments
Open

Comments

@TylerR909
Copy link
Contributor

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 TylerR909 mentioned this issue Nov 20, 2019
15 tasks
nspaeth pushed a commit to nspaeth/react-use that referenced this issue Mar 5, 2020
Fixes streamich#785 - useLocalStorage not updating if many components watch the same key

This pull request depends on pull requests streamich#1021 streamich#979
nspaeth pushed a commit to nspaeth/react-use that referenced this issue Mar 5, 2020
Fixes streamich#785 - useLocalStorage not updating if many components watch the same key

This pull request depends on pull requests streamich#1021 streamich#979
nspaeth pushed a commit to nspaeth/react-use that referenced this issue Mar 5, 2020
Fixes streamich#785 - useLocalStorage not updating if many components watch the same key

This pull request depends on pull requests streamich#1021 streamich#979
nspaeth pushed a commit to nspaeth/react-use that referenced this issue Mar 5, 2020
Fixes streamich#785 - useLocalStorage not updating if many components watch the same key

Previously pr streamich#786 addressed this issue by ensuring that other components
watching the key would see new updates, however, those updates would not be
rendered until something else triggered a re-render. This pr resolves that issue.

This pull request depends on pull requests streamich#1021 streamich#979
nspaeth pushed a commit to nspaeth/react-use that referenced this issue Mar 5, 2020
Fixes streamich#785 - useLocalStorage not updating if many components watch the same key

Previously pr streamich#786 addressed this issue by ensuring that other components
watching the key would see new updates, however, those updates would not be
rendered until something else triggered a re-render. This pr resolves that issue.

This pull request depends on pull requests streamich#1021 streamich#979
nspaeth pushed a commit to nspaeth/react-use that referenced this issue Mar 6, 2020
Fixes streamich#785 - useLocalStorage not updating if many components watch the same key

Previously pr streamich#786 addressed this issue by ensuring that other components
watching the key would see new updates, however, those updates would not be
rendered until something else triggered a re-render. This pr resolves that issue.

This pull request depends on pull requests streamich#1021 streamich#979
nspaeth pushed a commit to nspaeth/react-use that referenced this issue Mar 6, 2020
Fixes streamich#785 - useLocalStorage not updating if many components watch the same key

Previously pr streamich#786 addressed this issue by ensuring that other components
watching the key would see new updates, however, those updates would not be
rendered until something else triggered a re-render. This pr resolves that issue.

This pull request depends on pull requests streamich#1021 streamich#979
@nicgirault
Copy link
Contributor

nicgirault commented Apr 20, 2020

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

@TylerR909
Copy link
Contributor Author

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

nspaeth pushed a commit to nspaeth/react-use that referenced this issue Jun 20, 2020
Fixes streamich#785 - useLocalStorage not updating if many components watch the same key

Previously pr streamich#786 addressed this issue by ensuring that other components
watching the key would see new updates, however, those updates would not be
rendered until something else triggered a re-render. This pr resolves that issue.

This pull request depends on pull requests streamich#1021 streamich#979
nspaeth pushed a commit to nspaeth/react-use that referenced this issue Jul 6, 2020
Fixes streamich#785 - useLocalStorage not updating if many components watch the same key

Previously pr streamich#786 addressed this issue by ensuring that other components
watching the key would see new updates, however, those updates would not be
rendered until something else triggered a re-render. This pr resolves that issue.

This pull request depends on pull requests streamich#1021 streamich#979
@gimbo
Copy link

gimbo commented Mar 29, 2021

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.

@xobotyi
Copy link
Contributor

xobotyi commented May 15, 2021

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

@gimbo
Copy link

gimbo commented May 17, 2021

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!

@bertho-zero
Copy link

This hook remains unusable except in simple cases.

@dsgriffin
Copy link

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

@warbler9000
Copy link

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.

@alexgorbatchev
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants