Fixed some potential problem with the uselocalstorage
This commit is contained in:
parent
80f78d9940
commit
a780db9b0f
|
@ -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];
|
||||||
|
|
Loading…
Reference in New Issue