Skip to content

Change ReactPropTypes invariant's to console.warn. #912

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

Closed
wants to merge 2 commits into from

Conversation

cpojer
Copy link
Contributor

@cpojer cpojer commented Jan 16, 2014

This changes it so that PropTypes logs to the console via console.warn instead of throwing through an invariant. This means that PropType-validation can be stripped out in production to potentially speed up React apps. Since the code only logs to the console from now on, the logic that is followed is the same in prod and dev and actual errors will come up the same way.

Currently this is gated by DEV but we might want to add another magical constant.

90 % of the time making this was to fix all the unit tests to expect console.warn calls instead of expecting the functions to throw.

@vjeux
Copy link
Contributor

vjeux commented Jan 16, 2014

I really like this change!

cc @yungsters @jordwalke @petehunt @sebmarkbage

@cpojer
Copy link
Contributor Author

cpojer commented Jan 16, 2014

Just realized ReactErrorUtils is not a good place for this, how about an 'error' module that lives next to invariant?

@sebmarkbage
Copy link
Collaborator

This should use console.warn and probably be called something like warn. I spent a day once trying to figure out why an error wasn't interrupting code flow because it looks like it's thrown.

A rename to a short stand-alone name would be good. It's really nice to be able to statically analyze these.

@cpojer
Copy link
Contributor Author

cpojer commented Jan 16, 2014

I had this as 'warn' before but decided error is better. Warnings in the console don't really cry for much attention and if something goes wrong with your PropTypes I think it is better to tell you about it in RED.

@cpojer
Copy link
Contributor Author

cpojer commented Jan 16, 2014

Changed to an error module.

@yungsters
Copy link
Contributor

Since the console methods already support string formatting, why do we have to re-implement it?

@cpojer
Copy link
Contributor Author

cpojer commented Jan 16, 2014

@yungsters we wan't to be able to analyze this module and maybe strip it out in prod.

if (condition) console.warn(…); is harder to analyze than warning(condition, …)

@cpojer
Copy link
Contributor Author

cpojer commented Jan 16, 2014

discussed with @sebmarkbage in person, we decided to call this module warning.

@sebmarkbage
Copy link
Collaborator

@cpojer we don't need to do the string parsing since console.warn supports %s in the same way.

console.warn(format, ...args);

@cpojer
Copy link
Contributor Author

cpojer commented Jan 17, 2014

Hah, I never knew about this.

@sophiebits
Copy link
Collaborator

Even in old IE?

@sebmarkbage
Copy link
Collaborator

shim it :)

It's cool because it uses toString so we get custom formatting. But the inspector could turn that part of the string into a link to an object that can be inspected.

@cpojer cpojer closed this Jan 21, 2014
@cpojer cpojer deleted the proptypes-invariant-to-log branch January 21, 2014 22:06
@petehunt
Copy link
Contributor

yessssss

@yungsters
Copy link
Contributor

Wait, what? Why was this merged?

@plievone
Copy link
Contributor

You may have already considered this, but weakInvariant or warnInvariant or something would preserve the semantics, now the condition may get reversed if read/written quickly.

Also in ReactPropTypes.js s/shouldThrow/shouldWarn/ ?

@cpojer
Copy link
Contributor Author

cpojer commented Jan 22, 2014

@sebmarkbage, @jeffmo and I only came up with softInvariant which we didn't like. I really like weakInvariant! @sebmarkbage what do you say?

@zpao
Copy link
Member

zpao commented Jan 22, 2014

Please don't call something an invariant if you're not treating it as such.

[...] an invariant is a condition that can be relied upon to be true during execution of a program, or during some portion of it. It is a logical assertion that is held to always be true during a certain phase of execution. – Wikipedia

@plievone
Copy link
Contributor

@zpao It is a strange situation to be in if propTypes are not relied to be valid, problems will ensue? The validity is just checked via other means (console.warn) than throwing immediately. I just pointed out that as of now the condition to warning may get easily reversed, as in comment #912 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants