Redux combineReducer
Tags: redux, react, javascript, middleware
Published: 2020-09-23 06:25:47

I am trying to get the concept of redux and understand how its bits and pieces are functioning. I would like to share my experience of learning combine Reducers precisely here.
As per the docs of Redux there are three rules for combine Reducers.
Link here <https://redux.js.org/api/combinereducers>
1. For any action that is not recognized it must return state given to it as first argument. To elaborate this point, first we create a todos file under *reducer* folder.
The content should be as below, this will switch action based upon action.type selected, and be sure to return default state otherwise eror <b>Error: Reducer "todos"/"counter" returned undefined during initialization</b> will show:
```
export default function todos(state = [], action) {
  switch (action.type) {
    case 'ADD_TODO':
      return state.concat([action.text])
    default:
      return state
  }
}
```
we next create another file called counter.js, content is as below, here we are also just incrementing and decrementing counter based upon the action.type. Here initia state is zero.:
```
export default function counter(state = 0, action) {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1
    case 'DECREMENT':
      return state - 1
    default:
      return state
  }
}
```
Now we will create another file combineReducer.js inside reducer folder whose content is as below, this one will first import combineReducers from redux library. combineReducers will take an object whose value is `{  todos: todos,
  counter: counter
}`, as in ES6 syntax we can simply represent that as below :

```
import { combineReducers } from 'redux'
import todos from './todos'
import counter from './counter'

export default combineReducers({
  todos,
  counter
})
```

Now the most interesting part index.js or App.js where we will create store and will do dispatching action and console.logging. The content of which are as below:

```
import { createStore } from 'redux'
import combineReducers from './reducers/index'

const store = createStore(combineReducers)
console.log(store.getState());

//1.First argument is only returned
store.dispatch({
  type: 'ADD_TODO',
  text: 'This is the 1st argument from todos'
}, { type: 'DECREMENT'})

console.log(store.getState());

store.dispatch({
  type: 'INCREMENT'
})

console.log(store.getState());

store.dispatch({
  type: 'DECREMENT'
})

console.log(store.getState());

store.dispatch({
  type: 'DECREMENT'
})

console.log(store.getState());
```
In first line of code we imported createStore from redux library.
For code under commented section-1, we give two parameters to store.dispatch, but it returns only as below:
![first argument](https://user-images.githubusercontent.com/46415048/93972309-af78fa00-fd7a-11ea-92bf-a59d41ed292f.PNG)
Hence the rule, it must return the state given to it as first argument (only) is verified.

Second rule states that it must never return undefined, to verify this we supplied empty object which returns undefined. Hence we can say that action type has to be there. If object with type empty string is supplied, the output will be undefined as below
![undefined](https://user-images.githubusercontent.com/46415048/93973165-6a55c780-fd7c-11ea-8764-238ae2df7262.PNG)

Now the third rule, if the state given to it is undefined or simply empty string or null the output will be carried from the previous state as below:

![sameState](https://user-images.githubusercontent.com/46415048/93973608-2616f700-fd7d-11ea-9469-a1788349c67c.PNG)

![Null](https://user-images.githubusercontent.com/46415048/93974449-7e022d80-fd7e-11ea-996e-8b367bb59489.PNG)

Thank you for your time.
Happy Learning :)