Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warning for componentDidReceiveProps() #11479

Merged
merged 2 commits into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/react-dom/src/__tests__/ReactCompositeComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,27 @@ 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. ' +
'Did you mean componentDidUpdate()?',
);
});

it('should warn when defaultProps was defined as an instance property', () => {
spyOn(console, 'error');

Expand Down
9 changes: 9 additions & 0 deletions packages/react-reconciler/src/ReactFiberClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,15 @@ 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. ' +
'Did you mean componentDidUpdate()?',
Copy link
Collaborator

@acdlite acdlite Nov 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also have meant componentWillReceiveProps()? Let's include both

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in most cases people would mean componentWillReceiveProps.

Maybe we should say

If you meant to update the state in response to changing props, use componentWillReceiveProps(). If you meant to perform manual DOM manipulations or fetch data, use componentDidUpdate().

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that, though will also be seen for non-DOM users.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe

If you meant to update the state in response to changing props, use componentWillReceiveProps(). If you meant to fetch data or run some code after React has updated the UI, use componentDidUpdate().

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"some code" -> "side-effects or mutations"

?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

name,
);
const noComponentWillRecieveProps =
typeof instance.componentWillRecieveProps !== 'function';
warning(
Expand Down