首页 > 解决方案 > 使用流的 React 库会导致问题

问题描述

我对 js 和反应比较陌生,所以请多多包涵!create-react-app我使用和 react开始了一个新项目16.12.0。我安装了一个,我猜它使用flow。尝试使用这个 ( npm install) 库会给我以下 SynatError:

./node_modules/react-resize-aware/src/useResizeAware.js
SyntaxError: [...]\node_modules\react-resize-aware\src\useResizeAware.js: Unexpected token, expected "," (5:31)

  3 | import ResizeListener from './ResizeListener';
  4 | 
> 5 | const defaultReporter = (target: ?HTMLElement) => ({
    |                                ^
  6 |   width: target != null ? target.offsetWidth : null,
  7 |   height: target != null ? target.offsetHeight : null,
  8 | });

我觉得这是我这边的一个问题,或者至少是我可以通过对项目的package.json. 如果是这样,如果你能向我解释如何,我会很高兴。

//编辑:我尝试按照此处所述flow添加到我的项目中,但它给了我同样的错误。有效的方法是将库的源代码复制到我的项目中,然后从这个本地版本中导入它。但我真的很想使用 npm 包,而不是自己维护本地版本的 lib。

非常感谢您!

标签: javascriptreactjsnpmpackage.json

解决方案


Silly me, I simply imported from the wrong path. I used

import moduleName from "lib-name/src";

instead of

import moduleNamefrom "lib-name";

推荐阅读