Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check that container is a valid DOM element #112

Merged
merged 1 commit into from
Jun 25, 2013
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/core/ReactComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,10 @@ var ReactComponent = {
container,
transaction,
shouldReuseMarkup) {
invariant(
container && container.nodeType === 1,
'mountComponentIntoNode(...): Target container is not a DOM element.'
);
var renderStart = Date.now();
var markup = this.mountComponent(rootID, transaction);
ReactMount.totalInstantiationTime += (Date.now() - renderStart);
Expand Down
2 changes: 1 addition & 1 deletion src/core/ReactMount.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var containersByReactRootID = {};
* @return {?*} DOM element that may have the reactRoot ID, or null.
*/
function getReactRootElementInContainer(container) {
return container.firstChild;
return container && container.firstChild;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/core/__tests__/ReactComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ describe('ReactComponent', function() {
reactComponentExpect = require('reactComponentExpect');
});

it('should throw on invalid render targets', function() {
var container = document.createElement('div');
// jQuery objects are basically arrays; people often pass them in by mistake
expect(function() {
React.renderComponent(<div></div>, [container]);
}).toThrow(
'Invariant Violation: mountComponentIntoNode(...): Target container is ' +
'not a DOM element.'
);

expect(function() {
React.renderComponent(<div></div>, null);
}).toThrow(
'Invariant Violation: mountComponentIntoNode(...): Target container is ' +
'not a DOM element.'
);
});

it('should throw when supplying a ref outside of render method', function() {
var instance = <div ref="badDiv" />;
expect(function() {
Expand Down