Skip to content

Instantly share code, notes, and snippets.

@schichulin
Forked from kentcdodds/abort-controller.js
Created April 27, 2020 03:10
Show Gist options
  • Save schichulin/0588f494cea750773d0ad75b5c61b475 to your computer and use it in GitHub Desktop.
Save schichulin/0588f494cea750773d0ad75b5c61b475 to your computer and use it in GitHub Desktop.
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
}
@schichulin
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment