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.

Proposal: mechanism for handling multiple actions with the same reducer.

See original GitHub issue

Hi all!

I’d like to propose a new feature to allow handling multiple actions with the same reducer in the case that someone has very general, over-arching properties of their state.

For example, when using redux on its own I’ve found it useful to have a loading property which gets reduced like this:

function loading(state = false, action) {
  switch(action.type) {
  case LOAD_SOMETHING_START:
  case LOAD_SOMETHING_ELSE_START:
    return true;

  case LOAD_SOMETHING_COMPLETE:
  case LOAD_SOMETHING_ELSE_COMPLETE:
    return false;

  default:
    return state;
  }
}

I find this preferable to invoking an action to update loading within a composite/async action using redux-thunk because I would prefer not to have my state’s other properties know about the loading property and keep that logic central to the reducer for loading.

Initially I’d like to propose being able to define an action handler with a name that contains a delimiter like |. To illustrate with the previous example:

handleActions({
  'LOAD_SOMETHING_START | LOAD_SOMETHING_ELSE_START': () => true,
  'LOAD_SOMETHING_COMPLETE | LOAD_SOMETHING_ELSE_COMPLETE': () => false
}, false);

Alternatively, if it would be preferable not to expose the details about how to delimit action names, a function combineActions() might help:

const startActions = combineActions('LOAD_SOMETHING_START', 'LOAD_SOMETHING_ELSE_START');
const completeActions = combineActions('LOAD_SOMETHING_COMPLETE', 'LOAD_SOMETHING_ELSE_COMPLETE');

handleActions({
  [startActions]: () => true,
  [completeActions]: () => false
}, false);

What do you think? I’d be thrilled to submit a PR for this if we agree it would be worthwhile.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:9
  • Comments:8

github_iconTop GitHub Comments

2reactions
yangmillstheorycommented, Jul 9, 2016

I’ve been using this feature at work via a fork of this project and it’s turned out great for DRY’ing up our reducers (especially when using reducer composition). I’ll see if I can contribute this feature back to the repository.

As mentioned above though, I don’t see a cleaner path to doing this than the combineActions approach.

0reactions
yangmillstheorycommented, Jul 4, 2016

I was thinking that a conventional delimiter like | could work, but there’s no obvious way for library users to join action creators (which can be used as inputs to handleActions or handleAction) since the way an action creator stores its type (via toString) seems encapsulated from the library user.

So, I think the combineActions approach seems the most reasonable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sharing Redux Actions and Reducers
In Redux, reducers and actions are often seen in a one-to-one mapping. ... Sharing a Single Action Between Multiple Reducers.
Read more >
Where to dispatch multiple actions in redux?
I am aware dispatching actions from within the reducer is an anti pattern. I would like to know what is a suitable place...
Read more >
Bind multiple actions to same extra reducer · Issue #764
Hi, I couldn't find this in the documentation. Is it possible to bind multiple actions to the same extraReducer?
Read more >
Understanding the magic behind StoreModule of NgRx ...
We'll examine how state, reducers, store and actions work with each other and where meta reducers ... You can use the same action...
Read more >
Idiomatic Redux: The Tao of Redux, Part 2 - Practice and ...
One is that it guides you away from the idea of multiple slice reducers independently responding to the same action.
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