Module 2.1: React Overview - Why React?

🎯 Learning Objectives
By the end of this module, you will be able to:
- Understand what React is and why it’s a Library, not a Framework.
- Differentiate between Declarative and Imperative programming.
- Grasp Component-Based Architecture.
- Understand the Virtual DOM mechanism and why React is fast.
- Distinguish between SPA (Single Page Application) and MPA (Multi-Page Application).
- Get an overview of the React Ecosystem.
1. What is React?
1.1. Definition
React (or React.js) is a JavaScript Library for building user interfaces (UI). React was developed by Meta (Facebook) and first released in 2013.
Key Point: React is a Library, not a Framework.
| Criterion | Library (React) | Framework (Angular, Vue) |
|---|---|---|
| Control | Developer controls the flow | Framework controls the flow |
| Flexibility | Choose your own routing, state libraries | Comes with built-in solutions |
| Learning | Learn React core, then expand | Learn the entire ecosystem at once |
| Example | React + React Router + Redux | Angular (all built-in) |
1.2. Why is React Popular?
According to Stack Overflow trends:

Reasons for React’s Popularity:
- Component-Based: Reusable UI components.
- Virtual DOM: Excellent performance.
- Large Ecosystem: Thousands of supporting libraries.
- Meta Backing: Developed by a major tech company.
- Community: A massive developer community.
- Job Market: High demand for React developers.
2. Declarative vs Imperative
2.1. Imperative Programming
Imperative = “How” - Specifies every step explicitly.
// jQuery (Imperative) - Adding an item to a list
const button = document.getElementById('add-btn');
const list = document.getElementById('list');
const input = document.getElementById('input');
button.addEventListener('click', function () {
const value = input.value; // Step 1: Get input value
const li = document.createElement('li'); // Step 2: Create li element
li.textContent = value; // Step 3: Set text content
list.appendChild(li); // Step 4: Append to the list
input.value = ''; // Step 5: Clear input
});2.2. Declarative Programming
Declarative = “What” - Describes the desired outcome.
// React (Declarative) - Adding an item to a list
const TodoList = () => {
const [items, setItems] = React.useState([]); // State hook for items
const [input, setInput] = React.useState(''); // State hook for input
const addItem = () => {
setItems([...items, input]); // Declare the new state
setInput('');
};
// Describe the UI you WANT to see; React handles the "how"
return (
<div>
<input value={input} onChange={(e) = /> setInput(e.target.value)} />
<button onClick={addItem}>Add</button>
<ul>
{items.map((item, i) => (
<li key={i}>{item}</li>
))}
</ul>
</div>
);
};
2.3. Comparison
| Criterion | Imperative (jQuery) | Declarative (React) |
|---|---|---|
| Focus | How to do it | What the result is |
| DOM | Direct manipulation | React manages it |
| State | Stored in the DOM | Stored in JavaScript |
| Code | Many detailed steps | UI = f(state) |
| Debugging | Hard to trace DOM state | State is predictable |
React’s Formula:
UI = f(State)When State changes → React automatically re-renders the UI.
3. Component-Based Architecture
3.1. What is a Component?
A Component is a self-contained, reusable UI building block. Each component manages:
- Markup (JSX)
- Styling (CSS)
- Logic (JavaScript)

3.2. Breaking Down UI into Components
3.3. Example Component Hierarchy
// App.jsx
const App = () => {
return (
<div>
<Header />
<Main>
<ProductList />
</Main>
<Footer />
</div>
);
};// Header.jsx
const Header = () => {
return (
<header>
<Logo />
<Navigation />
</header>
);
};// ProductCard.jsx - Reusable Component
const ProductCard = ({ name, price, image }) => {
return (
<div className="card">
<img src={image} alt={name} />
<h3>{name}</h3>
<p>${price}</p>
</div>
);
};// ProductList.jsx - Using ProductCard multiple times
const ProductList = () => {
const products = [
{ id: 1, name: 'iPhone', price: 999, image: '/iphone.jpg' },
{ id: 2, name: 'MacBook', price: 1999, image: '/macbook.jpg' },
];
return (
<div className="product-grid">
{products.map((product) => (
<ProductCard key={product.id} {...product} />
))}
</div>
);
};3.4. Benefits
| Benefit | Description |
|---|---|
| Reusability | Use components in multiple places |
| Maintainability | Fix in one place, apply everywhere |
| Testability | Test individual components in isolation |
| Collaboration | Teams can work in parallel |
4. Virtual DOM
4.1. The Problem with the Real DOM
The DOM (Document Object Model) is a tree representation of your HTML in the browser.
The Problem: Direct DOM manipulation is slow because:
- The browser must recalculate layouts (Reflow).
- The browser must repaint pixels (Repaint).
- Every small change triggers this entire process.
// jQuery - Each line triggers a DOM update
$('#list').append('<li>Item 1</li>'); // Reflow + Repaint
$('#list').append('<li>Item 2</li>'); // Reflow + Repaint
$('#list').append('<li>Item 3</li>'); // Reflow + Repaint
// Total: 3 reflows + repaints4.2. What is the Virtual DOM?
The Virtual DOM is an in-memory representation of the Real DOM, stored as JavaScript objects.

