Skip to main content
JSX is a syntax extension for JavaScript that lets you write HTML-like markup inside JavaScript files. While not required for React, JSX makes component code more readable and expressive.

What is JSX?

JSX looks like HTML but it’s actually JavaScript. Each JSX element is transformed into a JavaScript function call.
This JSX is transformed by a compiler (like Babel) into:

How JSX Transforms to JavaScript

Under the hood, JSX elements are transformed into ReactElement objects using React’s JSX runtime. The modern JSX transform (introduced in React 17) uses jsx() and jsxs() functions:
The modern JSX transform (using jsx() and jsxs()) was introduced in React 17 and has several advantages:
  • You don’t need to import React to use JSX
  • Better performance in some cases
  • Future compatibility improvements

The ReactElement Structure

When JSX is compiled, it creates a ReactElement - a plain JavaScript object that describes what you want to see on the screen:
React elements are immutable. Once created, you can’t change their props or children. To update the UI, you create a new element and pass it to the renderer.

Embedding Expressions

You can embed any JavaScript expression in JSX by wrapping it in curly braces:
You can use any valid JavaScript expression:

JSX Attributes

You can specify attributes using either quotes for string literals or curly braces for expressions:

Attribute Naming

Since JSX is JavaScript, attribute names use camelCase:
  • className instead of class
  • htmlFor instead of for
  • onClick instead of onclick
  • tabIndex instead of tabindex

Special Props: key and ref

The key Prop

When creating lists of elements, you should provide a unique key prop to help React identify which items have changed:
The key prop is special and is not passed to your component as a prop. If you need the same value in your component, pass it explicitly with a different name:

The ref Prop

In React 19+, ref is now a regular prop (though it’s still special in how React handles it):

JSX Represents Objects

Because JSX compiles to function calls, it can be used anywhere JavaScript expressions can be used:

JSX Children

JSX tags can contain children:
Children can be:

JSX Prevents Injection Attacks

React DOM escapes any values embedded in JSX before rendering them, preventing XSS (cross-site-scripting) attacks:
Everything in JSX is converted to a string before being rendered, so you can never inject anything that’s not explicitly written in your application.

Fragments

Fragments let you group multiple children without adding extra DOM nodes:
The short syntax <>...</> is equivalent to <React.Fragment>...</React.Fragment>. Use the full syntax when you need to pass a key:

Next Steps

Props and State

Learn how to pass data and manage state

Rendering

Understand how React renders elements to the DOM