useSyncCallback.ts 515 B

123456789101112131415161718192021
  1. import { useEffect, useState, useCallback } from 'react'
  2. const useSyncCallback = (callback:Function)=> {
  3. const [proxyState, setProxyState] = useState({ current: false })
  4. const Func = useCallback(() => {
  5. setProxyState({ current: true })
  6. }, [proxyState])
  7. useEffect(() => {
  8. if (proxyState.current === true) setProxyState({ current: false })
  9. }, [proxyState])
  10. useEffect(() => {
  11. proxyState.current && callback()
  12. })
  13. return Func
  14. }
  15. export default useSyncCallback