diff options
Diffstat (limited to 'packages/web-util/src/hooks')
-rw-r--r-- | packages/web-util/src/hooks/index.ts | 13 | ||||
-rw-r--r-- | packages/web-util/src/hooks/useLocalStorage.ts | 36 |
2 files changed, 29 insertions, 20 deletions
diff --git a/packages/web-util/src/hooks/index.ts b/packages/web-util/src/hooks/index.ts index 9ac56c4ac..393a6fcbb 100644 --- a/packages/web-util/src/hooks/index.ts +++ b/packages/web-util/src/hooks/index.ts @@ -1,4 +1,11 @@ - export { useLang } from "./useLang.js"; -export { useLocalStorage, useNotNullLocalStorage } from "./useLocalStorage.js" -export { useAsyncAsHook, HookError, HookOk, HookResponse, HookResponseWithRetry, HookGenericError, HookOperationalError } from "./useAsyncAsHook.js"
\ No newline at end of file +export { useLocalStorage, useNotNullLocalStorage } from "./useLocalStorage.js"; +export { + useAsyncAsHook, + HookError, + HookOk, + HookResponse, + HookResponseWithRetry, + HookGenericError, + HookOperationalError, +} from "./useAsyncAsHook.js"; 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; |