有多个mobx实例活动

时间:2018-03-22 13:17:03

标签: reactjs react-native mobx mobx-react

自上次将Mobx 3.6更新到Mobx 4以来,我的应用程序才停止工作。我使用react-native而我只是按照说明迁移到最新功能,但我的应用程序只是不断崩溃显示以下错误:

  

[mobx]有多个mobx实例处于活动状态。这可能会导致意外结果:有关详细信息,请参阅https://github.com/mobxjs/mobx/issues/1082

Click here to check the error image

Mobx商店:

我刚创建了一个简单的可观察对象,其代码如下:

import React, { Component } from "react";

import { observable } from "mobx";

const ProductsStore = observable.object(
  {
    selectedProduct: null,
    products: [
      {
        id: 1,
        name: "NVIDIA 1050TI",
        desc: "4GB OC",
        model: "ASUS",
        price: 1050,
        quantity: 1
      },
      {
        id: 2,
        name: "NVIDIA 1060TI",
        desc: "6GB OC",
        model: "EVGA",
        price: 1050,
        quantity: 1
      },
      {
        id: 3,
        name: "NVIDIA 1070TI",
        desc: "8GB OC",
        model: "MSI",
        price: 1050,
        quantity: 1
      },
      {
        id: 4,
        name: "NVIDIA 1080TI",
        desc: "11GB OC",
        model: "FOUNDERS EDITION",
        price: 1050,
        quantity: 1
      }
    ]
  },
  { selectedProduct: observable, products: observable }
);

export { ProductsStore };

但是,当我尝试导入此文件时,它会使应用程序崩溃,它将显示我之前提到的错误。

import { ProductsStore } from '@store'

我试过没有别名,但它似乎没有用。

1 个答案:

答案 0 :(得分:3)

我在与@mweststrate聊天时解决了这个问题,并发现在我的项目中,我使用的是一个依赖旧版mobx的库。

版本> 3.6,只接受一个mobx实例。

react-native-router-flux正在使用旧版本,该版本负责导致此问题。

解决方法是将resolutions属性添加到package.json文件,并将mobxmobx-react添加到此属性。

  "resolutions": {
    "mobx": "^4.1.0",
    "mobx-react": "^5.0.0"
  },

resolutions属性仅适用于yarn 不适用于 npm。此属性将覆盖旧的引用依赖项。如有其他问题,请阅读文档。

Selective resolutions in yarn

Multiple MobX instances in your application #1082

相关问题