NIsham Mahsin
NIsham Mahsin

Reputation: 738

Callback function for this.props.dispatch

I am calling a dispatch function as below

this.props
      .dispatch({
        type: LOGIN_REQUESTED,
        payload: {
          username,
          password,
        },
})

i want to dispatch another action on successfully execution of this action i tried with then function but getting error that this.props.dispatch(...).then is not a function

Upvotes: 1

Views: 156

Answers (1)

Joseph D.
Joseph D.

Reputation: 12174

Since you're using redux saga, you can have a generator to watch for LOGIN_SUCCESS.

Dispatch this action after the API had successfully responded.

takeLatest(LOGIN_REQUESTED, watchForLogin)
takeLatest(LOGIN_SUCCESSFUL, watchForLoginSuccess)

watchForLogin(action) {
  doLogin('/some/url')
    .then(response => dispatch(loginSuccess(response))) // dispatch login success
    .catch(error => dispatch(loginFailed(error))) // dispatch error
}

watchForLoginSuccess(action) {
  doSomethingAfterLogin()
}

Upvotes: 1

Related Questions