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

Blog post explains to verify prop mutations in componentWillReceiveProps #5790

Merged
merged 1 commit into from
Jan 8, 2016

Conversation

jimfb
Copy link
Contributor

@jimfb jimfb commented Jan 6, 2016

This seems to be a recurring question. People always want to know WHY we sometimes call componentWillReceiveProps even when the props haven't changed. I've seen this in several threads now.

Blog post explains.

@@ -148,6 +148,8 @@ componentWillReceiveProps: function(nextProps) {

> Note:
>
> It is **incorrect** for code executed during this lifecycle method to assume that props have changed. To understand why, read [A implies B does not imply B implies A](/react/blog/2016/01/08/A-implies-B-does-not-imply-B-implies-A.html)
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of saying what's incorrect, I would suggest to make the explanation at the top more verbose. Now it just says: "Invoked when a component is receiving new props." That this sentence is formal logic is not obvious.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I want to make this point about it being incorrect abundantly clear, since people seem to make this assumption even after they are explicitly told that there is no guarantee that props have changed.

BTW, all sentences in the entire site should be interpreted logically; people should be careful in their parsing of all documentation. We work very hard to ensure that the documentation is as precise as possible, often spending hours nitpicking the choice of only a few words. I'll deemphasize the incorrectness to avoid hurting people's feelings, but I do want to call out the fact that it's a common mistake.

@jimfb jimfb force-pushed the componentWillReceiveProps-blogPost branch from 117465a to c85c566 Compare January 6, 2016 18:39
@milesj
Copy link
Contributor

milesj commented Jan 6, 2016

+1 ;)

@jimfb jimfb force-pushed the componentWillReceiveProps-blogPost branch from c85c566 to a596efa Compare January 6, 2016 18:48
author: jimfb
---

The documentation for `componentWillReceiveProps` states that `componentWillReceiveProps` will be invoked when the props change as the result of a rerender. Some newbies assume this means "if `componentWillReceiveProps` is called, then the props must have changed", but that conclusion is logically incorrect.
Copy link
Member

Choose a reason for hiding this comment

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

Let's never say "newbies" again?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=define%3Anewbie

newbie - noun - an inexperienced newcomer to a particular activity.

Seems like the perfect word, though admittedly a little informal. But I don't feel strongly, so I'll change it :P.

@jimfb jimfb force-pushed the componentWillReceiveProps-blogPost branch 2 times, most recently from 5872c98 to 9a402eb Compare January 6, 2016 21:00
@facebook-github-bot
Copy link

@jimfb updated the pull request.

@jimfb jimfb force-pushed the componentWillReceiveProps-blogPost branch from 9a402eb to 2d08123 Compare January 6, 2016 21:12
@facebook-github-bot
Copy link

@jimfb updated the pull request.

@benjycui
Copy link
Contributor

benjycui commented Jan 7, 2016

👍

@jimfb
Copy link
Contributor Author

jimfb commented Jan 7, 2016

@zpao Can I get the green light on this one? Is it ready to go?

author: jimfb
---

The documentation for `componentWillReceiveProps` states that `componentWillReceiveProps` will be invoked when the props change as the result of a rerender. Some inexperienced users assume this means "if `componentWillReceiveProps` is called, then the props must have changed", but that conclusion is logically incorrect.
Copy link
Member

Choose a reason for hiding this comment

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

s/inexperienced users/people/

I'm sure plenty of people who have been using React for a while make that assumption as well. I don't see a good reason for qualifying who those people are.

@jimfb jimfb force-pushed the componentWillReceiveProps-blogPost branch from 2d08123 to 70938de Compare January 7, 2016 19:12
@facebook-github-bot
Copy link

@jimfb updated the pull request.

@jimfb
Copy link
Contributor Author

jimfb commented Jan 7, 2016

@zpao done.

@jimfb
Copy link
Contributor Author

jimfb commented Jan 8, 2016

As per in person with @zpao, we're good-to-go on this one.

jimfb added a commit that referenced this pull request Jan 8, 2016
Blog post explains to verify prop mutations in componentWillReceiveProps
@jimfb jimfb merged commit a158405 into facebook:master Jan 8, 2016
jimfb added a commit that referenced this pull request Jan 8, 2016
Blog post explains to verify prop mutations in componentWillReceiveProps
(cherry picked from commit a158405)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants