Skip to content

Instantly share code, notes, and snippets.

@chrisdhanaraj
Created March 1, 2019 17:21
Show Gist options
  • Select an option

  • Save chrisdhanaraj/9aeee475e74546b9d73416ce02cb8d1b to your computer and use it in GitHub Desktop.

Select an option

Save chrisdhanaraj/9aeee475e74546b9d73416ce02cb8d1b to your computer and use it in GitHub Desktop.
Showcase how to use refs + hooks to only have a single request at a time
// in some file
const request = useRef();
useEffect(() => {
if (request.current !== undefined) {
request.current.cancel("Cancelled");
}
request.current = axios.CancelToken.source();
axios
.post('http://myawesomeurl', {
somedatahere: somedata
}, {
cancelToken: request.current.token
})
.then(data => console.log(data)
.catch(err => console.log(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment