-
Notifications
You must be signed in to change notification settings - Fork 47.1k
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
No reason to prohibit use of JS keywords in JSX? #2781
Comments
👍 |
+1 |
We are already deviating from pure HTML (it's XML, we camelCase attributes, have other enforcements). Overall we're using the JS API for attributes so We're already years deep on this commitment so we're not backing out, sorry. |
@zpao wrote:
That sounds like a self-contradiction. How can I use it if you don't support it?
Why?
This takes less than one minute to fix:
|
You can use a prop called “class” on your custom components, if you feel like it.
What would be the practical benefit of breaking “name attributes after DOM API properties” convention? |
To make it all easier to understand and parse. An HTML parser is hard and has lots of edge cases. XML-ish is much easier to parse because it has stricter rules. The other part of this is that we are trying to make it clear that this still becomes pure JS with minimal processing.
|
Since you're familiar with the internals of React (and I am not) I will just have to take your word on this and assume that there's no third-party parser that can be used to solve this problem.
Ah, I didn't understand that you were using the DOM className attribute (again, an internals thing that you would know and I wouldn't). That was the misunderstanding, and now @akiran's suggestion makes sense to me, and your response, likewise, also makes sense. The downturn in #2782 was the result of a gap in perspective ("new user" vs "knees deep in the implementation"), made worse by there seeming to be a lack of desire/effort to close that gap. The reasons why making JSX similar to HTML is challenging are not obvious from the explanations in the docs, and they weren't clear to me until your comment just now above where you said " Just some feedback. Sorry for the harsh words in the other thread, they were the result of misunderstanding. |
React uses DOM property names instead of HTML attribute names in many instances because they match semantics more closely. For example, take Of course we could also say JSX needs a CSS parser as well, but, aside from providing no benefit, this would also complicated developer's life, as generating strings is harder than settings properties on objects. So we need an object-ish In DOM, Do you see that JSX will in most cases be much closer to DOM API than to HTML? Because it's generated by JS and not some server template language, it needs to be very JS-friendly, or it would be awkward to use from within JS. That's the reason DOM API doesn't match HTML either. (DOM specification also uses So that's where JSX's “use DOM property name” convention is coming from. |
Thanks for the detailed explainer @gaearon. I feel like it would be valuable to include something explicit like that in the docs (and maybe it was there, but for whatever reason it didn't click the first time). So one of the main challenges seems to be the ability to diff the changes, as you say:
On the other hand, at least as far as Thinking it over more, I actually don't understand entirely this response that @zpao gave to @akiran:
This is probably my n00bness speaking, but that seems to be a misunderstanding of the suggestion. It's not that React components don't have or need And when not using JSX to create HTML components (as in Note I am no longer advocating this be done (since I get the feeling it may be asking a lot); at this point I am just curious. |
I see your latter point now and I agree it's doable although it would be breaking “use DOM property names” convention mentioned by @zpao. It's not all-encompassing, e.g. there is no corresponding React tries to stress that “it's all just JS” and there's no templating language so appealing to “the guts” (DOM API) as the reference point makes sense IMO. See also for some context around that decision: |
(Wow we're still on this! lol) IIRC the thing that killed this is |
The online react compiler seems to use |
I got here wondering the same thing as @taoeffect, and from the conversation I think this is a documentation issue. On http://facebook.github.io/react/docs/jsx-in-depth.html it says (emphasis mine):
In my opinion this suggests that valid JSX has to be valid JavaScript and therefore I cannot use "class". It should be changed to something like "Since JSX expressions are evaluated into DOM nodes, their attributes should follow the same naming conventions, i.e. Because that's the point, isn't it? |
So my understanding based on the docs is that
class
is prohibited as an HTML attribute in JSX, but I see no reason for this.One example given in the guide of using
className
instead ofclass
shows that the transformation results in this JS:But this is easily fixed by quoting
className
as'class'
:So, unless I'm missing something obvious, can we please have our normal HTML back? ^_^
The text was updated successfully, but these errors were encountered: