Skip to content

Commit

Permalink
feat: listen to storage event
Browse files Browse the repository at this point in the history
  • Loading branch information
vaakian committed Aug 19, 2023
1 parent 2ff3ed9 commit a0995dd
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/hooks/src/createUseStorageState/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useState } from 'react';
import useEventListener from '../useEventListener';
import useMemoizedFn from '../useMemoizedFn';
import useUpdateEffect from '../useUpdateEffect';
import { isFunction, isUndef } from '../utils';
Expand Down Expand Up @@ -78,6 +79,12 @@ export function createUseStorageState(getStorage: () => Storage | undefined) {
}
};

useEventListener('storage', (event) => {
if (event.key === key) {
setState(getStoredValue());
}
});

return [state, useMemoizedFn(updateState)] as const;
}
return useStorageState;
Expand Down

0 comments on commit a0995dd

Please sign in to comment.