I have a middleware that specificly reacts on two types of status codes of requests. Also I have a bunch of thunks that looks like this:
const updateUser = createAsyncThunk(
'users/update',
async (userData, { rejectWithValue }) => {
const { id, ...fields } = userData
try {
const response = await userAPI.updateById(id, fields)
return response.data.user
} catch (error) {
return rejectWithValue({ ...error.response.data, status: error.response.status })
}
},
)
I use rejectWithValue
to send status code into middleware. But I littlebit confused and don’t know, is it a good practive to use such construction with try catch
and rejectWithValue
in every request or there is some better solution?