DEV Community

Discussion on: useCancelToken: a custom React hook for cancelling Axios requests

Collapse
 
tmns profile image
tmns

Hey Mostafa!

Thanks for the thorough reply and bringing up the issue about creating multiple cancel tokens within the same component. It's true that if you're going to be using it in several different calls like that you'd either want to just create one cancel token and export it directly (like you suggested) or create the cancel token once in the calling component (and storing it in a ref if it may rerender). I think your approach is the simplest as long as a function isn't specifically needed for some reason. I'll add a little note to the article pointing to your comment πŸ™Œ

As for your other point about the use cases of the hook. Yes, this is only about unmounting. There are maaaany cases where you may want to cancel a request, which may not even require a dependency change (e.g. network issues and race conditions). For those cases I've typically just handled them manually like you suggested, as they tend to be more difficult to easily abstract than the classic "if component unmounts." If you come up with a better way though let me know!

Have a great one!

Collapse
 
mostafaomar98 profile image
MostafaOmar98

Thanks for the reply and the feature on the article! Have a great one too!