diff --git a/src/hooks/useLocalStorage.tsx b/src/hooks/useLocalStorage.tsx index 10579cd..da1c641 100644 --- a/src/hooks/useLocalStorage.tsx +++ b/src/hooks/useLocalStorage.tsx @@ -1,6 +1,7 @@ import React, { useMemo } from 'react' import { getLocalStorageItem, + mergeWithInitialValue, removeLocalStorageItem, setLocalStorageItem } from 'utils' @@ -10,17 +11,6 @@ const useLocalStorageSubscribe = (callback: () => void) => { return () => window.removeEventListener('storage', callback) } -function mergeWithInitialValue(storedValue: T, initialValue: T): T { - if ( - !Array.isArray(storedValue) && - typeof storedValue === 'object' && - storedValue !== null - ) { - return { ...initialValue, ...storedValue } - } - return storedValue -} - export function useLocalStorage( key: string, initialValue: T diff --git a/src/hooks/useSessionStorage.tsx b/src/hooks/useSessionStorage.tsx index cc0756f..ca194e6 100644 --- a/src/hooks/useSessionStorage.tsx +++ b/src/hooks/useSessionStorage.tsx @@ -1,6 +1,7 @@ import React, { useMemo } from 'react' import { getSessionStorageItem, + mergeWithInitialValue, removeSessionStorageItem, setSessionStorageItem } from 'utils' @@ -10,17 +11,6 @@ const useSessionStorageSubscribe = (callback: () => void) => { return () => window.removeEventListener('sessionStorage', callback) } -function mergeWithInitialValue(storedValue: T, initialValue: T): T { - if ( - !Array.isArray(storedValue) && - typeof storedValue === 'object' && - storedValue !== null - ) { - return { ...initialValue, ...storedValue } - } - return storedValue -} - export function useSessionStorage( key: string, initialValue: T diff --git a/src/utils/utils.ts b/src/utils/utils.ts index e28027d..2f19e8e 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -180,3 +180,14 @@ export const getFallbackPubkey = () => { // Silently ignore } } + +export function mergeWithInitialValue(storedValue: T, initialValue: T): T { + if ( + !Array.isArray(storedValue) && + typeof storedValue === 'object' && + storedValue !== null + ) { + return { ...initialValue, ...storedValue } + } + return storedValue +}