React ignores 'for' attribute of the label element

ReactjsLabel

Reactjs Problem Overview


In React (Facebook's framework), I need to render a label element bound to a text input using the standard for attribute.

e.g. the following JSX is used:

<label for="test">Test</label>
<input type="text" id="test" />

However, this produces HTML missing the required (and standard) for attribute:

<label>Test</label>
<input type="text" id="test">

What am I doing wrong?

Reactjs Solutions


Solution 1 - Reactjs

The for attribute is called htmlFor for consistency with the DOM property API. If you're using the development build of React, you should have seen a warning in your console about this.

Solution 2 - Reactjs

Yes, for react,

for becomes htmlFor

class becomes className

etc.

see full list of how HTML attributes are changed here:

https://facebook.github.io/react/docs/dom-elements.html

Solution 3 - Reactjs

For React you must use it's per-define keywords to define html attributes.

> class -> className

is used and

> for -> htmlFor

is used, as react is case sensitive make sure you must follow small and capital as required.

Solution 4 - Reactjs

both for and class are reserved words in JavaScript this is why when it comes to HTML attributes in JSX you need to use something else, React team decided to use htmlFor and className respectively

Solution 5 - Reactjs

just using react htmlFor to replace for!

> Since for is a reserved word in JavaScript, React elements use htmlFor instead.

refs

you can find more info by following the below links.

https://facebook.github.io/react/docs/dom-elements.html#htmlfor

https://github.com/facebook/react/issues/8483

https://github.com/facebook/react/issues/1819

Solution 6 - Reactjs

That is htmlFor in JSX and class is className in JSX

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongoofballLogicView Question on Stackoverflow
Solution 1 - ReactjsSophie AlpertView Answer on Stackoverflow
Solution 2 - ReactjsDerrickView Answer on Stackoverflow
Solution 3 - ReactjsJani DevangView Answer on Stackoverflow
Solution 4 - ReactjsTrident D'GaoView Answer on Stackoverflow
Solution 5 - ReactjsxgqfrmsView Answer on Stackoverflow
Solution 6 - ReactjsDaniel NguyenView Answer on Stackoverflow