我已经使用MobX和Redux大约6个月了.我发现,对于大多数应用程序,我更喜欢MobX的简单性.然而,我喜欢Redux的单店概念.我听到其他人 comments 说,他们用MobX创建了一个单一的故事,我正在try 确定最佳方式.目前,我创建了多个store ,然后将它们导入一个store .

class UiStore {
  @observable uiData;

  constructor() {
    this.uiData = {}
  }

  @action updateUI = (data) => {
    this.uiData = {
      data: data
    };
  }
}

let uiStore = new UiStore();
export default uiStore;
class Store {
  @observable currentUser;

  constructor() {
    this.auth = authStore;
    this.ui = uiStore;
  }

}

let store = new store();
export default store;

在这里,我基本上创建单独的store ,然后将其合并成一个store ,类似于redux reducer的工作方式.

还有其他/更好的方法吗?我曾考虑过将存储导入不同的文件中,并在类的prototype上放置一些方法和数据.

推荐答案

我使用MobX已经一年多了,我基本上也是这么做的:

1) 我有一个"主"或"父"store ,通常命名为class Store {...}

2) 然后,我有一些较小的store 被保存在"主"store .

3) 我更喜欢在master's store constructor中构建子store .此外,我发现有时我的子存储必须观察来自父存储的一些数据,所以我将this传递给子构造函数:

class UserStore {...}
class TodosStore {...}

class Store {
  constructor() {
    this.user = new UserStore(this);
    this.todos = new TodosStore(this);
  }
}

父对象保留对子对象的引用,每个子对象都有对父对象的引用.

React-native相关问答推荐

如何重复使用expo 音频对象S录音?

如何在Reaction Native中将身体分成三部分

FlatList 总是增长到 100% 高度

Appcenter codepush 返回请求被阻止

FBSDK 和 LinkingManager 的 AppDelegate.m

React-Native 是单线程执行还是多线程执行?

如何在 mac 上卸载 react-native-cli?

使用react native获取设备令牌

react Navigation 在标题中使用图像?

React-Native 应用程序的 Jest 测试 Animated.View

React native base headers for ios not found

如果我有 AngularJS 基础知识,怎么学习Thinking in ReactJS呢?

React Native Expo StackNavigator 重叠通知栏

React Native require(image) 返回数字

React Native:如何将文件拆分为多个文件并导入它们?

如何在 React Native 中将图像放置在其他图像之上?

React Context:TypeError:render is not a function

Mapping children of a parent component

python 3与本机兼容吗?还是我应该按照 react 文档的建议安装 python 2?

将 Picker 绑定到 React Native 中的 Picker.Item 列表