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.

Namespace for reducers

See original GitHub issue

So, you have mentioned that there is available possibility to add namespace for action creators. Like below (for example):

const actionCreators = createActions({
  USER: {
    CREATE_ITEM: data => ({ data }) ,
    DELETE_ITEM: ( ... ) => ...,
    GET_LIST: ( ... ) => ...,
    ...
  }
});

But how I can create namespace for reducers, like for such trivial operations as CRUD. Example:

// Creating CRUD reducer template
const crudReducerTemplate = {
   CREATE_ITEM: (state, action ) => ...,
   DELETE_ITEM: ( ... ) => ...,
   GET_LIST: ( ... ) => ...,
   ...
};

// Creating reducer for users' CRUD operations
const userReducers = handleAction({
   USER: {
      ...crudReducerTemplate
   }
}, /* Default state here */);

// Creating reducer for articles' CRUD operations
const articleReducers = handleAction({
   ARTICLE: {
      ...crudReducerTemplate
   }
}, /* Default state here */);

Is it possible to do with your utile? Thanks.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
tsarapkecommented, Mar 17, 2017

@akomm Thanks again. I’ll try it. Also I’ve found multireducer library. Seems it’s like that I’m want and looking for.

1reaction
akommcommented, Mar 17, 2017

There is a possibility to group reducers as you want using combineReducers from “redux” npm package. The problem is the action identity when you reuse reducers. You also have to duplicate-namespace actions.

Even though often you have “1:1 relation” between action/reducer, by design reducers can act on any action. You could make special factories which could generate what you want. But any approach I image feels to overcomplicated as there are other, easier approaches to solve this problem.

You could turn the whole structure. Instead of namespacing reducers, namespacing actions, adding new keys for generic stuff, I would create a single action set and reducer for generic states and namespace them under the reducer key itself. Check redux-form or apollo-client as an example. redux-form manages all different form states under a single “form” key and namespaces the form states inside via a key (formId) => state.

In your case you would have state { entities: { article: …, user: … }

Even though what you would could be done at first glance, I’m not sure whether it that makes any sense.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Namespacing Actions for Redux
I'll cover why we wanted to namespace our Redux actions, ... With Redux, you can use combineReducers to create nested reducers that only ......
Read more >
Namespacing · Redux Subspace - GitHub Pages
To prevent this type of action cross-talk, we can namespace the subspaces and the reducers to filter out actions destined the other sub-application:...
Read more >
Namespaces for Redux actions, reducers and state.
Build re-usable components in React, or any other UI library, using Redux adds complexity. Namespacing actions, reducers and state allows multiple instances of ......
Read more >
The Secret to Using Redux at Scale | by Kevin Ghadyani
Pretty simple. It let's you tie a namespace to a segment of state. And it really works as if it's a reducer itself....
Read more >
using reducer namespace in redux to practice DRY
using reducer namespace in redux to practice DRY ; switch (action.type) ; loading: true ; loading: false ; case "RESET" ; case "DONE_RESET" ......
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