Why React, and the component model

Beginner · 13 min

The manual-DOM pain React was built to remove, what "declarative" actually means, and why a component — not a page — is the unit you build with.

Web Dev Simplified · 13 min

Why React, and the component model

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Web Dev SimplifiedReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Hand-written DOM manipulation scales badly because every change to what the page should show has to be translated manually into a sequence of imperative steps that produce it.
  • Declarative UI means you describe what the interface should look like for a given piece of data, and the library figures out which DOM operations make that true.
  • A component is a reusable description of a piece of UI — markup plus the logic that drives it — which is the unit React is built around instead of the page or the template.
  • Components compose: a big UI is built by nesting small components inside larger ones, the same way functions call other functions.
  • One-way data flow means data moves down through components as input, never sideways or back up silently, which makes it possible to trace where a piece of UI state actually came from.
  • React's core job is keeping what's on screen in sync with your data; the mechanics of how it does that efficiently are a separate topic, covered next in this roadmap.

The page you have to update by hand

Before React, building an interactive page usually meant writing code that reached directly into the DOM and changed it, one operation at a time. Say you’re building a counter:

<button id="increment">+1</button>
<span id="count">0</span>
let count = 0;
const countEl = document.getElementById("count");
const buttonEl = document.getElementById("increment");

buttonEl.addEventListener("click", () => {
  count += 1;
  countEl.textContent = count;
});

This works. It is also a preview of the problem: every piece of state that can change needs its own line (or ten) of code that locates the right DOM node and mutates it, by hand, in the right place. Now add a “reset” button, a label that turns red above 10, and a second counter on the same page sharing some of that logic. Each new feature means more call sites that touch the DOM directly, and each one has to be kept perfectly in sync with the current value of count. Miss one, or update the DOM before updating the variable, and the screen lies about the actual state of the program. In a ten-line script this is a minor annoyance. In an application with hundreds of interactive pieces, it becomes the dominant source of bugs: the code’s job is no longer “decide what the data is,” it’s “remember every place the DOM needs to be told about it.”

Describing the result instead of the steps

React’s answer is to let you write what the UI should look like for a given state, and get out of the business of writing the DOM operations that produce it:

function Counter() {
  const [count, setCount] = useState(0);
  return (
    <div>
      <button onClick={() => setCount(count + 1)}>+1</button>
      <span>{count}</span>
    </div>
  );
}

(useState is covered properly later in this roadmap — for now, treat it as “a piece of data this component owns and can change.”) Notice what’s missing compared to the DOM version: there is no getElementById, no .textContent = . You describe the relationship between count and the markup once, and every time count changes, React re-runs that description and updates whatever changed on screen. You never write “find the span and change its text” — you write “the span contains count,” and that stays true no matter how count got updated or from where.

This is the distinction between imperative code (a sequence of steps that produces a result) and declarative code (a description of the result itself). CSS is a familiar declarative example: you write color: red, not “find this element and set its paint color.” React brings that same style to the parts of the UI that used to require imperative DOM scripting.

The component as the unit of reuse

Counter above is a component: a function that bundles a piece of UI together with the logic driving it, under one name, that can be placed anywhere a UI is needed. This is the core structural idea React adds on top of “declarative templates.” Instead of organizing an app by page or by template file, you organize it by component — a Button, a SearchBar, a ProductCard — and build pages by nesting components inside other components, much like composing small functions into larger programs.

That composability is what makes reuse practical at scale. A ProductCard written once can appear on a search results page, a category page, and a “recently viewed” sidebar, each time receiving different data but running the same logic and markup. Change how a product card looks, and every place it’s used updates together, because there is exactly one definition of it.

Data flows one way

The last concept worth having before writing any real React code is one-way data flow. Data is handed down from a component to the components it renders, as input — the same direction a function argument flows from caller to callee. A component lower in the tree can use what it’s given, but it cannot reach back up and silently change the data a component above it owns. If something needs to change, it has to happen explicitly — later lessons cover exactly how — rather than through a value getting mutated from some unrelated place in the tree.

This matters because it makes UI bugs traceable. In the imperative DOM version, any line of code anywhere could, in principle, touch any element. With one-way data flow, you can always ask “where did this value come from?” and follow a single direction — down the tree — to find the answer, instead of searching the whole codebase for anywhere it might have been mutated.

What the video covers

Web Dev Simplified’s video makes the motivational case directly: it explains why React exists, walks through the pain of imperative DOM updates at scale, and introduces the component as the reusable building block React is organized around — all without getting bogged down in code. It moves past the DOM-manipulation comparison more quickly than the walkthrough above, and it doesn’t use the phrase “one-way data flow” by name, so hold onto that term from this lesson rather than the video. Watch it for the broader motivation and framing; use the sections above for the sharper vocabulary.

Where this fits

This is the first lesson of the roadmap — everything else builds on “declarative,” “component,” and “data flows down.” Next: setting up a React project, where you’ll create a real project and see a component render for the first time. The JavaScript language itself (functions, variables, modules) is taught separately in the javascript roadmap; this roadmap assumes that and focuses only on what React adds on top of it.

Check yourself

  1. 01What specifically makes hand-written DOM code hard to maintain as an app grows?

    Every state change requires someone to manually work out and write the exact sequence of DOM calls that gets the page from its old appearance to its new one, and that translation logic is scattered across every place the state can change, so it is easy for one of those call sites to drift out of sync with the rest.

  2. 02What does it mean for a UI approach to be declarative rather than imperative?

    Declarative code describes the desired end state — what the UI should look like given the current data — and leaves the step-by-step mechanics of getting there to the library. Imperative code is the step-by-step mechanics themselves, written out by hand.

  3. 03Why is the component, rather than the page, the basic unit in React?

    A component bundles a piece of UI with the logic that drives it into one reusable, nameable thing that can be placed anywhere and composed with other components, the same way a function is a reusable unit of logic. A page is just whatever tree of components happens to be rendered at a given route.

  4. 04What does one-way data flow mean in React, at a conceptual level?

    Data is passed down from a component to the components it renders, as input, and never flows back up or sideways on its own. A child cannot silently change what a parent gave it; if the UI needs to change, something has to explicitly cause new data to flow down again.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark