From 5b750a8bc321a40f26ce18c8d3d8fb8f4106359a Mon Sep 17 00:00:00 2001 From: "Thomas F. K. Jorna" Date: Sun, 8 Aug 2021 23:48:37 +0200 Subject: feat: much smoother local mode --- util/persistant-state.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'util') 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( if (calculatedDefaultValue !== storageValue) { storage.update(calculatedDefaultValue) } - - const [value, setValue] = useState(calculatedDefaultValue) + const calculatedDefaultValueObject = + typeof storageValue === 'object' + ? { ...storageValue, ...calculatedDefaultValue } + : calculatedDefaultValue + const [value, setValue] = useState(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)) => { -- cgit v1.2.3