fpythons 0.1.0__py3-none-any.whl
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.
- fpythons/__init__.py +12 -0
- fpythons/__main__.py +4 -0
- fpythons/data/00_START_HERE.md +144 -0
- fpythons/data/ALL_SLIPS_CODE.txt +1854 -0
- fpythons/data/README.md +35 -0
- fpythons/data/Slip_01.md +160 -0
- fpythons/data/Slip_02.md +104 -0
- fpythons/data/Slip_03.md +95 -0
- fpythons/data/Slip_04.md +131 -0
- fpythons/data/Slip_05.md +120 -0
- fpythons/data/Slip_06.md +161 -0
- fpythons/data/Slip_07.md +121 -0
- fpythons/data/Slip_08.md +95 -0
- fpythons/data/Slip_09.md +92 -0
- fpythons/data/Slip_10.md +113 -0
- fpythons/data/Slip_11.md +109 -0
- fpythons/data/Slip_12.md +181 -0
- fpythons/data/Slip_13.md +163 -0
- fpythons/data/Slip_14.md +141 -0
- fpythons/data/Slip_15.md +100 -0
- fpythons/data/Slip_16.md +127 -0
- fpythons/data/Slip_17.md +93 -0
- fpythons/data/Slip_18.md +96 -0
- fpythons/data/Slip_19.md +94 -0
- fpythons/data/Slip_20.md +165 -0
- fpythons/data/Slip_21.md +122 -0
- fpythons/data/VIVA.md +195 -0
- fpythons/data/code/slip1/.gitignore +2 -0
- fpythons/data/code/slip1/package.json +31 -0
- fpythons/data/code/slip1/public/index.html +12 -0
- fpythons/data/code/slip1/src/App.js +7 -0
- fpythons/data/code/slip1/src/index.js +10 -0
- fpythons/data/code/slip1/src/slip1/loginForm.js +26 -0
- fpythons/data/code/slip1/src/slip1/style.css +46 -0
- fpythons/data/code/slip10/.gitignore +2 -0
- fpythons/data/code/slip10/package.json +31 -0
- fpythons/data/code/slip10/public/index.html +12 -0
- fpythons/data/code/slip10/src/App.js +7 -0
- fpythons/data/code/slip10/src/index.js +10 -0
- fpythons/data/code/slip10/src/slip10/SumOfDigits.js +37 -0
- fpythons/data/code/slip11/.gitignore +2 -0
- fpythons/data/code/slip11/package.json +31 -0
- fpythons/data/code/slip11/public/index.html +12 -0
- fpythons/data/code/slip11/src/App.js +7 -0
- fpythons/data/code/slip11/src/index.js +10 -0
- fpythons/data/code/slip11/src/slip11/StringOperation.js +33 -0
- fpythons/data/code/slip12/.gitignore +2 -0
- fpythons/data/code/slip12/package.json +33 -0
- fpythons/data/code/slip12/public/index.html +12 -0
- fpythons/data/code/slip12/src/App.js +7 -0
- fpythons/data/code/slip12/src/index.js +14 -0
- fpythons/data/code/slip12/src/slip12/NotesManagementApp.js +35 -0
- fpythons/data/code/slip12/src/slip12/redux/notesSlice.js +20 -0
- fpythons/data/code/slip12/src/slip12/redux/store.js +10 -0
- fpythons/data/code/slip13/.gitignore +2 -0
- fpythons/data/code/slip13/package.json +33 -0
- fpythons/data/code/slip13/public/index.html +12 -0
- fpythons/data/code/slip13/src/App.js +7 -0
- fpythons/data/code/slip13/src/index.js +14 -0
- fpythons/data/code/slip13/src/slip13/Counter.js +20 -0
- fpythons/data/code/slip13/src/slip13/redux/counterSlice.js +19 -0
- fpythons/data/code/slip13/src/slip13/redux/store.js +10 -0
- fpythons/data/code/slip14/.gitignore +2 -0
- fpythons/data/code/slip14/package.json +31 -0
- fpythons/data/code/slip14/public/index.html +12 -0
- fpythons/data/code/slip14/src/App.js +7 -0
- fpythons/data/code/slip14/src/index.js +10 -0
- fpythons/data/code/slip14/src/slip14/Calculator.js +64 -0
- fpythons/data/code/slip15/.gitignore +2 -0
- fpythons/data/code/slip15/package.json +31 -0
- fpythons/data/code/slip15/public/index.html +12 -0
- fpythons/data/code/slip15/src/App.js +7 -0
- fpythons/data/code/slip15/src/index.js +10 -0
- fpythons/data/code/slip15/src/slip15/Counter.js +24 -0
- fpythons/data/code/slip16/.gitignore +2 -0
- fpythons/data/code/slip16/package.json +31 -0
- fpythons/data/code/slip16/public/index.html +12 -0
- fpythons/data/code/slip16/src/App.js +7 -0
- fpythons/data/code/slip16/src/index.js +10 -0
- fpythons/data/code/slip16/src/slip16/TraditionalDOM.html +24 -0
- fpythons/data/code/slip16/src/slip16/VirtualDOM.js +16 -0
- fpythons/data/code/slip17/.gitignore +2 -0
- fpythons/data/code/slip17/package.json +31 -0
- fpythons/data/code/slip17/public/index.html +12 -0
- fpythons/data/code/slip17/src/App.js +7 -0
- fpythons/data/code/slip17/src/index.js +10 -0
- fpythons/data/code/slip17/src/slip17/Sign-in.js +17 -0
- fpythons/data/code/slip18/.gitignore +2 -0
- fpythons/data/code/slip18/package.json +31 -0
- fpythons/data/code/slip18/public/index.html +12 -0
- fpythons/data/code/slip18/public/logo192.png +0 -0
- fpythons/data/code/slip18/src/App.js +7 -0
- fpythons/data/code/slip18/src/index.js +10 -0
- fpythons/data/code/slip18/src/slip18/Profile.js +20 -0
- fpythons/data/code/slip19/.gitignore +2 -0
- fpythons/data/code/slip19/package.json +31 -0
- fpythons/data/code/slip19/public/index.html +12 -0
- fpythons/data/code/slip19/src/App.js +7 -0
- fpythons/data/code/slip19/src/index.js +10 -0
- fpythons/data/code/slip19/src/slip19/Movies.js +19 -0
- fpythons/data/code/slip2/.gitignore +2 -0
- fpythons/data/code/slip2/package.json +31 -0
- fpythons/data/code/slip2/public/index.html +12 -0
- fpythons/data/code/slip2/src/App.js +7 -0
- fpythons/data/code/slip2/src/index.js +10 -0
- fpythons/data/code/slip2/src/slip2/useEffect.js +23 -0
- fpythons/data/code/slip20/.gitignore +2 -0
- fpythons/data/code/slip20/package.json +32 -0
- fpythons/data/code/slip20/public/index.html +12 -0
- fpythons/data/code/slip20/src/App.js +16 -0
- fpythons/data/code/slip20/src/index.js +10 -0
- fpythons/data/code/slip20/src/slip20/Login.js +45 -0
- fpythons/data/code/slip20/src/slip20/Students.js +27 -0
- fpythons/data/code/slip21/.gitignore +2 -0
- fpythons/data/code/slip21/package.json +31 -0
- fpythons/data/code/slip21/public/index.html +12 -0
- fpythons/data/code/slip21/src/App.js +7 -0
- fpythons/data/code/slip21/src/index.js +10 -0
- fpythons/data/code/slip21/src/slip21/NumberString.js +46 -0
- fpythons/data/code/slip3/.gitignore +2 -0
- fpythons/data/code/slip3/package.json +31 -0
- fpythons/data/code/slip3/public/index.html +12 -0
- fpythons/data/code/slip3/src/App.js +7 -0
- fpythons/data/code/slip3/src/index.js +10 -0
- fpythons/data/code/slip3/src/slip3/toggleBtn.js +16 -0
- fpythons/data/code/slip4/.gitignore +2 -0
- fpythons/data/code/slip4/package.json +32 -0
- fpythons/data/code/slip4/public/index.html +12 -0
- fpythons/data/code/slip4/src/App.js +23 -0
- fpythons/data/code/slip4/src/index.js +10 -0
- fpythons/data/code/slip4/src/slip4/About.js +7 -0
- fpythons/data/code/slip4/src/slip4/Contact.js +7 -0
- fpythons/data/code/slip4/src/slip4/home.js +7 -0
- fpythons/data/code/slip5/.gitignore +2 -0
- fpythons/data/code/slip5/package.json +32 -0
- fpythons/data/code/slip5/public/index.html +12 -0
- fpythons/data/code/slip5/src/App.js +18 -0
- fpythons/data/code/slip5/src/index.js +10 -0
- fpythons/data/code/slip5/src/slip5/loginValidation.js +30 -0
- fpythons/data/code/slip6/.gitignore +2 -0
- fpythons/data/code/slip6/package.json +32 -0
- fpythons/data/code/slip6/public/index.html +12 -0
- fpythons/data/code/slip6/src/App.js +31 -0
- fpythons/data/code/slip6/src/index.js +10 -0
- fpythons/data/code/slip6/src/slip6/Dashboard.js +12 -0
- fpythons/data/code/slip6/src/slip6/Login.js +23 -0
- fpythons/data/code/slip6/src/slip6/ProtectedRoute.js +10 -0
- fpythons/data/code/slip7/.gitignore +2 -0
- fpythons/data/code/slip7/package.json +31 -0
- fpythons/data/code/slip7/public/index.html +12 -0
- fpythons/data/code/slip7/src/App.js +7 -0
- fpythons/data/code/slip7/src/index.js +10 -0
- fpythons/data/code/slip7/src/slip7/TodoList.js +42 -0
- fpythons/data/code/slip8/.gitignore +2 -0
- fpythons/data/code/slip8/package.json +31 -0
- fpythons/data/code/slip8/public/index.html +12 -0
- fpythons/data/code/slip8/src/App.js +7 -0
- fpythons/data/code/slip8/src/index.js +10 -0
- fpythons/data/code/slip8/src/slip8/ToggleButton.js +19 -0
- fpythons/data/code/slip9/.gitignore +2 -0
- fpythons/data/code/slip9/package.json +31 -0
- fpythons/data/code/slip9/public/index.html +12 -0
- fpythons/data/code/slip9/src/App.js +7 -0
- fpythons/data/code/slip9/src/index.js +10 -0
- fpythons/data/code/slip9/src/slip9/Counter.js +16 -0
- fpythons/main.py +226 -0
- fpythons-0.1.0.dist-info/METADATA +64 -0
- fpythons-0.1.0.dist-info/RECORD +170 -0
- fpythons-0.1.0.dist-info/WHEEL +4 -0
- fpythons-0.1.0.dist-info/entry_points.txt +2 -0
|
@@ -0,0 +1,1854 @@
|
|
|
1
|
+
ALL SLIPS CODE : React practical, slip-wise
|
|
2
|
+
|
|
3
|
+
Each slip has: question, commands, folder structure, then every file with its path and code.
|
|
4
|
+
|
|
5
|
+
CONTENTS
|
|
6
|
+
|
|
7
|
+
Slip 1 : Login form with event handlers
|
|
8
|
+
Slip 2 : useEffect and fetching a users list
|
|
9
|
+
Slip 3 : Toggle ON/OFF with background colour
|
|
10
|
+
Slip 4 : Multi-page app with react-router-dom
|
|
11
|
+
Slip 5 : Login validation and 404 route
|
|
12
|
+
Slip 6 : Protected route with mock authentication
|
|
13
|
+
Slip 7 : To-do list
|
|
14
|
+
Slip 8 : Toggle button ON/OFF
|
|
15
|
+
Slip 9 : Counter with initial value as a prop
|
|
16
|
+
Slip 10 : Validate a number and sum its digits
|
|
17
|
+
Slip 11 : String operations
|
|
18
|
+
Slip 12 : Notes app with Redux Toolkit
|
|
19
|
+
Slip 13 : Counter with a Redux store
|
|
20
|
+
Slip 14 : Calculator
|
|
21
|
+
Slip 15 : Counter using useReducer
|
|
22
|
+
Slip 16 : Virtual DOM and traditional DOM
|
|
23
|
+
Slip 17 : Sign in and sign out
|
|
24
|
+
Slip 18 : Profile page
|
|
25
|
+
Slip 19 : List of 10 favourite movies
|
|
26
|
+
Slip 20 : Login with route parameters
|
|
27
|
+
Slip 21 : Even/odd and palindrome
|
|
28
|
+
|
|
29
|
+
################################################################################
|
|
30
|
+
# SLIP 1 : Login form with event handlers
|
|
31
|
+
################################################################################
|
|
32
|
+
|
|
33
|
+
QUESTION
|
|
34
|
+
|
|
35
|
+
Create a React application which contains a user login form with styling, with event handlers for onInput, onSubmit and onChange. Display the entered values in the console.
|
|
36
|
+
|
|
37
|
+
COMMANDS
|
|
38
|
+
|
|
39
|
+
npx create-react-app slip1
|
|
40
|
+
cd slip1
|
|
41
|
+
mkdir src\slip1
|
|
42
|
+
code .
|
|
43
|
+
npm start
|
|
44
|
+
|
|
45
|
+
FOLDER STRUCTURE
|
|
46
|
+
|
|
47
|
+
slip1/
|
|
48
|
+
`-- src/
|
|
49
|
+
|-- slip1/
|
|
50
|
+
| |-- loginForm.js
|
|
51
|
+
| `-- style.css
|
|
52
|
+
|-- App.js
|
|
53
|
+
`-- index.js
|
|
54
|
+
|
|
55
|
+
--------------------------------------------------------------------------------
|
|
56
|
+
FILE : style.css
|
|
57
|
+
PATH : slip1/src/slip1/style.css
|
|
58
|
+
--------------------------------------------------------------------------------
|
|
59
|
+
body{
|
|
60
|
+
margin: 0;
|
|
61
|
+
padding: 0;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
.login-container{
|
|
65
|
+
display: flex;
|
|
66
|
+
justify-content: center;
|
|
67
|
+
align-items: center;
|
|
68
|
+
flex-direction: column;
|
|
69
|
+
height: 100vh;
|
|
70
|
+
background-color: rgba(0, 255, 255, 0.263);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
.heading{
|
|
74
|
+
font-size: 2rem;
|
|
75
|
+
text-align: center;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
.login-form{
|
|
79
|
+
display: flex;
|
|
80
|
+
flex-direction: column;
|
|
81
|
+
border: 2px solid #ccc;
|
|
82
|
+
border-radius: 8px;
|
|
83
|
+
width: 300px;
|
|
84
|
+
height: 150px;
|
|
85
|
+
padding: 10px;
|
|
86
|
+
gap: 5px;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
.login-form input {
|
|
90
|
+
padding: 6px;
|
|
91
|
+
border: 1px solid #ccc;
|
|
92
|
+
border-radius: 5px;
|
|
93
|
+
font-size: 14px;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
.login-form input[type="submit"] {
|
|
97
|
+
background-color: #2563eb;
|
|
98
|
+
color: white;
|
|
99
|
+
border: none;
|
|
100
|
+
padding: 6px;
|
|
101
|
+
border-radius: 5px;
|
|
102
|
+
font-size: 16px;
|
|
103
|
+
cursor: pointer;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
--------------------------------------------------------------------------------
|
|
107
|
+
FILE : loginForm.js
|
|
108
|
+
PATH : slip1/src/slip1/loginForm.js
|
|
109
|
+
--------------------------------------------------------------------------------
|
|
110
|
+
import React, { useState } from 'react'
|
|
111
|
+
import './style.css';
|
|
112
|
+
|
|
113
|
+
const LoginForm = () => {
|
|
114
|
+
|
|
115
|
+
const [username, setUsername] = useState("");
|
|
116
|
+
const [password, setPassword] = useState("");
|
|
117
|
+
|
|
118
|
+
function handleSubmit(e){
|
|
119
|
+
e.preventDefault();
|
|
120
|
+
console.log("Username:", username);
|
|
121
|
+
console.log("Password:", password);
|
|
122
|
+
};
|
|
123
|
+
return (
|
|
124
|
+
<div className="login-container">
|
|
125
|
+
<h2 className="heading">Login Form</h2>
|
|
126
|
+
<form onSubmit={handleSubmit} className="login-form">
|
|
127
|
+
<label>Username<input type='text' placeholder='Enter username' value={username} onInput={(e) => console.log(e.target.value)} onChange={(e) => setUsername(e.target.value)}></input></label><br></br>
|
|
128
|
+
<label>Password<input type='password' placeholder='Enter password' value={password} onInput={(e) => console.log(e.target.value)} onChange={(e) => setPassword(e.target.value)}></input></label><br></br>
|
|
129
|
+
<input type='submit' value="Login"></input>
|
|
130
|
+
</form>
|
|
131
|
+
</div>
|
|
132
|
+
)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export default LoginForm
|
|
136
|
+
|
|
137
|
+
--------------------------------------------------------------------------------
|
|
138
|
+
FILE : App.js
|
|
139
|
+
PATH : slip1/src/App.js
|
|
140
|
+
--------------------------------------------------------------------------------
|
|
141
|
+
import LoginForm from './slip1/loginForm';
|
|
142
|
+
|
|
143
|
+
function App() {
|
|
144
|
+
return <LoginForm />;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export default App;
|
|
148
|
+
|
|
149
|
+
################################################################################
|
|
150
|
+
# SLIP 2 : useEffect and fetching a users list
|
|
151
|
+
################################################################################
|
|
152
|
+
|
|
153
|
+
QUESTION
|
|
154
|
+
|
|
155
|
+
Create a React functional component using useEffect() that fetches and displays data from an API (for example a users list) on the console.
|
|
156
|
+
|
|
157
|
+
COMMANDS
|
|
158
|
+
|
|
159
|
+
npx create-react-app slip2
|
|
160
|
+
cd slip2
|
|
161
|
+
mkdir src\slip2
|
|
162
|
+
code .
|
|
163
|
+
npm start
|
|
164
|
+
|
|
165
|
+
FOLDER STRUCTURE
|
|
166
|
+
|
|
167
|
+
slip2/
|
|
168
|
+
`-- src/
|
|
169
|
+
|-- slip2/
|
|
170
|
+
| `-- useEffect.js
|
|
171
|
+
|-- App.js
|
|
172
|
+
`-- index.js
|
|
173
|
+
|
|
174
|
+
--------------------------------------------------------------------------------
|
|
175
|
+
FILE : useEffect.js
|
|
176
|
+
PATH : slip2/src/slip2/useEffect.js
|
|
177
|
+
--------------------------------------------------------------------------------
|
|
178
|
+
import React, { useState, useEffect } from 'react'
|
|
179
|
+
|
|
180
|
+
const UseEffect = () => {
|
|
181
|
+
const [users, setUsers] = useState([]);
|
|
182
|
+
|
|
183
|
+
useEffect(() =>{
|
|
184
|
+
fetch("https://jsonplaceholder.typicode.com/users")
|
|
185
|
+
.then(res => res.json())
|
|
186
|
+
.then(data =>{ setUsers(data);
|
|
187
|
+
console.log(data);
|
|
188
|
+
})
|
|
189
|
+
}, [])
|
|
190
|
+
return (
|
|
191
|
+
<div>
|
|
192
|
+
<h1>User List:</h1>
|
|
193
|
+
{users.map(user =>(
|
|
194
|
+
<p key={user.id}>{user.name}</p>
|
|
195
|
+
))}
|
|
196
|
+
</div>
|
|
197
|
+
)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export default UseEffect
|
|
201
|
+
|
|
202
|
+
--------------------------------------------------------------------------------
|
|
203
|
+
FILE : App.js
|
|
204
|
+
PATH : slip2/src/App.js
|
|
205
|
+
--------------------------------------------------------------------------------
|
|
206
|
+
import UseEffect from './slip2/useEffect';
|
|
207
|
+
|
|
208
|
+
function App() {
|
|
209
|
+
return <UseEffect />;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export default App;
|
|
213
|
+
|
|
214
|
+
################################################################################
|
|
215
|
+
# SLIP 3 : Toggle ON/OFF with background colour
|
|
216
|
+
################################################################################
|
|
217
|
+
|
|
218
|
+
QUESTION
|
|
219
|
+
|
|
220
|
+
Create a button that toggles between ON and OFF states when clicked. Change the background colour of the UI when the button state changes.
|
|
221
|
+
|
|
222
|
+
COMMANDS
|
|
223
|
+
|
|
224
|
+
npx create-react-app slip3
|
|
225
|
+
cd slip3
|
|
226
|
+
mkdir src\slip3
|
|
227
|
+
code .
|
|
228
|
+
npm start
|
|
229
|
+
|
|
230
|
+
FOLDER STRUCTURE
|
|
231
|
+
|
|
232
|
+
slip3/
|
|
233
|
+
`-- src/
|
|
234
|
+
|-- slip3/
|
|
235
|
+
| `-- toggleBtn.js
|
|
236
|
+
|-- App.js
|
|
237
|
+
`-- index.js
|
|
238
|
+
|
|
239
|
+
--------------------------------------------------------------------------------
|
|
240
|
+
FILE : toggleBtn.js
|
|
241
|
+
PATH : slip3/src/slip3/toggleBtn.js
|
|
242
|
+
--------------------------------------------------------------------------------
|
|
243
|
+
import React, { useState } from 'react'
|
|
244
|
+
|
|
245
|
+
const ToggleBtn = () => {
|
|
246
|
+
const [status, setStatus] = useState('ON');
|
|
247
|
+
function checkStatus(){
|
|
248
|
+
setStatus(status === 'ON' ? 'OFF' : 'ON');
|
|
249
|
+
}
|
|
250
|
+
return (
|
|
251
|
+
<div style = {{backgroundColor : status === 'ON' ? 'lightgreen' : 'lightcoral'}}>
|
|
252
|
+
<h2>{status}</h2>
|
|
253
|
+
<button onClick={checkStatus}>{status === 'ON' ? 'Turn OFF' : 'Turn ON'}</button>
|
|
254
|
+
</div>
|
|
255
|
+
)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export default ToggleBtn
|
|
259
|
+
|
|
260
|
+
--------------------------------------------------------------------------------
|
|
261
|
+
FILE : App.js
|
|
262
|
+
PATH : slip3/src/App.js
|
|
263
|
+
--------------------------------------------------------------------------------
|
|
264
|
+
import ToggleBtn from './slip3/toggleBtn';
|
|
265
|
+
|
|
266
|
+
function App() {
|
|
267
|
+
return <ToggleBtn />;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export default App;
|
|
271
|
+
|
|
272
|
+
################################################################################
|
|
273
|
+
# SLIP 4 : Multi-page app with react-router-dom
|
|
274
|
+
################################################################################
|
|
275
|
+
|
|
276
|
+
QUESTION
|
|
277
|
+
|
|
278
|
+
Create a multi-page React application using react-router-dom with Home, About and Contact pages. For each route display appropriate UI with validation.
|
|
279
|
+
|
|
280
|
+
COMMANDS
|
|
281
|
+
|
|
282
|
+
npx create-react-app slip4
|
|
283
|
+
cd slip4
|
|
284
|
+
npm install react-router-dom
|
|
285
|
+
mkdir src\slip4
|
|
286
|
+
code .
|
|
287
|
+
npm start
|
|
288
|
+
|
|
289
|
+
FOLDER STRUCTURE
|
|
290
|
+
|
|
291
|
+
slip4/
|
|
292
|
+
`-- src/
|
|
293
|
+
|-- slip4/
|
|
294
|
+
| |-- About.js
|
|
295
|
+
| |-- Contact.js
|
|
296
|
+
| `-- home.js
|
|
297
|
+
|-- App.js
|
|
298
|
+
`-- index.js
|
|
299
|
+
|
|
300
|
+
--------------------------------------------------------------------------------
|
|
301
|
+
FILE : About.js
|
|
302
|
+
PATH : slip4/src/slip4/About.js
|
|
303
|
+
--------------------------------------------------------------------------------
|
|
304
|
+
function About(){
|
|
305
|
+
return(
|
|
306
|
+
<h1>This is a About page.</h1>
|
|
307
|
+
)
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
export default About
|
|
311
|
+
|
|
312
|
+
--------------------------------------------------------------------------------
|
|
313
|
+
FILE : Contact.js
|
|
314
|
+
PATH : slip4/src/slip4/Contact.js
|
|
315
|
+
--------------------------------------------------------------------------------
|
|
316
|
+
function Contact(){
|
|
317
|
+
return(
|
|
318
|
+
<h1>Contact Us</h1>
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export default Contact
|
|
323
|
+
|
|
324
|
+
--------------------------------------------------------------------------------
|
|
325
|
+
FILE : home.js
|
|
326
|
+
PATH : slip4/src/slip4/home.js
|
|
327
|
+
--------------------------------------------------------------------------------
|
|
328
|
+
function Home(){
|
|
329
|
+
return(
|
|
330
|
+
<h1>Hello, Welcome to the Home!</h1>
|
|
331
|
+
)
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
export default Home
|
|
335
|
+
|
|
336
|
+
--------------------------------------------------------------------------------
|
|
337
|
+
FILE : App.js
|
|
338
|
+
PATH : slip4/src/App.js
|
|
339
|
+
--------------------------------------------------------------------------------
|
|
340
|
+
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';
|
|
341
|
+
import Home from './slip4/home';
|
|
342
|
+
import About from './slip4/About';
|
|
343
|
+
import Contact from './slip4/Contact';
|
|
344
|
+
|
|
345
|
+
function App() {
|
|
346
|
+
return (
|
|
347
|
+
<BrowserRouter>
|
|
348
|
+
<Link to="/">Home</Link> |{' '}
|
|
349
|
+
<Link to="/about">About</Link> |{' '}
|
|
350
|
+
<Link to="/contact">Contact Us</Link>
|
|
351
|
+
|
|
352
|
+
<Routes>
|
|
353
|
+
<Route path="/" element={<Home />} />
|
|
354
|
+
<Route path="/about" element={<About />} />
|
|
355
|
+
<Route path="/contact" element={<Contact />} />
|
|
356
|
+
<Route path="*" element={<h1>Page Not Found!</h1>} />
|
|
357
|
+
</Routes>
|
|
358
|
+
</BrowserRouter>
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
export default App;
|
|
363
|
+
|
|
364
|
+
################################################################################
|
|
365
|
+
# SLIP 5 : Login validation and 404 route
|
|
366
|
+
################################################################################
|
|
367
|
+
|
|
368
|
+
QUESTION
|
|
369
|
+
|
|
370
|
+
Design a login form which accepts username and password.
|
|
371
|
+
a. On submit, validate them and console log if valid. b. Valid values: username Admin, password Pass123. c. Any other credentials show "Unauthorized Access". d. If the route name is wrong, show "404 Error, Page Not Found".
|
|
372
|
+
|
|
373
|
+
COMMANDS
|
|
374
|
+
|
|
375
|
+
npx create-react-app slip5
|
|
376
|
+
cd slip5
|
|
377
|
+
npm install react-router-dom
|
|
378
|
+
mkdir src\slip5
|
|
379
|
+
code .
|
|
380
|
+
npm start
|
|
381
|
+
|
|
382
|
+
FOLDER STRUCTURE
|
|
383
|
+
|
|
384
|
+
slip5/
|
|
385
|
+
`-- src/
|
|
386
|
+
|-- slip5/
|
|
387
|
+
| `-- loginValidation.js
|
|
388
|
+
|-- App.js
|
|
389
|
+
`-- index.js
|
|
390
|
+
|
|
391
|
+
--------------------------------------------------------------------------------
|
|
392
|
+
FILE : loginValidation.js
|
|
393
|
+
PATH : slip5/src/slip5/loginValidation.js
|
|
394
|
+
--------------------------------------------------------------------------------
|
|
395
|
+
import React, { useState } from 'react'
|
|
396
|
+
|
|
397
|
+
const LoginValidation = () => {
|
|
398
|
+
const [username, setUsername] = useState('');
|
|
399
|
+
const [password, setPassword] = useState('');
|
|
400
|
+
const [message, setMessage] = useState('');
|
|
401
|
+
|
|
402
|
+
function validateLogin(e){
|
|
403
|
+
e.preventDefault();
|
|
404
|
+
if(username === 'Admin' && password === 'Pass123'){
|
|
405
|
+
console.log("Login successful.");
|
|
406
|
+
}
|
|
407
|
+
else{
|
|
408
|
+
setMessage("Unauthorized access.")
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
return (
|
|
412
|
+
<div>
|
|
413
|
+
<h1>Login Form</h1>
|
|
414
|
+
<form onSubmit={validateLogin}>
|
|
415
|
+
<label>Username:<input type='text' placeholder='Enter username' value={username} onChange={(e) => setUsername(e.target.value)}></input></label><br></br>
|
|
416
|
+
<label>Password:<input type='password' placeholder='Enter password' value={password} onChange={(e) => setPassword(e.target.value)}></input></label><br></br>
|
|
417
|
+
<input type='submit' value='Login'></input>
|
|
418
|
+
</form>
|
|
419
|
+
<p>{message}</p>
|
|
420
|
+
</div>
|
|
421
|
+
)
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
export default LoginValidation
|
|
425
|
+
|
|
426
|
+
--------------------------------------------------------------------------------
|
|
427
|
+
FILE : App.js
|
|
428
|
+
PATH : slip5/src/App.js
|
|
429
|
+
--------------------------------------------------------------------------------
|
|
430
|
+
import { BrowserRouter, Routes, Route, Link, Navigate } from 'react-router-dom';
|
|
431
|
+
import LoginValidation from './slip5/loginValidation';
|
|
432
|
+
|
|
433
|
+
function App() {
|
|
434
|
+
return (
|
|
435
|
+
<BrowserRouter>
|
|
436
|
+
<Link to="/login">Login</Link>
|
|
437
|
+
|
|
438
|
+
<Routes>
|
|
439
|
+
<Route path="/" element={<Navigate to="/login" />} />
|
|
440
|
+
<Route path="/login" element={<LoginValidation />} />
|
|
441
|
+
<Route path="*" element={<h1>404 Error, Page Not Found</h1>} />
|
|
442
|
+
</Routes>
|
|
443
|
+
</BrowserRouter>
|
|
444
|
+
);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
export default App;
|
|
448
|
+
|
|
449
|
+
################################################################################
|
|
450
|
+
# SLIP 6 : Protected route with mock authentication
|
|
451
|
+
################################################################################
|
|
452
|
+
|
|
453
|
+
QUESTION
|
|
454
|
+
|
|
455
|
+
Create a React application with a protected route that only shows a page if the user is authenticated (mock authentication). Display a proper message once the user authenticates.
|
|
456
|
+
|
|
457
|
+
COMMANDS
|
|
458
|
+
|
|
459
|
+
npx create-react-app slip6
|
|
460
|
+
cd slip6
|
|
461
|
+
npm install react-router-dom
|
|
462
|
+
mkdir src\slip6
|
|
463
|
+
code .
|
|
464
|
+
npm start
|
|
465
|
+
|
|
466
|
+
FOLDER STRUCTURE
|
|
467
|
+
|
|
468
|
+
slip6/
|
|
469
|
+
`-- src/
|
|
470
|
+
|-- slip6/
|
|
471
|
+
| |-- Dashboard.js
|
|
472
|
+
| |-- Login.js
|
|
473
|
+
| `-- ProtectedRoute.js
|
|
474
|
+
|-- App.js
|
|
475
|
+
`-- index.js
|
|
476
|
+
|
|
477
|
+
--------------------------------------------------------------------------------
|
|
478
|
+
FILE : Dashboard.js
|
|
479
|
+
PATH : slip6/src/slip6/Dashboard.js
|
|
480
|
+
--------------------------------------------------------------------------------
|
|
481
|
+
import React from 'react';
|
|
482
|
+
|
|
483
|
+
const Dashboard = () => {
|
|
484
|
+
return (
|
|
485
|
+
<div>
|
|
486
|
+
<h2>Welcome to Dashboard!</h2>
|
|
487
|
+
<p>You are successfully authenticated.</p>
|
|
488
|
+
</div>
|
|
489
|
+
);
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
export default Dashboard;
|
|
493
|
+
|
|
494
|
+
--------------------------------------------------------------------------------
|
|
495
|
+
FILE : Login.js
|
|
496
|
+
PATH : slip6/src/slip6/Login.js
|
|
497
|
+
--------------------------------------------------------------------------------
|
|
498
|
+
import React from 'react';
|
|
499
|
+
import { useNavigate } from 'react-router-dom';
|
|
500
|
+
|
|
501
|
+
const Login = ({ setIsAuthenticated }) => {
|
|
502
|
+
|
|
503
|
+
const navigate = useNavigate();
|
|
504
|
+
|
|
505
|
+
const handleLogin = () => {
|
|
506
|
+
setIsAuthenticated(true);
|
|
507
|
+
navigate('/dashboard');
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
return (
|
|
511
|
+
<div>
|
|
512
|
+
<h2>Login Page</h2>
|
|
513
|
+
<button onClick={handleLogin}>
|
|
514
|
+
Login
|
|
515
|
+
</button>
|
|
516
|
+
</div>
|
|
517
|
+
);
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
export default Login;
|
|
521
|
+
|
|
522
|
+
--------------------------------------------------------------------------------
|
|
523
|
+
FILE : ProtectedRoute.js
|
|
524
|
+
PATH : slip6/src/slip6/ProtectedRoute.js
|
|
525
|
+
--------------------------------------------------------------------------------
|
|
526
|
+
import React from 'react';
|
|
527
|
+
import { Navigate } from 'react-router-dom';
|
|
528
|
+
|
|
529
|
+
const ProtectedRoute = ({isAuthenticated, children}) => {
|
|
530
|
+
|
|
531
|
+
return isAuthenticated ? children : <Navigate to="/login" />;
|
|
532
|
+
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
export default ProtectedRoute
|
|
536
|
+
|
|
537
|
+
--------------------------------------------------------------------------------
|
|
538
|
+
FILE : App.js
|
|
539
|
+
PATH : slip6/src/App.js
|
|
540
|
+
--------------------------------------------------------------------------------
|
|
541
|
+
import { useState } from 'react';
|
|
542
|
+
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
|
|
543
|
+
import ProtectedRoute from './slip6/ProtectedRoute';
|
|
544
|
+
import Login from './slip6/Login';
|
|
545
|
+
import Dashboard from './slip6/Dashboard';
|
|
546
|
+
|
|
547
|
+
function App() {
|
|
548
|
+
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
|
549
|
+
|
|
550
|
+
return (
|
|
551
|
+
<BrowserRouter>
|
|
552
|
+
<Routes>
|
|
553
|
+
<Route path="/" element={<Navigate to="/dashboard" />} />
|
|
554
|
+
<Route
|
|
555
|
+
path="/login"
|
|
556
|
+
element={<Login setIsAuthenticated={setIsAuthenticated} />}
|
|
557
|
+
/>
|
|
558
|
+
<Route
|
|
559
|
+
path="/dashboard"
|
|
560
|
+
element={
|
|
561
|
+
<ProtectedRoute isAuthenticated={isAuthenticated}>
|
|
562
|
+
<Dashboard />
|
|
563
|
+
</ProtectedRoute>
|
|
564
|
+
}
|
|
565
|
+
/>
|
|
566
|
+
</Routes>
|
|
567
|
+
</BrowserRouter>
|
|
568
|
+
);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
export default App;
|
|
572
|
+
|
|
573
|
+
################################################################################
|
|
574
|
+
# SLIP 7 : To-do list
|
|
575
|
+
################################################################################
|
|
576
|
+
|
|
577
|
+
QUESTION
|
|
578
|
+
|
|
579
|
+
Create a basic React To-do list: a component for adding, displaying and deleting tasks, state for the list, a form for new tasks, and a button to remove each task.
|
|
580
|
+
|
|
581
|
+
COMMANDS
|
|
582
|
+
|
|
583
|
+
npx create-react-app slip7
|
|
584
|
+
cd slip7
|
|
585
|
+
mkdir src\slip7
|
|
586
|
+
code .
|
|
587
|
+
npm start
|
|
588
|
+
|
|
589
|
+
FOLDER STRUCTURE
|
|
590
|
+
|
|
591
|
+
slip7/
|
|
592
|
+
`-- src/
|
|
593
|
+
|-- slip7/
|
|
594
|
+
| `-- TodoList.js
|
|
595
|
+
|-- App.js
|
|
596
|
+
`-- index.js
|
|
597
|
+
|
|
598
|
+
--------------------------------------------------------------------------------
|
|
599
|
+
FILE : TodoList.js
|
|
600
|
+
PATH : slip7/src/slip7/TodoList.js
|
|
601
|
+
--------------------------------------------------------------------------------
|
|
602
|
+
import React, { useState } from 'react'
|
|
603
|
+
|
|
604
|
+
const TodoList = () => {
|
|
605
|
+
|
|
606
|
+
const [task, setTask] = useState('');
|
|
607
|
+
const [tasks, setTasks] = useState([]);
|
|
608
|
+
|
|
609
|
+
const handleSubmit = (e) =>{
|
|
610
|
+
e.preventDefault();
|
|
611
|
+
if(task.trim() === '')
|
|
612
|
+
return;
|
|
613
|
+
setTasks([...tasks, task]);
|
|
614
|
+
setTask('');
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
const handleDelete = (indexToDelete) => {
|
|
618
|
+
const updatedTasks = tasks.filter(
|
|
619
|
+
(item, index) => index !== indexToDelete
|
|
620
|
+
);
|
|
621
|
+
setTasks(updatedTasks);
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
return (
|
|
625
|
+
<div>
|
|
626
|
+
<h2>To-do List</h2>
|
|
627
|
+
<form onSubmit={handleSubmit}>
|
|
628
|
+
<label>Enter task<input type='text' value={task} onChange={(e) => setTask(e.target.value)}></input></label>
|
|
629
|
+
<button type='submit'>Add Task</button>
|
|
630
|
+
</form>
|
|
631
|
+
<ul>
|
|
632
|
+
{tasks.map((item, index) => (
|
|
633
|
+
<li key={index}>
|
|
634
|
+
{item}
|
|
635
|
+
<button onClick={() => handleDelete(index)}>Delete</button>
|
|
636
|
+
</li>
|
|
637
|
+
))}
|
|
638
|
+
</ul>
|
|
639
|
+
</div>
|
|
640
|
+
)
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
export default TodoList
|
|
644
|
+
|
|
645
|
+
--------------------------------------------------------------------------------
|
|
646
|
+
FILE : App.js
|
|
647
|
+
PATH : slip7/src/App.js
|
|
648
|
+
--------------------------------------------------------------------------------
|
|
649
|
+
import TodoList from './slip7/TodoList';
|
|
650
|
+
|
|
651
|
+
function App() {
|
|
652
|
+
return <TodoList />;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
export default App;
|
|
656
|
+
|
|
657
|
+
################################################################################
|
|
658
|
+
# SLIP 8 : Toggle button ON/OFF
|
|
659
|
+
################################################################################
|
|
660
|
+
|
|
661
|
+
QUESTION
|
|
662
|
+
|
|
663
|
+
Create a React application with a toggle button. When clicked, the button text toggles between ON and OFF and the background colour changes, visually showing the state.
|
|
664
|
+
|
|
665
|
+
COMMANDS
|
|
666
|
+
|
|
667
|
+
npx create-react-app slip8
|
|
668
|
+
cd slip8
|
|
669
|
+
mkdir src\slip8
|
|
670
|
+
code .
|
|
671
|
+
npm start
|
|
672
|
+
|
|
673
|
+
FOLDER STRUCTURE
|
|
674
|
+
|
|
675
|
+
slip8/
|
|
676
|
+
`-- src/
|
|
677
|
+
|-- slip8/
|
|
678
|
+
| `-- ToggleButton.js
|
|
679
|
+
|-- App.js
|
|
680
|
+
`-- index.js
|
|
681
|
+
|
|
682
|
+
--------------------------------------------------------------------------------
|
|
683
|
+
FILE : ToggleButton.js
|
|
684
|
+
PATH : slip8/src/slip8/ToggleButton.js
|
|
685
|
+
--------------------------------------------------------------------------------
|
|
686
|
+
import React, { useState } from 'react';
|
|
687
|
+
|
|
688
|
+
const ToggleButton = () => {
|
|
689
|
+
const [isOn, setIsOn] = useState(false);
|
|
690
|
+
|
|
691
|
+
return (
|
|
692
|
+
<div>
|
|
693
|
+
<h2>Toggle Button</h2>
|
|
694
|
+
<button
|
|
695
|
+
onClick={() => setIsOn(!isOn)}
|
|
696
|
+
style={{ backgroundColor: isOn ? 'lightgreen' : 'lightcoral' }}
|
|
697
|
+
>
|
|
698
|
+
{isOn ? 'ON' : 'OFF'}
|
|
699
|
+
</button>
|
|
700
|
+
</div>
|
|
701
|
+
);
|
|
702
|
+
};
|
|
703
|
+
|
|
704
|
+
export default ToggleButton;
|
|
705
|
+
|
|
706
|
+
--------------------------------------------------------------------------------
|
|
707
|
+
FILE : App.js
|
|
708
|
+
PATH : slip8/src/App.js
|
|
709
|
+
--------------------------------------------------------------------------------
|
|
710
|
+
import ToggleButton from './slip8/ToggleButton';
|
|
711
|
+
|
|
712
|
+
function App() {
|
|
713
|
+
return <ToggleButton />;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
export default App;
|
|
717
|
+
|
|
718
|
+
################################################################################
|
|
719
|
+
# SLIP 9 : Counter with initial value as a prop
|
|
720
|
+
################################################################################
|
|
721
|
+
|
|
722
|
+
QUESTION
|
|
723
|
+
|
|
724
|
+
Create a Counter component that displays a number with "Increment" and "Decrement" buttons. Start at 0. Use state for the number and pass the initial number as a prop.
|
|
725
|
+
|
|
726
|
+
COMMANDS
|
|
727
|
+
|
|
728
|
+
npx create-react-app slip9
|
|
729
|
+
cd slip9
|
|
730
|
+
mkdir src\slip9
|
|
731
|
+
code .
|
|
732
|
+
npm start
|
|
733
|
+
|
|
734
|
+
FOLDER STRUCTURE
|
|
735
|
+
|
|
736
|
+
slip9/
|
|
737
|
+
`-- src/
|
|
738
|
+
|-- slip9/
|
|
739
|
+
| `-- Counter.js
|
|
740
|
+
|-- App.js
|
|
741
|
+
`-- index.js
|
|
742
|
+
|
|
743
|
+
--------------------------------------------------------------------------------
|
|
744
|
+
FILE : Counter.js
|
|
745
|
+
PATH : slip9/src/slip9/Counter.js
|
|
746
|
+
--------------------------------------------------------------------------------
|
|
747
|
+
import React, {useState} from 'react'
|
|
748
|
+
|
|
749
|
+
const Counter = ({initialCount}) => {
|
|
750
|
+
|
|
751
|
+
const [count, setCount] = useState(initialCount);
|
|
752
|
+
|
|
753
|
+
return (
|
|
754
|
+
<div>
|
|
755
|
+
<h2>Count: {count}</h2>
|
|
756
|
+
<button onClick={() => setCount(count + 1)}>Increment</button>
|
|
757
|
+
<button onClick={() => setCount(count - 1)}>Decrement</button>
|
|
758
|
+
</div>
|
|
759
|
+
)
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
export default Counter
|
|
763
|
+
|
|
764
|
+
--------------------------------------------------------------------------------
|
|
765
|
+
FILE : App.js
|
|
766
|
+
PATH : slip9/src/App.js
|
|
767
|
+
--------------------------------------------------------------------------------
|
|
768
|
+
import Counter from './slip9/Counter';
|
|
769
|
+
|
|
770
|
+
function App() {
|
|
771
|
+
return <Counter initialCount={0} />;
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
export default App;
|
|
775
|
+
|
|
776
|
+
################################################################################
|
|
777
|
+
# SLIP 10 : Validate a number and sum its digits
|
|
778
|
+
################################################################################
|
|
779
|
+
|
|
780
|
+
QUESTION
|
|
781
|
+
|
|
782
|
+
Check whether a number is valid, calculate the sum of its digits and display it. Show messages if the value is invalid or blank.
|
|
783
|
+
|
|
784
|
+
COMMANDS
|
|
785
|
+
|
|
786
|
+
npx create-react-app slip10
|
|
787
|
+
cd slip10
|
|
788
|
+
mkdir src\slip10
|
|
789
|
+
code .
|
|
790
|
+
npm start
|
|
791
|
+
|
|
792
|
+
FOLDER STRUCTURE
|
|
793
|
+
|
|
794
|
+
slip10/
|
|
795
|
+
`-- src/
|
|
796
|
+
|-- slip10/
|
|
797
|
+
| `-- SumOfDigits.js
|
|
798
|
+
|-- App.js
|
|
799
|
+
`-- index.js
|
|
800
|
+
|
|
801
|
+
--------------------------------------------------------------------------------
|
|
802
|
+
FILE : SumOfDigits.js
|
|
803
|
+
PATH : slip10/src/slip10/SumOfDigits.js
|
|
804
|
+
--------------------------------------------------------------------------------
|
|
805
|
+
import React, { useState } from 'react'
|
|
806
|
+
|
|
807
|
+
const SumOfDigits = () => {
|
|
808
|
+
|
|
809
|
+
const [number, setNumber] = useState('');
|
|
810
|
+
const [result, setResult] = useState('');
|
|
811
|
+
|
|
812
|
+
const handleSubmit = (e) =>{
|
|
813
|
+
e.preventDefault();
|
|
814
|
+
if(number.trim() === ''){
|
|
815
|
+
alert("Please enter a number");
|
|
816
|
+
return;
|
|
817
|
+
}
|
|
818
|
+
if(!/^\d+$/.test(number)){
|
|
819
|
+
alert("Invalid input! Enter only digits");
|
|
820
|
+
return;
|
|
821
|
+
}
|
|
822
|
+
let sum = 0;
|
|
823
|
+
for(let digit of number){
|
|
824
|
+
sum += Number(digit);
|
|
825
|
+
}
|
|
826
|
+
setResult(sum);
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
return (
|
|
830
|
+
<div>
|
|
831
|
+
<h2>Sum Of Digits</h2>
|
|
832
|
+
<form>
|
|
833
|
+
<label>Enter number:<input type='text' value={number} onChange={(e) => setNumber(e.target.value)}></input></label>
|
|
834
|
+
<button type='submit' onClick={handleSubmit}>Calculate Sum</button><br></br>
|
|
835
|
+
<p>Sum of Digits: {result}</p>
|
|
836
|
+
</form>
|
|
837
|
+
</div>
|
|
838
|
+
)
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
export default SumOfDigits
|
|
842
|
+
|
|
843
|
+
--------------------------------------------------------------------------------
|
|
844
|
+
FILE : App.js
|
|
845
|
+
PATH : slip10/src/App.js
|
|
846
|
+
--------------------------------------------------------------------------------
|
|
847
|
+
import SumOfDigits from './slip10/SumOfDigits';
|
|
848
|
+
|
|
849
|
+
function App() {
|
|
850
|
+
return <SumOfDigits />;
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
export default App;
|
|
854
|
+
|
|
855
|
+
################################################################################
|
|
856
|
+
# SLIP 11 : String operations
|
|
857
|
+
################################################################################
|
|
858
|
+
|
|
859
|
+
QUESTION
|
|
860
|
+
|
|
861
|
+
Take a string from the user and show: uppercase, lowercase, length and reverse.
|
|
862
|
+
|
|
863
|
+
COMMANDS
|
|
864
|
+
|
|
865
|
+
npx create-react-app slip11
|
|
866
|
+
cd slip11
|
|
867
|
+
mkdir src\slip11
|
|
868
|
+
code .
|
|
869
|
+
npm start
|
|
870
|
+
|
|
871
|
+
FOLDER STRUCTURE
|
|
872
|
+
|
|
873
|
+
slip11/
|
|
874
|
+
`-- src/
|
|
875
|
+
|-- slip11/
|
|
876
|
+
| `-- StringOperation.js
|
|
877
|
+
|-- App.js
|
|
878
|
+
`-- index.js
|
|
879
|
+
|
|
880
|
+
--------------------------------------------------------------------------------
|
|
881
|
+
FILE : StringOperation.js
|
|
882
|
+
PATH : slip11/src/slip11/StringOperation.js
|
|
883
|
+
--------------------------------------------------------------------------------
|
|
884
|
+
import React, { useState } from 'react'
|
|
885
|
+
|
|
886
|
+
const StringOperation = () => {
|
|
887
|
+
|
|
888
|
+
const [str, setStr] = useState('');
|
|
889
|
+
const [result, setResult] = useState('');
|
|
890
|
+
|
|
891
|
+
const handleSubmit = (e) => {
|
|
892
|
+
e.preventDefault();
|
|
893
|
+
if(str.trim() === ''){
|
|
894
|
+
alert("Enter a string");
|
|
895
|
+
return;
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
setResult(str);
|
|
899
|
+
|
|
900
|
+
}
|
|
901
|
+
return (
|
|
902
|
+
<div>
|
|
903
|
+
<h2>String Operations</h2>
|
|
904
|
+
<form onSubmit={handleSubmit}>
|
|
905
|
+
<label>Enter string<input type='text' value={str} onChange={(e) => setStr(e.target.value)}></input></label>
|
|
906
|
+
<button type='submit'>Submit</button>
|
|
907
|
+
<p>String in UpperCase: {result.toUpperCase()}</p>
|
|
908
|
+
<p>String in LowerCae: {result.toLowerCase()}</p>
|
|
909
|
+
<p>Length of the string: {result.length}</p>
|
|
910
|
+
<p>Reverse string : {result.split('').reverse().join('')}</p>
|
|
911
|
+
</form>
|
|
912
|
+
</div>
|
|
913
|
+
)
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
export default StringOperation
|
|
917
|
+
|
|
918
|
+
--------------------------------------------------------------------------------
|
|
919
|
+
FILE : App.js
|
|
920
|
+
PATH : slip11/src/App.js
|
|
921
|
+
--------------------------------------------------------------------------------
|
|
922
|
+
import StringOperation from './slip11/StringOperation';
|
|
923
|
+
|
|
924
|
+
function App() {
|
|
925
|
+
return <StringOperation />;
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
export default App;
|
|
929
|
+
|
|
930
|
+
################################################################################
|
|
931
|
+
# SLIP 12 : Notes app with Redux Toolkit
|
|
932
|
+
################################################################################
|
|
933
|
+
|
|
934
|
+
QUESTION
|
|
935
|
+
|
|
936
|
+
Build a notes management app using Redux Toolkit with createSlice() to manage state.
|
|
937
|
+
|
|
938
|
+
COMMANDS
|
|
939
|
+
|
|
940
|
+
npx create-react-app slip12
|
|
941
|
+
cd slip12
|
|
942
|
+
npm install @reduxjs/toolkit react-redux
|
|
943
|
+
mkdir src\slip12
|
|
944
|
+
mkdir src\slip12\redux
|
|
945
|
+
code .
|
|
946
|
+
npm start
|
|
947
|
+
|
|
948
|
+
FOLDER STRUCTURE
|
|
949
|
+
|
|
950
|
+
slip12/
|
|
951
|
+
`-- src/
|
|
952
|
+
|-- slip12/
|
|
953
|
+
| |-- redux/
|
|
954
|
+
| | |-- notesSlice.js
|
|
955
|
+
| | `-- store.js
|
|
956
|
+
| `-- NotesManagementApp.js
|
|
957
|
+
|-- App.js
|
|
958
|
+
`-- index.js
|
|
959
|
+
|
|
960
|
+
--------------------------------------------------------------------------------
|
|
961
|
+
FILE : notesSlice.js
|
|
962
|
+
PATH : slip12/src/slip12/redux/notesSlice.js
|
|
963
|
+
--------------------------------------------------------------------------------
|
|
964
|
+
import {createSlice} from '@reduxjs/toolkit';
|
|
965
|
+
|
|
966
|
+
const notesSlice = createSlice ({
|
|
967
|
+
name : 'notes',
|
|
968
|
+
initialState : [],
|
|
969
|
+
reducers : {
|
|
970
|
+
|
|
971
|
+
addNote: (state, action) => {
|
|
972
|
+
state.push(action.payload);
|
|
973
|
+
},
|
|
974
|
+
|
|
975
|
+
deleteNote: (state, action) =>{
|
|
976
|
+
state.splice(action.payload, 1);
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
});
|
|
980
|
+
|
|
981
|
+
export const {addNote, deleteNote} = notesSlice.actions;
|
|
982
|
+
|
|
983
|
+
export default notesSlice.reducer;
|
|
984
|
+
|
|
985
|
+
--------------------------------------------------------------------------------
|
|
986
|
+
FILE : store.js
|
|
987
|
+
PATH : slip12/src/slip12/redux/store.js
|
|
988
|
+
--------------------------------------------------------------------------------
|
|
989
|
+
import {configureStore} from '@reduxjs/toolkit';
|
|
990
|
+
import notesReducer from './notesSlice';
|
|
991
|
+
|
|
992
|
+
const store = configureStore({
|
|
993
|
+
reducer: {
|
|
994
|
+
notes: notesReducer
|
|
995
|
+
}
|
|
996
|
+
})
|
|
997
|
+
|
|
998
|
+
export default store;
|
|
999
|
+
|
|
1000
|
+
--------------------------------------------------------------------------------
|
|
1001
|
+
FILE : NotesManagementApp.js
|
|
1002
|
+
PATH : slip12/src/slip12/NotesManagementApp.js
|
|
1003
|
+
--------------------------------------------------------------------------------
|
|
1004
|
+
import React, { useState } from 'react';
|
|
1005
|
+
import { useDispatch, useSelector } from 'react-redux';
|
|
1006
|
+
import { addNote, deleteNote } from './redux/notesSlice';
|
|
1007
|
+
|
|
1008
|
+
const NotesManagementApp = () => {
|
|
1009
|
+
|
|
1010
|
+
const [note, setNote] = useState('');
|
|
1011
|
+
const notes = useSelector((state) => state.notes);
|
|
1012
|
+
const dispatch = useDispatch();
|
|
1013
|
+
|
|
1014
|
+
const handleClick = () =>{
|
|
1015
|
+
if(note.trim() === ''){
|
|
1016
|
+
return;
|
|
1017
|
+
}
|
|
1018
|
+
dispatch(addNote(note));
|
|
1019
|
+
setNote('');
|
|
1020
|
+
}
|
|
1021
|
+
return (
|
|
1022
|
+
<div>
|
|
1023
|
+
<h2>Notes Management App</h2>
|
|
1024
|
+
<label>Enter note: <input type='text' value={note} onChange={(e) => setNote(e.target.value)}></input></label>
|
|
1025
|
+
<button type='submit' onClick={handleClick}>Add Note</button>
|
|
1026
|
+
<ul>
|
|
1027
|
+
{notes.map((item, index) =>(
|
|
1028
|
+
<li key={index}>
|
|
1029
|
+
{item}
|
|
1030
|
+
<button type='submit' onClick={() => dispatch(deleteNote(index))}>Delete Note</button>
|
|
1031
|
+
</li>
|
|
1032
|
+
))}
|
|
1033
|
+
</ul>
|
|
1034
|
+
</div>
|
|
1035
|
+
)
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
export default NotesManagementApp
|
|
1039
|
+
|
|
1040
|
+
--------------------------------------------------------------------------------
|
|
1041
|
+
FILE : App.js
|
|
1042
|
+
PATH : slip12/src/App.js
|
|
1043
|
+
--------------------------------------------------------------------------------
|
|
1044
|
+
import NotesManagementApp from './slip12/NotesManagementApp';
|
|
1045
|
+
|
|
1046
|
+
function App() {
|
|
1047
|
+
return <NotesManagementApp />;
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
export default App;
|
|
1051
|
+
|
|
1052
|
+
--------------------------------------------------------------------------------
|
|
1053
|
+
FILE : index.js
|
|
1054
|
+
PATH : slip12/src/index.js
|
|
1055
|
+
--------------------------------------------------------------------------------
|
|
1056
|
+
import React from 'react';
|
|
1057
|
+
import ReactDOM from 'react-dom/client';
|
|
1058
|
+
import { Provider } from 'react-redux';
|
|
1059
|
+
import App from './App';
|
|
1060
|
+
import store from './slip12/redux/store';
|
|
1061
|
+
|
|
1062
|
+
const root = ReactDOM.createRoot(document.getElementById('root'));
|
|
1063
|
+
root.render(
|
|
1064
|
+
<React.StrictMode>
|
|
1065
|
+
<Provider store={store}>
|
|
1066
|
+
<App />
|
|
1067
|
+
</Provider>
|
|
1068
|
+
</React.StrictMode>
|
|
1069
|
+
);
|
|
1070
|
+
|
|
1071
|
+
################################################################################
|
|
1072
|
+
# SLIP 13 : Counter with a Redux store
|
|
1073
|
+
################################################################################
|
|
1074
|
+
|
|
1075
|
+
QUESTION
|
|
1076
|
+
|
|
1077
|
+
Create a Redux store for counter buttons: display the current count, provide increment and decrement buttons, and display the count on the console.
|
|
1078
|
+
|
|
1079
|
+
COMMANDS
|
|
1080
|
+
|
|
1081
|
+
npx create-react-app slip13
|
|
1082
|
+
cd slip13
|
|
1083
|
+
npm install @reduxjs/toolkit react-redux
|
|
1084
|
+
mkdir src\slip13
|
|
1085
|
+
mkdir src\slip13\redux
|
|
1086
|
+
code .
|
|
1087
|
+
npm start
|
|
1088
|
+
|
|
1089
|
+
FOLDER STRUCTURE
|
|
1090
|
+
|
|
1091
|
+
slip13/
|
|
1092
|
+
`-- src/
|
|
1093
|
+
|-- slip13/
|
|
1094
|
+
| |-- redux/
|
|
1095
|
+
| | |-- counterSlice.js
|
|
1096
|
+
| | `-- store.js
|
|
1097
|
+
| `-- Counter.js
|
|
1098
|
+
|-- App.js
|
|
1099
|
+
`-- index.js
|
|
1100
|
+
|
|
1101
|
+
--------------------------------------------------------------------------------
|
|
1102
|
+
FILE : counterSlice.js
|
|
1103
|
+
PATH : slip13/src/slip13/redux/counterSlice.js
|
|
1104
|
+
--------------------------------------------------------------------------------
|
|
1105
|
+
import { createSlice } from "@reduxjs/toolkit";
|
|
1106
|
+
|
|
1107
|
+
const counterSlice = createSlice ({
|
|
1108
|
+
name : 'counter',
|
|
1109
|
+
initialState : 0,
|
|
1110
|
+
reducers : {
|
|
1111
|
+
increment(state){
|
|
1112
|
+
return state + 1;
|
|
1113
|
+
},
|
|
1114
|
+
|
|
1115
|
+
decrement(state){
|
|
1116
|
+
return state - 1;
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
})
|
|
1120
|
+
|
|
1121
|
+
export const {increment, decrement} = counterSlice.actions;
|
|
1122
|
+
|
|
1123
|
+
export default counterSlice.reducer;
|
|
1124
|
+
|
|
1125
|
+
--------------------------------------------------------------------------------
|
|
1126
|
+
FILE : store.js
|
|
1127
|
+
PATH : slip13/src/slip13/redux/store.js
|
|
1128
|
+
--------------------------------------------------------------------------------
|
|
1129
|
+
import { configureStore } from "@reduxjs/toolkit";
|
|
1130
|
+
import counterReducer from './counterSlice.js';
|
|
1131
|
+
|
|
1132
|
+
const counterStore = configureStore ({
|
|
1133
|
+
reducer : {
|
|
1134
|
+
counter : counterReducer
|
|
1135
|
+
}
|
|
1136
|
+
});
|
|
1137
|
+
|
|
1138
|
+
export default counterStore;
|
|
1139
|
+
|
|
1140
|
+
--------------------------------------------------------------------------------
|
|
1141
|
+
FILE : Counter.js
|
|
1142
|
+
PATH : slip13/src/slip13/Counter.js
|
|
1143
|
+
--------------------------------------------------------------------------------
|
|
1144
|
+
import React from 'react';
|
|
1145
|
+
import { increment, decrement } from './redux/counterSlice';
|
|
1146
|
+
import { useDispatch, useSelector } from 'react-redux';
|
|
1147
|
+
|
|
1148
|
+
const Counter = () => {
|
|
1149
|
+
|
|
1150
|
+
const count = useSelector((state) => state.counter);
|
|
1151
|
+
const dispatch = useDispatch();
|
|
1152
|
+
console.log("Current count is: ",count);
|
|
1153
|
+
|
|
1154
|
+
return (
|
|
1155
|
+
<div>
|
|
1156
|
+
<h2>Count: {count}</h2>
|
|
1157
|
+
<button type='submit' onClick={() => dispatch(increment())}>Increment</button>
|
|
1158
|
+
<button type='submit' onClick={() => dispatch(decrement())}>Decrement</button>
|
|
1159
|
+
</div>
|
|
1160
|
+
)
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
export default Counter
|
|
1164
|
+
|
|
1165
|
+
--------------------------------------------------------------------------------
|
|
1166
|
+
FILE : App.js
|
|
1167
|
+
PATH : slip13/src/App.js
|
|
1168
|
+
--------------------------------------------------------------------------------
|
|
1169
|
+
import Counter from './slip13/Counter';
|
|
1170
|
+
|
|
1171
|
+
function App() {
|
|
1172
|
+
return <Counter />;
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1175
|
+
export default App;
|
|
1176
|
+
|
|
1177
|
+
--------------------------------------------------------------------------------
|
|
1178
|
+
FILE : index.js
|
|
1179
|
+
PATH : slip13/src/index.js
|
|
1180
|
+
--------------------------------------------------------------------------------
|
|
1181
|
+
import React from 'react';
|
|
1182
|
+
import ReactDOM from 'react-dom/client';
|
|
1183
|
+
import { Provider } from 'react-redux';
|
|
1184
|
+
import App from './App';
|
|
1185
|
+
import counterStore from './slip13/redux/store';
|
|
1186
|
+
|
|
1187
|
+
const root = ReactDOM.createRoot(document.getElementById('root'));
|
|
1188
|
+
root.render(
|
|
1189
|
+
<React.StrictMode>
|
|
1190
|
+
<Provider store={counterStore}>
|
|
1191
|
+
<App />
|
|
1192
|
+
</Provider>
|
|
1193
|
+
</React.StrictMode>
|
|
1194
|
+
);
|
|
1195
|
+
|
|
1196
|
+
################################################################################
|
|
1197
|
+
# SLIP 14 : Calculator
|
|
1198
|
+
################################################################################
|
|
1199
|
+
|
|
1200
|
+
QUESTION
|
|
1201
|
+
|
|
1202
|
+
Create a React application to build a calculator.
|
|
1203
|
+
|
|
1204
|
+
COMMANDS
|
|
1205
|
+
|
|
1206
|
+
npx create-react-app slip14
|
|
1207
|
+
cd slip14
|
|
1208
|
+
mkdir src\slip14
|
|
1209
|
+
code .
|
|
1210
|
+
npm start
|
|
1211
|
+
|
|
1212
|
+
FOLDER STRUCTURE
|
|
1213
|
+
|
|
1214
|
+
slip14/
|
|
1215
|
+
`-- src/
|
|
1216
|
+
|-- slip14/
|
|
1217
|
+
| `-- Calculator.js
|
|
1218
|
+
|-- App.js
|
|
1219
|
+
`-- index.js
|
|
1220
|
+
|
|
1221
|
+
--------------------------------------------------------------------------------
|
|
1222
|
+
FILE : Calculator.js
|
|
1223
|
+
PATH : slip14/src/slip14/Calculator.js
|
|
1224
|
+
--------------------------------------------------------------------------------
|
|
1225
|
+
import React, { useState } from 'react';
|
|
1226
|
+
|
|
1227
|
+
const Calculator = () =>{
|
|
1228
|
+
|
|
1229
|
+
const [firstNumber, setFirstNumber] = useState('');
|
|
1230
|
+
const [secondNumber, setSecondNumber] = useState('');
|
|
1231
|
+
const [result, setResult] = useState(null);
|
|
1232
|
+
|
|
1233
|
+
const handleAddition = () =>{
|
|
1234
|
+
if(firstNumber.trim() === '' || secondNumber.trim() === '')
|
|
1235
|
+
return;
|
|
1236
|
+
|
|
1237
|
+
let total = 0;
|
|
1238
|
+
total = Number(firstNumber) + Number(secondNumber);
|
|
1239
|
+
setResult(total);
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
const handleSubtraction = () =>{
|
|
1243
|
+
if(firstNumber.trim() === '' || secondNumber.trim() === '')
|
|
1244
|
+
return;
|
|
1245
|
+
|
|
1246
|
+
let total = 0;
|
|
1247
|
+
total = Number(firstNumber) - Number(secondNumber);
|
|
1248
|
+
setResult(total);
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
const handleMultiplication = () =>{
|
|
1252
|
+
if(firstNumber.trim() === '' || secondNumber.trim() === '')
|
|
1253
|
+
return;
|
|
1254
|
+
|
|
1255
|
+
let total = 0;
|
|
1256
|
+
total = Number(firstNumber) * Number(secondNumber);
|
|
1257
|
+
setResult(total);
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
const handleDivision = () =>{
|
|
1261
|
+
if(firstNumber.trim() === '' || secondNumber.trim() === '')
|
|
1262
|
+
return;
|
|
1263
|
+
|
|
1264
|
+
if(Number(secondNumber) === 0){
|
|
1265
|
+
alert("Can't divide by zero");
|
|
1266
|
+
return;
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
let total = 0;
|
|
1270
|
+
total = Number(firstNumber) / Number(secondNumber);
|
|
1271
|
+
setResult(total);
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
return (
|
|
1275
|
+
<div>
|
|
1276
|
+
<h2>Calculator</h2>
|
|
1277
|
+
<input value={firstNumber} onChange={(e) => setFirstNumber(e.target.value)}></input>
|
|
1278
|
+
<input value={secondNumber} onChange={(e) => setSecondNumber(e.target.value)}></input>
|
|
1279
|
+
<button onClick={handleAddition}>Addition</button>
|
|
1280
|
+
<button onClick={handleSubtraction}>Subtraction</button>
|
|
1281
|
+
<button onClick={handleMultiplication}>Multiplication</button>
|
|
1282
|
+
<button onClick={handleDivision}>Division</button>
|
|
1283
|
+
<p>Result: {result}</p>
|
|
1284
|
+
</div>
|
|
1285
|
+
)
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
export default Calculator
|
|
1289
|
+
|
|
1290
|
+
--------------------------------------------------------------------------------
|
|
1291
|
+
FILE : App.js
|
|
1292
|
+
PATH : slip14/src/App.js
|
|
1293
|
+
--------------------------------------------------------------------------------
|
|
1294
|
+
import Calculator from './slip14/Calculator';
|
|
1295
|
+
|
|
1296
|
+
function App() {
|
|
1297
|
+
return <Calculator />;
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
export default App;
|
|
1301
|
+
|
|
1302
|
+
################################################################################
|
|
1303
|
+
# SLIP 15 : Counter using useReducer
|
|
1304
|
+
################################################################################
|
|
1305
|
+
|
|
1306
|
+
QUESTION
|
|
1307
|
+
|
|
1308
|
+
Create a simple counter using actions and reducers.
|
|
1309
|
+
|
|
1310
|
+
COMMANDS
|
|
1311
|
+
|
|
1312
|
+
npx create-react-app slip15
|
|
1313
|
+
cd slip15
|
|
1314
|
+
mkdir src\slip15
|
|
1315
|
+
code .
|
|
1316
|
+
npm start
|
|
1317
|
+
|
|
1318
|
+
FOLDER STRUCTURE
|
|
1319
|
+
|
|
1320
|
+
slip15/
|
|
1321
|
+
`-- src/
|
|
1322
|
+
|-- slip15/
|
|
1323
|
+
| `-- Counter.js
|
|
1324
|
+
|-- App.js
|
|
1325
|
+
`-- index.js
|
|
1326
|
+
|
|
1327
|
+
--------------------------------------------------------------------------------
|
|
1328
|
+
FILE : Counter.js
|
|
1329
|
+
PATH : slip15/src/slip15/Counter.js
|
|
1330
|
+
--------------------------------------------------------------------------------
|
|
1331
|
+
import React, { useReducer } from 'react'
|
|
1332
|
+
|
|
1333
|
+
const Counter = () => {
|
|
1334
|
+
|
|
1335
|
+
function reducer(state, action){
|
|
1336
|
+
if(action === 'increment'){
|
|
1337
|
+
return state + 1;
|
|
1338
|
+
}
|
|
1339
|
+
if(action === 'decrement'){
|
|
1340
|
+
return state - 1;
|
|
1341
|
+
}
|
|
1342
|
+
return state;
|
|
1343
|
+
}
|
|
1344
|
+
const [count, dispatch] = useReducer(reducer, 0);
|
|
1345
|
+
return (
|
|
1346
|
+
<div>
|
|
1347
|
+
<h2>Counter: {count}</h2>
|
|
1348
|
+
<button onClick={() => dispatch('increment')}>Increment</button>
|
|
1349
|
+
<button onClick={() => dispatch('decrement')}>Decrement</button>
|
|
1350
|
+
</div>
|
|
1351
|
+
)
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
export default Counter
|
|
1355
|
+
|
|
1356
|
+
--------------------------------------------------------------------------------
|
|
1357
|
+
FILE : App.js
|
|
1358
|
+
PATH : slip15/src/App.js
|
|
1359
|
+
--------------------------------------------------------------------------------
|
|
1360
|
+
import Counter from './slip15/Counter';
|
|
1361
|
+
|
|
1362
|
+
function App() {
|
|
1363
|
+
return <Counter />;
|
|
1364
|
+
}
|
|
1365
|
+
|
|
1366
|
+
export default App;
|
|
1367
|
+
|
|
1368
|
+
################################################################################
|
|
1369
|
+
# SLIP 16 : Virtual DOM and traditional DOM
|
|
1370
|
+
################################################################################
|
|
1371
|
+
|
|
1372
|
+
QUESTION
|
|
1373
|
+
|
|
1374
|
+
Demonstrate the concept of the virtual DOM by comparing traditional DOM updates with React rendering through a counter example.
|
|
1375
|
+
|
|
1376
|
+
COMMANDS
|
|
1377
|
+
|
|
1378
|
+
npx create-react-app slip16
|
|
1379
|
+
cd slip16
|
|
1380
|
+
mkdir src\slip16
|
|
1381
|
+
code .
|
|
1382
|
+
npm start
|
|
1383
|
+
|
|
1384
|
+
FOLDER STRUCTURE
|
|
1385
|
+
|
|
1386
|
+
slip16/
|
|
1387
|
+
`-- src/
|
|
1388
|
+
|-- slip16/
|
|
1389
|
+
| |-- TraditionalDOM.html
|
|
1390
|
+
| `-- VirtualDOM.js
|
|
1391
|
+
|-- App.js
|
|
1392
|
+
`-- index.js
|
|
1393
|
+
|
|
1394
|
+
--------------------------------------------------------------------------------
|
|
1395
|
+
FILE : TraditionalDOM.html
|
|
1396
|
+
PATH : slip16/src/slip16/TraditionalDOM.html
|
|
1397
|
+
--------------------------------------------------------------------------------
|
|
1398
|
+
<!DOCTYPE html>
|
|
1399
|
+
<html lang="en">
|
|
1400
|
+
<head>
|
|
1401
|
+
<meta charset="UTF-8">
|
|
1402
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
1403
|
+
<title>TraditionalDOM Counter</title>
|
|
1404
|
+
</head>
|
|
1405
|
+
<body>
|
|
1406
|
+
<h2 id="count">0</h2>
|
|
1407
|
+
<button onclick="increment()">Increment</button>
|
|
1408
|
+
<button onclick="decrement()">Decrement</button>
|
|
1409
|
+
<script>
|
|
1410
|
+
let count = 0;
|
|
1411
|
+
function increment(){
|
|
1412
|
+
count++;
|
|
1413
|
+
document.getElementById("count").innerHTML = count;
|
|
1414
|
+
}
|
|
1415
|
+
function decrement(){
|
|
1416
|
+
count--;
|
|
1417
|
+
document.getElementById("count").innerHTML = count;
|
|
1418
|
+
}
|
|
1419
|
+
</script>
|
|
1420
|
+
</body>
|
|
1421
|
+
</html>
|
|
1422
|
+
|
|
1423
|
+
--------------------------------------------------------------------------------
|
|
1424
|
+
FILE : VirtualDOM.js
|
|
1425
|
+
PATH : slip16/src/slip16/VirtualDOM.js
|
|
1426
|
+
--------------------------------------------------------------------------------
|
|
1427
|
+
import React, { useState } from 'react';
|
|
1428
|
+
|
|
1429
|
+
const VirtualDOM = () =>{
|
|
1430
|
+
|
|
1431
|
+
const [count, setCount] = useState(0);
|
|
1432
|
+
|
|
1433
|
+
return(
|
|
1434
|
+
<div>
|
|
1435
|
+
<h2>VirtualDOM Counter: {count}</h2>
|
|
1436
|
+
<button onClick={() => setCount(count + 1)}>Increment</button>
|
|
1437
|
+
<button onClick={() => setCount(count - 1)}>Decrement</button>
|
|
1438
|
+
</div>
|
|
1439
|
+
)
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
export default VirtualDOM
|
|
1443
|
+
|
|
1444
|
+
--------------------------------------------------------------------------------
|
|
1445
|
+
FILE : App.js
|
|
1446
|
+
PATH : slip16/src/App.js
|
|
1447
|
+
--------------------------------------------------------------------------------
|
|
1448
|
+
import VirtualDOM from './slip16/VirtualDOM';
|
|
1449
|
+
|
|
1450
|
+
function App() {
|
|
1451
|
+
return <VirtualDOM />;
|
|
1452
|
+
}
|
|
1453
|
+
|
|
1454
|
+
export default App;
|
|
1455
|
+
|
|
1456
|
+
################################################################################
|
|
1457
|
+
# SLIP 17 : Sign in and sign out
|
|
1458
|
+
################################################################################
|
|
1459
|
+
|
|
1460
|
+
QUESTION
|
|
1461
|
+
|
|
1462
|
+
Create Sign-In and Sign-Out functionality using the useState() hook. On Sign in show "Hello, Welcome in Home Page"; on Sign out show "Please sign in first".
|
|
1463
|
+
|
|
1464
|
+
COMMANDS
|
|
1465
|
+
|
|
1466
|
+
npx create-react-app slip17
|
|
1467
|
+
cd slip17
|
|
1468
|
+
mkdir src\slip17
|
|
1469
|
+
code .
|
|
1470
|
+
npm start
|
|
1471
|
+
|
|
1472
|
+
FOLDER STRUCTURE
|
|
1473
|
+
|
|
1474
|
+
slip17/
|
|
1475
|
+
`-- src/
|
|
1476
|
+
|-- slip17/
|
|
1477
|
+
| `-- Sign-in.js
|
|
1478
|
+
|-- App.js
|
|
1479
|
+
`-- index.js
|
|
1480
|
+
|
|
1481
|
+
--------------------------------------------------------------------------------
|
|
1482
|
+
FILE : Sign-in.js
|
|
1483
|
+
PATH : slip17/src/slip17/Sign-in.js
|
|
1484
|
+
--------------------------------------------------------------------------------
|
|
1485
|
+
import React, {useState} from 'react'
|
|
1486
|
+
|
|
1487
|
+
const SignIn = () => {
|
|
1488
|
+
|
|
1489
|
+
const [isLoggedIn, setIsLoggedIn] = useState(false);
|
|
1490
|
+
|
|
1491
|
+
return (
|
|
1492
|
+
<div>
|
|
1493
|
+
<h2>SignIn SignOut</h2>
|
|
1494
|
+
{isLoggedIn ? <h3>Hello! Welcome in Home page.</h3> : <h3>Please login first.</h3>}
|
|
1495
|
+
{!isLoggedIn && (<button onClick={() => setIsLoggedIn(true)}>Sign in</button>)}
|
|
1496
|
+
{isLoggedIn && (<button onClick={() => setIsLoggedIn(false)}>Sign out</button>)}
|
|
1497
|
+
</div>
|
|
1498
|
+
)
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
export default SignIn
|
|
1502
|
+
|
|
1503
|
+
--------------------------------------------------------------------------------
|
|
1504
|
+
FILE : App.js
|
|
1505
|
+
PATH : slip17/src/App.js
|
|
1506
|
+
--------------------------------------------------------------------------------
|
|
1507
|
+
import SignIn from './slip17/Sign-in';
|
|
1508
|
+
|
|
1509
|
+
function App() {
|
|
1510
|
+
return <SignIn />;
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
export default App;
|
|
1514
|
+
|
|
1515
|
+
################################################################################
|
|
1516
|
+
# SLIP 18 : Profile page
|
|
1517
|
+
################################################################################
|
|
1518
|
+
|
|
1519
|
+
QUESTION
|
|
1520
|
+
|
|
1521
|
+
Create a React application that displays your name, a short bio, an image and a description of your profile.
|
|
1522
|
+
|
|
1523
|
+
COMMANDS
|
|
1524
|
+
|
|
1525
|
+
npx create-react-app slip18
|
|
1526
|
+
cd slip18
|
|
1527
|
+
mkdir src\slip18
|
|
1528
|
+
code .
|
|
1529
|
+
npm start
|
|
1530
|
+
|
|
1531
|
+
FOLDER STRUCTURE
|
|
1532
|
+
|
|
1533
|
+
slip18/
|
|
1534
|
+
`-- src/
|
|
1535
|
+
|-- slip18/
|
|
1536
|
+
| `-- Profile.js
|
|
1537
|
+
|-- App.js
|
|
1538
|
+
`-- index.js
|
|
1539
|
+
|
|
1540
|
+
--------------------------------------------------------------------------------
|
|
1541
|
+
FILE : Profile.js
|
|
1542
|
+
PATH : slip18/src/slip18/Profile.js
|
|
1543
|
+
--------------------------------------------------------------------------------
|
|
1544
|
+
import React from 'react';
|
|
1545
|
+
|
|
1546
|
+
const Profile = () => {
|
|
1547
|
+
|
|
1548
|
+
const name = "Your Name";
|
|
1549
|
+
const bio = "I'm a Second year M.Sc.CA student.";
|
|
1550
|
+
const desc = "Learning React";
|
|
1551
|
+
|
|
1552
|
+
return (
|
|
1553
|
+
<div>
|
|
1554
|
+
<h2>My Profile</h2>
|
|
1555
|
+
<h3>{name}</h3>
|
|
1556
|
+
<p>{bio}</p>
|
|
1557
|
+
<img src='/logo192.png' alt='logo'></img>
|
|
1558
|
+
<p>{desc}</p>
|
|
1559
|
+
</div>
|
|
1560
|
+
)
|
|
1561
|
+
}
|
|
1562
|
+
|
|
1563
|
+
export default Profile
|
|
1564
|
+
|
|
1565
|
+
--------------------------------------------------------------------------------
|
|
1566
|
+
FILE : App.js
|
|
1567
|
+
PATH : slip18/src/App.js
|
|
1568
|
+
--------------------------------------------------------------------------------
|
|
1569
|
+
import Profile from './slip18/Profile';
|
|
1570
|
+
|
|
1571
|
+
function App() {
|
|
1572
|
+
return <Profile />;
|
|
1573
|
+
}
|
|
1574
|
+
|
|
1575
|
+
export default App;
|
|
1576
|
+
|
|
1577
|
+
################################################################################
|
|
1578
|
+
# SLIP 19 : List of 10 favourite movies
|
|
1579
|
+
################################################################################
|
|
1580
|
+
|
|
1581
|
+
QUESTION
|
|
1582
|
+
|
|
1583
|
+
Design a functional component which displays a list of 10 favourite movies using JSX.
|
|
1584
|
+
|
|
1585
|
+
COMMANDS
|
|
1586
|
+
|
|
1587
|
+
npx create-react-app slip19
|
|
1588
|
+
cd slip19
|
|
1589
|
+
mkdir src\slip19
|
|
1590
|
+
code .
|
|
1591
|
+
npm start
|
|
1592
|
+
|
|
1593
|
+
FOLDER STRUCTURE
|
|
1594
|
+
|
|
1595
|
+
slip19/
|
|
1596
|
+
`-- src/
|
|
1597
|
+
|-- slip19/
|
|
1598
|
+
| `-- Movies.js
|
|
1599
|
+
|-- App.js
|
|
1600
|
+
`-- index.js
|
|
1601
|
+
|
|
1602
|
+
--------------------------------------------------------------------------------
|
|
1603
|
+
FILE : Movies.js
|
|
1604
|
+
PATH : slip19/src/slip19/Movies.js
|
|
1605
|
+
--------------------------------------------------------------------------------
|
|
1606
|
+
import React from 'react'
|
|
1607
|
+
|
|
1608
|
+
const Movies = () => {
|
|
1609
|
+
|
|
1610
|
+
const movies = ['3 idiots', 'Bhag milkha bhag', 'Shershah', 'Amaran', 'Avengers', 'Drishyam', 'Golmaal', 'Singham', 'Mitwa', 'Timepass'];
|
|
1611
|
+
|
|
1612
|
+
return (
|
|
1613
|
+
<div>
|
|
1614
|
+
<h2>Favorite Movies List</h2>
|
|
1615
|
+
<ul>
|
|
1616
|
+
{movies.map((item, index) =>(
|
|
1617
|
+
<li key={index}>{item}</li>
|
|
1618
|
+
))}
|
|
1619
|
+
</ul>
|
|
1620
|
+
</div>
|
|
1621
|
+
)
|
|
1622
|
+
}
|
|
1623
|
+
|
|
1624
|
+
export default Movies
|
|
1625
|
+
|
|
1626
|
+
--------------------------------------------------------------------------------
|
|
1627
|
+
FILE : App.js
|
|
1628
|
+
PATH : slip19/src/App.js
|
|
1629
|
+
--------------------------------------------------------------------------------
|
|
1630
|
+
import Movies from './slip19/Movies';
|
|
1631
|
+
|
|
1632
|
+
function App() {
|
|
1633
|
+
return <Movies />;
|
|
1634
|
+
}
|
|
1635
|
+
|
|
1636
|
+
export default App;
|
|
1637
|
+
|
|
1638
|
+
################################################################################
|
|
1639
|
+
# SLIP 20 : Login with route parameters
|
|
1640
|
+
################################################################################
|
|
1641
|
+
|
|
1642
|
+
QUESTION
|
|
1643
|
+
|
|
1644
|
+
Create a login system to pass parameters through routes and display dynamic data (for example /user/:id should show user details).
|
|
1645
|
+
|
|
1646
|
+
COMMANDS
|
|
1647
|
+
|
|
1648
|
+
npx create-react-app slip20
|
|
1649
|
+
cd slip20
|
|
1650
|
+
npm install react-router-dom
|
|
1651
|
+
mkdir src\slip20
|
|
1652
|
+
code .
|
|
1653
|
+
npm start
|
|
1654
|
+
|
|
1655
|
+
FOLDER STRUCTURE
|
|
1656
|
+
|
|
1657
|
+
slip20/
|
|
1658
|
+
`-- src/
|
|
1659
|
+
|-- slip20/
|
|
1660
|
+
| |-- Login.js
|
|
1661
|
+
| `-- Students.js
|
|
1662
|
+
|-- App.js
|
|
1663
|
+
`-- index.js
|
|
1664
|
+
|
|
1665
|
+
--------------------------------------------------------------------------------
|
|
1666
|
+
FILE : Login.js
|
|
1667
|
+
PATH : slip20/src/slip20/Login.js
|
|
1668
|
+
--------------------------------------------------------------------------------
|
|
1669
|
+
import React, { useState } from 'react';
|
|
1670
|
+
import { useNavigate } from 'react-router-dom';
|
|
1671
|
+
|
|
1672
|
+
const Login = () => {
|
|
1673
|
+
|
|
1674
|
+
const [id, setId] = useState('');
|
|
1675
|
+
const [username, setUsername] = useState('');
|
|
1676
|
+
const [password, setPassword] = useState('');
|
|
1677
|
+
const navigate = useNavigate();
|
|
1678
|
+
|
|
1679
|
+
const users = [
|
|
1680
|
+
{id: 101, name: 'Asha'},
|
|
1681
|
+
{id: 102, name: 'Rahul'},
|
|
1682
|
+
{id: 103, name: 'Urmila'},
|
|
1683
|
+
{id: 104, name: 'Satish'}
|
|
1684
|
+
];
|
|
1685
|
+
|
|
1686
|
+
const handleSubmit = (e) =>{
|
|
1687
|
+
e.preventDefault();
|
|
1688
|
+
const user = users.find((item) => item.id === Number(id));
|
|
1689
|
+
if(user !== undefined && username === 'Admin' && password === '1234'){
|
|
1690
|
+
navigate(`/user/${id}`);
|
|
1691
|
+
}
|
|
1692
|
+
else{
|
|
1693
|
+
alert("Invalid credentials");
|
|
1694
|
+
}
|
|
1695
|
+
console.log("Id: ", id);
|
|
1696
|
+
console.log("Username: ", username);
|
|
1697
|
+
console.log("Password: ", password);
|
|
1698
|
+
}
|
|
1699
|
+
|
|
1700
|
+
return (
|
|
1701
|
+
<div>
|
|
1702
|
+
<h2>Login Form</h2>
|
|
1703
|
+
<form onSubmit={handleSubmit}>
|
|
1704
|
+
<label>UserId<input type='text' value={id} onChange={(e) => setId(e.target.value)}></input></label><br></br>
|
|
1705
|
+
<label>Username<input type='text' value={username} onChange={(e) => setUsername(e.target.value)}></input></label><br></br>
|
|
1706
|
+
<label>Password<input type='password' value={password} onChange={(e) => setPassword(e.target.value)}></input></label><br></br>
|
|
1707
|
+
<button type='submit'>Login</button>
|
|
1708
|
+
</form>
|
|
1709
|
+
</div>
|
|
1710
|
+
)
|
|
1711
|
+
}
|
|
1712
|
+
|
|
1713
|
+
export default Login
|
|
1714
|
+
|
|
1715
|
+
--------------------------------------------------------------------------------
|
|
1716
|
+
FILE : Students.js
|
|
1717
|
+
PATH : slip20/src/slip20/Students.js
|
|
1718
|
+
--------------------------------------------------------------------------------
|
|
1719
|
+
import React from 'react';
|
|
1720
|
+
import {useParams} from 'react-router-dom';
|
|
1721
|
+
|
|
1722
|
+
const Students = () => {
|
|
1723
|
+
|
|
1724
|
+
const {id} = useParams();
|
|
1725
|
+
const users = [
|
|
1726
|
+
{id: 101, name: 'Asha'},
|
|
1727
|
+
{id: 102, name: 'Rahul'},
|
|
1728
|
+
{id: 103, name: 'Urmila'},
|
|
1729
|
+
{id: 104, name: 'Satish'}
|
|
1730
|
+
];
|
|
1731
|
+
|
|
1732
|
+
const user = users.find((item) => item.id === Number(id));
|
|
1733
|
+
|
|
1734
|
+
return (
|
|
1735
|
+
<div>
|
|
1736
|
+
{user ? (<div>
|
|
1737
|
+
<h2>Student Id: {user.id}</h2>
|
|
1738
|
+
<p>Student Name: {user.name}</p>
|
|
1739
|
+
</div>
|
|
1740
|
+
) : (<h2>User not found</h2>)}
|
|
1741
|
+
</div>
|
|
1742
|
+
)
|
|
1743
|
+
}
|
|
1744
|
+
|
|
1745
|
+
export default Students
|
|
1746
|
+
|
|
1747
|
+
--------------------------------------------------------------------------------
|
|
1748
|
+
FILE : App.js
|
|
1749
|
+
PATH : slip20/src/App.js
|
|
1750
|
+
--------------------------------------------------------------------------------
|
|
1751
|
+
import { BrowserRouter, Routes, Route } from 'react-router-dom';
|
|
1752
|
+
import Login from './slip20/Login';
|
|
1753
|
+
import Students from './slip20/Students';
|
|
1754
|
+
|
|
1755
|
+
function App() {
|
|
1756
|
+
return (
|
|
1757
|
+
<BrowserRouter>
|
|
1758
|
+
<Routes>
|
|
1759
|
+
<Route path="/" element={<Login />} />
|
|
1760
|
+
<Route path="/user/:id" element={<Students />} />
|
|
1761
|
+
</Routes>
|
|
1762
|
+
</BrowserRouter>
|
|
1763
|
+
);
|
|
1764
|
+
}
|
|
1765
|
+
|
|
1766
|
+
export default App;
|
|
1767
|
+
|
|
1768
|
+
################################################################################
|
|
1769
|
+
# SLIP 21 : Even/odd and palindrome
|
|
1770
|
+
################################################################################
|
|
1771
|
+
|
|
1772
|
+
QUESTION
|
|
1773
|
+
|
|
1774
|
+
Take a number and a string from the user. Check whether the number is even or odd and whether the string is a palindrome. Display appropriate messages.
|
|
1775
|
+
|
|
1776
|
+
COMMANDS
|
|
1777
|
+
|
|
1778
|
+
npx create-react-app slip21
|
|
1779
|
+
cd slip21
|
|
1780
|
+
mkdir src\slip21
|
|
1781
|
+
code .
|
|
1782
|
+
npm start
|
|
1783
|
+
|
|
1784
|
+
FOLDER STRUCTURE
|
|
1785
|
+
|
|
1786
|
+
slip21/
|
|
1787
|
+
`-- src/
|
|
1788
|
+
|-- slip21/
|
|
1789
|
+
| `-- NumberString.js
|
|
1790
|
+
|-- App.js
|
|
1791
|
+
`-- index.js
|
|
1792
|
+
|
|
1793
|
+
--------------------------------------------------------------------------------
|
|
1794
|
+
FILE : NumberString.js
|
|
1795
|
+
PATH : slip21/src/slip21/NumberString.js
|
|
1796
|
+
--------------------------------------------------------------------------------
|
|
1797
|
+
import React, { useState } from 'react';
|
|
1798
|
+
|
|
1799
|
+
const NumberString = () => {
|
|
1800
|
+
|
|
1801
|
+
const [number, setNumber] = useState('');
|
|
1802
|
+
const [string, setString] = useState('');
|
|
1803
|
+
const [numberResult, setNumberResult] = useState(null);
|
|
1804
|
+
const [stringResult, setStringResult] = useState(null);
|
|
1805
|
+
|
|
1806
|
+
const EvenOdd = () =>{
|
|
1807
|
+
if(Number(number) % 2 === 0){
|
|
1808
|
+
setNumberResult("Even");
|
|
1809
|
+
}else{
|
|
1810
|
+
setNumberResult("Odd");
|
|
1811
|
+
}
|
|
1812
|
+
}
|
|
1813
|
+
|
|
1814
|
+
const checkPalindrome = () =>{
|
|
1815
|
+
let str = string.split('').reverse().join('');
|
|
1816
|
+
if(str === string){
|
|
1817
|
+
setStringResult("Palindrome");
|
|
1818
|
+
}else{
|
|
1819
|
+
setStringResult("Not palidrome");
|
|
1820
|
+
}
|
|
1821
|
+
}
|
|
1822
|
+
|
|
1823
|
+
const handleSubmit = (e) =>{
|
|
1824
|
+
e.preventDefault();
|
|
1825
|
+
EvenOdd();
|
|
1826
|
+
checkPalindrome();
|
|
1827
|
+
}
|
|
1828
|
+
|
|
1829
|
+
return (
|
|
1830
|
+
<div>
|
|
1831
|
+
<form onSubmit={handleSubmit}>
|
|
1832
|
+
<label>Enter number:<input type='text' value={number} onChange={(e) => setNumber(e.target.value)}></input></label>
|
|
1833
|
+
<label>Enter string:<input type='text' value={string} onChange={(e) => setString(e.target.value)}></input></label>
|
|
1834
|
+
<button type='submit'>Check</button>
|
|
1835
|
+
</form>
|
|
1836
|
+
<p>Number is {numberResult}</p>
|
|
1837
|
+
<p>String is {stringResult}</p>
|
|
1838
|
+
</div>
|
|
1839
|
+
)
|
|
1840
|
+
}
|
|
1841
|
+
|
|
1842
|
+
export default NumberString
|
|
1843
|
+
|
|
1844
|
+
--------------------------------------------------------------------------------
|
|
1845
|
+
FILE : App.js
|
|
1846
|
+
PATH : slip21/src/App.js
|
|
1847
|
+
--------------------------------------------------------------------------------
|
|
1848
|
+
import NumberString from './slip21/NumberString';
|
|
1849
|
+
|
|
1850
|
+
function App() {
|
|
1851
|
+
return <NumberString />;
|
|
1852
|
+
}
|
|
1853
|
+
|
|
1854
|
+
export default App;
|