Skip to content

Warn when two versions of React are used alongside #2402

Closed

Description

@gaearon

People lose hours of work debugging a simple issue: two versions of React being loaded at the same time.

gaearon/react-hot-loader#32 (comment)
KyleAMathews/coffee-react-quickstart#10 (comment)
gaearon/react-document-title#1 (comment)
clayallsopp/react.backbone#26

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?

Activity

  1. pluma commented on Oct 23, 2014

    @pluma
    Contributor

    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.

  2. gaearon commented on Oct 23, 2014

    @gaearon
    CollaboratorAuthor

    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.

  3. syranide commented on Oct 23, 2014

    @syranide
    Contributor

    @sebmarkbage @zpao This 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.

  4. sebmarkbage commented on Oct 23, 2014

    @sebmarkbage
    Contributor
  5. gaearon commented on Oct 23, 2014

    @gaearon
    CollaboratorAuthor

    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)?

  6. gaearon commented on Oct 23, 2014

    @gaearon
    CollaboratorAuthor

    (To clarify, I was referring to @syranide'a comment)

  7. syranide commented on Oct 23, 2014

    @syranide
    Contributor

    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).

    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

    Good point.

  8. stefanomasini commented on Nov 14, 2014

    @stefanomasini

    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().

  9. sophiebits commented on Nov 17, 2014

    @sophiebits
    Collaborator

    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?

  10. stefanomasini commented on Nov 17, 2014

    @stefanomasini

    Yes. At least it was in my case. Nested elements created from different React versions.

  11. added a commit that references this issue on Jan 6, 2015
  12. joaomilho commented on Feb 8, 2015

    @joaomilho

    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

  13. charypar commented on Feb 25, 2015

    @charypar

    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.

  14. nottoseethesun commented on Feb 26, 2015

    @nottoseethesun

    +1 ; thanks gaearon

  15. sedge commented on Mar 9, 2015

    @sedge

    @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.

  16. 40 remaining items

  17. mnquintana commented on Aug 4, 2016

    @mnquintana

    @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?

  18. sophiebits commented on Aug 4, 2016

    @sophiebits
    Collaborator

    @mnquintana No, that should work now if both are on a recent version of React.

  19. idhard commented on Sep 2, 2016

    @idhard

    if both are on a recent version of

    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

  20. sophiebits commented on Sep 2, 2016

    @sophiebits
    Collaborator

    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.

  21. jedwards1211 commented on Sep 14, 2018

    @jedwards1211
    Contributor

    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 upgrade that has been open for over a year...

  22. added a commit that references this issue on Jan 11, 2019
    343a70d
  23. OliverRadini commented on Mar 12, 2019

    @OliverRadini

    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.

  24. prasanthLalapeta commented on Jul 24, 2019

    @prasanthLalapeta

    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?

  25. dgreene1 commented on May 15, 2020

    @dgreene1

    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.

  26. robertknight commented on May 15, 2020

    @robertknight
    Contributor

    Would Preact and React have the same issue described here?

    It sounds to me like there are two separate issues here:

    1. 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?
    2. 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.

  27. dkeesey commented on Feb 23, 2022

    @dkeesey

    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

    This appears to be a bad link.

  28. IPWright83 commented on Oct 4, 2022

    @IPWright83

    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?

  29. fcano-ut commented on Mar 7, 2025

    @fcano-ut

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions