Bug: Allow multiple instance of React (or inject one into the other) #22159
Labels
Resolution: Stale
Automatically closed due to inactivity
Status: Unconfirmed
A potential issue that we haven't yet confirmed as a bug
FYI; Not sure if this is a bug or a feature request. It's a problem that is surprising when it happens, and there's no workaround I'm aware of.
Now that since 17, React is supposed to more easily be upgradable in that using components from an older/newer React in a different version will still work (and be supported), the ability to run multiple instances concurrently should be approached again.
From what I can see, this can be achieved in multiple ways;
There are use cases for this, e.g. when using a component from a webpage in a jsdom environment. This is working for passive components not using any hooks, but as soon as you need hooks (or lifecycle management in general I suppose), the Invalid Hook Call Warning (referenced in #13991) is triggered, due to "3. You might have more than one copy of React in the same app."
Consider the following code, which simulates a web page using React, which exposes a component on the global object "window" (this could be done in multiple ways, the implementation detail here is irrelevant). When running this webpage in a jsdom environment, using the web page's component from an outer context (potentially not even using react-dom but a custom reconciler), the "Invalid Hook Call Warning" is triggered. The example also shows where an outer React instance could be injected into another React (another global scope) if this was supported somehow:
Expected output (which you get when not using any hook):
Result: <div data-reactroot=""><span>from webpage</span></div>
There are two closed issues related to this, but with a different approach and not about actually enabling this afaict: #2402 and #3252
There's also #13991, but is mostly for how to detect when you have multiple instances concurrently, often due to packaging/bundling/dependency issues. This is not the same as intentionally using multiple instances.
The text was updated successfully, but these errors were encountered: