简体   繁体   English

如何防止加载多个 React 副本?

[英]How Prevent Multiple Copies Of React from Loading?

In my previous Meteor app, using browserify, and React, all was working until I switched to meteor webpack .在我之前的 Meteor 应用程序中,使用 browserify 和 React,一切正常,直到我切换到meteor webpack

I use react-select in my Meteor apps and it worked great but with browserify I could prevent multiple copies of react from loading which prevents this error I'm now having:我在我的 Meteor 应用程序中使用react-select并且效果很好,但是使用 browserify 我可以防止加载多个 react 副本,从而防止我现在遇到的这个错误:

Error: Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs. You might be adding a ref to a component that was not created inside a component's `render` method, or you have multiple copies of React loaded (details: https://fb.me/react-refs-must-have-owner).

My package.json look this:我的 package.json 看起来像这样:

...

"dependencies": {
    "classnames": "^2.1.3",
    "lodash": "^3.10.0",
    "react": "^0.14.6",
    "react-dom": "^0.14.6",
    "react-mixin": "^2.0.1",
    "react-select": "^1.0.0-beta8"
  },

...

Is there a configuration in webpack I could use something call externals? webpack 中是否有配置我可以使用称为外部的东西? Not fully sure what that means but a comment said to use:不完全确定这意味着什么,但评论说使用:

externals: {
  'react': 'React',
  'react-dom': 'ReactDOM'
}

Since you use webpack, you can add an alias for loading react, like this:由于您使用 webpack,您可以添加一个别名来加载 react,如下所示:

// In webpack.config.js

  resolve: {
    alias: {
      react: path.resolve('node_modules/react'),
    },
  },

This prevented the addComponentAsRefTo(...) error and made our build succeed again.这防止了addComponentAsRefTo(...)错误并使我们的构建再次成功。 However, for some reason, the testing build failed only on our CI environment as it could not resolve the node_modules/react path.但是,出于某种原因,测试构建仅在我们的 CI 环境中失败,因为它无法解析node_modules/react路径。 I think it's unlikely that you will encounter this particular problem, though.不过,我认为您不太可能遇到这个特殊问题。

Something that worked for me was:对我有用的是:

Uninstall all globally installed packages related to react (create-react-app, react-native, react and so on)卸载所有与 react 相关的全局安装包(create-react-app、react-native、react 等)

then: rm -rf node_modules然后: rm -rf node_modules

then: use npm install instead of yarn install然后:使用npm install而不是 yarn install

considering an App created with crate-react-app and ejected考虑使用 crate-react-app 创建并弹出的应用程序

In my case, I was building a separate npm module, then including that as a library in another project locally using npm link ../some-library .就我而言,我正在构建一个单独的 npm 模块,然后使用npm link ../some-library其作为库包含在本地的另一个项目中。 One of the modules within that library caused this error when I ran the parent project.当我运行父项目时,该库中的一个模块导致了这个错误。

When I ran into this error, the solution for me was to prevent react and react-dom from being including in the some-library bundle output, by adding the following to the module.exports of its webpack.config.js:当我遇到这个错误时,我的解决方案是通过将以下内容添加到其 webpack.config.js 的 module.exports 来防止 react 和 react-dom 包含在 some-library 包输出中:

externals: {
    react: 'react',
    'react-dom': 'react-dom'
}

如果您使用 webpack,那么您可以通过将以下内容添加到 Webpack 配置中来修复它,以用于 Don't bundle react 或 react-dom

externals: { 'react': 'React', 'react-dom': 'ReactDOM' }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM