LinkLoom_1 / hooks /useLocalStorage.ts
tahiryamin's picture
feat: Initialize LinkLoom project structure
ed3d7c3
raw
history blame contribute delete
863 Bytes
// FIX: Import `React` to use for type annotations like `React.Dispatch`.
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>(() => {
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];
}