asyc / await + babel + react-native问题

时间:2018-10-23 18:51:14

标签: react-native build async-await babel-jest

我使用create react native应用程序创建的package.json(尽管我将metro-react-native-babel-preset包的版本从0.45.3手动升级到0.48.1):

{
  "dependencies": {
    "react": "16.5.0",
    "react-native": "0.57.0"
  },
  "devDependencies": {
    "babel-jest": "23.6.0",
    "jest": "23.6.0",
    "metro-react-native-babel-preset": "0.48.1",
    "react-test-renderer": "16.5.0"
  },
  "jest": {
    "preset": "react-native"
  }
}

我正在使用yarn v1.10.1node v10.12.0,而我的.babelrc如下:

{
  "presets": ["module:metro-react-native-babel-preset"]
}

我的异步/等待代码示例是:

import { AsyncStorage } from 'react-native'

const MyStorage = {

  async saveItem(key, value) {
    try {
      await AsyncStorage.setItem(key, value)
    } catch(error) {
      console.log(error.message)
    }
  }
}

module.exports = { MyStorage }

我遇到了构建错误:

error: bundling failed: SyntaxError: myStorage.js: Unexpected token, expected "," (13:2)

> 13 |   async saveItem(key, value) {

我该如何解决?这是构建配置问题吗?

1 个答案:

答案 0 :(得分:0)

我保留了原始语法,但将以下开发依赖项添加到package.json:

@babel/plugin-transform-async-to-generator

@babel/runtime