DEV Community

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

Collapse
 
sgdxa profile image
𝗗 𝗢 𝗸 𝗼

Hi, thank you for the tutorial. One question from me.

If I have this code

api({
method: 'POST',
url: URL,
params: {
'user-token': Token,
},
});

*api is from axios instance

where should i put the cancelToken?

Collapse
 
tmns profile image
tmns

Hey there Diko!

In your code the cancel token would go within that same config object. So something like:

api({
  method: 'POST',
  url: URL,
  params: {
    'user-token': Token,
  },
  cancelToken: source.token
});
Enter fullscreen mode Exit fullscreen mode

Note however that if you're using at least Axios v0.22.0 the cancel token API has been deprecated in favor of a fetch-like abort controller.