Web Devolpment

Overview of JSX: HTML with JavaScript

Written by admin

JSX, like it or not, has had a significant influence on front-end engineering. An introduction to JavaScript, an HTML-based templating language, presented code-first.

Though it feels more like writing JavaScript inside of HTML, JSX is a technique to write HTML inside of JavaScript. It’s a templating language that some people love and some people detest. Here’s an explanation of its operation and significance.

JSX templating

As a templating language for the enormously popular React framework, JSX was released. It provides you with an interface to use HTML markup to create the structure of an application view that communicates with the JavaScript context of the application. Developers tend to love or loathe this basic concept since it goes against the common wisdom of separating the view from the behavior.

Ignoring the debate around JSX in general, we may concentrate on the issue of application. Reactive templating engines like as Vue, Svelte, and others are inspired by JSX, which is the de facto standard for them. This is an example of basic JSX in a React application (see the live version):

import React from 'react';
export function App(props) {
  return (
    <div className='App'>
      <h1>Greetings from InfoWorld</h1>
      <h2>This is some JSX</h2>
    </div>
  );
}

Everything inside the <div> is merely HTML, as you can see. However, JavaScript is used to encapsulate it. The App function, a React functional component, returns HTML as its value. The return value of the function is the JSX markup.

The React render engine is essentially informed about the component’s output by the JSX return value.

JavaScript enclosed in HTML

When markup was first inlined into JavaScript, it was unusual, but these days it’s standard practice. In actuality, having JavaScript and markup combined is quite handy. Suppose we would like to add a variable to the markup. Here’s how we could go about it (see the live version):

export function App(props) {
  let [name, setName] = React.useState("User");
  return (
    <div className='App'>
      <h1>Greetings from InfoWorld</h1>
      <h2>Hello {name}</h2>
    </div>
  );
}


The “name” variable is currently being used inside of the JSX. The React.useState hook is used to generate the name variable, although any JavaScript variable that is in scope will do. (The useState hook is the proper way to use a variable in JSX when utilizing functional components.)

A JSX expression is indicated by the curly brackets in the JSX template surrounding the name. They let you refer to variables and run JavaScript expressions inside of the markup. You can refer to the variables since JavaScript operates inside the broader context of the surrounding code.

We can now see a hint of the power that has contributed to JSX’s success. All of JavaScript’s functionalities are available to you, together with imported libraries such as the React framework and a full HTML syntax that makes use of these features.

You’ll see that expressions can be used in JSX but not in complete JavaScript. The expression’s result will be output to the view at the location specified in the template. Nothing functions, such as loops, that don’t return a value. (This differs from a few other tools for templating.)

Looping
Looping is one of the most crucial things you can perform with JSX. Suppose we wish to exhibit a variety of dog breeds that are within our reach. This is how we would go (see the live version):

<div className='App'>
    <h1>Greetings from InfoWorld</h1>
    <h2></h2>
    <h3>{breeds.map((breed) => {
    return <li key={breed}>{breed}</li>;
  })}</h3>
</div>


We loop over the breeds and produce the markup for each using the map function. Finally, what we have is JavaScript within HTML/JSX inside JavaScript inside HTML/JSX inside JavaScript!

By doing away with the return statement, we might minimize the amount of code as follows:

<h3>{breeds.map((breed) => <li key={breed}>{breed}</li> )}</h3></code>

Remember that you may create loops using the other functional methods, such as filter and reduce, on the output collections. This provides you leverage when managing loops. If needed, you may also edit the data variables directly in the JavaScript of the component and then expose them to the JSX. (You can even write HTML inside of JavaScript and have it appear as JSX directly.)

See more details, please click here

About the author

admin

Leave a Comment