import { useState , useCallback } from 'react' ; type HookSignature < T > = [ T , ( value : T ) => void ] ; export function useLocalStorage < T > ( key , initialValue ) { const [ keyValue , setKeyValue ] = useState < T > ( ( ) => { try { let value = window . localStorage . getItem ( key ) ; return value !== null ? JSON . parse ( value ) : initialValue ; } catch ( error ) { return initialValue ; } } ) ; const setValue = useCallback ( ( value ) => { try { window . localStorage . setItem ( key , JSON . stringify ( value ) ) ; } catch ( e ) { } setKeyValue ( value ) ; } , [ key , setKeyValue ] ) ; return [ keyValue , setValue ] ; }