Skip to content

Commit 5a3024c

Browse files
authored
feat(useStorage): conditionally use event based on the used storage backend (#3822)
Signed-off-by: GitHub <noreply@github.com>
1 parent edab62b commit 5a3024c

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

packages/core/useStorage/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,11 @@ export function useStorage<T extends(string | number | boolean | object | null)>
180180
if (window && listenToStorageChanges) {
181181
tryOnMounted(() => {
182182
// this should be fine since we are in a mounted hook
183-
useEventListener(window, 'storage', update)
184-
useEventListener(window, customStorageEventName, updateFromCustomEvent)
183+
if (storage instanceof Storage)
184+
useEventListener(window, 'storage', update)
185+
else
186+
useEventListener(window, customStorageEventName, updateFromCustomEvent)
187+
185188
if (initOnMounted)
186189
update()
187190
})
@@ -195,7 +198,7 @@ export function useStorage<T extends(string | number | boolean | object | null)>
195198
// send custom event to communicate within same page
196199
// importantly this should _not_ be a StorageEvent since those cannot
197200
// be constructed with a non-built-in storage area
198-
if (window) {
201+
if (window && !(storage instanceof Storage)) {
199202
window.dispatchEvent(new CustomEvent<StorageEventLike>(customStorageEventName, {
200203
detail: {
201204
key,

0 commit comments

Comments
 (0)