0.0.5 • Published 6 years ago

combine-reducers-enhanced v0.0.5

Weekly downloads
86
License
MIT
Repository
github
Last release
6 years ago

combined-reducer-enhanced Build Status

Extension on the combineReducer method as available in Redux libraries

Install

$ npm install --save combine-reducers-enhanced

How to use

import {combineReducersEnhanced} from "combine-reducers-enhanced";

const rootReducer = {
  ui: {
      login: loginReducer,
      main: mainReducer
  } ,
  data: dataReducer
}

const enhancedRootReducer = combineReducersEnhanced(rootReducer);

let store: Store = new Store(enhancedRootReducer);

Reason for creation

Every redux library provides us with a method called combineReducers (if you don't know this check the documentation. This method is really helpful but has its limitations. This library was created to fix one of this limitations.

Current situation with combineReducers

During design of your state tree, you typically divide it up into different sections. F.e.

{
  ui: uiReducer,
  data: dataReducer
}

@ngrx/store provides the combineReducers method to easily work with such structures.

Problem description

If you want to work with multiple levels of nesting in your state tree, you need to do something else F.e.

{
  ui: {
      login:...,
      main: ...
  } ,
  data: ...
}

In that case, you'd could: 1. Write a uiReducer yourself which delegates every action related to login to a loginReducer and every 'main' related action to the mainReducer. 2. Use a utility such as: https://github.com/brechtbilliet/create-reducer-tree which handles this for you 3. Nest the combineReducers method like this:

const rootReducer =
{
   ui: combineReducers({
         login: loginReducer,
         main: mainReducer
  }),
  data: dataReducer
}

Improvement description

Option 1 provides you with extra work and option 2 forces you to work with a third party library. I personally prefer option 3 where you nest the combineReducers method inside your tree. This could actually be easily integrated into the current combineReducers method and make the following possible:

rootReducer = {
   ui: {
      login: loginReducer,
      main: mainReducer
   },
   data: dataReducer
}

new Store(rootReducer)

It's a lot cleaner than approach where you nest the combineReducers method yourself. This implemented by making the combineReducers function recursive.

License

MIT © KwintenP

0.0.5

6 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago