redux combined reducers code example

Example 1: combine reducers redux

// reducers.js
export default theDefaultReducer = (state = 0, action) => state

export const firstNamedReducer = (state = 1, action) => state

export const secondNamedReducer = (state = 2, action) => state

// rootReducer.js
import { combineReducers, createStore } from 'redux'

import theDefaultReducer, {
  firstNamedReducer,
  secondNamedReducer
} from './reducers'

// Use ES6 object literal shorthand syntax to define the object shape
const rootReducer = combineReducers({
  theDefaultReducer,
  firstNamedReducer,
  secondNamedReducer
})

const store = createStore(rootReducer)
console.log(store.getState())
// {theDefaultReducer : 0, firstNamedReducer : 1, secondNamedReducer : 2}

Example 2: multiple reducers redux

/* Reducer Composition */
import { createStore, combineReducers } from 'redux';

const rootReducer = combineReducers({
  first: firstReducer,
  second: secondReducer
});
const store = createStore(rootReducer);

/* About Redux.combineReducers()
 * accepts an object in which keys are associated to specific
 * reducer functions, these keys are used as the name for the
 * associated piece of state
*/