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];