Skip to content

Commit e10d10e

Browse files
committed
Merge pull request facebook#1531 from spicyj/two-face
Give useful error when using two copies of React
2 parents 559eb89 + 70bf3e1 commit e10d10e

File tree

3 files changed

+668
-619
lines changed

3 files changed

+668
-619
lines changed

src/browser/ui/ReactMount.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
"use strict";
2020

2121
var DOMProperty = require('DOMProperty');
22-
var ReactCurrentOwner = require('ReactCurrentOwner');
2322
var ReactBrowserEventEmitter = require('ReactBrowserEventEmitter');
23+
var ReactCurrentOwner = require('ReactCurrentOwner');
24+
var ReactDescriptor = require('ReactDescriptor');
2425
var ReactInstanceHandles = require('ReactInstanceHandles');
2526
var ReactPerf = require('ReactPerf');
2627

@@ -335,6 +336,21 @@ var ReactMount = {
335336
* @return {ReactComponent} Component instance rendered in `container`.
336337
*/
337338
renderComponent: function(nextDescriptor, container, callback) {
339+
invariant(
340+
ReactDescriptor.isValidDescriptor(nextDescriptor),
341+
'renderComponent(): Invalid component descriptor.%s',
342+
(
343+
ReactDescriptor.isValidFactory(nextDescriptor) ?
344+
' Instead of passing a component class, make sure to instantiate ' +
345+
'it first by calling it with props.' :
346+
// Check if it quacks like a descriptor
347+
typeof nextDescriptor.props !== "undefined" ?
348+
' This may be caused by unintentionally loading two independent ' +
349+
'copies of React.' :
350+
''
351+
)
352+
);
353+
338354
var prevComponent = instancesByReactRootID[getReactRootID(container)];
339355

340356
if (prevComponent) {

src/browser/ui/__tests__/ReactMount-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var mocks = require('mocks');
2424
describe('ReactMount', function() {
2525
var React = require('React');
2626
var ReactMount = require('ReactMount');
27+
var ReactTestUtils = require('ReactTestUtils');
2728

2829
describe('constructAndRenderComponentByID', function() {
2930
it('throws if given an id for a component that doesn\'t exist', function() {
@@ -37,6 +38,29 @@ describe('ReactMount', function() {
3738
});
3839
});
3940

41+
it('throws when given a factory', function() {
42+
expect(function() {
43+
ReactTestUtils.renderIntoDocument(React.DOM.div);
44+
}).toThrow(
45+
'Invariant Violation: renderComponent(): Invalid component descriptor. ' +
46+
'Instead of passing a component class, make sure to instantiate it ' +
47+
'first by calling it with props.'
48+
);
49+
50+
var Component = React.createClass({
51+
render: function() {
52+
return <div />;
53+
}
54+
});
55+
expect(function() {
56+
ReactTestUtils.renderIntoDocument(Component);
57+
}).toThrow(
58+
'Invariant Violation: renderComponent(): Invalid component descriptor. ' +
59+
'Instead of passing a component class, make sure to instantiate it ' +
60+
'first by calling it with props.'
61+
);
62+
});
63+
4064
it('should render different components in same root', function() {
4165
var container = document.createElement('container');
4266
document.documentElement.appendChild(container);

0 commit comments

Comments
 (0)