diff options
author | Thomas F. K. Jorna <[email protected]> | 2021-08-08 14:49:47 +0200 |
---|---|---|
committer | Thomas F. K. Jorna <[email protected]> | 2021-08-08 14:49:47 +0200 |
commit | 86a6de7db06faad004386291ce6af3372132b2b1 (patch) | |
tree | 71c5e7d65f53e05919d926f4a173c09f0d0bc89a /util/persistant-state.ts | |
parent | c745d07018a46b1a20b9f571d999ecf7a092c2e1 (diff) |
fix: #39 and similar issues
Diffstat (limited to 'util/persistant-state.ts')
-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)) => { |