Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Axios Cancelation Hook
import axios from 'axios';
import { useEffect } from 'react';
function useEffectAsync(callbackAsync, useEffectDeps) {
var source = axios.CancelToken.source();
useEffect(()=> {
callbackAsync(source.token)
.catch(error => {
if(!axios.isCancel(error)) {
throw error;
}
})
return () => source.cancel()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, useEffectDeps)
}
export default useEffectAsync;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment