Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 11, 2020 08:19 pm GMT

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 well 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, its 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, dont worry if you dont 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 dont 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, dont worry about knowing exactly if youre 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 Reacts 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

Im a practising medical physician and educationalist as well as a web developer. Please see here for further details about what Im up to project-wise on my blog and other posts. If you want to get in contact with me, please do so here
[email protected] or on Twitter @aaronsmithdev.


Original Link: https://dev.to/aaronsm46722627/what-in-the-heck-is-jsx-58a3

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To