SkillPilotAI

JSX

JSX is the HTML-like syntax used to write React UI, but it is really JavaScript: a compiler turns every tag into a jsx() function call that returns a plain object. Knowing that explains all of its rules (one parent, className, closed tags, expressions in braces).

24.1 The problem: pasted HTML breaks

  • A signup form's HTML pasted directly inside return.
  • It has a heading, a label with class and for, and an unclosed input.
  • On save: Adjacent JSX elements must be wrapped in an enclosing tag; and that is only the first problem.
React (JSX)
function Signup() {  return (    <h1>Sign up</h1>    <label class="lbl" for="email">      Email    </label>    <input id="email" type="email">  );}
Output
$ npm run devAdjacent JSX elements must be wrapped in an enclosing tag.

24.2 What JSX really is

  • The browser does not understand JSX at all.
  • The compiler (Vite, via esbuild/Babel/SWC) converts each tag into a normal JavaScript function call.
  • That call returns a plain object called a React element; React uses it to build the real DOM.
  • JSX is JavaScript wearing an HTML costume, so JavaScript rules apply.
From JSX to the DOM
  1. 1.JSX: what we write

  2. 2.Compiler (Vite) converts it

  3. 3.jsx("h1", props) call: a normal function

  4. 4.React element: a plain object { type, props }

  5. 5.React builds / updates the real DOM

24.3 Proof: what JSX compiles into

  • jsx() gets the tag name first, then a props object.
  • className becomes a prop; the text goes into props.children.
  • Both versions log exactly the same type and props, so JSX is only a shortcut.
React (JSX)
import { jsx } from "react/jsx-runtime"; // What we writeconst a = <h1 className="title">Hello</h1>; // What the compiler turns it intoconst b = jsx("h1", {  className: "title",  children: "Hello",}); console.log(a.type, a.props);console.log(b.type, b.props);
Output
h1 { className: 'title', children: 'Hello' }h1 { className: 'title', children: 'Hello' }

24.4 Fix: the rules of JSX

  • One parent element per return, because a function returns one value. Use a Fragment <>...</> to group without an extra div in the DOM.
  • class becomes className and for becomes htmlFor (class and for are reserved words in JavaScript).
  • Every tag must be closed: <input ... />.
React (JSX)
function Signup() {  return (    <>      <h1>Sign up</h1>      <label className="lbl" htmlFor="email">        Email      </label>      <input id="email" type="email" />    </>  );}
Output
# Sign upEmail[ email input ]
HTMLJSXWhy
class="lbl"className="lbl"class is a reserved word in JS
for="email"htmlFor="email"for is a reserved word in JS
<input><input />Every tag must be closed
onclick, tabindexonClick, tabIndexAttributes are camelCase
style="font-size: 18px"style={{ fontSize: 18 }}style is a JS object
Siblings at top levelOne parent or <>...</>A function returns one value

24.5 Curly braces: JavaScript expressions

  • Inside {} you can write any JavaScript expression (code that produces a value).
  • {user.name} reads a value, {user.city.toUpperCase()} calls a method, {price * qty} does math, {items.join(", ")} builds a string.
React (JSX)
const user = { name: "Rahul", city: "Pune" };const price = 1299;const qty = 2;const items = ["Shoes", "Cap"]; function Order() {  return (    <div>      <h1>Hi, {user.name}!</h1>      <p>City: {user.city.toUpperCase()}</p>      <p>Total: ₹{price * qty}</p>      <p>Items: {items.join(", ")}</p>    </div>  );}
Output
# Hi, Rahul!City: PUNETotal: ₹2598Items: Shoes, Cap

24.6 style: an object, not a string

  • Double braces: the outer {} says an expression is coming, the inner {} is the object itself.
  • Property names are camelCase: font-size becomes fontSize.
  • A plain number gets px added automatically (fontSize: 18 means 18px).
  • In the final HTML it becomes a normal class and style string.
React (JSX)
function Badge() {  const color = "green";  return (    <p      className="badge"      style={{ color: color, fontSize: 18 }}    >      In stock    </p>  );}
Output
In stock

Checkpoint: what does each paragraph show? Without braces a + b is plain text; with braces JavaScript evaluates it.

React (JSX)
function Total() {  const a = 2;  const b = 3;  return (    <>      <p>Total: a + b</p>      <p>Total: {a + b}</p>    </>  );}
Output
Total: a + bTotal: 5

24.7 Common mistakes

  • Writing if inside braces: it does not compile because if is a statement, not a value. Put the logic above the return and use the variable in JSX (or a ternary, covered in the events module).
  • Pasting HTML as-is: fix class, for, unclosed tags and multiple top-level elements.
  • Using dangerouslySetInnerHTML with untrusted data: it bypasses React's escaping.
React (JSX)
// Wrong: Error, if is a statementfunction Greeting() {  const hour = new Date().getHours();  return (    <h1>      {if (hour < 12) { "Good morning" }}    </h1>  );}
React (JSX)
// Right: logic above, only a value in JSXfunction Greeting() {  const hour = new Date().getHours();  let text = "Good evening";  if (hour < 12) text = "Good morning";  return <h1>{text}</h1>;}

24.8 Security: values in braces are escaped

  • Myth: a user comment containing a <script> tag will run if placed in braces.
  • Reality: React escapes every value in {} so the script shows as plain text and never runs.
  • This is automatic XSS protection; only dangerouslySetInnerHTML bypasses it.

24.9 Five rules of JSX

  1. Return one parent element, or a Fragment.
  2. Close every tag, including input and img.
  3. className instead of class, htmlFor instead of for.
  4. Other attributes in camelCase too: onClick, tabIndex.
  5. Braces take only expressions, never statements.

24.10 Interview tips

  • JSX is a syntax extension for JavaScript that lets you write UI that looks like HTML.
  • At build time Babel, esbuild or SWC turns each tag into a jsx() call that returns a plain object (React element).
  • Before React 17 it compiled to React.createElement, which is why old code imported React in every file.
  • Values in {} are escaped automatically (XSS protection).
  • Braces accept expressions, not statements like if.