Fixed some potential problem with the uselocalstorage

This commit is contained in:
DrMint 2022-04-23 23:28:52 +02:00
parent 80f78d9940
commit a780db9b0f
1 changed files with 2 additions and 2 deletions

View File

@ -10,7 +10,7 @@ export default function useStateWithLocalStorage<T>(
useEffect(() => { useEffect(() => {
try { try {
const item = localStorage.getItem(key); const item = localStorage.getItem(key);
if (item !== undefined && item !== null) { if (item !== "undefined" && item !== null) {
setValue(JSON.parse(item) as T); setValue(JSON.parse(item) as T);
} else { } else {
setValue(initialValue); setValue(initialValue);
@ -23,7 +23,7 @@ export default function useStateWithLocalStorage<T>(
}, [initialValue, key]); }, [initialValue, key]);
useEffect(() => { useEffect(() => {
localStorage.setItem(key, JSON.stringify(value)); if (value !== undefined) localStorage.setItem(key, JSON.stringify(value));
}, [value, key]); }, [value, key]);
return [value, setValue]; return [value, setValue];