- 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.
推薦閱讀
- Cybersecurity:Attack and Defense Strategies
- Linux操作系統基礎
- 操作系統基礎與實踐:基于openEuler平臺
- SOA實踐者說
- 玩到極致 iPhone 4S完全攻略
- 嵌入式應用程序設計綜合教程(微課版)
- SharePoint 2013 應用開發實戰
- Windows Server 2012網絡操作系統企業應用案例詳解
- Moodle 3.x Teaching Techniques(Third Edition)
- iOS 8開發指南
- INSTANT Galleria Howto
- Learn CUDA Programming
- μC/OS-III內核實現與應用開發實戰指南:基于STM32
- Multi-Cloud for Architects
- Office 365 User Guide