import { useEffect , useState } from 'react' ; import { SetState } from 'type' ; export const useStorage = < T > ( key , initValue ) : [ T , SetState < T > ] => { const [ value , setValue ] = useState < T > ( ( ) => { try { const item = window . localStorage . getItem ( key ) ; return item ? JSON . parse ( item ) : initValue ; } catch ( error ) { console . error ( error ) ; return initValue ; } } ) ; useEffect ( ( ) => { try { window . localStorage . setItem ( key , JSON . stringify ( value ) ) ; } catch ( error ) { console . error ( error ) ; } } , [ key , value ] ) ; return [ value , setValue ] ; } ;