简体   繁体   English

尝试将 react-select 导入使用 create-react-app 创建的项目时出错

[英]Error trying to import react-select to a project created with create-react-app

yarn create react-app
yarn add react-select

I created a react app using create-react-app, and it worked fine.我使用 create-react-app 创建了一个反应应用程序,它运行良好。 Then I wanted to use react-select, so I installed it with yarn, added an import statement like import Select from "react-select/src/Select";然后我想使用react-select,所以我用yarn安装了它,添加了一个import语句,比如import Select from "react-select/src/Select";

I got this error:我收到了这个错误:

Failed to compile.

./node_modules/react-select/src/Select.js
SyntaxError: ~~~~\node_modules\react-select\src\Select.js: Unexpected token, expected "," (3:32)

  1 | // @flow
  2 | 
> 3 | import React, { Component, type ElementRef, type Node } from 'react';
    |                                 ^
  4 | import memoizeOne from 'memoize-one';
  5 | import { MenuPlacer } from './components/Menu';
  6 | import isEqual from './internal/react-fast-compare';

I can see the type keyword is causing the issue and suspect it has to do with something named flow which seems a static type checker.我可以看到type关键字导致了问题,并怀疑它与名为flow的东西有关,它似乎是 static 类型检查器。 What should I do to make it work?我应该怎么做才能让它工作?

Just import like就像导入一样

import Select from "react-select";

Please note that: import Select from "react-select";请注意: import Select from "react-select"; as above may not be enough!如上可能还不够!

In my case, in VSCODE there are macros that when invoked, generates someting like:就我而言,在 VSCODE 中,有一些宏在调用时会生成如下内容:

import { valueFocusAriaMessage } from 'react-select/src/accessibility'

Comment out / delete the line to solve the syntax error problem.注释掉/删除该行以解决语法错误问题。 I emphasize the issue since the line is added at the top, while you may not notice it.我强调这个问题,因为该行添加在顶部,而您可能没有注意到它。 Beware.谨防。

暂无
暂无

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

相关问题 尝试在 create-react-app 中导入 npm 时出错 - Error while trying to import npm got in create-react-app 然后尝试 create-react-app 时出错(npm ERR!cb() never call!然后尝试 create-react-app) - Error then trying create-react-app( npm ERR! cb() never called! then trying create-react-app) 是否可以将一组图像作为数组导入? (create-react-app项目) - Is it possible to import a group of images as an array? (create-react-app project) 在 create-react-app 中有条件地导入资产 - Conditionally import assets in create-react-app 将 bootstrap 5 组件导入 create-react-app - Import bootstrap 5 component into create-react-app 如何为使用 create-react-app 创建的 react 项目更新 webpack 配置? - How to update webpack config for a react project created using create-react-app? 在Python和使用create-react-app创建的React项目之间共享配置文件 - Sharing a configuration file between Python and a React project created with create-react-app 如何编写在项目创建的 create-react-app 的单页反应路由器上工作的 JS - How to write JS which working on single page of react router at project created create-react-app 如何从不同的 create-react-app 项目本地导入 React 组件? - How to import React component locally from a different create-react-app project? 将使用 create-react-app 创建的 React 应用程序集成到外部应用程序中 - Integrate React app created with create-react-app into an external application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM