加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – 如何在开发外部组件时避免“加载两个React副本”错误

发布时间:2020-12-15 20:54:46 所属栏目:百科 来源:网络整理
导读:我正在开发一个外部组件(假设我的组件,我用npm链接链接到项目(因为它正在进行中,我需要包来反映更改)。 在my-component文件夹中有node_modules / react和node_modules / react-dom,因为它们是它的依赖项。但是它们是peerDpendences,所以我没想把它们带到
我正在开发一个外部组件(假设我的组件,我用npm链接链接到项目(因为它正在进行中,我需要包来反映更改)。

在my-component文件夹中有node_modules / react和node_modules / react-dom,因为它们是它的依赖项。但是它们是peerDpendences,所以我没想把它们带到链接这个包的项目中。

但是,当使用npm链接时,它会链接整个目录,包括node_modules。因此,当项目构建时,它包括2次包:来自node_modules / *和来自node_modules / my-component / node_modules / *。

这开始影响组件何时使用ReactDOM.findDOMNode,它会导致此错误:

Warning: React can't find the root component node for data-reactid value `.0.2.0`. If you're seeing this message,it probably means that you've loaded two copies of React on the page. At this time,only a single copy of React can be loaded at a time.

此外,它可能有助于了解发生了什么:仅当存在node_modules / my-component / node_modules / react和node_modules / my-component / node_modules / react-dom时才会出现问题。如果只有其中一个,则没有错误消息。

通常的软件包安装不会带来这样的错误,因为那里没有node_modules / react-dom。

如何同时开发外部组件和项目?

我相信答案是在外部组件的package.json中将react和react-dom指定为peerDependencies。尽管我可以遵循 here和 here,但是npm链接应该(从npm @ 3开始)不再安装peerDependencies(或者也可以是`devDependencies)。

Aaaand我只是更仔细地阅读你的帖子,并意识到你已经将它们指定为peerDependencies。因此,我认为答案归结为:

升级到npm @ 3:

npm install -g npm@3.0-latest

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读