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

Document how React treats different attributes #80

Open
bvaughn opened this issue Oct 8, 2017 · 7 comments
Open

Document how React treats different attributes #80

bvaughn opened this issue Oct 8, 2017 · 7 comments

Comments

@bvaughn
Copy link
Contributor

bvaughn commented Oct 8, 2017

This issue was originally reported by @MMeent via facebook/react/issues/9220

Please refer to the original issue for the related discussion thread.

Text of original issue:

As of #9106 there are quite a lot of tests for HTML's boolean properties on tags. According to these tests HTML properties should get rendered into empty strings or not set, e.g. <div hidden={true} /> should render to <div hidden="" /> and <div hidden={false} /> should render to <div />.

But when I look further, there are other tests which do similar things: <a href={true} /> should render to <a href="true" />. This means that if I take true as my property value, I will not know what the result will be.

The issue being: React currently does not have a clearly-defined and documented way with which they handle their props. Could the docs be updated to specify which type of prop will result in what? e.g. "any number gets parsed to a string (using Number.toString()), an object gets stringified by using arg => Object.entries(arg).map(([key, val]) => ${key}: ${val};).join(), and booleans toggle the property."

In short, these are the bugs/undocumented features/contradictions that the tests expect to happen, plus my expectations:

  • prop={true} gets parsed to prop="" or prop="true" for select properties
    expected: either prop="" or prop="prop", not both // as per checked=checked HTML standard
  • prop="" gets parsed as prop="" or
    expected: prop=""
  • prop="prop" might get parsed as either prop="prop", prop="" or even
    expected: prop="prop"
  • prop="foo" => prop="" or prop="foo"
    expected: prop="foo"
  • prop={false} => or prop="false"
    expected:
  • prop={['foo', 'bar']} => prop="" or prop="foo,bar" (hand-tested, no automatic test available)
    expected: prop="foo bar"
  • prop={{foo: 'bar', baz: 5}} => prop="" or prop="foo: bar; baz: 5;" or prop="[object Object]" (hand-tested, no automatic test available)
    expected: prop="foo: bar; baz: 5" // as per style object transform

Hacky showcase: https://jsfiddle.net/ox8a7vfe/3/

Are these features or are these bugs?

If they are bugs, and work is going to be done on the props value parsing, then the following might be a nice addition:

  • prop={{foo: true, bar: true, baz: false}} => prop="foo bar" // render all true keys, disregard false keys, like classNames.
@mrscobbler
Copy link
Contributor

It looks like this blog post: https://reactjs.org/blog/2017/09/08/dom-attributes-in-react-16.html addresses a lot of the concerns in this particular issue.

Do we want to create a new docs page that summarizes what the blog post talks about and reference the blog post?

@bvaughn
Copy link
Contributor Author

bvaughn commented Oct 13, 2017

Perhaps, or perhaps we could add to this page: https://reactjs.org/docs/dom-elements.html

To be honest, I moved this issue from the old repo over the weekend along with a lot of others. I didn't read too deeply into it.

Would you be interested in working on this @mrscobbler ?

@mrscobbler
Copy link
Contributor

Sure! I can work on it. I think adding to the existing page would be great.

@bvaughn
Copy link
Contributor Author

bvaughn commented Oct 13, 2017

This issue is all yours! 😄

I've added an "in-progress" label so that others will know not to start work on the issue. If you change your mind about the issue, no worries! Just let me know so that I can remove the label and free it up for someone else to claim.

Cheers!

@bvaughn
Copy link
Contributor Author

bvaughn commented Oct 18, 2017

Hey @mrscobbler! Just wanting to confirm that this issue is still being worked on.

No hurry of course. I'm just going through in-progress issues to see if any have been dropped. 😄

@mrscobbler
Copy link
Contributor

Yes! Still planning on working on it. I'll make sure to comment on this if for some reason I can't get to it.

@bvaughn
Copy link
Contributor Author

bvaughn commented Oct 19, 2017

Great! Thanks!

jhonmike pushed a commit to jhonmike/reactjs.org that referenced this issue Jul 1, 2020
BetterZxx pushed a commit to BetterZxx/react.dev that referenced this issue Mar 21, 2023
* fix: change the translation of mock. 原型图 ->设计稿

* fix: build error
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

2 participants