Open In App

React JSX

Last Updated : 20 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

JSX Stands for JavaScript XML and its special syntax is used in React to make building user interfaces easier. JSX allows you to write HTML-like code directly inside JavaScript. JSX looks like regular HTML, it’s actually a syntax extension for JavaScript.

JSX Basics

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>;
  • <h1>Hello, world!</h1> is a JSX element, similar to HTML, that represents a heading tag.
  • JSX is converted into JavaScript behind the scenes, where React uses React.createElement() to turn the JSX code into actual HTML elements that the browser can understand.

Why Use JSX in React

JSX provides several advantages when working with React:

  • Declarative UI: JSX allows you to write HTML-like code directly in your JavaScript files. This makes it easier to visualize how your components will render and simplifies the UI development process.
  • Cleaner Syntax: JSX is cleaner and more concise than manually using React.createElement() for each element. It reduces the amount of boilerplate code and makes components more readable.
  • Dynamic Content: JSX makes it easy to embed dynamic content within your UI. JavaScript expressions can be placed inside {} within JSX tags, allowing for dynamic rendering of data and content.

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

  • Writing JSX: Write JSX just like HTML inside JavaScript files (React components).
const element = <h1>Hello, World!</h1>;
  • JSX Gets Transformed: JSX is not directly understood by browsers. So, it gets converted into JavaScript by a tool called Babel. After conversion, the JSX becomes equivalent to React.createElement() calls. After transformation JSX becomes
const element = React.createElement('h1', null, 'Hello, World!');
  • React Creates Elements: React takes the JavaScript code generated from JSX and uses it to create real DOM elements that the browser can render on the screen.

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:

  • Create a React App: If you don’t have a React app yet, create one using Create React App:
npx create-react-app jsx-example
cd jsx-example
npm start
  • Write JSX in the Component: In the src/App.js file, write JSX to display a message:
import React from "react";

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

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

export default App;

Output:


  • How It Works: The JSX code <h1>{message}</h1> will be transformed into JavaScript by Babel. Then react will then create a virtual DOM element for the <h1> tag with the text inside. and this virtual DOM is then used to update the actual browser DOM, displaying “Hello, JSX works!” on the screen.
  • Result: After React processes the JSX, it renders the message on the screen.

Uses of JSX

Here are some significant uses of JSX:

Embedding Expressions

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

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

In this code

  • {name} in JSX dynamically inserts the value of the name variable into the rendered output.
  • JSX allows embedding JavaScript expressions, enabling dynamic content to be rendered within the UI.

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.

const element = <img src="" alt="A description" />;
  • CamelCase for Attribute Names: In JSX, some HTML attributes are written in camelCase.
  • For example, class becomes className, for becomes htmlFor, and style is an object.
  • This is because class and for are reserved words in JavaScript.

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.

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

  • Children as Props: The Welcome component does not explicitly define the content inside it. Instead, it uses {props.children} to render any child elements that are passed between the opening and closing tags of the component when it is used.
  • Flexibility: The App component passes two child elements (<h1> and <p>) to Welcome. By using {props.children}, the Welcome component can render any child content, making it reusable with different content each time it’s used.

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.

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

The JSX code is converted into a JavaScript object

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

In this code

  • type: ‘button’: Defines the element type (button).
  • props: Contains attributes like:
  • className: ‘btn’: For styling.
  • onClick: () => alert(‘Clicked!’): Event handler for click.
  • children: [‘Click Me’]: Content inside the button.
  • React converts JSX into a JavaScript object to efficiently render and manage the UI.

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.

React JSX – FAQs

Can I use JSX without React?

JSX is specifically designed for React and requires React libraries to work.

What happens if I don’t use a key in lists?

React will warn about missing keys, and updates may behave unpredictably.

Is JSX faster than plain JavaScript?

No, JSX is transpiled to JavaScript, so there’s no inherent performance difference.

Can I use JavaScript functions in JSX?

Yes, you can call JavaScript functions or use expressions directly within {}.

What are fragments in JSX?

Fragments (<> </>) let you group elements without adding an extra DOM node.



Next Article

Similar Reads

three90RightbarBannerImg