Last active
May 17, 2022 17:18
-
-
Save ljaviertovar/0bf9218f550d4f17863dbb16ef8e38dc to your computer and use it in GitHub Desktop.
Clean up function in React, useEffect
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(() => { | |
// create a controller | |
const controller = new AbortController(); | |
axios | |
.get(API, { | |
signal: controller.signal | |
}) | |
.then({data}) => { | |
// handle success | |
setList(data); | |
}) | |
.catch((err) => { | |
// Handle the error | |
}); | |
//aborts the request when the component umounts | |
return () => controller?.abort(); | |
}, []); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment