-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2018_react-oose-hh.html
More file actions
3793 lines (3383 loc) · 131 KB
/
Copy path2018_react-oose-hh.html
File metadata and controls
3793 lines (3383 loc) · 131 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/simple.css" id="theme">
<!-- Code syntax highlighting -->
<!-- <link rel="stylesheet" href="reveal.js/lib/css/zenburn.css"> -->
<link rel="stylesheet" href="reveal.js/lib/css/github.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 {
max-width: 1100px;
}
.reveal pre code {
max-height: 1000px !important;
}
.reveal section img {
border: 0;
box-shadow: 0 0 0 0
}
/*rgb(196, 215, 240)*/
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>
<!--
Moderne Web-Anwendungen werden immer häufiger als Single-Page-Anwendungen gebaut, die vollständig im Browser laufen, um höchstmöglichen Ansprüchen an UI und UX zu genügen.
Dieses Seminar führt Sie in die Entwicklung von Single-Page-Anwendungen am Beispiel mit React ein. React ist eine JavaScript-Bibliothek die von Facebook entwickelt und als Open-Source-Projekt zur Verfügung gestellt wird. Obwohl React an vielen Stellen mit bestehenden „Best-Practices“ bricht, hat es sich in vielen Projekten und namhaften Websites etabliert.
In diesem Seminar lernen Sie, wie Sie React-Komponenten entwickeln und daraus schließlich eine ganze Anwendung. Sie werden sehen, wie Sie Zugriffe auf eine REST-API machen und 3rd-Party-Bibliotheken in Ihre Anwendung integrieren können.
Außerdem werfen wir einen Blick auf verschiedene Architekturmuster, wie etwa „Flux“ und das Framework „Redux“, mit denen typische Architektur-Probleme bei der Entwicklung von Web-Anwendungen adressiert werden und die auch außerhalb von React Verbreitung finden.
Um Anforderungen wie Langlebigkeit und Wartbarkeit zu genügen, werden wir uns ansehen, wie Sie mit TypeScript auch typsichere Anwendungen mit React bauen können. TypeScript ist eine von Microsoft entwickelte Sprache, die JavaScript unter anderem um ein Typ-System erweitert.
Am Ende des Workshops werden Sie eine Vorstellung davon haben, wie React-Anwendungen aufgebaut sind und was die Stärken und Schwächen von React sind. Die vorgestellten Architekturmuster (und die Sprache TypeScript) lassen sich auch mit anderen JavaScript Frameworks wie Angular oder Vue verwenden.
Zusätzlich zu den Seminarunterlagen erhält jeder Teilnehmer ein Exemplar des Buches „React – Die praktische Einführung in React, React Router und Redux“ von Oliver Zeigermann und Nils Hartmann.
Inhalt
Was sind Single-Page-Anwendungen?
React Grundlagen (inkl. Einführung in ES6)
Architekturmuster: Smart- und Dumb Komponenten
Anbindung einer REST-API
Lebenszyklus von React Komponenten
TypeScript Grundlagen
Typsichere React-Anwendungen mit TypeScript
Statemanagement mit Redux
Voraussetzungen
Dieses Seminar richtet sich an Entwickler und Architekten, die Single-Page-Anwendungen entwickeln wollen. Hierzu werden im Seminar React, JavaScript & TypeScript verwendet. Die im Laufe des Seminars vorgestellten Architekturmuster sowie TypeScript lassen sich allerdings auch mit anderen Web-Frameworks wie z.B. Angular anwenden. Teilnehmer sollten erste Erfahrungen mit der Web-Entwicklung und der Sprache JavaScript haben, am besten von ECMAScript 2015 (ES6).
-->
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h3>React Workshop - Vorbereitung</h3>
<ol style="font-size: 85%">
<li>
<ul>
<li>
<code>git clone https://github.com/reactbuch/react-workshop.git</code>
</li>
<li> oder
<code>git pull</code>
</li>
</ul>
</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 run start-backend</code>
</li>
<li>In neuem Terminal:
<ul>
<li>in
<code>code/workspace</code> wechseln</li>
<li>Ausführen:
<code>npm start</code>
</li>
</ul>
</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>2018_react-oose-hh.html</code>
<br>oder: <a href="https://reactbuch.github.io/react-workshop/2018_react-oose-hh.html#/">https://reactbuch.github.io/react-workshop/2018_react-oose-hh.html</a>
</p>
</section>
<section>
<h2>
<b>Webanwendungen entwickeln</b>
</h2>
<h3>Single-Page-Anwendungen mit React, Redux und TypeScript</h3>
<h4>
<a href="http://nilshartmann.net" target="_blank">Nils Hartmann</a> /
<a href="http://twitter.com/nilshartmann" target="_blank">@nilshartmann</a>
/
<a href="mailto:nils@nilshartmann.net" target="_blank">E-Mail: nils@nilshartmann.net</a>
</h4>
</section>
<section>
<h2>Inhalt</h2>
<ul>
<li>
<a href="#/t0">Teil 0: React-Einführung und Build-Prozess</a>
</li>
<li>
<a href="#/t1">Teil I: React-Komponenten (Grundlagen)</a>
</li>
<li>
<a href="#/t2">Teil II: React-Komponenten: Details, Hierarchien und Anwendungen</a>
</li>
<li>
<a href="#/t3">Teil III: Daten lesen und schreiben vom Server</a>
</li>
<li>
<a href="#/t4">Teil IV: React Anwendungen mit TypeScript entwickeln</a>
</li>
<li>
<a href="#/t5">Teil V: Testen von React Anwendungen</a>
</li>
<li>
<a href="#/t6">Teil VI: Client-seitiges Routing</a>
</li>
<li>
<a href="#/t7">Teil VII: Externes Statemanagement mit Redux</a>
</li>
</ul>
<div style="font-size:80%">
<p>
<b>Jederzeit:</b> Fragen und Diskussionen!</p>
</div>
</section>
<section id="example-app">
<h2>Beispiel-Anwendung</h2>
<a target="_blank" href="code/schritte/5-third-party/public/index.html">
<img src="images/greeting-master-and-detail.png" style="border:0;box-shadow:0 0 0 0;height:650px" />
</a>
</section>
<section id="t0">
<h1>Teil 0</h1>
<h2>React-Einführung und Build-Prozess</h2>
</section>
<section>
<h2>Moderne Webanwendungen</h2>
<div class="fragment">
<h3 style="font-weight: bold">Aus Benutzersicht: bestes UI/UX</h3>
<ul>
<li>Einheitliches Layout und Design</li>
<li>Konsistenes Verhalten in der ganzen Anwendung</li>
<li>Konsistente Darstellung der Daten</li>
<li>Gewohntes Verhalten von Desktop Anwendungen</li>
<li>Kurze Reaktionszeiten</li>
</ul>
</div>
<div class="fragment">
<h3 style="font-weight: bold">Aus Entwicklersicht</h3>
<ul>
<li>Einfach und schnell</li>
<li>Saubere und verständliche Architektur</li>
<li>Wartbar auch bei großer und langlebiger Code-Basis
</li>
</ul></div>
</section>
<section>
<h3>Single-Page-Anwendungen</h3>
<ul>
<li>Technisch auf einer (index.html)-Seite</li>
<li>JavaScript ist für Logik und Darstellung alleinverantwortlich</li>
<li>Back-Button, Deep Links etc trotzdem möglich</li>
</ul>
</section>
<section>
<h3>Single-Page-Anwendungen</h3>
<p>Beispiele</p>
<ul>
<li><a href="https://outlook.live.com" target="_blank">Microsoft Outlook</a></li>
<li><a href="https://www.figma.com/" target="_blank">Figma</a></li>
<li><a href="https://open.spotify.com/browse/featured" target="_blank">Spotify</a></li>
</ul>
</section>
<section>
<h3>"Klassische" Web Anwendungen</h3>
<img src="images/abb-02a-classic-webapp-architecture.png" style="border:0;box-shadow:0 0 0 0;height:650px" />
</section>
<section>
<h3>Single-Page-Anwendungen</h3>
<img src="images/abb-02c-spa-webapp-architecture.png" style="border:0;box-shadow:0 0 0 0;height:650px" />
</section>
<section id="react">
<h2>React</h2>
<p>
<a href="https://reactjs.org" target="_blank">https://reactjs.org</a>
</p>
<ul>
<li class="fragment">Framework von und für Facebook</li>
<li class="fragment">Eingesetzt u.a. von Microsoft, Paypal, Spotify uvm</li>
<li class="fragment">Minimales API
<li class="fragment">Minimales Feature Set
<ul>
<li>Ihr könnt/müsst viele Entscheidungen selber treffen</li>
</ul>
<li class="fragment">Bewusste Verstöße gegen Best-Practices
<li class="fragment">Zentrales Konzept: <b>Komponenten</b>
</ul>
</section>
<section>
<h3>Komponenten in React</h3>
<img src="ai/soc.png">
</section>
<section>
<h3>Komponenten in React</h3>
<p style="font-size:smaller">Unser Beispiel in Komponenten</p>
<img src="ai/greeting-example-component-hierarchy.png" style="height: 650px">
</section>
<section>
<h3>React Komponenten</h3>
<ul>
<li class="fragment">bestehen aus
<b>Logik und UI</b>
<li class="fragment">
<b>keine Templatesprache</b>
<li class="fragment">werden
<b>deklarativ</b> beschrieben
<li class="fragment">werden immer
<b>komplett gerendert</b> (kein 2-Wege-Data-Binding)
<li class="fragment">werden zu
<b>ganzen Anwendungen</b> aggregiert
</ul>
</section>
<section>
<h3>React Komponenten</h3>
<ul class="fragment">
<li>Werden als ES6 Klasse (oder Funktion) implementiert
<li>Keine Templatesprache (stattdessen JavaScript)
<li>Templates können HTML-artige Syntax enthalten (JSX)
</ul>
</section>
<section>
<h3>Eine erste Komponente: Hello, World!</h3>
<p><a target="_blank" href="code/schritte/0-hello_world/public/index.html">Demo</a></p>
<ul class="fragment">
<li>Beispiel Schritt-für-Schritt (<code>code/workspace-live-coding</code>)</li>
</ul>
</section>
<section>
<h3>Hello World React</h3>
<pre><code data-trim contenteditable>
import React from "react";
export default class HelloMessage extends React.Component {
constructor(props) {
super(props);
this.state = {greeting: props.initialGreeting};
}
render() {
return (
<div>
<input
value={this.state.greeting}
onChange=
{event => this.setState({greeting: event.target.value})}
/>
<p>{this.state.greeting}, World</p>
</div>
);
}
}</code></pre>
</section>
<section>
<h3>Aufruf</h3>
<div class="fragment">
<p>
<code>index.html</code>
</p>
<pre><code data-trim contenteditable>
<html>
<body>
<div id="mount"></div>
</body>
<script src="dist/main.js"></script>
</html>
</code></pre>
</div>
<div class="fragment">
<p>
<code>main.js</code>
</p>
<pre><code data-trim contenteditable class="javascript">
import React from 'react';
import ReactDOM from 'react-dom';
import HelloMessage from './HelloMessage';
const mountNode = document.getElementById('mount');
ReactDOM.render(<HelloMessage initialGreeting="Hello"/>, mountNode);
</code></pre>
</div>
<p class="fragment">
<a target="_blank" href="code/schritte/0-hello_world/public/index.html">Run</a>
</p>
</section>
<section data-state="exkurs">
<h3>Hintergrund</h3>
<h2>ES6 Features</h2>
</section>
<section data-state="exkurs">
<h3>ES6: Klassen</h3>
<pre><code class="javascript" contenteditable>class Person {
constructor(name) {
this._name = name;
}
getName() {
return this._name;
}
}
class Programmer extends Person {
constructor(name, language) {
super(name);
this.language = language;
}
code() {
return `${this.getName()} 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">
<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 = ...;
// specify exports
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 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 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><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>
<h2>Hintergrund</h2>
<h1>Buildprozess</h1>
</section>
<section>
<h2>Buildprozess</h2>
<ul>
<li>ES6ff- und JSX-Code muss nach ECMAScript 5 übersetzt werden
<ul>
<li>Status der ES6 Implementierungen der Browser:
<a href="https://kangax.github.io/compat-table/es6/" target="blank">
https://kangax.github.io/compat-table/es6/
</a>
</li>
</ul>
<li class="fragment">Typische Werkzeuge
<ul>
<li>Babel oder TypeScript (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 in Memory (Performance)</li>
<li>...Hot Reloading: Automatische Aktualisierung nach Code Änderung</li>
<li>...React Hot Loader: Zustand bleibt nach Aktualisierung erhalten (Beta)</li>
</ul>
</ul>
</section>
<section>
<h3>create-react-app</h3>
<a href="https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#table-of-contents"
target="_blank">User Guide</a>
<p>Bootstrap von React Anwendung</p>
<p>Fertige Konfiguration von Webpack, React, ...</p>
<p>Beispiel: npx create-react-app PROJEKTNAME --typescript</p>
</section>
<section>
<h3>Der yarn Package Manager</h3>
<a href="https://yarnpkg.com/" target="_blank">https://yarnpkg.com</a>
<p class="fragment">Alternative zu NPM</p>
<p class="fragment">Verwendet ebenfalls package.json</p>
<span class="fragment">
<p>Etwas anderes Kommandozeileninterface:</p>
<pre><code data-trim contenteditable class="bash">
# Installiert alle Pakete aus der package.json
yarn
# oder:
yarn install
# Neues Paket installieren oder aktualiseren
yarn add [--dev] package
# Script aus package.json Ausführen
yarn SCRIPT_NAME
</code>
</pre>
</span>
</section>
<section>
<h3>Das Beispiel-Project</h3>
<p>Workspace</p>
<ul>
<li>
<code>2018_react-oose-hh.html</code> Slides im Root-Verzeichnis</li>
<li>
<code>code/workspace</code>: Verzeichnis für Eure Übungen</li>
<li>
<code>code/schritte</code>: Fertige Stände nach den einzelnen Teilen</li>
<li>
<code>code/material</code>: Code als Ausgangspunkt für Übungen</li>
</ul>
<p>Starten</p>
<ol>
<li>Im
<b>Root-Verzeichnis (falls noch nicht geschehen)</b>:
<code>npm run start-backend</code>, um den REST-Server zustarten (einmal starten, dann einfach durchlaufen
lassen)</li>
<li>In
<b>code/workspace</b>:
<code>npm start</code>, um Webpack DevServer zu starten. Kann durchlaufen, falls Reload nicht klappt, neu
starten</li>
</ol>
</section>
<section>
<h2>Übung 0: 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>
<em>Zusatzaufgabe: Experimentiere mit der Anwendung, mache einige Änderungen, wie z.B.</em>
<ul>
<li>Alle Eingaben sollen in Großbuchstaben auftauchen
<li>Gib irgendeine Rückmeldung wenn die Eingabe leer ist
</ul>
</li>
</ul>
</section>
<section id="t1">
<h1>Teil I</h1>
<h2>React-Komponenten (Grundlagen)</h2>
<a href="code/schritte/1-detail/public/index.html" target="_blank">Ziel-Anwendung</a>
</section>
<section>
<h2>Dokumentation</h2>
<p>
<a href="https://reactjs.org/docs/hello-world.html" target="_blank">
https://reactjs.org/docs/hello-world.html
</a>
</p>
</section>
<section>
<h3>Themen</h3>
<ul>
<li>Rendering
<li>Properties und Zustand
<li>Referenzen auf DOM-Elemente (Refs)
</ul>
</section>
<section>
<h3>React: Rendering</h3>
<span class="fragment">
<p>
<em>Jede React-Komponente braucht eine
<code>render</code>-Methode:</em>
<ul>
<li>wird aufgerufen beim ersten Rendering und wenn sich der Zustand ändert</li>
<li>kann HTML-artige JSX-Syntax nutzen</li>
<li>liefert genau
<b>ein</b> Element oder
<b>null</b> oder
<b>ein Boolean</b> oder
<b>ein Array</b> oder
<b>ein Fragment</b> oder
<b>einen String</b> zurück</li>
</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>
);
}
// ...
}
</code></pre>
</span>
</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 {} eingeschlossen
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: JSX #2</h3>
<ul>
<li style="font-size: 80%" class="fragment">Zuweisen an Variablen
<pre><code class="xml" contenteditable data-trim>
const counter = <Counter label="..." />
</code></pre>
</li>
<li style="font-size: 80%" class="fragment">...als Return-Wert:
<pre><code class="xml" contenteditable data-trim>
function renderCounter(label) { return <Counter label="..." />; }
</code></pre>
</li>
<li style="font-size: 80%" class="fragment">Fragmente (rendern selber kein Element in den DOM, nur ihre
Kind-Elemente):
<pre><code class="xml" contenteditable data-trim>
render() {
return <React.Fragment>
<li>Yes</li>
<li>No</li>
</React.Fragment>
}
</code></pre>
</li>
<li style="font-size: 80%" class="fragment">null, false oder boolean, um nichts zu rendern:
<pre><code class="xml" contenteditable data-trim>
render() {
if (!someCondition) {
return null; // oder false oder true
}
return ...;
}
</code></pre>
</li>
</ul>
</section>
<section data-state="exkurs">
<h3>ES6: Erweiterte Objekt-Literale</h3>
<pre><code class="javascript" contenteditable>
// ES5:
const firstName = 'Klaus';
const person = {
firstName: firstName
};
console.log(person.firstName); // Klaus
// ES6:
const firstName = 'Klaus';
const person = {
firstName
};
console.log(person.firstName); // Klaus
</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 = {
fistName: 'Klaus',
lastName: 'Mueller',
age: 42
};</code></pre>
<pre class="fragment"><code class="javascript" contenteditable>const {firstName, age, notThere} = person;
console.log(firstName); // Klaus
console.log(age); // 42
console.log(notThere); // undefined
</code></pre>
<pre class="fragment"><code class="javascript" contenteditable>function someFunction({firstName, age, notThere}) {
console.log(firstName); // Klaus
console.log(age); // 42
console.log(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>
</ul>
</section>
<section>
<h3>Properties einer Komponente</h3>
<ul class="fragment">
<li>Properties werden über den Konstruktor in die Komponente hineingereicht</li>
<li>Properties dürfen nicht verändert werden</li>
<li>Zugriff über
<code>this.props</code>
</li>
</ul>
</section>
<section>
<h3>Children einer Komponente</h3>
<ul class="fragment">
<code>this.props.children</code> enthält Kind-Elemente</li>
</ul>
<div class="fragment">Beispiel:
<pre><code class="javascript" contenteditable data-trim>
class NavBar extends React.Component {
render() {
return (
<div className="NavBar">
<h1>{this.props.title}</h1>
{this.props.children}
</div>
);
}
}
</code></pre></div><div class="fragment">Verwendung:
<pre><code class="javascript" contenteditable data-trim>
<NavBar title="Navigation">
<a href="/home">Home</a>
<a href="/shop">Shop</a>
</NavBar>
</code></pre>
</div>
</section>
<section>
<h3>Zustand einer Komponente</h3>
<ul class="fragment">
<li>Beispiel: Inhalt eines Eingabefelds, Daten vom Server, Menu offen oder zu</li>
<li>
<b>Werte</b> üblicherweise immutable</li>
<li class="fragment">
<b>Initialisieren</b> im Konstruktor mit
<code>this.state={}</code>
<li class="fragment">Zustand
<b>lesen</b> über
<code>this.state</code>
<li class="fragment">Zustand
<b>setzen</b> über
<code>this.setState()</code>
<ul class="fragment">
<li>
<b>Achtung:</b> kein "reiner" Setter</li>
<li class="fragment">Führt alten und neuen Zustand zusammen</li>
<li class="fragment">Wird asynchron ausgeführt!</li>
<li class="fragment">
<b>Löst erneutes rendern der
<i>gesamten</i> Komponente aus</b>
</li>
</ul>
<li class="fragment">Eines der <b>zentralen</b> Konzepte von React</li>
</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 value={this.state.name}
onChange={e => this.updateModel(e)} />
}
// ...
}
</code></pre>
</section>
<section>
<h3>React: Events und Data Binding</h3>
<p>
<em>Es gibt keine automatische Bindung an Modelle (2-Wege Databinding)</em>
</p>
<ul class="fragment">
<li>DOM-Events werden in React-Events verpackt
<li>React-Events haben weitgehend selbe API wie DOM-Events
<li>Besonderes Event:
<b>onChange</b>
<ul>
<li>onChange ist auf
<code>input</code>,
<code>textarea</code>,
<code>select</code> definiert und verhält sich Komponenten- und Browser-übergreifend einheitlich
</li>
</ul>
</li>
</ul>
<pre class="fragment"><code class="xml" contenteditable data-trim>
class GreetingDetail extends React.Component {
render() {
return (
<input onChange={event => . . .} />
);
}
// ...
}
</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>
<p>"Rendern" hat doppelte Bedeutung!</p>
<img src="images/virtual-dom.png" style="border:0;box-shadow:0 0 0 0;height:650px">