Skip to main content

βš›οΈ What is React?

πŸ“– Definition​

React is a JavaScript library for building user interfaces developed by Facebook (Meta). It uses component-based architecture for reusable UI and Virtual DOM for efficient screen updates. Declarative programming simplifies complex UI management.

🎯 Simple Analogy​

LEGO Blocks​

React Components = LEGO Blocks
β”œβ”€ Combine small blocks
β”œβ”€ Reuse same blocks multiple times
β”œβ”€ Build complex structures easily
└─ Replace individual blocks as needed

<House>
<Roof />
<Wall />
<Door />
<Window />
</House>

βš™οΈ Core Concepts​

Component Structure​

// Function component (modern approach)
function Welcome({ name }) {
return <h1>Hello, {name}!</h1>;
}

// Usage
<Welcome name="John" />

State Management​

import { useState } from 'react';

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

return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
</div>
);
}

useEffect Hook​

import { useState, useEffect } from 'react';

function UserProfile({ userId }) {
const [user, setUser] = useState(null);

useEffect(() => {
fetch(`/api/users/${userId}`)
.then(res => res.json())
.then(data => setUser(data));
}, [userId]); // Run when userId changes

return <div>{user?.name}</div>;
}

πŸ’‘ Key Examples​

List Rendering​

function TodoList() {
const [todos, setTodos] = useState([
{ id: 1, text: 'Exercise', done: false },
{ id: 2, text: 'Study', done: true }
]);

return (
<ul>
{todos.map(todo => (
<li key={todo.id}>
{todo.text}
</li>
))}
</ul>
);
}

Form Handling​

function LoginForm() {
const [formData, setFormData] = useState({
username: '',
password: ''
});

const handleChange = (e) => {
setFormData({
...formData,
[e.target.name]: e.target.value
});
};

const handleSubmit = (e) => {
e.preventDefault();
// API call...
};

return (
<form onSubmit={handleSubmit}>
<input name="username" onChange={handleChange} />
<input name="password" type="password" onChange={handleChange} />
<button type="submit">Login</button>
</form>
);
}

πŸ€” FAQ​

Q1. Why use React?​

// ❌ Vanilla JS (complex)
const button = document.createElement('button');
button.addEventListener('click', () => {
// Manual DOM manipulation
});

// βœ… React (simple)
function Counter() {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>{count}</button>;
}

Q2. What is JSX?​

A: JavaScript + XML syntax for React elements:

// JSX
const element = <h1 className="title">Hello</h1>;

// Converted to
React.createElement('h1', { className: 'title' }, 'Hello');

Q3. What are Hooks?​

A: Functions that let you use state and lifecycle in function components:

useState    // State management
useEffect // Side effects (API calls)
useContext // Context access
useRef // DOM access
useMemo // Memoization
useCallback // Function memoization

🎬 Summary​

  • Components: Reusable UI blocks
  • Virtual DOM: Efficient updates
  • JSX: Intuitive syntax
  • Hooks: Powerful function components

Build interactive applications with React! βš›οΈ