@samitouri / QOS-React-2 / commits / d002abf0e7

Warn with two copies of React

Ben Alpert committed Mar 5, 2015 at 16:22 UTC d002abf0e715319eb23ce9e02d9d9fddd538522a
2 files changed +37
src/renderers/dom/client/ReactMount.js
+12
@@ -795,6 +795,18 @@ var ReactMount = {
795
796 var deepestAncestor = findDeepestCachedAncestor(targetID) || ancestorNode;
797
798 + if (__DEV__) {
799 + // This will throw on the next line; give an early warning
800 + warning(
801 + deepestAncestor != null,
802 + 'React can\'t find the root component node for data-reactid value ' +
803 + '`%s`. If you\'re seeing this message, it probably means that ' +
804 + 'you\'ve loaded two copies of React on the page. At this time, only ' +
805 + 'a single copy of React can be loaded at a time.',
806 + targetID
807 + );
808 + }
809 +
810 firstChildren[0] = deepestAncestor.firstChild;
811 firstChildren.length = 1;
812
src/renderers/dom/client/__tests__/ReactMount-test.js
+25
@@ -199,4 +199,29 @@ describe('ReactMount', function() {
199 element.unmount();
200 });
201 }
202 +
203 + it.only('warns when using two copies of React before throwing', function() {
204 + require('mock-modules').dumpCache();
205 + var RD1 = require('ReactDOM');
206 + require('mock-modules').dumpCache();
207 + var RD2 = require('ReactDOM');
208 +
209 + var X = React.createClass({
210 + render: function() {
211 + return <div />;
212 + },
213 + });
214 +
215 + var container = document.createElement('div');
216 + console.error = mocks.getMockFunction();
217 + var component = RD1.render(<X />, container);
218 + expect(console.error.mock.calls.length).toBe(0);
219 +
220 + // This fails but logs a warning first
221 + expect(function() {
222 + RD2.findDOMNode(component);
223 + }).toThrow();
224 + expect(console.error.mock.calls.length).toBe(1);
225 + expect(console.error.mock.calls[0][0]).toContain('two copies of React');
226 + });
227 });