Learn the building blocks of React — the most popular library for building user interfaces.
Note: React requires a build tool (like Vite or Create React App) to run, so the code examples in this chapter are read-only demonstrations. Study them carefully — in the next project chapter, you'll set up a real React app and write this code yourself!
What is React?
Components
JSX
Props
State
Events
Quiz
What is React?
Think of building a website like building with LEGO blocks. Without React, you'd build the entire model as one solid piece — if you wanted to change one part, you'd have to rebuild everything. With React, each part is a separate LEGO block (called a component). Want to change the roof? Just swap that one block!
React is a JavaScript library (created by Facebook/Meta) for building user interfaces. It's the most popular front-end library in the world.
Declarative — You describe WHAT you want, React figures out HOW to update the page
Virtual DOM — React only updates what changed, making it fast
Huge ecosystem — Thousands of libraries, massive community, tons of jobs
How React Works (The Big Picture)
You write Components (JavaScript functions that return UI)
⬇️ React builds a Virtual DOM (a lightweight copy of the real page)
⬇️ When data changes, React compares old vs new Virtual DOM
⬇️ React updates ONLY what changed in the real page (super fast!)
Components
Components are the LEGO blocks of React. Just like a LEGO house is made of wall blocks, window blocks, and door blocks, a React app is made of Header components, Card components, Button components, etc. Each block is independent and reusable.
A React component is just a JavaScript function that returns HTML-like code (called JSX):
React — Your First Component (Read-Only)
Component rules:
1. Component names MUST start with a capital letter (Welcome, not welcome)
2. Components must return one parent element (wrap in a <div> or <>...</>)
3. Always close self-closing tags: <Welcome /> not <Welcome>
Building a Page with Components
React — Component Tree (Read-Only)
JSX
JSX is like a bilingual person who speaks both JavaScript and HTML at the same time. It looks like HTML but lives inside JavaScript. Behind the scenes, it gets converted to regular JavaScript function calls.
JSX = JavaScript XML. It lets you write HTML-like syntax directly in your JavaScript:
React — JSX Syntax (Read-Only)
JSX vs HTML Differences
HTML
JSX
Why?
class="btn"
className="btn"
"class" is a reserved word in JavaScript
for="email"
htmlFor="email"
"for" is a reserved word in JavaScript
style="color: red"
style={{color: "red"}}
Style takes an object in JSX
<!-- comment -->
{/* comment */}
JavaScript comments in curly braces
<img>
<img />
All tags must be closed in JSX
Props
Props are like sending a letter to a component. The parent writes data on the letter (props), seals it, and sends it to the child component. The child can READ the letter but cannot change it — props are read-only! If you want to send different data, send a different letter.
Props (short for "properties") let you pass data from a parent component to a child component:
React — Props (Read-Only)
Destructuring props: Instead of writing function UserCard(props) and then props.name, we use { name, age, role } to directly extract the values. This is called destructuring — a JavaScript feature you learned earlier!
Props are READ-ONLY! A child component should never try to modify its props. If you need data that changes, you need state (next section).
State
If props are like a letter (read-only, from someone else), then state is like a whiteboard in your room. You can write on it, erase it, and update it anytime. When you change what's on the whiteboard, React automatically re-renders (redraws) the component to show the new data.
State is data that belongs to a component and can change over time. When state changes, React re-renders the component:
React — useState Hook (Read-Only)
State vs Props
Feature
Props
State
Owned by
Parent component
The component itself
Can change?
No (read-only)
Yes (via setter function)
Analogy
Letter from parent
Whiteboard in your room
Triggers re-render?
Yes (when parent sends new props)
Yes (when setState is called)
Events
Events in React work like doorbell buttons. When someone presses the doorbell (the event), a specific action happens (the handler function runs). In React, you attach handler functions to elements, and they run when the user interacts with them.
React — Event Handling (Read-Only)
Common React Events
React Event
HTML Equivalent
When It Fires
onClick
onclick
User clicks an element
onChange
onchange
Input value changes
onSubmit
onsubmit
Form is submitted
onMouseEnter
onmouseenter
Mouse enters an element
onKeyDown
onkeydown
Key is pressed
Important pattern: In React, form inputs are "controlled" — their value comes from state, and onChange updates state. This means React is always the "single source of truth" for what the input shows.
📝 Chapter 6 Quiz
1. What is React?
A programming language
A CSS framework
A JavaScript library for building UIs
A database system
2. In JSX, what do you use instead of class?
cssClass
className
htmlClass
Class
3. Can a child component modify its props?
No — props are read-only
Yes — with this.props.set()
Yes — props are always writable
Only if the parent allows it
4. What does useState(0) return?
Just the value 0
An object with value and setter
A boolean
An array: [currentValue, setterFunction]
5. Which is the correct way to handle a click in React?