Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

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); case 'CREATE_SURVEY_FAIL': return setSurveyFail(state, action); default: 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...