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
fpythons/data/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# React practical slips: solutions
|
|
2
|
+
|
|
3
|
+
Read [00_START_HERE.md](00_START_HERE.md) first.
|
|
4
|
+
|
|
5
|
+
| Slip | Topic | Guide | Solved project |
|
|
6
|
+
|---|---|---|---|
|
|
7
|
+
| 1 | Login form with event handlers | [Slip 1](Slip_01.md) | [code](code/slip1/) |
|
|
8
|
+
| 2 | useEffect and fetching a users list | [Slip 2](Slip_02.md) | [code](code/slip2/) |
|
|
9
|
+
| 3 | Toggle ON/OFF with background colour | [Slip 3](Slip_03.md) | [code](code/slip3/) |
|
|
10
|
+
| 4 | Multi-page app with react-router-dom | [Slip 4](Slip_04.md) | [code](code/slip4/) |
|
|
11
|
+
| 5 | Login validation and 404 route | [Slip 5](Slip_05.md) | [code](code/slip5/) |
|
|
12
|
+
| 6 | Protected route with mock authentication | [Slip 6](Slip_06.md) | [code](code/slip6/) |
|
|
13
|
+
| 7 | To-do list | [Slip 7](Slip_07.md) | [code](code/slip7/) |
|
|
14
|
+
| 8 | Toggle button ON/OFF | [Slip 8](Slip_08.md) | [code](code/slip8/) |
|
|
15
|
+
| 9 | Counter with initial value as a prop | [Slip 9](Slip_09.md) | [code](code/slip9/) |
|
|
16
|
+
| 10 | Validate a number and sum its digits | [Slip 10](Slip_10.md) | [code](code/slip10/) |
|
|
17
|
+
| 11 | String operations | [Slip 11](Slip_11.md) | [code](code/slip11/) |
|
|
18
|
+
| 12 | Notes app with Redux Toolkit | [Slip 12](Slip_12.md) | [code](code/slip12/) |
|
|
19
|
+
| 13 | Counter with a Redux store | [Slip 13](Slip_13.md) | [code](code/slip13/) |
|
|
20
|
+
| 14 | Calculator | [Slip 14](Slip_14.md) | [code](code/slip14/) |
|
|
21
|
+
| 15 | Counter using useReducer | [Slip 15](Slip_15.md) | [code](code/slip15/) |
|
|
22
|
+
| 16 | Virtual DOM and traditional DOM | [Slip 16](Slip_16.md) | [code](code/slip16/) |
|
|
23
|
+
| 17 | Sign in and sign out | [Slip 17](Slip_17.md) | [code](code/slip17/) |
|
|
24
|
+
| 18 | Profile page | [Slip 18](Slip_18.md) | [code](code/slip18/) |
|
|
25
|
+
| 19 | List of 10 favourite movies | [Slip 19](Slip_19.md) | [code](code/slip19/) |
|
|
26
|
+
| 20 | Login with route parameters | [Slip 20](Slip_20.md) | [code](code/slip20/) |
|
|
27
|
+
| 21 | Even/odd and palindrome | [Slip 21](Slip_21.md) | [code](code/slip21/) |
|
|
28
|
+
|
|
29
|
+
## Run a solved project
|
|
30
|
+
|
|
31
|
+
```powershell
|
|
32
|
+
cd code\slip1
|
|
33
|
+
npm install
|
|
34
|
+
npm start
|
|
35
|
+
```
|
fpythons/data/Slip_01.md
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# Slip 1: Login form with event handlers
|
|
2
|
+
|
|
3
|
+
[All slips](README.md) | [Basics: read first](00_START_HERE.md) | [Viva](VIVA.md)
|
|
4
|
+
|
|
5
|
+
## 1. Question
|
|
6
|
+
|
|
7
|
+
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.
|
|
8
|
+
|
|
9
|
+
## 2. Files you will touch
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
slip1/
|
|
13
|
+
src/slip1/style.css
|
|
14
|
+
src/slip1/loginForm.js
|
|
15
|
+
src/App.js
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## 3. Commands (PowerShell, in this order)
|
|
19
|
+
|
|
20
|
+
```powershell
|
|
21
|
+
npx create-react-app slip1
|
|
22
|
+
cd slip1
|
|
23
|
+
mkdir src\slip1
|
|
24
|
+
code .
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## 4. Write the code
|
|
28
|
+
|
|
29
|
+
### Step 1: `src/slip1/style.css`
|
|
30
|
+
|
|
31
|
+
```css
|
|
32
|
+
body{
|
|
33
|
+
margin: 0;
|
|
34
|
+
padding: 0;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
.login-container{
|
|
38
|
+
display: flex;
|
|
39
|
+
justify-content: center;
|
|
40
|
+
align-items: center;
|
|
41
|
+
flex-direction: column;
|
|
42
|
+
height: 100vh;
|
|
43
|
+
background-color: rgba(0, 255, 255, 0.263);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.heading{
|
|
47
|
+
font-size: 2rem;
|
|
48
|
+
text-align: center;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.login-form{
|
|
52
|
+
display: flex;
|
|
53
|
+
flex-direction: column;
|
|
54
|
+
border: 2px solid #ccc;
|
|
55
|
+
border-radius: 8px;
|
|
56
|
+
width: 300px;
|
|
57
|
+
height: 150px;
|
|
58
|
+
padding: 10px;
|
|
59
|
+
gap: 5px;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
.login-form input {
|
|
63
|
+
padding: 6px;
|
|
64
|
+
border: 1px solid #ccc;
|
|
65
|
+
border-radius: 5px;
|
|
66
|
+
font-size: 14px;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
.login-form input[type="submit"] {
|
|
70
|
+
background-color: #2563eb;
|
|
71
|
+
color: white;
|
|
72
|
+
border: none;
|
|
73
|
+
padding: 6px;
|
|
74
|
+
border-radius: 5px;
|
|
75
|
+
font-size: 16px;
|
|
76
|
+
cursor: pointer;
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Step 2: `src/slip1/loginForm.js`
|
|
81
|
+
|
|
82
|
+
```jsx
|
|
83
|
+
import React, { useState } from 'react'
|
|
84
|
+
import './style.css';
|
|
85
|
+
|
|
86
|
+
const LoginForm = () => {
|
|
87
|
+
|
|
88
|
+
const [username, setUsername] = useState("");
|
|
89
|
+
const [password, setPassword] = useState("");
|
|
90
|
+
|
|
91
|
+
function handleSubmit(e){
|
|
92
|
+
e.preventDefault();
|
|
93
|
+
console.log("Username:", username);
|
|
94
|
+
console.log("Password:", password);
|
|
95
|
+
};
|
|
96
|
+
return (
|
|
97
|
+
<div className="login-container">
|
|
98
|
+
<h2 className="heading">Login Form</h2>
|
|
99
|
+
<form onSubmit={handleSubmit} className="login-form">
|
|
100
|
+
<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>
|
|
101
|
+
<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>
|
|
102
|
+
<input type='submit' value="Login"></input>
|
|
103
|
+
</form>
|
|
104
|
+
</div>
|
|
105
|
+
)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export default LoginForm
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Step 3: `src/App.js`
|
|
112
|
+
|
|
113
|
+
```jsx
|
|
114
|
+
import LoginForm from './slip1/loginForm';
|
|
115
|
+
|
|
116
|
+
function App() {
|
|
117
|
+
return <LoginForm />;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export default App;
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## 5. How it works
|
|
124
|
+
|
|
125
|
+
- `useState` holds `username` and `password`. Each input shows the state value (a *controlled component*).
|
|
126
|
+
- `onChange` saves the typed value in state.
|
|
127
|
+
- `onInput` prints the value in the console on every key press.
|
|
128
|
+
- `onSubmit` is written on the `<form>`. `e.preventDefault()` stops the page from reloading, then both values are printed.
|
|
129
|
+
- `import './style.css'` applies the styling. JSX uses `className`, not `class`.
|
|
130
|
+
|
|
131
|
+
## 6. Run it
|
|
132
|
+
|
|
133
|
+
```powershell
|
|
134
|
+
npm start
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Open the console (F12), type in both boxes, click **Login**.
|
|
138
|
+
|
|
139
|
+
## 7. Errors and fixes
|
|
140
|
+
|
|
141
|
+
| Problem | Fix |
|
|
142
|
+
|---|---|
|
|
143
|
+
| Cannot type in the box | `value` is given but `onChange` is missing or wrong |
|
|
144
|
+
| Page reloads on submit | add `e.preventDefault()` |
|
|
145
|
+
| No styling | check the file name and path in `import './style.css'` |
|
|
146
|
+
|
|
147
|
+
## 8. Viva questions
|
|
148
|
+
|
|
149
|
+
**Q. What is a controlled component?**
|
|
150
|
+
An input whose value comes from React state and changes through an event handler.
|
|
151
|
+
|
|
152
|
+
**Q. Why is `preventDefault()` used?**
|
|
153
|
+
It stops the browser's default action, here reloading the page on form submit.
|
|
154
|
+
|
|
155
|
+
**Q. Difference between `onChange` and `onInput` in React?**
|
|
156
|
+
In React both fire on every change of the input. `onChange` is the one normally used.
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
Solved project: [code/slip1](code/slip1/)
|
fpythons/data/Slip_02.md
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# Slip 2: useEffect and fetching a users list
|
|
2
|
+
|
|
3
|
+
[All slips](README.md) | [Basics: read first](00_START_HERE.md) | [Viva](VIVA.md)
|
|
4
|
+
|
|
5
|
+
## 1. Question
|
|
6
|
+
|
|
7
|
+
Create a React functional component using `useEffect()` that fetches and displays data from an API (for example a users list) on the console.
|
|
8
|
+
|
|
9
|
+
## 2. Files you will touch
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
slip2/
|
|
13
|
+
src/slip2/useEffect.js
|
|
14
|
+
src/App.js
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 3. Commands (PowerShell, in this order)
|
|
18
|
+
|
|
19
|
+
```powershell
|
|
20
|
+
npx create-react-app slip2
|
|
21
|
+
cd slip2
|
|
22
|
+
mkdir src\slip2
|
|
23
|
+
code .
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 4. Write the code
|
|
27
|
+
|
|
28
|
+
### Step 1: `src/slip2/useEffect.js`
|
|
29
|
+
|
|
30
|
+
```jsx
|
|
31
|
+
import React, { useState, useEffect } from 'react'
|
|
32
|
+
|
|
33
|
+
const UseEffect = () => {
|
|
34
|
+
const [users, setUsers] = useState([]);
|
|
35
|
+
|
|
36
|
+
useEffect(() =>{
|
|
37
|
+
fetch("https://jsonplaceholder.typicode.com/users")
|
|
38
|
+
.then(res => res.json())
|
|
39
|
+
.then(data =>{ setUsers(data);
|
|
40
|
+
console.log(data);
|
|
41
|
+
})
|
|
42
|
+
}, [])
|
|
43
|
+
return (
|
|
44
|
+
<div>
|
|
45
|
+
<h1>User List:</h1>
|
|
46
|
+
{users.map(user =>(
|
|
47
|
+
<p key={user.id}>{user.name}</p>
|
|
48
|
+
))}
|
|
49
|
+
</div>
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export default UseEffect
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Step 2: `src/App.js`
|
|
57
|
+
|
|
58
|
+
```jsx
|
|
59
|
+
import UseEffect from './slip2/useEffect';
|
|
60
|
+
|
|
61
|
+
function App() {
|
|
62
|
+
return <UseEffect />;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export default App;
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## 5. How it works
|
|
69
|
+
|
|
70
|
+
- `useState([])` starts with an empty list.
|
|
71
|
+
- `useEffect(..., [])` runs after the first render. The empty array means it does not run again on later renders.
|
|
72
|
+
- `fetch` calls the API, `res.json()` converts the response, then the data is saved and printed with `console.log`.
|
|
73
|
+
- `users.map` shows each name. `key={user.id}` identifies each item.
|
|
74
|
+
|
|
75
|
+
## 6. Run it
|
|
76
|
+
|
|
77
|
+
```powershell
|
|
78
|
+
npm start
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Internet is required. Open the console (F12) to see the array. In development, React Strict Mode runs the effect twice, so the log appears twice.
|
|
82
|
+
|
|
83
|
+
## 7. Errors and fixes
|
|
84
|
+
|
|
85
|
+
| Problem | Fix |
|
|
86
|
+
|---|---|
|
|
87
|
+
| Requests repeat without end | the `[]` dependency array is missing |
|
|
88
|
+
| List stays empty | check the internet connection and the URL |
|
|
89
|
+
| Warning about `key` | give each list item a unique `key` |
|
|
90
|
+
|
|
91
|
+
## 8. Viva questions
|
|
92
|
+
|
|
93
|
+
**Q. What is `useEffect` used for?**
|
|
94
|
+
Side effects such as API calls, timers and subscriptions.
|
|
95
|
+
|
|
96
|
+
**Q. What does an empty dependency array mean?**
|
|
97
|
+
Run the effect only after the first render (mount).
|
|
98
|
+
|
|
99
|
+
**Q. What does `fetch` return?**
|
|
100
|
+
A Promise that resolves to a Response object.
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
Solved project: [code/slip2](code/slip2/)
|
fpythons/data/Slip_03.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# Slip 3: Toggle ON/OFF with background colour
|
|
2
|
+
|
|
3
|
+
[All slips](README.md) | [Basics: read first](00_START_HERE.md) | [Viva](VIVA.md)
|
|
4
|
+
|
|
5
|
+
## 1. Question
|
|
6
|
+
|
|
7
|
+
Create a button that toggles between ON and OFF states when clicked. Change the background colour of the UI when the button state changes.
|
|
8
|
+
|
|
9
|
+
## 2. Files you will touch
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
slip3/
|
|
13
|
+
src/slip3/toggleBtn.js
|
|
14
|
+
src/App.js
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 3. Commands (PowerShell, in this order)
|
|
18
|
+
|
|
19
|
+
```powershell
|
|
20
|
+
npx create-react-app slip3
|
|
21
|
+
cd slip3
|
|
22
|
+
mkdir src\slip3
|
|
23
|
+
code .
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 4. Write the code
|
|
27
|
+
|
|
28
|
+
### Step 1: `src/slip3/toggleBtn.js`
|
|
29
|
+
|
|
30
|
+
```jsx
|
|
31
|
+
import React, { useState } from 'react'
|
|
32
|
+
|
|
33
|
+
const ToggleBtn = () => {
|
|
34
|
+
const [status, setStatus] = useState('ON');
|
|
35
|
+
function checkStatus(){
|
|
36
|
+
setStatus(status === 'ON' ? 'OFF' : 'ON');
|
|
37
|
+
}
|
|
38
|
+
return (
|
|
39
|
+
<div style = {{backgroundColor : status === 'ON' ? 'lightgreen' : 'lightcoral'}}>
|
|
40
|
+
<h2>{status}</h2>
|
|
41
|
+
<button onClick={checkStatus}>{status === 'ON' ? 'Turn OFF' : 'Turn ON'}</button>
|
|
42
|
+
</div>
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export default ToggleBtn
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Step 2: `src/App.js`
|
|
50
|
+
|
|
51
|
+
```jsx
|
|
52
|
+
import ToggleBtn from './slip3/toggleBtn';
|
|
53
|
+
|
|
54
|
+
function App() {
|
|
55
|
+
return <ToggleBtn />;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export default App;
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## 5. How it works
|
|
62
|
+
|
|
63
|
+
- `status` starts as `'ON'`.
|
|
64
|
+
- Clicking the button calls `checkStatus`, which switches the value with a ternary operator.
|
|
65
|
+
- The inline `style` object picks the background colour from `status`. React then re-renders.
|
|
66
|
+
|
|
67
|
+
## 6. Run it
|
|
68
|
+
|
|
69
|
+
```powershell
|
|
70
|
+
npm start
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Click the button. The heading, button text and background colour change together.
|
|
74
|
+
|
|
75
|
+
## 7. Errors and fixes
|
|
76
|
+
|
|
77
|
+
| Problem | Fix |
|
|
78
|
+
|---|---|
|
|
79
|
+
| `Too many re-renders` | write `onClick={checkStatus}`, not `onClick={checkStatus()}` |
|
|
80
|
+
| Style error | `style` needs an object: `style={{backgroundColor: 'red'}}` |
|
|
81
|
+
|
|
82
|
+
## 8. Viva questions
|
|
83
|
+
|
|
84
|
+
**Q. What is state?**
|
|
85
|
+
Data kept inside a component. Changing it re-renders the component.
|
|
86
|
+
|
|
87
|
+
**Q. Why two curly braces in `style={{ }}`?**
|
|
88
|
+
The outer pair means JavaScript inside JSX, the inner pair is the style object.
|
|
89
|
+
|
|
90
|
+
**Q. What is a hook?**
|
|
91
|
+
A function such as `useState` that gives React features to functional components.
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
Solved project: [code/slip3](code/slip3/)
|
fpythons/data/Slip_04.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# Slip 4: Multi-page app with react-router-dom
|
|
2
|
+
|
|
3
|
+
[All slips](README.md) | [Basics: read first](00_START_HERE.md) | [Viva](VIVA.md)
|
|
4
|
+
|
|
5
|
+
## 1. Question
|
|
6
|
+
|
|
7
|
+
Create a multi-page React application using `react-router-dom` with Home, About and Contact pages. For each route display appropriate UI with validation.
|
|
8
|
+
|
|
9
|
+
## 2. Files you will touch
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
slip4/
|
|
13
|
+
src/slip4/About.js
|
|
14
|
+
src/slip4/Contact.js
|
|
15
|
+
src/slip4/home.js
|
|
16
|
+
src/App.js
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 3. Commands (PowerShell, in this order)
|
|
20
|
+
|
|
21
|
+
```powershell
|
|
22
|
+
npx create-react-app slip4
|
|
23
|
+
cd slip4
|
|
24
|
+
npm install react-router-dom
|
|
25
|
+
mkdir src\slip4
|
|
26
|
+
code .
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## 4. Write the code
|
|
30
|
+
|
|
31
|
+
### Step 1: `src/slip4/About.js`
|
|
32
|
+
|
|
33
|
+
```jsx
|
|
34
|
+
function About(){
|
|
35
|
+
return(
|
|
36
|
+
<h1>This is a About page.</h1>
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default About
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Step 2: `src/slip4/Contact.js`
|
|
44
|
+
|
|
45
|
+
```jsx
|
|
46
|
+
function Contact(){
|
|
47
|
+
return(
|
|
48
|
+
<h1>Contact Us</h1>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default Contact
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Step 3: `src/slip4/home.js`
|
|
56
|
+
|
|
57
|
+
```jsx
|
|
58
|
+
function Home(){
|
|
59
|
+
return(
|
|
60
|
+
<h1>Hello, Welcome to the Home!</h1>
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export default Home
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Step 4: `src/App.js`
|
|
68
|
+
|
|
69
|
+
```jsx
|
|
70
|
+
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';
|
|
71
|
+
import Home from './slip4/home';
|
|
72
|
+
import About from './slip4/About';
|
|
73
|
+
import Contact from './slip4/Contact';
|
|
74
|
+
|
|
75
|
+
function App() {
|
|
76
|
+
return (
|
|
77
|
+
<BrowserRouter>
|
|
78
|
+
<Link to="/">Home</Link> |{' '}
|
|
79
|
+
<Link to="/about">About</Link> |{' '}
|
|
80
|
+
<Link to="/contact">Contact Us</Link>
|
|
81
|
+
|
|
82
|
+
<Routes>
|
|
83
|
+
<Route path="/" element={<Home />} />
|
|
84
|
+
<Route path="/about" element={<About />} />
|
|
85
|
+
<Route path="/contact" element={<Contact />} />
|
|
86
|
+
<Route path="*" element={<h1>Page Not Found!</h1>} />
|
|
87
|
+
</Routes>
|
|
88
|
+
</BrowserRouter>
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export default App;
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## 5. How it works
|
|
96
|
+
|
|
97
|
+
- `BrowserRouter` enables routing.
|
|
98
|
+
- `Link` changes the address without reloading the page.
|
|
99
|
+
- `Routes` picks the first matching `Route` and shows its `element`.
|
|
100
|
+
- `path="*"` matches any other address and shows "Page Not Found!". This is the route validation.
|
|
101
|
+
|
|
102
|
+
## 6. Run it
|
|
103
|
+
|
|
104
|
+
```powershell
|
|
105
|
+
npm start
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Click each link. Then type `/xyz` after the address to see the not-found message.
|
|
109
|
+
|
|
110
|
+
## 7. Errors and fixes
|
|
111
|
+
|
|
112
|
+
| Problem | Fix |
|
|
113
|
+
|---|---|
|
|
114
|
+
| `Module not found: react-router-dom` | `npm install react-router-dom` |
|
|
115
|
+
| `useHref() may be used only in the context of a Router` | put `Link` inside `BrowserRouter` |
|
|
116
|
+
| Blank page | use `element={<Home />}`, not `element={Home}` |
|
|
117
|
+
|
|
118
|
+
## 8. Viva questions
|
|
119
|
+
|
|
120
|
+
**Q. What is a Single Page Application?**
|
|
121
|
+
One HTML page whose content changes with JavaScript, without full reloads.
|
|
122
|
+
|
|
123
|
+
**Q. Difference between `Link` and `<a>`?**
|
|
124
|
+
`<a>` reloads the page. `Link` changes only the route.
|
|
125
|
+
|
|
126
|
+
**Q. What does `path="*"` do?**
|
|
127
|
+
It matches every address that no other route matches.
|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
Solved project: [code/slip4](code/slip4/)
|
fpythons/data/Slip_05.md
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# Slip 5: Login validation and 404 route
|
|
2
|
+
|
|
3
|
+
[All slips](README.md) | [Basics: read first](00_START_HERE.md) | [Viva](VIVA.md)
|
|
4
|
+
|
|
5
|
+
## 1. Question
|
|
6
|
+
|
|
7
|
+
Design a login form which accepts username and password.
|
|
8
|
+
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".
|
|
9
|
+
|
|
10
|
+
## 2. Files you will touch
|
|
11
|
+
|
|
12
|
+
```text
|
|
13
|
+
slip5/
|
|
14
|
+
src/slip5/loginValidation.js
|
|
15
|
+
src/App.js
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## 3. Commands (PowerShell, in this order)
|
|
19
|
+
|
|
20
|
+
```powershell
|
|
21
|
+
npx create-react-app slip5
|
|
22
|
+
cd slip5
|
|
23
|
+
npm install react-router-dom
|
|
24
|
+
mkdir src\slip5
|
|
25
|
+
code .
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## 4. Write the code
|
|
29
|
+
|
|
30
|
+
### Step 1: `src/slip5/loginValidation.js`
|
|
31
|
+
|
|
32
|
+
```jsx
|
|
33
|
+
import React, { useState } from 'react'
|
|
34
|
+
|
|
35
|
+
const LoginValidation = () => {
|
|
36
|
+
const [username, setUsername] = useState('');
|
|
37
|
+
const [password, setPassword] = useState('');
|
|
38
|
+
const [message, setMessage] = useState('');
|
|
39
|
+
|
|
40
|
+
function validateLogin(e){
|
|
41
|
+
e.preventDefault();
|
|
42
|
+
if(username === 'Admin' && password === 'Pass123'){
|
|
43
|
+
console.log("Login successful.");
|
|
44
|
+
}
|
|
45
|
+
else{
|
|
46
|
+
setMessage("Unauthorized access.")
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return (
|
|
50
|
+
<div>
|
|
51
|
+
<h1>Login Form</h1>
|
|
52
|
+
<form onSubmit={validateLogin}>
|
|
53
|
+
<label>Username:<input type='text' placeholder='Enter username' value={username} onChange={(e) => setUsername(e.target.value)}></input></label><br></br>
|
|
54
|
+
<label>Password:<input type='password' placeholder='Enter password' value={password} onChange={(e) => setPassword(e.target.value)}></input></label><br></br>
|
|
55
|
+
<input type='submit' value='Login'></input>
|
|
56
|
+
</form>
|
|
57
|
+
<p>{message}</p>
|
|
58
|
+
</div>
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export default LoginValidation
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Step 2: `src/App.js`
|
|
66
|
+
|
|
67
|
+
```jsx
|
|
68
|
+
import { BrowserRouter, Routes, Route, Link, Navigate } from 'react-router-dom';
|
|
69
|
+
import LoginValidation from './slip5/loginValidation';
|
|
70
|
+
|
|
71
|
+
function App() {
|
|
72
|
+
return (
|
|
73
|
+
<BrowserRouter>
|
|
74
|
+
<Link to="/login">Login</Link>
|
|
75
|
+
|
|
76
|
+
<Routes>
|
|
77
|
+
<Route path="/" element={<Navigate to="/login" />} />
|
|
78
|
+
<Route path="/login" element={<LoginValidation />} />
|
|
79
|
+
<Route path="*" element={<h1>404 Error, Page Not Found</h1>} />
|
|
80
|
+
</Routes>
|
|
81
|
+
</BrowserRouter>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export default App;
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## 5. How it works
|
|
89
|
+
|
|
90
|
+
- Two state values hold the inputs and `message` holds the error text.
|
|
91
|
+
- `validateLogin` compares the values with `Admin` and `Pass123`.
|
|
92
|
+
- Correct: "Login successful." is printed in the console. Wrong: `message` is set and shown below the form.
|
|
93
|
+
- `App.js` has the `/login` route and a `*` route for the 404 message.
|
|
94
|
+
|
|
95
|
+
## 6. Run it
|
|
96
|
+
|
|
97
|
+
```powershell
|
|
98
|
+
npm start
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Try `Admin` / `Pass123` with the console open, then wrong values, then the address `/abc`.
|
|
102
|
+
|
|
103
|
+
## 7. Errors and fixes
|
|
104
|
+
|
|
105
|
+
| Problem | Fix |
|
|
106
|
+
|---|---|
|
|
107
|
+
| Valid login rejected | the check is case sensitive |
|
|
108
|
+
| Old error stays after a correct login | add `setMessage('')` in the success branch |
|
|
109
|
+
|
|
110
|
+
## 8. Viva questions
|
|
111
|
+
|
|
112
|
+
**Q. Difference between `==` and `===`?**
|
|
113
|
+
`===` compares value and type. `==` converts types first.
|
|
114
|
+
|
|
115
|
+
**Q. Is this real authentication?**
|
|
116
|
+
No. The values are fixed in the browser code. Real apps check them on a server.
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
Solved project: [code/slip5](code/slip5/)
|