Add warning for componentDidReceiveProps() (#11479)

* Add warning for componentDidReceiveProps()

* Adjust message for componentDidReceiveProps() warning
This commit is contained in:
Tom authored and Dan Abramov committed 2017-11-07 14:00:31 +00:00
1 parent acb268c577
commit 48012ef839
2 files changed
+34

No files matched your search

@@ -533,6 +533,29 @@ describe('ReactCompositeComponent', () => {
);
});
it('should warn when componentDidReceiveProps method is defined', () => {
spyOn(console, 'error');
class Component extends React.Component {
componentDidReceiveProps = () => {};
render() {
return <div />;
}
}
ReactTestUtils.renderIntoDocument(<Component />);
expectDev(console.error.calls.count()).toBe(1);
expectDev(console.error.calls.argsFor(0)[0]).toBe(
'Warning: Component has a method called ' +
'componentDidReceiveProps(). But there is no such lifecycle method. ' +
'If you meant to update the state in response to changing props, ' +
'use componentWillReceiveProps(). If you meant to fetch data or ' +
'run side-effects or mutations after React has updated the UI, use componentDidUpdate().',
);
});
it('should warn when defaultProps was defined as an instance property', () => {
spyOn(console, 'error');
@@ -281,6 +281,17 @@ export default function(
'Did you mean componentWillUnmount()?',
name,
);
const noComponentDidReceiveProps =
typeof instance.componentDidReceiveProps !== 'function';
warning(
noComponentDidReceiveProps,
'%s has a method called ' +
'componentDidReceiveProps(). But there is no such lifecycle method. ' +
'If you meant to update the state in response to changing props, ' +
'use componentWillReceiveProps(). If you meant to fetch data or ' +
'run side-effects or mutations after React has updated the UI, use componentDidUpdate().',
name,
);
const noComponentWillRecieveProps =
typeof instance.componentWillRecieveProps !== 'function';
warning(