-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
useLocalStorage doesn't fire when localStorage is changed from another tab #1245
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
Comments
Component which used useLocalStroage should be forced to mount again. |
If you read and write it frequently, suggest you encapsulate it yourself, It could be costly. |
I think useLocalStorage just modify the value in the window.localStorage. So you should register storage eventListener to update another tab page. Like below const Demo = () => {
const [value, setValue] = useLocalStorage('hello-key', 'foo', {raw: true});
const [removableValue, setRemovableValue, remove] = useLocalStorage('removeable-key');
const callback = React.useCallback((e) => {
setValue(e.newValue);
}, [setValue]);
useEvent('storage', callback);
return (
<div>
<div>Value: {value}</div>
<button onClick={() => setValue('bar')}>bar</button>
<button onClick={() => setValue('baz')}>baz</button>
<br />
<br />
<div>Removable Value: {removableValue}</div>
<button onClick={() => setRemovableValue('foo')}>foo</button>
<button onClick={() => setRemovableValue('bar')}>bar</button>
<button onClick={() => remove()}>Remove</button>
</div>
);
}; |
I tried registering for storage events in a local |
If anyone interested it is implemented in upcoming hook of |
Draft PR for possible solution #2307 |
@streamich fyi |
Can anyone ping one of the maintainers or contributors. My pr stuck in workflow because I am "First-time contributor". Thanks! |
@innrvoice It's not stuck because you're a "First-time contributor", it's stuck because this project is kind of dead. If you look at the history, there haven't been any proper PR's merged for a long time. |
@Svish thank you for the answer. Its a pity that this project is no longer needed. |
I believe
useLocalStorage
should fire and update its value when local storage is changed from another browser tab, isn't it?The text was updated successfully, but these errors were encountered: