Skip to content

Introduction to ReactJS

🌟 What is ReactJS?

ReactJS is a JavaScript library (not a full-fledged framework) that helps developers build fast and dynamic user interfaces (UIs), especially for Single Page Applications (SPAs)β€”web apps that update content without reloading the entire page.

πŸ” Real-world analogy: Think of React as LEGO blocks (components). You can create small pieces (like buttons, cards, menus), and then assemble them into a complete website or app UI.


πŸ“ˆ Why Use ReactJS?

πŸš€ Benefits of ReactJS:

FeatureWhat It Means
🧱 Component-BasedReuse small pieces of UI across the app
⚑ Fast RenderingUses Virtual DOM for performance
πŸ” One-Way Data FlowMakes code easier to debug
πŸ”„ Real-time UpdatesUI updates automatically with data changes
πŸ› οΈ Developer ToolsExcellent tooling and community support

πŸ” Key Concepts in ReactJS – In Depth


1️⃣ Components – The Building Blocks

A component is a self-contained piece of the UI. It can be as small as a button or as large as an entire webpage section.

πŸ§ͺ Example:

function Header() {
return <h1>Welcome to React</h1>;
}

You can reuse this Header anywhere in your app!

πŸ”„ Two types of components:

  • Functional Components (modern and simple)
  • Class Components (older style, but still used)

2️⃣ JSX – JavaScript + XML

JSX allows writing HTML-like syntax in JavaScript.

πŸ“„ Example:

const title = <h2>Hello MCA Students</h2>;

βœ… JSX gets compiled to React.createElement(...) calls behind the scenes.


3️⃣ Props – Component Communication

Props (short for properties) are used to pass data from one component to another.

πŸ“Œ Example:

function Greeting(props) {
return <h2>Hello, {props.name}</h2>;
}

// Usage:
<Greeting name="Anjali" />

βœ… Props are read-only. They help make components dynamic.


4️⃣ State – Managing Data in Components

State is used to store values that can change over time, like user input or counter values.

βœ… Example using useState Hook:

import { useState } from 'react';

function Counter() {
const [count, setCount] = useState(0); // Initialize state

return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click Me</button>
</div>
);
}

βœ… useState() is a Hookβ€”a special function to use state in functional components.


5️⃣ Virtual DOM – Speed Booster

Instead of changing the real DOM directly (which is slow), React uses a Virtual DOMβ€”a lightweight copy of the real DOM.

βš™οΈ How it works:

  • React creates a virtual copy of the UI.
  • When data changes, React compares the old and new virtual DOM using Diffing Algorithm.
  • It updates only the changed parts in the actual DOM.

βœ… Result: High performance and fast user experience.


6️⃣ React Lifecycle (for Class Components)

Lifecycle methods allow you to run code at specific points in a component’s life.

⏱ Common methods:

  • componentDidMount() – After component is added to DOM
  • componentDidUpdate() – After data (props or state) changes
  • componentWillUnmount() – Before component is removed

In modern React, you achieve similar results using useEffect() in functional components.


🧠 React Folder Structure – Organized Codebase

my-app/
β”œβ”€β”€ public/ # index.html (root of your app)
β”œβ”€β”€ src/
β”‚ β”œβ”€β”€ App.js # Main component
β”‚ β”œβ”€β”€ index.js # Entry point for rendering React
β”‚ └── components/ # All reusable components
β”œβ”€β”€ package.json # Project metadata and dependencies

βœ… npx create-react-app my-app sets up everything automatically.


🌐 Real World Apps Built with React

App/WebsiteReact Usage
FacebookFeeds, notifications, dynamic chat
NetflixFast rendering UI for TV, movies
InstagramRealtime image feeds, stories
WhatsApp WebMessaging interface
Zomato, SwiggyDynamic filtering, menus, cart updates

πŸ§ͺ React Mini Projects for Practice

  1. πŸ“‹ To-Do List App – Add, delete, and mark tasks as complete.
  2. 🏫 Student Portal – Show subjects, attendance, and marks using components.
  3. πŸ“Š Dashboard – Cards, graphs, tables using React and Bootstrap.
  4. πŸ“… Calendar/Event App – Show events with dynamic date handling.
  5. πŸ›’ Shopping Cart – Use props/state to add/remove products.

πŸ” ReactJS vs AngularJS vs VueJS

FeatureReactJSAngularJSVueJS
TypeLibraryFrameworkFramework
Learning CurveEasySteepModerate
DOMVirtual DOMReal DOMVirtual DOM
FlexibilityHighModerateModerate
Data FlowOne-wayTwo-way bindingTwo-way

πŸ“š Summary

ConceptDescription
ComponentReusable block of UI
JSXHTML + JS in one syntax
PropsPass data from parent to child
StateInternal, dynamic data
HooksModern way to use features in functional components
Virtual DOMReact’s fast UI rendering system