React.js_ Introduction and How It Works
React.js_ Introduction and How It Works
Inefficiencies in
Difficulty in
Manual Updates Traditional Web
Reusability
Development
Sluggish Applications
Slow Direct
Manipulation
Performance Issues
// Adding a student
function addStudent(name, rollNo) {
const studentItem = document.createElement('li');
studentItem.textContent = `${name} (${rollNo})`;
studentItem.appendChild(deleteBtn);
studentList.appendChild(studentItem);
}
React Approach:
return (
<div>
<h1>Student List</h1>
<form onSubmit={addStudent}>
<input
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Name"
/>
<input
value={rollNo}
onChange={(e) => setRollNo(e.target.value)}
placeholder="Roll No"
/>
<button type="submit">Add Student</button>
</form>
<ul>
{students.map((student, index) => (
<li key={index}>
{student.name} ({student.rollNo})
<button onClick={() => deleteStudent(index)}>Delete</button>
</li>
))}
</ul>
</div>
);
}
2. Component-Based Architecture
• Everything in React is a component
• Components can be:
• Function components (modern approach)
• Class components (older approach)
• Components can be nested and reused
• Each component manages its own state and props
State and
Props Function
Management Components
Highlights the Represent the
component's modern approach
control over its to building
own data and components in
configuration. React.
React
Components
Class
Reusability
Components
Emphasizes the
efficiency of using Reflect the older,
components traditional method
multiple times. of creating
components.
Nesting
Illustrates the
ability to embed
components within
one another.
Initial Render:
JSX → React.createElement() → Virtual DOM → Real DOM
Updates:
State Change → Re-render Component → New Virtual DOM →
Diffing with Previous Virtual DOM → Minimal Real DOM Updates
4. JSX
• JSX is a syntax extension that looks like HTML but is actually JavaScript
• Gets transformed into React.createElement() calls during build
• Allows you to write UI components in a familiar HTML-like syntax
• Example: <div className="container">Hello {name}!</div>