Introduction to React
React is a JavaScript library for building user interfaces, developed by Facebook. It allows developers to build reusable UI components and manage the state of their applications efficiently.Why React?
- Component-Based: Build encapsulated components that manage their own state, then compose them to make complex UIs.
- Declarative: Design simple views for each state in your application, and React will efficiently update and render just the right components when your data changes.
- Virtual DOM: React uses a virtual DOM to improve performance. It calculates the minimal set of changes needed to update the actual DOM.
- Ecosystem: A massive ecosystem of libraries, tools, and community support.
How React Works Under the Hood
Understanding what happens behind the scenes helps you write better React code and debug issues effectively.JSX Transformation
JSX is not valid JavaScript—it needs to be transformed before the browser can execute it. Tools like Babel or SWC (used by Vite) compile JSX into regular JavaScript function calls.createElement calls:
Modern React (17+): Uses
jsx-runtime instead of React.createElement, which is slightly more efficient and doesn’t require importing React in every file.The Virtual DOM Explained
The Virtual DOM is a lightweight JavaScript representation of the actual DOM. React uses it to minimize expensive DOM operations.| Direct DOM Manipulation | React’s Approach |
|---|---|
| Every change = immediate DOM update | Changes batched together |
| DOM operations are slow (layout, paint) | Virtual DOM operations are fast (JS objects) |
| May update more than needed | Only updates what changed (diffing) |
The Reconciliation Algorithm
React’s diffing algorithm has O(n) complexity through these heuristics:- Different element types → Rebuild entire subtree
- Same element type → Update only changed attributes
- Lists with keys → Efficiently reorder/update items
Setting Up a React Project
The easiest way to start a new React project is using Vite (recommended) or Create React App.Using Vite
JSX (JavaScript XML)
JSX is a syntax extension for JavaScript. It looks like HTML, but it allows you to write HTML structures within JavaScript code.Rules of JSX
-
Return a Single Root Element: You must wrap adjacent elements in a parent tag or a Fragment (
<>...</>). -
Close All Tags: Self-closing tags must end with a slash (e.g.,
<img />,<br />). -
camelCase Properties: HTML attributes become camelCase in JSX.
class->classNameonclick->onClicktabindex->tabIndex
-
JavaScript Expressions: You can embed any valid JavaScript expression inside curly braces
{}.
Conditional Rendering
You can use JavaScript operators likeif or the ternary operator inside JSX.
Ternary Operator
Logical AND (&&)
Render something only if a condition is true:Logical OR (||) and Nullish Coalescing (??)
Provide fallback values:Early Returns
For complex conditions, use early returns for cleaner code:Styling in React
React offers multiple ways to style components:Inline Styles
Inline styles use camelCase property names (
backgroundColor not background-color) and values are strings or numbers.CSS Modules
CSS Modules scope styles locally to the component:Conditional Classes
Common JSX Pitfalls
1. Forgetting to Return JSX
2. Using class Instead of className
3. Forgetting to Close Tags
4. Rendering Objects Directly
5. Incorrect Boolean Attributes
🎯 Practice Exercises
Exercise 1: Create a Profile Card
Exercise 1: Create a Profile Card
Create a
ProfileCard component that displays:- A profile image
- Name
- Bio
- A “Follow” button
Exercise 2: Conditional Badge
Exercise 2: Conditional Badge
Create a component that shows different status badges based on a
status prop:- “online” → Green badge
- “away” → Yellow badge
- “offline” → Gray badge
Exercise 3: Render a List of Items
Exercise 3: Render a List of Items
Given an array of products, render them as a list:
Summary
| Concept | Description |
|---|---|
| React | JavaScript library for building component-based UIs |
| Virtual DOM | Lightweight representation of the DOM for efficient updates |
| JSX | Syntax extension that lets you write HTML-like code in JavaScript |
| Babel/SWC | Compilers that transform JSX into React.createElement calls |
| Vite | Modern, fast build tool for React projects |
| Fragments | <>...</> to group elements without adding DOM nodes |
| Expressions | Use {} to embed JavaScript in JSX |
| Conditional Rendering | Ternary ? :, logical &&, or early returns |
Next Steps
In the next chapter, you’ll learn about Components & Props — how to break your UI into reusable pieces and pass data between them.