Created
November 4, 2020 21:28
-
-
Save bradyclifford/c930c74c4991c80479568f257adf89d6 to your computer and use it in GitHub Desktop.
Axios Cancelation Hook
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
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