Attributes that work differently between React and HTML
Last Updated :
01 Dec, 2023
There is a minor difference between React(JSX) and HTML attributes. Like for example, the HTML attributes like class and for are replaced with className and htmlFor in React. There are a number of attributes that work differently between React and HTML. Grammar in JSX is mostly the same as in HTML, but there are subtle differences to watch out for.
Some tags are given below:
1. className:
In HTML, it’s common to use the class as an attribute name as shown below:
<h1 class="gfg">Welcome to GeeksforGeeks</h1>
But in JSX, we can’t use the word class. We have to use className instead which is applied to all regular DOM elements like <div>, <a>, and others. This is because JSX gets translated into JavaScript, and class is a reserved word in JavaScript.
<h1 className="gfg">Welcome to GeeksforGeeks</h1>
When JSX is rendered, the JSX className attributes are automatically rendered as class attributes.
2. Self-Closing Tag:
Some HTML elements such as <img>, <br> and <input> use only one tag. The tag that belongs to a single tag that neither has an opening tag nor a closing tag, that is called self-closing tag.
When we write a self-closing tag in HTML, it is optional to use the self-closing tag. For example:
<img src=""> and <input>
But in JSX, we have to include the forward-slash.
<img src="" /> and <input />
3. htmlFor:
In HTML, we often use for attribute in <label> element which is labeled control for the label element.
<label for="username">Click me</label>
<input type="text" id="username">
But in React instead of for attribute, we can use htmlFor attribute.
4. Select Tag:
In HTML, the <select> tag creates multiple options and values. Our react-select component follows the same convention but the options and values are passed in as props. For example:
<select>
<option value="GFG">GFG</option>
<option value="OS">OS</option>
<option value="DBMS">DBMS</option>
<option selected value="Data Structure">
Data Structure
</option>
</select>
Note: Data Structure option is initially selected, because of the selected attribute. But in React instead of using a selected attribute, it uses a value attribute on the root select tag which is shown in the below example.
render() {
return (
<select value={this.state.value}
onChange={this.handleChange}>
<option value="GFG">GFG</option>
<option value="OS">OS</option>
<option value="DBMS">DBMS</option>
<option selected value="Data Structure">
Data Structure
</option>
</select>
)
}
5. textarea Tag:
In HTML <textarea> tag defines a multi-line text input control as shown below.
<textarea>Welcome to GeeksforGeeks</textarea>
But in React <textarea> uses a value attribute as shown below.
render() {
return (
<textarea value={this.state.value}
onChange={this.handleChange} />
)
}
6. dangerouslySetInnerHTML:
In HTML, we often use innerHTML either to set or return the HTML content of an element.
HTML
<!DOCTYPE html>
< html >
< body style = "text-align: center" >
< h1 style = "color:green" >GeeksforGeeks</ h1 >
< h2 >DOM innerHTML Property</ h2 >
< p id = "P" >A computer science portal for geeks.</ p >
< button onclick = "geek()" >
Try it
</ button >
< p id = "p" ></ p >
< script >
function geek() {
var x = document.getElementById("P").innerHTML;
document.getElementById("p").innerHTML = x;
document.getElementById("p").style.color = "green";
}
</ script >
</ body >
</ html >
|
But in React instead of innerHTML we can use dangerouslySetInnerHTML in the browser DOM as shown below.
function gfg() {
return { __html: 'First · Second' };
};
<div dangerouslySetInnerHTML={gfg()} />
Similar Reads
What is the difference between properties and attributes in HTML?
HTML is the standard language for creating documents that are intended to be displayed on web browsers. It is very usual to get confused with attributes and properties while performing DOM object manipulation in JavaScript. Before learning about the differences, let us first define the Document Obje
4 min read
Difference between disabled and readonly attribute in HTML
In this article, we will see the basic difference between the disabled & readonly attributes in HTML, along with understanding through the basic examples. Both disabled and readonly attributes in HTML are used to restrict user input in form fields. They only differ in how they restrict input and
3 min read
Difference between id and class Attributes in HTML
The id and class attributes in HTML are used for element identification and styling. The id attribute uniquely identifies a single element, while the class attribute can be applied to multiple elements. Both are essential for CSS styling and JavaScript manipulation. HTML id AttributeThe id attribute
3 min read
What are the differences between JSX and HTML?
JSX and HTML differ in their integration and usage within web development. HTML is a standard markup language for creating static web pages, while JSX is a syntax extension for JavaScript, mainly used with React, allowing HTML-like code within JavaScript. Additionally, JSX uses camelCase for attribu
4 min read
What's the difference between element value and data attribute ?
The element value attribute specifies the default value of the HTML element whereas the data attribute allows you to store extra data to tags in HTML when no other HTML attribute can do so. Element value: The HTML element is the collection of start and end tags with the content inserted in between t
2 min read
Difference between 'hidden' and 'aria-hidden' attributes in HTML
The HyperText Markup Language (HTML) is a powerful web development tool combined with CSS and JavaScript. Apart from these, HTML also uses Accessible Rich Internet Application (ARIA) to make web content affable for a person with disabilities. Although ARIA is beneficial, there are keywords common to
2 min read
Difference between HTML and React Event Handling
Event handling in HTML and React are different from one another in terms of syntax and some rules. The reason behind this is that React works on the concept of virtual DOM, on the other hand, the HTML has access to the Real DOM all the time. We are going to see how we can add the events in HTML and
4 min read
What are the differences between props and state ?
In React JS, the main difference between props and state is that the props are a way to pass the data or properties from one component to other components while the state is the real-time data available to use within only that component. PrerequisitesReact JSReact StatesReact Js PropsKnowing the dif
4 min read
Difference between prop() and attr() Methods in jQuery
In this article, we will learn about the differences between the prop() and the attr() in JQuery. jQuery is the fastest and lightweight JavaScript library that is used to simplify the interactions between an HTML/CSS document, or more precisely the Document Object Model (DOM), and JavaScript. JQuery
2 min read
How To Conditionally Add Attributes To React Components?
In React, it's common to render components based on certain conditions dynamically. Often, you may need to add or remove attributes to a React component based on the application's state, the props passed down, or other factors. Handling these dynamic conditions effectively is key to building flexibl
3 min read