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

后端 未结 6 1657
梦如初夏
梦如初夏 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:27

    The accepted answer doesn't make use of redux-promise. Since the question is actually about handling errors using redux-promise I provide another answer.

    In the reducer you should inspect the existence of the error attribute on the action object:

    // This is the reducer
    export default function(previousState = null, action) {
      if (action.error) {
        action.type = 'HANDLE_XHR_ERROR'; // change the type
      }  
      switch(action.type) {
        ...
    

    And change the type of the action, triggering a state change for an error handling component that you have set up for this.

    You can read a bit more about it here on github.

提交回复
热议问题