Ask all your coding questions @ codequery.io

JSX adds an XML-like syntax to JavaScript and makes creating ReactJS components easier. While not required, JSX adds enough benefits to be considered a standard part of React development. In this tutorial, we'll explain what JSX is, the benefits of using JSX, and examples of JSX with React.

What is JSX?

JSX is a syntax extension to JavaScript. It uses an XML-like syntax to make creating React components more elegant. JSX provides a shortcut for working with React.createElement() and describes how the UI should render a component.

Babel inherently supports JSX as a preprocessor. In fact, Babel transpiles JSX into React.createElement() API calls. If you followed the recommended environment setup then JSX is already working with your React project.

The Benefits of Using JSX

JSX provides an easier syntax for working with React elements. Here is an example of creating an element with JSX:

const element = (

<h1 className="greeting">Hello, world!</h1>

);

and without:

const element = React.createElement(

'h1',

{className: 'greeting'},

'Hello, world!'

);

Notice how the first example resembles HTML more than the React.createElement() API call.

JSX also improves development time. It optimizes code at compile while also catching any errors. This makes the resulting JavaScript run faster (or as fast as) vanilla JS.

JSX also forces safer coding. Before rendering elements, JSX converts everything to strings to escape user inputs and XSS vulnerabilities.

Using JSX with React

Since JSX compiles to React.createElement() calls, it depends directly on the ReactJS library. This means React must always be in scope when using JSX:

import React from 'react';



class App extends React.Component {

render() {

return (

<div>

<h1 className='title'>Hello React! </h1>

<p>Check out this paragraph.</p>

</div>

);

}

}



export default App;



Notice how we first import the React library. We then use JSX to render both an <h1> and a <p>. Remember that elements must be wrapped by a parent element when there are more than one. In this case, <div> is used to wrap the child elements.

Using JavaScript expressions with JSX

JSX can evaluate inline JavaScript expressions. For example:

import React from 'react';



let name = 'Sam'



class App extends React.Component {

render() {

return (

<div>

<h1 className='title'>Hello {name}! </h1>

<p>Check out this paragraph. </p>

</div>

);

}

}

export default App;

Notice how the <h1> tag evaluates the {name} expression inline.

While largely resembling HTML, JSX has some subtle differences. For example, className is used instead of class for class attributes. Likewise tabIndex is used instead of tabindex.

You can still add custom attributes with the data- prefix just like regular HTML.

Using CSS with JSX

You can easily apply CSS rules and style elements with JSX:

import React from 'react';



class App extends React.Component {

render() {



let titleText = {

fontSize: 100,

color: '#c8c8c8'

}



return (

<div>

<h1 style = {titleText} className='title'>Hello React! </h1>

<p>Check out this paragraph.</p>

</div>

);

}

}



export default App;

Notice how we reference the CSS properties as a JavaScript object titleText.

Conclusion

JSX is similar to HTML with a few subtle (yet key) differences. It provides a preprocessing step to catch errors and optimize code at compile time. JSX also adds syntactic sugar to creating React elements. For these reasons, it is highly recommended that JSX is used with your ReactJS project.