What is JSX ?

React’s JSX markup language lets you write HTML-like syntax in JavaScript. It is essential to any markup that React will generate into your application and acts as the templating language for React elements. HTML standard is not JSX. Instead, it is a JavaScript syntactic extension that lets you generate HTML elements inside JavaScript files without using functions like appendChild() and createElement(). This hybrid method combines the full functionality of JavaScript with the familiar feel of HTML syntax.
Key Aspects of JSX
Simplifies UI Development: React makes UI creation easier by reducing the amount of React-specific expertise required, which builds on your foundational understanding of web programming. Learning about templates and controllers is not necessary because the majority of your work will be JavaScript with JSX, which combines it with conventional HTML syntax. Because of this, writing and reading React applications is much easier.
Behind the Scenes: React parses JSX before your code enters the Document Object Model (DOM). Writing JavaScript is essentially giving React syntactic sugar.calls to the createElement function. For For instance, React interprets <h1>Hello, world! ‘Hello, world!’ from JSX.constructElement(‘h1’, null This transition often uses a JavaScript compiler like Babel.
Efficiency and Performance: Application performance is enhanced by React’s usage of JSX and a virtual document object model. A representation of the real DOM stored in memory is called the Virtual DOM. React determines how many operations are necessary to update the HTML DOM based on changes in the Virtual DOM, guaranteeing that only the modified components are re-rendering quickly and effectively.
Structure and Grouping: A single enclosing tag must be used to include all of the JSX items that are returned from a function or statement. This could be an empty tag (<></>), which is also referred to as a JSX fragment, or a typical HTML element like <div>. By avoiding additional markup in the processed result, the empty tag helps you maintain a clean HTML page.
Attribute Naming Conventions:The naming conventions for attributes in JSX deviate from those in regular HTML because of JavaScript’s reserved keywords. Take, for example:
- You replace class with className.
- Use htmlFor in place of for (like in <label for=”text”>).
- CamelCase uses some of the other HTML characteristics.
Embedding JavaScript: Using curly brackets {} to enclose any JavaScript expression allows you to incorporate it directly within JSX. Variables, function calls, and even intricate reasoning fall under this.In this scenario, <h1>{1+1}</h1> yields 2.
Comments: JSX comments in tag children should be enclosed in curly brackets. For example, `/* This is a comment */}.
Iteration in JSX
You may utilise JavaScript directly in your HTML with JSX, which is particularly useful when generating dynamic content from data. In React apps, using JavaScript array functions like map() in your JSX is a standard way to present lists of elements.
Each item requires a unique property called a key when iterating over data to produce a list of React elements. The key must be a distinct piece of information that React may use to identify the elements. In order to prevent needless re-rendering of the complete list, this uniqueness aids React in effectively updating the component by recognising which items have changed, been added, or removed. Crucially, as the key is only used internally by React, it will be removed from the produced HTML.
Code Example:
import React from 'react';
const emojis = [
{ emoji: "😀", name: "grinning face" },
{ emoji: "🎉", name: "party popper" },
{ emoji: "💃", name: "woman dancing" }
];
function App() {
const handleClick = name => alert(`You clicked: ${name}`);
return (
<>
<h1 className="title">Emoji List</h1>
<label htmlFor="search">Search:</label>
<input id="search" placeholder="Type something..." />
<ul>
{emojis.map((item) => (
<li key={item.name}>
<button onClick={() => handleClick(item.name)}>
<span role="img" aria-label={item.name}>
{item.emoji}
</span>
</button>
</li>
))}
</ul>
{/* JSX comment: This is a small example of JSX features */}
<p>Total emojis: {emojis.length}</p>
</>
);
}
export default App;
Output:
Emoji List
Search:
Type something...
•😀
•🎉
•💃
Total emojis: 3
In this example:
- The emojis array contains named and character objects.
- The map() method on the emojis array is invoked using a JavaScript expression enclosed in curly brackets {} within the <ul> tag.
- The map() method returns each emoji object as a <li> (list item) element.
- The key prop is “emoji.name”. The fact that emoji.name is unique for each emoji in this list helps React monitor these components.
- The <span> element uses emoji.emoji as its content, while the aria-label and id attributes dynamically use emoji.name from the loop item.
Conclusion
In conclusion, React’s main feature, JSX (JavaScript XML), combines JavaScript’s capability with HTML’s syntax to simplify UI creation. Embed JavaScript directly into HTML streamlines dynamic, data-driven component creation. JSX is React syntactic sugar.clearer, more readable code with createElement calls. JSX improves efficiency and code maintainability with the Virtual DOM, fragment structures, and strict attribute naming requirements like className and htmlFor. JSX array techniques like map() make dynamic lists easy to render, with each member allocated a unique key for quick updates and drawing. JSX tightly integrates logic with layout in a highly readable language, making React apps more structured, interactive, and easier to develop.