-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.html
More file actions
538 lines (469 loc) · 19.3 KB
/
Copy pathstate.html
File metadata and controls
538 lines (469 loc) · 19.3 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>React State Management</title>
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"/>
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="reveal.js/css/reveal.css">
<!--<link rel="stylesheet" href="reveal.js/css/theme/white.css" id="theme">-->
<!--<link rel="stylesheet" href="reveal.js/css/theme/black.css" id="theme">-->
<!--<link rel="stylesheet" href="reveal.js/css/theme/night.css" id="theme">-->
<link rel="stylesheet" href="reveal.js/css/theme/simple.css" id="theme">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="reveal.js/lib/css/zenburn.css">
<style>
/*pre code {*/
/*display: block;*/
/*padding: 0.5em;*/
/*background: #FFFFFF !important;*/
/*color: #000000 !important;*/
/*}*/
.right-img {
margin-left: 10px !important;
float: right;
height: 500px;
}
.todo:before {
content: 'TODO: ';
}
.todo {
color: red !important;
}
code span.line-number {
color: lightcoral;
}
.reveal pre code {
max-height: 1000px !important;
}
img {
border: 0 !important;
box-shadow:0 0 0 0 !important;
}
</style>
<!-- Printing and PDF exports -->
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match(/print-pdf/gi) ? 'reveal.js/css/print/pdf.css' : 'reveal.js/css/print/paper.css';
document.getElementsByTagName('head')[0].appendChild(link);
</script>
<!--[if lt IE 9]>
<script src="reveal.js/lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>Bibliotheken für Zustands-Management</h1>
</section>
<section>
<h2>Über Komponenten verteilter Zustand</h2>
<img src="ai/distributed-state.png" />
</section>
<section>
<h2>Render Cycle in Pure React</h2>
<img src="images/redux-01-react-cycle-no-redux.png" style="border:0;box-shadow:0 0 0 0"/>
</section>
<section>
<h2>Motivation für Zustandsmanagement</h2>
<p class="fragment">Zustand, der über viele Komponenten verteilt ist, macht Programme kompliziert</p>
<p class="fragment">Gemeinsame Nutzung von State in unterschiedlichen Komponenten-Hierarchien ist schwierig</p>
<p class="fragment">Zustandmanagement und UI-Handler werden aus React-Komponenten extrahiert</p>
<p class="fragment">Besseres SoC</p>
</section>
<section>
<h1>Option 1: Flux</h1>
<p>Die klassische Idee</p>
</section>
<section>
<img src="ai/flux.png">
</section>
<section>
<h1>Option 2: MobX</h1>
</section>
<section>
<h2>Referenz</h2>
<p><a href="https://mobx.js.org/" target="_blank">
https://mobx.js.org/
</a>
</p>
</section>
<section>
<h2>Prinzipien</h2>
<p><a href="https://mobx.js.org/intro/concepts.html" target="_blank">
https://mobx.js.org/intro/concepts.html
</a>
</p>
<img src="images/mobx-action-state-view.png">
<ol>
<li>Zustand wird extern in einer Klasse gehalten
<li>Abgeleitete Darstellungen des Zustands werden automatisch berechnet, sind getter einer Klasse
<li>Aktionen, die den Zustand ändern, sind Methoden des Klasse
<li>Komponenten werden bei Bedarf automatisch neu gerendert
</ol>
</section>
<section>
<h2>Beispiel-Zustand</h2>
<pre><code class="javascript" contenteditable data-trim>
class Store {
@observable greetings = [];
@observable filter = null;
@computed get filteredGreetings() {
return filterGreetings(this.greetings, this.filter);
}
@action.bound
setFilter(filter) {
if (this.filter === filter) {
// reset filter when clicking again
this.filter = null;
} else {
this.filter = filter;
}
}
}
export default new Store();
</code></pre>
</section>
<section>
<h2>Komponenten reagieren</h2>
<pre class="fragment"><code class="javascript" contenteditable data-trim>
import store from './store';
ReactDOM.render(<GreetingController store={store}/>, mountNode);
</code></pre>
<pre class="fragment"><code class="javascript" contenteditable data-trim>
import {observer} from 'mobx-react';
@observer
export default class GreetingController extends React.Component {
render() {
const {store} = this.props;
const {filteredGreetings} = store;
const {setFilter} = store;
// ...
}
// ...
}
</code></pre>
</section>
<section>
<h3>Zusammenfassung</h3>
<img src="ai/mobx.png" height="600px">
</section>
<section>
<section>
<h2>Übung: Stelle deine Anwendung auf MobX um</h2>
<h3>Nutze die vorbereitete Implementierung</h3>
</section>
<section>
<h3>Schritte</h3>
<ul>
<li>mache dich mit der vorbereiteten und lauffähigen Implementierung in <code>code/experiment/mobx</code> vertraut
<li>integriere sie in deine bestehende Lösung inklusive des Routings
<li>füge in eine neue Komponente hinzu, die zählt, wie viele Grüße es insgesamt gibt und wie viele davon nach der Filterung noch angezeigt werden
</ul>
</section>
</section>
<section>
<h2>Wie kann man Stores noch an Komponenten übergeben?</h2>
<a href="https://github.com/mobxjs/mobx/issues/300#issuecomment-223704275" target="_blank">
https://github.com/mobxjs/mobx/issues/300#issuecomment-223704275</a>
<ol>
<li>Explizit durch props
<li>Direkter Zugriff aus importierten Stores
<li>Implizit über einen Provider (wie bei Redux),
<a target="_blank" href="https://github.com/mobxjs/mobx-react#provider-and-inject">
https://github.com/mobxjs/mobx-react#provider-and-inject
</a>
</ol>
</section>
<section>
<h2>Wie funktioniert MobX?</h2>
<a href="https://www.youtube.com/watch?v=TfxfRkNCnmk" target="_blank">
RuhrJS 2016 - Michel Weststrate - MAGIC MOBX BECOME A REACTIVE WIZARD IN 30 MINUTES
</a>
</section>
<section>
<h1>Option 3: Redux</h1>
</section>
<section>
<h2>Render-Zyklus in purem React</h2>
<img src="images/redux-01-react-cycle-no-redux.png" style="border:0;box-shadow:0 0 0 0"/>
</section>
<section>
<h2>Redux extrahiert die Verantwortlichkeiten</h2>
<img src="images/redux-02-extracting-responsibility.png" style="border:0;box-shadow:0 0 0 0"/>
</section>
<section>
<h2>Resultierende Redux-Architektur</h2>
<img src="images/redux-03-resulting-redux-architecture.png" style="border:0;box-shadow:0 0 0 0"/>
</section>
<section>
<h2>Redux vs MobX</h2>
<p><a href="http://redux.js.org/" target="_blank">
http://redux.js.org/
</a>
</p>
<ul>
<li>Redux ist die Mainstream-Lösung
<li>Lässt sich sehr gut testen
<li>Fehlersuche und Debugging erstaunlich einfach
<li>Funktioniert auch in großen Anwendungen sehr gut
<li>Dev-Tools erlauben Magisches
</ul>
<p>Allerdings: Lernkurve steil, gerade am Anfang wirklich viel Code</p>
<p><strong>Redux ist für kleinere Anwendungen meistens Overkill</strong></p>
<p>MobX erlaubt einen sehr einfachen Einstieg</p>
</section>
<section>
<h3>Event-Handlers werden Action-Creators</h3>
<pre class="fragment"><code class="typescript line-numbers" contenteditable data-trim>
export function setFilter(filter) {
return {
type: SET_FILTER,
filter
};
}
</code></pre>
<ul>
<li class="fragment">Action-Creators erzeugen Action-Objekte</li>
<li class="fragment">Actions sind Kommando-artige Strukturen von Dingen, die die Applikation tun soll
<li class="fragment">Über die dispatch-Methode des Stores werden sie an <em>alle</em> Reducer weiter gegeben
<li class="fragment">Actions bestehen aus einem Typen und einer belibiegen Nutzlast (payload)</li>
</ul>
</section>
<section>
<h3>Action-Creators machen Server-Calls</h3>
<pre class="fragment"><code class="typescript line-numbers" contenteditable data-trim>
export const loadGreetings = dispatch => {
return fetch(BACKEND_URL)
.then(response => response.json())
.then(greetings => dispatch({
type: SET_GREETINGS,
greetings
});
);
};
</code></pre>
<ul>
<li class="fragment">Da der Server-Call asynchron ist, bekommen wir die dispatch Methode als Parameter, mit der wir später
die Action dispatchen</li>
</ul>
<p class="fragment"><strong>Action-Creators sind die einzigen Teile einer Redux-Anwendung, die asynchrone Operationen
ausführen dürfen, wie z.B. eben Server-Calls</strong></p>
</section>
<section>
<h3>Ein <em>einziger</em> Store hält den <em>kompletten</em> Zustand</h3>
<pre class="fragment"><code class="typescript line-numbers" contenteditable data-trim>
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import { rootReducer } from './reducers';
// http://redux.js.org/docs/api/createStore.html
const store = createStore(
rootReducer // reducer
);
ReactDOM.render(
<Provider store={store}>
<GreetingController />
</Provider>,
mountNode
);
</code></pre>
<ul>
<li class="fragment">Zentraler Teil der Anwendung
<li class="fragment">Liefert die bereits bekannte dispatch-Methode
<li class="fragment">Der Store wird allen Componenten über die Wrapper-Komponenten <em>Provider</em>
zur Verfügung gestellt
</ul>
</section>
<section>
<h3>Middleware sitzt zwischen Dispatch und Store</h3>
<pre class="fragment"><code class="typescript line-numbers" contenteditable data-trim>
import { applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
const store = createStore(
rootReducer, // reducer
applyMiddleware(thunk) // middleware as enhancer
);
</code></pre>
<ul>
<li class="fragment">Die Thunk Middleware dient als de-facto-Standard</li>
<li class="fragment">Gibt die dispatch-Methode and (asynchrone) Action Creators weiter
<li class="fragment"><a href="https://github.com/gaearon/redux-thunk#whats-a-thunk" target="_blank">
'A thunk is a function that wraps an expression to delay its evaluation'
</a></li>
<li class="fragment">Middleware ist ein fortgeschrittenes Feature und meistens genügt die Thunk Middleware</li>
</ul>
</section>
<section>
<h3>Reducers enthalten die Kern-(Business)-Logik</h3>
<pre class="fragment"><code class="typescript line-numbers" contenteditable data-trim>
import {combineReducers} from 'redux';
// http://redux.js.org/docs/api/combineReducers.html
export const rootReducer = combineReducers({
greetings, // updates greeting partial state
filter,
mode
});
</code></pre>
<ul>
<li class="fragment">Reducer sind pure Funtionen, die den alten Zustand und eine Action bekommen und einen neuen Zustand erzeugen
<li class="fragment">Oft bearbeitet ein Reducer nur einen Teil des Zustands
</ul>
</section>
<section>
<h3>Teil-Reducer</h3>
<pre class="fragment"><code class="typescript line-numbers" contenteditable data-trim>
const mode = (state = MODE_MASTER, action) => {
switch (action.type) {
case SET_MODE:
return action.mode;
default:
return state;
}
};
</code></pre>
<ul>
<li class="fragment">Initialisiert seinen Teilzustand (oft mit einem Default-Parameter)</li>
<li class="fragment">Ändert niemals zustand direkt</li>
<li class="fragment">Sondern liefert einen neuen Zustand (manchmal teilweise als Kopie)
</ul>
</section>
<section>
<h3>Zustand mit Komponenten verbinden</h3>
<pre class="fragment"><code class="typescript line-numbers" contenteditable data-trim>
import { connect } from 'react-redux';
import * as actions from './actions';
export default connect(
state => ({
mode: state.mode
// ...
}),
actions
)(GreetingController);
</code></pre>
<ul>
<li class="fragment">der Provider gibt den Store in alle Komponenten</li>
<li class="fragment">Die Connect-Funktion extrahiert daraus Zustand, der als Property an Komponenten übergeben wird
<li class="fragment">Action-Creators können ebenso in Komponent gegeben werden</li>
</ul>
</section>
<section>
<h3>Verwendung von Zustand und Action-Creators in verbundenen Komponenten</h3>
<pre class="fragment"><code class="typescript line-numbers" contenteditable data-trim>
class GreetingController extends React.Component {
render() {
// state from store
const {aggregatedGreetings, greetings, mode} = this.props;
// action creators bound to dispatch from store
const {setMode, saveGreeting, setFilter} = this.props;
// ...
}
}
</code></pre>
<ul>
<li class="fragment">Action-Creators und Zustand werden in Properties hinein gemerged
<li class="fragment">Diese werden mit ES6-Destructuring an den Stellen aufgelöst wo wir sie brauchen
<li class="fragment">Die Komponenten wird nur neu gerendert wenn sich der benutzte Zustand verändert</li>
</ul>
</section>
<section>
<h3>Selektoren</h3>
<pre class="fragment"><code class="typescript line-numbers" contenteditable data-trim>
export const filterGreetings = (greetings, filter) =>
filter ? greetings.filter(greeting => greeting.name === filter)
: greetings;
export default connect(
state => ({
greetings: filterGreetings(state.greetings, state.filter),
// ...
}),
// ...
)(GreetingController); </code></pre>
<ul>
<li class="fragment">Berechnen abgeleiteten Zustand</li>
<li class="fragment">Oft in einer eigener Datei</li>
<li class="fragment">Erlauben Wiederbenutzung und Caching (normalerweise
<a href="http://redux.js.org/docs/recipes/ComputingDerivedData.html" target="_blank">reselect</a>)</li>
</ul>
</section>
<section>
<h2>Übung: eine Redux Anwendung fertig stellen</h2>
<h3>Es gibt bereits eine vorgebaute Redux-Anwendung, aber wir die Filterung über das Chart muss noch implementiert werden</h3>
</section>
<section>
<h3>Schritte</h3>
<ul>
<li>Alles Material von <code>code/material/6-redux</code> in deinem Source-Ordner kopieren
<li>TODOs leiten dich, die bereits in den Actions, den Reducers und dem GreetingController eingefügt sind
<li>Nutzen den bereits existierenden Filer-Coder als dem vorherigen Schritt
<li>Erzeuge einen neuen Action-Typ und einen Action-Creator für das setzen des Filters
<li>Füge einen passenden Reducer zur Behandlung einer solchen Action hinzu und füge ihn als Teil-Reducer in
<code>combineReducers</code> hinzu
<li>Reiche den Action-Creator von <code>GreetingController</code> in die Chart-Komponente in der du auf das
Filter-Event reagierst
</ul>
</section>
</div>
</div>
<script src="reveal.js/lib/js/head.min.js"></script>
<script src="reveal.js/js/reveal.js"></script>
<script src="lib/jquery-2.2.4.js"></script>
<script>
Reveal.addEventListener( 'ready', function( event ) {
if (window.location.hostname.indexOf('localhost') !== -1) {
// only applies to presentation version
Reveal.configure({ controls: false });
} else {
// only applies to public version
$('.fragment').removeClass('fragment');
}
// applies to all versions
$('code').addClass('line-numbers');
} );
</script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{
src: 'reveal.js/lib/js/classList.js', condition: function () {
return !document.body.classList;
}
},
{
src: 'reveal.js/plugin/markdown/marked.js', condition: function () {
return !!document.querySelector('[data-markdown]');
}
},
{
src: 'reveal.js/plugin/markdown/markdown.js', condition: function () {
return !!document.querySelector('[data-markdown]');
}
},
{
src: 'reveal.js/plugin/highlight/highlight.js', async: true, condition: function () {
return !!document.querySelector('pre code');
}, callback: function () {
hljs.initHighlightingOnLoad();
}
},
{src: 'reveal.js/plugin/zoom-js/zoom.js', async: true},
{src: 'reveal.js/plugin/notes/notes.js', async: true},
{src: 'lib/js/line-numbers.js'}
]
});
</script>
</body>
</html>