1. What is React?

React is a JavaScript library for building user interfaces. Created by Facebook (now Meta) in 2013, React introduced a radical idea: instead of manipulating the DOM directly, you describe what the UI should look like and React figures out how to update it efficiently.

🛒
UrbanCart Example
React powers modern shopping experiences

Imagine building UrbanCart — an e-commerce platform with product listings, a search bar, a shopping cart, and a checkout flow. With vanilla JavaScript, you'd write hundreds of lines of createElement and appendChild calls. With React, each piece of the UI is a component — a self-contained, reusable building block. The header is a component. Each product card is a component. The cart is a component. You compose them together like LEGO bricks.

Library, not framework: React is a library focused on the view layer — it handles UI rendering. Frameworks like Angular include routing, state management, and HTTP clients out of the box. React lets you choose your own tools for those concerns.

2. Why React?

React isn't the only UI library, but it's the most popular for good reasons:

🧩

Reusable Components

Build a ProductCard once, use it for every product. Props let you customize each instance. No copy-paste HTML.

⚡

Virtual DOM

React keeps a lightweight copy of the DOM in memory. When data changes, it diffs the old and new copies, then updates only what changed. Fast.

🌐

Massive Ecosystem

React Router for navigation, Redux/Zustand for state, Next.js for server-side rendering. Thousands of libraries work with React.

🏢

Industry Standard

Used by Meta, Netflix, Airbnb, Uber, Shopify, and thousands of startups. React skills are the most in-demand for front-end roles.

Job market reality: React is the #1 most requested front-end skill on job boards across India and East Africa. Learning React opens doors to full-stack roles at both startups and enterprise companies.

3. Components — the building blocks

Everything in React is a component. A component is a JavaScript function that returns a piece of UI. Think of it as a custom HTML tag that you define yourself.

🧱
UrbanCart Components
Breaking UrbanCart into components

An UrbanCart page might consist of these components:

  • <Header /> — logo, search bar, cart icon
  • <ProductCard /> — image, title, price, "Add to Cart" button
  • <CartSidebar /> — list of items, quantities, total
  • <SearchBar /> — input field with live filtering
  • <Footer /> — links, copyright
component.jsx A simple React component
Naming convention: React components always start with a capital letter — ProductCard, not productCard. Lowercase names are treated as regular HTML tags.

4. JSX — JavaScript + XML

JSX is React's syntax extension that lets you write HTML-like code inside JavaScript. It looks like HTML, but it's actually JavaScript under the hood.

jsx-basics.jsx JSX compiles to function calls

JSX rules

  • Expressions in curly braces: Use {} to embed any JavaScript expression — {price * quantity}, {user.name}, {items.length}
  • className, not class: Since class is a reserved word in JS, React uses className for CSS classes
  • Self-closing tags: Tags without children must self-close — <img />, <br />, <input />
  • One root element: A component must return a single root element. Wrap siblings in a <div> or <>...</> (fragment)
jsx-expressions.jsx Dynamic values with curly braces
JSX is not HTML: JSX looks like HTML but has key differences — className instead of class, htmlFor instead of for, camelCase event handlers like onClick instead of onclick. These are because JSX compiles to JavaScript objects, not HTML strings.

5. The Virtual DOM

The virtual DOM is React's secret to fast UI updates. Instead of touching the real DOM directly (which is slow), React maintains a lightweight JavaScript copy of the DOM tree in memory.

How the Virtual DOM works

State changes
→
New Virtual DOM
→
Diff (compare)
→
Patch real DOM
  1. State changes: A user adds an item to the UrbanCart cart
  2. New virtual DOM: React creates a new virtual DOM tree reflecting the updated cart count
  3. Diffing: React compares the new virtual DOM with the previous one and finds exactly what changed (e.g., the cart badge number went from 2 to 3)
  4. Patching: React updates only the cart badge in the real DOM — not the entire page
Why is the real DOM slow? Every time you modify the real DOM, the browser may need to recalculate styles, reflow the layout, and repaint pixels. React minimizes these expensive operations by batching changes and only updating the parts that actually changed.

6. React vs Vanilla JS

Let's build the same UrbanCart product card using vanilla JavaScript and React, side by side.

Vanilla JavaScript

vanilla.js The manual way

React (JSX)

react.jsx The declarative way

The React version is shorter, more readable, and easier to maintain. As UI complexity grows, the gap widens dramatically. Imagine building an entire product listing page with filtering, sorting, and a cart — vanilla JS becomes unwieldy, while React stays clean.

7. Project structure

In production, React apps are typically created with tools like Vite or Create React App. These set up a build pipeline with module bundling, hot reload, and optimizations.

terminal Creating a React project with Vite
For this tutorial track: We'll use CDN React (React loaded via script tags) so you can learn React concepts without build tools. In the real world, you'll use Vite or Next.js. We'll cover those in later lessons.

A typical React project structure looks like this:

project-tree Typical Vite + React structure

8. Try It Yourself — Product Card

Your first React component! This ProductCard renders a product with a name, price, and stock badge. Click Run to see it render live.

⚛️ Try It Yourself — ProductCard Component
Preview
Experiment! Try changing the product name, price, or badge text. Change in-stock to out-of-stock and hit Run again. Notice how React re-renders the entire component with the new values.

9. Try It Yourself — Multiple Components

Real React apps compose multiple components together. Here we create a reusable ProductCard that accepts props (properties) and render three products inside an App component.

⚛️ Try It Yourself — Multiple Components with Props
Preview
Props are powerful: Notice how we defined ProductCard once but used it three times with different data. This is the heart of React — reusable components + dynamic data. We'll dive deep into props in the next lesson.

🧠 Quiz — Test your understanding

Question 1 of 5
What is React?
Question 2 of 5
What does JSX compile to?
Question 3 of 5
Why does React use a virtual DOM instead of updating the real DOM directly?
Question 4 of 5
In JSX, why do you write className instead of class?
Question 5 of 5
What is the correct way to embed a JavaScript expression inside JSX?
0
/5
Great job!

🏆 Challenge: Build an UrbanCart product listing

Combine everything you've learned into a mini product listing:

  1. Create a ProductCard component that accepts name, price, and inStock as props
  2. Show a green "In Stock" or red "Out of Stock" badge based on the inStock prop
  3. Create an App component that renders at least 4 different products
  4. Add an <h1> with "UrbanCart" and a subtitle "Today's Deals"

Bonus: Add a category prop and display it as a small label above the product name.

Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You just wrote your first React component.
Now build a full-stack career.

Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses →