Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional Chaining not enabled ReactNative

I am getting this error when running the android project in react react native.

This is fresh install of react native version "react": "^16.3.1","react-native": "^0.57.1",

It gives error of optional chaining. Can anyone please help me how to enable optional chaining in react native.

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.
like image 1000
Vinay Sikarwar Avatar asked Oct 02 '18 05:10

Vinay Sikarwar


People also ask

How do I enable optional chaining?

To enable optional chaining, you need to install a package. At the time of writing, optional chaining is not natively supported in Javascript, it is a new feature introduced in ES2020. Until it is fully adopted we can get all the optional goodness by installing a package!

Does react support optional chaining?

Yes, if you are running recent versions of React and TypeScript: TypeScript 3.7 supports optional chaining. Babel 7.8. 0 supports optional chaining.

Is optional chaining supported?

Using “?.” (optional chaining) in JavaScript/TypeScriptIt's been supported since Chrome/Chrome for Android/Edge 80, Firefox 74, Safari 13.1, Safari for iOS 14.


2 Answers

I got it fixed by adding file .babelrc file in root

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

restart metro after change .babelrc file

like image 57
Vinay Sikarwar Avatar answered Sep 23 '22 20:09

Vinay Sikarwar


The error says that to add @babel/plugin-proposal-optional-chaining

Try installing this

npm install --save-dev @babel/plugin-proposal-optional-chaining
like image 42
Jason Hong Avatar answered Sep 24 '22 20:09

Jason Hong