@samitouri / QOS-React-2 / commits / 9965b6b9dd
Fork 0

The library for web and native user interfaces.

Fix Listener Cleanup on Unmount

We need to make sure that deleteAllListeners() is invoked before we call the superclass's unmountComponent() method or else we will lose this._rootNodeID. I also added an invariant and unit test to make sure we do not break this in the future.

CommitSyncScript committed Jun 4, 2013 at 12:52 UTC 9965b6b9dda2716e3618e4cb1acbf83ce29e3c2f
2 files changed +23 -1
src/core/ReactNativeComponent.js
+1 -1
@@ -317,9 +317,9 @@ ReactNativeComponent.Mixin = {
317 * @internal
318 */
319 unmountComponent: function() {
320 + ReactEvent.deleteAllListeners(this._rootNodeID);
321 ReactComponent.Mixin.unmountComponent.call(this);
322 this.unmountMultiChild();
322 - ReactEvent.deleteAllListeners(this._rootNodeID);
323 }
324
325 };
src/core/__tests__/ReactNativeComponent-test.js
+22
@@ -239,4 +239,26 @@ describe('ReactNativeComponent', function() {
239 });
240 });
241
242 + describe('unmountComponent', function() {
243 + it("should clean up listeners", function() {
244 + var React = require('React');
245 + var ReactEvent = require('ReactEvent');
246 +
247 + var container = document.createElement('div');
248 + document.documentElement.appendChild(container);
249 +
250 + var callback = function() {};
251 + var instance = <div onClick={callback} />;
252 + React.renderComponent(instance, container);
253 +
254 + var rootNode = instance.getDOMNode();
255 + var rootNodeID = rootNode.id;
256 + expect(ReactEvent.getListener(rootNodeID, 'onClick')).toBe(callback);
257 +
258 + React.unmountAndReleaseReactRootNode(container);
259 +
260 + expect(ReactEvent.getListener(rootNodeID, 'onClick')).toBe(undefined);
261 + });
262 + });
263 +
264 });