未启用可选链接 React Native

Posted

技术标签:

【中文标题】未启用可选链接 React Native【英文标题】:Optional Chaining not enabled ReactNative 【发布时间】:2019-03-07 05:35:28 【问题描述】:

在 react react native 中运行 android 项目时出现此错误。

这是全新安装的 react native 版本 "react": "^16.3.1","re​​act-native": "^0.57.1​​",

它给出了可选链接的错误。谁能帮助我如何在本机反应中启用可选链接。

Loading dependency graph, done.
BUNDLE [android, dev] ....../index.js ▓▓▓▓▓▓▓▓▓▓░░░░░░ 64.3% (667/832)::ffff:127.0.0.1 - - [02/Oct/2018:04:30:46 +0000] "GET /index.delta?platform=android&dev=true&minify=false HTTP/1.1" 500 - "-" "okhttp/3.10.0"
error: bundling failed: SyntaxError: C:\ReactNative\mrn\node_modules\react-native\Libraries\Components\Switch\Switch.js: Support for the experimental syntax 'optionalChaining' isn't currently enabled (103:41):

101 | // Support deprecated color props.
102 | let _thumbColor = thumbColor;

103 | let _trackColorForFalse = trackColor?.false;
| ^
104 | let _trackColorForTrue = trackColor?.true;
105 |
106 | // TODO: Remove support for these props after a couple releases.

Add @babel/plugin-proposal-optional-chaining (https://git.io/vb4Sk) to the 'plugins' section of your Babel config to enable transformation.
at _class.raise (C:\ReactNative\mrn\node_modules@babel\parser\lib\index.js:3939:15)
at _class.expectPlugin (C:\ReactNative\mrn\node_modules@babel\parser\lib\index.js:5253:18)
at _class.parseSubscript (C:\ReactNative\mrn\node_modules@babel\parser\lib\index.js:5959:12)
at _class.parseSubscript (C:\ReactNative\mrn\node_modules@babel\parser\lib\index.js:2716:51)
at _class.parseSubscripts (C:\ReactNative\mrn\node_modules@babel\parser\lib\index.js:5944:19)
at _class.parseSubscripts (C:\ReactNative\mrn\node_modules@babel\parser\lib\index.js:2669:52)
at _class.parseExprSubscripts (C:\ReactNative\mrn\node_modules@babel\parser\lib\index.js:5934:17)
at _class.parseMaybeUnary (C:\ReactNative\mrn\node_modules@babel\parser\lib\index.js:5903:21)
at _class.parseExprOps (C:\ReactNative\mrn\node_modules@babel\parser\lib\index.js:5812:21)
at _class.parseMaybeConditional (C:\ReactNative\mrn\node_modules@babel\parser\lib\index.js:5784:21)
BUNDLE [android, dev] ....../index.js ▓▓▓▓▓▓▓▓▓▓▓▓░░░░ 77.5% (752/854), failed.

【问题讨论】:

【参考方案1】:

我通过在根目录中添加文件 .babelrc 文件来修复它


  "plugins": [
    "@babel/plugin-proposal-optional-chaining"
  ],
  "presets": [
    "react-native"
  ]

修改.babelrc文件后重启metro

【讨论】:

【参考方案2】:

错误说要添加@babel/plugin-proposal-optional-chaining

尝试安装this

npm install --save-dev @babel/plugin-proposal-optional-chaining

【讨论】:

已安装但未启用 尝试将 "plugins": ["@babel/plugin-proposal-optional-chaining"] 添加到您的 .babelrc 或运行 babel --plugins @babel/plugin-proposal-optional-chaining script.js【参考方案3】:

在将我的应用程序从旧的 react-native 版本升级到新版本后,我也遇到了这个问题。它需要 devDependencies 中的一些 babel 库。这是我尝试过的 devDependencies

“devDependencies”: "babel-jest": "23.6.0", “笑话”:“23.6.0”, “metro-react-native-babel-preset”:“0.50.0”, “反应测试渲染器”:“16.6.1”,“开玩笑”: “预设”:“反应原生”

【讨论】:

以上是关于未启用可选链接 React Native的主要内容,如果未能解决你的问题,请参考以下文章

《从零开始学Swift》学习笔记(Day 26)——可选链

可选链打包报错

Swift 可选链在闭包中不起作用

Swift之深入解析可选链的功能和使用

可选链

可选链(?.)