Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mobx in react-native 0.56 (Babel 7) with Decorators

i've upgraded my RN app from 0.55.4 to 0.56 that use Babel 7.

In 0.55.4 to use decorators for MOBX i use "babel-plugin-transform-decorators-legacy" but is not compatible with Babel 7...

react-native ver: 0.56.0 mobx ver: 5.0.3 mobx-react ver: 5.2.3

does anyone have the solution?

Thanks

UPDATE:

The app works in DEBUG with this configuration

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 }]
  ]
}

But in RELEASE xCode crash with this error:

babelHelpers.applyDecoratedDescriptor is not a function.

UPDATE 2, WORKING CONFIG:

This is my working configuration:

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 }]
  ]
}

Then in the index.js (main app starting file) i need to import the decorators babel libraries:

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);
like image 904
Alessandro Bottamedi Avatar asked Jul 06 '18 14:07

Alessandro Bottamedi


2 Answers

Ok, i solved all the errors by adding @babel/runtime, now the app works in DEBUG and RELEASE too.

Here the correct configuration:

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
    }]
  ]
}

Thanks @Hkan.

like image 92
Alessandro Bottamedi Avatar answered Sep 28 '22 02:09

Alessandro Bottamedi


I solved this issue by installing @babel/[email protected] and @babel/[email protected].

Versions might change for you. I used those versions because @babel/core was also at 7.0.0-beta.47.

Current .babelrc is:

{
  "presets": [
    "react-native"
  ],
  "plugins": [
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    ["@babel/plugin-transform-runtime", {
      "helpers": true,
      "polyfill": false,
      "regenerator": false
    }]
  ]
}
like image 40
Hkan Avatar answered Sep 28 '22 01:09

Hkan