Why React, JSX and Components: The Page as a Function of Data
The same task card built twice: by hand with the DOM, and as a React component. See what JSX compiles to, why a component is just a function, the four JSX rules with their real compiler errors, and how createRoot puts the tree on the page.
What you will learn
- Explain why describing the page beats changing it step by step
- Read what JSX compiles to and what a React element really is
- Write a component as a function: props in, JSX out
- Follow the four JSX rules and mount an app with createRoot
Before you start
- Basic HTML and CSS
- JavaScript functions, objects and the ternary operator
A small page shows three task cards. One of them, Email Jack, owned by Olivia, has a yellow badge that says To do. Then Jack gets his email, and the badge has to turn green and say Done. How does the page find out?
There are two answers. You can change the page yourself, one step at a time. Or you can describe what the page should look like for any data, and let React work out the steps. This article builds the same card both ways, then opens up JSX and components to show what is going on inside. Every output below comes from running the lecture's project.
Two ways to change a page
Building and changing a card by hand
Here is app.js with no framework at all. addCard creates a div, an h3, a p and a span, fills each one, and hangs them on the page. markDone then changes one card later.
// Plain DOM: build and change every element by hand
const app = document.getElementById("app");
function addCard(title, owner, done) {
const card = document.createElement("div");
card.className = "card";
const h3 = document.createElement("h3");
h3.textContent = title;
const p = document.createElement("p");
p.textContent = "Owner: " + owner;
const badge = document.createElement("span");
badge.className = done ? "badge done" : "badge";
badge.textContent = done ? "Done" : "To do";
card.append(h3, p, badge);
app.append(card);
return card;
}
addCard("Write report", "Emma", true);
const jack = addCard("Email Jack", "Olivia", false);
addCard("Book flights", "Lucas", false);
console.log("before:", jack.lastChild.outerHTML);
// A change, by hand: find each piece, fix it
function markDone(card) {
const badge = card.querySelector(".badge");
badge.className = "badge done";
badge.textContent = "Done";
}
markDone(jack);
console.log("after: ", jack.lastChild.outerHTML);
The console shows the badge before and after the change:
before: <span class="badge">To do</span>
after: <span class="badge done">Done</span>
That is twelve lines of steps for one card, and we haven't changed anything yet. Notice also that we had to keep jack around, the card element itself, only so we could find it again later.
Now the trap. Delete line 27, the className line, and run it again:
BREAK IT, no className line:
before: <span class="badge">To do</span>
after: <span class="badge">Done</span>
The badge says Done but keeps the yellow To do style. The text and the style disagree, and nothing warns you. Every change by hand is a list of steps, and every step has to match what the page already shows. That is the problem React solves.
What JSX really is
React components are written in JSX, the HTML-like syntax in .tsx files. Here is a small demo file:
// What JSX makes (see it compiled: npm run compile)
import TaskCard from '../src/TaskCard'
const title = <h3 className="title">Write report</h3>
const card = (
<TaskCard title="Email Jack" owner="Olivia"
done={false} />
)
console.log('title.type: ', title.type)
console.log('title.props:', title.props)
console.log('card.type: ', card.type)
console.log('card.props: ', card.props)
The line <h3 className="title">Write report</h3> has three parts: the tag name h3 is the type, the attribute is a prop, and the text between the tags is the children.
Is this HTML? No. The browser can't read it at all. Vite compiles it first, and this is what comes out:
import { jsx as _jsx } from "react/jsx-runtime";
const title = _jsx("h3", { className: "title", children: "Write report" });
const card = _jsx(TaskCard, { title: "Email Jack", owner: "Olivia", done: false });
So JSX is a function call: jsx(type, props), with the content passed in as children. The compiler even adds the import from react/jsx-runtime for you.
The element is a plain object
What does that jsx call return? The demo prints it:
title.type: h3
title.props: { className: 'title', children: 'Write report' }
card.type: [Function: TaskCard]
card.props: { title: 'Email Jack', owner: 'Olivia', done: false }
title is an ordinary object with a type and props. We call it a React element. It is not on the page, and nothing has been drawn yet. It is only a description of an h3, and React reads it later to make the real one.
Look at card.type. For the TaskCard tag the type is not a string, it is the component function itself. That is why the capital letter matters: a capital letter means your own component, and lowercase means a plain HTML tag like h3.
A component: data in, JSX out
Here is the first component, TaskCard.tsx:
// A component: data (props) in, JSX out
type Props = {
title: string
owner: string
done: boolean
}
export default function TaskCard(props: Props) {
const label = props.done ? 'Done' : 'To do'
const badge = props.done ? 'badge done' : 'badge'
return (
<div className="card">
<h3>{props.title}</h3>
<p>Owner: {props.owner}</p>
<span className={badge}>{label}</span>
</div>
)
}
Lines 2 to 6 describe the props, the data the component takes in. Line 8 is the component, and it is just a function. It works out two small values from done, a label and a badge class, then returns a small tree of elements. Call it with done as false and then as true:
done: false -> <span class="badge">To do</span>
done: true -> <span class="badge done">Done</span>
Same function, new data, new page. And there is no step anywhere that changes the badge. You only describe it.
Components nest into a tree
To put cards on a page you write another component. App returns a heading and three cards, each with its own data:
import TaskCard from './TaskCard'
// The parent: a heading and three cards
export default function App() {
return (
<>
<h1>Team tasks</h1>
<TaskCard title="Write report" owner="Emma"
done={true} />
<TaskCard title="Email Jack" owner="Olivia"
done={false} />
<TaskCard title="Book flights" owner="Lucas"
done={false} />
</>
)
}
Compiled, App is one jsxs call with a fragment and four children:
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
return _jsxs(_Fragment, { children: [
_jsx("h1", { children: "Team tasks" }),
_jsx(TaskCard, { title: "Write report", owner: "Emma", done: true }),
_jsx(TaskCard, { title: "Email Jack", owner: "Olivia", done: false }),
_jsx(TaskCard, { title: "Book flights", owner: "Lucas", done: false })
The four JSX rules
JSX has a few rules. Each one makes sense once you remember that every tag is a function call.
Rule 1: one parent, or a fragment
// Wrong: two elements, and no parent around them
export function Header() {
return (
<h1>Team tasks</h1>
<p>3 tasks</p>
)
}
// Fixed: a fragment, <> ... </>, holds both
export function FixedHeader() {
return (
<>
<h1>Team tasks</h1>
<p>3 tasks</p>
</>
)
}
Header returns an h1 and a p side by side. That is two jsx calls, two values, and a function can return only one. Vite stops:
[PARSE_ERROR] Adjacent JSX elements must be wrapped in an enclosing tag.
Help: Did you want a JSX fragment `<>...</>`?
The fix is the fragment, the empty <> ... </>. It compiles to one call with both elements in its children, and it adds no element to the page. That is why the h1 and the three cards in App sit directly inside the root div with no extra wrapper.
Rule 2: className, not class
// Wrong: class, as we would write it in HTML
export function Badge() {
return <span class="badge">To do</span>
}
// Fixed: className, the name JSX expects
export function FixedBadge() {
return <span className="badge">To do</span>
}
JSX is JavaScript, and in JavaScript class already means something. TypeScript refuses it:
break-it/ClassName.tsx(3,16): error TS2322
Property 'class' does not exist on type 'DetailedHTMLProps<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>'.
Did you mean 'className'?
React also warns while rendering, and still writes the class:
Invalid DOM property `class`. Did you mean `className`?
class: <span class="badge">To do</span>
className: <span class="badge">To do</span>
JSX attributes are props, named like the DOM properties. className sets the DOM property of the same name, which becomes class="badge" in the HTML.
Rule 3: curly braces run an expression
// What curly braces put on the page
import * as server from 'react-dom/server'
const html = server.renderToStaticMarkup
const owner = 'Olivia'
const tasks = 3
const done = false
console.log(html(<p>Owner: {owner}</p>))
console.log(html(<p>Owner: owner</p>))
console.log(html(<p>{tasks + 2} tasks</p>))
console.log(html(<p>{done ? 'Done' : 'To do'}</p>))
console.log(html(<p>{owner.toUpperCase()}</p>))
Each line prints the HTML it produces:
<p>Owner: Olivia</p>
<p>Owner: owner</p>
<p>5 tasks</p>
<p>To do</p>
<p>OLIVIA</p>
With braces, owner becomes Olivia. Without them you get the word "owner". Inside the braces any JavaScript expression works: a variable, a sum (3 + 2 gives 5), a ternary, a method call. Anything that gives back a value.
Rule 4: close every tag
// Wrong: an <input> that is never closed
export function DoneBox() {
return (
<label>
<input type="checkbox">
Done
</label>
)
}
// Fixed: a self-closing tag, <input />
export function FixedDoneBox() {
return (
<label>
<input type="checkbox" />
Done
</label>
)
}
The compiler keeps a list of open tags. It opens label, then input, then meets </label> while the input is still open:
break-it/Closing.tsx(5,8): error TS17008
JSX element 'input' has no corresponding closing tag.
HTML lets an input stay open. JSX never does. Write <input /> and it closes itself.
Into the page with createRoot
index.html has one empty <div id="root"> and nothing else. main.tsx hands it to React:
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App'
// the one empty <div id="root"> in index.html
const el = document.getElementById('root')!
// React takes over that element ...
const root = createRoot(el)
// ... and draws the whole App tree inside it
root.render(<App />)
index.html: <div id="root"></div> -> 0 children
after render: 4 children
h1 Team tasks
div.card Write report | Owner: Emma | Done
div.card Email Jack | Owner: Olivia | To do
div.card Book flights | Owner: Lucas | To do
Before render the div has zero children. createRoot(el) tells React it owns that div from now on, and root.render(<App />) calls App, then each TaskCard, and puts four children inside: the h1 and three cards.
Common mistakes
- Writing a lowercase component name like
taskCard. React then treats it as an HTML tag, not your function. - Returning two sibling elements without a fragment.
- Copying HTML into JSX and keeping
classor an unclosed<input>. - Forgetting the braces, so the page shows the variable's name instead of its value.
- Thinking a React element is on the page. It is only an object until React renders it.
Recap
| Idea | In one line |
|---|---|
| Why React | Describe the page as a function of the data; React works out the steps |
| JSX | Compiles to jsx(type, props) calls that return plain objects |
| Component | A capitalised function: props in, JSX out |
| Tree | Components nest; the tree becomes the page |
| Rules | One parent, className, braces for expressions, closed tags |
| createRoot | Takes the empty root div and renders the App tree into it |
Try it yourself: add a fourth TaskCard to App with your own title and owner, then delete the fragment and read the error Vite gives you. Next, props and composition shows how components share data and layout.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
One Request, Two Answers: the Same Post as an HTML Page and as JSON
Ask a Django blog for the same post twice: once as a 12,731-byte HTML page built by a template, once as 2,976 bytes of JSON built by a serializer. Compare the headers, see who builds the page, and why the server still owns every write.
A Blog Design from a Prompt: Downloading Figma Make Code and Reading App.tsx and BlogCard.tsx
Make a blog design from a prompt in Figma Make, download src.zip, unzip it with Python's zipfile module, and read the two files that matter: App.tsx with its selectedPost state and BlogCard.tsx with its props.
How a Web Page Works: HTML Tags, Elements and Attributes
A web page is a text file full of labelled pieces. Learn tags, attributes and nesting with three tiny pages, and see what breaks when an attribute or a closing tag is wrong.
Semantic HTML: header, nav, main, aside and footer as Page Landmarks
The same page written as div soup and with semantic tags looks identical, but only one gives screen readers and search engines landmarks and an outline. Plus tables, div, span and the one-main rule.
