question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fallthrough actions

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
xiaohanzhangcommented, Nov 18, 2016

have you checked combineActions ?

0reactions
yangmillstheorycommented, Nov 19, 2016

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

C++ attribute: fallthrough (since C++17) - cppreference.com
Indicates that the fall through from the previous case label is intentional and should not be diagnosed by a compiler that warns on ......
Read more >
Code Inspection: Fallthrough in 'switch' statement - JetBrains
Show intention actions: Alt+Enter. Reports a switch statement where control can proceed from a branch to the next one. Such "fall-through" ...
Read more >
How to fallthrough to a specific case in switch statement
A fallthrough statement causes program execution to continue from one case in a switch statement to the next case. Program execution continues ...
Read more >
How To Write Switch Statements in Go - DigitalOcean
Switch is commonly used to describe the actions taken by a program ... body of the next case clause listed using the fallthrough...
Read more >
Fall through on switch case - warnings? : r/cpp - Reddit
So, for years, in both c and c++, I've used a technique where a case: does a bunch of stuff, call it X,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found