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.

Choosing actions for reducer with regexp

See original GitHub issue

Normally you write something like:

[SOME_ACTION_TYPE]: someActionTypeHandler,

So it picks a specific action type and handles it with specific reducer.

I’d like to be able to “catch” action types with regexp. For example, I want to handle every action type that ends with _DONE with a specific successReducer.

Don’t see a way to do that with the current API. Should we implement something to enable such usecase?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:4
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
mslippercommented, Jan 16, 2018

Why not compose the redux-actions reducer? Something like this:

const internalReducer = handleActions({ 
  SOME_ACTION_NAME: (state, { payload: { amount } }) {
    return { ...state, counter: state.counter + amount };
  }
});

export function reducer(state, action) {
  if (yourRegex.test(action.type)) {
    return internalReducer(state, SOME_ACTION_NAME);
  }

  return internalReducer(state, action)
}

You could easily extract this into a helper method and use it across your app. I’m reticent to add support for regular expression matching since it introduces quite a bit of complexity. What happens when both a regex and string key match? How do we store references to the passed-in regexes, since JS objects can only support string keys? If we’re supporting regexes, why not arbitrary functions?

1reaction
Jeewescommented, Sep 27, 2017

Yes, but I’m looking for a more generic solution…

27.9.2017 23.59 “Victor Alvarez” notifications@github.com kirjoitti:

Have you tried combineReducers? Less permissive than a refex, but more intentional.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/reduxactions/redux-actions/issues/246#issuecomment-332653528, or mute the thread https://github.com/notifications/unsubscribe-auth/ABilapHmBqxkCLhojvJQI6lyIPfreZTIks5smrc-gaJpZM4PmYuB .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redux Fundamentals, Part 3: State, Actions, and Reducers
Reducers are functions that take the current state and an action as arguments, and return a new state result. In other words, (state,...
Read more >
Redux can reducer accept multiple actions - Stack Overflow
There are always a couple of different ways that you can create you actions, action reducers etc. However the most important thing to ......
Read more >
RegExp: Match, Search, Replace and Split Like a Master
In this post, I want to show you the beauty of regular expressions. Develop a Zen mind. RegEx is one of those 'things'...
Read more >
Building out Redux store / actions / reducers -- strategies?
Do you brainstorm a list of all possible actions, or take your database tree ... value: '' }], // array of react-select objects...
Read more >
Where do I put my business logic in a React Redux application?
Choosing where to put your business logic is an important decision. ... fat action creators; reducers; thunks; sagas - redux-saga; epics - redux-observable ......
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