Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-js ignores label's 'for' attribute

I know that for 'class' we must use className, but how do i get react to preserve 'for' attribute?

The following:

<label for="recipient-name" className="control-label">Recipient:</label> 

is rendered as:

<label class="control-label">Recipient:</label> 

on an unrelated note, i find it annoying that i can not change attributes using chrome's console when using React. is there a way around that? for example if i inspect the rendered element and add the 'for' attribute manually, it disappears when i click away from that control (presumably because react re-renders the control i'm guessing)

like image 283
mike01010 Avatar asked Mar 20 '15 07:03

mike01010


People also ask

Does React support all HTML attributes?

All Supported HTML AttributesAs of React 16, any standard or custom DOM attributes are fully supported. These props work similarly to the corresponding HTML attributes, with the exception of the special cases documented above. You may also use custom attributes as long as they're fully lowercase.

What is the alternative of for attribute in React for input element?

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

How do you use attributes in React?

To add the for attribute to a label element to associate it with a form field in React, we can set the htmlFor attribute for the label. to add the htmlFor attribute to the label element. We set htmlFor to name so it'll be rendered as the label element with the for attribute set to name .

How do you give labels in React?

React has no for property for labels. It defined the htmlFor attribute. The reason is, that you are writing HTML code in JSX syntax. JSX is an extension to javascript which executes javascript code.


1 Answers

You must use htmlFor attribute instead

<label htmlFor="recipient-name" className="control-label">Recipient:</label> 
like image 193
zerkms Avatar answered Oct 27 '22 20:10

zerkms