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

Warn if using React.unmountComponentAtNode on a different React instance's tree. #7456

Merged
merged 3 commits into from
Aug 18, 2016
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
65 changes: 50 additions & 15 deletions src/renderers/dom/client/ReactMount.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,45 @@ function hasNonRootReactChild(container) {
}
}

/**
* True if the supplied DOM node is a React DOM element and
* it has been rendered by another copy of React.
*
* @param {?DOMElement} node The candidate DOM node.
* @return {boolean} True if the DOM has been rendered by another copy of React
* @internal
*/
function nodeIsRenderedByOtherInstance(container) {
var rootEl = getReactRootElementInContainer(container);
return !!(rootEl && isReactNode(rootEl) && !ReactDOMComponentTree.getInstanceFromNode(rootEl));
}

/**
* True if the supplied DOM node is a valid node element.
*
* @param {?DOMElement} node The candidate DOM node.
* @return {boolean} True if the DOM is a valid DOM node.
* @internal
*/
function isValidContainer(node) {
return !!(node && (
node.nodeType === ELEMENT_NODE_TYPE ||
node.nodeType === DOC_NODE_TYPE ||
node.nodeType === DOCUMENT_FRAGMENT_NODE_TYPE
));
}

/**
* True if the supplied DOM node is a valid React node element.
*
* @param {?DOMElement} node The candidate DOM node.
* @return {boolean} True if the DOM is a valid React DOM node.
* @internal
*/
function isReactNode(node) {
return isValidContainer(node) && (node.hasAttribute(ROOT_ATTR_NAME) || node.hasAttribute(ATTR_NAME));
}

function getHostRootInstanceInContainer(container) {
var rootEl = getReactRootElementInContainer(container);
var prevHostInstance =
Expand Down Expand Up @@ -329,11 +368,7 @@ var ReactMount = {
);

invariant(
container && (
container.nodeType === ELEMENT_NODE_TYPE ||
container.nodeType === DOC_NODE_TYPE ||
container.nodeType === DOCUMENT_FRAGMENT_NODE_TYPE
),
isValidContainer(container),
'_registerComponent(...): Target container is not a DOM element.'
);

Expand Down Expand Up @@ -546,14 +581,18 @@ var ReactMount = {
);

invariant(
container && (
container.nodeType === ELEMENT_NODE_TYPE ||
container.nodeType === DOC_NODE_TYPE ||
container.nodeType === DOCUMENT_FRAGMENT_NODE_TYPE
),
isValidContainer(container),
'unmountComponentAtNode(...): Target container is not a DOM element.'
);

if (__DEV__) {
warning(
!nodeIsRenderedByOtherInstance(container),
'unmountComponentAtNode(): The node you\'re attempting to unmount ' +
'was rendered by another copy of React.'
);
}

var prevComponent = getTopLevelWrapperInContainer(container);
if (!prevComponent) {
// Check if the node being unmounted was rendered by React, but isn't a
Expand Down Expand Up @@ -599,11 +638,7 @@ var ReactMount = {
transaction
) {
invariant(
container && (
container.nodeType === ELEMENT_NODE_TYPE ||
container.nodeType === DOC_NODE_TYPE ||
container.nodeType === DOCUMENT_FRAGMENT_NODE_TYPE
),
isValidContainer(container),
'mountComponentIntoNode(...): Target container is not valid.'
);

Expand Down
28 changes: 28 additions & 0 deletions src/renderers/dom/client/__tests__/ReactMount-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,34 @@ describe('ReactMount', function() {
);
});

it('should warn if the unmounted node was rendered by another copy of React', function() {
jest.resetModuleRegistry();
var ReactDOMOther = require('ReactDOM');
var container = document.createElement('div');

class Component extends React.Component {
render() {
return <div><div /></div>;
}
}

ReactDOM.render(<Component />, container);
// Make sure ReactDOM and ReactDOMOther are different copies
expect(ReactDOM).not.toEqual(ReactDOMOther);

spyOn(console, 'error');
ReactDOMOther.unmountComponentAtNode(container);
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toBe(
'Warning: unmountComponentAtNode(): The node you\'re attempting to unmount ' +
'was rendered by another copy of React.'
);

// Don't throw a warning if the correct React copy unmounts the node
ReactDOM.unmountComponentAtNode(container);
expect(console.error.calls.count()).toBe(1);
});

it('passes the correct callback context', function() {
var container = document.createElement('div');
var calls = 0;
Expand Down