redux-promise with Axios, and how do deal with errors?

后端 未结 6 1642
梦如初夏
梦如初夏 2020-12-16 02:07

So, I see on an error, redux-promise hands me back error: true, along with the payload, but that is once it hits the reducer... to me, decoupling the request AND error condi

6条回答
  •  粉色の甜心
    2020-12-16 02:08

    This might not be the best approach but it works for me. I pass the 'this' of my component as var context. Then when i get response back i just execute the methods defined in my components context. In my component i have successHdl and errorHdl. From there i can trigger more redux actions as normal. I checked all the previous answers and seem too daunting for such a trivial task.

    export function updateJob(payload, context){
    
        const request = axios.put(UPDATE_SOMETHING, payload).then(function (response) {
            context.successHdl(response);
        })
        .catch(function (error) {
            context.errorHdl(error);
        });;
    
        return {
            type: UPDATE_SOMETHING,
            payload: payload,
        }
    }
    

提交回复
热议问题