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.
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.
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.
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.
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
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 rules
- Expressions in curly braces: Use
{}to embed any JavaScript expression —{price * quantity},{user.name},{items.length} - className, not class: Since
classis a reserved word in JS, React usesclassNamefor 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)
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: A user adds an item to the UrbanCart cart
- New virtual DOM: React creates a new virtual DOM tree reflecting the updated cart count
- 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)
- Patching: React updates only the cart badge in the real DOM — not the entire page
6. React vs Vanilla JS
Let's build the same UrbanCart product card using vanilla JavaScript and React, side by side.
Vanilla JavaScript
React (JSX)
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.
A typical React project structure looks like this:
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.
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.
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
className instead of class?🏆 Challenge: Build an UrbanCart product listing
Combine everything you've learned into a mini product listing:
- Create a
ProductCardcomponent that acceptsname,price, andinStockas props - Show a green "In Stock" or red "Out of Stock" badge based on the
inStockprop - Create an
Appcomponent that renders at least 4 different products - 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.
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 →