Before writing a single line of React, it helps to feel the problem React was built to solve. So this chapter has no React in it. Just the browser, a list of todos, and a bug you've probably shipped at some point.
The problem#
Here's a todo list written the way most of the web was written before React. There's an array of todos (your data), and there's HTML on the screen (the DOM). When the user does something, an event handler updates both.
Try it. Check off "Walk dog" and watch the counter.
const todos = [
{ id: 1, text: "Buy milk", done: true },
{ id: 2, text: "Walk dog", done: false },
{ id: 3, text: "Read", done: false },
];
const list = document.getElementById("list");
const count = document.getElementById("count");
// First paint: build the DOM from the data.
for (const todo of todos) {
const li = document.createElement("li");
const box = document.createElement("input");
box.type = "checkbox";
box.checked = todo.done;
box.addEventListener("change", () => onToggle(todo.id, li));
li.append(box, " " + todo.text);
if (todo.done) li.style.textDecoration = "line-through";
list.append(li);
}
count.textContent = todos.filter((t) => !t.done).length + " left";
function onToggle(id, li) {
// 1. Update the data.
const todo = todos.find((t) => t.id === id);
todo.done = !todo.done;
// 2. Update the DOM that depends on it.
li.style.textDecoration = todo.done ? "line-through" : "none";
// 3. ...and the counter? Nobody remembered.
console.log("left:", todos.filter((t) => !t.done).length);
}The console says one todo is left. The screen still says two. Nothing crashed, no error appeared. The page is just quietly wrong.
That's the whole problem in one picture. You have two copies of the truth: the data in JavaScript and the DOM on screen. Every time the data changes, some code has to go and patch every part of the DOM that depends on it. Forget one and they drift apart.
Your data says three todos, one done. The DOM shows the same thing. Two copies of one truth, in sync for now.
What the DOM actually is#
The DOM (Document Object Model) is the browser's live, in-memory tree of your page. Every tag becomes an object you can read and change from JavaScript: document.createElement makes one, appendChild attaches it, textContent and style change it. When you change the tree, the browser recalculates styles and layout and repaints the parts that changed.
Two properties of the DOM matter for everything that follows:
- It's stateful. A text input holds what the user typed. A checkbox knows if it's checked. A scrolled list remembers its scroll position. If you destroy and recreate those nodes, that state is gone.
- It's imperative. The API is a list of commands: create this, insert that, set this attribute. It has no idea what your data is. It only knows what you told it to do last.
So the DOM is a very good drawing surface with a memory. What it isn't is a model of your app.
Imperative updates don't scale#
The todo list above has one piece of data and three places that depend on it. Real screens have dozens. Think about a chat app: a new message changes the message list, the unread badge, the conversation preview in the sidebar, the browser tab title and maybe a notification. Every event handler that can produce a message has to know about all five.
The math gets worse than it looks. If your UI can be in n different states, an imperative approach makes you write code for every transition between states, not for every state. Going from "loading" to "loaded" is different code from "error" to "loaded", which is different from "loaded" to "loading more". Those transitions grow much faster than the states themselves, and each one is a chance to forget a step.
This style is called imperative: you describe how to change the screen, step by step. The alternative, which the next chapter builds, is declarative: you describe what the screen should look like for a given state and let something else work out the steps.
Fix it by hand#
Before reaching for a better idea, fix the bug the old way. It's worth feeling how fiddly it is.
The counter should always show how many todos are not done. Add the missing update to onToggle, then add a Clear done button that removes finished todos from both the data and the screen. Press Check when you think it works.
const todos = [
{ id: 1, text: "Buy milk", done: true },
{ id: 2, text: "Walk dog", done: false },
{ id: 3, text: "Read", done: false },
];
const list = document.getElementById("list");
const count = document.getElementById("count");
for (const todo of todos) {
const li = document.createElement("li");
li.dataset.id = todo.id;
const box = document.createElement("input");
box.type = "checkbox";
box.checked = todo.done;
box.addEventListener("change", () => onToggle(todo.id, li));
li.append(box, " " + todo.text);
if (todo.done) li.style.textDecoration = "line-through";
list.append(li);
}
count.textContent = todos.filter((t) => !t.done).length + " left";
function onToggle(id, li) {
const todo = todos.find((t) => t.id === id);
todo.done = !todo.done;
li.style.textDecoration = todo.done ? "line-through" : "none";
// TODO: keep the counter in sync
}
document.getElementById("clear").addEventListener("click", () => {
// TODO: remove done todos from the data AND the DOM
});Pitfalls#
Check your understanding#
Using the first todo list on this page (the one with the bug), you check "Walk dog", then check "Read". What does the counter on screen say, and what does the console's last line say?
What makes imperative UI code hard to keep correct as an app grows?
An interviewer asks: "What problem does React actually solve?" Answer in under a minute, without mentioning the virtual DOM.
Interview angle
The browser's DOM is a stateful, imperative tree. Without a framework you keep your data and the DOM in sync by hand: every event handler has to update the data and then patch every piece of the screen that depends on it. That's describing transitions between states, and transitions grow much faster than states, so screens drift out of sync. React's core idea is to describe what the UI should look like for a given state and let the library apply the changes.
What problem does React solve, in one sentence?
What's the difference between imperative and declarative UI code?
Why does it matter that the DOM is stateful?
Is hand-written DOM code ever the better choice over a declarative framework?
Recap#
- The DOM is a live, stateful, imperative tree. It's a good drawing surface, not a model of your app.
- Hand-written updates keep two copies of the truth (data and DOM) in sync by hand, inside every event handler.
- Imperative code describes transitions between states, and transitions grow much faster than states. That's where the bugs come from.
- The fix is to describe the UI for a given state and let something else apply the changes. The next chapter builds the simplest version of that.
Sources#
- MDN: Introduction to the DOM
- react.dev: Reacting to input with state, which compares the imperative and declarative styles
- Facts checked November 24, 2026.