From a780db9b0fbb4c507803386e91cd2246e39e2395 Mon Sep 17 00:00:00 2001 From: DrMint Date: Sat, 23 Apr 2022 23:28:52 +0200 Subject: [PATCH] Fixed some potential problem with the uselocalstorage --- src/hooks/useStateWithLocalStorage.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hooks/useStateWithLocalStorage.ts b/src/hooks/useStateWithLocalStorage.ts index a20f08f..5981b2e 100644 --- a/src/hooks/useStateWithLocalStorage.ts +++ b/src/hooks/useStateWithLocalStorage.ts @@ -10,7 +10,7 @@ export default function useStateWithLocalStorage( useEffect(() => { try { const item = localStorage.getItem(key); - if (item !== undefined && item !== null) { + if (item !== "undefined" && item !== null) { setValue(JSON.parse(item) as T); } else { setValue(initialValue); @@ -23,7 +23,7 @@ export default function useStateWithLocalStorage( }, [initialValue, key]); useEffect(() => { - localStorage.setItem(key, JSON.stringify(value)); + if (value !== undefined) localStorage.setItem(key, JSON.stringify(value)); }, [value, key]); return [value, setValue];