-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathdiff.txt
309 lines (276 loc) · 9.85 KB
/
diff.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
setState is asynchronous - an example!
npm build => What is Bundling?
React Templates Plugin
React - Designing Apps
Formatting Code => visual studio code settings windows
Generic Error Handling
- componentDidCatch
- https://reactjs.org/blog/2017/07/26/error-handling-in-react-16.html
Redux
User Context API
const UserContext = React.createContext()
const UserContextConsumer = UserContext.Consumer
class UserContextProvider extends Component {
state = {
username: '',
isUserLoggedin: false,
userLoggedInSuccessfully: (username) => this.userLoggedInSuccessfully(username),
userLoggedOut: () => this.userLoggedOut()
}
userLoggedInSuccessfully = username => {
this.setState({ username, isUserLoggedin: true })
}
userLoggedOut = () => {
this.setState({ username: '', isUserLoggedin: false })
}
render() {
//console.log(this.props)
return (
<UserContext.Provider value={this.state}>
{this.props.children}
</UserContext.Provider>
)
}
}
<UserContextProvider></UserContextProvider> around the router
static contextType = UserContext;
<UserContextConsumer>
{(context) => (<div><p>{context.username}</p></div>)}
</UserContextConsumer>
# Redux
https://redux.js.org/introduction/getting-started
https://codesandbox.io/s/4z8x75qow9
# Use React Partially - Add React to HTML Page in One Minute
Add React in One Minute
- https://reactjs.org/docs/getting-started.html
- https://reactjs.org/docs/add-react-to-a-website.html
+/**
+ * This is a reducer, a pure function with (state, action) => state signature.
+ * It describes how an action transforms the state into the next state.
+ *
+ * The shape of the state is up to you: it can be a primitive, an array, an object,
+ * or even an Immutable.js data structure. The only important part is that you should
+ * not mutate the state object, but return a new object if the state changes.
+ *
+ * In this example, we use a `switch` statement and strings, but you can use a helper that
+ * follows a different convention (such as function maps) if it makes sense for your
+ * project.
+ */
+function counter(state = 0, action) {
+ switch (action.type) {
+ case 'INCREMENT':
+ return state + 1
+ case 'DECREMENT':
+ return state - 1
+ default:
+ return state
+ }
+}
+
+// Create a Redux store holding the state of your app.
+// Its API is { subscribe, dispatch, getState }.
+let store = createStore(counter)
+
+// You can use subscribe() to update the UI in response to state changes.
+// Normally you'd use a view binding library (e.g. React Redux) rather than subscribe() directly.
+// However it can also be handy to persist the current state in the localStorage.
+
+store.subscribe(() => console.log(store.getState()))
+
+// The only way to mutate the internal state is to dispatch an action.
+// The actions can be serialized, logged or stored and later replayed.
+store.dispatch({ type: 'INCREMENT' })
+// 1
+store.dispatch({ type: 'INCREMENT' })
+// 2
+store.dispatch({ type: 'DECREMENT' })
+// 1
class App extends Component {
render() {
return (
- <div className="App">
- {/*<Counter/>*/}
- <TodoApp />
- </div>
+ <Provider store={store}>
+ <UserContextProvider>
+ <div className="App">
+ <Counter />
+ {/*<TodoApp />*/}
+ </div>
+ </UserContextProvider>
+ </Provider>
);
}
}
@@ -31,4 +80,30 @@ class App extends Component {
// }
// }
+export const UserContext = React.createContext()
+
+export const UserContextConsumer = UserContext.Consumer
+
+export class UserContextProvider extends Component {
+
+ state = {
+ counter: 0,
+ incrementCounter: (by) => this.incrementCounter(by)
+ }
+
+ incrementCounter = (by) => {
+ this.setState({ counter: this.state.counter + by })
+ }
+
+ render() {
+ //console.log(this.props)
+ return (
+ <UserContext.Provider value={this.state}>
+ {this.props.children}
+ </UserContext.Provider>
+ )
+ }
+}
+
+
export default App;
\ No newline at end of file
diff --git a/frontend/todo-app/src/components/counter/Counter.css b/frontend/todo-app/src/components/counter/Counter.css
index 3225523..4f3b58f 100644
--- a/frontend/todo-app/src/components/counter/Counter.css
+++ b/frontend/todo-app/src/components/counter/Counter.css
@@ -1,4 +1,4 @@
-/*
+
button {
background-color: green;
font-size : 16px;
@@ -17,6 +17,7 @@ button {
width : 200px;
}
+/*
body {
padding : 15px 30px;
}
diff --git a/frontend/todo-app/src/components/counter/Counter.jsx b/frontend/todo-app/src/components/counter/Counter.jsx
index 34a28ae..2c98db2 100644
--- a/frontend/todo-app/src/components/counter/Counter.jsx
+++ b/frontend/todo-app/src/components/counter/Counter.jsx
@@ -1,9 +1,12 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import './Counter.css'
+import { UserContextConsumer, UserContext } from '../../App'
class Counter extends Component {
+ static contextType = UserContext;
+
constructor() {
super(); //Error 1
@@ -18,14 +21,20 @@ class Counter extends Component {
}
render() {
+ console.log('render called ' + this.context.counter)
+
return (
- <div className="counter">
- <CounterButton by={1} incrementMethod={this.increment} decrementMethod={this.decrement} />
- <CounterButton by={5} incrementMethod={this.increment} decrementMethod={this.decrement} />
- <CounterButton by={10} incrementMethod={this.increment} decrementMethod={this.decrement} />
- <span className="count">{this.state.counter}</span>
- <div><button className="reset" onClick={this.reset}>Reset</button></div>
- </div>
+ <UserContextConsumer>
+ {(context) => (
+ <div className="counter">
+ <CounterButton by={1} incrementMethod={this.increment} decrementMethod={this.decrement} />
+ <CounterButton by={5} incrementMethod={this.increment} decrementMethod={this.decrement} />
+ <CounterButton by={10} incrementMethod={this.increment} decrementMethod={this.decrement} />
+ <span className="count">{context.counter}</span>
+ <div><button className="reset" onClick={this.reset}>Reset</button></div>
+ </div>
+ )}
+ </UserContextConsumer>
);
}
@@ -35,11 +44,12 @@ class Counter extends Component {
increment(by) {
//console.log(`increment from child - ${by}`)
- this.setState(
- (prevState) => {
- return { counter: prevState.counter + by }
- }
- );
+ for (let i = 1; i <= 100; i++)
+ this.setState(
+ (prevState) => {
+ return { counter: prevState.counter + by }
+ }
+ );
}
decrement(by) {
@@ -67,16 +77,26 @@ class CounterButton extends Component {
// this.decrement = this.decrement.bind(this);
//}
+ static contextType = UserContext;
+
render() {
+
+
//render = () => {
//const style = {fontSize : "50px", padding : "15px 30px"};
return (
- <div className="counter">
- <button onClick={() => this.props.incrementMethod(this.props.by)} >+{this.props.by}</button>
- <button onClick={() => this.props.decrementMethod(this.props.by)} >-{this.props.by}</button>
- {/*<span className="count"
- >{this.state.counter}</span>*/}
- </div>
+ <UserContextConsumer>
+ {(context) => (
+ <div className="counter">
+ <button onClick={() => context.incrementCounter(this.props.by)} >+{this.props.by}</button>
+ <button onClick={() => this.props.decrementMethod(this.props.by)} >-{this.props.by}</button>
+ {context.counter}
+ {/*<span className="count"
+ >{this.state.counter}</span>*/}
+ </div>
+
+ )}
+ </UserContextConsumer >
)
}
@@ -107,4 +127,6 @@ CounterButton.propTypes = {
by: PropTypes.number
}
+
+
export default Counter
\ No newline at end of file
diff --git a/frontend/todo-app/src/components/todo/ListTodosComponent.jsx b/frontend/todo-app/src/components/todo/ListTodosComponent.jsx
index 4f4f894..b6db735 100644
--- a/frontend/todo-app/src/components/todo/ListTodosComponent.jsx
+++ b/frontend/todo-app/src/components/todo/ListTodosComponent.jsx
@@ -101,8 +101,9 @@ class ListTodosComponent extends Component {
todo =>
<tr key={todo.id}>
<td>{todo.description}</td>
- <td>{todo.done.toString()}</td>
<td>{moment(todo.targetDate).format('YYYY-MM-DD')}</td>
+ <td>{todo.done.toString()}</td>
+
<td><button className="btn btn-success" onClick={() => this.updateTodoClicked(todo.id)}>Update</button></td>
<td><button className="btn btn-warning" onClick={() => this.deleteTodoClicked(todo.id)}>Delete</button></td>
</tr>
const mapStateToProps = (state) => {
return {
counter: state
}
};
const mapDispatchToProps = dispatch => ({
increment: by => dispatch({ type: 'INCREMENT', by }),
decrement: by => dispatch({ type: 'DECREMENT', by }),
reset: by => dispatch({ type: 'RESET' }),
});
export default connect(mapStateToProps, mapDispatchToProps)(Counter)
// export default Counter
this.props.increment(by);
this.props.decrement(by);