- Mastering JavaScript Functional Programming
- Federico Kereki
- 394字
- 2021-07-02 22:41:13
A React+Redux reducer
We can see another example that involves assigning functions. As we mentioned earlier in this chapter, React+Redux works by dispatching actions that are processed by a reducer. Usually, the reducer includes code with a switch:
function doAction(state = initialState, action) {
let newState = {};
switch (action.type) {
case "CREATE":
// update state, generating newState,
// depending on the action data
// to create a new item
return newState;
case "DELETE":
// update state, generating newState,
// after deleting an item
return newState;
case "UPDATE":
// update an item,
// and generate an updated state
return newState;
default:
return state;
}
}
Providing initialState as a default value for state is a simple way of initializing the global state the first time around. Pay no attention to that default; it's not relevant for our example, and I included it just for completeness.
By taking advantage of the possibility of storing functions, we can build a dispatch table and simplify the preceding code. First, we would initialize an object with the code for the functions for each action type. Basically, we are just taking the preceding code, and creating separate functions:
const dispatchTable = {
CREATE: (state, action) => {
// update state, generating newState,
// depending on the action data
// to create a new item
return newState;
},
DELETE: (state, action) => {
// update state, generating newState,
// after deleting an item
return newState;
},
UPDATE: (state, action) => {
// update an item,
// and generate an updated state
return newState;
}
};
We have stored the different functions that process each type of action, as attributes in an object that will work as a dispatcher table. This object is created only once and is constant during the execution of the application. With it, we can now rewrite the action processing code in a single line of code:
function doAction2(state = initialState, action) {
return dispatchTable[action.type]
? dispatchTable[action.type](state, action)
: state;
}
Let's analyze it: given the action, if action.type matches an attribute in the dispatching object, we execute the corresponding function, taken from the object where it was stored. If there isn't a match, we just return the current state, as Redux requires. This kind of code wouldn't be possible if we couldn't handle functions (storing and recalling them) as first-class objects.
- 自然語(yǔ)言處理實(shí)戰(zhàn):預(yù)訓(xùn)練模型應(yīng)用及其產(chǎn)品化
- Vue.js快速入門與深入實(shí)戰(zhàn)
- 羅克韋爾ControlLogix系統(tǒng)應(yīng)用技術(shù)
- 假如C語(yǔ)言是我發(fā)明的:講給孩子聽的大師編程課
- Python貝葉斯分析(第2版)
- R語(yǔ)言與網(wǎng)絡(luò)輿情處理
- AIRIOT物聯(lián)網(wǎng)平臺(tái)開發(fā)框架應(yīng)用與實(shí)戰(zhàn)
- HTML+CSS+JavaScript網(wǎng)頁(yè)設(shè)計(jì)從入門到精通 (清華社"視頻大講堂"大系·網(wǎng)絡(luò)開發(fā)視頻大講堂)
- JQuery風(fēng)暴:完美用戶體驗(yàn)
- Learning Grunt
- Go語(yǔ)言入門經(jīng)典
- Application Development with Swift
- Java從入門到精通(視頻實(shí)戰(zhàn)版)
- Developing RESTful Web Services with Jersey 2.0
- 程序員超強(qiáng)大腦