简体   繁体   English

可选链在 create-react-app 中不起作用

[英]Optional-Chaining does not work in create-react-app

In a create-react-app project, I am using @babel/plugin-proposal-optional-chaining in my `.babelrccreate-react-app项目中,我在 `.babelrc 中使用@babel/plugin-proposal-optional-chaining

However, I have this error: Module parse failed: Unexpected token (22:16) You may need an appropriate loader to handle this file type.但是,我有这个错误: Module parse failed: Unexpected token (22:16) You may need an appropriate loader to handle this file type.

This is all my babel dependencies:这是我所有的 babel 依赖项:

{
  "dependencies": {
    "@babel/core": "^7.9.0",
    "@babel/node": "^7.8.7",
    "@babel/plugin-external-helpers": "^7.8.3",
    "@babel/plugin-proposal-class-properties": "^7.8.3",
    "@babel/plugin-proposal-decorators": "^7.8.3",
    "@babel/plugin-proposal-json-strings": "^7.8.3",
    "@babel/plugin-proposal-object-rest-spread": "^7.9.5",
    "@babel/plugin-proposal-optional-chaining": "^7.9.0",
    "@babel/plugin-syntax-dynamic-import": "^7.8.3",
    "@babel/plugin-syntax-import-meta": "^7.8.3",
    "@babel/plugin-transform-async-to-generator": "^7.8.3",
    "@babel/plugin-transform-modules-commonjs": "^7.9.0",
    "@babel/plugin-transform-react-constant-elements": "^7.9.0",
    "@babel/plugin-transform-react-inline-elements": "^7.9.0",
    "@babel/plugin-transform-runtime": "^7.9.0",
    "@babel/polyfill": "^7.8.7",
    "babel-plugin-add-module-exports": "^1.0.2",
    "babel-plugin-array-includes": "^2.0.3",
    "babel-plugin-inline-react-svg": "^1.1.1"
  }
}
  • node v14.0.0节点 v14.0.0
  • npm 6.14.4 npm 6.14.4
  • react-scripts 2.1.8反应脚本 2.1.8

What else do I need in order to get this to work?为了让它工作,我还需要什么?

You only need to make sure you have installed react-scripts 3.3.0+ version as it already comes with optional chaining built-in:你只需要确保你已经安装了react-scripts 3.3.0+版本,因为它已经内置了可选的链接:

const obj = { a: `Hello` };

// Hello World
const App = () => {
  return (
    <>
      {obj?.a}
      {obj?.b ?? ` World`}
    </>
  );
};

编辑 nice-paper-74xbv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM