- Redux Quick Start Guide
- James Lee Tao Wei Suresh Kumar Mukhiya
- 537字
- 2021-07-02 12:40:30
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
}
}
- OpenDaylight Cookbook
- Getting Started with ResearchKit
- Building a Game with Unity and Blender
- Web交互界面設計與制作(微課版)
- PHP從入門到精通(第4版)(軟件開發視頻大講堂)
- PHP+Ajax+jQuery網站開發項目式教程
- Angular應用程序開發指南
- Natural Language Processing with Python Quick Start Guide
- Delphi開發典型模塊大全(修訂版)
- Microsoft Exchange Server 2016 PowerShell Cookbook(Fourth Edition)
- DevOps 精要:業務視角
- 例說FPGA:可直接用于工程項目的第一手經驗
- 計算機程序的構造和解釋(JavaScript版)
- Visual C#(學習筆記)
- R語言數據分析從入門到實戰