Introduction to React.js

  • Introduction to React.js is a beginner-friendly lesson that explains what React is, why it was created, how Single Page Applications work, the history of React, and how to set up a React development environment step by step.

  • What is React?

    React is a JavaScript library used to build user interfaces (UI), especially for single-page applications (SPAs) where the page does not reload again and again.

    👉 In simple words:
    React helps us build fast, interactive, and reusable web pages.

    Example:

    Think of a website like Facebook or Instagram:

    • Likes update instantly

    • Comments appear without page refresh

    • Content changes smoothly

    This smooth experience is possible because of React.

    Key Points:

    • Created by Facebook (Meta)

    • Used for frontend development

    • Focuses only on UI (View layer)

    Why React Was Created?

    Before React, developers used traditional JavaScript or jQuery to update the UI.

    Problems with Traditional JavaScript:

    • Full page reloads

    • Complex and messy code

    • Difficult to manage large applications

    • Slow performance

    Facebook’s Problem:

    Facebook had huge, dynamic UIs:

    • News feed updates

    • Likes, comments, notifications

    • Data changing every second

    Managing this with normal JavaScript became very hard.

    Solution:

    Facebook created React to:

    • Update only the changed part of UI

    • Make code reusable and manageable

    • Improve performance

    👉 React was created to solve UI complexity and performance issues.

    Key Features of React

    🔹 1. Component-Based Architecture

    React applications are built using components.

    A component is a small, reusable piece of UI.

    Example:

    • Header component

    • Footer component

    • Button component

    ✅ Benefits:

    • Reusable code

    • Easy maintenance

    • Clean structure

    🔹 2. Virtual DOM

    React uses a Virtual DOM, which is a lightweight copy of the real DOM.

    How it works:

    1. UI changes

    2. React updates Virtual DOM

    3. React compares old vs new Virtual DOM

    4. Only changed parts update in real DOM

    ✅ Result:

    • Faster performance

    • Less browser work

    🔹 3. JSX (JavaScript XML)

    JSX allows writing HTML inside JavaScript.

    <h1>Hello React</h1>

    ✅ Benefits:

    • Easy to read

    • Combines logic and UI

    • Less code

    🔹 4. One-Way Data Binding

    Data flows in one direction (Parent → Child).

    ✅ Benefits:

    • Easy debugging

    • Better control over data

    • Predictable behavior

    🔹 5. Reusability

    Once you create a component, you can use it again and again.

    Example:

    • Same button used in multiple pages

    🔹 6. Huge Ecosystem & Community

    • Libraries

    • Tools

    • Tutorials

    • Job opportunities

    React vs Traditional JavaScript

    Feature

    React

    Traditional JavaScript

    Page Reload

    No reload

    Full reload

    DOM Updates

    Virtual DOM

    Real DOM

    Code Structure

    Component-based

    Script-based

    Performance

    Fast

    Slower

    Reusability

    High

    Low

    Maintenance

    Easy

    Difficult

    Simple Explanation:

    👉 React updates only what changes,
    👉 JavaScript often updates everything.

    Where React Is Used in Industry?

    React is used by top tech companies worldwide.

    Famous Companies Using React:

    • Facebook

    • Instagram

    • Netflix

    • WhatsApp Web

    • Airbnb

    • Uber

    • Amazon

    • Flipkart

    Use Cases:

    • Social media apps

    • E-commerce websites

    • Dashboards

    • Admin panels

    • Single-page applications

    • Progressive web apps

    Who Can Learn React?

    React is suitable for many types of learners.

    👩‍🎓 Students:

    • College students

    • IT beginners

    • Final year project students

    👨‍💻 Developers:

    • JavaScript developers

    • Frontend developers

    • Full-stack developers

    Prerequisites:

    • Basic HTML

    • Basic CSS

    • Basic JavaScript

    👉 You don’t need to be an expert in JavaScript to start React.

    Advantages of React

    ✅ 1. Fast Performance

    • Virtual DOM makes apps faster

    ✅ 2. Reusable Components

    • Saves development time

    ✅ 3. Easy Maintenance

    • Small components = easy updates

    ✅ 4. Strong Community Support

    • Huge learning resources

    • Easy problem solving

    ✅ 5. SEO Friendly

    • Supports server-side rendering

    ✅ 6. Industry Demand

    • High-paying jobs

    • Popular skill worldwide

    Single Page Applications (SPA)

    What is a Single Page Application (SPA)?

    A Single Page Application (SPA) is a web application that loads only ONE HTML page and updates the content without reloading the entire page.

    👉 In SPA:

    • Page loads once

    • Only data changes

    • User gets smooth and fast experience

    Simple Example:

    Think about Gmail:

    • Clicking emails does NOT reload the page

    • Content changes instantly

    • Feels like a mobile app

    👉 That’s a Single Page Application.

    How Traditional Websites Work (Before SPA)

    Traditional websites are called Multi Page Applications (MPA).

    What happens in MPA?

    1. User clicks a link

    2. Browser requests a new HTML page

    3. Server sends a new page

    4. Entire page reloads

    ⛔ Problems:

    • Slower

    • Poor user experience

    • More server load

    SPA vs Multi Page Applications (MPA)

    Feature

    SPA

    MPA

    Page Load

    Single time

    Every page

    Page Reload

    No reload

    Full reload

    Speed

    Very fast

    Slower

    User Experience

    Smooth (App-like)

    Traditional

    Server Requests

    Less

    More

    Frontend Logic

    Heavy

    Light

    SEO

    Needs setup

    SEO-friendly by default

    Easy Explanation:

    • SPA feels like a mobile app

    • MPA feels like a traditional website

    How Navigation Works in SPA

    In SPA:

    • Clicking links does NOT reload the page

    • JavaScript changes content dynamically

    • URLs still change using client-side routing

    Example:

    /home → /profile → /settings

    But the page is still the same HTML file.

    How React Enables SPA Architecture

    React is perfect for building SPAs.

    🔹 1. Component-Based Structure

    React breaks UI into components:

    • Navbar

    • Sidebar

    • Content area

    Only the required component updates.

    🔹 2. Virtual DOM

    React updates only the changed part of the UI.

    Example:

    • Change username

    • React updates only username, not entire page

    🔹 3. React Router (Client-Side Routing)

    React uses React Router to manage navigation.

    Example:

    <Route path="/about" element={<About />} />

     👉 No server request
    👉 No page reload
    👉 Instant navigation

    🔹 4. API-Based Data Fetching

    React SPAs fetch data from APIs:

    • REST API

    • GraphQL

    Server sends data (JSON), not full pages.

    🔹 5. State Management

    React manages application state efficiently:

    • useState

    • useContext

    • Redux

    This keeps UI updated automatically.

    Real-Life Analogy for SPA

    📱 Mobile App Example:

    • Instagram app

    • Pages change without closing the app

    👉 SPA works the same way, but in the browser.

    Advantages of SPA

     ✅ Faster loading
    ✅ Better user experience
    ✅ Less server load
    ✅ App-like feel
    ✅ Smooth navigation

    Disadvantages of SPA (Important for Students)

     ❌ Initial load can be heavy
    ❌ SEO needs configuration
    ❌ JavaScript dependency

    👉 React solves many of these with:

    • Code splitting

    • Server-side rendering

    • SEO tools

    Final Summary

    • SPA loads once

    • Content updates dynamically

    • React makes SPAs easy and powerful

    • Used in modern web apps

    History of React

    React is a JavaScript library that was created to solve complex user interface problems faced by large web applications.

    React did not appear suddenly. It was developed because companies like Facebook needed a better way to manage fast-changing data and UI.

    👉 In simple words:
    React was created to handle dynamic, large-scale applications efficiently.

    Background:

    • Early websites were simple and static

    • As web apps became complex, UI management became difficult

    • Facebook needed a fast and scalable UI solution

    Who Created React?

    React was created by Jordan Walke, a software engineer working at Facebook (Meta).

    Why Facebook Created React?

    Facebook faced problems such as:

    • Frequently updating news feeds

    • Real-time likes and comments

    • Notifications changing every second

    Managing these with traditional JavaScript was:
    ❌ slow
    ❌ difficult
    ❌ hard to maintain

    👉 Jordan Walke developed React to solve these issues.

    Key Points:

    • Created at Facebook

    • Initially used internally

    • Later shared with the public as open source

    Evolution of React

    React evolved step-by-step over the years to become what it is today.

    Early Stage:

    • Only class components

    • Limited features

    • Harder for beginners

    Growth Stage:

    • Performance improvements

    • Better architecture

    • More developer-friendly features

    Modern React:

    • Functional components

    • Hooks

    • Better performance

    • Easier to learn and maintain

    👉 React continuously improves based on developer needs and industry demands.

    Major React Versions and Changes

    🔹 React 0.3 – 0.13 (2013–2015)

    First public versions of React.

    Key Features:

    • Component-based architecture

    • Virtual DOM

    • JSX support

    Limitations:

    • Only class components

    • Complex lifecycle methods

    🔹 React 15 (2016)

    First stable and widely used version.

    Improvements:

    • Better performance

    • Improved error handling

    • Stable API

    Result:

    • React became popular in the industry

    🔹 React 16 (2017)

    Major update with Fiber architecture.

    New Features:

    • Faster rendering

    • Error Boundaries

    • Fragments

    • Portals

    👉 This version significantly improved performance.

    🔹 React 16.8 (2019) – Hooks Introduction 🎣

    Most important update in React history.

    Introduced:

    • useState

    • useEffect

    • useContext

    Benefits:

    • Use state without class components

    • Less code

    • Cleaner logic

    👉 Functional components became the standard.

    🔹 React 17 (2020)

    Focused on stability and smooth upgrades.

    Key Changes:

    • No major new features

    • Easier migration

    • Improved event handling

    👉 Helped large apps upgrade safely.

    🔹 React 18 (2022)

    Modern React with performance improvements.

    Key Features:

    • Concurrent rendering

    • Automatic batching

    • Improved Suspense

    👉 React apps became faster and more responsive.

    📌 Final Summary

    React:

    • Was created by Faceboo

    • Solved complex UI problems

    • Evolved over time

    • Became easier and faster

    • Is now a modern, industry-ready library

    Development Environment Setup

    Before starting React development, we need to prepare our system with the required tools.

    A development environment is the setup where developers:

    • Write code

    • Run applications

    • Test projects

    Tools Required for React Development:

    • Computer (Windows / macOS / Linux)

    • Code Editor (VS Code recommended)

    • Web Browser (Chrome)

    • Node.js and npm

    👉 Proper setup ensures smooth development and fewer errors.

    Node.js Overview

    Node.js is a JavaScript runtime that allows JavaScript to run outside the browser.

    👉 In simple words:
    Node.js lets us use JavaScript on our computer, not just in the browser.

    Why React Needs Node.js?

    • To install React libraries

    • To run development servers

    • To manage packages using npm

    Key Points:

    • Built on Chrome’s V8 engine

    • Very fast

    • Widely used in modern web development

    Installing Node.js and npm

    What is npm?

    npm (Node Package Manager) is used to:

    • Install libraries

    • Manage project dependencies

    Installation Steps:

    1. Visit Node.js official website

    2. Download LTS version

    3. Install Node.js

    4. npm is installed automatically

    Verify Installation:

    node -v

    npm -v

    If versions appear, installation is successful ✅

    4️⃣ Project Setup Using Create React App (CRA)

    Create React App (CRA) is a tool to quickly create a React project with default configuration.

    👉 Best for beginners.

    Steps to Create Project:

    npx create-react-app my-app

    cd my-app

    npm start


    Features of CRA:

    • Zero configuration

    • Webpack setup included

    • Babel support

    • Development server ready

    Output:

    • App runs at http://localhost:3000

    Project Setup Using Vite

    Vite is a modern build tool that provides faster development than CRA.

    👉 Best for modern and large applications.

    Steps to Create Project:

    npm create vite@latest my-app

    cd my-app

    npm install

    npm run dev


    Why Vite is Faster?

    • Uses native ES modules

    • Faster hot reload

    • Lightweight bundling

    Output:

    • App runs at http://localhost:5173

    CRA vs Vite (Quick Comparison)

    Feature

    CRA

    Vite

    Speed

    Slower

    Very Fast

    Configuration

    Zero config

    Minimal config

    Modern Support

    Limited

    Excellent

    Recommended For

    Beginners

    Modern projects


    Understanding Project Folder Structure

    Typical React Project Structure:

    my-app/

    ├── node_modules/

    ├── public/

    │   └── index.html

    ├── src/

    │   ├── App.jsx

    │   ├── main.jsx / index.js

    │   ├── components/

    │   ├── assets/

    │   └── styles/

    ├── package.json

    └── vite.config.js / webpack config


    Folder Explanation:

    🔹 node_modules

    • Contains all installed packages

    • Automatically generated

    • Do not modify manually

    🔹 public

    • Contains static files

    • index.html is the main HTML file

    🔹 src

    • Main React code lives here

    • Components, styles, logic

    🔹 App.jsx

    • Root component

    • UI starts here

    🔹 main.jsx / index.js

    • Entry point of React app

    • Renders App component

    🔹 package.json

    • Project details

    • Dependencies

    • Scripts

    📌 Final Summary

    • Node.js is required for React

    • npm manages packages

    • CRA is beginner-friendly

    • Vite is fast and modern

    • Folder structure helps organize code