import { useState } from 'react' ; interface LocalStorageState { darkModeEnabled : boolean ; } interface UseLocalStorageI { < K extends keyof LocalStorageState > ( key , initialValue ) : [ LocalStorageState [ K ] , ( v : LocalStorageState [ K ] ) => void ] ; } param key param initialValue const useLocalStorage = ( key , initialValue ) => { const [ storedValue , setStoredValue ] = useState ( ( ) => { try { const item = window . localStorage . getItem ( key ) ; return item ? JSON . parse ( item ) : initialValue ; } catch ( error ) { return initialValue ; } } ) ; const setValue = ( value ) => { try { const valueToStore = value instanceof Function ? value ( storedValue ) : value ; setStoredValue ( valueToStore ) ; window . localStorage . setItem ( key , JSON . stringify ( valueToStore ) ) ; } catch ( error ) { console . error ( error ) ; } } ; return [ storedValue , setValue ] ; } ; export default useLocalStorage ;