Wiring up abort controller on RTKQ #2048
-
Hi all, I kindly will like to ask a question about how abort control may be wired up with RTKQ. I understand that both base query and endpoint query have a signal API which I could tap into. However, I don't know where I will instantiate an abort controller and how would likely fire the abort command. My scenario is such that, should there be no token supplied to my auth slice via state then the fetch should be cancelled when an attempt to load this component occurs. Since signal is available on both baseQuery and endpoint query I will appreciate if someone could kindly explain the use case scenario for both. A sample of my code is here: export const massAPI = createApi({
reducerPath: 'massAPI',
baseQuery: fetchBaseQuery({
baseUrl: 'http://localhost:3000/data/',
prepareHeaders: (header, {getState}) => {
const token = getState().users.user.token
if(token){
header.set('Authorization', `Bearer ${token}`)
}else{
}
header.set('Content-Type', 'application/json')
return header
},
// signal: <- "Access to signal here"
}),
endpoints: (builder)=>({
getData: builder.query({
query: (date) => ({
url: `detail/${date}`,
method: 'GET',
// signal: <-"Access to signal here"
}),
})
Thanks in advance. Cheers. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Realistically I think it's the wrong tool for what you are doing here tbh. Because at that point the request is pretty much already done. If you want to instantly return an error when there is no token in state yet, then better wrap your baseQuery and just return an early error there. |
Beta Was this translation helpful? Give feedback.
-
@checkDaResume https://redux-toolkit.js.org/api/createAsyncThunk#canceling-while-running |
Beta Was this translation helpful? Give feedback.
signal
is already passed in - and you should not tinker with that. You can call.cancel()
on your submitted query, but not add a new signal overriding the internal one.Realistically I think it's the wrong tool for what you are doing here tbh. Because at that point the request is pretty much already done.
If you want to instantly return an error when there is no token in state yet, then better wrap your baseQuery and just return an early error there.