You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe. useLocalStorage internally keeps a copy of the stored value using useState, if this state is updated within one component the change won't be reflected in other components using the same hook with the same key.
Describe the solution you'd like
A short note in the docs describing that useLocalStorage should only be used in a single component instance for a given key. If used in multiple locations a custom version of useLocalStorage must be implemented that keeps its state in a global store (eg. redux).
I used this hook without thinking and spent 1-hour debugging strange behavior. The issue was obvious once I looked at the underlying code. Seems like an easy trap to fall into without a clear warning in the docs.
It could also be that useLocalStorage uses a trick I'm unaware of to mimic storing the state globally. In that case, I am probably using the hook incorrectly, and once I figure it out we can add a short description of what I'm doing wrong to the docs.
Describe alternatives you've considered
Deprecate/remove useLocalStorage from the react-use code base, but there are probably too many dependants.
The text was updated successfully, but these errors were encountered:
geanify
added a commit
to geanify/react-use
that referenced
this issue
Feb 19, 2024
Is your feature request related to a problem? Please describe.
useLocalStorage
internally keeps a copy of the stored value usinguseState
, if this state is updated within one component the change won't be reflected in other components using the same hook with the same key.Describe the solution you'd like
A short note in the docs describing that
useLocalStorage
should only be used in a single component instance for a given key. If used in multiple locations a custom version ofuseLocalStorage
must be implemented that keeps its state in a global store (eg. redux).I used this hook without thinking and spent 1-hour debugging strange behavior. The issue was obvious once I looked at the underlying code. Seems like an easy trap to fall into without a clear warning in the docs.
It could also be that
useLocalStorage
uses a trick I'm unaware of to mimic storing the state globally. In that case, I am probably using the hook incorrectly, and once I figure it out we can add a short description of what I'm doing wrong to the docs.Describe alternatives you've considered
Deprecate/remove
useLocalStorage
from the react-use code base, but there are probably too many dependants.The text was updated successfully, but these errors were encountered: