- Notifications
You must be signed in to change notification settings - Fork 49.8k
Warn if shouldComponentUpdate returns anything other than true/false #1513
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
Warn if shouldComponentUpdate returns anything other than true/false #1513
Conversation
| Way too strict in my opinion, simply warning on |
| I'm open to feedback, I can change the logic to only check for undefined. I was just under the assumption that shouldComponentUpdate should really only return a true/false answer and hence answer the question. |
| That makes logical, but not practical, sense to me. Most things you pass around as booleans in JavaScript are not actually booleans ( |
| Yeah I think only checking for undefined makes the most sense here. |
src/core/ReactCompositeComponent.js Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This moves code that was try/catch outside so we might end up in some failure cases we didn't previously experience. That said, it's probably ok.
But you can drop the surrounding parens here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zpao Considering it calls this.shouldComponentUpdate(nextProps, nextState, nextContext)); it's probably not a good idea to leave it outside.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Meh, we don't try/catch any other user-specified lifecycle functions so at least we'll be consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's because we don't have anything to put in a finally block. Here _compositeLifeCycleState will be left at RECEIVING_STATE if shouldComponentUpdate throws which is bad.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so are we good leaving it outside the try/catch? I'll remove the parens.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like it to be in the try/finally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. :)
src/core/ReactCompositeComponent.js Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a if (__DEV__ && here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it was referenced before that we want to remove the DEV question...?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All warnings are shown only in DEV. The warning() helper checks __DEV__ for you but when using console.warn directly you need to check it by hand.
Side note: someone else will probably come along and ask you to change this to typeof shouldUpdate === "undefined" because in theory you could redefine undefined.
| Looks pretty good, a few inline comments -- thanks for working on this. :) |
| Alright, let's try it again :) @spicyj - no problem! I think the React style of thinking is the future way front end should be done, I'm excited to be working on this. |
src/core/ReactCompositeComponent.js Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I didn't mean to get rid of the mention of undefined in the message -- sorry! What do you think of:
(this.constructor.displayName || 'ReactCompositeComponent') + '.shouldComponentUpdate(): Returned undefined instead of a ' + 'boolean value. Make sure to return true or false.' (And FB style guide says single quotes.)
Otherwise I think this looks great now. Do you mind squashing your commits into one (using git rebase -i)? You'll need to force-push to your branch but GitHub will update the PR automatically. After that I'll let @zpao double-check and merge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me and no problem on the rebase... I wasn't sure how you guys work this and if you guys wanted to do the rebase a moment before merging. Will do both
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either way; usually I just amend and push unless the incremental changeset is particularly useful to look at on its own.
| Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
| AFAIK, you can remove I also think we settled on using |
| No, console.warn isn't stripped in |
| @spicyj Hmm? I'm pretty sure Also my point about |
| No, leave it as is. |
…oolean Warn if shouldComponentUpdate returns anything other than true/false
| Thanks and sorry for the delay! |
This commit should handle the issue raised in #1391