Part 01 / Topic 5 of 8
What JSX actually compiles to
JSX is not a feature of the React library itself - it is syntax the build tool's JSX transform rewrites into plain React.createElement() (or the newer automatic-runtime jsx()) calls before the code reaches the browser. examples/01_jsx_and_createElement.jsx writes the "same" element two ways:
import { createElement } from "react";
export function greetingViaJsx(name) {
return <p className="greeting">Namaste, {name}!</p>;
}
export function greetingViaCreateElement(name) {
return createElement("p", { className: "greeting" }, "Namaste, ", name, "!");
}
Both are plain JavaScript objects describing an element, not markup and not DOM nodes - typeof greetingViaJsx("Asha") is "object", and .type is the string "p". Rendered to a string with react-dom/server's renderToStaticMarkup, both produce exactly the same HTML (run with React 19.3.1 and Vitest 5.1.2, see examples/01_jsx_and_createElement.test.jsx):
<p class="greeting">Namaste, Asha!</p>
This is also why values inside {} are escaped automatically instead of being inserted as raw HTML - a real, executed check, not a hypothetical:
greetingViaJsx("<script>alert(1)</script>");
renders as:
<p class="greeting">Namaste, <script>alert(1)</script>!</p>
The literal <script> text became harmless, escaped characters. This is the actual reason JSX is safe against a user typing HTML into a text field: every {} value goes through this escaping, every time, with no opt-in required. (Dangerously inserting raw HTML on purpose needs a deliberately scary-named prop, dangerouslySetInnerHTML, which this course does not use.)
๐ Going deeper: the root README's "every output is executed" rule applies to this section directly - the HTML strings above were not typed from memory; they are copy-pasted from an actual
vitest runofexamples/01_jsx_and_createElement.test.jsx.
Done here? The practice set for Part 1 โ including the solutions โ is on the part page.