Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to dispatch many actions in one action creator #1

Closed
vslinko opened this issue Jun 1, 2015 · 3 comments
Closed

How to dispatch many actions in one action creator #1

vslinko opened this issue Jun 1, 2015 · 3 comments

Comments

@vslinko
Copy link
Contributor

vslinko commented Jun 1, 2015

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?

@gaearon
Copy link
Contributor

gaearon commented Jun 1, 2015

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

@gaearon gaearon closed this as completed in b02ce1f Jun 1, 2015
@gaearon
Copy link
Contributor

gaearon commented Jun 1, 2015

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.

@gaearon
Copy link
Contributor

gaearon commented Jun 2, 2015

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());
  };
}

markerikson pushed a commit that referenced this issue May 25, 2021
webMasterMrBin pushed a commit to webMasterMrBin/redux that referenced this issue Aug 21, 2021
webMasterMrBin pushed a commit to webMasterMrBin/redux that referenced this issue Aug 21, 2021
Update part-5-ui-and-react.md

Former-commit-id: 9dbfd03
webMasterMrBin pushed a commit to webMasterMrBin/redux that referenced this issue Aug 21, 2021
Update part-5-ui-and-react.md

Former-commit-id: 9dbfd03
Former-commit-id: dcb69c6
webMasterMrBin pushed a commit to webMasterMrBin/redux that referenced this issue Aug 21, 2021
Update part-5-ui-and-react.md

Former-commit-id: 9dbfd03
Former-commit-id: dcb69c6
webMasterMrBin pushed a commit to webMasterMrBin/redux that referenced this issue Aug 21, 2021
Update part-5-ui-and-react.md

Former-commit-id: 9dbfd03
Former-commit-id: dcb69c6
webMasterMrBin pushed a commit to webMasterMrBin/redux that referenced this issue Aug 21, 2021
Update part-5-ui-and-react.md

Former-commit-id: 9dbfd03
Former-commit-id: dcb69c6
markerikson pushed a commit that referenced this issue Jan 9, 2022
nevilm-lt pushed a commit to nevilm-lt/redux that referenced this issue Apr 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants