From db40beafbe04fcc53b31b52232d65cfb1638aaa7 Mon Sep 17 00:00:00 2001 From: oluckyman Date: Mon, 22 Jun 2015 21:07:24 +0200 Subject: [PATCH] Show warning when componentDidUnmount is defined Fixes #4194 --- .../reconciler/ReactCompositeComponent.js | 8 ++++++ .../__tests__/ReactCompositeComponent-test.js | 27 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/renderers/shared/reconciler/ReactCompositeComponent.js b/src/renderers/shared/reconciler/ReactCompositeComponent.js index f1c84438eb..ad2c53aeb7 100644 --- a/src/renderers/shared/reconciler/ReactCompositeComponent.js +++ b/src/renderers/shared/reconciler/ReactCompositeComponent.js @@ -257,6 +257,14 @@ var ReactCompositeComponentMixin = { if (inst.componentWillUnmount) { inst.componentWillUnmount(); } + if (__DEV__) { + warning( + typeof inst.componentDidUnmount !== 'function', + 'componentDidUnmount was defined on %s. But there is no such ' + + 'lifecycle method. Use componentWillUnmount instead.', + this.getName() || 'ReactCompositeComponent' + ); + } ReactReconciler.unmountComponent(this._renderedComponent); this._renderedComponent = null; diff --git a/src/renderers/shared/reconciler/__tests__/ReactCompositeComponent-test.js b/src/renderers/shared/reconciler/__tests__/ReactCompositeComponent-test.js index 15a56b60da..cc70e59ba5 100644 --- a/src/renderers/shared/reconciler/__tests__/ReactCompositeComponent-test.js +++ b/src/renderers/shared/reconciler/__tests__/ReactCompositeComponent-test.js @@ -552,6 +552,33 @@ describe('ReactCompositeComponent', function() { ); }); + it('should warn when defined method componentDidUnmount', function() { + var container = document.createElement('div'); + document.body.appendChild(container); + + var Component = React.createClass({ + componentDidUnmount: function() { + }, + + render: function() { + return
; + }, + }); + + var instance = ; + + instance = React.render(instance, container); + expect(console.error.calls.length).toBe(0); + + React.unmountComponentAtNode(container); + + expect(console.error.calls.length).toBe(1); + expect(console.error.argsForCall[0][0]).toBe( + 'Warning: componentDidUnmount was defined on Component. But there is no such ' + + 'lifecycle method. Use componentWillUnmount instead.' + ); + }); + it('should pass context to children when not owner', function() { var Parent = React.createClass({ render: function() {