DEV Community

Cover image for JSX in react.js
Anil
Anil

Posted on

JSX in react.js

JSX, or JavaScript XML, is an extension of JavaScript syntax commonly used with React to describe what the UI should look like. It allows developers to write HTML-like code directly within JavaScript, making it easier to create and manipulate UI components.

In traditional JavaScript, creating UI elements dynamically involves using functions like createElement or methods like appendChild. This can lead to verbose and less readable code, especially when dealing with complex UI structures. JSX simplifies this process by allowing developers to write UI components using a syntax that closely resembles HTML.

Here's a more detailed explanation of JSX with an example:

Consider the following JSX code snippet:

import React from 'react';

const MyComponent = () => {
  return (
    <div>
      <h1>Hello, world!</h1>
      <p>This is a paragraph.</p>
    </div>
  );
};

ReactDOM.render(<MyComponent />, document.getElementById('root'));

Enter fullscreen mode Exit fullscreen mode

In this example:

  1. We import the React library, which provides the necessary functionality for using JSX.
  2. We define a functional component called MyComponent. Inside the component's body, we return JSX code.
  3. The JSX code resembles HTML markup. We have a element containing an <h1> heading and a <p> paragraph.
  4. We use ReactDOM.render() to render the MyComponent component into the HTML document. The JSX <MyComponent /> syntax is transformed into a JavaScript function call that creates an instance of the MyComponent component.
  5. When the code runs, it will render the content specified in the JSX code onto the HTML document.

    Under the hood, JSX is transpiled into JavaScript by tools like Babel before being executed by the browser. For example, the JSX code above would be transformed into something like:

    const MyComponent = () => {
      return React.createElement(
        'div',
        null,
        React.createElement('h1', null, 'Hello, world!'),
        React.createElement('p', null, 'This is a paragraph.')
      );
    };
    
    

    While JSX may look like HTML, it's important to note that it's ultimately compiled down to regular JavaScript function calls. This allows developers to leverage the power of JavaScript while writing UI components in a more declarative and intuitive manner.

    Array methods in react.js
    Fragment in react.js
    Conditional rendering in react.js
    Children component in react.js
    use of Async/Await in react.js
    Array methods in react.js
    JSX in react.js
    Event Handling in react.js
    Arrow function in react.js
    Virtual DOM in react.js
    React map() in react.js
    How to create dark mode in react.js
    How to use of Props in react.js
    Class component and Functional component
    How to use of Router in react.js
    All React hooks explain
    CSS box model
    How to make portfolio nav-bar in react.js

Top comments (0)