What is JSX?
JSX looks like HTML but it’s actually JavaScript. Each JSX element is transformed into a JavaScript function call.How JSX Transforms to JavaScript
Under the hood, JSX elements are transformed intoReactElement objects using React’s JSX runtime. The modern JSX transform (introduced in React 17) uses jsx() and jsxs() functions:
- JSX
- Compiled (Modern)
- Compiled (Classic)
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 aReactElement - a plain JavaScript object that describes what you want to see on the screen:
Embedding Expressions
You can embed any JavaScript expression in JSX by wrapping it in curly braces: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:classNameinstead ofclasshtmlForinstead offoronClickinstead ofonclicktabIndexinstead oftabindex
Special Props: key and ref
The key Prop
When creating lists of elements, you should provide a uniquekey prop to help React identify which items have changed:
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:JSX Prevents Injection Attacks
React DOM escapes any values embedded in JSX before rendering them, preventing XSS (cross-site-scripting) attacks:Fragments
Fragments let you group multiple children without adding extra DOM nodes:<>...</> 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