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

Make invalid prop type warnings more specific if they are falsy #11289

Closed
sebmarkbage opened this issue Oct 19, 2017 · 8 comments
Closed

Make invalid prop type warnings more specific if they are falsy #11289

sebmarkbage opened this issue Oct 19, 2017 · 8 comments

Comments

@sebmarkbage
Copy link
Collaborator

This is a common pattern:

<div onClick={condition && () => {}} className={condition && 'foo'} />

However if condition is false, 0 or "" then this correctly warns.

You should have used condition ? ... : undefined instead.

We could suggest that more specifically in the warning.

The longer term alternative is to use the proposed more concise ?? operator.

@sebmarkbage sebmarkbage changed the title Make invalid prop warnings more specific if they are falsy Make invalid prop type warnings more specific if they are falsy Oct 19, 2017
@NicBonetto
Copy link
Contributor

I would like to take this issue on :) Just need a little direction as to where to start.

@gaearon
Copy link
Collaborator

gaearon commented Oct 19, 2017

Run the code above. See the emitted warning. Find it by message in the source. Then you can tweak the message to be more helpful. Does this help?

@NicBonetto
Copy link
Contributor

Yes, thank you!

@alenkart
Copy link

alenkart commented Oct 20, 2017

Hi, I'm beginner too.

I think the warning is located on the line 5724 in the react-dom.development.js file, correct me if I'm wrong.

  if (typeof value === 'boolean') {
      warning$16(DOMProperty_1.shouldAttributeAcceptBooleanValue(name), 'Received `%s` for non-boolean attribute `%s`. If this is expected, cast ' + 'the value to a string.%s', value, name, getStackAddendum$2(debugID));
      warnedProperties$1[name] = true;
      return true;
    }

@NicBonetto
Copy link
Contributor

NicBonetto commented Oct 20, 2017

That specific file is in your local node_modules directory when you install react-dom via npm.

@gaearon
Copy link
Collaborator

gaearon commented Oct 20, 2017

Yes. If you search the source code, though, you’ll find it here:

https://github.com/facebook/react/blob/master/packages/react-dom/src/shared/ReactDOMUnknownPropertyHook.js

@NicBonetto
Copy link
Contributor

NicBonetto commented Oct 20, 2017

Proposed fix. #11300

@gaearon
Copy link
Collaborator

gaearon commented Oct 31, 2017

Fixed by #11308.

@gaearon gaearon closed this as completed Oct 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants