dev-code-snippets-md 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,458 +0,0 @@
1
- # React Lab Programs (Experiment 06–10)
2
-
3
- Source: BCSL657B React Lab Manual
4
-
5
- ---
6
-
7
- # EXPERIMENT 06 — React Form Validation
8
-
9
- ## File: src/App.js
10
-
11
- ```javascript
12
- import './App.css';
13
- import React, { useState } from "react";
14
-
15
- const App = () => {
16
-
17
- const [formData, setFormData] = useState({
18
- name: "",
19
- email: "",
20
- password: "",
21
- showPassword: false
22
- });
23
-
24
- const [errors, setErrors] = useState({});
25
-
26
- const handleChange = (e) => {
27
- const { name, value } = e.target;
28
- let sanitizedValue = value.replace(/[<>]/g, "");
29
- setFormData({ ...formData, [name]: sanitizedValue });
30
- };
31
-
32
- const toggleShowPassword = () => {
33
- setFormData({ ...formData, showPassword: !formData.showPassword });
34
- };
35
-
36
- const validateForm = () => {
37
-
38
- let newErrors = {};
39
-
40
- if (!formData.name.trim())
41
- newErrors.name = "Name is required.";
42
-
43
- if (!formData.email.trim())
44
- newErrors.email = "Email is required.";
45
- else if (!/^\S+@\S+\.\S+$/.test(formData.email))
46
- newErrors.email = "Invalid email format.";
47
-
48
- if (!formData.password.trim())
49
- newErrors.password = "Password is required.";
50
- else if (formData.password.length < 6)
51
- newErrors.password = "Password must be at least 6 characters.";
52
-
53
- setErrors(newErrors);
54
-
55
- return Object.keys(newErrors).length === 0;
56
-
57
- };
58
-
59
- const handleSubmit = (e) => {
60
-
61
- e.preventDefault();
62
-
63
- if (validateForm()) {
64
-
65
- console.log("Form Submitted Successfully!", formData);
66
-
67
- alert("Form Submitted Successfully!");
68
-
69
- setFormData({
70
- name: "",
71
- email: "",
72
- password: "",
73
- showPassword: false
74
- });
75
-
76
- setErrors({});
77
-
78
- }
79
-
80
- };
81
-
82
- return (
83
-
84
- <div className="container">
85
-
86
- <h2>React Form</h2>
87
-
88
- <form onSubmit={handleSubmit}>
89
-
90
- <label>Name</label>
91
- <input name="name" value={formData.name} onChange={handleChange} />
92
- {errors.name && <p>{errors.name}</p>}
93
-
94
- <label>Email</label>
95
- <input name="email" value={formData.email} onChange={handleChange} />
96
- {errors.email && <p>{errors.email}</p>}
97
-
98
- <label>Password</label>
99
- <input
100
- type={formData.showPassword ? "text" : "password"}
101
- name="password"
102
- value={formData.password}
103
- onChange={handleChange}
104
- />
105
- {errors.password && <p>{errors.password}</p>}
106
-
107
- <label>
108
- <input type="checkbox" checked={formData.showPassword} onChange={toggleShowPassword}/>
109
- Show Password
110
- </label>
111
-
112
- <button type="submit">Submit</button>
113
-
114
- </form>
115
-
116
- </div>
117
-
118
- );
119
-
120
- };
121
-
122
- export default App;
123
- ```
124
-
125
- ---
126
-
127
- # EXPERIMENT 07 — Profile Card Component
128
-
129
- ## File: src/components/ProfileCard.js
130
-
131
- ```javascript
132
- import React, { useState } from "react";
133
-
134
- const ProfileCard = ({ name, bio, profilePic, initialBgColor }) => {
135
-
136
- const [bgColor, setBgColor] = useState(initialBgColor || "#f8f9fa");
137
-
138
- const changeBackground = () => {
139
-
140
- const colors = ["#ffadad","#ffd6a5","#cce5ff","#d4a5a5","#caffbf"];
141
-
142
- const randomColor = colors[Math.floor(Math.random() * colors.length)];
143
-
144
- setBgColor(randomColor);
145
-
146
- };
147
-
148
- return (
149
-
150
- <div style={{ backgroundColor: bgColor, padding: "20px", textAlign: "center" }}>
151
-
152
- <img
153
- src={profilePic}
154
- alt="profile"
155
- style={{ width: "100px", borderRadius: "50%" }}
156
- />
157
-
158
- <h2>{name}</h2>
159
-
160
- <p>{bio}</p>
161
-
162
- <button onClick={changeBackground}>
163
- Change Background
164
- </button>
165
-
166
- </div>
167
-
168
- );
169
-
170
- };
171
-
172
- export default ProfileCard;
173
- ```
174
-
175
- ---
176
-
177
- # EXPERIMENT 08 — Reminder Application
178
-
179
- ## File: src/components/AddTask.js
180
-
181
- ```javascript
182
- import React, { useState } from 'react';
183
-
184
- const AddTask = ({ addTask }) => {
185
-
186
- const [taskName, setTaskName] = useState('');
187
- const [dueDate, setDueDate] = useState('');
188
-
189
- const handleSubmit = (e) => {
190
-
191
- e.preventDefault();
192
-
193
- if (taskName.trim()) {
194
-
195
- addTask(taskName, dueDate);
196
-
197
- setTaskName('');
198
- setDueDate('');
199
-
200
- }
201
-
202
- };
203
-
204
- return (
205
-
206
- <form onSubmit={handleSubmit}>
207
-
208
- <input
209
- type="text"
210
- placeholder="Task Name"
211
- value={taskName}
212
- onChange={(e) => setTaskName(e.target.value)}
213
- />
214
-
215
- <input
216
- type="date"
217
- value={dueDate}
218
- onChange={(e) => setDueDate(e.target.value)}
219
- />
220
-
221
- <button type="submit">Add Task</button>
222
-
223
- </form>
224
-
225
- );
226
-
227
- };
228
-
229
- export default AddTask;
230
- ```
231
-
232
- ---
233
-
234
- ## File: src/App.js
235
-
236
- ```javascript
237
- import React, { useState } from 'react';
238
- import AddTask from './components/AddTask';
239
-
240
- function App() {
241
-
242
- const [tasks, setTasks] = useState([]);
243
-
244
- const addTask = (name, dueDate) => {
245
-
246
- const newTask = {
247
- id: Date.now(),
248
- name,
249
- dueDate,
250
- completed: false
251
- };
252
-
253
- setTasks([...tasks, newTask]);
254
-
255
- };
256
-
257
- return (
258
-
259
- <div>
260
-
261
- <h1>Reminder App</h1>
262
-
263
- <AddTask addTask={addTask} />
264
-
265
- <ul>
266
-
267
- {tasks.map(task => (
268
- <li key={task.id}>
269
- {task.name} - {task.dueDate}
270
- </li>
271
- ))}
272
-
273
- </ul>
274
-
275
- </div>
276
-
277
- );
278
-
279
- }
280
-
281
- export default App;
282
- ```
283
-
284
- ---
285
-
286
- # EXPERIMENT 09 — React Router Navigation
287
-
288
- ## File: src/App.js
289
-
290
- ```javascript
291
- import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
292
-
293
- function Home() {
294
- return <h1>Welcome to Home</h1>;
295
- }
296
-
297
- function About() {
298
- return <h1>About Us</h1>;
299
- }
300
-
301
- function Contact() {
302
- return <h1>Contact Us</h1>;
303
- }
304
-
305
- function App() {
306
-
307
- return (
308
-
309
- <Router>
310
-
311
- <Routes>
312
-
313
- <Route path="/" element={<Home />} />
314
-
315
- <Route path="/about" element={<About />} />
316
-
317
- <Route path="/contact" element={<Contact />} />
318
-
319
- </Routes>
320
-
321
- </Router>
322
-
323
- );
324
-
325
- }
326
-
327
- export default App;
328
- ```
329
-
330
- ---
331
-
332
- # EXPERIMENT 10 — Lifecycle Methods + API Fetch
333
-
334
- ## File: src/DataFetcher.js
335
-
336
- ```javascript
337
- import React, { Component } from "react";
338
- import axios from "axios";
339
-
340
- class DataFetcher extends Component {
341
-
342
- constructor(props) {
343
-
344
- super(props);
345
-
346
- this.state = {
347
- data: [],
348
- searchTerm: '',
349
- error: null,
350
- };
351
-
352
- }
353
-
354
- componentDidMount() {
355
- this.fetchData();
356
- }
357
-
358
- componentDidUpdate(prevProps, prevState) {
359
-
360
- if (prevState.searchTerm !== this.state.searchTerm) {
361
- this.fetchData();
362
- }
363
-
364
- }
365
-
366
- fetchData = () => {
367
-
368
- axios.get("https://jsonplaceholder.typicode.com/posts")
369
-
370
- .then(response => {
371
- this.setState({
372
- data: response.data,
373
- error: null
374
- });
375
- })
376
-
377
- .catch(() => {
378
- this.setState({
379
- error: "Failed to fetch data"
380
- });
381
- });
382
-
383
- };
384
-
385
- handleSearch = (event) => {
386
-
387
- this.setState({
388
- searchTerm: event.target.value
389
- });
390
-
391
- };
392
-
393
- render() {
394
-
395
- const { data, searchTerm, error } = this.state;
396
-
397
- const filteredData =
398
- data.filter(item =>
399
- item.title.includes(searchTerm)
400
- );
401
-
402
- return (
403
-
404
- <div>
405
-
406
- <h1>Data Fetcher</h1>
407
-
408
- <input
409
- type="text"
410
- placeholder="Search..."
411
- onChange={this.handleSearch}
412
- />
413
-
414
- {error && <p>{error}</p>}
415
-
416
- <ul>
417
-
418
- {filteredData.map(item => (
419
- <li key={item.id}>{item.title}</li>
420
- ))}
421
-
422
- </ul>
423
-
424
- </div>
425
-
426
- );
427
-
428
- }
429
-
430
- }
431
-
432
- export default DataFetcher;
433
- ```
434
-
435
- ---
436
-
437
- ## File: src/App.js
438
-
439
- ```javascript
440
- import React from "react";
441
- import DataFetcher from "./DataFetcher";
442
-
443
- function App() {
444
-
445
- return (
446
-
447
- <div>
448
-
449
- <DataFetcher />
450
-
451
- </div>
452
-
453
- );
454
-
455
- }
456
-
457
- export default App;
458
- ```