aboutsummaryrefslogtreecommitdiff
path: root/packages/web-util/src/hooks/useLocalStorage.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/web-util/src/hooks/useLocalStorage.ts')
-rw-r--r--packages/web-util/src/hooks/useLocalStorage.ts36
1 files changed, 19 insertions, 17 deletions
diff --git a/packages/web-util/src/hooks/useLocalStorage.ts b/packages/web-util/src/hooks/useLocalStorage.ts
index f518405b6..ab786db13 100644
--- a/packages/web-util/src/hooks/useLocalStorage.ts
+++ b/packages/web-util/src/hooks/useLocalStorage.ts
@@ -35,13 +35,13 @@ export function useLocalStorage(
useEffect(() => {
const listener = buildListenerForKey(key, (newValue) => {
- setStoredValue(newValue ?? initialValue)
- })
- window.addEventListener('storage', listener)
+ setStoredValue(newValue ?? initialValue);
+ });
+ window.addEventListener("storage", listener);
return () => {
- window.removeEventListener('storage', listener)
- }
- }, [])
+ window.removeEventListener("storage", listener);
+ };
+ }, []);
const setValue = (
value?: string | ((val?: string) => string | undefined),
@@ -62,11 +62,14 @@ export function useLocalStorage(
return [storedValue, setValue];
}
-function buildListenerForKey(key: string, onUpdate: (newValue: string | undefined) => void): () => void {
+function buildListenerForKey(
+ key: string,
+ onUpdate: (newValue: string | undefined) => void,
+): () => void {
return function listenKeyChange() {
- const value = window.localStorage.getItem(key)
- onUpdate(value ?? undefined)
- }
+ const value = window.localStorage.getItem(key);
+ onUpdate(value ?? undefined);
+ };
}
//TODO: merge with the above function
@@ -80,16 +83,15 @@ export function useNotNullLocalStorage(
: initialValue;
});
-
useEffect(() => {
const listener = buildListenerForKey(key, (newValue) => {
- setStoredValue(newValue ?? initialValue)
- })
- window.addEventListener('storage', listener)
+ setStoredValue(newValue ?? initialValue);
+ });
+ window.addEventListener("storage", listener);
return () => {
- window.removeEventListener('storage', listener)
- }
- })
+ window.removeEventListener("storage", listener);
+ };
+ });
const setValue = (value: string | ((val: string) => string)): void => {
const valueToStore = value instanceof Function ? value(storedValue) : value;