【问题标题】:How to use mobx in react-native 0.56 (Babel 7) with Decorators如何在带有装饰器的 react-native 0.56 (Babel 7) 中使用 mobx
【发布时间】:2018-10-07 19:49:38
【问题描述】:

我已将使用 Babel 7 的 RN 应用从 0.55.4 升级到 0.56。

在 0.55.4 中为 MOBX 使用装饰器时,我使用“babel-plugin-transform-decorators-legacy”,但与 Babel 7 不兼容...

react-native 版本:0.56.0 mobx 版本:5.0.3 mobx-react 版本:5.2.3

有人有解决办法吗?

谢谢

更新:

应用程序在 DEBUG 中使用此配置运行

package.json

...
"devDependencies": {
    "@babel/core": "7.0.0-beta.47",
    "@babel/plugin-proposal-decorators": "7.0.0-beta.47"
    ...
}

.babelrc

{
  "presets": [
    ["react-native"]
  ],
  "plugins": [
    ["@babel/plugin-proposal-decorators", { "legacy": true }]
  ]
}

但在RELEASE xCode 中出现此错误:

babelHelpers.applyDecoratedDescriptor is not a function.

更新 2,工作配置:

这是我的工作配置:

package.json

...
"devDependencies": {
   "@babel/core": "7.0.0-beta.47",
   "@babel/plugin-proposal-decorators": "7.0.0-beta.47",
   "@babel/runtime": "7.0.0-beta.47",
   "babel-jest": "23.2.0",
   "babel-preset-react-native": "5.0.2",
   "jest": "23.3.0",
   "react-test-renderer": "16.4.1"
}
...

.babelrc

{
  "presets": [
    ["react-native"]
  ],
  "plugins": [
    ["@babel/plugin-proposal-decorators", { "legacy": true }]
  ]
}

然后在 index.js(主应用程序启动文件)中我需要导入装饰器 babel 库:

index.js

import applyDecoratedDescriptor from '@babel/runtime/helpers/es6/applyDecoratedDescriptor';
import initializerDefineProperty from '@babel/runtime/helpers/es6/initializerDefineProperty';

Object.assign(babelHelpers, {applyDecoratedDescriptor, initializerDefineProperty});

require('./app.js');

app.js

import {AppRegistry} from 'react-native';
import AppName from './app/index';

AppRegistry.registerComponent(appName, () => AppName);

【问题讨论】:

  • 有这个@babel/plugin-transform-decorators 包,但它也无济于事。更奇怪的是,我的问题仅在我为 Release 配置构建时才存在。 Debug 构建工作正常。
  • @Hkan 我也尝试过@babel/plugin-transform-decorators,但我遇到了同样的错误...也调试和发布...
  • 我想我通过安装与@babel/core 完全相同版本的@babel/plugin-transform-decorators 解决了这个问题。
  • 好的,@babel/plugin-transform-decorators@7.0.0-beta.47 构建在 Debug 中工作,但在 Release 应用程序崩溃时,xcode 上的错误是:babelHelpers.applyDecoratedDescriptor is not a function.。有什么建议吗?
  • 我也是。发布错误。

标签: reactjs react-native babeljs mobx


【解决方案1】:

好的,我通过添加@babel/runtime 解决了所有错误,现在该应用程序也可以在DEBUGRELEASE 中运行。

这里是正确的配置:

package.json

...
"devDependencies": {
  "@babel/core": "7.0.0-beta.47",
  "@babel/plugin-proposal-decorators": "7.0.0-beta.47",
  "@babel/plugin-transform-runtime": "7.0.0-beta.47",
  "@babel/runtime": "7.0.0-beta.47",
  "babel-jest": "23.2.0",
  "babel-preset-react-native": "5.0.2",
  "jest": "23.3.0",
  "react-test-renderer": "16.4.1"
}
...

.babelrc

{
  "presets": [
    "react-native"
  ],
  "plugins": [
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    ["@babel/plugin-transform-runtime", {
      "helpers": true,
      "polyfill": false,
      "regenerator": false
    }]
  ]
}

谢谢@Hkan。

【讨论】:

  • 原来,我也安装了@babel/runtime。它隐含地是@babel/plugin-transform-runtimemoduleName 属性的默认值。请参阅@babel/plugin-transform-runtime documentation 了解更多信息。
  • 像魅力一样工作!!
  • 我现在收到此错误,[tid:com.facebook.react.JavaScript] TypeError: undefined is not an object (evaluating 'e.default')
  • 我删除了 babel 系列并再次添加了 yarn add。 7.0.0-beta.47 好用吗?但不好。它不起作用!
  • 我遇到了与@FahidMohammad 相同的错误,您修复了吗?
【解决方案2】:

我通过安装@babel/plugin-proposal-decorators@7.0.0-beta.47@babel/plugin-transform-runtime@7.0.0-beta.47 解决了这个问题。

版本可能会因您而异。我使用这些版本是因为@babel/core 也在7.0.0-beta.47

当前.babelrc 是:

{
  "presets": [
    "react-native"
  ],
  "plugins": [
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    ["@babel/plugin-transform-runtime", {
      "helpers": true,
      "polyfill": false,
      "regenerator": false
    }]
  ]
}

【讨论】:

  • 谢谢,我收到另一个错误Unable to resolve module @babel/runtime/helpers/interopRequireDefault 你能分享你的 package.json devDependencies 吗?
  • 做了上面提到的步骤,我遇到了和@AlessandroBottamedi 一样的错误
  • 我的@babel/plugin-proposal-decorators": "^7.1.0",
猜你喜欢
  • 2019-03-16
  • 1970-01-01
  • 2018-07-06
  • 2019-02-09
  • 1970-01-01
  • 1970-01-01
  • 2017-01-08
  • 1970-01-01
  • 2018-03-16
相关资源
最近更新 更多