I'm getting into using redux-toolkit and after reading the docs and implementing some code, there's something I don't quite understand, or maybe my approach is wrong, thats why I'm asking here.
Anyways, I'm having some slice with some asyncThunk which makes a call to an API and sets some state from values returned from that call, and if it fails, show an error notification and some logging. I'm not at work at the moment so I'll write some pseudo code:
const loadData = createAsyncThunk('loadData',
async ({ product }, { dispatch, extra: { API }, rejectWithValue }) => {
try {
const response = await API.getProduct(product)
return response
} catch (error) {
dispatch(setErrorNotification(error.message))
dispatch(logError(error))
rejectWithValue(null)
}
}
This code does what I want actually, but my question lies in having to use rejectWithValue(null)for the code to go through the rejected reducer, , which looks really fishy to me and it makes me believe I'm not understanding this quite well. If I don't do this, even if it goes into the catch, it goes through the fulfilled reducer which throws an error since this is not expected and there's no payload.
Any pointers? is there a way for, when catching the API call (in case of failure) going to the rejected reducer without having to explicitly rejectWithValue?
Thanks!