Skip to content

Re-rendering components into a document leaks components #423

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

Merged
merged 1 commit into from
Oct 29, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/core/ReactMount.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,10 @@ var ReactMount = {
unmountComponentFromNode: function(instance, container) {
instance.unmountComponent();

if (container.nodeType === DOC_NODE_TYPE) {
container = container.documentElement;
}

// http://jsperf.com/emptying-a-node
while (container.lastChild) {
container.removeChild(container.lastChild);
Expand Down Expand Up @@ -592,6 +596,8 @@ var ReactMount = {

ATTR_NAME: ATTR_NAME,

getReactRootID: getReactRootID,

getID: getID,

setID: setID,
Expand Down
60 changes: 60 additions & 0 deletions src/core/__tests__/ReactRenderDocument-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,66 @@ describe('rendering React components at document', function() {
testDocument = getTestDocument();
});

it('should be able to get root component id for document node', function() {
if (!testDocument) {
// These tests are not applicable in jst, since jsdom is buggy.
return;
}

var Root = React.createClass({
render: function() {
return (
<html>
<head>
<title>Hello World</title>
</head>
<body>
Hello world
</body>
</html>
);
}
});

ReactMount.allowFullPageRender = true;
var component = React.renderComponent(<Root />, testDocument);
Copy link
Member

Choose a reason for hiding this comment

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

Can you get rid of the var container = (lint is complaining about unused variables - not caught in this repo because we don't lint the tests)

expect(testDocument.body.innerHTML).toBe(' Hello world ');

var componentID = ReactMount.getReactRootID(testDocument);
expect(componentID).toBe(component._rootNodeID);
});

it('should be able to unmount component from document node', function() {
if (!testDocument) {
// These tests are not applicable in jst, since jsdom is buggy.
return;
}

var Root = React.createClass({
render: function() {
return (
<html>
<head>
<title>Hello World</title>
</head>
<body>
Hello world
</body>
</html>
);
}
});

ReactMount.allowFullPageRender = true;
React.renderComponent(<Root />, testDocument);
expect(testDocument.body.innerHTML).toBe(' Hello world ');

var unmounted = React.unmountComponentAtNode(testDocument);
expect(unmounted).toBe(true);
expect(testDocument.documentElement).not.toBe(null);
expect(testDocument.documentElement.innerHTML).toBe('');
});

it('should be able to switch root constructors via state', function() {
if (!testDocument) {
// These tests are not applicable in jst, since jsdom is buggy.
Expand Down
15 changes: 13 additions & 2 deletions src/core/getReactRootElementInContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,23 @@

"use strict";

var DOC_NODE_TYPE = 9;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is already defined in the codebase. Maybe create a DOMNodeTypes module and put that constant in there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we use Node.DOCUMENT_NODE for that or it's not standardised?

Copy link
Member

Choose a reason for hiding this comment

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

I've thought about this before but ended up letting us add a couple constants. I think we should only be accessing these in places where we know we're in a browser environment (since we're checking nodeType), but I wasn't sure at the time so didn't press the matter. Since then though we've added the constants in a couple other places and so long as it's supported in all browsers, we should probably just use Node.*. If anybody knows for sure or wants to test all browsers to make sure, chime up :)

Copy link
Member

Choose a reason for hiding this comment

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

For the time being, let's just keep this in like this and then followup to replace everything across the codebase separately.


/**
* @param {DOMElement} container DOM element that may contain a React component
* @param {DOMElement|DOMDocument} container DOM element that may contain
* a React component
* @return {?*} DOM element that may have the reactRoot ID, or null.
*/
function getReactRootElementInContainer(container) {
return container && container.firstChild;
if (!container) {
return null;
}

if (container.nodeType === DOC_NODE_TYPE) {
return container.documentElement;
} else {
return container.firstChild;
}
}

module.exports = getReactRootElementInContainer;