官术网_书友最值得收藏!

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.
主站蜘蛛池模板: 闻喜县| 罗城| 金塔县| 玉山县| 全南县| 吉林省| 宜兰县| 江达县| 洞口县| 白玉县| 肥城市| 武功县| 全椒县| 通道| 额济纳旗| 株洲县| 富蕴县| 尚志市| 汝城县| 左权县| 安陆市| 丘北县| 彩票| 方城县| 蚌埠市| 都匀市| 曲松县| 白玉县| 郴州市| 垦利县| 缙云县| 南溪县| 辰溪县| 芦溪县| 吴江市| 黑河市| 靖远县| 米泉市| 枝江市| 白山市| 安义县|