loading...
Cover image for What in the Heck is JSX ?

What in the Heck is JSX ?

aaronsm46722627 profile image Aaron Smith ・4 min read

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.

const html = <h1>Hello World</h1> 
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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

  • 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> 
Enter fullscreen mode Exit fullscreen mode

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

Hello 3
Enter fullscreen mode Exit fullscreen mode
  • We can nest these JSX elements
const html = 
   <div> Here is a list 
      <ul> 
         <li>Item 1</li>
         <li>Item 2</li>
      </ul>
   </div>
Enter fullscreen mode Exit fullscreen mode

React will render this into a list of items!

  • You can render a list on the page using a list of JSX expressions.

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

const todos = ['finish doc','submit pr']
const html = 
    <ul>
      {todos.map(message =><li> {message}</li>}
    </ul> 
Enter fullscreen mode Exit fullscreen mode

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

const html = 
   <ul> 
     {[<li> finish doc</li>,<li>submit pr</li>]}
   </ul>
Enter fullscreen mode Exit fullscreen mode

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

  • false, null, undefined and true are valid JSX, but they don’t get rendered by React onto the page.
<div>
<div></div>
<div>{false}</div>
<div>{null}</div>
<div>{undefined}</div>
<div>{true}</div>
Enter fullscreen mode Exit fullscreen mode

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.

  • Based on conditions, we can tell React what specific JSX we want to render

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.

<div>
   {showHeader && <Header />}
   <Content />
</div>
Enter fullscreen mode Exit fullscreen mode

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. Why you should know about the Virtual DOM
  2. Why should you care about how the Browser works in React
  3. Why you should be using Fragments
  4. Why the div in React

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.

Discussion

pic
Editor guide
Collapse
florianrappl profile image
Florian Rappl

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.

Sorry that I need to be pedantic here. Its more XML like and we cannot just put HTML into JS.

For instance, the following is totally legit HTML

<ul>
  <li>1
  <li>2
  <li>3
</ul>
Enter fullscreen mode Exit fullscreen mode

however, will result in invalid JSX. There are multiple other examples where both differ significantly.

You can use React without JSX but here is why most don't.

Sure - and you can also use JSX without React. Both are totally independent. JSX can be useful for many many things - going actually beyond VDOM. Many tree structures can be very nicely described with diamond brackets.

Otherwise nice article - thanks!

Collapse
aaronsm46722627 profile image
Aaron Smith Author

Hi Florian,

Thank you for taking the time to be detail orientated with my article. These articles I'm writing are to document my learning and focus my understanding of the library and I'm glad when people like you help fill in any gaps I've missed. The beauty is that, these articles can be updated, what you're saying about it being more XML like make sense to me and will edit this in response.

It's interesting you say about JSX being useful for tree structures. You're absolutely right about this and something when I was writing this article up was thinking about the consequences of this. Have you seen any other use cases for this ?

Kind regards
Aaron

Collapse
stereobooster profile image
stereobooster

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.

It was invented by the same team which invented React. See facebook.github.io/jsx/

JSX is an XML-like syntax extension to ECMAScript without any defined semantics. It's NOT intended to be implemented by engines or browsers. It's NOT a proposal to incorporate JSX into the ECMAScript spec itself. It's intended to be used by various preprocessors (transpilers) to transform these tokens into standard ECMAScript.

Collapse
aaronsm46722627 profile image
Aaron Smith Author

Thanks stereobooster for this, dev.to is great for providing feedback. I will update the article accordingly!

Collapse
jonrandy profile image
Jon Randy

What in the Heck is JSX ?

An abomination