Skip to content

Instantly share code, notes, and snippets.

@bradyclifford
Created November 4, 2020 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradyclifford/c930c74c4991c80479568f257adf89d6 to your computer and use it in GitHub Desktop.
Save bradyclifford/c930c74c4991c80479568f257adf89d6 to your computer and use it in GitHub Desktop.
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