Skip to content

Instantly share code, notes, and snippets.

@ljaviertovar
Last active May 17, 2022 17:18
Show Gist options
  • Save ljaviertovar/0bf9218f550d4f17863dbb16ef8e38dc to your computer and use it in GitHub Desktop.
Save ljaviertovar/0bf9218f550d4f17863dbb16ef8e38dc to your computer and use it in GitHub Desktop.
Clean up function in React, useEffect
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