diff options
-rw-r--r-- | util/persistant-state.ts | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/util/persistant-state.ts b/util/persistant-state.ts index 5e7e35e..707c029 100644 --- a/util/persistant-state.ts +++ b/util/persistant-state.ts @@ -14,13 +14,16 @@ export function usePersistantState<V>( if (calculatedDefaultValue !== storageValue) { storage.update(calculatedDefaultValue) } - - const [value, setValue] = useState<V>(calculatedDefaultValue) + const calculatedDefaultValueObject = + typeof storageValue === 'object' + ? { ...storageValue, ...calculatedDefaultValue } + : calculatedDefaultValue + const [value, setValue] = useState<V>(calculatedDefaultValueObject) // change state gracefully when changing the storageKey useEffect(() => { - if (value !== calculatedDefaultValue) { - setValue(calculatedDefaultValue) + if (value !== calculatedDefaultValueObject) { + setValue(calculatedDefaultValueObject) } }, [storageKey]) const set = (newValueOrFn: V | ((v: V) => V)) => { |