diff options
author | Thomas F. K. Jorna <[email protected]> | 2021-08-08 23:48:37 +0200 |
---|---|---|
committer | Thomas F. K. Jorna <[email protected]> | 2021-08-08 23:48:37 +0200 |
commit | 5b750a8bc321a40f26ce18c8d3d8fb8f4106359a (patch) | |
tree | d2a16e52246db1a127afd84028214a7d5ed07045 /util/persistant-state.ts | |
parent | c745d07018a46b1a20b9f571d999ecf7a092c2e1 (diff) |
feat: much smoother local mode
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)) => { |