MODULE 3

Introduction to React

Understand what React is, why it exists, and how to get started

What is React?

React is a JavaScript library for building user interfaces using components. It makes it easier to create interactive, dynamic web applications by breaking them into reusable pieces.

Key Characteristics:

What React IS NOT

It's important to understand what React doesn't do:

Why React Exists

React was created to solve real problems in web development:

The Problem

Before React, managing complex UIs was difficult because:

React's Solution

SPA Concept (Single Page Application)

Most modern web applications are SPAs:

Traditional Multi-Page Application

User clicks link → Request goes to server → Server returns HTML → Page reloads

Single Page Application

User clicks link → JavaScript loads new content → Page updates instantly → No reload

Benefits of SPAs:

SPA vs traditional web architecture

How React Thinks (Components)

React is all about thinking in components. A component is a reusable piece of UI.

Component Example

Instead of thinking about individual buttons and labels, think about a "Button" component or a "Card" component:

// Traditional thinking
const button = document.getElementById("my-button");
button.textContent = "Click me";
button.addEventListener("click", () => {
  // do something
});

// React thinking

Benefits of Component-Based Thinking

Component Hierarchy

Components nest inside other components to form a tree structure:


  
Component tree structure

Development Environment Setup

Before you can use React, you need some tools installed.

Prerequisites

  1. Node.js: Download and install from nodejs.org
  2. Code Editor: VS Code (recommended)
  3. Terminal: Command line access

Verify Installation

node --version    # Should show v14.0.0 or higher
npm --version     # Should show version number

CRA vs Vite

There are two main ways to start a React project:

Create React App (CRA)

The official, beginner-friendly way to start a React project:

npx create-react-app my-app
cd my-app
npm start

Pros:

Cons:

Vite

A modern, faster alternative:

npm create vite@latest my-app -- --template react
cd my-app
npm install
npm run dev

Pros:

Cons:

💡 Recommendation: For beginners, start with Create React App for simplicity. As you get more comfortable, try Vite.

Your First React Application

Let's look at what a basic React app looks like.

File Structure

my-app/
├── public/
│   └── index.html
├── src/
│   ├── App.js
│   ├── App.css
│   ├── index.js
│   └── index.css
├── package.json
└── node_modules/

Entry Point (index.js)

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render();

Main App Component (App.js)

function App() {
  return (
    

Hello, React!

This is my first React app.

); } export default App;

HTML Template (public/index.html)



  
    React App
  
  
    

How It All Works Together

  1. Browser loads public/index.html
  2. index.js runs and mounts the React app into the element with id "root"
  3. App component renders
  4. React elements appear on the page
  5. When state changes, React re-renders affected components
React application flow

The React Mindset

Getting into the React mindset is crucial:

Think in Components

Break your UI into components first, then build them.

Manage State Carefully

State drives your UI. Whenever state changes, UI updates.

Keep Components Pure

Components should be predictable. Same props → same output.

Embrace Unidirectional Data Flow

Data flows from parent to child through props. This makes apps predictable.

⚠️ Important: React has a learning curve. Don't expect to understand everything immediately. Each module builds on previous concepts.

What's Next

In the next module, you'll learn:

Key Takeaways