@samitouri / QOS-React-2 / commits / db40beafbe

Show warning when componentDidUnmount is defined

Fixes #4194

oluckyman committed Jun 22, 2015 at 21:07 UTC db40beafbe04fcc53b31b52232d65cfb1638aaa7
2 files changed +35
src/renderers/shared/reconciler/ReactCompositeComponent.js
+8
@@ -257,6 +257,14 @@ var ReactCompositeComponentMixin = {
257 if (inst.componentWillUnmount) {
258 inst.componentWillUnmount();
259 }
260 + if (__DEV__) {
261 + warning(
262 + typeof inst.componentDidUnmount !== 'function',
263 + 'componentDidUnmount was defined on %s. But there is no such ' +
264 + 'lifecycle method. Use componentWillUnmount instead.',
265 + this.getName() || 'ReactCompositeComponent'
266 + );
267 + }
268
269 ReactReconciler.unmountComponent(this._renderedComponent);
270 this._renderedComponent = null;
src/renderers/shared/reconciler/__tests__/ReactCompositeComponent-test.js
+27
@@ -552,6 +552,33 @@ describe('ReactCompositeComponent', function() {
552 );
553 });
554
555 + it('should warn when defined method componentDidUnmount', function() {
556 + var container = document.createElement('div');
557 + document.body.appendChild(container);
558 +
559 + var Component = React.createClass({
560 + componentDidUnmount: function() {
561 + },
562 +
563 + render: function() {
564 + return <div />;
565 + },
566 + });
567 +
568 + var instance = <Component />;
569 +
570 + instance = React.render(instance, container);
571 + expect(console.error.calls.length).toBe(0);
572 +
573 + React.unmountComponentAtNode(container);
574 +
575 + expect(console.error.calls.length).toBe(1);
576 + expect(console.error.argsForCall[0][0]).toBe(
577 + 'Warning: componentDidUnmount was defined on Component. But there is no such ' +
578 + 'lifecycle method. Use componentWillUnmount instead.'
579 + );
580 + });
581 +
582 it('should pass context to children when not owner', function() {
583 var Parent = React.createClass({
584 render: function() {