javascript - 将 MobX 存储保存和加载到 React Native AsyncStorage 的最佳模式?

标签 javascript react-native mobx

我有一个使用 MobX 的 React Native 应用程序。我的商店有一个默认对象,当用户使用操作更新值时(在 Options.js 文件中),我想将其保存在本地(React Native 的 AsyncStorage),以便下次打开应用程序时对象未重置为默认值。在第一次运行时处理这个问题、检查和加载新值的好模式是什么?

我的 RootStore.js 看起来像这样:

import { observable, action } from 'mobx';

export default class RootStore {
  constructor() {
    this.sources = new Sources(this);
  }
}

class Sources {
  constructor(rootStore) {
    this.rootStore = rootStore;
  }
  @observable
  left = [
    {
      active: 'yes',
      side: 'value One',
      name: 'value Two',
    },
  ];
//passing in new object from Options.js
  @action
  updateActiveLeft(data) {
    let object = data;
    this.left = object;
    console.log('New active name set in store - ' + this.left[0].name);
  }

最佳答案

我已经更新了您的文件以包含我在所有 React Native 应用程序中使用的基本模式。我正在使用 async/await 语法来处理异步调用,但您可以使用 promises 或您喜欢的任何模式。您可能还想让它成为一个 DRY 类来处理您所有的本地 API。请注意,如果您在商店中存储非 mobx 方法,这将不起作用,因为 mobx.toJS 不会删除它们并且 JSON.stringify 无法序列化函数。

编辑:修复了编译错误并添加了 updateStore() 方法。

import { observable, action, toJS } from 'mobx';
import { AsyncStorage } from 'react-native';
import _ from 'lodash';

export default class RootStore {
  constructor() {
    this.sources = new Sources(this);
    // You can move this to your App.js class when your app
    // for more control but this will start loading your data
    // as soon as RootStore is created.
    this.sources.refreshStores();
  }
}

class Sources {

  @observable
  left = [
    {
      active: 'yes',
      side: 'value One',
      name: 'value Two',
    },
  ];

  //passing in new object from Options.js
  @action
  updateActiveLeft(data) { // May want to make this a promise or async
    let object = data;
    // this.left = object; // Now using updateStore to set MobX obs
    this.updateStore(toJS(object)).then(() => {
      console.log('Data is now saved in AsyncStorage.')
    });
  }

  /**
   * Set async data to local memory
   * @param newdata {object} An updated store abject
   */
  @action
  updateStore = async (newdata) => {
    try {
      const AppData = newdata;
      // Set MobX observables with new data
      _.forEach(AppData, (value, key) => {
        this[key] = value;
      });
      // Set AsyncStorage
      await AsyncStorage.setItem('app', JSON.stringify(AppData));
    } catch(e) {
      console.log('Could not update app store. [store/App]')
    }
  }

  /**
   * Set MobX data from AsyncStorage
   */
  @action
  refreshStores = async () => {
    try {
      const RootStore = await this.getStore();
      // I store my data in AsyncStorage exactly the same way I store my observables
      // so that when I recall them I can just iterate through my object or array
      // and set them easily to MobX observables.
      _.forEach(RootStore, (value, key) => {
        this[key] = value;
      });
    } catch(e) {
      console.log('Could not refresh app store.');
    }
  }

  /**
   * Retrieve data from AsyncStorage
   */
  @action
  getStore = async () => {
    try {
      // I'm just using RootStore as a storage key name but you can use whatever you want.
      // I'm also shortcircuiting the call to async to be replaced with the default values
      // in case the store doesn't exist.
      let RootStore = await AsyncStorage.getItem('RootStore') || JSON.stringify(toJS(this));
      RootStore = JSON.parse(RootStore);
      return RootStore;
    } catch(e) {
      console.log('Could not get data from store.')
    }
  }

}

关于javascript - 将 MobX 存储保存和加载到 React Native AsyncStorage 的最佳模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47873943/

相关文章:

Javascript 问题,需要根据 2 <select> 中的选择对表内的值进行着色

javascript - 我的 jquery 加载了两次吗?

javascript - state.findIndex is not a function 错误与 findIndex

reactjs - 观察者无法与 useLocalObservable 一起使用

javascript - 计算 sha256 忽略元数据 - Javascript

javascript - 我们如何信任 npm 模块?

javascript - 如何在没有 babel 的情况下动态创建 "compiled"React 组件?

javascript - 如何使用 redux React Native 添加拉动刷新

javascript - React 和 MobX - 用户离开现有页面时的确认对话框

javascript - Jest - 函数不调用其子异步函数