我有一个相当标准的 lerna monorepo,它看起来像这样:
packages/
main/ - This is the main deployable application, it depends on both dep and react-dep
dep/ - Just some pure functions, no problems here
react-dep/ - A design system built with react, this is where we have problems.
因此,一个真正常见的问题是,一旦您开始在依赖库中使用钩子,您就会收到以下消息:
Error: 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
See https://reactjs.org/warnings/invalid-hook-call-warning.html for tips about how to debug and fix this problem.
这是因为您的应用程序中有两个版本的 React,一个来自主应用程序,一个来自依赖项。
现在 - 我已经使用和工作的一个常见解决方案是在你的 webpack 配置中声明react
和任何其他共享/对等依赖项。例如,正如这里所建议的那样。或查看来自 react 的这个 Github 问题线程。externals
但是,我不喜欢这个解决方案,首先,如果我不使用 webpack 怎么办,其次我不应该手动跟踪我需要标记为外部的依赖项。
我认为应该起作用的是:
在react-dep
我声明react
两者devDependencies
和peerDependencies
。我把它放进去的原因devDependencies
是因为我的依赖库可能使用故事书或类似的东西来开发组件,所以我确实需要在开发中做出反应。
我认为如果我要发布react-dep
到 npm 并使用 npm in 中的编译代码,这应该可以工作main
,因为只会dependencies
获取。
但是,我认为由于 lerna 符号链接,在这种情况下发生的情况是 dev 依赖仍然存在,我们得到了这个错误。
有没有办法为 lerna monorepo 解决这个问题?
这是演示此问题的 github 存储库:https ://github.com/dwjohnston/lerna-react-monorepo