svrcekmichal / redux-axios-middleware

Redux middleware for fetching data with axios HTTP client
MIT License
920 stars 96 forks source link

Dispatch new action when async action is succeeded. #46

Closed themarvelme closed 7 years ago

themarvelme commented 7 years ago

Lets say that i have login action like following.

export function login(credentials) {
    return {
        types: ['LOGIN','LOGIN_SUCCESS','LOGIN_ERROR'],
        payload: {
        request: {
            url: '/api/login',
            method: 'POST',,
            data: credentials
        }
        }
    }
}

When login is succeeded, i want to dispatch new action doAnother. How can i do that?

Do i need to do that in reducer when action.type = LOGIN_SUCCESS? I guess its anti-pattern, and there must be more redux-like approach.

nmaves commented 7 years ago

Easy!

You can see in the docs that actions that are handled by this middleware return a Promise.

this.props.login(credentials)
  .then(() => {
    // router the user away
    this.context.router.push("/my/home/page")
  })
  .catch((response) => {
    //handle login errors
  })