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

Just ignore events on unmounted components #4903

Merged
merged 2 commits into from
Sep 22, 2015
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ before_install:
echo "Only docs were updated, stopping build process."
exit
fi
npm install -g npm
npm install -g npm@latest-2
script:
- |
if [ "$TEST_TYPE" = build_website ]; then
Expand Down
11 changes: 5 additions & 6 deletions src/renderers/dom/client/ReactMount.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,11 @@ function findFirstReactDOMImpl(node) {
do {
lastID = internalGetID(current);
current = current.parentNode;
invariant(
current != null,
'findFirstReactDOMImpl(...): Unexpected detached subtree found when ' +
'traversing DOM from node `%s`.',
nodeID
);
if (current == null) {
// The passed-in node has been detached from the container it was
// originally rendered into.
return null;
}
} while (lastID !== reactRootID);

if (current === containersByReactRootID[reactRootID]) {
Expand Down
70 changes: 70 additions & 0 deletions src/renderers/dom/client/__tests__/ReactEventIndependence-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails react-core
*/

'use strict';

var React;
var ReactDOM;
var ReactTestUtils;

describe('ReactEventIndependence', function() {
beforeEach(function() {
require('mock-modules').dumpCache();

React = require('React');
ReactDOM = require('ReactDOM');
ReactTestUtils = require('ReactTestUtils');
});

it('does not crash with other react inside', function() {
var clicks = 0;
var div = ReactTestUtils.renderIntoDocument(
<div
onClick={() => clicks++}
dangerouslySetInnerHTML={{
__html: '<button data-reactid=".z">click me</div>',
}}
/>
);
ReactTestUtils.SimulateNative.click(div.firstChild);
expect(clicks).toBe(1);
});

it('does not crash with other react outside', function() {
var clicks = 0;
var outer = document.createElement('div');
outer.setAttribute('data-reactid', '.z');
var inner = ReactDOM.render(
<button onClick={() => clicks++}>click me</button>,
outer
);
ReactTestUtils.SimulateNative.click(inner);
expect(clicks).toBe(1);
});

it('does not when event fired on unmounted tree', function() {
var clicks = 0;
var container = document.createElement('div');
var button = ReactDOM.render(
<button onClick={() => clicks++}>click me</button>,
container
);

// Now we unmount the component, as if caused by a non-React event handler
// for the same click we're about to simulate, like closing a layer:
ReactDOM.unmountComponentAtNode(container);
ReactTestUtils.SimulateNative.click(button);

// Since the tree is unmounted, we don't dispatch the click event.
expect(clicks).toBe(0);
});

});