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

Removal of localStorage or sessionStorage key is not synced across tabs #1545

Open
develohpanda opened this issue Apr 12, 2024 · 0 comments
Open

Comments

@develohpanda
Copy link

develohpanda commented Apr 12, 2024

Prior Issues

None that I could find

What is the current behavior?

Remove a local storage key in one tab, observe that it does not trigger an update in another tab.

Steps to Reproduce

  1. Open the storybook example in two tabs https://react-hookz.github.io/web/?path=/docs/side-effect-uselocalstoragevalue--example
  2. Edit the value in one tab, notice that it updates in the other tab
  3. Press the "remove storgae value" button in one tab, notice that it does not update in the other tab

What is the expected behavior?

Key removals should be synchronized across tabs

Potential cause

I believe the cause is in this event handler, specifically the evt.newValue condition.

const storageEventHandler = (evt: StorageEvent) => {
if (evt.storageArea && evt.key && evt.newValue) {
invokeStorageKeyListeners(evt.storageArea, evt.key, evt.newValue);
}
};

When a key is removed, evt.newValue is set to null, and thus gets filtered out in this conditional. I have confirmed through while debugging, and the second tab does receive the storage event correctly, but gets filtered out here.

This seems like a bug, because there's an invocation to the event handlers with the value null on the during the remove step, however that only applies to the current tab and is not synced to other tabs.

invokeStorageKeyListeners(storage, key, null);

Environment Details

  • @react-hookz/web version: 23.1.0 and 24.0.4
  • react version: 18.2.0
  • react-dom version: 18.2.0
  • typescript version: 5.4.4
  • OS: Windows
  • Browser: Firefox/Edge/Chrome
  • Did this work in previous versions? Not that I know of
@develohpanda develohpanda changed the title Removal of localStorage or sessionStorage key is not synced across tabs/instances Removal of localStorage or sessionStorage key is not synced across tabs Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant