A simple SEO inspecter Tool, to get social media card previews
at main 25 lines 807 B view raw
1import { useState, useEffect } from 'react'; 2 3export function useLocalStorage<T>(key: string, initialValue: T) { 4 const [storedValue, setStoredValue] = useState<T>(() => { 5 try { 6 const item = window.localStorage.getItem(key); 7 return item ? JSON.parse(item) : initialValue; 8 } catch (error) { 9 console.error('Error reading from localStorage:', error); 10 return initialValue; 11 } 12 }); 13 14 const setValue = (value: T | ((val: T) => T)) => { 15 try { 16 const valueToStore = value instanceof Function ? value(storedValue) : value; 17 setStoredValue(valueToStore); 18 window.localStorage.setItem(key, JSON.stringify(valueToStore)); 19 } catch (error) { 20 console.error('Error writing to localStorage:', error); 21 } 22 }; 23 24 return [storedValue, setValue] as const; 25}