-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2017_ix.html
More file actions
executable file
·2018 lines (1805 loc) · 75.4 KB
/
Copy path2017_ix.html
File metadata and controls
executable file
·2018 lines (1805 loc) · 75.4 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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>React Workshop</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;
}
.reveal section img {
border:0;
box-shadow:0 0 0 0
}
html.exkurs body {
background: rgb(252, 220, 185) url("ai/es6-icon.png") no-repeat right bottom;
}
</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">
<!--
Titel:
Einführung in die Philosophie von React
Abstract:
Dieser Workshop führt dich in die Programmierung und Philosophie von
React ein. Dabei wirst du sehen, wie React mit vielen bisher
bestehenden Best-Practices bricht und warum das so ist. Wir werden uns
daher viel Zeit für individuelle Fragestellungen und Übungen nehmen,
um dir diese Grundlagen auch nachhaltig zu vermitteln.
Am Ende es Workshops hast du eine Vorstellung davon, wie
React-Anwendungen aufgebaut sind und wie du eine neue React-Anwendung
beginnst. Du kannst ebenso einschätzen, was die Stärken und Schwächen
von React sind.
Inhaltlich wirst du lernen, React-Komponenten zu entwickeln und daraus
eine ganze Anwendung zu bauen. Dazu sehen wir uns einen typischen
Entwicklungsprozess mit Webpack und Babel an. Außerdem zeigen wir dir, wie du
Datenzugriffe auf einen Server machen und 3rd-Party-Bibliotheken in deine
Anwendung integrieren kannst. Zur Strukturierung deiner Anwendung
und der Trennung von UI und Geschäftlogik sehen wir uns das Framework "Redux"
an, das häufig gemeinsam mit React
eingesetzt wird.
Mit einem Ausblick auf das React-Ökosystem schließen wir den Workshop ab,
so dass du Orientierung und Ideen für weitere Themen bekommst.
Zielgruppe
Du hast bereits grundlegende Erfahrungen mit JavaScript, am besten mit
der Version ES2015. Solltest du ES2015 noch nicht kennen, keine Angst,
wir beginnen den Workshop mit einer Einführung/Wiederholung der
Sprach-Features, die du für die React-Programmierung auch wirklich
brauchst. Wir setzen keine Erfahrung mit React voraus und beginnen
tatsächlich bei Null.
Technische Voraussetzungen der Teilnehmer
Du brauchst ein Laptop, auf dem du Software installieren kannst und
das den Anforderungen moderner Software-Entwicklung gewachsen ist.
Bitte installiere vor dem Workshop Node.js (https://nodejs.org), git
(https://git-scm.com/downloads) und eine IDE. Wir empfehlen entweder
Visual Studio Code (kostenlos, https://code.visualstudio.com/), oder Webstorm
(https://www.jetbrains.com/webstorm/specials/webstorm/webstorm.html)
bzw. IDEA (https://www.jetbrains.com/idea/). Als Browser empfehlen wir
Chrome, es tut aber auch Firefox, Safari oder der aktuelle
Edge-Browser von Microsoft.
Mögliche Agenda (grob, ohne Pausen):
- Teil 1 (3 Stunden)
- Einführung in React
- Build-Prozess
- ES6-Grundlagen
- React Komponenten
- Komponentenhierarchien
- Teil 2 (2 Stunden) Serverzugriffe
- Die fetch-Bibliothek
- ES6 Promises
- Lebenszyklus von React Komponenten
- Teil 3 (2 Stunden) Integration mit 3rd-Party-Apps
- Referenzen in React
- Zugriff von React auf 3rd-Party Lib
- Zugriff von Lib auf React-Komponente
- Teil 4 (3 Stunden): Redux
- Motivation: Trennung von UI und Logik, nachvollziehbarerer Datenfluss etc
- (Einführung Flux?)
- Einführung Redux / Redux im Überblick
- Action, Action Creator, Reducer
- Verbinden von Komponenten
- Store (mehrere Reducer)
- Middleware / Asynchrone Aufrufe mit thunk
- Teil 5 (1 Stunde): Performance Optimierung
- Immutable / shouldComponentUpdate (Immutable ggf schon vorher)
- Prod-Build
- Teil 6 (1 Stunde)
- Ausblick / Überblick über weiteres React Öko-System
Zeitplan:
Tag 1:
ab 10 Uhr Registrierung
11 Uhr Beginn Workshop
12.30 - 13.30 Uhr Mittagessen
13 - 14 Uhr Mittagessen
15.30 - 15.45 Uhr: 1. Kaffeepause
17.15 - 17.30 Uhr: 2 Kaffeepause
Ende gegen 18.30 Uhr
Tag 2:
9 Uhr Beginn
10.45 - 11.00 Uhr: Kaffeepause
12.30 - 13.30 Uhr: Mittagessen
15.00 - 15.15 Uhr: Kaffeepause
Ende gegen 16.30 Uhr
-->
<!--
TODO:
- Optionaler Schritt: GraphQL als optionaler Server-Teil
- Async Await Demo, 3b-remote-async-await
- Was kommt in 16? https://twitter.com/reactjs/status/866329614796431360?s=03
-->
<!--http://dpunkt.de/lecture.php?event_id=3536-->
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h3>React Workshop - Vorbereitung</h3>
<p>Falls noch nicht gemacht:</p>
<ol style="font-size: 85%">
<li><code>git clone https://github.com/DJCordhose/react-workshop.git</code></li>
<li>Im geklonten Verzeichnis: <code>npm install</code>
<br><span style="font-size: 85%">(Dafür evtl. einen Proxy konfigurieren <a target="_blank" href="http://wil.boayue.com/blog/2013/06/14/using-npm-behind-a-proxy/">http://wil.boayue.com/blog/2013/06/14/using-npm-behind-a-proxy/</a>)</span>
<li><code>npm start</code></li>
<li>In neuem Terminal: in <code>code/workspace</code> wechseln</li>
<li>Ausführen: <code>npm start</code></li>
<li>Browser öffnen: <a href="http://localhost:8080" target="_blank">http://localhost:8080</a></li>
<li>Wenn dort ein Hello-World-Text erscheint ist alles gut
</ol>
<p>Folien: Im geklonten Verzeichnis <code>2017_ix.html</code>
</p>
</section>
<section>
<h1>React Workshop</h1>
<div style="float:left">
<p>
<a href="http://nilshartmann.net" target="_blank">Nils Hartmann</a> / <a href="http://twitter.com/nilshartmann" target="_blank">@nilshartmann</a>
</p>
<p>
<a href="http://zeigermann.eu" target="_blank">Oliver Zeigermann</a> / <a href="http://twitter.com/djcordhose" target="_blank">@DJCordhose</a>
</p>
<p><a href="http://bit.ly/react-ix">http://bit.ly/react-ix</a></p>
<p style="border-top: 2px solid darkgray;margin-top:25px;padding-top:25px;">
<a href="https://reactbuch.de" target="_blank">React Buch (https://reactbuch.de)</a></p>
</p>
</div>
<div style="float:right">
<img style="max-height:350px" src="images/reactbuch-de.png" />
<p>
</div>
</section>
<section>
<h2>Inhalt</h2>
<ul>
<li><a href="#/t0">Teil 0: React-Einführung und Build-Prozess</a>
<li><a href="#/t1">Teil I: React-Komponenten und ES6 Grundlagen</a>
<li><a href="#/t2">Teil II: Komponentenhierarchien</a>
<li><a href="#/t3">Teil III: Remote-Calls gegen Server</a>
<li><a href="#/t4">Teil IV: Integration mit 3rd-Party Libs</a>
<li><a href="#/t5">Teil V: Zustandsmanagement (Redux)</a>
<li><a href="#/t6">Teil VI: Performance</a>
</li>
</ul>
</section>
<section id="example-app">
<h2>Beispiel-Anwendung</h2>
<a target="_blank" href="code/schritte/redux/7-redux-complete-app/public/index.html"><img src="images/greeting-app.png" style="border:0;box-shadow:0 0 0 0;max-width: 80%"/></a>
</section>
<section id="t0">
<h1>Teil 0</h1>
<h2>React-Einführung und Build-Prozess</h2>
</section>
<section id="react">
<h2>React</h2>
<ul>
<li class="fragment">Framework für Facebook und viele andere
<li class="fragment">Komponenten kapseln Template und Logik
<li class="fragment">Deklarativ
<li class="fragment">Abstraktion vom DOM: <em>f(model) -> UI</em>
<li class="fragment">Minimales API
<li class="fragment">Ein-Weg-Data-Binding: Zustandsänderungen stellen Komponente <b>komplett</b> neu dar
<li class="fragment">Kann im Browser und auf dem Server rendern
<li class="fragment">wird für komplette Anwendung typischerweise mit React Router und einem Flux-Framework gekoppelt
<li class="fragment">viel Inspiration aus der funktionalen Ecke (Immutable, pure functions, stateless)
</ul>
</section>
<section>
<h3>React Komponenten</h3>
<ul>
<li class="fragment">Werden als ES6 Klasse oder Funktion implementiert
<li class="fragment">Keine Templatesprache (stattdessen JavaScript)
<li class="fragment">Templates können HTML-artige Syntax enthalten (JSX)
</ul>
<pre class="fragment"><code data-trim contenteditable>class HelloMessage extends React.Component {
render() {
return <h1 className='title'>Hello, World!</h1>
}
}
</code></pre>
</section>
<section>
<h3>Eine erste Komponente: Hello, World!</h3>
<a target="_blank" href="code/schritte/0-hello_world/public/index.html">Demo</a>
</section>
<section data-state="exkurs">
<h2>ES6-Referenz</h2>
<p>ES6 Features werden vorgestellt, wo wir sie brauchen</p>
<p><a href="http://exploringjs.com/es6/" target="_blank">
http://exploringjs.com/es6/</a>
</section>
<section data-state="exkurs">
<h3>ES6: Template Strings</h3>
<p>Template Strings werden in Backticks (``) geschrieben und können Ausdrücke
(in <code>${}</code>) enthalten:</p>
<pre class="fragment"><code class="javascript" contenteditable>const name = "Susi";
const greeting = `Hello, ${name}`; // Hello, Susi
const four = `Two and two is: ${2+2}` // Two and two is: 4
const time = `The time is: ${new Date()}`); // The time is: ...
</code></pre>
</section>
<section data-state="exkurs">
<h3>ES6: Klassen</h3>
<pre><code class="javascript" contenteditable>class Person {
constructor(name) {
this._name = name;
}
get name() {
return this._name;
}
}
class Programmer extends Person {
constructor(name, language) {
super(name);
this.language = language;
}
code() {
return `${this.name} codes in ${this.language}`;
}
}</code></pre>
<pre class="fragment"><code class="javascript" contenteditable>const programmer = new Programmer('Erna', 'JavaScript');
console.log(programmer.code());
console.log(programmer instanceof Programmer); // true
console.log(programmer instanceof Person); // true</code></pre>
</section>
<section data-state="exkurs">
<h3>ES6: Arrow Functions</h3>
<pre><code class="javascript" contenteditable>const displayInPage = (text) => {
return document.body.innerHTML +=
`${text}<br>`;
};</code></pre>
<pre class="fragment"><code class="javascript" contenteditable>// Klammern können weggelassen werden, genau ein Parameter
// ebenso die geschweiften Klassen, wenn nur ein Statement:
const displayInPage = text => document.body.innerHTML += `${text}<br>`;
</code></pre>
</section>
<section>
<h3>Hello World React</h3>
<pre><code data-trim contenteditable>class HelloMessage extends React.Component {
render() {
return (<div>
<input onChange={event => this.updateModel(event)}
value={this.state.greeting} />
<p>{this.state.greeting}, World</p>
<button
onClick={() => this.reset()}>
Clear
</button>
</div>);
}</code></pre>
<pre class="fragment"><code contenteditable class="javascript"> constructor(props) {
super(props);
this.state = {greeting: this.props.greeting};
}
updateModel(event) {
this.setState({greeting: event.target.value});
}
reset() {
this.setState({greeting: ""});
}
}</code></pre>
</section>
<section>
<h3>Aufruf</h3>
<pre><code data-trim contenteditable>
// index.html
<html>
<body>
<div id="mount"></div>
</body>
<script src="dist/main.js"></script>
</html>
</code></pre>
<pre class="fragment"><code data-trim contenteditable class="javascript">
// main.js
import React from 'react';
import ReactDOM from 'react-dom';
import HelloMessage from './HelloMessage';
const mountNode = document.getElementById('mount');
ReactDOM.render(<HelloMessage greeting="Hello"/>, mountNode);
</code></pre>
<a target="_blank" href="code/hello_world/public/index.html">Run</a>
</section>
<section>
<h2>Buildprozess</h2>
<ul>
<li>ES6- und JSX-Code muss nach ECMAScript 5 übersetzt werden
<li>Typische Werkzeuge
<ul><li>Babel (Compiler)
<li>Webpack (Bundler)
<li>Webpack Dev Sever (HTTP Server mit Hot Reload)
</ul>
</ul>
</section>
<section>
<h2>Webpack und Babel</h2>
<img src="images/webpack-und-babel.png" style="border:0;box-shadow:0 0 0 0"/>
</section>
<section>
<h2>Webpack Development Server</h2>
<ul>
<li>Zum Ausführen der Anwendung wird ein Webserver benötigt</li>
<li class="fragment">Für die Entwicklung <b>Webpack Dev Server</b>:
<ul>
<li>...führt Webpack auf Basis der Projekt Webpack Konfiguration aus</li>
<li>...erzeugt Ausgabe nur in Memory (nicht auf Platte)</li>
<li>...Hot Reloading: Automatische Aktualisierung nach Code Änderung</li>
<li>...React Hot Loader: Zustand bleibt nach Aktualisierung erhalten (Beta)</li>
</ul>
<li class="fragment"><code>npm start</code> in unserem Beispiel-Projekt</li>
</ul>
</section>
<section>
<section>
<h2>Übung: React Hello-World</h2>
<h3>Mach dich mit den Werkzeugen vertraut und schreib deine erste React-Komponente</h3>
</section>
<section>
<h3>Schritt #1: Tool-Chain starten (falls noch nicht gemacht)</h3>
<p>Nutze für die Übungen das <code>workspace</code>-Verzeichnis in diesem Repository. Hier ist eine Tool-Chain mit Webpack und Babel vorbereitet</p>
<ol style="font-size: 85%">
<li><code>npm install</code> (auf oberster Ebene)
<br><span style="font-size: 85%">(Dafür evtl. einen Proxy konfigurieren <a target="_blank" href="http://wil.boayue.com/blog/2013/06/14/using-npm-behind-a-proxy/">http://wil.boayue.com/blog/2013/06/14/using-npm-behind-a-proxy/</a>)</span>
<li><code>cd code/workspace</code>
<li><code>npm start</code>
<li>Öffne <a href="http://localhost:8080" target="_blank">http://localhost:8080</a> im Browser
<li>Wenn auf der Seite 'Hello, World' steht, ist alles gut
</ol>
</section>
<section>
<h3>Schritt #2: Deine erste React-Komponente</h3>
<ul>
<li>Ersetze die "statische" Komponente (HelloMessage) mit der React-Komponente aus dem vorherigen Beispiel aus den Folien
<li>Experimentiere mit der Anwendung, mache einige Änderungen, wie z.B.
<ul>
<li>Alle Eingaben sollen in Großbuchstaben auftauchen
<li>Gib irgendeine Rückmeldung wenn die Eingabe erfolgreich gelöscht wurde
</ul>
</li>
</ul>
</section>
</section>
<section id="t1">
<h1>Teil I</h1>
<h2>React-Komponenten und ES6 Grundlagen</h2>
<a href="code/schritte/1-detail/public/index.html" target="_blank">Ziel-Anwendung</a>
</section>
<section>
<h3>Themen</h3>
<ul>
<li>Rendering
<li>Properties und Zustand
<li>Refs
</ul>
</section>
<section>
<h3>React: Rendering</h3>
<em>Jede React-Komponente braucht eine <code>render</code>-Methode:</em>
<ul>
<li class="fragment">wird aufgerufen beim ersten Rendering und immer wenn sich der Zustand ändert</li>
<li class="fragment">kann HTML-artige <b>JSX</b>-Syntax nutzen</li>
<li class="fragment">JSX kann pures JavaScript in geschweiften Klammern enthalten (<code>{}</code>)
</ul>
<pre class="fragment"><code class="jsx" contenteditable data-trim>
class GreetingDetail extends React.Component {
render() {
return (
<div>
<input
onChange={event => this.updateModel(event.target.value)}
value={this.state.greeting} />
<p>{this.state.greeting}, World</p>
</div>
);
}
// ...
}
</code></pre>
</section>
<section>
<h3>React: JSX</h3>
<ul>
<li style="font-size: 80%" class="fragment">Wird wie HTML hingeschrieben, inkl Attribute: <pre><code class="xml" contenteditable data-trim>
<div><input type="text"/></div>
</code></pre></li>
<li style="font-size: 80%" class="fragment">Attribute, die keine Strings sind, müssen in {} eingechlossen werden:
<pre><code class="xml" contenteditable data-trim>
<Counter label="Count" count={7} showValues={true} />
</code></pre>
</li>
<li style="font-size: 80%" class="fragment">Kann pures JavaScript enthalten, eingeschlossen in {}:
<pre><code class="xml" contenteditable data-trim>
const title = 'Hello, World';
<h1>{title.toUpperCase()}</h1>
</code></pre>
</li>
<li style="font-size: 80%" class="fragment"><code>class</code>-Attribut heißt <code>className</code>:
<pre><code class="xml" contenteditable data-trim>
<h1 className="title">...</h1>
</code></pre>
</li>
<li style="font-size: 80%" class="fragment">CSS-Eigenschaften werden als Objekt übergeben in Camel-Case-Notation:
<pre><code class="xml" contenteditable data-trim>
const styles = { marginLeft: '10px', border: '1px solid red' };
<h1 style={styles}>...</h1>
</code></pre>
</li>
</ul>
</section>
<section>
<h3>React: Rendering #2</h3>
<ul class="fragment">
<li>DOM-Events werden in React-Events verpackt
<li>React-Events haben weitgehend selbe API wie DOM-Events
<li>Es gibt keine automatische Bindung an Modelle
</ul>
<pre class="fragment"><code class="xml" contenteditable data-trim>
class GreetingDetail extends React.Component {
render() {
return (
<div>
<input onChange={event => this.updateModel(event.target.value)}
value={this.state.greeting} />
<p>{this.state.greeting}, World</p>
</div>
);
}
updateModel(greeting) {
this.setState({greeting});
}
// ...
}
</code></pre>
</section>
<section data-state="exkurs">
<h3>ES6: Erweiterte Objekt-Literale</h3>
<pre><code class="javascript" contenteditable>const name = 'Oma';
const person = {
// ES5: name: name
name
};
console.log(person.name); // Oma
</code></pre>
</section>
<section data-state="exkurs">
<h3>ES6: Computed Properties</h3>
<em>Berechnete Keys in Objekten</em>
<pre><code class="javascript" contenteditable>const schluessel = 'name';
const person = {
[schluessel]: 'Klaus'
};
console.log(person.name); // Klaus
let id = 1;
const personen = {
[`id_${id++}`]: 'Klaus',
[`id_${id++}`]: 'Susi'
};
console.log(personen); // Object {id_1: "Klaus", id_2: "Susi"}
</code></pre>
</section>
<section data-state="exkurs">
<h2>ES6: Module, Importe und Exporte</h2>
</section>
<section data-state="exkurs">
<h3>ES6: Export einer einzigen Klasse</h3>
<pre><code class="javascript" contenteditable>// Person.js
class Person {
// ...
}
export default Person;
</code></pre>
<pre><code class="javascript" contenteditable>// Person.js
// in einer Zeile zusammengefasst
export default class Person {
// ...
}
</code></pre>
</section>
<section data-state="exkurs">
<h3>ES6: Import</h3>
<pre><code class="javascript" contenteditable>// Programmer.js
import Person from './Person';
export default class Programmer extends Person {
// ...
}
</code></pre>
</section>
<section data-state="exkurs">
<h3>ES6: Benannte Exporte</h3>
<pre><code class="javascript" contenteditable data-trim>// util.js
export function displayInPage(text) {
document.body.innerHTML +=
`${text}<br>` ;
}
export showInfo = msg => window.alert(`Wichtige Info: ${msg}`);
// or
function displayInPage(text) { . . . }
const showInfo = ...;
export { displayInPage, showInfo };
</code></pre>
<pre class="fragment"><code class="javascript" contenteditable data-trim>import {displayInPage} from "./util";
displayInPage('Hello, World');
</code></pre>
<!--<pre class="fragment"><code class="javascript" contenteditable data-trim>import {displayInPage as display} from "./util";-->
<!--display('Hello, World');-->
<!--</code></pre>-->
<!--<pre class="fragment"><code class="javascript" contenteditable data-trim>import * as util from "./util";-->
<!--util.displayInPage('Hello, World');-->
<!--</code></pre>-->
</section>
<!--<section>-->
<!--<h2>Neuerungen und Verbesserungen in ES6</h2>-->
<!--</section>-->
<!--<section>-->
<!--<h3>Tagged Template Literals</h3>-->
<!--<p><a target="_blank" href="http://exploringjs.com/es6/ch_template-literals.html#_tagged-templates">-->
<!--Mini-DSLs: Template-Literals mit tag-->
<!--</a>-->
<!--</p>-->
<!--<pre class="fragment"><code class="javascript" contenteditable>function tag(strings, ...values) {-->
<!--console.log(strings);-->
<!--//[ '', ' codes in ', '.' ]-->
<!--console.log(values);-->
<!--//[ 'Oma', 'Haskell' ]-->
<!--return 'whatever you want';-->
<!--}-->
<!--</code></pre>-->
<!--<pre><code class="javascript" contenteditable>const expanded = tag`${person} codes in ${language}.`;-->
<!--console.log(expanded);-->
<!--//whatever you want-->
<!--</code></pre>-->
<!--</section>-->
<section data-state="exkurs">
<h3>ES6: Destructuring von Objekten</h3>
<pre><code class="javascript" contenteditable>const person = {
name: 'Olli',
email: 'oliver.zeigermann@gmail.com'
};</code></pre>
<pre class="fragment"><code class="javascript" contenteditable>const {name, notThere} = person;
console.log(`name=${name}`);
// name=Olli
console.log(`notThere=${notThere}`);
// notThere=undefined
</code></pre>
<pre class="fragment"><code class="javascript" contenteditable>function someFunction({name, notThere}) {
console.log(`name=${name}`);
// name=Olli
console.log(`notThere=${notThere}`);
// notThere=undefined
}
someFunction(person);
</code></pre>
</section>
<section>
<h3>React: Properties und Zustand</h3>
<ul class="fragment">
<li><b>Properties</b> werden der Komponente von <b>außen</b> übergeben (und nicht verändert)</li>
<li><b>Zustand (State)</b> ist eine <b>innere</b> Eigenschaft der Komponente (die verändert werden kann)</li>
<li>Beides sind Objekte mit Key-Value-Paaren</li>
<li>Beide können an Unterkomponenten übergeben werden</li>
</ul>
</section>
<section>
<h3>Properties einer Komponente</h3>
<ul class="fragment">
<li>Properties werden von außen über den Konstruktor übergeben</li>
<li>Properties dürfen nicht verändert werden</li>
<li>Zugriff über <code>this.props</code></li>
<li><code>this.props.children</code> enthält Kind-Elemente</li>
</ul>
<pre class="fragment"><code class="javascript" contenteditable data-trim>
class TitleComponent extends React.Component {
constructor(props) {
super(props);
}
render() {
return <h1>{this.props.title}</h1>
}
// ...
}
</code></pre>
<pre class="fragment"><code class="javascript" contenteditable data-trim>
<TitleComponent title='Hello World' />
</code></pre>
</section>
<section>
<h3>Zustand einer Komponente</h3>
<ul class="fragment">
<li>Beispiel: Inhalt eines Eingabefelds, Daten vom Server</li>
<li>Werte üblicherweise immutable</li>
<li><b>Initialisieren</b> im Konstruktor mit <code>this.state={}</code>
<li>Zustand <b>lesen</b> über <code>this.state</code>
<li>Zustand <b>setzen</b> über <code>this.setState()</code>
<ul><li><b>Achtung:</b> kein "reiner" Setter</li>
<li>Führt alten und neuen Zustand zusammen</li>
<li>Wird asynchron ausgeführt!</li>
<li><b>Löst erneutes rendern der <i>gesamten</i> Komponente aus</b></li>
</ul>
</ul>
</section>
<section>
<h3>Beispiel: Zustand einer Komponente</h3>
<pre><code class="javascript" contenteditable data-trim>
class GreetingDetail extends React.Component {
constructor(props) {
super(props);
this.state = { name: 'Klaus' };
}
updateModel(event) {
// Zustand ändern: Komponente wird neu gerendert
this.setState({name: event.target.value});
}
render() {
return <input name="name"
value={this.state.name}
onChange={e => this.updateModel(e)} />
}
// ...
}
</code></pre>
</section>
<section>
<h3>Render Zyklus</h3>
<img src="images/event-zustand-render.png" height="550" style="border:0;box-shadow:0 0 0 0">
</section>
<section>
<h3>Virtual DOM</h3>
<img src="images/virtual-dom.png" style="border:0;box-shadow:0 0 0 0">
</section>
<section>
<h3>React: Referenzen auf nativen DOM</h3>
<em>Elementen kann eine Callback-Funktion <code>ref</code> übergeben werden</em>
<ul>
<li class="fragment">Funktion wird nach dem Rendern mit Referenz auf DOM Element aufgerufen (oder <code>null</code>)
<li class="fragment">Diese Referenz kann man z.B. als Member-Variable speichern
</ul>
<pre class="fragment"><code class="javascript" contenteditable data-trim>
class HelloMessage extends React.Component {
render() {
return (
<div>
<input ref={inputNode => this.inputNode = inputNode} />
<button
onClick={() => this.inputNode.focus()}>
Focus
</button>
</div>);
}
}
</code></pre>
</section>
<section>
<section>
<h2>Übung: Detail-Ansicht für unsere Gruß-Anwendung</h2>
<em>Erzeuge eine Komponenten mit der man einen Namen und eine Grußformel eingeben kann</em>
</section>
<section>
<h3>Schritte</h3>
<h4>Erweitere deine erste React-Komponente im workspace-Ordner</h4>
<ul>
<li>benenne deine Komponente in <code>GreetingDetail</code> um
<li>du brauchst zwei Eingabefelder, die <code>name</code> und <code>greeting</code> im Zustand der Komponente setzen
<li>Kannst du die Komponente so bauen, dass du mit einer einzigen update-Methode beide Felder im Zustand updaten kannst?
<li>Erweitere deine Komponente so, dass man von außen Properties übergeben kann, die den Zustand initialisieren
</ul>
</section>
</section>
<section id="t2">
<h1>Teil II</h1>
<h2>Komponentenhierarchien</h2>
<em>Komplette Anwendung aus Komponenten bauen</em>
<p><a href="code/schritte/2-hierarchy/public/index.html" target="_blank">Ziel-Anwendung</a></p>
</section>
<section>
<h2>Thinking in React</h2>
<p><a href="https://facebook.github.io/react/docs/thinking-in-react.html" target="_blank">
https://facebook.github.io/react/docs/thinking-in-react.html
</a>
</p>
</section>
<section>
<h3>Themen</h3>
<ul>
<li class="fragment">Komponenten als Funktion
<li class="fragment">Darstellung von Listen und Keys
<li class="fragment">Architektur-Idee: Controller-Komponente und View-Komponente
<li class="fragment">Durchreichen von Zustand und Callbacks
<li class="fragment">PropTypes
</ul>
</section>
<section>
<h3>Komponenten als Funktion</h3>
<em>Komponenten können auch als reguläre JavaScript Funktion geschrieben werden</em>
<ul>
<li class="fragment">entspricht der <code>render</code>-Methode</li>
<li class="fragment">Properties werden per Parameter in einem Objekt übergeben</li>
<li class="fragment">Zurzeit nur ohne Zustand und ohne Lifecycle-Methoden</li>
<li class="fragment">Empfehlung: Funktionen statt Klassen verwenden (wenn möglich)</li>
</ul>
</section>
<section data-transition="none">
<h3>Komponenten als Funktion</h3><pre class="fragment"><code class="javascript" contenteditable data-trim>
import React from 'react';
function Greet(props) {
return (
<div>
<h1>{props.greeting}</h1>
<h2>{props.name}</h2>
</div>
);
}
// Verwendung:
<Greet name="Susi" greeting="Hello" />
</code></pre></section>
<section data-transition="none">
<h3>Komponenten als Funktion</h3>
<pre class="fragment"><code class="javascript" contenteditable data-trim>
// Mit Destructuring
function Greet({greeting, name}) {
return (
<div>
<h1>{greeting}</h1>
<h2>{name}</h2>
</div>
);
}
</code></pre>
<pre class="fragment"><code class="javascript" contenteditable data-trim>
// Als Arrow Function
const Greet = ({greeting, name}) => <div>
<h1>{greeting}</h1>
<h2>{name}</h2>
</div>
}
</code></pre>
</section>
<section>
<h3>Listen und Keys</h3>
<ul class="fragment">
<li>JSX bietet nichts für Listen
<li>Ausgabe typischerweise über <code>Array.map</code>
<li>Elemente einer Liste brauchen einen eindeutigen key
</ul>
<pre class="fragment"><code class="javascript" contenteditable data-trim>
const greetings = [{
id: 0,
name: 'Olli',
greeting: 'Huhu'
},
{
id: 1,
name: 'Oma',
greeting: 'Hallo'
}
];
const body = greetings.map(greeting =>
<tr key={greeting.id}>
<td>{greeting.name}</td>
<td>{greeting.greeting}</td>
</tr>);
</code></pre>
</section>
<section>
<h3>Komponenten in Hierarchien</h3>
<p>Beispiel: Unsere Anwendung</p>
<img src="ai/greeting-hierarchy-without-callbacks.png"/>
</section>
<section>
<h3>Controller und Child-Views</h3>
<!--<img src="smart-dumb.jpg">-->
<!--<img src="smart-dumb.png">-->
<img src="images/smart-dumb-w-label.png">
</section>
<section>
<h3>Durchreichen vom Zustand und Callbacks</h3>
<ul class="fragment">
<li>Funktioniert beides über Properties
</ul>
<pre class="fragment"><code class="javascript" contenteditable data-trim>
class GreetingController extends React.Component {
render() {
const {greetings} = this.state;
return (
<div>
<GreetingMaster greetings={greetings}
onAdd={() => this.setState({mode: MODE_DETAIL})}
</div>);
}
// ...
}
</code></pre>
</section>
<section>
<h3>Properties übergeben mit Object-Spread</h3>
<ul class="fragment">
<li>Übergibt <b>alle</b> Eigenschaften eines Objektes als individuelle Properties</li>
</ul>
<pre class="fragment"><code class="javascript" contenteditable data-trim>
class GreetingController extends React.Component {
const greeting = { name: 'Klaus', greeting: 'Hello' };
render() {
return <GreetingDetail {...greeting} />
// entspricht:
// <GreetingDetail name='Klaus' greeting='Hello' />
}
}
</code></pre>
</section>
<section>
<h3>PropTypes</h3>
<em>Eine Komponente kann deklarieren, welche Properties sie erwartet</em>
<ul>
<li class="fragment">Name und Typ wird angegeben (<a href="https://facebook.github.io/react/docs/typechecking-with-proptypes.html" target="_blank">Mögliche Typen</a>)
<li class="fragment">Fehlende / falsche Properties führen zu Laufzeitfehlern
<li class="fragment">Ab React 15.5 eigenes npm modul: <a href="https://www.npmjs.com/package/prop-types" target="_blank"><code>prop-types</code></a></li>
<li class="fragment">Alternative: Typsystem (<a href="https://flow.org" target="_blank">Flow</a> oder <a href="https://typescriptlang.org" target="_blank">TypeScript</a>)
</ul>
<pre class="fragment"><code class="javascript" contenteditable data-trim>
import PropTypes from 'prop-types';
class GreetingDetail extends React.Component { . . . };
GreetingDetail.propTypes = {
greeting: PropTypes.shape({
name: PropTypes.string.isRequired,
greeting: PropTypes.string.isRequired
}),
onSave: PropTypes.func.isRequired