React Introduction

React: Environment Setup & Introduction

⚛️ The Library for Web and Native Interfaces

CSCI 4513 - Advanced Web Programming

Week 11, Lecture 18

Welcome to React!

Before We Begin:

Make sure you have a solid understanding of JavaScript!

At the end of the day, React IS vanilla JavaScript. Being able to navigate JavaScript is paramount to building successful React projects.

Remember: React = JavaScript + Special Powers

Today's Learning Objectives

The Journey Ahead

📚 Structure: Lessons with assignments (just like before)

💻 Interactive examples: CodeSandbox demonstrations

🛠️ Projects: Build portfolio-worthy applications

Honest Talk: Learning React can be frustrating

You might think "I can do this with vanilla JavaScript, why React?"

But... Remember that dreaded Todo List project? In React, it takes less time to build!

What is React?

Official definition:

"The library for web and native interfaces"

More specifically:

React is a JavaScript library that provides powerful primitives (built-in functions/modules) to build user interfaces of varying complexities.

JavaScript Library vs Framework

Library

Collection of pre-written code

You call the library

Example: React, jQuery

  • More flexibility
  • You're in control
  • Pick and choose what to use

Framework

Complete structure for your app

Framework calls your code

Example: Angular, Vue

  • More opinionated
  • Sets the structure
  • All-in-one solution

Why Learn React?

One of the most powerful, widely used JavaScript libraries

♻️ Reusable components - Write once, use everywhere

👥 Large community - Tons of support, resources, packages

🎨 Not opinionated - No forced patterns or structures

📉 Smaller learning curve - Especially with good JS knowledge

🚀 Makes code scalable, readable, and efficient

React's Popularity

Used by major companies worldwide:

Facebook

Instagram

Netflix

Airbnb

Uber

Discord

The React Documentary

Highly Recommended Watch!

React.js: The Documentary

An incredible hour-long film about the origin and impact of React

🔗 Link: youtube.com/watch?v=8pDqJVdNa44

What you'll learn:

  • How React was created at Facebook
  • Why it revolutionized frontend development
  • The philosophy behind React's design
  • Stories from the core team

Setting Up a React Environment

Many Paths to Choose From

Ways to start using React:

  • CDN script tags (simple, limited)
  • Vite (our choice! ⚡)
  • Gatsby (static sites)
  • Next.js (full-stack framework)
  • ~~Create React App~~ (Deprecated 2023)

Why Do We Need Toolchains?

Can't we just write React directly?

Setting up React from scratch requires:

  • 📦 Package Management (npm, Yarn)
  • 📦 Module Bundling (Webpack, Parcel)
  • 🔧 Compilation (Babel)
  • ⚛️ React itself
  • 🛠️ Development server
  • 🔥 Hot module replacement
  • ...and much more!

The short answer: Yes, you can set it up yourself. But it's hard and time-consuming!

Enter Vite ⚡

Vite (French for "fast") is a modern build tool

Why Vite?

  • ⚡ Lightning-fast development server
  • 🔥 Hot Module Replacement (HMR)
  • 📦 Optimized builds
  • ⚙️ Minimal configuration needed
  • 🎯 Perfect for React development
  • ✨ Excellent developer experience

Creating Your First React App

One command to rule them all:

npm create vite@latest my-first-react-app -- --template react

What happens:

  1. You may be asked to install create-vite package (type 'y')
  2. Project folder is created with React template
  3. Dependencies are installed
  4. Development server starts automatically
  5. Your browser opens to localhost:5173

Using an Existing Repo

If you already have a GitHub repo cloned:

# Navigate to your repo
cd my-existing-repo

# Create Vite project in current directory
npm create vite@latest . -- --template react

The . tells Vite to use the current directory instead of creating a new one

This keeps your existing git setup intact!

Project Structure

my-first-react-app/
├── node_modules/      # Dependencies (don't touch!)
├── public/            # Static assets (images, icons)
├── src/               # Your code lives here!
│   ├── App.css
│   ├── App.jsx        # Main App component
│   ├── index.css
│   └── main.jsx       # Entry point
├── .gitignore
├── index.html         # HTML template
├── package.json       # Project config & dependencies
└── vite.config.js     # Vite configuration

The public Folder

