From 89a0244cf23dba91d2b3638990c3fcbb710f0863 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Mon, 2 Aug 2021 12:23:38 -0600 Subject: [PATCH] ui: fix usePersistedState erroring when key doesn't exist --- resources/scripts/plugins/usePersistedState.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/resources/scripts/plugins/usePersistedState.ts b/resources/scripts/plugins/usePersistedState.ts index 007e3fcde..034e81e1c 100644 --- a/resources/scripts/plugins/usePersistedState.ts +++ b/resources/scripts/plugins/usePersistedState.ts @@ -6,7 +6,11 @@ export function usePersistedState (key: string, defaultValue: S): try { const item = localStorage.getItem(key); - return JSON.parse(item || (String(defaultValue))); + if (item === null) { + return defaultValue; + } + + return JSON.parse(item || String(defaultValue)); } catch (e) { console.warn('Failed to retrieve persisted value from store.', e);