Hello! Welcome to your third lesson.
In our last session, we successfully set up a complete React development environment using Vite and TypeScript. You now have a running application and are familiar with the project structure, particularly the src/App.tsx file, which will be our canvas for today.
This lesson addresses our next learning outcome: to write JSX syntax and explain how it transforms to JavaScript. We'll dive into the syntax that makes React so expressive and, given your background in computer science, we'll look under the hood to understand how this special syntax is converted into code that browsers can execute. This is where React's declarative power begins to shine.
What is JSX?
At first glance, the code inside a React component like App.tsx looks like a strange mix of HTML and JavaScript. This syntax is called JSX, which stands for JavaScript XML or JavaScript Syntax Extension. It allows us to write HTML-like structures directly within our JavaScript/TypeScript files.
While it looks like HTML, it is fundamentally JavaScript. Think of it as syntactic sugar—a more convenient and readable way to describe what the UI should look like. To understand why JSX was created and how it evolved from older methods of web development, the following video provides excellent context.
This video from Tech with Nader explains the motivation behind JSX, tracing the history from separate HTML/JS files to templating languages, and finally to the integrated approach React uses.
Watch from 00:33 to 11:31. Focus on understanding: What JSX stands for (JavaScript Syntax Extension). The problem it solves: merging markup (the 'what') and logic (the 'how') into one place. How it differs from older approaches like separate files or traditional templating languages.
The Transformation: From JSX to JavaScript
Your browser does not understand JSX. Before your code runs, it must be compiled into standard JavaScript. This compilation step is handled for you by tools like Vite, which uses a compiler (like SWC or Babel) under the hood.
So, what does JSX compile into? It transforms into a series of JavaScript function calls.
The Classic Transform: React.createElement()
For a long time, every piece of JSX was transformed into a React.createElement() function call. This function takes three main arguments:
type: The element type (e.g.,'div','h1', or another React component).props: An object containing all the attributes (e.g.,{ className: 'card' }).children: The content inside the element (e.g., text or otherReact.createElementcalls).

To explore this transformation in more detail, let's look at a text resource and another video segment.
React Internals — JSX transformation to JS and DOM
This article, 'React Internals', breaks down the React.createElement() API and the JavaScript object it produces.
Read the first two sections of the article, stopping before 'And finally, everything is rendered...'. Focus on understanding the three arguments of React.createElement() and the structure of the JSX.Element object that it returns. This object is React's internal representation of your UI element.
Now, let's see this in action. The Babel REPL is a great tool for visualizing this transformation live.
The same video from Tech with Nader demonstrates this transformation using an online compiler, showing how JSX directly becomes React.createElement calls.
Watch from 11:31 to 13:56 to see the direct JSX-to-createElement transformation. You can then jump to 32:13 and watch until 42:26, where the presenter codes a live example showing that JSX and a manual React.createElement call produce identical results. This practical demonstration solidifies the concept.
The New JSX Transform (React 17+)
With the release of React 17, this transformation was updated. While React.createElement() still works, the new JSX transform offers a more optimized approach.
Modern toolchains like Vite automatically use this new transform. Instead of injecting React.createElement(), the compiler now imports special functions from a react/jsx-runtime module (e.g., _jsx) and uses those.
The two main benefits for you as a developer are:
- You no longer need to import React at the top of every file just to use JSX.
- It can lead to slight bundle size improvements.
Since your Vite project is set up this way, it's important to understand this modern approach.
Introducing the New JSX Transform
The official React blog post, 'Introducing the New JSX Transform', is the best source for understanding this change.
Read the sections 'What’s a JSX Transform?', 'What’s Different in the New Transform?', and 'Removing Unused React Imports'. Pay close attention to the side-by-side comparison of the compiled output from the old and new transforms.
Your tsconfig.json file, which we looked at in the last lesson, has a setting that tells the TypeScript compiler which JSX transform to use. Vite configures this for you automatically.
React Typescript 2023 - 2. JSX
This short video segment connects the new JSX transform directly to the TypeScript configuration you have in your project.
Watch from 02:56 to 04:08. The video points out the jsx property in tsconfig.json and explains why react-jsx (the new transform) is the modern default.
Writing JSX: The Rules
Now that the theory is covered, let's focus on the practical syntax rules for writing JSX in your App.tsx file.
-
Return a Single Root Element: A component can only return one top-level element. If you need to return multiple elements, wrap them in a parent
<div>or, more commonly, a Fragment (<>...</>), which doesn't add an extra node to the DOM.// Correct: Wrapped in a Fragment return ( <> <h1>My App</h1> <p>Welcome!</p> </> ); // Incorrect: Two root elements // return ( // <h1>My App</h1> // <p>Welcome!</p> // ); -
Use
classNamefor CSS Classes: Sinceclassis a reserved keyword in JavaScript for creating classes, JSX usesclassNameinstead.// Correct <div className="card">...</div> // Incorrect // <div class="card">...</div> -
Use camelCase for Attributes: HTML attributes with hyphens (like
tab-indexoronclick) become camelCased in JSX (tabIndex,onClick). -
Close All Tags: In JSX, every tag must be closed. For tags that don't have content (like
<img>or<br>), you must use a self-closing tag:<img />,<br />.
The following video quickly covers some of these key differences.
Let's review the most common HTML-to-JSX attribute changes.
Watch from 13:56 to 17:20. This part explains why className is used instead of class and how other attributes like onClick are camelCased, reinforcing the rules we just discussed.
Test your understanding!
A colleague shows you the following piece of React code. It's not working. Can you spot at least three syntax errors based on the rules of JSX?
function BrokenComponent() {
return (
<h1>User Profile</h1>
<div class="profile-container">
<img src="avatar.png">
<p>This is a user profile.</p>
</div>
);
}
Show answer
Here are the three errors:
- Multiple root elements: The component returns an
<h1>and a<div>at the top level. They should be wrapped in a single root element, like a Fragment (<>...</>). classinstead ofclassName: Thedivuses the HTMLclassattribute. In JSX, this must beclassName.- Unclosed
<img>tag: The<img>tag is not self-closed. It should be<img src="avatar.png" />.
Conclusion
In this lesson, we demystified JSX. You learned that it's not magic, but rather a powerful syntactic sugar that makes writing complex UI structures in React feel as simple as writing HTML.
Here are the key takeaways:
- JSX is JavaScript: It's a syntax extension that allows us to write UI markup directly in our code.
- It Requires Compilation: Browsers don't understand JSX. It's transformed by compilers into JavaScript function calls.
- The New JSX Transform: Modern React uses a transform that automatically imports functions from
react/jsx-runtime, eliminating the need forimport React from 'react';in every component file. - Key Syntax Rules: Remember to return a single root element, use
classNameand camelCase for attributes, and close all tags.
We've focused on writing static content with JSX. In our next lesson, we'll unlock its true power by learning how to embed JavaScript expressions in JSX using curly braces. This will allow us to render dynamic data, making our components interactive and data-driven.