import { useState } from "https://esm.sh/react";
export function useLocalStorage<T>(key: string, initialValue: T) {
const [val, setStored] = useState<T>(() => {
try {
const item = window.localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
} catch (error) {
console.log(error);
return initialValue;
}
});
function setVal(value: T): void {
try {
setStored(value);
window.localStorage.setItem(key, JSON.stringify(value));
} catch (error) {
console.log(error);
}
}
return [val, setVal];
}