Introducing JSX

Introducing JSX

Consider this variable declaration:

This funny tag syntax is neither a string nor HTML.

It is called JSX, and it is a syntax extension to JavaScript. We recommend using it with React to describe what the UI should look like. JSX may remind you of a template langu age, but it comes with the full power of JavaScript.

Why JSX?

React embraces the fact that rendering logic is inherently coupled with other UI logic: how events are handled, how the state changes over time, and how the data is prepared for display.

Instead of artificially separating technologies by putting markup and logic in separate files, React separates concerns with loosely coupled units called “components” that contain both logic and markup

React doesn’t require using JSX, but most people find it helpful as a visual aid when working with UI inside the JavaScript code. It also allows React to show more useful error and warning messages.

Embedding Expressions in JSX

Whenever JavaScript constants or expressions are to be written inside JSX, they should be wrapped in curly braces.  In the example below, we declared a variable called name and then use it inside JSX by wrapping it in curly braces:

As illustrated in the above code snippet, you can put any valid JavaScript expression inside the curly braces in JSX.

In the example below, we embed the result of calling a JavaScript function, formatName(user), into an <h1> element.

After compilation, JSX expressions become regular JavaScript function calls and evaluate to JavaScript objects.

This means that you can use JSX inside of if statements and for loops, assign it to variables, accept it as arguments, and return it from functions:

Specifying Attributes with JSX

Attributes in JSX are specified in quotes.

You may also use curly braces to embed a JavaScript expression in an attribute:

Don’t put quotes around curly braces when embedding a JavaScript expression in an attribute. You should either use quotes (for string values) or curly braces (for expressions), but not both in the same attribute.


Since JSX is closer to JavaScript than to HTML, React DOM uses camelCase property naming convention instead of HTML attribute names.

For example, class becomes className in JSX, and tabindex becomes tabIndex.

Specifying Children with JSX

For every open JXS tag, there must be a corresponding closing JXS tag. If a tag is empty, you may close it immediately with />, like XML. The illustration below will clearify.

JSX tags may also contain other JSX tags as their children, as illustrated in the snippet below.

Preventing Injection Attacks with JSX

Embedding user input in JSX is completely safe, this is because by default React DOM escapes any values embedded in JSX before rendering them. Thus it ensures that you can never inject anything that’s not explicitly written in your application. Everything is converted to a string before being rendered. This helps prevent XSS (cross-site-scripting) attacks.

JSX Represents Objects

When JSX are transpiled, they compile down to React.createElement() calls. In React, these two examples are equal and identical

React.createElement() performs a few checks to help you write bug-free code but essentially it creates an object like this:

These objects are called “React elements”. You can think of them as descriptions of what you want to see on the screen. React reads these objects and uses them to construct the DOM and keep it up to date

In the next tutorial, we explore the act of rendering react elements on the DOM.


Join the discussion

  • A lot of thanks for all your work on this site. Betty delights in setting aside time for research and it is obvious why. We know all relating to the lively method you present great guidance through your web blog and in addition attract contribution from others on this area of interest and our favorite daughter is undoubtedly becoming educated a great deal. Take advantage of the rest of the new year. You’re the one carrying out a fantastic job.

  • Thanks so much for providing individuals with an extraordinarily nice opportunity to check tips from this site. It is always very brilliant plus stuffed with a lot of fun for me and my office colleagues to visit your website particularly thrice every week to read through the fresh guides you have got. Not to mention, we’re actually fulfilled with the superb solutions you give. Certain 2 tips in this article are absolutely the finest we’ve had.

  • I enjoy you because of all your work on this site. My aunt takes pleasure in going through investigation and it is easy to see why. Many of us hear all of the powerful method you deliver very important tips through the web blog and therefore increase contribution from some others on this point and our own princess is starting to learn a lot of things. Have fun with the rest of the new year. Your performing a dazzling job.

  • I simply wished to appreciate you all over again. I do not know the things I would’ve made to happen in the absence of the thoughts provided by you regarding such a concern. It was actually the depressing condition in my view, nevertheless spending time with a new well-written technique you resolved the issue made me to leap for joy. I am thankful for your advice and thus pray you comprehend what a great job you are always providing educating many people by way of a site. Most likely you haven’t got to know all of us.

  • I needed to draft you a bit of note to give many thanks again about the gorgeous suggestions you’ve shared in this case. It’s so open-handed with you to deliver easily what exactly some people might have distributed for an e-book to earn some money on their own, specifically considering the fact that you might well have tried it in case you desired. Those inspiring ideas additionally served as a fantastic way to understand that other individuals have the identical dream the same as my very own to realize whole lot more in regard to this matter. Certainly there are several more pleasurable moments up front for individuals who browse through your website.

Vivvaas’ Corner

Learn skills that matters from a community of talented developers and writers whose joy is to write in a way that you will understand.