π 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:
Feature | What It Means |
---|---|
π§± Component-Based | Reuse small pieces of UI across the app |
β‘ Fast Rendering | Uses Virtual DOM for performance |
π One-Way Data Flow | Makes code easier to debug |
π Real-time Updates | UI updates automatically with data changes |
π οΈ Developer Tools | Excellent 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 DOMcomponentDidUpdate()
β After data (props or state) changescomponentWillUnmount()
β 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/Website | React Usage |
---|---|
Feeds, notifications, dynamic chat | |
Netflix | Fast rendering UI for TV, movies |
Realtime image feeds, stories | |
WhatsApp Web | Messaging interface |
Zomato, Swiggy | Dynamic filtering, menus, cart updates |
π§ͺ React Mini Projects for Practice
- π To-Do List App β Add, delete, and mark tasks as complete.
- π« Student Portal β Show subjects, attendance, and marks using components.
- π Dashboard β Cards, graphs, tables using React and Bootstrap.
- π Calendar/Event App β Show events with dynamic date handling.
- π Shopping Cart β Use props/state to add/remove products.
π ReactJS vs AngularJS vs VueJS
Feature | ReactJS | AngularJS | VueJS |
---|---|---|---|
Type | Library | Framework | Framework |
Learning Curve | Easy | Steep | Moderate |
DOM | Virtual DOM | Real DOM | Virtual DOM |
Flexibility | High | Moderate | Moderate |
Data Flow | One-way | Two-way binding | Two-way |
π Summary
Concept | Description |
---|---|
Component | Reusable block of UI |
JSX | HTML + JS in one syntax |
Props | Pass data from parent to child |
State | Internal, dynamic data |
Hooks | Modern way to use features in functional components |
Virtual DOM | Reactβs fast UI rendering system |