import { useState } from "react";
export function useLocalStorage<T>(keyName: string, defaultValue: T) {
const [storedValue, setStoredValue] = useState(() => {
try {
const value = window.localStorage.getItem(keyName);
return value ? JSON.parse(value) : defaultValue;
} catch (err) {
return defaultValue;
}
});
const setValue = (newValue: T) => {
try {
window.localStorage.setItem(keyName, JSON.stringify(newValue));
} finally {
setStoredValue(newValue);
}
};
return [storedValue, setValue];
}