- MobX Quick Start Guide
- Pavan Podila Michel Weststrate
- 374字
- 2021-08-05 10:34:24
Actions
Although you can change an observable directly, it is highly recommended that you use actions to do it. If you remember, in the previous chapter, we saw that actions are the ones that cause a state-change. The UI simply fires the actions and expects some observables to be mutated. Actions hide the details of how the mutation should happen or what observables should be affected.
The diagram below is a reminder that UI can modify the State only via an Action:

Actions introduce vocabulary into the UI and give declarative names to the operations that mutate the state. MobX embraces this idea completely and makes actions a first-class concept. To create an action, we simply wrap the mutating function inside the action() API. This gives us back a function that can be invoked just like the original passed-in function. Take a look at this code block:
import { observable, action } from 'mobx';
const cart = observable({
items: [],
modified: new Date(),
});
// Create the actions
const addItem = action((name, quantity) => {
const item = cart.items.find(x => x.name === name);
if (item) {
item.quantity += 1;
} else {
cart.items.push({ name, quantity });
}
cart.modified = new Date();
});
const removeItem = action(name => {
const item = cart.items.find(x => x.name === name);
if (item) {
item.quantity -= 1;
if (item.quantity <= 0) {
cart.items.remove(item);
}
cart.modified = new Date();
}
});
// Invoke actions
addItem('balloons', 2);
addItem('paint', 2);
removeItem('paint');
In the preceding snippet, we have introduced two actions: addItem() and removeItem(), which add and remove an item to and from the cart observable. Since action() returns a function that forwards arguments to the passed-in function, we can invoke addItem() and removeItem() with the required arguments.
Besides improving the readability of the code, actions also boost performance of MobX. By default, when you modify an observable, MobX will immediately fire a notification for the change. If you are modifying a bunch of observables together, you would rather fire the change notifications after all of them are modified. This would reduce the noise of too many notifications and also treat the set of changes as one atomic transaction. These are, in essence, the core responsibilities of an action().
- 面向物聯網的CC2530與傳感器應用開發
- TCP/IP入門經典(第5版)
- 局域網組建、管理與維護項目教程(Windows Server 2003)
- 互聯網安全的40個智慧洞見:2014年中國互聯網安全大會文集
- Building RESTful Web Services with Spring 5(Second Edition)
- 計算機網絡原理與應用技術
- Kong網關:入門、實戰與進階
- The Kubernetes Workshop
- 人人都該都懂的互聯網思維
- 計算機網絡技術
- 新媒體交互藝術
- 網絡信息安全工程技術與應用分析
- Architecting Data:Intensive Applications
- 物聯網導論
- 網絡分析技術揭秘:原理、實踐與WinPcap深入解析