4.3. How the Virtual DOM Works
Terminology:
- Diffing: Comparing two Virtual DOM trees to find differences.
- Reconciliation: The process of updating the Real DOM based on the diff.
4.4. Example Comparison
// React - Internal process
// Imagine a list with 100 items, and the user adds one more
// Step 1: State changes
setItems([...items, newItem]); // Now we have 101 items
// Step 2: React creates a new Virtual DOM (with 101 items)
// Step 3: Diffing - Compares the new VDOM to the old one (100 items)
// Step 4: The diff reveals only 1 new item
// Step 5: Update the Real DOM - Only appends the single new <li>
// It doesn't re-render the 100 existing items!4.5. Performance Benefits
| Scenario | jQuery (Direct DOM) | React (Virtual DOM) |
|---|---|---|
| Add 1 item to 100-item list | Might re-render the whole list | Only adds 1 item |
| Update 1 field in a form | Updates that specific field | Updates that specific field |
| Re-order list items | Complex, many DOM operations | Optimized batching |
5. Single Page Application (SPA)
5.1. MPA vs SPA Comparison
Examples:
- MPA: Traditional websites - each link requests a new page from the server.
- SPA: Gmail, Facebook, Twitter, Spotify Web.

5.2. SPA vs MPA Comparison
| Criterion | MPA (Multi-Page) | SPA (Single-Page) |
|---|---|---|
| Page Load | Full reload on each nav | Load once, update partially |
| UX | White screen between pages | Smooth transitions |
| Server | Server renders HTML | Server returns JSON/API |
| SEO | Good (HTML ready) | Needs SSR/SSG for SEO |
| Complexity | Simpler | More complex (routing, state) |
| Initial Load | Faster (just one page) | Slower (loads entire app) |
| Subsequent | Slower (reloads) | Faster (no reloads) |
5.3. SPA Advantages
- User Experience: Seamless page transitions without full reloads.
- Performance: After the initial load, only data is fetched.
- Mobile-like Feel: Behaves more like a native application.
- Offline Support: Can cache data and function offline.
5.4. SPA Disadvantages
- SEO Challenges: Search engines can struggle to crawl JavaScript-heavy applications.
- Initial Load: Large JavaScript bundles can lead to slower first loads.
- Complexity: Requires handling routing and state management.
- Browser History: Needs explicit implementation for history management.
Solutions to Disadvantages:
- SEO → SSR (Server-Side Rendering) with Next.js
- Initial Load → Code Splitting, Lazy Loading
- Routing → React Router
6. React Ecosystem Overview
6.1. React Core vs React DOM
- React (Core): Component logic, state management, hooks.
- React DOM: Renders to the browser’s DOM.
- React Native: Renders to iOS/Android native components.
6.2. Build Tools
| Tool | Description | Status |
|---|---|---|
| Vite | Fast, modern build tool | ✅ Recommended |
| Next.js | Full-featured React framework | ✅ Production-ready |
| Create React App | Older tool from Facebook | ⚠️ Deprecated |
| Remix | Framework focusing on web standards | ✅ Alternative |
6.3. Ecosystem Libraries
| Category | Popular Libraries |
|---|---|
| Routing | React Router, TanStack Router |
| State Management | Zustand, Redux Toolkit, Jotai |
| Data Fetching | TanStack Query, SWR |
| Forms | React Hook Form, Formik |
| UI Components | Shadcn/ui, Material UI, Chakra UI |
| Styling | Tailwind CSS, Styled Components |
| Testing | Vitest, React Testing Library |