Created
March 1, 2019 17:21
-
-
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
This file contains hidden or 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
| // 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