What React actually does
Understand what React does every time your data changes. One small inbox, built three ways: updating the page by hand (and forgetting a place), redrawing everything with innerHTML (and losing what you typed), and React, which describes the whole screen and changes only what differs. Along the way: what a component is, what JSX turns into, what the virtual DOM really is, what React DOM does, and why state exists.
In this lesson
Components, JSX, the virtual DOM and state are all parts of React’s fix for one kind of bug. Here it is in a small inbox. You have two unread messages and you open one. The bell drops to one, and so does the heading, but the sidebar still says two. Nothing crashed and the data is right. One part of the screen just never heard the news.
To see what React does differently, this lesson fixes that inbox twice in plain JavaScript. Both fixes break, and the second one deletes what the user is typing. React doesn’t, and once you can say why, you can explain React.
Updating by hand
Save this as inbox.html and open it in a browser. The page shows the inbox, and the script opens Maya’s message:
<header>Mail <span class="badge">2</span></header>
<nav>Inbox <span class="count">2</span></nav>
<main>
<h2>2 unread</h2>
<ul>
<li id="msg-1" class="unread">Maya: Lunch on Friday?</li>
<li id="msg-2" class="unread">Sam: The deploy is done</li>
<li id="msg-3">Priya: Notes from today</li>
</ul>
</main>
<script>
const messages = [
{ id: 1, from: "Maya", subject: "Lunch on Friday?", read: false },
{ id: 2, from: "Sam", subject: "The deploy is done", read: false },
{ id: 3, from: "Priya", subject: "Notes from today", read: true },
];
const unreadCount = () => messages.filter((m) => !m.read).length;
function open(id) {
const message = messages.find((m) => m.id === id);
message.read = true;
document.querySelector(`#msg-${id}`).classList.remove("unread");
document.querySelector(".badge").textContent = unreadCount();
document.querySelector("h2").textContent = `${unreadCount()} unread`;
}
open(1);
console.log(unreadCount()); // 1
console.log(document.querySelector(".count").textContent); // 2
</script>
The first line of open marks the message as read. That’s the data, and it’s correct. Everything after it is instructions for the screen: remove the dot, update the bell, update the heading. The sidebar has no line, so it keeps saying 2. Add one and the bug is gone, for now.
Each of those lines changes the DOM, the objects the browser keeps for every element on the page. Step-by-step instructions like these are called imperative code, and they hide a trap: every change is two jobs. You change the data, then you track down every place on screen that shows it. A new message arriving needs the same updates, and so does a “Mark all as read” button. Three events and four places on screen make twelve updates to get right. Miss one and the badge is wrong.
Redraw everything
So stop updating piece by piece. Write one function, render, that builds the whole inbox from the data as a string of HTML and hands it to innerHTML. Call it after every change:
<div id="app"></div>
<script>
const messages = [
{ id: 1, from: "Maya", subject: "Lunch on Friday?", read: true },
{ id: 2, from: "Sam", subject: "The deploy is done", read: false },
{ id: 3, from: "Priya", subject: "Notes from today", read: true },
];
const app = document.querySelector("#app");
function render() {
const unread = messages.filter((m) => !m.read).length;
app.innerHTML = `
<header>Mail <span class="badge">${unread}</span></header>
<nav>Inbox <span class="count">${unread}</span></nav>
<main>
<h2>${unread} unread</h2>
<ul>${messages.map((m) => `<li class="${m.read ? "" : "unread"}">${m.from}: ${m.subject}</li>`).join("")}</ul>
<textarea id="reply" placeholder="Reply"></textarea>
</main>`;
}
function receive(message) {
messages.unshift(message);
render();
}
render();
const reply = document.querySelector("#reply");
reply.value = "Sounds good, see you at";
receive({ id: 4, from: "Leo", subject: "Quick question", read: false });
console.log(document.querySelector(".count").textContent); // 2
console.log(document.querySelector("#reply") === reply); // false
console.log(document.querySelector("#reply").value === ""); // true
</script>
Now nothing can be forgotten, because every number comes from the same data: the bell, the sidebar and the heading all say 2. But someone was typing a reply, a new message arrived, and the reply is gone. innerHTML threw away every element and built new ones, including the text box and the cursor in it. A scrolled list or a playing video would reset the same way.
So we want two things at once: code that describes the whole screen from the data, and a page where only the parts that changed get touched.
Components and state
React gives you both. In React, the inbox is a function, called a component. It takes the data and returns what the screen should look like. To run it, make a React project with Vite (npm create vite@latest, then React and JavaScript; the next lesson walks through it) and replace src/App.jsx with this:
import { useState } from "react";
const startingMessages = [
{ id: 1, from: "Maya", subject: "Lunch on Friday?", read: false },
{ id: 2, from: "Sam", subject: "The deploy is done", read: false },
{ id: 3, from: "Priya", subject: "Notes from today", read: true },
];
export default function App() {
const [messages, setMessages] = useState(startingMessages);
const unread = messages.filter((m) => !m.read).length;
function open(id) {
setMessages(messages.map((m) => (m.id === id ? { ...m, read: true } : m)));
}
return (
<>
<header>Mail <span className="badge">{unread}</span></header>
<nav>Inbox <span className="count">{unread}</span></nav>
<main>
<h2>{unread} unread</h2>
{unread === 0 && <p className="caught-up">You're all caught up</p>}
<ul>
{messages.map((m) => (
<li key={m.id} className={m.read ? "" : "unread"} onClick={() => open(m.id)}>
{m.from}: {m.subject}
</li>
))}
</ul>
<textarea id="reply" placeholder="Reply" />
</main>
</>
);
}
Look at what’s missing. Nothing finds an element and nothing changes its text. The unread count is written into all three places. Code that describes the result instead of the steps is called declarative. In a bigger app, the bell, the sidebar and the list would each be a component of their own, used like HTML tags.
But one function that describes the whole screen is the redraw idea again, and the redraw lost the reply. So React owes two answers: how does it know the data changed, and why doesn’t it wipe the reply?
The first answer is state. The messages live in state, which is data React keeps for a component from one call to the next. useState sets it up and hands back two things: the current list, and a function that replaces it.
Opening a message builds a new list where that message is read and passes it to setMessages. That’s the whole handler, with no bell and no sidebar in it. Calling setMessages is how React finds out the data changed. React then calls the component again, and this time useState hands back the new list. Calling a component is called rendering.
Skip setMessages and change the list directly, and React never finds out:
function open(id) {
messages.find((m) => m.id === id).read = true; // the data changes, the page doesn't
}
The page stays as it was, and the badge is wrong again.
One rule
In React, the sidebar’s number comes from the same unread as the bell, so it can’t fall behind. And a “You’re all caught up” message once nothing is unread is one line in the description:
{unread === 0 && <p className="caught-up">You're all caught up</p>}
Done by hand, that would be a new line in every handler. That’s the rule behind good React code: keep the data in one place, work everything else out from it, and let React keep the page in step.
What JSX turns into
Now the harder answer. React calls your component after every change, just like the redraw. So why is the reply safe?
The answer is in what the component returns. It looks like HTML inside JavaScript. That’s JSX, and no browser can run it. Before your code reaches the browser, a build tool rewrites every tag as an ordinary function call. This span:
<span className="badge">{unread}</span>
becomes a call to a function named jsx, with the tag’s name, its class and its content. Curly braces in JSX mean “this part is JavaScript”, so the count goes in as a value:
import { jsx } from "react/jsx-runtime";
jsx("span", { className: "badge", children: unread });
Run that call and you don’t get an element. You get a small object. In the Vite project, save this as badge.mjs and run node badge.mjs:
import { jsx } from "react/jsx-runtime";
const unread = 2;
const badge = jsx("span", { className: "badge", children: unread });
console.log(badge.type); // span
console.log(badge.props); // { className: 'badge', children: 2 }
console.log(typeof badge); // object
So JSX is a short way to write objects that describe the screen. A whole component returns a tree of them, one for every tag. That tree is what people call the virtual DOM. It isn’t a second page. It’s plain JavaScript objects, which React can create and compare without touching the page.
Render and commit
Back to the situation that broke the redraw: someone is typing a reply, and a new message arrives. React renders: it calls your component and gets a new tree. It kept the previous tree, so it compares the two. Three numbers went from 1 to 2, and there’s one new row in the list. The text box is the same in both trees, so React leaves it alone.
Turning those differences into real changes is the job of a separate package, React DOM. React itself only knows components and descriptions; React DOM is the part that knows browsers. It builds the real elements the first time, and after that it applies only what changed. That step is called the commit.
Measured in a browser, the redraw rebuilt every element in the inbox. React’s commit made four changes: the text of the bell, the sidebar and the heading, and one new list item. The reply was still there, and so was the cursor.
When React is worth it
React isn’t free. The browser has to download it, and comparing trees is extra work, so it won’t beat careful hand-written updates for speed. What you pay for is that tracking every change becomes React’s job instead of yours. For a page with one button, plain JavaScript is fine. React pays off when many parts of a screen depend on the same data and that data keeps changing: an inbox, a dashboard, a checkout.
What React does
You describe the screen for the data you have. When the data changes, React renders a new description, compares it with the last one, and commits only the difference. Components, JSX, the virtual DOM and state are all parts of that one idea, and it’s the answer when an interviewer asks what happens when state changes.
Video transcript
What React actually does
Anyone can write React. Few can explain what it actually does. By the end of this video, you'll be one of them. Components, JSX, the virtual DOM, state: they're all pieces of React's fix for a bug like this one. You have two unread messages. You open one. The bell drops to one. So does the heading. But the sidebar still says two. Nothing crashed. The data is right. One part of the screen never heard the news. Now the same inbox in React. Same click and all three numbers move together. To see what React does differently, we're going to fix that first inbox ourselves, twice, in plain JavaScript. Both fixes break. The second one deletes what your user is typing. React doesn't, and once you can say why, you can explain React.
Updating by hand
Here's the code that runs when you open a message. First it marks the message as read. That's the data, and it's correct. Everything after it is instructions for the screen. Remove the dot. Update the bell. Update the heading. And the sidebar? Nobody wrote that line. Add it and the bug is gone. For now. Each of these lines changes the DOM, the objects the browser keeps for every element on the page. Step-by-step instructions like these are called imperative code. And they hide a trap: every change is two jobs. Change the data, then track down every place on screen that shows it. A new message arriving needs the same updates. So does a 'Mark all as read' button. Three events and four places on screen make twelve updates to get right. Miss one and the badge lies.
Redraw everything
So stop updating piece by piece. Write one function, render, that builds the whole inbox from the data as a string of HTML, and hand that string to innerHTML. Call render after every change. Now nothing can be forgotten, because every number comes from the same data. It works. Until someone starts typing a reply. A new message arrives, and the reply is gone. innerHTML threw away every element and built new ones, including the text box and the cursor in it. A scrolled list or a playing video would reset the same way. So now we want two things at once: code that describes the whole screen from the data, and a page where only the parts that changed get touched.
Components
React gives you both. In React, the inbox is a function, called a component. It takes the data and returns what the screen should look like. Look at what's missing. Nothing in it finds an element. Nothing changes its text. The unread count is simply written into all three places. Code that describes the result instead of the steps is called declarative. In a bigger app, the bell, the sidebar and the list would each be their own component, used like HTML tags. But one function that describes the whole screen is the redraw idea again, and the redraw lost the reply. So React owes us two answers. How does it know the data changed? And why doesn't it wipe the reply?
State
First answer: state. The messages live in state, which is data React keeps for a component from one call to the next. useState sets it up and hands back two things: the current list, and a function that replaces it. Now look at opening a message. It builds a new list where that message is read, and passes it to setMessages. That's the whole handler. No bell. No sidebar. Calling setMessages is how React finds out the data changed. So it calls the component again, and this time useState hands back the new list. Calling a component is called rendering. Skip setMessages and change the list directly, and React never finds out. The page stays as it was, and the badge lies again.
One rule
Remember the sidebar from the start, stuck on two? In React, its number comes from the same unread count as the bell, so it can't fall behind. Want a "You're all caught up" message once nothing is unread? Done by hand, that's a new line in every handler. In React, it's one line in the description. That's the rule behind good React code. Keep the data in one place. Work out everything else from it. Let React keep the page in step.
What JSX turns into
One answer down. Now the harder one. The redraw ran after every change and wiped the text box. React calls your component after every change too. So why is the reply safe? The answer is in what the component returns. It looks like HTML sitting inside JavaScript. That's JSX, and no browser can run it. Before your code reaches the browser, a build tool rewrites every tag as an ordinary function call. This span becomes a call to a function named jsx, with the tag's name, its class and its content. Curly braces in JSX mean "this part is JavaScript", so the count goes in as a value. Now run that call. You don't get an element. You get a small object: type span, class name badge, children two. So JSX is a short way to write objects that describe the screen. A whole component returns a tree of them, one for every tag. That tree is what people call the virtual DOM. It isn't a second page. It's plain JavaScript objects, which React can create and compare without touching the page.
Render and commit
Time to test it, in the exact situation that broke the redraw. Someone is typing a reply, and a new message arrives. React renders: it calls your component and gets a new tree. It kept the previous one, so it compares the two. Three numbers went from one to two. There's one new row in the list. The text box is the same in both trees, so React leaves it alone. Turning those differences into real changes is the job of a separate package, React DOM. React itself only knows components and descriptions. React DOM is the part that knows browsers. In a phone app, React Native does that job instead. React DOM builds the real elements the first time, and after that it applies only what changed. That step is called the commit. Now compare. The redraw rebuilt every element on the page. React's commit makes four changes. The reply is still there. So is the cursor.
When React is worth it
React isn't free, though. The browser has to download it, and comparing trees is extra work, so it won't beat careful hand-written updates for speed. What you're paying for is that tracking every change becomes React's job instead of yours. For a page with one button, plain JavaScript is fine. React pays off when many parts of a screen depend on the same data and that data keeps changing: an inbox, a dashboard, a checkout.
Now you can explain it
So, what does React actually do? You describe the screen for the data you have. When the data changes, React renders a new description, compares it with the last one, and commits only the difference. A component, JSX, the virtual DOM and state are all parts of that one idea. It's how you fix a bug without guessing, and it's what you say when an interviewer asks what happens when state changes. Anyone can write React. Now you can explain it. This was the first video of a full React course, and the next step is on screen right now.

