- MobX Quick Start Guide
- Pavan Podila Michel Weststrate
- 184字
- 2021-08-05 10:34:23
Observable arrays
Using observable.array() is very similar to using an observable(). You pass an array as initial value or start with an empty array. In the following code example, we are starting with an empty array:
const items = observable.array(); // Start with empty array
console.log(items.length); // Prints: 0
items.push({
name: 'hats', quantity: 40,
});
// Add one in the front
items.unshift({ name: 'Ribbons', quantity: 2 });
// Add at the back
items.push({ name: 'balloons', quantity: 1 });
console.log(items.length); // Prints: 3
Do note that the observable array is not a real JavaScript array, even though it has the same API as a JS Array. When you are passing this array to other libraries or APIs, you can convert it into a JS Array by calling toJS(), as shown here:
import { observable, toJS } from 'mobx';
const items = observable.array();
/* Add/remove items*/
const plainArray = toJS(items);
console.log(plainArray);
MobX will apply deep observability to observable arrays, which means it will track additions and removals of items from the array and also track property changes happening to each item in the array.
推薦閱讀
- Web安全防護指南:基礎篇
- 物聯(lián)網(wǎng)智慧安監(jiān)技術
- Force.com Development Blueprints
- 物聯(lián)網(wǎng)信息安全
- 互聯(lián)網(wǎng)安全的40個智慧洞見:2014年中國互聯(lián)網(wǎng)安全大會文集
- 網(wǎng)絡安全技術與解決方案(修訂版)
- 大話社交網(wǎng)絡
- 2018網(wǎng)信發(fā)展報告
- 數(shù)字通信同步技術的MATLAB與FPGA實現(xiàn):Altera/Verilog版(第2版)
- The Kubernetes Workshop
- 物聯(lián)網(wǎng)與智能家居
- 5G+區(qū)塊鏈
- Implementing NetScaler VPX?
- 園區(qū)網(wǎng)絡架構與技術
- 華為HCIA-Datacom認證指南