useCallback



  • eab3a352-c61e-4d31-a912-6d1e9e210509-image.png

    hook

    
    import Taro, { useState, useCallback, useRef, useDidHide } from '@tarojs/taro'
    
    export function useBool(initStatus = false){
    
        const [ status, setStatus ] = useState(initStatus)
        
        const setTrue = useCallback(
            () => setStatus(true)
            [ setStatus ],
        )
        
        const setFalse = useCallback(
            () => setStatus(false),
            [ setStatus ],
        )
        
        // 报错
        const toggle = useCallback(
            value => setStatus( prev => value !== undefined ? !!value : !prev  )
            [ setStatus ],
        )
    
        // 正常 
        const toggle = value => setStatus( prev => value !== undefined ? !!value : !prev  )
    
        return [ status, setTrue, setFalse, toggle ]
        
    }
    
    

登录后回复