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/Slip_12.md
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# Slip 12: Notes app with Redux Toolkit
|
|
2
|
+
|
|
3
|
+
[All slips](README.md) | [Basics: read first](00_START_HERE.md) | [Viva](VIVA.md)
|
|
4
|
+
|
|
5
|
+
## 1. Question
|
|
6
|
+
|
|
7
|
+
Build a notes management app using Redux Toolkit with `createSlice()` to manage state.
|
|
8
|
+
|
|
9
|
+
## 2. Files you will touch
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
slip12/
|
|
13
|
+
src/slip12/redux/notesSlice.js
|
|
14
|
+
src/slip12/redux/store.js
|
|
15
|
+
src/slip12/NotesManagementApp.js
|
|
16
|
+
src/App.js
|
|
17
|
+
src/index.js
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## 3. Commands (PowerShell, in this order)
|
|
21
|
+
|
|
22
|
+
```powershell
|
|
23
|
+
npx create-react-app slip12
|
|
24
|
+
cd slip12
|
|
25
|
+
npm install @reduxjs/toolkit react-redux
|
|
26
|
+
mkdir src\slip12
|
|
27
|
+
mkdir src\slip12\redux
|
|
28
|
+
code .
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## 4. Write the code
|
|
32
|
+
|
|
33
|
+
### Step 1: `src/slip12/redux/notesSlice.js`
|
|
34
|
+
|
|
35
|
+
```jsx
|
|
36
|
+
import {createSlice} from '@reduxjs/toolkit';
|
|
37
|
+
|
|
38
|
+
const notesSlice = createSlice ({
|
|
39
|
+
name : 'notes',
|
|
40
|
+
initialState : [],
|
|
41
|
+
reducers : {
|
|
42
|
+
|
|
43
|
+
addNote: (state, action) => {
|
|
44
|
+
state.push(action.payload);
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
deleteNote: (state, action) =>{
|
|
48
|
+
state.splice(action.payload, 1);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
export const {addNote, deleteNote} = notesSlice.actions;
|
|
54
|
+
|
|
55
|
+
export default notesSlice.reducer;
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Step 2: `src/slip12/redux/store.js`
|
|
59
|
+
|
|
60
|
+
```jsx
|
|
61
|
+
import {configureStore} from '@reduxjs/toolkit';
|
|
62
|
+
import notesReducer from './notesSlice';
|
|
63
|
+
|
|
64
|
+
const store = configureStore({
|
|
65
|
+
reducer: {
|
|
66
|
+
notes: notesReducer
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
export default store;
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Step 3: `src/slip12/NotesManagementApp.js`
|
|
74
|
+
|
|
75
|
+
```jsx
|
|
76
|
+
import React, { useState } from 'react';
|
|
77
|
+
import { useDispatch, useSelector } from 'react-redux';
|
|
78
|
+
import { addNote, deleteNote } from './redux/notesSlice';
|
|
79
|
+
|
|
80
|
+
const NotesManagementApp = () => {
|
|
81
|
+
|
|
82
|
+
const [note, setNote] = useState('');
|
|
83
|
+
const notes = useSelector((state) => state.notes);
|
|
84
|
+
const dispatch = useDispatch();
|
|
85
|
+
|
|
86
|
+
const handleClick = () =>{
|
|
87
|
+
if(note.trim() === ''){
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
dispatch(addNote(note));
|
|
91
|
+
setNote('');
|
|
92
|
+
}
|
|
93
|
+
return (
|
|
94
|
+
<div>
|
|
95
|
+
<h2>Notes Management App</h2>
|
|
96
|
+
<label>Enter note: <input type='text' value={note} onChange={(e) => setNote(e.target.value)}></input></label>
|
|
97
|
+
<button type='submit' onClick={handleClick}>Add Note</button>
|
|
98
|
+
<ul>
|
|
99
|
+
{notes.map((item, index) =>(
|
|
100
|
+
<li key={index}>
|
|
101
|
+
{item}
|
|
102
|
+
<button type='submit' onClick={() => dispatch(deleteNote(index))}>Delete Note</button>
|
|
103
|
+
</li>
|
|
104
|
+
))}
|
|
105
|
+
</ul>
|
|
106
|
+
</div>
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export default NotesManagementApp
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Step 4: `src/App.js`
|
|
114
|
+
|
|
115
|
+
```jsx
|
|
116
|
+
import NotesManagementApp from './slip12/NotesManagementApp';
|
|
117
|
+
|
|
118
|
+
function App() {
|
|
119
|
+
return <NotesManagementApp />;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export default App;
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Step 5: `src/index.js`
|
|
126
|
+
|
|
127
|
+
```jsx
|
|
128
|
+
import React from 'react';
|
|
129
|
+
import ReactDOM from 'react-dom/client';
|
|
130
|
+
import { Provider } from 'react-redux';
|
|
131
|
+
import App from './App';
|
|
132
|
+
import store from './slip12/redux/store';
|
|
133
|
+
|
|
134
|
+
const root = ReactDOM.createRoot(document.getElementById('root'));
|
|
135
|
+
root.render(
|
|
136
|
+
<React.StrictMode>
|
|
137
|
+
<Provider store={store}>
|
|
138
|
+
<App />
|
|
139
|
+
</Provider>
|
|
140
|
+
</React.StrictMode>
|
|
141
|
+
);
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## 5. How it works
|
|
145
|
+
|
|
146
|
+
- `notesSlice.js`: `createSlice` defines the name, initial state and reducers `addNote` and `deleteNote`.
|
|
147
|
+
- `store.js`: `configureStore` creates the store.
|
|
148
|
+
- `index.js`: `Provider` gives the store to the whole app.
|
|
149
|
+
- Component: `useSelector` reads the notes, `useDispatch` sends actions.
|
|
150
|
+
- Redux Toolkit uses Immer, so `push` and `splice` are safe inside reducers.
|
|
151
|
+
|
|
152
|
+
## 6. Run it
|
|
153
|
+
|
|
154
|
+
```powershell
|
|
155
|
+
npm start
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Add two notes and delete one.
|
|
159
|
+
|
|
160
|
+
## 7. Errors and fixes
|
|
161
|
+
|
|
162
|
+
| Problem | Fix |
|
|
163
|
+
|---|---|
|
|
164
|
+
| `could not find react-redux context value` | wrap `App` in `Provider` in `index.js` |
|
|
165
|
+
| `Module not found` | `npm install @reduxjs/toolkit react-redux` |
|
|
166
|
+
| `notes.map is not a function` | the key in `reducer` must match `state.notes` |
|
|
167
|
+
|
|
168
|
+
## 8. Viva questions
|
|
169
|
+
|
|
170
|
+
**Q. What is Redux?**
|
|
171
|
+
A library that keeps application state in one central store.
|
|
172
|
+
|
|
173
|
+
**Q. What does `createSlice` do?**
|
|
174
|
+
Creates the reducer and action creators for one part of the state.
|
|
175
|
+
|
|
176
|
+
**Q. What is `action.payload`?**
|
|
177
|
+
The data sent with an action.
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
Solved project: [code/slip12](code/slip12/)
|
fpythons/data/Slip_13.md
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
# Slip 13: Counter with a Redux store
|
|
2
|
+
|
|
3
|
+
[All slips](README.md) | [Basics: read first](00_START_HERE.md) | [Viva](VIVA.md)
|
|
4
|
+
|
|
5
|
+
## 1. Question
|
|
6
|
+
|
|
7
|
+
Create a Redux store for counter buttons: display the current count, provide increment and decrement buttons, and display the count on the console.
|
|
8
|
+
|
|
9
|
+
## 2. Files you will touch
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
slip13/
|
|
13
|
+
src/slip13/redux/counterSlice.js
|
|
14
|
+
src/slip13/redux/store.js
|
|
15
|
+
src/slip13/Counter.js
|
|
16
|
+
src/App.js
|
|
17
|
+
src/index.js
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## 3. Commands (PowerShell, in this order)
|
|
21
|
+
|
|
22
|
+
```powershell
|
|
23
|
+
npx create-react-app slip13
|
|
24
|
+
cd slip13
|
|
25
|
+
npm install @reduxjs/toolkit react-redux
|
|
26
|
+
mkdir src\slip13
|
|
27
|
+
mkdir src\slip13\redux
|
|
28
|
+
code .
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## 4. Write the code
|
|
32
|
+
|
|
33
|
+
### Step 1: `src/slip13/redux/counterSlice.js`
|
|
34
|
+
|
|
35
|
+
```jsx
|
|
36
|
+
import { createSlice } from "@reduxjs/toolkit";
|
|
37
|
+
|
|
38
|
+
const counterSlice = createSlice ({
|
|
39
|
+
name : 'counter',
|
|
40
|
+
initialState : 0,
|
|
41
|
+
reducers : {
|
|
42
|
+
increment(state){
|
|
43
|
+
return state + 1;
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
decrement(state){
|
|
47
|
+
return state - 1;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
export const {increment, decrement} = counterSlice.actions;
|
|
53
|
+
|
|
54
|
+
export default counterSlice.reducer;
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Step 2: `src/slip13/redux/store.js`
|
|
58
|
+
|
|
59
|
+
```jsx
|
|
60
|
+
import { configureStore } from "@reduxjs/toolkit";
|
|
61
|
+
import counterReducer from './counterSlice.js';
|
|
62
|
+
|
|
63
|
+
const counterStore = configureStore ({
|
|
64
|
+
reducer : {
|
|
65
|
+
counter : counterReducer
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
export default counterStore;
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Step 3: `src/slip13/Counter.js`
|
|
73
|
+
|
|
74
|
+
```jsx
|
|
75
|
+
import React from 'react';
|
|
76
|
+
import { increment, decrement } from './redux/counterSlice';
|
|
77
|
+
import { useDispatch, useSelector } from 'react-redux';
|
|
78
|
+
|
|
79
|
+
const Counter = () => {
|
|
80
|
+
|
|
81
|
+
const count = useSelector((state) => state.counter);
|
|
82
|
+
const dispatch = useDispatch();
|
|
83
|
+
console.log("Current count is: ",count);
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<div>
|
|
87
|
+
<h2>Count: {count}</h2>
|
|
88
|
+
<button type='submit' onClick={() => dispatch(increment())}>Increment</button>
|
|
89
|
+
<button type='submit' onClick={() => dispatch(decrement())}>Decrement</button>
|
|
90
|
+
</div>
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export default Counter
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Step 4: `src/App.js`
|
|
98
|
+
|
|
99
|
+
```jsx
|
|
100
|
+
import Counter from './slip13/Counter';
|
|
101
|
+
|
|
102
|
+
function App() {
|
|
103
|
+
return <Counter />;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export default App;
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Step 5: `src/index.js`
|
|
110
|
+
|
|
111
|
+
```jsx
|
|
112
|
+
import React from 'react';
|
|
113
|
+
import ReactDOM from 'react-dom/client';
|
|
114
|
+
import { Provider } from 'react-redux';
|
|
115
|
+
import App from './App';
|
|
116
|
+
import counterStore from './slip13/redux/store';
|
|
117
|
+
|
|
118
|
+
const root = ReactDOM.createRoot(document.getElementById('root'));
|
|
119
|
+
root.render(
|
|
120
|
+
<React.StrictMode>
|
|
121
|
+
<Provider store={counterStore}>
|
|
122
|
+
<App />
|
|
123
|
+
</Provider>
|
|
124
|
+
</React.StrictMode>
|
|
125
|
+
);
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## 5. How it works
|
|
129
|
+
|
|
130
|
+
- `counterSlice.js` starts at 0; each reducer returns the new number.
|
|
131
|
+
- `store.js` registers it under the key `counter`.
|
|
132
|
+
- `index.js` wraps the app in `Provider`.
|
|
133
|
+
- The component reads `state.counter` and logs it on every render.
|
|
134
|
+
|
|
135
|
+
## 6. Run it
|
|
136
|
+
|
|
137
|
+
```powershell
|
|
138
|
+
npm start
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Open the console (F12) and click both buttons.
|
|
142
|
+
|
|
143
|
+
## 7. Errors and fixes
|
|
144
|
+
|
|
145
|
+
| Problem | Fix |
|
|
146
|
+
|---|---|
|
|
147
|
+
| Count does not change | call the action: `dispatch(increment())` |
|
|
148
|
+
| Number reducer does not update | return the new value; `state++` does nothing for a number |
|
|
149
|
+
|
|
150
|
+
## 8. Viva questions
|
|
151
|
+
|
|
152
|
+
**Q. What are the main parts of Redux?**
|
|
153
|
+
Store, actions, reducers and dispatch.
|
|
154
|
+
|
|
155
|
+
**Q. What is a reducer?**
|
|
156
|
+
A function that takes the current state and an action and gives the next state.
|
|
157
|
+
|
|
158
|
+
**Q. Difference between `useSelector` and `useDispatch`?**
|
|
159
|
+
One reads from the store, the other sends actions to it.
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
Solved project: [code/slip13](code/slip13/)
|
fpythons/data/Slip_14.md
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# Slip 14: Calculator
|
|
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 to build a calculator.
|
|
8
|
+
|
|
9
|
+
## 2. Files you will touch
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
slip14/
|
|
13
|
+
src/slip14/Calculator.js
|
|
14
|
+
src/App.js
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 3. Commands (PowerShell, in this order)
|
|
18
|
+
|
|
19
|
+
```powershell
|
|
20
|
+
npx create-react-app slip14
|
|
21
|
+
cd slip14
|
|
22
|
+
mkdir src\slip14
|
|
23
|
+
code .
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 4. Write the code
|
|
27
|
+
|
|
28
|
+
### Step 1: `src/slip14/Calculator.js`
|
|
29
|
+
|
|
30
|
+
```jsx
|
|
31
|
+
import React, { useState } from 'react';
|
|
32
|
+
|
|
33
|
+
const Calculator = () =>{
|
|
34
|
+
|
|
35
|
+
const [firstNumber, setFirstNumber] = useState('');
|
|
36
|
+
const [secondNumber, setSecondNumber] = useState('');
|
|
37
|
+
const [result, setResult] = useState(null);
|
|
38
|
+
|
|
39
|
+
const handleAddition = () =>{
|
|
40
|
+
if(firstNumber.trim() === '' || secondNumber.trim() === '')
|
|
41
|
+
return;
|
|
42
|
+
|
|
43
|
+
let total = 0;
|
|
44
|
+
total = Number(firstNumber) + Number(secondNumber);
|
|
45
|
+
setResult(total);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const handleSubtraction = () =>{
|
|
49
|
+
if(firstNumber.trim() === '' || secondNumber.trim() === '')
|
|
50
|
+
return;
|
|
51
|
+
|
|
52
|
+
let total = 0;
|
|
53
|
+
total = Number(firstNumber) - Number(secondNumber);
|
|
54
|
+
setResult(total);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const handleMultiplication = () =>{
|
|
58
|
+
if(firstNumber.trim() === '' || secondNumber.trim() === '')
|
|
59
|
+
return;
|
|
60
|
+
|
|
61
|
+
let total = 0;
|
|
62
|
+
total = Number(firstNumber) * Number(secondNumber);
|
|
63
|
+
setResult(total);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const handleDivision = () =>{
|
|
67
|
+
if(firstNumber.trim() === '' || secondNumber.trim() === '')
|
|
68
|
+
return;
|
|
69
|
+
|
|
70
|
+
if(Number(secondNumber) === 0){
|
|
71
|
+
alert("Can't divide by zero");
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let total = 0;
|
|
76
|
+
total = Number(firstNumber) / Number(secondNumber);
|
|
77
|
+
setResult(total);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<div>
|
|
82
|
+
<h2>Calculator</h2>
|
|
83
|
+
<input value={firstNumber} onChange={(e) => setFirstNumber(e.target.value)}></input>
|
|
84
|
+
<input value={secondNumber} onChange={(e) => setSecondNumber(e.target.value)}></input>
|
|
85
|
+
<button onClick={handleAddition}>Addition</button>
|
|
86
|
+
<button onClick={handleSubtraction}>Subtraction</button>
|
|
87
|
+
<button onClick={handleMultiplication}>Multiplication</button>
|
|
88
|
+
<button onClick={handleDivision}>Division</button>
|
|
89
|
+
<p>Result: {result}</p>
|
|
90
|
+
</div>
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export default Calculator
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Step 2: `src/App.js`
|
|
98
|
+
|
|
99
|
+
```jsx
|
|
100
|
+
import Calculator from './slip14/Calculator';
|
|
101
|
+
|
|
102
|
+
function App() {
|
|
103
|
+
return <Calculator />;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export default App;
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## 5. How it works
|
|
110
|
+
|
|
111
|
+
- Two state values hold the inputs, `result` holds the answer.
|
|
112
|
+
- Each button has its own handler. Blank inputs are ignored.
|
|
113
|
+
- `Number()` converts the text before calculating.
|
|
114
|
+
- Division by zero shows an alert.
|
|
115
|
+
|
|
116
|
+
## 6. Run it
|
|
117
|
+
|
|
118
|
+
```powershell
|
|
119
|
+
npm start
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Enter 20 and 5 and try all four buttons, then divide by 0.
|
|
123
|
+
|
|
124
|
+
## 7. Errors and fixes
|
|
125
|
+
|
|
126
|
+
| Problem | Fix |
|
|
127
|
+
|---|---|
|
|
128
|
+
| `2 + 3` gives `23` | convert with `Number()` |
|
|
129
|
+
| Result is `NaN` | text that is not a number was entered |
|
|
130
|
+
|
|
131
|
+
## 8. Viva questions
|
|
132
|
+
|
|
133
|
+
**Q. Why is the input value a string?**
|
|
134
|
+
HTML inputs always give text.
|
|
135
|
+
|
|
136
|
+
**Q. What is `NaN`?**
|
|
137
|
+
"Not a Number", the result of an invalid numeric operation.
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
Solved project: [code/slip14](code/slip14/)
|
fpythons/data/Slip_15.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# Slip 15: Counter using useReducer
|
|
2
|
+
|
|
3
|
+
[All slips](README.md) | [Basics: read first](00_START_HERE.md) | [Viva](VIVA.md)
|
|
4
|
+
|
|
5
|
+
## 1. Question
|
|
6
|
+
|
|
7
|
+
Create a simple counter using actions and reducers.
|
|
8
|
+
|
|
9
|
+
## 2. Files you will touch
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
slip15/
|
|
13
|
+
src/slip15/Counter.js
|
|
14
|
+
src/App.js
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 3. Commands (PowerShell, in this order)
|
|
18
|
+
|
|
19
|
+
```powershell
|
|
20
|
+
npx create-react-app slip15
|
|
21
|
+
cd slip15
|
|
22
|
+
mkdir src\slip15
|
|
23
|
+
code .
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 4. Write the code
|
|
27
|
+
|
|
28
|
+
### Step 1: `src/slip15/Counter.js`
|
|
29
|
+
|
|
30
|
+
```jsx
|
|
31
|
+
import React, { useReducer } from 'react'
|
|
32
|
+
|
|
33
|
+
const Counter = () => {
|
|
34
|
+
|
|
35
|
+
function reducer(state, action){
|
|
36
|
+
if(action === 'increment'){
|
|
37
|
+
return state + 1;
|
|
38
|
+
}
|
|
39
|
+
if(action === 'decrement'){
|
|
40
|
+
return state - 1;
|
|
41
|
+
}
|
|
42
|
+
return state;
|
|
43
|
+
}
|
|
44
|
+
const [count, dispatch] = useReducer(reducer, 0);
|
|
45
|
+
return (
|
|
46
|
+
<div>
|
|
47
|
+
<h2>Counter: {count}</h2>
|
|
48
|
+
<button onClick={() => dispatch('increment')}>Increment</button>
|
|
49
|
+
<button onClick={() => dispatch('decrement')}>Decrement</button>
|
|
50
|
+
</div>
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export default Counter
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Step 2: `src/App.js`
|
|
58
|
+
|
|
59
|
+
```jsx
|
|
60
|
+
import Counter from './slip15/Counter';
|
|
61
|
+
|
|
62
|
+
function App() {
|
|
63
|
+
return <Counter />;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export default App;
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## 5. How it works
|
|
70
|
+
|
|
71
|
+
- `reducer(state, action)` returns the new count for `'increment'` or `'decrement'`.
|
|
72
|
+
- `useReducer(reducer, 0)` returns the count and `dispatch`.
|
|
73
|
+
- Clicking a button dispatches an action; React calls the reducer.
|
|
74
|
+
|
|
75
|
+
## 6. Run it
|
|
76
|
+
|
|
77
|
+
```powershell
|
|
78
|
+
npm start
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Click both buttons.
|
|
82
|
+
|
|
83
|
+
## 7. Errors and fixes
|
|
84
|
+
|
|
85
|
+
| Problem | Fix |
|
|
86
|
+
|---|---|
|
|
87
|
+
| Count becomes empty | the reducer must return `state` by default |
|
|
88
|
+
| Nothing happens | the action text must match exactly |
|
|
89
|
+
|
|
90
|
+
## 8. Viva questions
|
|
91
|
+
|
|
92
|
+
**Q. When is `useReducer` better than `useState`?**
|
|
93
|
+
When the state has several related update rules.
|
|
94
|
+
|
|
95
|
+
**Q. Why must a reducer be pure?**
|
|
96
|
+
The same input must give the same output with no side effects.
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
Solved project: [code/slip15](code/slip15/)
|
fpythons/data/Slip_16.md
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# Slip 16: Virtual DOM and traditional DOM
|
|
2
|
+
|
|
3
|
+
[All slips](README.md) | [Basics: read first](00_START_HERE.md) | [Viva](VIVA.md)
|
|
4
|
+
|
|
5
|
+
## 1. Question
|
|
6
|
+
|
|
7
|
+
Demonstrate the concept of the virtual DOM by comparing traditional DOM updates with React rendering through a counter example.
|
|
8
|
+
|
|
9
|
+
## 2. Files you will touch
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
slip16/
|
|
13
|
+
src/slip16/TraditionalDOM.html
|
|
14
|
+
src/slip16/VirtualDOM.js
|
|
15
|
+
src/App.js
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## 3. Commands (PowerShell, in this order)
|
|
19
|
+
|
|
20
|
+
```powershell
|
|
21
|
+
npx create-react-app slip16
|
|
22
|
+
cd slip16
|
|
23
|
+
mkdir src\slip16
|
|
24
|
+
code .
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## 4. Write the code
|
|
28
|
+
|
|
29
|
+
### Step 1: `src/slip16/TraditionalDOM.html`
|
|
30
|
+
|
|
31
|
+
```html
|
|
32
|
+
<!DOCTYPE html>
|
|
33
|
+
<html lang="en">
|
|
34
|
+
<head>
|
|
35
|
+
<meta charset="UTF-8">
|
|
36
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
37
|
+
<title>TraditionalDOM Counter</title>
|
|
38
|
+
</head>
|
|
39
|
+
<body>
|
|
40
|
+
<h2 id="count">0</h2>
|
|
41
|
+
<button onclick="increment()">Increment</button>
|
|
42
|
+
<button onclick="decrement()">Decrement</button>
|
|
43
|
+
<script>
|
|
44
|
+
let count = 0;
|
|
45
|
+
function increment(){
|
|
46
|
+
count++;
|
|
47
|
+
document.getElementById("count").innerHTML = count;
|
|
48
|
+
}
|
|
49
|
+
function decrement(){
|
|
50
|
+
count--;
|
|
51
|
+
document.getElementById("count").innerHTML = count;
|
|
52
|
+
}
|
|
53
|
+
</script>
|
|
54
|
+
</body>
|
|
55
|
+
</html>
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Step 2: `src/slip16/VirtualDOM.js`
|
|
59
|
+
|
|
60
|
+
```jsx
|
|
61
|
+
import React, { useState } from 'react';
|
|
62
|
+
|
|
63
|
+
const VirtualDOM = () =>{
|
|
64
|
+
|
|
65
|
+
const [count, setCount] = useState(0);
|
|
66
|
+
|
|
67
|
+
return(
|
|
68
|
+
<div>
|
|
69
|
+
<h2>VirtualDOM Counter: {count}</h2>
|
|
70
|
+
<button onClick={() => setCount(count + 1)}>Increment</button>
|
|
71
|
+
<button onClick={() => setCount(count - 1)}>Decrement</button>
|
|
72
|
+
</div>
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export default VirtualDOM
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Step 3: `src/App.js`
|
|
80
|
+
|
|
81
|
+
```jsx
|
|
82
|
+
import VirtualDOM from './slip16/VirtualDOM';
|
|
83
|
+
|
|
84
|
+
function App() {
|
|
85
|
+
return <VirtualDOM />;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export default App;
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## 5. How it works
|
|
92
|
+
|
|
93
|
+
- `TraditionalDOM.html`: JavaScript finds the element with `getElementById` and changes `innerHTML` itself.
|
|
94
|
+
- `VirtualDOM.js`: only the state changes. React builds a new virtual tree, compares it with the old one and updates what changed in the real DOM.
|
|
95
|
+
|
|
96
|
+
| Traditional DOM | React (virtual DOM) |
|
|
97
|
+
|---|---|
|
|
98
|
+
| you update the page by hand | you update state, React updates the page |
|
|
99
|
+
| code names each element | code describes the UI |
|
|
100
|
+
| many manual updates are slow to manage | only changed nodes are written |
|
|
101
|
+
|
|
102
|
+
## 6. Run it
|
|
103
|
+
|
|
104
|
+
```powershell
|
|
105
|
+
npm start
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Open `src/slip16/TraditionalDOM.html` in the browser, and run the React app. In DevTools (Elements tab) watch which nodes flash when you click.
|
|
109
|
+
|
|
110
|
+
## 7. Errors and fixes
|
|
111
|
+
|
|
112
|
+
| Problem | Fix |
|
|
113
|
+
|---|---|
|
|
114
|
+
| `increment is not defined` | the function names in `onclick` and the script must match |
|
|
115
|
+
| React count does not change | use `setCount`, not `count++` |
|
|
116
|
+
|
|
117
|
+
## 8. Viva questions
|
|
118
|
+
|
|
119
|
+
**Q. What is the virtual DOM?**
|
|
120
|
+
A lightweight copy of the page structure kept in memory by React.
|
|
121
|
+
|
|
122
|
+
**Q. What is reconciliation?**
|
|
123
|
+
Comparing the new virtual tree with the old one to find the changes.
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
Solved project: [code/slip16](code/slip16/)
|