-
-
Save jgthms/951267efd87edd267a8538cee612857a 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