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

Observable maps

You can make an observable map with the observable.map() API. In principle, it works the same way as observable.array() and observable.object(), but it is meant for ES6 Maps. The observable map instance shares the same API as a regular ES6 Map. Observable maps are great for tracking dynamic changes to the keys and values. This is in stark contrast to observable objects, which do not track properties that are added after creation.

In the following code example, we are creating a dynamic dictionary of Twitter-handles to names. This is a great fit for an observable map, as we are adding keys after creation. Take a look at this code block: 

import { observable } from 'mobx';

// Create an Observable Map
const twitterUserMap = observable.map();

console.log(twitterUserMap.size); // Prints: 0

// Add keys
twitterUserMap.set('pavanpodila', 'Pavan Podila');
twitterUserMap.set('mweststrate', 'Michel Weststrate');

console.log(twitterUserMap.get('pavanpodila')); // Prints: Pavan Podila
console.log(twitterUserMap.has('mweststrate')); // Prints: Michel Weststrate

twitterUserMap.forEach((value, key) => console.log(`${key}: ${value}`));

// Prints:
// pavanpodila: Pavan Podila
// mweststrate: Michel Weststrate
主站蜘蛛池模板: 遵化市| 衡南县| 峨眉山市| 凉山| 桃源县| 枣庄市| 博兴县| 安图县| 珠海市| 镇原县| 石狮市| 安顺市| 金寨县| 南川市| 融水| 玛多县| 元氏县| 图们市| 清涧县| 疏勒县| 英超| 丹阳市| 怀安县| 综艺| 九寨沟县| 巴青县| 蒲江县| 涪陵区| 崇左市| 从化市| 西吉县| 镇远县| 康马县| 阳山县| 陈巴尔虎旗| 洛阳市| 横峰县| 旬阳县| 长葛市| 土默特右旗| 兴安盟|