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

Creating observables

The simplest way to create an observable is to use the observable() function. Take a look at the following:

const item = observable({
name: 'Party Balloons',
itemId: '1234',
quantity: 2,
price: 10,
coupon: {
code: 'BIGPARTY',
discountPercent: 50
}
});

item is now an observable object and will start tracking changes to its properties. You can use this object as a regular JavaScript object without any special API to get or set its values. In the preceding snippet, you can also create an observable item using observable.object().

In the following snippet, we can see simple mutations made to the observables, like any regular JavaScript code:

// Set values
item.quantity += 3;
item.name = 'Small Balloons';

// Get values
console.log(`Buying ${item.quantity} of ${item.name}`);

Observable objects only track the properties provided in the initial value given to observable() or observable.object(). This means if you add new properties later, they will not become observable automatically. This is an important characteristic to remember about observable objects. They are like records or classes with a fixed set of attributes. If you do need dynamic tracking of properties, you should consider using observable maps; these will be covered further ahead in the chapter.

Internally, MobX takes care of transparently tracking the property changes and notifying the corresponding observers. We will look into this internal behavior in a later chapter.

The observable() function automatically converts an object, an array, or a map into an observable entity. This automatic conversion is not applied for other types of data—such as JavaScript primitives (number, string, boolean, null, undefined), functions, or for class- instances (objects with prototypes). So, if you call observable(20), it will fail with an error, as shown here:

Error: [mobx] The provided value could not be converted into an observable. If you want just create an observable reference to the object use 'observable.box(value)'

As suggested in the error, we have to use the more specialized observable.box() to convert primitive values into an observable. Observables that wrap primitivesfunctions, or class-instances are called boxed observables. Take a look at this:

const count = observable.box(20);

// Get the count
console.log(`Count is ${count.get()}`);

// Change count
count.set(22);

We have to use the get() and set() methods of a boxed observable instead of directly reading or assigning to it. These methods give us the observability that is inherent to MobX.

Besides objects and singular values, you can also create observables out of arrays and maps. They have a corresponding API, as can be seen in this table:

 

As we mentioned earlier, observable() will automatically convert an object, array, or a map into an observable. It is shorthand for observable.object(), observable.array(), or observable.map(), respectively. For primitives, functions, and class-instances, you should use the observable.box() API. Although, in practice, the use of observable.box() is fairly rare. It is more common to use observable.object()observable.array(), or observable.map().

MobX applies deep observability when creating an observable. This means MobX will automatically observe every property, at every level, in the object-tree, array, or map. It also tracks additions or removals in the cases of arrays and maps. This behavior works well for most scenarios but could be excessive in some cases. There are special decorators that you can apply to control this observability. We will look into this in Chapter 4Crafting the Observable Tree.
主站蜘蛛池模板: 嘉黎县| 海城市| 南投市| 沛县| 乐亭县| 女性| 郑州市| 民乐县| 鄯善县| 汉中市| 静海县| 景泰县| 乌兰浩特市| 朔州市| 樟树市| 大邑县| 平湖市| 朔州市| 汤原县| 醴陵市| 平武县| 合作市| 鹤岗市| 仙桃市| 子长县| 进贤县| 昌宁县| 新巴尔虎左旗| 牟定县| 永平县| 广宁县| 达州市| 南江县| 衡山县| 灵山县| 富源县| 郁南县| 将乐县| 泸溪县| 定安县| 龙口市|