What the heck is JSX?

What the heck is JSX?

JSX is still just JavaScript with some extra functionality. JSX is a concise HTML like structure in the same file as we write the JavaScript code. With JSX, you can write code that looks very similar to HTML or XML.

React is a JavaScript library that uses a syntax called JSX this stands for JavaScript XML. It is a syntax much like XML/HTML that can co-exist with JavaScript code. This means we can write HTML like content and combine it with JavaScript.

This syntax is intended to be used by a preprocessor like Babel which converts this syntax into JavaScript that the JavaScript engine can run.

JSX is a concise HTML like structure in the same file as we write the JavaScript code. Unlike in the past, we can put HTML into JavaScript.

So let's see some code, as we’ll get a better sense of this doing that.

const html = <h1>Hello World</h1>

This looks like a cross between HTML and JavaScript. Babel is able to detect this is JSX and transform it into the following

const html = React.createElement('h1', null, "Hello World")

Babel takes this JSX code we give it and takes the tags and content and uses them as arguments for React.createElement function. Think of JSX as a shorthand way of calling this function React.createElement. The React documentation calls it ‘syntactic sugar’ for React.createElement

You can see how much easier JSX is to read, particularly when you start nesting JSX. It is not a template though! It is the syntax that has to be compiled into JavaScript.

For the purposes of the examples, we will assume that JSX gets converted, this is sometimes called rendered by React into working DOM nodes that get displayed on the page. This just reduces the complexity down of this article to focus just on JSX.

Why use JSX

JSX is not created by React, it’s an extension of the ECMAScript. You can use React without JSX but here is why most don’t.

  1. Less proficient coders can get started early and understand and modify it easily. Designers are also more likely to understand it!
  2. You leverage the power of JavaScript without having to learn a template language. But remember JSX is not a template, it’s a syntax to express tree structures of a UI component
  3. JSX promotes the idea of inline styles, which has been a shift from previous ways to develop websites

JSX Rules

  • The first part of the JSX tag determines the type of React element. We have seen this in a simple example.
  • Capitalising tags indicate that the JSX tag is referring to a React component.
  • We can evaluate JavaScript within our JSX by using curly braces
const html = <h1> Hello {1+2} </h1>

If we were to convert this and display the output HTML, the JavaScript 1+2 would evaluate and the result would be

Hello 3

javascript programming react web-development developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

How To Write Better Code As A Web Developer - React

Look at three different React code examples from a beginner, intermediate, and advanced web developer. How senior developers think. How to use React state properly. How to use React useEffect properly. What to think about when programming. The differences between senior and junior developers

How to Become A React JavaScript Developer 🚀

Today Qazi & Sonny will be showing you How To Become a React JavaScript Developer 🚀👨‍💻

Hire Dedicated React Native Developer

Have you ever thought of having your own app that runs smoothly over multiple platforms? React Native is an open-source cross-platform mobile application framework which is a great option to create mobile apps for both Android and iOS. **[Hire...

Top 10 Web Development Trends in 2020 | Web Development Skills

Top 10 Web Development Trends in 2020 will provide you with a detailed list of the trends which are currently being noticed. Upskilling to these trends will help you in landing onto better jobs in 2020-2021.