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

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
主站蜘蛛池模板: 德江县| 福贡县| 江都市| 胶南市| 灵石县| 保山市| 化州市| 黄大仙区| 山东省| 错那县| 通州区| 绍兴县| 邛崃市| 白水县| 滦平县| 秭归县| 江口县| 麻栗坡县| 拉萨市| 泌阳县| 河池市| 临湘市| 汕头市| 饶阳县| 高阳县| 廊坊市| 民勤县| 泾阳县| 邵阳县| 上栗县| 定襄县| 嘉峪关市| 武定县| 哈密市| 龙门县| 北票市| 河北区| 玛多县| 铁岭市| 张掖市| 顺昌县|