Skip to content

Instantly share code, notes, and snippets.

@capJavert
Created May 2, 2019 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capJavert/b29d5dfd13f27e453ff81834871d7cdf to your computer and use it in GitHub Desktop.
Save capJavert/b29d5dfd13f27e453ff81834871d7cdf to your computer and use it in GitHub Desktop.
WIP: Uses signal option of fetch method to cleanup request handlers after component unmount
import { useEffect } from 'react'
const noop = () => {}
export default (
url,
options = {},
complete = noop,
error = noop,
final = noop
) => (
useEffect(() => {
const abortController = new AbortController()
const { signal } = abortController
options.signal = signal
fetch(url, options).then(
complete,
error,
final
)
return () => abortController.abort()
}, [])
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment