ReactJS: What is JSX - An Overview

Mar 11, 2023ยท

4 min read

ReactJS: What is JSX - An Overview
Play this article

Introduction

JSX (JavaScript XML) is an extension to the JavaScript language syntax that allows developers to write HTML-like code in JavaScript. JSX is commonly used in React, a popular JavaScript library for building user interfaces.

With JSX, we can write code that looks like a combination of HTML and JavaScript. For example, instead of using vanilla JavaScript to create a new HTML element, we can use JSX syntax to create a new element with properties and attributes.

A basic example of JSX code can be:-

const element = <h1>Hello, World</h1>;

This code creates a new h1 element with the text 'Hello, World' and stores it in the element variable.

When a React application is built, the JSX code is transformed into regular JavaScript code that can be executed by the browser. This process is typically done using a tool like Babel, which compiles the JSX code into JavaScript.

Fundamentally, JSX just provides syntactic sugar for the React.createElement(component, props, ...children)function.

Therefore if we don't want to use JSX then we can create React element with the help of React.createElement. Thus, the JSX code:-

<MyButton color="blue" shadowSize={2}>
  Click Me
</MyButton>

compiles into:-

React.createElement(
  MyButton,
  {color: 'blue', shadowSize: 2},
  'Click Me'
)

We can see that if we have a very nested element then creating an element with just JavaScript can be very tedious work and hard to read in comparison to JSX code which is like HTML. Due to this JSX is a common choice among developers. But in the end, it all comes down to the Developer's preference of either using JSX or not.

An Example of JSX

In this example, we will see how to create a basic form component with the help of JSX.

const Form = () => {  
  const heading = 'Form Example';
  return (
    <form className="form">
      <h4>{heading}</h4>
      <div className="form-row">
        <label htmlFor="name" className="form-label">
          name
        </label>
        <input
          type="text"
          id="name"
          className="form-input"
          value={name}
        />
      </div>
      <div className="form-row">
        <label htmlFor="email" className="form-label">
          email
        </label>
        <input
          type="email"
          className="form-input"
          value={email}
        />
      </div>
      <button type="submit" className="btn btn-block">
        submit
      </button>
    </form>
  );
};

export default Form;

By adding some basic CSS styling on the above code we get the output of this form as:-

So we can see here that by using JSX it is so easy to create components just like HTML. Javascript code can be added in the JSX within the curly braces {}. We can add further functionality to this form and use the data according to our use case.

Why use JSX?

  • Expressiveness: JSX allows developers to write HTML-like code in JavaScript, which makes it easier to express UI components and their relationships with each other. This can make code more readable and maintainable.

  • Performance: Because JSX is compiled into plain JavaScript, it can be optimized by the JavaScript engine to run faster than equivalent code written in other templating languages.

  • Familiarity: Many developers are already familiar with HTML and CSS, and JSX syntax resembles HTML syntax. This can make it easier for developers to learn and adopt React.

  • Integration with JavaScript: JSX is integrated with JavaScript, so developers can use JavaScript expressions inside JSX tags to create dynamic UI components.

  • Tooling support: JSX is widely used in the React ecosystem, so there are many tools and libraries available to support it, such as code editors and linters.

  • Type checking: Because JSX is integrated with JavaScript, it can be statically type-checked using tools like TypeScript or Flow. This can help catch errors at compile time and improve code quality.

  • Code reusability: With JSX, developers can create reusable components that can be used across multiple pages or applications. This can save development time and improve code maintainability.

  • Server-side rendering: React applications can be rendered on the server using tools like Next.js or Gatsby. Because JSX code can be compiled to regular JavaScript, it can be used in both the client and server-side rendering contexts.

  • Conditional rendering: JSX supports conditional rendering of UI components using JavaScript expressions. This can make it easier to create dynamic UI components that respond to user interactions or data changes.

  • Community support: JSX is widely used in the React community, which means that there are many resources available to developers who want to learn how to use it effectively. This includes documentation, tutorials, and open-source projects.

Conclusion

We can say that JSX comes with a lot of features and it makes it easier for Developers to create reusable components in React with easy-to-read and understand code. Overall JSX is a very powerful tool for building React Projects and we developers use it mostly for all our projects.

Thank You and Happy Coding ๐Ÿ˜€.

Did you find this article valuable?

Support Kushal Karan by becoming a sponsor. Any amount is appreciated!

ย