const abortController = new AbortController();
const doStuff = (payload) => dispatch(thinkAction(payload, abortController.signal));
doStuff(payload);
// to abort it: call abortController.abort();
it's not limited to fetch or axios - you can use AbortSignal for many things, just be careful it might not throw error automatically when used outside of making requests.
Yes I checked their doc, I have trouble cancelling one request via redux. Basically I have my axios in a service file, then I call the axios req in the action. In my component I have a useEffect which runs when require, and save data to api, and whenever I trigger the button, I would like the call to be cancelled. Any help on how to achieve this ? thanks
it does not seem to work, idk why, is there a chance you can take a look at the service and action file that I posted, just to check if I have made any mistake thanks
oh yea about that there are different methods. I think the abort controller should only be created before each request. one way of doing that is useRef
This is what I have so far. I have a modal, this modal is saving data to a database, once I click on the abort, i cancelled the call (to save data) in the useEffect. Let 's say now the user exit the modal and decide some other time to go back on the modal to save its data again, the cancel signal is still on the route. How do I clear up the abort controller ?
in your example it's hard to tell what the issue is or what are you trying to do.
two things that's kinda obvious here:
you are not creating a new AbortController before each request (check my previous example)
your useEffect only fires once (when the component mounts) - the request is not made on demand, which seems to be what you are trying to do here. If you want the request to be made every time the modal is opened, you will need to make sure opening the modal (either rendering the modal, or changing the state) is tied to the effect/request.
also, when you say "it doesn't work" - please elaborate why or how. there could be many reasons for broken code, without errors/logs/descriptions, it's impossible to tell what might be wrong by simply looking at a few lines of code.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
dispatch
also need to be abortable...dispatch
can be aborted if you useredux-thunk
orredux-saga
- only applies to async actions though.for example:
to use it:
it's not limited to
fetch
oraxios
- you can use AbortSignal for many things, just be careful it might not throw error automatically when used outside of making requests.developer.mozilla.org/en-US/docs/W...
You are using fetch, is it working the same for axios or is there some change?
pretty much the same.
axios
can either use an AbortController or a CancelToken, check their examples here: axios-http.com/docs/cancellationYes I checked their doc, I have trouble cancelling one request via redux. Basically I have my axios in a service file, then I call the axios req in the action. In my component I have a useEffect which runs when require, and save data to api, and whenever I trigger the button, I would like the call to be cancelled. Any help on how to achieve this ? thanks
Here is my service file :
my action file
and my component
looks like you are calling
abort
already in the dispatch line instead of providing a signal. same to theonClick
handler. I would do this:it does not seem to work, idk why, is there a chance you can take a look at the service and action file that I posted, just to check if I have made any mistake thanks
could you be a bit more specific though 😅 a minimal example would be helpful.
the method explained in the post definitely works - you probably have something else going on in the app
yes the component seems fine, it is just that in my action and service file, I am not sure I set up the controller properly
I managed to make it work, thank you. I was wondering, how do I clear the abort controller once request cancelled ? thanks
awesome news. 😅
oh yea about that there are different methods. I think the abort controller should only be created before each request. one way of doing that is
useRef
ok but is there another method, because this one does not work for my use case thanks
This is what I have so far. I have a modal, this modal is saving data to a database, once I click on the abort, i cancelled the call (to save data) in the useEffect. Let 's say now the user exit the modal and decide some other time to go back on the modal to save its data again, the cancel signal is still on the route. How do I clear up the abort controller ?
Here is my service file :
my action file
and my component
in your example it's hard to tell what the issue is or what are you trying to do.
two things that's kinda obvious here:
useEffect
only fires once (when the component mounts) - the request is not made on demand, which seems to be what you are trying to do here. If you want the request to be made every time the modal is opened, you will need to make sure opening the modal (either rendering the modal, or changing the state) is tied to the effect/request.also, when you say "it doesn't work" - please elaborate why or how. there could be many reasons for broken code, without errors/logs/descriptions, it's impossible to tell what might be wrong by simply looking at a few lines of code.