31 lines
945 B
TypeScript
31 lines
945 B
TypeScript
// FIX: Import React to make the 'React' namespace available for type annotations.
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
export function useLocalStorage<T,>(key: string, initialValue: T): [T, React.Dispatch<React.SetStateAction<T>>] {
|
|
const [storedValue, setStoredValue] = useState<T>(() => {
|
|
if (typeof window === 'undefined') {
|
|
return initialValue;
|
|
}
|
|
try {
|
|
const item = window.localStorage.getItem(key);
|
|
return item ? JSON.parse(item) : initialValue;
|
|
} catch (error) {
|
|
console.error(error);
|
|
return initialValue;
|
|
}
|
|
});
|
|
|
|
useEffect(() => {
|
|
try {
|
|
const valueToStore =
|
|
typeof storedValue === 'function'
|
|
? storedValue(storedValue)
|
|
: storedValue;
|
|
window.localStorage.setItem(key, JSON.stringify(valueToStore));
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
}, [key, storedValue]);
|
|
|
|
return [storedValue, setStoredValue];
|
|
} |