question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow className and for in JSX element declaration

See original GitHub issue

Since JSX is JavaScript, identifiers such as class and for are discouraged as XML attribute names. Instead, React DOM components expect DOM property names like className and htmlFor, respectively.

from: https://facebook.github.io/react/docs/jsx-in-depth.html

  • Instead of <div className='foo'> we should be able to have <div class='foo'>.
  • Instead of <div htmlFor='bar'> we should be able to have <div for='bar'>.

It is true that class and for 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 and for attributes for what they are and convert them to:

React.createElement('div', { 'class': 'foo' });
React.createElement('div', { 'for': 'bar' });

Refer to: https://github.com/babel/babel/issues/2039

React should support class and for element properties and not throw a warning (Warning: Unknown DOM property class. Did you mean className?).

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:22 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
sophiebitscommented, Sep 28, 2015

The JSX transform used to do that, but we don’t recommend it because it’s confusing if you ever try to pass class= or for= as a prop to your own components – you wouldn’t expect to access them with a different name.

0reactions
ericclemmonscommented, Nov 4, 2016

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 .eslintrc is more divisive 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSX In Depth - React
React.createElement( 'div', {className: 'sidebar'} ). If you want to test out how some specific JSX is converted into JavaScript, you can try out...
Read more >
Passing in class names to react components - Stack Overflow
In React, when you want to pass an interpreted expression, you have to open a pair of curly braces. Try: render () {...
Read more >
Applying React Conditional classNames - Pluralsight
Depending on your preferences and needs, you can apply conditional classNames with a ternary or with a library.
Read more >
How To Create React Elements with JSX - DigitalOcean
JSX is an abstraction that allows you to write HTML-like syntax in your JavaScript code and will enable you to build React components...
Read more >
TypeScript - Emotion
When using our JSX factory, TypeScript only allows the css prop on components that accept a className prop. This is because @emotion/react resolves...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found