JSX Compiles to Function Calls
What you'll learn: How JSX syntax transforms into actual JavaScript function calls before running in the browser.
The Magic Behind JSX
When you write JSX like <div>Hello</div>, it looks like HTML, but browsers don't understand it natively. Before your code runs, a build tool (like Vite) transforms it into plain JavaScript function calls.
Every JSX element becomes a React.createElement() call. This is what "syntactic sugar" means—JSX is just a friendlier way to write these function calls.
The Transformation
Here's what happens behind the scenes:
You write:
<button className="primary">Click me</button>
It compiles to:
React.createElement('button', { className: 'primary' }, 'Click me')
The pattern is: React.createElement(type, props, children).
For nested elements:
You write:
<div>
<h1>Title</h1>
<p>Text</p>
</div>
It compiles to:
React.createElement('div', null,
React.createElement('h1', null, 'Title'),
React.createElement('p', null, 'Text')
)
Why This Matters
Understanding this transformation helps you realize that:
- JSX isn't magic—it's just function calls
- Every element creates a JavaScript object describing what to render
- The build step (from your Vite setup) is essential—browsers can't run JSX directly
- These objects feed into React's reconciliation algorithm (the Virtual DOM concept you learned earlier)
Key Takeaway: JSX is syntactic sugar that compiles to React.createElement() calls during the build process, turning your HTML-like syntax into JavaScript objects React can work with.