Skip to content

Conversation

@danielschonfeld
Copy link
Contributor

This commit should handle the issue raised in #1391

@syranide
Copy link
Contributor

Way too strict in my opinion, simply warning on undefined solves @spicyj's case without having to booleanize every value (which could likely lead to just ignoring the warning because it's far too common).

@danielschonfeld
Copy link
Contributor Author

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.

@syranide
Copy link
Contributor

That makes logical, but not practical, sense to me. Most things you pass around as booleans in JavaScript are not actually booleans (|| and && does not cast to booleans).

@petehunt
Copy link
Contributor

Yeah I think only checking for undefined makes the most sense here.

Copy link
Member

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.

Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will do

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks. :)

@danielschonfeld
Copy link
Contributor Author

let me know if you guys want to take it in a different direction, otherwise here's everything changed as discussed.
cc @zpao @syranide @spicyj

Copy link
Collaborator

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?

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 thought it was referenced before that we want to remove the DEV question...?

Copy link
Collaborator

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.

@sophiebits
Copy link
Collaborator

Looks pretty good, a few inline comments -- thanks for working on this. :)

@danielschonfeld
Copy link
Contributor Author

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.

Copy link
Collaborator

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.

Copy link
Contributor Author

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

Copy link
Collaborator

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.

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@syranide
Copy link
Contributor

AFAIK, you can remove if (__DEV__) {, console.warn is automatically stripped in production builds, meaning it'll get removed anyway. @spicyj @petehunt invariant or console.warn? invariant kind of makes sense to me (but I don't really care).

I also think we settled on using value === undefined for testing for undefined, if undefined is overwritten all hope is lost anyway.

@sophiebits
Copy link
Collaborator

No, console.warn isn't stripped in __DEV__ currently (see vendor/constants.js). Let's leave the check dev-only (and we don't have dev-only invariants because we want the dev and prod behavior to match exactly).

@syranide
Copy link
Contributor

@spicyj Hmm? I'm pretty sure console.warn is stripped in production-builds, which means the __DEV__ check should be superflous or did I misunderstand you?

Also my point about invariant, I'm pondering if we want to aggressively forbid it (seems like a bad practice to rely on not explicitly returning). Especially as it's not intuitive whether no return means true or false.

@zpao
Copy link
Member

zpao commented May 19, 2014

No, leave it as is. console.warn isn't stripped. We'll do it like this for now, and maybe be more aggressive if we see it become a larger issue.

zpao added a commit that referenced this pull request Jun 18, 2014
…oolean Warn if shouldComponentUpdate returns anything other than true/false
@zpao zpao merged commit 92e3384 into facebook:master Jun 18, 2014
@zpao
Copy link
Member

zpao commented Jun 18, 2014

Thanks and sorry for the delay!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

6 participants