Created
May 2, 2019 09:10
-
-
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
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 } 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