The library for web and native user interfaces.
Check that `container` is a valid DOM element
Ben Alpert committed
Jun 19, 2013 at 00:23 UTC
f6c4d2d16110e45bc9969cf6ad42290dd4d09c60
3 files changed
+23
-1
src/core/ReactComponent.js
+4
@@ -511,6 +511,10 @@ var ReactComponent = {
511
container,
512
transaction,
513
shouldReuseMarkup) {
514
+ invariant(
515
+ container && container.nodeType === 1,
516
+ 'mountComponentIntoNode(...): Target container is not a DOM element.'
517
+ );
518
var renderStart = Date.now();
519
var markup = this.mountComponent(rootID, transaction);
520
ReactMount.totalInstantiationTime += (Date.now() - renderStart);
src/core/ReactMount.js
+1
-1
@@ -35,7 +35,7 @@ var containersByReactRootID = {};
35
* @return {?*} DOM element that may have the reactRoot ID, or null.
36
*/
37
function getReactRootElementInContainer(container) {
38
- return container.firstChild;
38
+ return container && container.firstChild;
39
}
40
41
/**
src/core/__tests__/ReactComponent-test.js
+18
@@ -31,6 +31,24 @@ describe('ReactComponent', function() {
31
reactComponentExpect = require('reactComponentExpect');
32
});
33
34
+ it('should throw on invalid render targets', function() {
35
+ var container = document.createElement('div');
36
+ // jQuery objects are basically arrays; people often pass them in by mistake
37
+ expect(function() {
38
+ React.renderComponent(<div></div>, [container]);
39
+ }).toThrow(
40
+ 'Invariant Violation: mountComponentIntoNode(...): Target container is ' +
41
+ 'not a DOM element.'
42
+ );
43
+
44
+ expect(function() {
45
+ React.renderComponent(<div></div>, null);
46
+ }).toThrow(
47
+ 'Invariant Violation: mountComponentIntoNode(...): Target container is ' +
48
+ 'not a DOM element.'
49
+ );
50
+ });
51
+
52
it('should throw when supplying a ref outside of render method', function() {
53
var instance = <div ref="badDiv" />;
54
expect(function() {