Skip to content

fallthrough actions #163

@smeijer

Description

@smeijer

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions