React Basics

Session 1 — How React Works, Its Structure & Your First Code

⏱ 45 minutes
📚 Beginner
🎯 Foundations
Scroll to begin ▼
Part 1 · Overview

Session Agenda

Here's what we'll cover in 45 minutes. Each block builds on the last — by the end, you'll write a working React component from scratch.

0 – 5 min

What is React & Why Use It?

The problem React solves and where it fits in web development.

5 – 15 min

How React Works Under the Hood

Virtual DOM, components, and the rendering cycle.

15 – 25 min

React Project Structure

Files, folders, JSX syntax, and how everything connects.

25 – 40 min

Your First React Code

Components, props, state, and a hands-on counter app.

40 – 45 min

Recap & Quick Quiz

Review key concepts and test what you've learned.

Part 2 · The Basics

What is React?

React is a JavaScript library for building user interfaces, created by Facebook (now Meta) in 2013. It's not a full framework — it focuses on one thing: rendering UI efficiently.

📦

Library, Not Framework

React handles the view layer only. You pick your own tools for routing, state management, and more.

🧩

Component-Based

Build your UI from small, reusable pieces called components. Like LEGO blocks for interfaces.

⚡

Declarative

You describe what the UI should look like, and React figures out how to update the DOM.

🔄

One-Way Data Flow

Data flows from parent to child components, making your app predictable and easier to debug.

💡 Key Insight

Think of React as a smart assistant: you tell it "the button should say 'Hello'" and React handles adding it to the page and updating it when things change.

Part 3 · Motivation

Why React?

Before React, updating the DOM manually was painful. Let's compare approaches:

Aspect Vanilla JS React
Updating UI Manual DOM manipulation Automatic via state changes
Code structure Spaghetti as app grows Clean component hierarchy
Reusability Copy-paste HTML Reuse components anywhere
Performance Full DOM rewrites Smart, minimal updates
State management Scattered variables Centralized, predictable
🌍 Who Uses React?

Facebook, Instagram, Netflix, Airbnb, Uber, WhatsApp Web, Discord, and thousands more production apps worldwide.

Part 4 · Under the Hood

How React Works

React's secret weapon is the Virtual DOM — a lightweight copy of the real DOM that React keeps in memory.

The React Rendering Cycle
State
Changes
→
Re-render
Component
→
Virtual
DOM Diff
→
Update
Real DOM
1

State changes trigger a re-render

When data (state) in your component changes, React notices and kicks off an update.

2

React builds a new Virtual DOM tree

It creates a fresh virtual copy of what the UI should look like with the new data.

3

It diffs the old and new trees

React compares both versions and finds the minimum set of changes needed. This is called reconciliation.

4

Only the changes hit the real DOM

Instead of rewriting everything, React surgically updates just the parts that changed — fast and efficient.

💡 Analogy

Imagine editing a Google Doc: instead of reprinting the entire document after every typo, only the changed paragraph gets updated. That's what React does with your UI.

Part 5 · Project Anatomy

React Project Structure

When you create a React app with Vite (the modern way), you get this folder structure:

project-structure
my-react-app/
├── node_modules/      # installed packages
├── public/            # static files (favicon, etc)
├── src/               # 👈 YOUR CODE LIVES HERE
│   ├── App.jsx        # main component
│   ├── main.jsx       # entry point
│   ├── App.css        # styles for App
│   └── index.css      # global styles
├── index.html         # single HTML page
├── package.json       # dependencies & scripts
└── vite.config.js     # build configuration

The most important file is main.jsx — this is where React "mounts" your app into the HTML page:

src/main.jsx
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App'

ReactDOM.createRoot(
  document.getElementById('root')
).render(<App />)
⚠️ Key Point

React apps are Single Page Applications — there's only one HTML file. React dynamically swaps content in and out of the <div id="root"> element.

Part 6 · Syntax

JSX — HTML in JavaScript

JSX lets you write HTML-like syntax directly inside JavaScript. It looks like HTML, but it's actually JavaScript under the hood.

JSX vs HTML
// ✅ This is JSX (inside a React component)
const element = (
  <div className="greeting">
    <h1>Hello, World!</h1>
    <p>Welcome to <strong>React</strong></p>
  </div>
)

// ⚠️ Key JSX Rules:
// 1. Use className instead of class
// 2. Use camelCase for attributes (onClick, htmlFor)
// 3. Must have ONE parent element
// 4. Close ALL tags: <img />, <br />
// 5. Use { } for JavaScript expressions

You can embed any JavaScript expression inside curly braces { }:

Dynamic JSX
const name = "Ahmed"
const age = 25

const element = (
  <div>
    <h1>Hello, {name}!</h1>
    <p>You are {age} years old</p>
    <p>Next year: {age + 1}</p>
  </div>
)
Part 7 · Building Blocks

Components & Props

Components are reusable functions that return JSX. Props let you pass data into them — like function parameters for your UI.

Your First Component
// 1. Define a component (always starts with uppercase!)
function Greeting({ name, emoji }) {
  return (
    <div>
      <h2>{emoji} Hello, {name}!</h2>
    </div>
  )
}

// 2. Use it like an HTML tag
function App() {
  return (
    <div>
      <Greeting name="Ahmed" emoji="👋" />
      <Greeting name="Sara" emoji="🌟" />
      <Greeting name="Youssef" emoji="🚀" />
    </div>
  )
}
💡 Props Rule

Props are read-only. A component should never modify its own props. Think of them as arguments to a function — you use them, you don't change them.

Part 8 · Interactivity

State — Making Things Interactive

State is data that belongs to a component and can change over time. When state changes, React re-renders the component automatically.

Counter.jsx — Your First Stateful Component
import { useState } from 'react'

function Counter() {
  // Declare state: [value, setter] = useState(initial)
  const [count, setCount] = useState(0)

  return (
    <div>
      <h2>Count: {count}</h2>
      <button onClick={() => setCount(count + 1)}>
        + Add
      </button>
      <button onClick={() => setCount(count - 1)}>
        − Subtract
      </button>
      <button onClick={() => setCount(0)}>
        Reset
      </button>
    </div>
  )
}

🔴 Live Demo — Try It!

This is exactly what the code above produces:

0
🧠 State vs Props

Props = data passed into a component (from parent). State = data managed inside a component. Props are read-only; state can be updated with its setter function.

Part 9 · Wrap-Up

Recap & Quick Quiz

Let's review the key concepts from today's session, then test your understanding.

✅

React is Declarative

You describe the desired UI, React handles DOM updates.

✅

Virtual DOM

React diffs a virtual copy against the real DOM for efficient updates.

✅

Components

Reusable functions that return JSX. They accept props as input.

✅

State & useState

Component-local data that triggers re-renders when updated.

🧪 Quiz — Question 1 of 3

What does React use to efficiently update the UI?

🧪 Quiz — Question 2 of 3

Which is the correct way to embed JavaScript in JSX?

🧪 Quiz — Question 3 of 3

What is the difference between props and state?
🎯 What's Next?

In Session 2, we'll cover event handling, conditional rendering, lists & keys, and building a real mini-project. See you there!