Create Reducer for Redux Applications

In redux you will have to generate reducers to update the state. You will have to call relevant reducer based on the action type.

For example,

function setSurveySuccess(state, action) {
  return newState;

function setSurveyFail(state, action) {
  return newState;

function currentSurveyReducer(state, action) {

  switch(action.type) {

    case 'CREATE_SURVEY':
      return setSurveySuccess(state, action);

      return setSurveyFail(state, action);

      return state;

In a large application, you end up repeating the above switch case statements all over the place. There is a useful utility function createReducer in redux-create-reducer which helps us avoid writing such a repetitive code.

import { createReducer } from 'redux-create-reducer';

function currentSurveyReducer = createReducer(state, {
    'CREATE_SURVEY': setSurveySuccess,
    'CREATE_SURVEY_FAIL': setSurveyFail

The implementation this simple utility function is shown below.

You may also like...

Leave a Reply

Your email address will not be published.