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

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
主站蜘蛛池模板: 龙井市| 东海县| 德化县| 清丰县| 邵阳市| 新巴尔虎左旗| 盖州市| 金昌市| 洛宁县| 平利县| 大同县| 南阳市| 咸阳市| 赤峰市| 和平县| 错那县| 宝坻区| 凌源市| 万安县| 临潭县| 丰台区| 朝阳市| 五河县| 顺昌县| 青神县| 通海县| 松潘县| 静宁县| 玉林市| 永福县| 高雄县| 博乐市| 蓬溪县| 汕尾市| 长丰县| 保德县| 海阳市| 浦东新区| 襄汾县| 资阳市| 绥棱县|