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

前端 未结 6 1956
名媛妹妹
名媛妹妹 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

    Erik’s answer is correct but I would like to add that you don’t have to fire separate actions for adding errors. An alternative approach is to have a reducer that handles any action with an error field. This is a matter of personal choice and convention.

    For example, from Redux real-world example that has error handling:

    // Updates error message to notify about the failed fetches.
    function errorMessage(state = null, action) {
      const { type, error } = action
    
      if (type === ActionTypes.RESET_ERROR_MESSAGE) {
        return null
      } else if (error) {
        return error
      }
    
      return state
    }
    

提交回复
热议问题