Created
January 20, 2020 20:10
-
-
Save bartcis/00137d09549e667c8b2b5710725ba012 to your computer and use it in GitHub Desktop.
Example aborting of multiple signals on destroy
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
useEffect(() => { | |
const abortController = new AbortController(); | |
const {signal} = abortController; | |
const apiCall = async path => { | |
try { | |
const request = await fetch(path, { | |
signal: signal, | |
method: 'GET', | |
}); | |
const response = await request.json(); | |
setState([response]); | |
} catch (e) { | |
if (!signal?.aborted) { | |
console.error(e); | |
} | |
} | |
}; | |
const abortClickRequests = () => { | |
abortFuncs.current.map(abort => abort()); | |
} | |
apiCall('https://jsonplaceholder.typicode.com/posts/1'); | |
return () => { | |
abortClickRequests(); | |
abortController.abort(); | |
}; | |
}, [setState]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment