Skip to content

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

Open
vadzim opened this issue May 28, 2020 · 10 comments
Open

Comments

@vadzim
Copy link

vadzim commented May 28, 2020

I believe useLocalStorage should fire and update its value when local storage is changed from another browser tab, isn't it?

@zhengbigbig
Copy link

Component which used useLocalStroage should be forced to mount again.

@zhengbigbig
Copy link

If you read and write it frequently, suggest you encapsulate it yourself, It could be costly.

@sungwoo91
Copy link

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>
  );
};

@Svish
Copy link
Contributor

Svish commented Feb 22, 2021

I tried registering for storage events in a local useLocalStorage variant of mine, and... found it was very easy to end up in infinite update-loops. If it's going to be a feature in this library, it should be very well tested.

@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

@innrvoice
Copy link

Draft PR for possible solution #2307

@innrvoice
Copy link

@streamich fyi

@innrvoice
Copy link

Can anyone ping one of the maintainers or contributors. My pr stuck in workflow because I am "First-time contributor".
@vadzim @Svish @sungwoo91

Thanks!

@Svish
Copy link
Contributor

Svish commented Mar 31, 2022

@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.

@innrvoice
Copy link

@Svish thank you for the answer. Its a pity that this project is no longer needed.

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

6 participants