'How to handle errors to dispatch a POST in redux
Hi I am new to Redux and looking how to handle errors to dispatch a POST using axios with thunk. I am trying to catch any error coming from the extra reducers. I am using a console.log for error temporarily. Any feedback will be great! Here is a snip of my code:
export const postInfo = createAsyncThunk(
'info/postInfo'
async (infoObject: InfoRequest) => {
const response = await axios.post(`${url}/info`, infoObject);
return response.data;
}
);
...
extraReducers: (builder) => {
builder
....
.addCase(postInfo.rejected, (state, action) => {
state.request.status = 'failed';
state.request.error = action.error.message;
})
}
...
const sendInfoRequest = async () => {
try {
const infoObjRequest: InfoRequest = {
userId: 8,
firstName: 'John',
lastName: 'Smith'
};
await dispatch(postInfo(infoObjRequest));
} catch (err) {
// TODO: how to use error handling for rejected?
console.log('rejected for post /info', err);
}
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
