import { useEffect , useState , useRef } from 'react' ; param instance param props param propsName export function useSetStatus < T extends { getStatus : ( ) => any ; setStatus : ( opt : any ) => void ; } , F = { } > ( instance , props = { } as F , propsName = [ ] ) { propsName . forEach ( ( name ) => { const eName = name as keyof F ; const [ state , setState ] = useState ( props [ eName ] ) ; useEffect ( ( ) => { if ( instance && props [ eName ] !== undefined ) { if ( props [ eName ] !== state ) { const status = instance . getStatus ( ) ; instance . setStatus ( { ... status , [ eName ] : props [ eName ] } ) ; setState ( props [ eName ] ) ; } } } , [ instance , props [ eName ] ] ) ; } ) ; } /**
 * 针对 Overlay 类型的组件，有公共的是否显示 对象处理
 * 通过参数 `visiable` 来控制执行 `show()` or `hide()`
 */ export function useVisiable < T extends { show : ( ) => void ; hide : ( ) => void ; } > ( instance , visiable ? ) { const [ state , setState ] = useState ( visiable ) ; useEffect ( ( ) => { if ( instance && visiable !== undefined ) { if ( visiable ) { instance . show && instance . show ( ) ; } else { instance . hide && instance . hide ( ) ; } if ( visiable !== state ) { setState ( visiable ) ; } } } , [ instance , visiable ] ) ; } example export function usePrevious < T > ( value ) { const ref = useRef < T > ( ) ; useEffect ( ( ) => { ref . current = value ; } ) ; return ref . current ; } export type EventNameType = 'LowerCase' ; param instance param props param eventName example export function useEventProperties < T extends AMap . MapEventListener < any > , F > ( instance , props = { } as F , eventName = [ ] , type ? ) { eventName . forEach ( ( name ) => { const eventName = name as keyof F ; const eventHandle = props [ eventName ] ; useEffect ( ( ) => { if ( ! instance ) return ; let eName = name . toLocaleLowerCase ( ) . replace ( /^on/ , '' ) ; if ( eventHandle && eName ) { instance . on ( eName , eventHandle ) ; } return ( ) => { if ( eName && eventHandle ) { instance . off ( eName , eventHandle ) ; } } ; } , [ instance , props [ eventName ] ] ) ; } ) ; } param instance param props param propsName example export function useSettingProperties < T , F = { } > ( instance = { } as T , props = { } as F , propsName = [ ] ) { propsName . forEach ( ( name ) => { const eName = `set${ name }` as keyof T ; const vName = `${ name . charAt ( 0 ) . toLowerCase ( ) }${ name . slice ( 1 ) }` as keyof F ; const [ state , setState ] = useState ( props [ vName ] ) ; useEffect ( ( ) => { if ( instance && props [ vName ] !== undefined ) { if ( props [ vName ] !== state && instance [ eName ] && typeof instance [ eName ] === 'function' ) { ( instance [ eName ] as any ) ( props [ vName ] ) ; setState ( props [ vName ] ) ; } } } , [ instance , props [ vName ] ] ) ; } ) ; }