Skip to content

Instantly share code, notes, and snippets.

@ykzts
Last active March 21, 2021 15:38
Show Gist options
  • Save ykzts/cf2267eb52436f944c3908be0bf759f7 to your computer and use it in GitHub Desktop.
Save ykzts/cf2267eb52436f944c3908be0bf759f7 to your computer and use it in GitHub Desktop.
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