You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Warn when two versions of React are used alongside #2402
Because there is no warning right away when this happens, they usually discover the problem through invariant violations. Some of them are descriptive (e.g. Uncaught Error: Invariant Violation: The handler for Route "hello" must be a valid React component) and I think I even saw warning that said something like "check if two copies of React are loaded", but some are really cryptic: Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs....
Is there a reason why we don't want to warn right away when two copies of React are loaded?
Reacted by Andy Edwards, Bruno Lemos, nmccready, udayrajMT, Kyle Vasconcellos, Preet Sanghavi, shaune, Ruofan Wei, Ko Seoyoung, Max Donchenko and 7 more
There's probably no good way to find out if two copies of React are loaded (CommonJS modules are the antithesis of sharing a global scope, they're literally two duplicate but separate modules). But I would expect React to be clever enough to figure out that, say, a component is a React component even if it's not from the same copy of React.
@sebmarkbage@zpaoThis is a commonly recurring problem that is very hard to debug, simply setting window.ReactWasHere = true and testing whether it is set would save a lot of people a lot of headache. Any objections to this? Should be preferable to just extend isValidElement (or w/e) with simple duck-typing that warns if it looks like a ReactElement but isn't... or something like that. It's free and doesn't interfere with intentionally running multiple React instances.
Would it catch all of the issues listed above though? Usually the problem comes up when some component or helper library loads its own version. Will we get to isValidElement in this case?
Besides, does React truly support independent Reacts on page right now? Do they not interfere in any way at all if they aren't mixed (e.g. browser event handling)?
Would it catch all of the issues listed above though? Usually the problem comes up when some component or helper library loads its own version. Will we get to isValidElement in this case?
I might be mistaken, but ignoring weird hacks then the only issue with multiple instances of React are when ReactElements from different instances gets mixed up. Depending on which API you feed them to, you're going to see lots of different errors though. So yeah... I think that should take care of all, or most at the very least.
Besides, does React truly support independent Reacts on page right now? Do they not interfere in any way at all if they aren't mixed (e.g. browser event handling)?
As long as you never stop event propagation or mount them inside each other it should be OK I think (but still not great though).
Just a confirmation from someone who just flushed 3 hours of debugging time down the toilet that, yes, this issue is hard to debug, please make at least a warning.
In my case I was having a weird exception (TypeError: Cannot read property 'firstChild' of undefined) caused by the fact that a component created with a different (uninitialised) copy of React was carrying a _owner = null, thereby breaking findComponentRoot().
The other issue with this is you can't reliably use the dependency injection mechanism, e.g. to change the batching strategy, while using components from multiple modules where each has it's own instance of React as a dependency. Even if they are the same version of React.
@charypar said: "The other issue with this is you can't reliably use the dependency injection mechanism, e.g. to change the batching strategy, while using components from multiple modules where each has it's own instance of React as a dependency. Even if they are the same version of React."
@gaearon@spicyj Is it still an issue to load more than one version of React on a page, even if those instances are managing totally separate DOM trees and aren't being explicitly instantiated as globals? Like if, say, I had a package with ComponentA and ComponentB as dependencies, and they both depended on different versions of React, with npm as the dependency manager, could they be used together on one page?
Could you please point me out what would be the behavior of React if , let say , we want to introduce a widget including React in a host page that has an older version of it and the possible workaround to make it work?
Thanks
Old versions of React are probably only tripped up by data-reactid IDs in the DOM that it didn't generate. If you're doing only client-side rendering with your widget, it should only have a data-reactroot attribute and no data-reactid so there shouldn't be any conflict.
Would Preact and React have the same issue described here?
It sounds to me like there are two separate issues here:
Can you have two different copies of the library loaded and in use anywhere on a page in separate components that don't interact with each other?
Can you have components from two different bundles, each with their own copy of the library, interact with each other (eg. the render function from one library receiving an element created with createElement from the other library)
I don't know about the status with modern versions of React. In the case of Preact (1) should be fine but since it does have some global state you could run into problems in situation (2). Please file an issue in the Preact repository if you'd like to discuss further.
For someone encountering this at the moment - does anyone have any tips on identifying the cause and debugging the issue? I've used the check from the hooks page (how I discovered this) which confirms I've got 2 instances loaded:
// Add this in node_modules/react-dom/index.js
window.React1 = require('react');
// Add this in your component file
require('react-dom');
window.React2 = require('react');
console.log(window.React1 === window.React2);
I believe I'm doing things by the book, in a monorepo react, react-dom etc are all always peerDependencies. What steps would you recommend after this?
I believe that as long as you're using React 17+, it is now possible to use more than one version at once. I gather that from the React 17 release notes.
React 17 enables gradual React upgrades. When you upgrade from React 15 to 16 (or, this time, from React 16 to 17), you would usually upgrade your whole app at once. This works well for many apps. But it can get increasingly challenging if the codebase was written more than a few years ago and isn’t actively maintained. And while it’s possible to use two versions of React on the page, until React 17 this has been fragile and caused problems with events.
We’re fixing many of those problems with React 17. This means that when React 18 and the next future versions come out, you will now have more options. The first option will be to upgrade your whole app at once, like you might have done before. But you will also have an option to upgrade your app piece by piece. For example, you might decide to migrate most of your app to React 18, but keep some lazy-loaded dialog or a subroute on React 17.
– React 17 release notes
Activity
pluma commentedon Oct 23, 2014
There's probably no good way to find out if two copies of React are loaded (CommonJS modules are the antithesis of sharing a global scope, they're literally two duplicate but separate modules). But I would expect React to be clever enough to figure out that, say, a component is a React component even if it's not from the same copy of React.
gaearon commentedon Oct 23, 2014
I think it's lesser evil to put some flag on
window, like the flag React reads from devtools, than to spend any more time on this.syranide commentedon Oct 23, 2014
@sebmarkbage @zpao
This is a commonly recurring problem that is very hard to debug, simply settingShould be preferable to just extendwindow.ReactWasHere = trueand testing whether it is set would save a lot of people a lot of headache. Any objections to this?isValidElement(or w/e) with simple duck-typing that warns if it looks like a ReactElement but isn't... or something like that. It's free and doesn't interfere with intentionally running multiple React instances.sebmarkbage commentedon Oct 23, 2014
I think that in the future, this will actually just work. In the meantime we can warn here: https://github.com/facebook/react/blob/master/src/core/ReactElement.js#L230
gaearon commentedon Oct 23, 2014
Would it catch all of the issues listed above though? Usually the problem comes up when some component or helper library loads its own version. Will we get to
isValidElementin this case?Besides, does React truly support independent Reacts on page right now? Do they not interfere in any way at all if they aren't mixed (e.g. browser event handling)?
gaearon commentedon Oct 23, 2014
(To clarify, I was referring to @syranide'a comment)
syranide commentedon Oct 23, 2014
I might be mistaken, but ignoring weird hacks then the only issue with multiple instances of React are when ReactElements from different instances gets mixed up. Depending on which API you feed them to, you're going to see lots of different errors though. So yeah... I think that should take care of all, or most at the very least.
As long as you never stop event propagation or mount them inside each other it should be OK I think (but still not great though).
Good point.
stefanomasini commentedon Nov 14, 2014
Just a confirmation from someone who just flushed 3 hours of debugging time down the toilet that, yes, this issue is hard to debug, please make at least a warning.
In my case I was having a weird exception (TypeError: Cannot read property 'firstChild' of undefined) caused by the fact that a component created with a different (uninitialised) copy of React was carrying a _owner = null, thereby breaking findComponentRoot().
sophiebits commentedon Nov 17, 2014
We should already be warning in the case that an element from one copy of React is passed to React.render in another:
https://github.com/facebook/react/blob/d75512f211e4240a040eba4c4cb337f539281bc3/src/browser/ui/ReactMount.js#L363
I guess the cases here are when nesting elements from different versions inside each other?
stefanomasini commentedon Nov 17, 2014
Yes. At least it was in my case. Nested elements created from different React versions.
Removing react dep
joaomilho commentedon Feb 8, 2015
Just my 2cents: I had the same issue today and my problem was using browserify 8.1.3 instead of 8.1.1
Hope it helps
charypar commentedon Feb 25, 2015
The other issue with this is you can't reliably use the dependency injection mechanism, e.g. to change the batching strategy, while using components from multiple modules where each has it's own instance of React as a dependency. Even if they are the same version of React.
nottoseethesun commentedon Feb 26, 2015
+1 ; thanks gaearon
sedge commentedon Mar 9, 2015
@charypar said: "The other issue with this is you can't reliably use the dependency injection mechanism, e.g. to change the batching strategy, while using components from multiple modules where each has it's own instance of React as a dependency. Even if they are the same version of React."
This.
40 remaining items
mnquintana commentedon Aug 4, 2016
@gaearon @spicyj Is it still an issue to load more than one version of React on a page, even if those instances are managing totally separate DOM trees and aren't being explicitly instantiated as globals? Like if, say, I had a package with
ComponentAandComponentBas dependencies, and they both depended on different versions of React, with npm as the dependency manager, could they be used together on one page?sophiebits commentedon Aug 4, 2016
@mnquintana No, that should work now if both are on a recent version of React.
idhard commentedon Sep 2, 2016
Could you please point me out what would be the behavior of React if , let say , we want to introduce a widget including React in a host page that has an older version of it and the possible workaround to make it work?
Thanks
sophiebits commentedon Sep 2, 2016
Old versions of React are probably only tripped up by data-reactid IDs in the DOM that it didn't generate. If you're doing only client-side rendering with your widget, it should only have a data-reactroot attribute and no data-reactid so there shouldn't be any conflict.
jedwards1211 commentedon Sep 14, 2018
Just ran into problems again today when I accidentally had 16.4.2 and 16.5.0 installed in a project, no warning from React.
To make matters worse, the duplicate versions were caused by a bug in
yarn upgradethat has been open for over a year...warn when it looks like you haven't updated react-dom
OliverRadini commentedon Mar 12, 2019
Was there any resolution as to whether or not this is possible and/or a good idea? It seems like it'd provide developers with useful information.
prasanthLalapeta commentedon Jul 24, 2019
Project A uses React 16 wherein Project B uses React 15, Lets say Project A is package got used in Project B - can that work?
dgreene1 commentedon May 15, 2020
Would Preact and React have the same issue described here?
I’m asking because I’m using single-spa for a platform play at a large company of distributed teams.
robertknight commentedon May 15, 2020
It sounds to me like there are two separate issues here:
renderfunction from one library receiving an element created withcreateElementfrom the other library)I don't know about the status with modern versions of React. In the case of Preact (1) should be fine but since it does have some global state you could run into problems in situation (2). Please file an issue in the Preact repository if you'd like to discuss further.
dkeesey commentedon Feb 23, 2022
This appears to be a bad link.
IPWright83 commentedon Oct 4, 2022
For someone encountering this at the moment - does anyone have any tips on identifying the cause and debugging the issue? I've used the check from the hooks page (how I discovered this) which confirms I've got 2 instances loaded:
I believe I'm doing things by the book, in a monorepo react, react-dom etc are all always
peerDependencies. What steps would you recommend after this?fcano-ut commentedon Mar 7, 2025
I believe that as long as you're using React 17+, it is now possible to use more than one version at once. I gather that from the React 17 release notes.
Warn when multiple instances of React are loaded on the same page
Warn when multiple instances of React are loaded on the same page