Introduction to React
What is React and why use it for building user interfaces
What is React?
React is a JavaScript library for building user interfaces, created by Facebook (now Meta) in 2013. It's the most popular frontend library in the world, used by companies like Netflix, Airbnb, Instagram, and thousands more.
React makes it easy to create interactive UIs by breaking your application into small, reusable pieces called components. Each component manages its own state and renders its own UI.
Why Choose React?
- 🧩 Component-Based: Build encapsulated components that manage their own state, then compose them to make complex UIs.
- ⚡ Virtual DOM: React uses a virtual DOM to efficiently update only what's changed, making your apps fast.
- 🔄 Declarative: Describe what your UI should look like, and React handles the updates when data changes.
- 📱 Learn Once, Write Anywhere: Use React for web apps, React Native for mobile apps, and more.
- 🌍 Huge Ecosystem: Massive community, thousands of libraries, and excellent tooling.
How React Works
Traditional web development involves manually manipulating the DOM (Document Object Model) with JavaScript. This can be slow and error-prone. React takes a different approach:
❌ Traditional Approach
Directly manipulate the DOM, track changes manually, update elements imperatively
✓ React Approach
Describe your UI declaratively, React calculates the minimal DOM changes needed
Your First React Code
Here's what a simple React component looks like:
function Welcome() {
return (
<div>
<h1>Hello, React!</h1>
<p>Welcome to your first React component.</p>
</div>
);
}
export default Welcome;
This is a functional component. It's just a JavaScript function that returns JSX (HTML-like syntax). We'll learn more about JSX in the next lesson.
React vs Other Frameworks
| Feature | React | Vue | Angular |
|---|---|---|---|
| Type | Library | Framework | Framework |
| Learning Curve | Moderate | Easy | Steep |
| Flexibility | High | Medium | Low |
| Job Market | Largest | Growing | Enterprise |
Setting Up React
The easiest way to start a new React project is with Vite or Next.js:
# Using Vite (recommended for learning)
npm create vite@latest my-react-app -- --template react
# Using Next.js (recommended for production)
npx create-next-app@latest my-next-app
# Navigate to your project
cd my-react-app
# Install dependencies
npm install
# Start development server
npm run dev
💡 Prerequisites
Before diving into React, make sure you're comfortable with:
- • JavaScript ES6+: Arrow functions, destructuring, spread operator, modules
- • HTML & CSS: Basic web structure and styling
- • npm/Node.js: Package management basics
Key Concepts We'll Cover
📦 Components
Reusable building blocks of React apps
🔧 Props
Passing data between components
🎣 Hooks
useState, useEffect, and more
🔄 State Management
Managing application data