Reducers

JavaScript functions that take actions and states as input and return the new states are reducers. Well, if this is confusing, try to keep in mind that the action only describes what happened, not how the application state transforms.

It is very important to understand the reducer function. Let's consider our hospital management system. Our application's state can look like the following:

{
doctors: [
{
name: "John Doe",
department: "Radiology",
address: "Kathmandu, 4017, Nepal",
telecom: "999-999-999"
},
{
name: "Ola Nordmann",
department: "General Physician",
address: "Kong Oscarsgate 29, 5017, Bergen, Norway",
telecom: "111-111-1111"
}
];
}

When creating a reducer function, it is important that we remember the reducer principle: it must be a pure function. It should just take the action and return a new state, with no side effects, no mutations, and no API calls.

Let's consider another example of a content management system. In a normal CMS, we have posts and categories. So, our state at an instance could look like the following:

{
posts: [
{ user: 'John Doe', category: 'Practitioner', text: 'This is the first post about Practitioner.' },
{ user: 'Ola Nordmann', category: 'Patients', text: 'This is the first post about Patients.' }
],
filter: ‘Patients’
}

There's nothing complicated here, right? Now, let's start to write our reducer function for both use cases: our CMS use case and our hospital management system use case.

We will start by defining an initial state. Let's initiate our initial state by creating an empty object with an array of empty doctors:

const initialState = {
doctors: []
};

In any database, there is a need for creating, updating, reading, and deleting resources. Similarly, in the hospital management system, we need to read a doctor's record, create a new record, update it, or delete it. Hence, we are likely to have multiple action objects defined, as we mentioned in the preceding section.

This introduces a requirement to handle reducer functions for each of the actions. We can create a single reducer function to handle a similar scenario, and make use of the switch case to handle multiple action types:

import {
ADD_NEW_DOCTOR_REQUEST,
} from './actions'

function addDoctor(state = initialState, action) {
switch (action.type) {
case ADD_NEW_DOCTOR_REQUEST:
return Object.assign({}, state, {
doctors: [
...state.doctors,
{
name: action.name,
age: action.age,
department: action.department,
telecom: action.telecom
}
]
});
default:
return state;
}
}

In the preceding snippet, we have defined ADD_NEW_DOCTOR_REQUEST in the actions. We can check the action type for deleting the doctor's record. Go ahead and add a reducer use case for deleting a doctor.

Now, your task is to check the initial state of the CMS system and write reducer functions for CREATE_POST, EDIT_POST, and SET_FILTER. Once you have finished writing the reducer function, it should look something like the following:

import { CREATE_POST, EDIT_POST, SET_FILTER } from './actionTypes'

function postsReducer (state = [], action) {
switch (action.type) {
case CREATE_POST: {
const { type, ...post } = action
return [ ...state, post ]
}

case EDIT_POST: {
const { type, id, ...newPost } = action
return state.map((oldPost, index) =>
action.id === index
? { ...oldPost, ...newPost }
: oldPost
)
}

default:
return state
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset