Last active
March 21, 2021 15:38
-
-
Save ykzts/cf2267eb52436f944c3908be0bf759f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useMemo, useRef } from 'react' | |
function useAbortSignal(): AbortSignal | undefined { | |
const controller = useRef( | |
typeof AbortController !== 'undefined' ? new AbortController() : undefined | |
) | |
const signal = useMemo(() => controller.current?.signal, [controller.current]) | |
useEffect( | |
() => () => { | |
controller.current?.abort() | |
}, | |
[] | |
) | |
return signal | |
} | |
export default useAbortSignal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment