Redux: How to dispatch many actions in one action creator

Created on 1 Jun 2015  ·  3Comments  ·  Source: reduxjs/redux

I have some example on fluce:

function authFormSubmit(fluce) {
  const {valid, disabled, data: {username, password}} = fluce.stores.authForm

  if (disabled) return

  if (!valid) {
    fluce.dispatch(AUTH_FORM_ERROR, new Error('Form is invalid'))
    return
  }

  fluce.dispatch(AUTH_FORM_DISABLED, true)
  fluce.dispatch(AUTH_FORM_ERROR, null)

  authorize({username, password})
    .then(
      user => fluce.dispatch(CURRENT_USER, user),
      error => fluce.dispatch(AUTH_FORM_ERROR, error)
    )
    .then(
      () => fluce.dispatch(AUTH_FORM_DISABLED, false)
    );
}

How to implement same action creator in redux? Should I create action creator for every action type?

Most helpful comment

I'd probably let action creator return a function. If it's a function, it's given dispatch and the state.

All 3 comments

I'd probably let action creator return a function. If it's a function, it's given dispatch and the state.

I added support for async ACs as described above. For now, they can't access the state—I'll probably get back to it at some point, but I don't want to rush an API.

Update: you can do this now.

// Could also look into state in the callback form
export function incrementIfOdd() {
  return (dispatch, state) => {
    if (state.counterStore.counter % 2 === 0) {
      return;
    }

    dispatch(increment());
  };
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

rui-ktei picture rui-ktei  ·  3Comments

dmitry-zaets picture dmitry-zaets  ·  3Comments

cloudfroster picture cloudfroster  ·  3Comments

benoneal picture benoneal  ·  3Comments

elado picture elado  ·  3Comments