- 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.
推薦閱讀
- Linux網絡管理與配置(第2版)
- 阿里云數字新基建系列:云原生操作系統Kubernetes
- SOA實踐者說
- WindowsServer2012Hyper-V虛擬化部署與管理指南
- Social Data Visualization with HTML5 and JavaScript
- Distributed Computing with Go
- Cassandra 3.x High Availability(Second Edition)
- Heroku Cloud Application Development
- VMware vSphere 5.1 Cookbook
- Learning IBM Watson Analytics
- Mastering Azure Serverless Computing
- 大規模Linux集群架構最佳實踐:如何管理上千臺服務器
- SAP后勤模塊實施攻略:SAP在生產、采購、銷售、物流中的應用
- Getting Started with Citrix XenApp 6.5
- 微軟360度