What is the best way to deal with a fetch error in react redux?

前端 未结 6 1964
名媛妹妹
名媛妹妹 2020-12-04 04:41

I have one reducer for Clients, one other for AppToolbar and some others...

Now lets say that I created a fetch action to delete client, and if it fails I have code

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-04 05:28

    The approach I'm currently taking for a few specific errors (user input validation) is to have my sub-reducers throw an exception, catch it in my root reducer, and attach it to the action object. Then I have a redux-saga that inspects action objects for an error and update the state tree with error data in that case.

    So:

    function rootReducer(state, action) {
      try {
        // sub-reducer(s)
        state = someOtherReducer(state,action);
      } catch (e) {
        action.error = e;
      }
      return state;
    }
    
    // and then in the saga, registered to take every action:
    function *errorHandler(action) {
      if (action.error) {
         yield put(errorActionCreator(error));
      }
    }
    

    And then adding the error to the state tree is as Erik describes.

    I use it pretty sparingly, but it keeps me from having to duplicate logic which legitimately belongs in the reducer (so it can protect itself from an invalid state).

提交回复
热议问题