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.
- package/package.json +1 -1
- package/snippets/react-lab-1-10.md +1099 -0
- package/snippets/react-code-snippets.md +0 -458
|
@@ -0,0 +1,1099 @@
|
|
|
1
|
+
```
|
|
2
|
+
// Program 1 App.css
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
import "./App.css";
|
|
5
|
+
function App() {
|
|
6
|
+
const [text, setText] = useState("");
|
|
7
|
+
return (
|
|
8
|
+
<div className="App">
|
|
9
|
+
<h1>Dynamic Text Display</h1>
|
|
10
|
+
<input
|
|
11
|
+
type="text"
|
|
12
|
+
value={text}
|
|
13
|
+
onChange={(e) => setText(e.target.value)}
|
|
14
|
+
placeholder="Type something..."
|
|
15
|
+
/>
|
|
16
|
+
<p>You typed: {text}</p>
|
|
17
|
+
</div>
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
export default App;
|
|
21
|
+
// Program 2 App.js
|
|
22
|
+
import Header from "./Header";
|
|
23
|
+
import Footer from "./Footer";
|
|
24
|
+
import "./App.css";
|
|
25
|
+
function App() {
|
|
26
|
+
return (
|
|
27
|
+
<div className="App">
|
|
28
|
+
<Header title="Welcome to My React App" />
|
|
29
|
+
<Footer
|
|
30
|
+
tagline="Building great apps with React"
|
|
31
|
+
copyright="© 2025 MyApp"
|
|
32
|
+
/>
|
|
33
|
+
</div>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
export default App;
|
|
37
|
+
// Program 2 Header.js
|
|
38
|
+
function Header({ title }) {
|
|
39
|
+
return (
|
|
40
|
+
<header>
|
|
41
|
+
<h1>{title}</h1>
|
|
42
|
+
</header>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
export default Header;
|
|
46
|
+
// Program 2 Footer.js
|
|
47
|
+
function Footer({ tagline, copyright }) {
|
|
48
|
+
return (
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
<footer>
|
|
53
|
+
<p>{tagline}</p>
|
|
54
|
+
<p>{copyright}</p>
|
|
55
|
+
</footer>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
export default Footer;
|
|
59
|
+
/* Program 2 App.css */
|
|
60
|
+
.App {
|
|
61
|
+
text-align: center;
|
|
62
|
+
font-family: Arial, sans-serif;
|
|
63
|
+
}
|
|
64
|
+
header {
|
|
65
|
+
background-color: #282c34;
|
|
66
|
+
padding: 20px;
|
|
67
|
+
color: white;
|
|
68
|
+
}
|
|
69
|
+
footer {
|
|
70
|
+
background-color: #282c34;
|
|
71
|
+
padding: 10px;
|
|
72
|
+
color: white;
|
|
73
|
+
position: absolute;
|
|
74
|
+
bottom: 0;
|
|
75
|
+
width: 100%;
|
|
76
|
+
text-align: center;
|
|
77
|
+
}
|
|
78
|
+
// Program 3 App.js
|
|
79
|
+
import { useState } from "react";
|
|
80
|
+
import "./App.css";
|
|
81
|
+
function App() {
|
|
82
|
+
const [count, setCount] = useState(0);
|
|
83
|
+
const [step, setStep] = useState(1);
|
|
84
|
+
return (
|
|
85
|
+
<div className="App">
|
|
86
|
+
<h1>Counter Application</h1>
|
|
87
|
+
<h2>{count}</h2>
|
|
88
|
+
<button onClick={() => setCount(count + step)}>
|
|
89
|
+
Increase by {step}
|
|
90
|
+
</button>
|
|
91
|
+
<button
|
|
92
|
+
onClick={() =>
|
|
93
|
+
count - step >= 0 && setCount(count - step)
|
|
94
|
+
}
|
|
95
|
+
>
|
|
96
|
+
Decrease by {step}
|
|
97
|
+
</button>
|
|
98
|
+
<button onClick={() => setCount(0)}>
|
|
99
|
+
Reset
|
|
100
|
+
</button>
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
```
|
|
104
|
+
<br />
|
|
105
|
+
<br />
|
|
106
|
+
<input
|
|
107
|
+
type="number"
|
|
108
|
+
value={step}
|
|
109
|
+
min="1"
|
|
110
|
+
onChange={(e) =>
|
|
111
|
+
setStep(Number(e.target.value))
|
|
112
|
+
}
|
|
113
|
+
/>
|
|
114
|
+
</div>
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
export default App;
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
```
|
|
121
|
+
/* Program 3 App.css */
|
|
122
|
+
.App {
|
|
123
|
+
text-align: center;
|
|
124
|
+
}
|
|
125
|
+
button {
|
|
126
|
+
margin: 10px;
|
|
127
|
+
padding: 10px;
|
|
128
|
+
font-size: 16px;
|
|
129
|
+
cursor: pointer;
|
|
130
|
+
}
|
|
131
|
+
input {
|
|
132
|
+
padding: 5px;
|
|
133
|
+
font-size: 16px;
|
|
134
|
+
}
|
|
135
|
+
// Program 4 App.js
|
|
136
|
+
import { useState } from "react";
|
|
137
|
+
import "./App.css";
|
|
138
|
+
function App() {
|
|
139
|
+
const [tasks, setTasks] = useState([]);
|
|
140
|
+
const [text, setText] = useState("");
|
|
141
|
+
const addTask = () => {
|
|
142
|
+
if (text) {
|
|
143
|
+
setTasks([
|
|
144
|
+
...tasks,
|
|
145
|
+
{
|
|
146
|
+
id: Date.now(),
|
|
147
|
+
text,
|
|
148
|
+
done: false
|
|
149
|
+
}
|
|
150
|
+
]);
|
|
151
|
+
setText("");
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
const deleteTask = (id) => {
|
|
155
|
+
setTasks(tasks.filter((t) => t.id !== id));
|
|
156
|
+
};
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
```
|
|
160
|
+
const toggleTask = (id) => {
|
|
161
|
+
setTasks(
|
|
162
|
+
tasks.map((t) =>
|
|
163
|
+
t.id === id
|
|
164
|
+
? { ...t, done: !t.done }
|
|
165
|
+
: t
|
|
166
|
+
)
|
|
167
|
+
);
|
|
168
|
+
};
|
|
169
|
+
return (
|
|
170
|
+
<div className="todo">
|
|
171
|
+
<h2>To-Do List</h2>
|
|
172
|
+
<input
|
|
173
|
+
value={text}
|
|
174
|
+
onChange={(e) => setText(e.target.value)}
|
|
175
|
+
placeholder="Add task"
|
|
176
|
+
/>
|
|
177
|
+
<button onClick={addTask}>Add</button>
|
|
178
|
+
<ul>
|
|
179
|
+
{tasks.map((t) => (
|
|
180
|
+
<li
|
|
181
|
+
key={t.id}
|
|
182
|
+
className={t.done ? "done" : ""}
|
|
183
|
+
>
|
|
184
|
+
<span
|
|
185
|
+
onClick={() => toggleTask(t.id)}
|
|
186
|
+
>
|
|
187
|
+
{t.text}
|
|
188
|
+
</span>
|
|
189
|
+
<button
|
|
190
|
+
onClick={() => deleteTask(t.id)}
|
|
191
|
+
>
|
|
192
|
+
X
|
|
193
|
+
</button>
|
|
194
|
+
</li>
|
|
195
|
+
))}
|
|
196
|
+
</ul>
|
|
197
|
+
</div>
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
export default App;
|
|
201
|
+
/* Program 4 App.css */
|
|
202
|
+
.todo {
|
|
203
|
+
width: 400px;
|
|
204
|
+
margin: 40px auto;
|
|
205
|
+
text-align: center;
|
|
206
|
+
font-family: Arial;
|
|
207
|
+
border: 1px solid gray;
|
|
208
|
+
padding: 20px;
|
|
209
|
+
background: white;
|
|
210
|
+
}
|
|
211
|
+
input {
|
|
212
|
+
padding: 8px;
|
|
213
|
+
width: 60%;
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
```
|
|
218
|
+
button {
|
|
219
|
+
padding: 8px;
|
|
220
|
+
margin-left: 5px;
|
|
221
|
+
cursor: pointer;
|
|
222
|
+
}
|
|
223
|
+
ul {
|
|
224
|
+
list-style: none;
|
|
225
|
+
padding: 0;
|
|
226
|
+
}
|
|
227
|
+
li {
|
|
228
|
+
margin: 10px 0;
|
|
229
|
+
padding: 8px;
|
|
230
|
+
border: 1px solid gray;
|
|
231
|
+
display: flex;
|
|
232
|
+
justify-content: space-between;
|
|
233
|
+
}
|
|
234
|
+
.done {
|
|
235
|
+
text-decoration: line-through;
|
|
236
|
+
background: lightgray;
|
|
237
|
+
}
|
|
238
|
+
Program 5 - App.js
|
|
239
|
+
import FigureList from "./components/FigureList";
|
|
240
|
+
import "./App.css";
|
|
241
|
+
function App() {
|
|
242
|
+
return (
|
|
243
|
+
<div className="app">
|
|
244
|
+
<h1>Dynamic Image Gallery</h1>
|
|
245
|
+
<FigureList />
|
|
246
|
+
</div>
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
export default App;
|
|
250
|
+
Program 5 - BasicFigure.js
|
|
251
|
+
function BasicFigure({ image, caption }) {
|
|
252
|
+
return (
|
|
253
|
+
<div className="card">
|
|
254
|
+
<img src={image} alt={caption} />
|
|
255
|
+
<p>{caption}</p>
|
|
256
|
+
</div>
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
export default BasicFigure;
|
|
260
|
+
// Program 5 FigureList.js
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
```
|
|
264
|
+
import { useState } from "react";
|
|
265
|
+
import BasicFigure from "./BasicFigure";
|
|
266
|
+
function FigureList() {
|
|
267
|
+
const [images, setImages] = useState([
|
|
268
|
+
{ image: "https://picsum.photos/200", caption: "Image 1" },
|
|
269
|
+
{ image: "https://picsum.photos/201", caption: "Image 2" },
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
```
|
|
273
|
+
{ image: "https://picsum.photos/202", caption: "Image 3" }
|
|
274
|
+
]);
|
|
275
|
+
const addImage = () => {
|
|
276
|
+
const n = images.length + 1;
|
|
277
|
+
setImages([
|
|
278
|
+
...images,
|
|
279
|
+
{
|
|
280
|
+
image: `https://picsum.photos/20${n}`,
|
|
281
|
+
caption: `Image ${n}`
|
|
282
|
+
}
|
|
283
|
+
]);
|
|
284
|
+
};
|
|
285
|
+
const removeImage = () => {
|
|
286
|
+
setImages(images.slice(0, -1));
|
|
287
|
+
};
|
|
288
|
+
return (
|
|
289
|
+
<div>
|
|
290
|
+
<button onClick={addImage}>Add Image</button>
|
|
291
|
+
<button onClick={removeImage}>
|
|
292
|
+
Remove Image
|
|
293
|
+
</button>
|
|
294
|
+
<div className="grid">
|
|
295
|
+
{images.map((img, i) => (
|
|
296
|
+
<BasicFigure
|
|
297
|
+
key={i}
|
|
298
|
+
image={img.image}
|
|
299
|
+
caption={img.caption}
|
|
300
|
+
/>
|
|
301
|
+
))}
|
|
302
|
+
</div>
|
|
303
|
+
</div>
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
export default FigureList;
|
|
307
|
+
/* Program 5 App.css */
|
|
308
|
+
.app {
|
|
309
|
+
text-align: center;
|
|
310
|
+
font-family: Arial;
|
|
311
|
+
}
|
|
312
|
+
h1 {
|
|
313
|
+
background: black;
|
|
314
|
+
color: white;
|
|
315
|
+
padding: 10px;
|
|
316
|
+
}
|
|
317
|
+
button {
|
|
318
|
+
padding: 8px;
|
|
319
|
+
margin: 10px;
|
|
320
|
+
cursor: pointer;
|
|
321
|
+
}
|
|
322
|
+
.grid {
|
|
323
|
+
display: flex;
|
|
324
|
+
justify-content: center;
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
```
|
|
328
|
+
flex-wrap: wrap;
|
|
329
|
+
gap: 15px;
|
|
330
|
+
}
|
|
331
|
+
.card {
|
|
332
|
+
border: 1px solid gray;
|
|
333
|
+
padding: 10px;
|
|
334
|
+
background: white;
|
|
335
|
+
}
|
|
336
|
+
.card img {
|
|
337
|
+
width: 200px;
|
|
338
|
+
height: 200px;
|
|
339
|
+
}
|
|
340
|
+
src/
|
|
341
|
+
│
|
|
342
|
+
├── App.js
|
|
343
|
+
├── App.css
|
|
344
|
+
│
|
|
345
|
+
└── components/
|
|
346
|
+
├── FigureList.js
|
|
347
|
+
└── BasicFigure.js
|
|
348
|
+
// Program 6 App.js
|
|
349
|
+
import React, { useState } from "react";
|
|
350
|
+
import "./App.css";
|
|
351
|
+
function App() {
|
|
352
|
+
const [data, setData] = useState({
|
|
353
|
+
name: "",
|
|
354
|
+
email: "",
|
|
355
|
+
password: ""
|
|
356
|
+
});
|
|
357
|
+
const [error, setError] = useState({});
|
|
358
|
+
function handleChange(e) {
|
|
359
|
+
let value = e.target.value.replace(/[<>]/g, "");
|
|
360
|
+
setData({
|
|
361
|
+
...data,
|
|
362
|
+
[e.target.name]: value
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
function handleSubmit(e) {
|
|
366
|
+
e.preventDefault();
|
|
367
|
+
let err = {};
|
|
368
|
+
if (data.name === "")
|
|
369
|
+
err.name = "Enter name";
|
|
370
|
+
if (data.email === "") {
|
|
371
|
+
err.email = "Enter email";
|
|
372
|
+
} else if (!data.email.includes("@")) {
|
|
373
|
+
err.email = "Invalid email";
|
|
374
|
+
}
|
|
375
|
+
if (data.password.length < 6)
|
|
376
|
+
err.password = "Min 6 characters";
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
```
|
|
380
|
+
setError(err);
|
|
381
|
+
if (Object.keys(err).length === 0) {
|
|
382
|
+
alert("Form Submitted");
|
|
383
|
+
console.log(data);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
return (
|
|
387
|
+
<div className="box">
|
|
388
|
+
<h2>React Form</h2>
|
|
389
|
+
<form onSubmit={handleSubmit}>
|
|
390
|
+
<input
|
|
391
|
+
type="text"
|
|
392
|
+
name="name"
|
|
393
|
+
placeholder="Name"
|
|
394
|
+
onChange={handleChange}
|
|
395
|
+
className={error.name ? "red" : ""}
|
|
396
|
+
/>
|
|
397
|
+
<p>{error.name}</p>
|
|
398
|
+
<input
|
|
399
|
+
type="text"
|
|
400
|
+
name="email"
|
|
401
|
+
placeholder="Email"
|
|
402
|
+
onChange={handleChange}
|
|
403
|
+
className={error.email ? "red" : ""}
|
|
404
|
+
/>
|
|
405
|
+
<p>{error.email}</p>
|
|
406
|
+
<input
|
|
407
|
+
type="password"
|
|
408
|
+
name="password"
|
|
409
|
+
placeholder="Password"
|
|
410
|
+
onChange={handleChange}
|
|
411
|
+
className={error.password ? "red" : ""}
|
|
412
|
+
/>
|
|
413
|
+
<p>{error.password}</p>
|
|
414
|
+
<button>Submit</button>
|
|
415
|
+
</form>
|
|
416
|
+
</div>
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
export default App;
|
|
420
|
+
/* Program 6 App.css */
|
|
421
|
+
body {
|
|
422
|
+
font-family: Arial;
|
|
423
|
+
background: #f2f2f2;
|
|
424
|
+
}
|
|
425
|
+
.box {
|
|
426
|
+
width: 300px;
|
|
427
|
+
margin: 100px auto;
|
|
428
|
+
padding: 20px;
|
|
429
|
+
background: white;
|
|
430
|
+
border-radius: 5px;
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
```
|
|
434
|
+
}
|
|
435
|
+
input {
|
|
436
|
+
width: 100%;
|
|
437
|
+
padding: 8px;
|
|
438
|
+
margin-top: 10px;
|
|
439
|
+
}
|
|
440
|
+
button {
|
|
441
|
+
width: 100%;
|
|
442
|
+
padding: 8px;
|
|
443
|
+
margin-top: 10px;
|
|
444
|
+
background: blue;
|
|
445
|
+
color: white;
|
|
446
|
+
border: none;
|
|
447
|
+
}
|
|
448
|
+
.red {
|
|
449
|
+
border: 2px solid red;
|
|
450
|
+
}
|
|
451
|
+
p {
|
|
452
|
+
color: red;
|
|
453
|
+
font-size: 12px;
|
|
454
|
+
}
|
|
455
|
+
// Program 7 App.js
|
|
456
|
+
import React, { useState } from "react";
|
|
457
|
+
import "./App.css";
|
|
458
|
+
function App() {
|
|
459
|
+
const [color, setColor] = useState("lightblue");
|
|
460
|
+
function changeColor() {
|
|
461
|
+
setColor(
|
|
462
|
+
color === "lightblue"
|
|
463
|
+
? "lightpink"
|
|
464
|
+
: "lightgreen"
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
return (
|
|
468
|
+
<div
|
|
469
|
+
className="card"
|
|
470
|
+
style={{ backgroundColor: color }}
|
|
471
|
+
>
|
|
472
|
+
<img
|
|
473
|
+
src="https://randomuser.me/api/portraits/men/1.jpg"
|
|
474
|
+
alt="profile"
|
|
475
|
+
/>
|
|
476
|
+
<h2>John Doe</h2>
|
|
477
|
+
<p>Web Developer</p>
|
|
478
|
+
<button onClick={changeColor}>
|
|
479
|
+
Change Color
|
|
480
|
+
</button>
|
|
481
|
+
</div>
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
```
|
|
487
|
+
export default App;
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
```
|
|
491
|
+
/* Program 7 App.css */
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
```
|
|
495
|
+
body {
|
|
496
|
+
font-family: Arial;
|
|
497
|
+
background: #f2f2f2;
|
|
498
|
+
}
|
|
499
|
+
.card {
|
|
500
|
+
width: 250px;
|
|
501
|
+
margin: 100px auto;
|
|
502
|
+
padding: 20px;
|
|
503
|
+
text-align: center;
|
|
504
|
+
border-radius: 10px;
|
|
505
|
+
transition: 0.3s;
|
|
506
|
+
}
|
|
507
|
+
.card:hover {
|
|
508
|
+
transform: scale(1.05);
|
|
509
|
+
}
|
|
510
|
+
img {
|
|
511
|
+
width: 100px;
|
|
512
|
+
height: 100px;
|
|
513
|
+
border-radius: 50%;
|
|
514
|
+
}
|
|
515
|
+
h2 {
|
|
516
|
+
color: #333;
|
|
517
|
+
}
|
|
518
|
+
p {
|
|
519
|
+
color: #666;
|
|
520
|
+
}
|
|
521
|
+
button {
|
|
522
|
+
padding: 8px 15px;
|
|
523
|
+
border: none;
|
|
524
|
+
background: blue;
|
|
525
|
+
color: white;
|
|
526
|
+
border-radius: 5px;
|
|
527
|
+
}
|
|
528
|
+
// Program 8 App.js
|
|
529
|
+
import React, { useState } from "react";
|
|
530
|
+
import "./App.css";
|
|
531
|
+
function App() {
|
|
532
|
+
const [task, setTask] = useState("");
|
|
533
|
+
const [date, setDate] = useState("");
|
|
534
|
+
const [tasks, setTasks] = useState([]);
|
|
535
|
+
const [filter, setFilter] = useState("all");
|
|
536
|
+
function addTask() {
|
|
537
|
+
if (task !== "") {
|
|
538
|
+
setTasks([
|
|
539
|
+
...tasks,
|
|
540
|
+
{
|
|
541
|
+
text: task,
|
|
542
|
+
due: date,
|
|
543
|
+
```
|
|
544
|
+
|
|
545
|
+
```
|
|
546
|
+
done: false
|
|
547
|
+
setTask("");
|
|
548
|
+
setDate("");
|
|
549
|
+
function toggle(index) {
|
|
550
|
+
let newTasks = [...tasks];
|
|
551
|
+
newTasks[index].done = !newTasks[index].done;
|
|
552
|
+
setTasks(newTasks);
|
|
553
|
+
let showTasks = tasks.filter((t) =>
|
|
554
|
+
filter === "all"
|
|
555
|
+
? true
|
|
556
|
+
: filter === "done"
|
|
557
|
+
? t.done
|
|
558
|
+
: !t.done
|
|
559
|
+
<div className="box">
|
|
560
|
+
<h2>Reminder App</h2>
|
|
561
|
+
<input
|
|
562
|
+
type="text"
|
|
563
|
+
placeholder="Task"
|
|
564
|
+
value={task}
|
|
565
|
+
onChange={(e) => setTask(e.target.value)}
|
|
566
|
+
<input
|
|
567
|
+
type="date"
|
|
568
|
+
value={date}
|
|
569
|
+
onChange={(e) => setDate(e.target.value)}
|
|
570
|
+
<button onClick={addTask}>Add</button>
|
|
571
|
+
<div className="filter">
|
|
572
|
+
<button onClick={() => setFilter("all")}>
|
|
573
|
+
All
|
|
574
|
+
</button>
|
|
575
|
+
<button onClick={() => setFilter("done")}>
|
|
576
|
+
Completed
|
|
577
|
+
</button>
|
|
578
|
+
<button onClick={() => setFilter("pending")}>
|
|
579
|
+
Pending
|
|
580
|
+
</button>
|
|
581
|
+
</div>
|
|
582
|
+
{showTasks.map((t, i) => (
|
|
583
|
+
<div
|
|
584
|
+
key={i}
|
|
585
|
+
className="task"
|
|
586
|
+
style={{
|
|
587
|
+
textDecoration: t.done
|
|
588
|
+
```
|
|
589
|
+
|
|
590
|
+
```
|
|
591
|
+
? "line-through"
|
|
592
|
+
: "none"
|
|
593
|
+
}}
|
|
594
|
+
>
|
|
595
|
+
<h4>{t.text}</h4>
|
|
596
|
+
```
|
|
597
|
+
|
|
598
|
+
```
|
|
599
|
+
<p>{t.due}</p>
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
```
|
|
603
|
+
<button onClick={() => toggle(i)}>
|
|
604
|
+
{t.done ? "Undo" : "Done"}
|
|
605
|
+
</button>
|
|
606
|
+
</div>
|
|
607
|
+
))}
|
|
608
|
+
</div>
|
|
609
|
+
);
|
|
610
|
+
}
|
|
611
|
+
export default App;
|
|
612
|
+
```
|
|
613
|
+
|
|
614
|
+
```
|
|
615
|
+
/* Program 8 App.css */
|
|
616
|
+
```
|
|
617
|
+
|
|
618
|
+
```
|
|
619
|
+
body {
|
|
620
|
+
font-family: Arial;
|
|
621
|
+
background: #f2f2f2;
|
|
622
|
+
}
|
|
623
|
+
.box {
|
|
624
|
+
width: 300px;
|
|
625
|
+
margin: 50px auto;
|
|
626
|
+
padding: 20px;
|
|
627
|
+
background: white;
|
|
628
|
+
border-radius: 10px;
|
|
629
|
+
}
|
|
630
|
+
input {
|
|
631
|
+
width: 100%;
|
|
632
|
+
padding: 8px;
|
|
633
|
+
margin-top: 10px;
|
|
634
|
+
}
|
|
635
|
+
button {
|
|
636
|
+
margin-top: 10px;
|
|
637
|
+
padding: 8px;
|
|
638
|
+
}
|
|
639
|
+
.task {
|
|
640
|
+
margin-top: 15px;
|
|
641
|
+
padding: 10px;
|
|
642
|
+
background: #eee;
|
|
643
|
+
border-radius: 5px;
|
|
644
|
+
}
|
|
645
|
+
.filter button {
|
|
646
|
+
margin-right: 5px;
|
|
647
|
+
}
|
|
648
|
+
// Program 9 App.js
|
|
649
|
+
import React from "react";
|
|
650
|
+
import {
|
|
651
|
+
BrowserRouter,
|
|
652
|
+
Routes,
|
|
653
|
+
```
|
|
654
|
+
|
|
655
|
+
```
|
|
656
|
+
Route,
|
|
657
|
+
Link
|
|
658
|
+
} from "react-router-dom";
|
|
659
|
+
function Home() {
|
|
660
|
+
return <h2>Home Page</h2>;
|
|
661
|
+
}
|
|
662
|
+
function About() {
|
|
663
|
+
return <h2>About Page</h2>;
|
|
664
|
+
}
|
|
665
|
+
function Contact() {
|
|
666
|
+
return <h2>Contact Page</h2>;
|
|
667
|
+
}
|
|
668
|
+
function App() {
|
|
669
|
+
return (
|
|
670
|
+
<BrowserRouter>
|
|
671
|
+
<nav>
|
|
672
|
+
<Link to="/">Home</Link>
|
|
673
|
+
<Link to="/about">About</Link>
|
|
674
|
+
<Link to="/contact">Contact</Link>
|
|
675
|
+
</nav>
|
|
676
|
+
<Routes>
|
|
677
|
+
<Route
|
|
678
|
+
path="/"
|
|
679
|
+
element={<Home />}
|
|
680
|
+
/>
|
|
681
|
+
<Route
|
|
682
|
+
path="/about"
|
|
683
|
+
element={<About />}
|
|
684
|
+
/>
|
|
685
|
+
<Route
|
|
686
|
+
path="/contact"
|
|
687
|
+
element={<Contact />}
|
|
688
|
+
/>
|
|
689
|
+
</Routes>
|
|
690
|
+
</BrowserRouter>
|
|
691
|
+
);
|
|
692
|
+
}
|
|
693
|
+
export default App;
|
|
694
|
+
/* Program 9 App.css */
|
|
695
|
+
nav {
|
|
696
|
+
margin: 20px;
|
|
697
|
+
}
|
|
698
|
+
nav a {
|
|
699
|
+
margin-right: 15px;
|
|
700
|
+
text-decoration: none;
|
|
701
|
+
color: blue;
|
|
702
|
+
font-size: 20px;
|
|
703
|
+
}
|
|
704
|
+
nav a:hover {
|
|
705
|
+
color: red;
|
|
706
|
+
```
|
|
707
|
+
|
|
708
|
+
```
|
|
709
|
+
}
|
|
710
|
+
// Program 10 App.js
|
|
711
|
+
import React, { Component } from "react";
|
|
712
|
+
import "./App.css";
|
|
713
|
+
class App extends Component {
|
|
714
|
+
state = {
|
|
715
|
+
data: [],
|
|
716
|
+
search: "",
|
|
717
|
+
error: ""
|
|
718
|
+
};
|
|
719
|
+
componentDidMount() {
|
|
720
|
+
fetch("https://jsonplaceholder.typicode.com/users")
|
|
721
|
+
.then((res) => res.json())
|
|
722
|
+
.then((result) =>
|
|
723
|
+
this.setState({ data: result })
|
|
724
|
+
)
|
|
725
|
+
.catch(() =>
|
|
726
|
+
this.setState({
|
|
727
|
+
error: "Data not found"
|
|
728
|
+
})
|
|
729
|
+
);
|
|
730
|
+
}
|
|
731
|
+
componentDidUpdate(prevProps, prevState) {
|
|
732
|
+
if (
|
|
733
|
+
prevState.search !== this.state.search
|
|
734
|
+
) {
|
|
735
|
+
console.log("Search Updated");
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
render() {
|
|
739
|
+
let users = this.state.data.filter((u) =>
|
|
740
|
+
u.name
|
|
741
|
+
.toLowerCase()
|
|
742
|
+
.includes(
|
|
743
|
+
this.state.search.toLowerCase()
|
|
744
|
+
)
|
|
745
|
+
);
|
|
746
|
+
return (
|
|
747
|
+
<div className="box">
|
|
748
|
+
<h2>API Data</h2>
|
|
749
|
+
<input
|
|
750
|
+
type="text"
|
|
751
|
+
placeholder="Search"
|
|
752
|
+
onChange={(e) =>
|
|
753
|
+
this.setState({
|
|
754
|
+
search: e.target.value
|
|
755
|
+
})
|
|
756
|
+
}
|
|
757
|
+
/>
|
|
758
|
+
<button
|
|
759
|
+
onClick={() =>
|
|
760
|
+
window.location.reload()
|
|
761
|
+
}
|
|
762
|
+
>
|
|
763
|
+
Refresh
|
|
764
|
+
```
|
|
765
|
+
|
|
766
|
+
```
|
|
767
|
+
</button>
|
|
768
|
+
```
|
|
769
|
+
|
|
770
|
+
```
|
|
771
|
+
<p>{this.state.error}</p>
|
|
772
|
+
{users.map((user) => (
|
|
773
|
+
<div
|
|
774
|
+
className="card"
|
|
775
|
+
key={user.id}
|
|
776
|
+
>
|
|
777
|
+
<h3>{user.name}</h3>
|
|
778
|
+
<p>{user.email}</p>
|
|
779
|
+
</div>
|
|
780
|
+
))}
|
|
781
|
+
</div>
|
|
782
|
+
);
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
export default App;
|
|
786
|
+
/* Program 10 App.css */
|
|
787
|
+
body {
|
|
788
|
+
font-family: Arial;
|
|
789
|
+
background: #f2f2f2;
|
|
790
|
+
}
|
|
791
|
+
.box {
|
|
792
|
+
width: 350px;
|
|
793
|
+
margin: auto;
|
|
794
|
+
padding: 20px;
|
|
795
|
+
}
|
|
796
|
+
input {
|
|
797
|
+
padding: 8px;
|
|
798
|
+
width: 60%;
|
|
799
|
+
}
|
|
800
|
+
button {
|
|
801
|
+
padding: 8px;
|
|
802
|
+
margin-left: 5px;
|
|
803
|
+
}
|
|
804
|
+
.card {
|
|
805
|
+
background: white;
|
|
806
|
+
padding: 10px;
|
|
807
|
+
margin-top: 10px;
|
|
808
|
+
border-radius: 5px;
|
|
809
|
+
}
|
|
810
|
+
/////For execution /////
|
|
811
|
+
```
|
|
812
|
+
|
|
813
|
+
```
|
|
814
|
+
Experiment 9 = React Router Navigation Application.
|
|
815
|
+
```
|
|
816
|
+
|
|
817
|
+
```
|
|
818
|
+
src/
|
|
819
|
+
│
|
|
820
|
+
├── App.js
|
|
821
|
+
├── App.css
|
|
822
|
+
├── Home.js
|
|
823
|
+
├── About.js
|
|
824
|
+
├── Contact.js
|
|
825
|
+
└── Navbar.js
|
|
826
|
+
```
|
|
827
|
+
|
|
828
|
+
```
|
|
829
|
+
npm install react-router-dom
|
|
830
|
+
```
|
|
831
|
+
|
|
832
|
+
```
|
|
833
|
+
// Home.js
|
|
834
|
+
function Home() {
|
|
835
|
+
return <h1>Welcome to the Home Page</h1>;
|
|
836
|
+
}
|
|
837
|
+
export default Home;
|
|
838
|
+
// About.js
|
|
839
|
+
function About() {
|
|
840
|
+
return <h1>About Us</h1>;
|
|
841
|
+
}
|
|
842
|
+
export default About;
|
|
843
|
+
// Contact.js
|
|
844
|
+
function Contact() {
|
|
845
|
+
return <h1>Contact Us</h1>;
|
|
846
|
+
}
|
|
847
|
+
export default Contact;
|
|
848
|
+
// Navbar.js
|
|
849
|
+
import { NavLink } from "react-router-dom";
|
|
850
|
+
function Navbar() {
|
|
851
|
+
return (
|
|
852
|
+
<nav>
|
|
853
|
+
<NavLink to="/" className="active">
|
|
854
|
+
Home
|
|
855
|
+
</NavLink>
|
|
856
|
+
<NavLink to="/about" className="active">
|
|
857
|
+
About
|
|
858
|
+
</NavLink>
|
|
859
|
+
<NavLink to="/contact" className="active">
|
|
860
|
+
Contact
|
|
861
|
+
</NavLink>
|
|
862
|
+
</nav>
|
|
863
|
+
);
|
|
864
|
+
}
|
|
865
|
+
export default Navbar;
|
|
866
|
+
// App.js
|
|
867
|
+
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
|
|
868
|
+
import Navbar from "./Navbar";
|
|
869
|
+
import Home from "./Home";
|
|
870
|
+
import About from "./About";
|
|
871
|
+
import Contact from "./Contact";
|
|
872
|
+
function App() {
|
|
873
|
+
return (
|
|
874
|
+
```
|
|
875
|
+
|
|
876
|
+
```
|
|
877
|
+
<Router>
|
|
878
|
+
<Navbar />
|
|
879
|
+
<Routes>
|
|
880
|
+
<Route path="/" element={<Home />} />
|
|
881
|
+
<Route path="/about" element={<About />} />
|
|
882
|
+
<Route path="/contact" element={<Contact />} />
|
|
883
|
+
</Routes>
|
|
884
|
+
</Router>
|
|
885
|
+
);
|
|
886
|
+
}
|
|
887
|
+
export default App;
|
|
888
|
+
/* App.css */
|
|
889
|
+
nav a {
|
|
890
|
+
margin: 10px;
|
|
891
|
+
text-decoration: none;
|
|
892
|
+
color: black;
|
|
893
|
+
}
|
|
894
|
+
.active {
|
|
895
|
+
font-weight: bold;
|
|
896
|
+
color: blue;
|
|
897
|
+
}
|
|
898
|
+
```
|
|
899
|
+
|
|
900
|
+
```
|
|
901
|
+
///Program 8 for execution///
|
|
902
|
+
src/
|
|
903
|
+
│
|
|
904
|
+
├── App.js
|
|
905
|
+
│
|
|
906
|
+
└── components/
|
|
907
|
+
├── AddTask.js
|
|
908
|
+
├── Task.js
|
|
909
|
+
└── TaskList.js
|
|
910
|
+
components/Task.js
|
|
911
|
+
import React from "react";
|
|
912
|
+
const Task = ({ task, toggleComplete }) => {
|
|
913
|
+
return (
|
|
914
|
+
<div
|
|
915
|
+
className="task"
|
|
916
|
+
style={{
|
|
917
|
+
textDecoration: task.completed
|
|
918
|
+
? "line-through"
|
|
919
|
+
: "none"
|
|
920
|
+
}}
|
|
921
|
+
>
|
|
922
|
+
<h3>{task.name}</h3>
|
|
923
|
+
<p>Due: {task.dueDate}</p>
|
|
924
|
+
<button
|
|
925
|
+
onClick={() =>
|
|
926
|
+
toggleComplete(task.id)
|
|
927
|
+
}
|
|
928
|
+
>
|
|
929
|
+
```
|
|
930
|
+
|
|
931
|
+
```
|
|
932
|
+
{task.completed
|
|
933
|
+
? "Mark as Incomplete"
|
|
934
|
+
: "Mark as Completed"}
|
|
935
|
+
</button>
|
|
936
|
+
</div>
|
|
937
|
+
);
|
|
938
|
+
};
|
|
939
|
+
export default Task;
|
|
940
|
+
```
|
|
941
|
+
|
|
942
|
+
```
|
|
943
|
+
components/TaskList.js
|
|
944
|
+
import React from "react";
|
|
945
|
+
import Task from "./Task";
|
|
946
|
+
const TaskList = ({
|
|
947
|
+
tasks,
|
|
948
|
+
toggleComplete
|
|
949
|
+
}) => {
|
|
950
|
+
return (
|
|
951
|
+
<div>
|
|
952
|
+
{tasks.map((task) => (
|
|
953
|
+
<Task
|
|
954
|
+
key={task.id}
|
|
955
|
+
task={task}
|
|
956
|
+
toggleComplete={toggleComplete}
|
|
957
|
+
/>
|
|
958
|
+
))}
|
|
959
|
+
</div>
|
|
960
|
+
);
|
|
961
|
+
};
|
|
962
|
+
export default TaskList;
|
|
963
|
+
```
|
|
964
|
+
|
|
965
|
+
```
|
|
966
|
+
components/AddTask.js
|
|
967
|
+
import React, { useState } from "react";
|
|
968
|
+
const AddTask = ({ addTask }) => {
|
|
969
|
+
const [taskName, setTaskName] =
|
|
970
|
+
useState("");
|
|
971
|
+
const [dueDate, setDueDate] =
|
|
972
|
+
useState("");
|
|
973
|
+
const handleSubmit = (e) => {
|
|
974
|
+
e.preventDefault();
|
|
975
|
+
if (taskName.trim()) {
|
|
976
|
+
addTask(taskName, dueDate);
|
|
977
|
+
setTaskName("");
|
|
978
|
+
setDueDate("");
|
|
979
|
+
}
|
|
980
|
+
};
|
|
981
|
+
return (
|
|
982
|
+
<form onSubmit={handleSubmit}>
|
|
983
|
+
<input
|
|
984
|
+
type="text"
|
|
985
|
+
placeholder="Task Name"
|
|
986
|
+
```
|
|
987
|
+
|
|
988
|
+
```
|
|
989
|
+
value={taskName}
|
|
990
|
+
onChange={(e) =>
|
|
991
|
+
setTaskName(e.target.value)
|
|
992
|
+
}
|
|
993
|
+
required
|
|
994
|
+
/>
|
|
995
|
+
<input
|
|
996
|
+
type="date"
|
|
997
|
+
value={dueDate}
|
|
998
|
+
onChange={(e) =>
|
|
999
|
+
setDueDate(e.target.value)
|
|
1000
|
+
}
|
|
1001
|
+
/>
|
|
1002
|
+
<button type="submit">
|
|
1003
|
+
Add Task
|
|
1004
|
+
</button>
|
|
1005
|
+
</form>
|
|
1006
|
+
);
|
|
1007
|
+
};
|
|
1008
|
+
export default AddTask;
|
|
1009
|
+
```
|
|
1010
|
+
|
|
1011
|
+
```
|
|
1012
|
+
App.js
|
|
1013
|
+
```
|
|
1014
|
+
|
|
1015
|
+
```
|
|
1016
|
+
import React, { useState } from "react";
|
|
1017
|
+
import AddTask from "./components/AddTask";
|
|
1018
|
+
import TaskList from "./components/TaskList";
|
|
1019
|
+
function App() {
|
|
1020
|
+
const [tasks, setTasks] = useState([]);
|
|
1021
|
+
const [filter, setFilter] =
|
|
1022
|
+
useState("all");
|
|
1023
|
+
// Add New Task
|
|
1024
|
+
const addTask = (name, dueDate) => {
|
|
1025
|
+
const newTask = {
|
|
1026
|
+
id: Date.now(),
|
|
1027
|
+
name,
|
|
1028
|
+
dueDate,
|
|
1029
|
+
completed: false
|
|
1030
|
+
};
|
|
1031
|
+
setTasks([...tasks, newTask]);
|
|
1032
|
+
};
|
|
1033
|
+
// Mark Task Complete / Incomplete
|
|
1034
|
+
const toggleComplete = (id) => {
|
|
1035
|
+
setTasks(
|
|
1036
|
+
tasks.map((task) =>
|
|
1037
|
+
task.id === id
|
|
1038
|
+
? {
|
|
1039
|
+
...task,
|
|
1040
|
+
completed:
|
|
1041
|
+
!task.completed
|
|
1042
|
+
}
|
|
1043
|
+
: task
|
|
1044
|
+
```
|
|
1045
|
+
|
|
1046
|
+
```
|
|
1047
|
+
)
|
|
1048
|
+
);
|
|
1049
|
+
};
|
|
1050
|
+
// Filter Tasks
|
|
1051
|
+
const filteredTasks = tasks.filter(
|
|
1052
|
+
(task) =>
|
|
1053
|
+
filter === "all"
|
|
1054
|
+
? true
|
|
1055
|
+
: filter === "completed"
|
|
1056
|
+
? task.completed
|
|
1057
|
+
: !task.completed
|
|
1058
|
+
);
|
|
1059
|
+
return (
|
|
1060
|
+
<div>
|
|
1061
|
+
<h1>
|
|
1062
|
+
Reminder Application
|
|
1063
|
+
</h1>
|
|
1064
|
+
<AddTask addTask={addTask} />
|
|
1065
|
+
<div>
|
|
1066
|
+
<button
|
|
1067
|
+
onClick={() =>
|
|
1068
|
+
setFilter("all")
|
|
1069
|
+
}
|
|
1070
|
+
>
|
|
1071
|
+
All Tasks
|
|
1072
|
+
</button>
|
|
1073
|
+
<button
|
|
1074
|
+
onClick={() =>
|
|
1075
|
+
setFilter("completed")
|
|
1076
|
+
}
|
|
1077
|
+
>
|
|
1078
|
+
Completed
|
|
1079
|
+
</button>
|
|
1080
|
+
<button
|
|
1081
|
+
onClick={() =>
|
|
1082
|
+
setFilter("pending")
|
|
1083
|
+
}
|
|
1084
|
+
>
|
|
1085
|
+
Pending
|
|
1086
|
+
</button>
|
|
1087
|
+
</div>
|
|
1088
|
+
<TaskList
|
|
1089
|
+
tasks={filteredTasks}
|
|
1090
|
+
toggleComplete={
|
|
1091
|
+
toggleComplete
|
|
1092
|
+
}
|
|
1093
|
+
/>
|
|
1094
|
+
</div>
|
|
1095
|
+
);
|
|
1096
|
+
}
|
|
1097
|
+
export default App;
|
|
1098
|
+
```
|
|
1099
|
+
|