- 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安全防護指南:基礎(chǔ)篇
- C++黑客編程揭秘與防范
- 電子政務(wù)效益的經(jīng)濟分析與評價
- 網(wǎng)絡(luò)創(chuàng)新指數(shù)研究
- 物聯(lián)網(wǎng)信息安全
- 物聯(lián)網(wǎng)概論(第2版)
- 信息通信網(wǎng)絡(luò)建設(shè)安全管理概要2
- 企業(yè)私有云建設(shè)指南
- 夢工廠之材質(zhì)N次方:Maya材質(zhì)手冊
- 深入理解OpenStack Neutron
- 高級網(wǎng)絡(luò)技術(shù)
- 深入理解計算機網(wǎng)絡(luò)
- 圖解物聯(lián)網(wǎng)
- 物聯(lián)網(wǎng)概論
- 智慧的物聯(lián)網(wǎng):感知中國和世界的技術(shù)