Learn React fundamentals, JSX syntax, components, and the virtual DOM. Understand why React revolutionized frontend development and build your first interactive components.
React is a powerful JavaScript library created by Facebook for building user interfaces. Since its release in 2013, React has revolutionized how developers approach frontend development, becoming the most popular choice for modern web applications.
Think of React as a sophisticated tool for building interactive, dynamic websites. While HTML gives you static structure and CSS provides styling, React brings your pages to life with dynamic content, user interactions, and real-time updates.
Companies like Facebook, Netflix, Instagram, Airbnb, and countless others rely on React to power their user interfaces. By learning React, you're mastering the same technology used by the world's most successful web applications.
In this comprehensive lesson, you'll discover what makes React special, learn its fundamental concepts, and write your first React code using JSX.
React is a JavaScript library for building user interfaces, particularly web applications. It was created by Jordan Walke at Facebook and released as open-source in 2013.
What Makes React Special:
Before React (Traditional JavaScript):
1// Complex DOM manipulation
2const button = document.getElementById('myButton');
3button.addEventListener('click', function() {
4 const counter = document.getElementById('counter');
5 const currentValue = parseInt(counter.textContent);
6 counter.textContent = currentValue + 1;
7});With React:
1function Counter() {
2 const [count, setCount] = useState(0);
3 return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
4}Key Benefits:
JSX (JavaScript XML) is a syntax extension for JavaScript that allows you to write HTML-like code within JavaScript. It's the foundation of React component development.
What JSX Looks Like:
1const element = <h1>Hello, React!</h1>;
2
3const greeting = (
4 <div className="welcome">
5 <h1>Welcome to React!</h1>
6 <p>Let's build something amazing together.</p>
7 </div>
8);JSX vs HTML - Key Differences:
1. Attribute Names (JSX uses camelCase):
1// HTML
2<div class="container" tabindex="0"></div>
3
4// JSX
5<div className="container" tabIndex="0"></div>2. Inline Styles (JSX uses objects):
1// HTML
2<div style="color: red; font-size: 16px;"></div>
3
4// JSX
5<div style={{color: 'red', fontSize: '16px'}}></div>3. JavaScript Expressions (JSX uses curly braces):
1const name = 'React';
2const element = <h1>Hello, {name}!</h1>;
3
4const user = { firstName: 'John', lastName: 'Doe' };
5const greeting = <h1>Hello, {user.firstName} {user.lastName}!</h1>;4. Self-Closing Tags (JSX requires explicit closing):
1// HTML
2<img src="image.jpg">
3<input type="text">
4
5// JSX
6<img src="image.jpg" />
7<input type="text" />Why JSX is Powerful:
Components are the heart of React applications. They're reusable pieces of UI that can manage their own state and lifecycle.
Two Types of Components:
1. Function Components (Modern, Recommended):
1function Welcome(props) {
2 return <h1>Hello, {props.name}!</h1>;
3}
4
5// Or using arrow functions
6const Welcome = (props) => {
7 return <h1>Hello, {props.name}!</h1>;
8};2. Class Components (Legacy, Still Used):
1class Welcome extends React.Component {
2 render() {
3 return <h1>Hello, {this.props.name}!</h1>;
4 }
5}Using Components:
1function App() {
2 return (
3 <div>
4 <Welcome name="Alice" />
5 <Welcome name="Bob" />
6 <Welcome name="Charlie" />
7 </div>
8 );
9}Component Best Practices:
Real-World Example:
1// Reusable Button component
2function Button({ children, onClick, type = 'primary' }) {
3 const className = `btn btn-${type}`;
4 return (
5 <button className={className} onClick={onClick}>
6 {children}
7 </button>
8 );
9}
10
11// Using the Button component
12function App() {
13 return (
14 <div>
15 <Button type="primary" onClick={() => alert('Primary!')}>
16 Primary Button
17 </Button>
18 <Button type="secondary" onClick={() => alert('Secondary!')}>
19 Secondary Button
20 </Button>
21 </div>
22 );
23}The Virtual DOM is one of React's most important innovations. It's what makes React applications fast and efficient.
What is the Virtual DOM?
The Virtual DOM is a JavaScript representation of the real DOM (Document Object Model). It's a programming concept where a "virtual" representation of the UI is kept in memory and synced with the "real" DOM.
How Traditional DOM Updates Work:
How React's Virtual DOM Works:
Example of Virtual DOM in Action:
1// Before state change
2<div>
3 <h1>Counter: 5</h1>
4 <button>Click me</button>
5</div>
6
7// After state change (only h1 content changes)
8<div>
9 <h1>Counter: 6</h1> {/* Only this changes */}
10 <button>Click me</button>
11</div>Benefits of Virtual DOM:
Real-World Impact:
Applications with thousands of components can update smoothly because React only updates what actually changed, not the entire DOM tree. This is why React applications feel fast and responsive even with complex user interfaces.
Experience React's power firsthand with this interactive demonstration featuring components, JSX syntax, props, and dynamic rendering.
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <title>React Introduction & JSX Demo</title>
7 <script src="https://unpkg.com/react@18/umd/react.development.js"></script>
8 <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
9 <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
10 <style>
11 body {
12 font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif;
13 margin: 0;
14 padding: 20px;
15 background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
16 min-height: 100vh;
17 color: #333;
18 }
19
20 .app {
21 max-width: 1000px;
22 margin: 0 auto;
23 background: white;
24 border-radius: 16px;
25 overflow: hidden;
26 box-shadow: 0 20px 40px rgba(0,0,0,0.1);
27 }
28
29 .header {
30 background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
31 color: white;
32 padding: 40px;
33 text-align: center;
34 }
35
36 .header h1 {
37 margin: 0;
38 font-size: 2.5rem;
39 }
40
41 .header p {
42 margin: 10px 0 0 0;
43 opacity: 0.9;
44 font-size: 1.2rem;
45 }
46
47 .content {
48 padding: 40px;
49 }
50
51 .section {
52 margin-bottom: 40px;
53 padding: 30px;
54 background: #f8f9ff;
55 border-radius: 12px;
56 border-left: 4px solid #667eea;
57 }
58
59 .section h2 {
60 margin: 0 0 20px 0;
61 color: #1e40af;
62 font-size: 1.5rem;
63 }
64
65 .component-demo {
66 display: grid;
67 grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
68 gap: 20px;
69 margin: 20px 0;
70 }
71
72 .demo-card {
73 background: white;
74 padding: 20px;
75 border-radius: 8px;
76 box-shadow: 0 2px 8px rgba(0,0,0,0.1);
77 text-align: center;
78 }
79
80 .demo-card h3 {
81 margin: 0 0 10px 0;
82 color: #667eea;
83 }
84
85 .demo-button {
86 background: #667eea;
87 color: white;
88 border: none;
89 padding: 10px 20px;
90 border-radius: 6px;
91 cursor: pointer;
92 font-size: 14px;
93 margin: 10px 5px;
94 transition: all 0.2s;
95 }
96
97 .demo-button:hover {
98 background: #5a67d8;
99 transform: translateY(-1px);
100 }
101
102 .demo-button.secondary {
103 background: #718096;
104 }
105
106 .demo-button.secondary:hover {
107 background: #4a5568;
108 }
109
110 .counter {
111 font-size: 2rem;
112 font-weight: bold;
113 color: #667eea;
114 margin: 10px 0;
115 }
116
117 .jsx-example {
118 background: #1a202c;
119 color: #e2e8f0;
120 padding: 20px;
121 border-radius: 8px;
122 font-family: 'Monaco', 'Courier New', monospace;
123 font-size: 14px;
124 margin: 20px 0;
125 overflow-x: auto;
126 }
127
128 .highlight {
129 background: #ffd700;
130 padding: 2px 4px;
131 border-radius: 3px;
132 color: #333;
133 font-weight: bold;
134 }
135
136 .feature-grid {
137 display: grid;
138 grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
139 gap: 15px;
140 margin: 20px 0;
141 }
142
143 .feature-item {
144 padding: 15px;
145 background: white;
146 border-radius: 8px;
147 box-shadow: 0 2px 4px rgba(0,0,0,0.1);
148 border-left: 3px solid #667eea;
149 }
150
151 .feature-item h4 {
152 margin: 0 0 8px 0;
153 color: #667eea;
154 }
155
156 .feature-item p {
157 margin: 0;
158 font-size: 14px;
159 color: #666;
160 }
161 </style>
162</head>
163<body>
164 <div id="root"></div>
165
166 <script type="text/babel">
167 const { useState } = React;
168
169 // Reusable Button Component
170 function DemoButton({ children, onClick, variant = 'primary' }) {
171 const className = `demo-button ${variant}`;
172 return (
173 <button className={className} onClick={onClick}>
174 {children}
175 </button>
176 );
177 }
178
179 // Counter Component with State
180 function Counter() {
181 const [count, setCount] = useState(0);
182
183 return (
184 <div className="demo-card">
185 <h3>🔢 Interactive Counter</h3>
186 <div className="counter">{count}</div>
187 <DemoButton onClick={() => setCount(count + 1)}>
188 Increment
189 </DemoButton>
190 <DemoButton onClick={() => setCount(count - 1)} variant="secondary">
191 Decrement
192 </DemoButton>
193 <DemoButton onClick={() => setCount(0)} variant="secondary">
194 Reset
195 </DemoButton>
196 </div>
197 );
198 }
199
200 // Greeting Component with Props
201 function Greeting({ name, role, emoji }) {
202 return (
203 <div className="demo-card">
204 <h3>{emoji} Hello, {name}!</h3>
205 <p>Role: <span className="highlight">{role}</span></p>
206 <DemoButton onClick={() => alert(`Nice to meet you, ${name}!`)}>
207 Say Hello
208 </DemoButton>
209 </div>
210 );
211 }
212
213 // Dynamic List Component
214 function TeamList() {
215 const team = [
216 { id: 1, name: 'Alice', role: 'Developer', emoji: '👩💻' },
217 { id: 2, name: 'Bob', role: 'Designer', emoji: '👨🎨' },
218 { id: 3, name: 'Carol', role: 'Manager', emoji: '👩💼' }
219 ];
220
221 return (
222 <div className="component-demo">
223 {team.map(person => (
224 <Greeting
225 key={person.id}
226 name={person.name}
227 role={person.role}
228 emoji={person.emoji}
229 />
230 ))}
231 </div>
232 );
233 }
234
235 // Main App Component
236 function App() {
237 return (
238 <div className="app">
239 <div className="header">
240 <h1>⚛️ React Introduction & JSX</h1>
241 <p>Experience React's Component-Based Architecture</p>
242 </div>
243
244 <div className="content">
245 <div className="section">
246 <h2>🧩 What Makes React Special?</h2>
247 <div className="feature-grid">
248 <div className="feature-item">
249 <h4>Components</h4>
250 <p>Reusable UI building blocks</p>
251 </div>
252 <div className="feature-item">
253 <h4>JSX</h4>
254 <p>HTML-like syntax in JavaScript</p>
255 </div>
256 <div className="feature-item">
257 <h4>Virtual DOM</h4>
258 <p>Efficient rendering and updates</p>
259 </div>
260 <div className="feature-item">
261 <h4>Declarative</h4>
262 <p>Describe what, not how</p>
263 </div>
264 </div>
265 </div>
266
267 <div className="section">
268 <h2>🎯 JSX in Action</h2>
269 <p>JSX allows us to write HTML-like syntax in JavaScript:</p>
270 <div className="jsx-example">
271 {/* JSX Example */}
272 const element = <h1>Hello, React!</h1>;<br/>
273 const greeting = (<br/>
274 <div className="welcome"><br/>
275 <h1>Welcome to React!</h1><br/>
276 <p>Dynamic content: {new Date().getFullYear()}</p><br/>
277 </div><br/>
278 );
279 </div>
280 </div>
281
282 <div className="section">
283 <h2>🔄 Interactive State Management</h2>
284 <p>React components can manage their own state and respond to user interactions:</p>
285 <Counter />
286 </div>
287
288 <div className="section">
289 <h2>🎭 Component Reusability</h2>
290 <p>The same component can be used multiple times with different data (props):</p>
291 <TeamList />
292 </div>
293
294 <div className="section">
295 <h2>🚀 Why Choose React?</h2>
296 <div className="feature-grid">
297 <div className="feature-item">
298 <h4>🏢 Industry Standard</h4>
299 <p>Used by Facebook, Netflix, Airbnb</p>
300 </div>
301 <div className="feature-item">
302 <h4>🔧 Rich Ecosystem</h4>
303 <p>Thousands of libraries and tools</p>
304 </div>
305 <div className="feature-item">
306 <h4>📱 Cross-Platform</h4>
307 <p>Web, mobile, and desktop apps</p>
308 </div>
309 <div className="feature-item">
310 <h4>⚡ Performance</h4>
311 <p>Virtual DOM optimization</p>
312 </div>
313 </div>
314 </div>
315 </div>
316 </div>
317 );
318 }
319
320 // Render the App
321 ReactDOM.render(<App />, document.getElementById('root'));
322 </script>
323</body>
324</html>This comprehensive React introduction demonstration covers all fundamental concepts from Lesson 1:
🎯 Complete React Fundamentals Coverage:
1. Component-Based Architecture:
2. JSX Syntax and Features:
3. Props and Data Flow:
4. State Management:
5. Interactive Functionality:
6. Modern React Patterns:
7. Real-World Application:
8. Development Best Practices:
This demonstration serves as both a learning tool and practical reference, showing how React's component-based architecture, JSX syntax, and state management combine to create dynamic, interactive web applications. Every concept from the lesson is represented with hands-on, working examples.
Excellent work mastering React fundamentals! You now understand React's core concepts, JSX syntax, and component architecture. In our next lesson, we'll dive deeper into Components & Props, where you'll learn advanced prop patterns, prop validation, component composition techniques, and how to build scalable component hierarchies. Get ready to master the art of React component design!
Your progress is saved in this browser, on this device only.