【发布时间】:2019-11-30 09:13:59
【问题描述】:
我正在为我常用的组件开发一个 React 库。在我的根文件夹中:
- 我的汇总配置和包含我的库的
src/文件夹。构建时,捆绑文件(ES、CJS 和 UMD)位于dist/文件夹中。 - 我的工作区:一个简单的
parcel捆绑应用程序,其中有一个独立的package.json。在这个package.json中,myLib 位于 dependencies 中,我已链接它。
当我想在工作区中使用 myLib 时,我会像这样导入它:import { Button } from 'myLib'。
在这里,一切似乎都很好。但是在一个组件中我使用了一个钩子并且我有这个错误:
Uncaught Invariant Violation: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
React 好像有两个副本,npm ls react 返回:
└─┬ myLib@1.0.0 invalid
└── react@16.8.6 extraneous
我试图在 myLib/ 和 workspace/ 之间链接 react 和 react-dom node_modules,但它从来没有工作,这个错误仍然存在。
TLDR;
这是我的文件树:
myLib
│ package.json
│ rollup.config.js
│
└───dist/
│
└───src/
│ │ **
│
└───workspace
│ │ package.json
│ │ index.js
要将 myLib 链接到工作区:我转到 myLib/ 并执行 yarn link 然后转到 workspace/ 并执行 yarn link myLib
react 和 react-dom 在myLib中是peerDependencies,在workspace/中是devDependencies
哦,当然我已经看过这里了:
【问题讨论】:
标签: javascript reactjs npm npm-link