-
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
Allow className and for in JSX element declaration #4433
Comments
While it is true that we are writing virtual DOM, the markup is identical to HTML. Developer does not need to be aware that in DOM API |
this issue has already been raised and declined many times |
I have look through 10 pages of issues and found only this issue discussing this very problem. The latter issue did not give any arguments to support the use of |
Found: #2781
|
Copying from my Quora answer:
|
Thank you @spicyj |
@spicyj Ok, I guess object destructuring and how |
If you just want it to work, it's pretty simple to write a babel transform to convert them for you. Here is an example... var transform = new babel.Transformer('class-to-classname', {
JSXAttribute: {
exit: function (node, parent, scope, file) {
if (node.name.name === 'class') {
node.name.name = 'className';
} else if (node.name.name === 'for') {
node.name.name = 'htmlFor';
}
return node;
}
}
}) |
@cmmartin That sounds like a bad idea. That makes class in JSX work. However the prop is still |
Correct. Although, I'd argue that your developers should know that |
@cmmartin Your suggestion might make sense for DOM components but would be utterly surprising for a user-defined component if you write |
Agreed. I only explored this solution as a means to paste chunks of DOM written by designers into high level React components. I'm not so much advocating it as suggesting a possibility for those in similar situations. Aside from these special cases, I think the current solution works best. |
Maybe use https://facebook.github.io/react/html-jsx.html instead. |
Posting for future readers:
|
The JSX transform used to do that, but we don't recommend it because it's confusing if you ever try to pass |
⬆️ That's correct & very valid. Just adding some flava flave to the thread. 🕐 |
I believe there is a typo in your example @gajus . In the second bullet point of your original post you say "instead of htmlFor=bar we should be able to use foo=bar." Should that say for=bar? If I'm wrong, sorry for the distraction! It was confusing to me until I realized, though. |
@DrewWarrenTIY - the standard placeholders for things in code are, in order, "foo, bar, baz, quux, corge, grault, flarp." |
@StoneCypher - Feel free to correct me if I am wrong, but in his example in the first post of this conversation, @gajus gives us two examples of how this plugin is used to simplify writing our JSX code. Instead of having to type 'className' in our code, with this plug-in we can simply type 'class' However, in the example above, he shortens 'htmlFor' to 'foo' . I believe it is supposed to be 'for'. Thanks for your reply. |
That was a typo. For the record, I'd like to withdraw my support for this proposal. I have been working with React now for couple of years. There is hardly ever a use case for this in practice. |
Thank you @gajus I appreciate your response! |
Interestingly enough, https://github.com/insin/babel-plugin-react-html-attrs continues to be one of the first installs when we convert a project to React. Apparently adding |
from: https://facebook.github.io/react/docs/jsx-in-depth.html
<div className='foo'>
we should be able to have<div class='foo'>
.<div htmlFor='bar'>
we should be able to have<div for='bar'>
.It is true that
class
andfor
are reserved keywords in ECMAScript. However, JSX is not part of the ECMAScript. JSX is being transpiled to JavaScript. Just because JSX can be inlined with JavaScript, it should not be restricted by the same limitations. The transpiler should carry the weight of the incompatibilities.Babel
Babel transpiler is able to read
class
andfor
attributes for what they are and convert them to:Refer to: babel/babel#2039
React should support
class
andfor
element properties and not throw a warning (Warning: Unknown DOM property class. Did you mean className?
).The text was updated successfully, but these errors were encountered: