Lesson 1 of 15
Why React Exists The Declarative Revolution
Introduces the core motivation behind React by contrasting imperative DOM manipulation with the declarative model where UI is expressed as a pure function of state.
Context / Motivation
Before React, building user interfaces in JavaScript meant manually manipulating the DOM. You wrote instructions for what to do, not descriptions of what you wanted.
For example, when a user clicked a button, you’d find the element, update its text, maybe attach or remove listeners, and keep the UI in sync with the data by hand.
This imperative model worked fine at small scale. But as applications grew — multiple sources of state, re-rendered views, nested updates — the DOM became a shared mutable structure with no clear owner. The smallest change could cause unpredictable bugs.
Let’s see why.
The Imperative Pain
Imagine a counter app — a simple one.
<div id="root">
<p>Count: 0</p>
<button>Increment</button>
</div>
<script>
const countEl = document.querySelector("p");
const btn = document.querySelector("button");
let count = 0;
btn.addEventListener("click", () => {
count++;
countEl.textContent = `Count: ${count}`;
});
</script>This works fine. But now add:
- A reset button,
- A total sum in another part of the page,
- A conditional color when the count is even.
Suddenly, every change in state must be manually reflected in multiple places.
You are directly telling the browser how to change, line by line.
If someone else modifies the DOM or resets the state elsewhere, there’s no central truth — the DOM is the truth, but it’s mutable and fragile.
You spend most of your effort managing UI consistency, not the problem itself.
The Declarative Idea
React’s core insight was to invert control.
Instead of telling the browser how to mutate the DOM, you simply describe what the UI should look like given the current state.
In declarative style, the counter becomes:
function Counter() {
const [count, setCount] = React.useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
ReactDOM.createRoot(document.getElementById("root")).render(<Counter />);Now, we no longer mutate the DOM manually.
We only say:
“When count is 0, render this. When it’s 1, render that.”
React’s renderer takes care of how to make the DOM match this description efficiently.
That’s the declarative model.
Why This Matters
Under the hood, React builds an internal structure (a virtual DOM) that represents what the UI should be. Whenever state changes, React:
- Runs your function again → producing a new description (the next “virtual DOM tree”)
- Diffs it with the previous one
- Applies the minimal mutations to the actual DOM
This means:
- The UI is always consistent with your data.
- You no longer manually synchronize DOM and state.
- Logic and rendering are now deterministic — given the same state, you always get the same UI.
Declarative UIs are therefore predictable and testable, because they’re pure functions of state.
Imperative vs Declarative: A Mental Model
| Aspect | Imperative DOM | Declarative React |
|---|---|---|
| What you write | Step-by-step instructions | A snapshot of desired UI |
| Control | You control how to update | React controls how to update |
| Synchronization | Manual | Automatic |
| Source of truth | DOM | State |
| Readability | Mixed data + UI logic | State-driven, modular components |
Real-World Analogy
Think of imperative code as giving the browser a recipe:
“Take that element, remove its text, add this, change its color…”
Declarative code is like giving the browser a picture:
“Here’s how it should look; you figure out the steps.”
The second approach allows React’s internal engine to optimize — batch updates, schedule rendering, and even pause and resume work (which becomes crucial in concurrent rendering later).
Evolution / Refinement
React began as a radical experiment: a UI library that re-renders everything on every state change. At the time, developers thought it would be too slow.
But the virtual DOM and diffing algorithm made it practical, and the mental clarity it brought was revolutionary:
- Each component is a pure function of props and state.
- There’s no global mutable DOM state.
- Updates are deterministic.
This shift allowed React to evolve toward pure functional components and concurrent rendering, where even time becomes declarative — but we’ll get there step by step.
Principle / Takeaway
React exists to make UI a pure function of state. The declarative model replaces manual DOM manipulation with predictable descriptions of how the UI should look, not instructions for how to get there.
In short:
UI = f(state)
Every innovation in modern React — hooks, effects, suspense, concurrency — refines this one idea: UI as a pure function of time and data.
Next, we’ll move to Lesson 2 – Components as Pure Descriptions, Not Procedures, where we’ll see how React breaks the UI into composable, deterministic units, and why this modularity is key to understanding everything that follows?