React Native 未启用可选链接

2024-04-14

在 React Native 中运行 Android 项目时出现此错误。

这是反应本机版本“react”的全新安装:“^16.3.1”,“react-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.

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

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

更改 .babelrc 文件后重新启动 Metro

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

React Native 未启用可选链接 的相关文章

随机推荐