Skip to content

Commit

Permalink
Merge pull request facebook#1531 from spicyj/two-face
Browse files Browse the repository at this point in the history
Give useful error when using two copies of React
  • Loading branch information
chenglou committed Jul 3, 2014
2 parents 559eb89 + 70bf3e1 commit e10d10e
Show file tree
Hide file tree
Showing 3 changed files with 668 additions and 619 deletions.
18 changes: 17 additions & 1 deletion src/browser/ui/ReactMount.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
"use strict";

var DOMProperty = require('DOMProperty');
var ReactCurrentOwner = require('ReactCurrentOwner');
var ReactBrowserEventEmitter = require('ReactBrowserEventEmitter');
var ReactCurrentOwner = require('ReactCurrentOwner');
var ReactDescriptor = require('ReactDescriptor');
var ReactInstanceHandles = require('ReactInstanceHandles');
var ReactPerf = require('ReactPerf');

Expand Down Expand Up @@ -335,6 +336,21 @@ var ReactMount = {
* @return {ReactComponent} Component instance rendered in `container`.
*/
renderComponent: function(nextDescriptor, container, callback) {
invariant(
ReactDescriptor.isValidDescriptor(nextDescriptor),
'renderComponent(): Invalid component descriptor.%s',
(
ReactDescriptor.isValidFactory(nextDescriptor) ?
' Instead of passing a component class, make sure to instantiate ' +
'it first by calling it with props.' :
// Check if it quacks like a descriptor
typeof nextDescriptor.props !== "undefined" ?
' This may be caused by unintentionally loading two independent ' +
'copies of React.' :
''
)
);

var prevComponent = instancesByReactRootID[getReactRootID(container)];

if (prevComponent) {
Expand Down
24 changes: 24 additions & 0 deletions src/browser/ui/__tests__/ReactMount-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var mocks = require('mocks');
describe('ReactMount', function() {
var React = require('React');
var ReactMount = require('ReactMount');
var ReactTestUtils = require('ReactTestUtils');

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

it('throws when given a factory', function() {
expect(function() {
ReactTestUtils.renderIntoDocument(React.DOM.div);
}).toThrow(
'Invariant Violation: renderComponent(): Invalid component descriptor. ' +
'Instead of passing a component class, make sure to instantiate it ' +
'first by calling it with props.'
);

var Component = React.createClass({
render: function() {
return <div />;
}
});
expect(function() {
ReactTestUtils.renderIntoDocument(Component);
}).toThrow(
'Invariant Violation: renderComponent(): Invalid component descriptor. ' +
'Instead of passing a component class, make sure to instantiate it ' +
'first by calling it with props.'
);
});

it('should render different components in same root', function() {
var container = document.createElement('container');
document.documentElement.appendChild(container);
Expand Down
Loading

0 comments on commit e10d10e

Please sign in to comment.