30 Mins

3. JSX Fundamentals

Understanding JSX: The Secret Engine

JSX, or JavaScript XML, is a syntax extension for JavaScript that allows you to write HTML-like structures directly inside your component files. When you write a JSX element like

Hello World

, it is not actually valid JavaScript that a browser can execute. Behind the scenes, the React compiler (such as Babel or SWC in Vite) transforms this syntax into a function call called React.createElement(). This process is known as "syntactic sugar"—a feature designed to make your code easier to read and write without changing its underlying functionality.

To start, let us look at your project file structure. After initializing a Vite project, your file tree looks like this:

Text
my-react-app/
├── node_modules/
├── src/
│   ├── App.tsx
│   └── main.tsx
├── index.html
└── package.json

Inside src/App.tsx, you write your JSX. When the build tool processes this, the function React.createElement(component, props,...children) is invoked. Understanding this is critical because it explains why you must have React in scope—or properly configured in your compiler—whenever you use JSX. If you are ever curious about what your code compiles to, you can use the official React Babel REPL to paste your code and see the raw function calls generated.

React (TSX)
// JSX Syntax
const element = <h1 className="title">Hello</h1>;

// Compiled equivalent
const element = React.createElement('h1', { className: 'title' }, 'Hello');

In this example, the first argument is the HTML tag, the second is the object containing props (attributes), and the third is the children of the element. This structure is what allows React to construct the Virtual DOM, a lightweight representation of the real DOM, enabling fast reconciliation during state changes.

Developer Note: While you rarely call React.createElement manually, knowing it exists helps you debug issues where components might fail to render due to missing React imports in older environments.

Embedding Expressions and Syntax Differences

JSX allows you to embed any valid JavaScript expression by wrapping it in curly braces {}. This is the primary way you make your UI dynamic. However, you must be careful: you cannot use statements (like if or for loops) directly inside the JSX return block. You can only use expressions that resolve to a value (like variables, function calls, or ternary operators).

Furthermore, because JSX is JavaScript, there are strict syntax differences between it and standard HTML. For example, attributes are written in camelCase. You must use className instead of class, and htmlFor instead of for, because these are reserved keywords in JavaScript.

HTML AttributeJSX Attribute
classclassName
forhtmlFor
onclickonClick
React (TSX)
export default function UserCard({ name }) {
  const isActive = true;
  return (
    <div className="card-container">
      <h1>{name.toUpperCase()}</h1>
      <label htmlFor="user-status">Status:</label>
      <p>{isActive ? "Online" : "Offline"}</p>
); }

In the code above: Line 3 uses className for styling. Line 4 uses {name.toUpperCase()}, an expression returning a string. Line 5 uses htmlFor to link the label to an input. Line 6 uses a ternary operator as a dynamic expression. If you used an if statement here, the compiler would throw an error.

Fragments and Nesting Rules

A fundamental rule of JSX is that every component must return a single top-level element. You cannot return sibling nodes side-by-side. If you have multiple elements, you must wrap them in a container, such as a

. However, using unnecessary div tags creates "div soup," which bloats your final HTML and can break CSS layouts like Flexbox or Grid.

React provides Fragments (<>...) to solve this. A Fragment allows you to group elements without adding a new node to the DOM. Additionally, remember that all tags in JSX must be explicitly closed. Self-closing tags like or
are mandatory even if they don't have closing counterparts in HTML.

React (TSX)
export default function Content() {
  return (
    <>
      <header>Site Title</header>
      <main>Content here</main>
      <img src="logo.png" alt="Logo" />
    <>
  );
}

Line 3 opens the Fragment. Line 7 closes it. The elements header, main, and img are now valid children of the component return. The rendered HTML will contain the header and main tags directly at the level where Content was called, effectively cleaning up your DOM tree.

Knowledge Check

Ready to test your understanding of 3. JSX Fundamentals?

Back