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

Warn against custom non-lowercase attributes #10699

Merged
merged 3 commits into from
Sep 13, 2017

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Sep 13, 2017

Fixes #10590.

'React does not recognize the `helloWorld` prop on a DOM element. ' +
'If you intentionally wanted it to appear in the DOM as a custom ' +
'attribute, spell it as the lowercase `helloworld` instead. ' +
'If you passed it accidentally from a parent component, remove ' +
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it’s important to mention this since people will often get this warning due to spreading.

Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like practically this is going to make the unknown DOM warning permanent (modulo some cases). Granted it's probably not what the user intends it seems a bit heavy handed.

Copy link
Contributor

Choose a reason for hiding this comment

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

or perhaps maybe just that it's going to give the impression that everything is fine when they aren't getting this warning, while foo="[Object object]" still gets passed

Copy link
Contributor

Choose a reason for hiding this comment

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

The react prop name can still have casing, as long as it's passed as lowercase to the HTML element. This doesn't allow spreading of attributes, but you can do it at least.

I think it's important to communicate what this is protecting against. I don't know that this should live in the warning, but I do agree that a developer not familiar with the problem-space could see this as an arbitrary restriction.

We could turn it into a "React's got your back" kind of thing if we effectively communicate it.

Copy link
Contributor

Choose a reason for hiding this comment

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

What about custom elements (or does that not hit here)? Chance of a false positive? If we are trying to get out of folks way about passing what they want to the DOM this feels a bit arbitrary as you say.

Copy link
Contributor

Choose a reason for hiding this comment

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

sigh didn't see the original issue, sorry for the noise :P

const e = await render(<div data-fooBar="true" />);
expect(e.getAttribute('data-fooBar')).toBe('true');
const e = await render(<div data-fooBar="true" />, 1);
expect(e.getAttribute('data-foobar')).toBe('true');
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changing these calls wasn't essential but I figured I'd emphasize they are normalized by the browser anyway.

const e = await render(<div data-fooBar={true} />);
expect(e.getAttribute('data-fooBar')).toBe('true');
const e = await render(<div data-foobar={true} />);
expect(e.getAttribute('data-foobar')).toBe('true');
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These tests were not specifically about casing so I fixed them.

var rARIA = new RegExp('^(aria)-[' + DOMProperty.ATTRIBUTE_NAME_CHAR + ']*$');
var rARIACamel = new RegExp(
'^(aria)[A-Z][' + DOMProperty.ATTRIBUTE_NAME_CHAR + ']*$',
);
Copy link
Collaborator Author

@gaearon gaearon Sep 13, 2017

Choose a reason for hiding this comment

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

Copy pasta from ARIA hook. Catches more cases which ensures we don't fire two warnings instead of one.

@gaearon gaearon added this to the 16.0 milestone Sep 13, 2017
Copy link
Contributor

@nhunzaker nhunzaker left a comment

Choose a reason for hiding this comment

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

Looks good, but I think this warning is going to require some community education. We need to broadly communicate what pitfalls we're protecting against.

@@ -701,23 +701,23 @@
## `as` (on `<div>`)
| Test Case | Flags | Result |
| --- | --- | --- |
| `as=(string)`| (changed)| `"a string"` |
| `as=(string)`| (initial)| `<empty string>` |
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is an unrelated change, and seems like Chrome version difference. Setting <link as="whatever"> doesn't return "whatever" from link.as property anymore. But it works with valid values like "audio".

@gaearon
Copy link
Collaborator Author

gaearon commented Sep 13, 2017

I agree but it’s also a bit hard to guess in advance what kind of things people would be using this for. So I’d wait until 16 and then look at these warnings again and maybe unify them more.

| `selectedIndex=(-1)`| (initial)| `<number: -1>` |
| `selectedIndex=(0)`| (initial)| `<number: -1>` |
| `selectedIndex=(integer)`| (initial)| `<number: -1>` |
| `selectedIndex=(string)`| (initial, warning)| `<number: -1>` |
Copy link
Collaborator

Choose a reason for hiding this comment

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

I love that we finally got a nice warning on these for free.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Although I guess this will just make people try selectedindex instead. 😕

@gaearon gaearon merged commit c99bad9 into facebook:master Sep 13, 2017
@gaearon gaearon deleted the unknown-warn branch September 13, 2017 17:13
@bvaughn bvaughn mentioned this pull request Sep 14, 2017
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.

5 participants