React Components and Props
Introduction to React
Build Interactive UIs with Components & State
By AI Learning Assistant · React · JSX · Hooks · Virtual DOM
🤖 YOUR KIND AI LEARNING ASSISTANT
Hello learner! I'm here to help you understand React. Don't worry if some concepts feel challenging at first — every React developer started exactly where you are now. Take your time, read each section carefully, and feel free to ask me questions. You've got this! 💪
What is React?
A LIBRARY FOR USER INTERFACES · NOT A FRAMEWORK
React is a JavaScript library for building user interfaces. Created by Facebook in 2013, it's now the most popular frontend library in the world. But here's what makes React special: instead of manually changing HTML elements with code like document.getElementById().innerHTML = ..., you simply describe what your UI should look like, and React figures out how to make it happen.
✨ A Gentle Analogy: Imagine you're directing a play. Without React, you'd have to tell each actor exactly when to walk, where to stand, and what to say, step by step. With React, you just describe the final scene ("Romeo is standing under the balcony looking up at Juliet"), and React handles all the movement. You describe the destination, not the journey.
🎯 DECLARATIVE vs IMPERATIVE — A FRIENDLY COMPARISON
Imperative (Vanilla JS - the old way): "Get the button element. Add a click listener. When clicked, find the counter span. Read its text. Convert to number. Add 1. Put it back." — You tell the browser EVERY tiny step.
Declarative (React - the modern way): "Here's a button and a counter. The counter should show 'count' clicks. When button is clicked, increase count." — You describe WHAT should happen, React handles HOW.
What is JSX?
WRITING HTML INSIDE JAVASCRIPT
JSX (JavaScript XML) lets you write HTML-like code directly inside your JavaScript files. It looks like HTML, but it's actually JavaScript under the hood. This is one of the things beginners love about React — you can see exactly what your UI will look like right in your code.
📜 JSX EXAMPLES — DON'T WORRY, IT'S SIMPLER THAN IT LOOKS
// ============================================ // BASIC JSX — Just like HTML but inside JavaScript! // ============================================ // A heading with your name const element =Hello, Kemi!
; // Embedding JavaScript variables inside curly braces { } const userName = "Tunde"; const greeting =Welcome back, {userName}!
; // Using expressions inside curly braces const total =5 + 3 = {5 + 3}; // Shows "5 + 3 = 8" // Conditional rendering (if this, show that) const isLoggedIn = true; const message = ( <div> {isLoggedIn ?You are logged in!
:Please log in
} div> ); // ============================================ // IMPORTANT JSX RULES (Don't worry, you'll memorize them) // ============================================ // 1. Use className instead of class (class is a reserved word in JS) // ✅// ❌// 2. Use camelCase for event handlers // ✅ // ❌ // 3. Self-closing tags MUST have a forward slash // ✅✅ ✅
// 4. Return only ONE parent element (use <> fragment if needed) // ✅ return ()Title
Text
🤗 AI ASSISTANT SAYS:
"JSX might feel strange at first — mixing HTML inside JavaScript? But trust me, after writing just a few components, it will feel completely natural. Every React developer went through this same learning curve. You're doing great! 🌟"
Creating Your First React App
ONE COMMAND · ALL SET UP FOR YOU
📜 STEP-BY-STEP: CREATING YOUR REACT APP
// First, make sure Node.js is installed (check with node --version) // Then run these commands in your terminal: // This creates a brand new React project in a folder called "webbo3-react" npx create-react-app webbo3-react // Move into your new project folder cd webbo3-react // Start the development server (your app will open in your browser!) npm start // You should see "Welcome to React" at http://localhost:3000 // To stop the server: press Ctrl + CUnderstanding useState
THE HOOK THAT MAKES YOUR COMPONENTS REMEMBER
In React, state is data that changes over time. For example: what someone typed in a form, whether a button was clicked, items in a shopping cart. Without state, your app would be static — like a printed poster instead of an interactive website.
The useState hook is how functional components in React create and update state. It returns an array with exactly two things: the current state value, and a function to update it.
🎯 useState — A Gentle Explanation
// This is how useState works: const [name, setName] = useState(""); // ^ ^ ^ // | | | // | | +-- Initial value (empty string) // | +-- Setter function (only way to update state) // +-- Current state value (changes when setter is called) // To read state: console.log(name); // shows current value // To update state (this triggers a re-render!): setName("Amara");🎈 Simple Analogy: Think of useState as a box with a label. The box holds one value at a time. The setter function is the only way to change what's in the box. When you put something new in the box, React automatically updates anything that depends on that box. That's why your UI changes automatically!
Your First React Component — The Enrollment App
PUTTING EVERYTHING TOGETHER
📜 COMPLETE src/App.js — EVERY LINE EXPLAINED WITH ENCOURAGEMENT
// ============================================ // STEP 1: Import React and the useState hook // ============================================ import React, { useState } from "react"; // ============================================ // STEP 2: Define the App component // ============================================ function App() { // ============================================ // STEP 3: Create state variables // ============================================ // name: stores what the user types in the input field const [name, setName] = useState(""); // enrolled: stores whether the user has clicked Enroll const [enrolled, setEnrolled] = useState(false); // ============================================ // STEP 4: Event handler — runs when button is clicked // ============================================ const handleEnroll = () => { // trim() removes spaces — so " Amara " becomes "Amara" // We only enroll if they actually typed something meaningful if (name.trim() !== "") { setEnrolled(true); // This triggers a re-render! } }; // ============================================ // STEP 5: Return the JSX (what the user sees) // ============================================ return ( <div style={{ padding: "40px", fontFamily: "Arial" }}> <h1>Welcome to Webbo3h1> <h2>JavaScript Mastery Course — Cohort 01h2> // Conditional rendering: show different UI based on enrollment status {!enrolled ? ( // NOT enrolled — show the form <div> <input placeholder="Enter your name" value={name} onChange={e => setName(e.target.value)} /> <button onClick={handleEnroll}>Enroll Nowbutton> div> ) : ( // Enrolled — show the welcome message <p style={{ color: "green" }}> Welcome, {name}! You are enrolled in Cohort 01. p> )} div> ); } export default App;🎯 Try It Yourself! — Interactive Demo
Welcome to Webbo3
JavaScript Mastery Course — Cohort 01
✨ This is exactly what your React app will look like! Type your name and click Enroll.
🤗 AI ASSISTANT SAYS:
"Did you see that? You just built an interactive React app! 🎉 The input remembers what you type (controlled component), the button responds to clicks, and the UI changes automatically. This is the same pattern used by Facebook, Netflix, and Airbnb. You should be proud of yourself! Take a moment to appreciate what you've learned."
The Virtual DOM — Why React is Fast
THE SECRET SAUCE
🎯 UNDERSTANDING THE VIRTUAL DOM — WITH A STORY
Imagine you're rearranging furniture in a huge mansion. Every time you move one chair, you call a moving company to rearrange everything — that's the Real DOM (slow!).
React's Virtual DOM is like drawing the floor plan on paper first. You move chairs on paper, erase, draw again, until you have the perfect layout. Then you make all the changes in the real mansion at once — much faster!
How it actually works: When your state changes, React creates a new Virtual DOM tree (lightweight JavaScript object), compares it with the previous Virtual DOM tree (diffing), finds the minimal set of changes, and applies only those changes to the real DOM. One batch update instead of many small ones.
Why you should care: This is what makes React feel fast and responsive even in complex apps with hundreds of components updating frequently.
Quick Reference — Keep This Handy!
Concept What It Does Example Component Reusable piece of UI function Button() { return } JSX HTML inside JavaScript HellouseState Adds memory to components const [count, setCount] = useState(0) Conditional Rendering Show different UI based on state {isLoggedIn ? : } Virtual DOM React's efficient update system Plan on paper before painting the wall 🤗 YOUR KIND AI ASSISTANT — ALWAYS HERE TO HELP
💬 Ask me anything! "Why isn't my component re-rendering?" → I'll help you check if you used the setter function correctly.
💬 Stuck on an error? "I'm getting 'Cannot read property of undefined' — what does this mean?" → I'll explain the error in plain English and help you fix it.
💬 Want to go further? "Show me how to add a list of enrolled students to this app." → I'll guide you through adding new features step by step.
Remember: Every expert was once a beginner. You are exactly where you need to be. Keep coding, keep asking questions, and celebrate every small victory! 🎉
AI-Assisted JavaScript Learning · Introduction to React · You've Got This! 💙
Complete this lesson
Mark as complete to track your progress
✅ ✅