What in the Heck is JSX ?

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 = 

Hello World

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 = 

Hello {1+2}

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 = 
   
Here is a list
  • Item 1
  • Item 2
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 = 
    
    {todos.map(message =>
  • {message}
  • }
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 

  •  tag and the output is a list of the array’s items

    const html = 
       
      {[
    • finish doc
    • ,
    • submit pr
    • ]}
    Enter fullscreen mode Exit fullscreen mode

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

    • falsenullundefined and true are valid JSX, but they don’t get rendered by React onto the page.
    {false}
    {null}
    {undefined}
    {true}
    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.

    {showHeader &&
    }
    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

  • from Tumblr https://generouspiratequeen.tumblr.com/post/634668113056743424

    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