What is JSX in React Js?

devquora
devquora

Posted On: May 29, 2024

 

JSX is an XML/HTML-like syntax used by React that extends ECMAScript so that XML/HTML-like text can co-exist with JavaScript/React code. The syntax is intended to be used by preprocessors (i.e., transpilers like Babel) to transform HTML-like text found in JavaScript files into standard JavaScript objects that a JavaScript engine will parse.

Basically, by using JSX you can write concise HTML/XML-like structures (e.g., DOM like tree structures) in the same file as you write JavaScript code, then Babel will transform these expressions into actual JavaScript code. Unlike the past, instead of putting JavaScript into HTML, JSX allows us to put HTML into JavaScript.

By using JSX one can write the following JSX/JavaScript code:

var nav = (
    <ul id="nav">
      <li><a href="#">Home</li>
      <li><a href="#">About

<li><a href=”#”>Clients

<li><a href=”#”>Contact Us

</ul> );

And Babel will transform it into this:

var nav = React.createElement(
   "ul",
   { id: "nav" },
   React.createElement(
      "li",
      null,
      React.createElement(
         "a",
         { href: "#" },
         "Home"
      )
   ),
   React.createElement(
      "li",
      null,
      React.createElement(
         "a",
         { href: "#" },
         "About"
      )
   ),
   React.createElement(
      "li",
      null,
      React.createElement(
         "a",
         { href: "#" },
         "Clients"
      )
   ),
   React.createElement(
      "li",
      null,
      React.createElement(
         "a",
         { href: "#" },
         "Contact Us"
      )
   )
);

source:https://www.reactenlightenment.com/react-jsx/5.1.html

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    React Js Interview Questions

    What is ReactJS ?

    ReactJS, created by Facebook, is a JavaScript library for crafting dynamic, stateful, and reusable user interface components. It's renowned for simplifying the presentation layer of web and mobile app..

    React Js Interview Questions

    List some advantages of ReactJS ?

    React.js offers efficiency with its virtual DOM, simplifies JavaScript with JSX, provides developer tools, enhances SEO, and facilitates UI test cases effortlessly...

    React Js Interview Questions

    What are Components in ReactJS ?

    React components divide UI into reusable pieces, akin to JavaScript functions. They accept props and return elements for screen rendering. Below is an ES6 class-based component showcasing a welcome me..