agraboso / redux-api-middleware

Redux middleware for calling an API.
MIT License
1.49k stars 195 forks source link

Does this support dispatching more than 1 action on SUCCESS? #185

Open christopher-francisco opened 6 years ago

christopher-francisco commented 6 years ago

On a "delete resource" context, Success need to call for loading once again. In my case, it also has to close the confirm dialog. Can I dispatch multiple actions on success?

tkajtoch commented 6 years ago

You shouldn't use redux-api-middleware's action types to dispatch such actions. However, you can catch the success action somewhere (you can choose from writing a middleware handling this, using redux-saga, and so on). redux-api-middleware allows you to pass a function to payload as well, which will be called on successful server response - you can experiment with placing a thunk into there but unfortunately, I didn't test it by myself.

nason commented 6 years ago

Would this approach work for you? (note: depends on redux-thunk)

dispatch(myRequestActionCreator()).then(action => {
  if (action.error) { 
    // handle error
  } else {
    return dispatch(followupActionCreator()) 
  }
})

See more at https://github.com/agraboso/redux-api-middleware#dispatching-thunks

Let us know if this helps, or if there's something you'd like better explained/documented!