Show warning when componentDidUnmount is defined

Fixes #4194
This commit is contained in:
oluckyman
2015-06-22 21:07:24 +02:00
parent df05c6efb8
commit db40beafbe
2 changed files with 35 additions and 0 deletions
@@ -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;
@@ -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 <div />;
},
});
var instance = <Component />;
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() {