-
-
Save schichulin/0588f494cea750773d0ad75b5c61b475 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
function useAbortController() { | |
const abortControllerRef = React.useRef() | |
const getAbortController = React.useCallback(() => { | |
if (!abortControllerRef.current) { | |
abortControllerRef.current = new AbortController() | |
} | |
return abortControllerRef.current | |
}, []) | |
React.useEffect(() => { | |
return () => getAbortController().abort() | |
}, [getAbortController]) | |
const getSignal = React.useCallback(() => getAbortController().signal, [ | |
getAbortController, | |
]) | |
return getSignal | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://developers.google.com/web/updates/2017/09/abortable-fetch