Introduction to React – Why Component-Driven UIs Matter
Quick Answer: React lets you build web interfaces from small, reusable pieces called components. You describe what the screen should look like for a given state, and React's Virtual DOM updates only the exact elements that changed.
Imagine Arjun builds a live cricket score app for fans in Bengaluru. During the final over, the score changes every 30 seconds.
With plain JavaScript, Arjun has to hunt for the score box, the run rate box, and the batter box in the HTML and rewrite them by hand. One missed element breaks the screen.
How do we build fast, interactive screens without writing fragile DOM update code every time data changes?
1. Why Do We Need Component-Driven UIs?
The problem without React
In a naive plain-JavaScript setup, you write step-by-step instructions to find and change HTML elements. This is called imperative UI programming.
Example 1: Updating Arjun's cricket scoreboard with plain JavaScript
This simple code works for one score box, but becomes messy as the page grows.
function addFourRuns(newRuns, newRate) {
// Manually find and rewrite each HTML element
document.getElementById("score").innerText = newRuns;
document.getElementById("run-rate").innerText = newRate;
document.getElementById("last-ball").className = "badge-four";
}
What happens as the app grows
1. Designer moves #score into a new tab -> getElementById returns null -> App crashes 2. Developer forgets to update mobile banner -> Header shows 146, banner shows 142 3. Rebuilding a whole player list on every ball -> Browser stutters on older phones
This setup works on a small static page. However, it breaks down when a real web app has dozens of moving parts.
- Your JavaScript becomes tightly glued to specific HTML IDs.
- Different parts of the page easily fall out of sync with the latest data.
- Changing the real browser DOM too often makes the page feel slow and laggy.
We need a way to split the screen into independent pieces and let a smart engine handle the DOM updates for us.
The solution: React Components & the Virtual DOM
Component = a small, self-contained UI block (like a score badge or player row) that bundles its own markup and logic.
Declarative UI = telling React what the screen should look like for the current data, instead of how to change the HTML step by step.
Virtual DOM = a fast, lightweight copy of the real browser DOM kept inside JavaScript memory.
Reconciliation = the diffing process where React compares the old Virtual DOM with the new one and patches only what changed.
Live Flow: React Virtual DOM Diffing & Surgical Patching
Watch what happens in Arjun's cricket app when a batter hits a boundary (142 → 146).
React never touches <Header> or <PlayerList> in the real browser DOM. Only the single text node inside <ScoreBadge> updates.
2. How React Works in Practice
State = the changing data inside a component, like the current cricket score.
In React, you write a function that returns the UI for the current state. When state changes, React re-runs the function and updates the screen automatically.
Example 2: A declarative ScoreBadge component in React
Arjun never touches document.getElementById. He only updates runs.
import { useState } from "react";
export function ScoreBadge() {
const [runs, setRuns] = useState(142);
return (
<div>
<h2>Bengaluru Live: {runs}/3</h2>
<button onClick={() => setRuns(runs + 4)}>
+4 Boundary
</button>
</div>
);
}
Expected result in the browser
Initial render: Bengaluru Live: 142/3 User clicks +4: Bengaluru Live: 146/3 (only the text "142" -> "146" is patched)
Notice how clean this is. The UI is simply a direct reflection of the runs state.
3. Plain JavaScript vs. Component-Driven React
| Without React (Imperative) | With React (Declarative) |
|---|---|
| You manually find and edit HTML elements. | You update state, and React updates the DOM. |
| UI code is hard to reuse across pages. | Small components can be reused anywhere. |
| Easy for data and screen to get out of sync. | Single source of truth keeps UI always in sync. |
The important terms to remember
- Component = a reusable, independent building block of a React UI.
- Declarative UI = describing the desired final screen for a given state.
- Virtual DOM = an in-memory tree React uses to calculate minimal updates.
- Reconciliation = React's diffing algorithm that compares old and new trees.
Design tip: Keep each component focused on one job. If a component grows past 150 lines, split it into smaller child components.
4. Common Mistakes to Avoid
1. Changing state directly instead of using the setter
Writing runs = runs + 4 changes the variable in memory, but React never knows it happened, so the screen stays stuck at 142.
Better approach: Always call the state setter function, like setRuns(runs + 4), so React triggers reconciliation.
2. Putting the entire page inside one giant component
When everything lives in one component, a tiny score update forces React to re-run logic for the whole page.
Better approach: Break the page into small components like Header, ScoreBadge, and PlayerList.
5. Interview Questions & Practice
Question 1: What is the Virtual DOM, and does it make React faster than raw DOM updates?
What the interviewer expects: Understanding that the Virtual DOM is about predictable, batched minimal updates, not magic speed.
Strong answer: "The Virtual DOM is a lightweight JavaScript copy of the real DOM. When Arjun's cricket score changes from 142 to 146, React compares the old and new Virtual DOM trees and patches only that one score element in the real DOM. Hand-tuned vanilla JS can technically be faster for one tiny change, but React stays fast and bug-free at scale without manual DOM code."
Practice: Hospital appointment status board
You are building a live waiting-room screen for a hospital in Chennai. It shows the hospital header, the current token number, and a list of 8 doctors on duty. Split this screen into 3 React components and name which single component updates when Token #42 changes to Token #43.
Expected result: Components: HospitalHeader, CurrentToken, and DoctorList. Only CurrentToken patches in the real DOM.
6. Next Steps
- Learn Next: JSX Syntax & Passing Data with Props
- Practice Next: Managing Interactive State with
useStateand Event Handlers
Discussion & Comments
0No comments yet
Start the discussion above — ask a question, share a tip, or reply to a comment.