有多个 mobx 实例处于活动状态

There are multiple mobx instances actives

自从上次将 Mobx 3.6 更新到 Mobx 4 后,我的应用程序就停止工作了。我正在使用 react-native,我只是按照说明迁移到最新功能,但我的应用程序一直崩溃并显示以下错误:

[mobx] There are multiple mobx instances active. This might lead to unexpected results: See https://github.com/mobxjs/mobx/issues/1082 for details.

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'

我试过不使用别名,但似乎不起作用。

我在与@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