Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/renderers/dom/client/ReactMount.js
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,18 @@ var ReactMount = {

var deepestAncestor = findDeepestCachedAncestor(targetID) || ancestorNode;

if (__DEV__) {
// This will throw on the next line; give an early warning
warning(
deepestAncestor != null,
'React can\'t find the root component node for data-reactid value ' +
'`%s`. If you\'re seeing this message, it probably means that ' +
'you\'ve loaded two copies of React on the page. At this time, only ' +
'a single copy of React can be loaded at a time.',
targetID
);
}

firstChildren[0] = deepestAncestor.firstChild;
firstChildren.length = 1;

Expand Down
25 changes: 25 additions & 0 deletions src/renderers/dom/client/__tests__/ReactMount-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,29 @@ describe('ReactMount', function() {
element.unmount();
});
}

it.only('warns when using two copies of React before throwing', function() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit confused, why is .only there?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake, thanks.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(#4609)

require('mock-modules').dumpCache();
var RD1 = require('ReactDOM');
require('mock-modules').dumpCache();
var RD2 = require('ReactDOM');

var X = React.createClass({
render: function() {
return <div />;
},
});

var container = document.createElement('div');
console.error = mocks.getMockFunction();
var component = RD1.render(<X />, container);
expect(console.error.mock.calls.length).toBe(0);

// This fails but logs a warning first
expect(function() {
RD2.findDOMNode(component);
}).toThrow();
expect(console.error.mock.calls.length).toBe(1);
expect(console.error.mock.calls[0][0]).toContain('two copies of React');
});
});