Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple React instances per DOM (when using AMD for example) #3252

Closed
sennett opened this issue Feb 24, 2015 · 20 comments
Closed

Multiple React instances per DOM (when using AMD for example) #3252

sennett opened this issue Feb 24, 2015 · 20 comments

Comments

@sennett
Copy link

sennett commented Feb 24, 2015

When using AMD, a component might use React as a dependency. This poses a problem when another instance of React has already manipulated the DOM. The wrong events get fired, or ID collisions occur (Invariant Violation: ReactMount: Two valid but unequal nodes with the same data-reactid: .0).

jsbin here: http://jsbin.com/riqotavoxa/11/edit?js,output
SO here for internet points: http://stackoverflow.com/q/28685857/614523
Google Group post here: https://groups.google.com/forum/#!topic/reactjs/RRDUaF19CM0

@sennett
Copy link
Author

sennett commented Feb 24, 2015

If I may be so bold, I see two ways forward. Either each React knows about other instances of React through something global (it's operating over the entire DOM after all) or each React instance creates its own random root key in getReactRootIDString similar.

@gaearon
Copy link
Collaborator

gaearon commented Feb 24, 2015

Is this #2402?

@sennett
Copy link
Author

sennett commented Feb 25, 2015

I did not see that one.

It is similar, but where they just want a warning (that they can presumably act upon by removing one of the Reacts they mistakenly loaded), there are scenarios where multiple React instances operating over the same DOM (but not component tree) are required.

@gaearon
Copy link
Collaborator

gaearon commented Feb 25, 2015

there are scenarios where multiple React instances operating over the same DOM (but not component tree) are required.

I don't understand. What is the use case for this?

@glenjamin
Copy link
Contributor

there are scenarios where multiple React instances operating over the same DOM (but not component tree) are required.

I don't understand. What is the use case for this?

The cases I've seen are usually around addons or 3rd-party widgets/scripts.

@sennett
Copy link
Author

sennett commented Feb 25, 2015

What is the use case for this?

Mine is UI composition via AMD, but it could be something like two separate CommonJS bundles on a page (possibly - I don't use CommonJS), or widgets.

@charypar
Copy link

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.

(originally posted here, sorry for the duplicate comment: #2402 (comment))

@nottoseethesun
Copy link

+1

@xarlio
Copy link

xarlio commented Mar 3, 2015

+1

@Macil
Copy link

Macil commented Mar 7, 2015

I'm interested in using React from inside Chrome extensions to add stuff to pages (that may or may not be using React themselves). Chrome extensions share the DOM but don't share the javascript context with the page, so I guess this falls under "multiple React instances operating over the same DOM (but not component tree)".

@sedge
Copy link

sedge commented Mar 9, 2015

+1

@sedge
Copy link

sedge commented 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.

@Robert-W
Copy link

+1 Seeing similar issue in many places with the error being shown from #2402, I have to write in AMD since our company uses Esri's JavaScript API, which heavily relies on Dojo. I have one module which creates a component and returns the result of React.createClass. Somewhere else in the app we will call React.render(<MyComponent />, someNode); and it throws the multiple instances error. I can get around this by returning something like

return function (node, props) {
/* jshint ignore:start /
return React.render(<MyComponent />, document.getElementById(node));
/
jshint ignore:end */
};

Also seeing it occasionally in jasmine-node when calling TestUtils.renderIntoDocument

Note: I noticed this started when upgrading to React 0.13.1

@zbyte64
Copy link

zbyte64 commented Apr 17, 2015

+1 noticed this in nodejs, calling a renderToStaticMarkup from one package on react components from another package.

@fourcube
Copy link

+1

1 similar comment
@YPCrumble
Copy link

+1

@mathieumg
Copy link
Contributor

+1

@zpao
Copy link
Member

zpao commented Jul 31, 2015

Yea, closing. This is the same as other issues mentioned.

@zpao zpao closed this as completed Jul 31, 2015
@sennett
Copy link
Author

sennett commented Jul 31, 2015

@zpao what issue is this now tracked under please?

@zpao
Copy link
Member

zpao commented Jul 31, 2015

#2402

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests