/  Technology   /  How to add a label for an attribute in react?
How to add a label for an attribute in react?

How to add a label for an attribute in react?

 

React provides web availability guidelines to create a website. It helps us to navigate and access the web page content for each and every one. React Labels are helpful to understand human-readable text for each and every  form of input control and help blind, keyboard, and mouse users.

When you are creating a form in Html web pages, You will create a label and input that bind together as seen below

<label for="name">Name</label>
<input id="name" type="text" />

Usually, the  labels is bound to input text using the for attribute and Input control id is matched with the for attribute of the label. This applies to checkboxes, radio, buttons,and select controls. if you use the same code in React JSX(JavaScript XML), It breaks during component loading and throws the below error

Invalid DOM property for. Did you mean htmlFor?`

Example:

React has no for property for labels, and Instead make use of the [htmlFor](https://reactjs.org/docs/dom-elements.html#htmlfor) attribute.

The reason is, that you are writing HTML(Hypertext Markup Language) code in JSX (JavaScript XML) syntax. JSX is an extension to javascript which executes JS(javascript) code and the  for is a keyword or reserved word in JS(javascript). So we can’t use it directly.

So we have to make use of [htmlFor](https://reactjs.org/docs/dom-elements.html#htmlfor) attribute as seen below.

Note: React is case sensitive, the first word letter is small, and the second letter work is capital Here is a react for label example components.

import React, { Component } from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
import './style.css';
interface AppProps {}
interface AppState {
  name: string;
}
class App extends Component<AppProps, AppState> {
  constructor(props) {
    super(props);
    this.state = {
      name: 'React'
    };
  }
  render() {
    return (
      <div>
        <label htmlFor="name">Name</label>
        <input id="name" type="text" />
      </div>
    );
  }
}
render(<App />, document.getElementById('root'));

Once this components is rendered, generated DOM(Document Object Model ) contains the for attribute instead of the htmlFor attribute for a label When you inspect the code, You will see the below code.

<html class=" ">
  <head>
  </head>
  <body>
    <div id="root">
      <div>
        <label for="name">Name</label>
        <input id="name" type="text">
      </div>
    </div>
</body>
</html>

How do you set labels in React?

React uses different attributes for labels.

Define label with htmlFor attribute tag as given below

 <label htmlFor="name">Name</label>

Next, use the htmlFor value to an input element tag with the id attribute

<input id="name" type="text" />

What is the label used for in React?

a label is  define a string of text for input elements. It is a standard HTML(Hypertext Markup Language) tag, that tells the user about input controls on User Interface.

EXAMPLE:

<label for="name">Name</label>

htmlFor in the label React?

htmlFor is used in place of for attribute in React, and the reason to replace for an attribute in react, is for is a keyword in JS(javascript) to iterate the loop. So you can use

 <label htmlFor="name">Name</label>

instead of

<label for="name">Name</label>

 

Leave a comment