A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/reactjs/reactjs-jsx-introduction/ below:

React JSX - GeeksforGeeks

React JSX

Last Updated : 04 Apr, 2025

JSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, it’s actually a syntax extension for JavaScript.

What is JSX?

JSX combines HTML and JavaScript in a single syntax, allowing you to create UI components in React. It simplifies rendering dynamic content by embedding JavaScript expressions inside HTML-like tags.

Syntax:

const element = <h1>Hello, world!</h1>;
How JSX Works

When React processes this JSX code, it converts it into JavaScript using Babel. This JavaScript code then creates real HTML elements in the browser’s DOM . which is how your web page gets displayed.

JSX Transformation Process
const element = <h1>Hello, World!</h1>;
const element = React.createElement('h1', null, 'Hello, World!');
How to Implement JSX in Action

JSX can be implemented in a React project to create dynamic and interactive UI components. Here are the steps to use JSX in a React application:

npx create-react-app jsx-example
cd jsx-example
npm start
JavaScript
import React from "react";

function App() {
    const message = "Hello, JSX works!";

    
    return <h1>{message}</h1>;
}

export default App;

Output:

Uses of JSX

Here are some significant uses of JSX:

1. Embedding Expressions

JSX allows you to embed JavaScript expressions directly within the HTML-like syntax. You can use curly braces {} to insert JavaScript expressions.

JavaScript
const name = 'Jonny';
const greeting = <h1>Hello, {name}!</h1>;

In this code

2. Using Attributes in JSX

In JSX, attributes are specified similarly to HTML, but with some differences. Since JavaScript is used alongside JSX, certain attribute names are written in camelCase instead of the lowercase syntax used in HTML.

JavaScript
const element = <img src="" alt="A description" />;
3. Passing Children in JSX

In JSX, components or elements can accept children just like HTML elements. Children are nested elements or content that are passed into a component. This allows for flexible and reusable components.

JavaScript
const Welcome = (props) => {
  return <div>{props.children}</div>;
};

const App = () => {
  return (
    <Welcome>
      <h1>Hello, World!</h1>
      <p>Welcome to React.</p>
    </Welcome>
  );
};

In this code

4. JSX Represents Objects

JSX is not directly rendered as HTML by React; instead, it gets compiled into JavaScript objects representing virtual DOM elements. These objects are later used by React to efficiently update the real DOM.

JavaScript
const element = React.createElement(
  "button",
  {
    className: "btn",
    onClick: () => alert("Clicked!"),
  },
  "Click Me"
);

The JSX code is converted into a JavaScript object

C++
{
  type: 'button',                 
  props: {                        
    className: 'btn',             
    onClick: () => alert('Clicked!'), 
    children: ['Click Me']        
  }
}

In this code

Why Use JSX in React

JSX provides several advantages when working with React:

Conclusion

JSX is a powerful feature of React that allows you to write HTML-like code within JavaScript. It simplifies the process of building user interfaces by combining the flexibility of JavaScript with the structure of HTML. JSX makes it easier to render dynamic content, embed expressions, and manage event handling. React efficiently converts JSX into JavaScript objects, which are used to update the real DOM.


JSX Attributes and styling in React

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4