What in the heck is JSX ?

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> 

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’ forReact.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 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

  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

  1. The first part of the JSX tag determines the type of React element. We have seen this in the simple example.
  2. Capitalising tags indicate that the JSX tag is referring to a React component.
  3. 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 the result would be

Hello 3

4. We can nest these JSX elements

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!

5. 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> 

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 this items

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.

6. 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>

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.

7. 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>

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 of 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!

Until next time!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s