3
yarn create react-app
yarn add react-select

我使用 create-react-app 创建了一个反应应用程序,它运行良好。然后我想使用react-select,所以我用yarn安装了它,添加了一个import语句,比如import Select from "react-select/src/Select";

我收到了这个错误:

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';

我可以看到type关键字导致了问题,并怀疑它与命名的东西有关flow,它似乎是一个静态类型检查器。我应该怎么做才能让它工作?

4

2 回答 2

9

就像导入一样

import Select from "react-select";
于 2020-04-29T14:20:07.870 回答
0

请注意:import Select from "react-select";如上可能还不够!

就我而言,在 VSCODE 中,有一些宏在调用时会生成如下内容:

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

注释掉/删除该行以解决语法错误问题。我强调这个问题,因为该行添加在顶部,而您可能没有注意到它!谨防。

于 2021-02-07T08:50:32.997 回答