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

  • React Native Blueprints
  • Emilio Rodriguez Martinez
  • 178字
  • 2021-07-02 15:20:02

Setting up the store

After understanding how MobX works, we are ready to create our store:

/*** src/store.js ** */

import { observable } from 'mobx';
import { AsyncStorage } from 'react-native';

class Store {
@observable feeds;
@observable selectedFeed;
@observable selectedEntry;

constructor() {
AsyncStorage.getItem('@feeds').then(sFeeds => {
this.feeds = JSON.parse(sFeeds) || [];
});
}

_persistFeeds() {
AsyncStorage.setItem('@feeds', JSON.stringify(this.feeds));
}

addFeed(url, feed) {
this.feeds.push({
url,
entry: feed.entry,
title: feed.title,
updated: feed.updated,
});
this._persistFeeds();
}

removeFeed(url) {
this.feeds = this.feeds.filter(f => f.url !== url);
this._persistFeeds();
}

selectFeed(feed) {
this.selectedFeed = feed;
}

selectEntry(entry) {
this.selectedEntry = entry;
}
}

const store = new Store();
export default store;

We have already seen the basic structure of this file in the MobX section of this chapter. Now, we will add some methods to modify the list of feeds and to select a specific feed/entry when the user taps on them in our app's listings for feeds/entries.

We are also making use of AsyncStorage to persist the list of feeds every time it is modified by either addFeed or removeFeed

主站蜘蛛池模板: 遵化市| 新化县| 眉山市| 葫芦岛市| 辛集市| 秀山| 德格县| 榆树市| 怀集县| 沙雅县| 太康县| 海晏县| 闻喜县| 汤原县| 措美县| 延吉市| 章丘市| 阿坝县| 乡宁县| 鹤岗市| 光山县| 金沙县| 山东| 宝山区| 潞城市| 方山县| 呼图壁县| 乐清市| 江西省| 扬中市| 高安市| 兰州市| 金平| 本溪市| 昌图县| 永胜县| 遂川县| 密云县| 汉沽区| 湘潭县| 启东市|