28 lines
757 B
TypeScript
28 lines
757 B
TypeScript
|
|
import { useState } from 'preact/hooks';
|
||
|
|
|
||
|
|
function usePersistedState<T>(key: string, defaultValue: T): [T, (value: T) => void] {
|
||
|
|
const [state, setState] = useState<T>(() => {
|
||
|
|
try {
|
||
|
|
const item = localStorage.getItem(key);
|
||
|
|
return item ? JSON.parse(item) : defaultValue;
|
||
|
|
} catch (error) {
|
||
|
|
console.warn(`Error reading localStorage key "${key}":`, error);
|
||
|
|
return defaultValue;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
const setPersistedState = (value: T) => {
|
||
|
|
try {
|
||
|
|
setState(value);
|
||
|
|
localStorage.setItem(key, JSON.stringify(value));
|
||
|
|
} catch (error) {
|
||
|
|
console.warn(`Error writing to localStorage key "${key}":`, error);
|
||
|
|
setState(value);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
return [state, setPersistedState];
|
||
|
|
}
|
||
|
|
|
||
|
|
export default usePersistedState;
|