Skip to content

fallthrough actions #163

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

Closed
smeijer opened this issue Nov 18, 2016 · 7 comments
Closed

fallthrough actions #163

smeijer opened this issue Nov 18, 2016 · 7 comments

Comments

@smeijer
Copy link

smeijer commented Nov 18, 2016

In normal reducers we can use fallthrough to use the same reducing method for multiple action types. What's the easy way to do this in redux-actions?

// normal reducer
switch (action.type) {
    case FETCH_ERROR:
    case CREATE_ERROR:
    case EDIT_ERROR:
    case DELETE_ERROR:
      return state
        .set('phase', ERROR)
        .set('error', String(action.payload.error));

  default:
    return state;
}
// redux-actions
handleActions({
  [FETCH_ERROR]: (state, { payload }) => state
    .set('phase', ERROR)
    .set('error', String(payload.error));

  [CREATE_ERROR]: (state, { payload }) => state
    .set('phase', ERROR)
    .set('error', String(payload.error));

  [EDIT_ERROR]: (state, { payload }) => state
    .set('phase', ERROR)
    .set('error', String(payload.error));

  [DELETE_ERROR]: (state, { payload }) => state
    .set('phase', ERROR)
    .set('error', String(payload.error));
}, {});

Is this even possible?

I realize I can create a small helper function, and call this function 4 times, but still it doesn't look very nice:

// redux-actions
const setError = (state, { payload }) => state
    .set('phase', ERROR)
    .set('error', String(payload.error));

handleActions({
  [FETCH_ERROR]: setError,
  [CREATE_ERROR]: setError,
  [EDIT_ERROR]: setError,
  [DELETE_ERROR]: setError,
}, {});

Anyone with suggestions to handle this in a clean way?

@xiaohanzhang
Copy link
Contributor

have you checked combineActions ?

@yangmillstheory
Copy link
Contributor

@smeijer
Copy link
Author

smeijer commented Nov 18, 2016

Yes, I did see combineActions. But even after rereading this section several times, it's not directly obvious how to handle this case.

@yangmillstheory
Copy link
Contributor

yangmillstheory commented Nov 18, 2016

This is how we usually use it in code:

handleActions({
  [
    combineActions(
      FETCH_ERROR,
      CREATE_ERROR,
      EDIT_ERROR,
      DELETE_ERROR
    )
  ]: (state, { payload }) => state
      .set('phase', ERROR)
      .set('error', String(payload.error));
}, {});

@smeijer
Copy link
Author

smeijer commented Nov 19, 2016

That one is briljant. Can't believe it's not that obviously noted in the readme.

@yangmillstheory
Copy link
Contributor

@smeijer Would you mind submitting a PR to improve the README?

@yangmillstheory
Copy link
Contributor

Actually, this is taken care of in https://github.com/acdlite/redux-actions/pull/165/files.

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

3 participants