What goes here:

  • 📸 Images that don't change
  • 🎨 Icons and favicons
  • 📄 Static files (robots.txt, manifest.json)
  • 🎵 Audio/video files

Important: Files here are served as-is, they're not processed by Vite

The src Folder

This is where the magic happens!

  • 📝 main.jsx - Entry point of your application
  • ⚛️ App.jsx - Your main App component
  • 🎨 CSS files - Component styles
  • 🧩 Components - Reusable UI pieces (you'll create these!)

Rule of thumb: All your React code goes in src/

Understanding main.jsx

import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App.jsx";
import "./index.css";

createRoot(document.getElementById("root")).render(
  <StrictMode>
    <App />
  </StrictMode>,
);

What's happening:

  1. Import React utilities and your App component
  2. Import CSS styles
  3. Find the "root" element in index.html
  4. Render your App component inside it
  5. StrictMode helps catch bugs during development

That Weird Syntax...

<StrictMode>
  <App />
</StrictMode>

This is JSX!

(JavaScript XML)

JSX lets you write HTML-like code in JavaScript

Don't worry if it looks strange - you'll be writing it fluently soon!

React Developer Tools

Essential Chrome/Firefox Extension

Why You Need This:

  • 🔍 Inspect React component hierarchy
  • 🎯 View component props and state
  • ⚡ Make live changes for debugging
  • 📊 Track component performance
  • 🛠️ Essential for React development!

Install it now! Get comfortable with it early - it's invaluable for debugging

Installing React DevTools

Two options:

Chrome

Search "React Developer Tools" in Chrome Web Store

Click "Add to Chrome"

Firefox

Search "React Developer Tools" in Firefox Add-ons

Click "Add to Firefox"

After installing, you'll see two new tabs in DevTools:

⚛️ Components | ⚡ Profiler

Practice Time!

Your First React Challenge

Task: Clean up your my-first-react-app project

Goal: Display "Hello, World!" instead of the default Vite page

Hints:

  • Look in src/App.jsx
  • Find the return statement
  • Replace the content inside with your own!

A Note on Create React App

⚠️ Deprecated in 2023

Create React App (CRA) was the official way to start React projects from 2016-2023

You'll see it mentioned in many tutorials and guides

Important: It's no longer recommended for new projects

If you see "npx create-react-app":

Use Vite instead! It's faster and more modern

Running Your React App

Essential npm commands:

# Start development server
npm run dev

# Build for production
npm run build

# Preview production build
npm run preview

Development workflow:

  1. Run npm run dev
  2. Open localhost:5173 in browser
  3. Edit files in src/
  4. Save and watch changes appear instantly!

Hot Module Replacement (HMR)

One of Vite's superpowers! ⚡

What is HMR?

When you save a file, changes appear in the browser instantly without refreshing the page

  • ✅ Preserves application state
  • ✅ Updates only what changed
  • ✅ Lightning-fast feedback
  • ✅ Amazing developer experience

Assignment

📖 Read: React official website introduction

📖 Read: History of React article

📖 Read: FreeCodeCamp - Libraries vs Frameworks

📖 Read: Main advantages of using React

📖 Read: Vite's Getting Started Page

🎥 Watch: React.js: The Documentary (highly recommended!)

🔧 Install: React Developer Tools extension

💻 Do: Create your first React app with Vite

✏️ Practice: Make it display "Hello, World!"

Knowledge Check

❓ What is the purpose of React?

❓ What are the benefits of using React?

❓ What's the difference between a library and a framework?

❓ What are some ways to start a new React project?

❓ Why should we use pre-made toolchains?

❓ What is Vite and why would we use it?

❓ What command creates a new React project with Vite?

❓ What goes in the public folder?

❓ What goes in the src folder?

❓ Why are React Developer Tools useful?

Today's Key Takeaways

React is a JavaScript library for building UIs

Libraries provide tools; frameworks provide structure

Vite is the modern, fast way to start React projects

npm create vite@latest gets you started in seconds

src/ is where your code lives

React DevTools are essential for debugging

HMR gives instant feedback while coding

Next Class

React Fundamentals

🧩 Deep dive into React components

📝 Understanding JSX syntax

📦 Props and component composition

🎨 Styling React components

🔄 Component lifecycle basics

Get ready to build real React apps!