Created
October 1, 2021 09:19
-
-
Save Kolenov/095c68f0d1033943960af2b7dedd268a to your computer and use it in GitHub Desktop.
AbortController
This file contains hidden or 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() // creating an AbortController | |
fetch(url, { signal: abortController.signal }) // passing the signal to the query | |
.then(data => { | |
setState(data) // if everything went well, set the state | |
}) | |
.catch(error => { | |
if (error.name === 'AbortError') return // if the query has been aborted, do nothing | |
throw error | |
}) | |
return () => { | |
abortController.abort() // stop the query by aborting on the AbortController on unmount | |
} | |
}, []) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment