What in the Heck is JSX ?
Tags: javascript, react
Published: 2020-11-11 20:19:17

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 lets see some code, as we’ll get a better sense of this doing that.

```javascript
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

```javascript
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 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

* <b>The first part of the JSX tag determines the type of React element. We have seen this in a simple example.</b>

* <b>Capitalising tags indicate that the JSX tag is referring to a React component.</b>

* <b>We can evaluate JavaScript within our JSX by using curly braces </b>

```javascript
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

```javascript
Hello 3
```

* <b>We can nest these JSX elements</b>

```javascript
const html = 
   <div> Here is a list 
      <ul> 
         <li>Item 1</li>
         <li>Item 2</li>
      </ul>
   </div>
```

React will render this into a list of items!

* <b>You can render a list on the page using a list of JSX expressions.</b>

This is more complicated, don’t worry if you don’t get this.

```javascript
const todos = ['finish doc','submit pr']
const html = 
    <ul>
      {todos.map(message =><li> {message}</li>}
    </ul> 
```
If we give this JSX to react, if evaluates this JavaScript within the curly brackets. In this case we use the map function to create an array of JSX. We take the todos array items and wrap a `<li>` tag and the output is a list of the array's items

```javascript
const html = 
   <ul> 
     {[<li> finish doc</li>,<li>submit pr</li>]}
   </ul>
```

Then JavaScript interprets the JavaScript in the curly brackets and renders the bullet pointed array items we created.

* <b> `false`, `null`, `undefined` and `true` are valid JSX, but they don’t get rendered by React onto the page. </b>

```javascript
<div>
<div></div>
<div>{false}</div>
<div>{null}</div>
<div>{undefined}</div>
<div>{true}</div>
```

Beware, some falsy values DO get rendered. 0 for example still gets rendered.

The fact that they are valid JSX, and they do not get rendered to anything on the page means we can create situations where we can conditionally render certain JSX.

* <b> Based on conditions, we can tell React what specific JSX we want to render</b>

For the moment, assume that if a tag with Capitalised First letter name a /> is a React component, don’t worry about knowing exactly if you’re unfamiliar with it. React builds from elements up to components as it becomes more complex, and they can be written in JSX like so below.

```javascript
<div>
   {showHeader && <Header />}
   <Content />
</div>
```

Here we want to display the header component if showHeader variable is true. If showHeader was false, the Header component would not be seen on the screen!

This is not the end to JSX. But to understand how to properly use it and how it properly fits into the React code, we have to understand a few other concepts. Like how does React turn this JSX into something on the page.

The `ReactDOM.render()` function which converts all our JSX eventually into DOM nodes. We also have to understand what components are and how to create React components. Lastly to fully utilise JSX we need to understand the concept of props. Prop stands for properties and it is React’s way to pass Data down into components. This is incredibly useful and we will get to that!

## Other Articles by Author

1. <a href="https://dev.to/aaronsm46722627/why-you-should-know-about-the-virtual-dom-48bc"> Why you should know about the Virtual DOM </a>
2. <a href="https://dev.to/aaronsm46722627/why-should-you-care-about-how-the-browsers-work-in-react-13mn"> Why should you care about how the Browser works in React </a>
3. <a href="https://dev.to/aaronsm46722627/why-you-should-be-using-fragments-o5n"> Why you should be using Fragments </a>
4. <a href="https://dev.to/aaronsm46722627/why-the-div-in-react-1cfk"> Why the div in React </a> 

##About the Author
I’m a practising medical physician and educationalist as well as a web developer. Please see here for further details about what I’m up to project-wise on my blog and other posts. If you want to get in contact with me, please do so here
aaron.smith.07@aberdeen.ac.uk or on Twitter @aaronsmithdev.