ui: fix usePersistedState erroring when key doesn't exist

This commit is contained in:
Matthew Penner 2021-08-02 12:23:38 -06:00
parent 1d9927d7f8
commit 89a0244cf2

View file

@ -6,7 +6,11 @@ export function usePersistedState<S = undefined> (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);