-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter5.html
1126 lines (936 loc) · 63.8 KB
/
chapter5.html
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>Learning React - React level 2</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="icon" type="image/x-icon" href="mc2/images/favicon.ico">
<link rel="stylesheet" href="mc2/styles/reveal.css">
<link rel="stylesheet" href="mc2/styles/theme.css" id="theme">
<link rel="stylesheet" href="mc2/styles/code.css">
<link rel="stylesheet" href="styles/react.css">
</head>
<body>
<div id="pos"></div>
<div class="reveal">
<div class="slides">
<section class="slide chaptertitle">
<div class="slidecontent">
<div class="chapternumber"> chapter 5 of 14 </div>
<h1>React level 2</h1>
<span>Things're getting serious</span>
</div>
</section>
<section class="slide" data-pos="5-0-1">
<span class="pos">5-0-1</span>
<div class="slidecontent"><p>Armed with the <strong>basics</strong> of <strong>components</strong> and <strong>JSX</strong>, we're now ready to explore <strong>more powerful API:s</strong>!</p>
</div></section>
<section class="slide sectionlist">
<div class="slidecontent">
<h3>Sections in this chapter</h3>
<ol>
<li><a href="#/3">Class syntax</a></li>
<li><a href="#/4">Stateful components</a></li>
<li><a href="#/5">Event handling</a></li>
<li><a href="#/6">Default properties</a></li>
<li><a href="#/7">Lifecycle methods</a></li>
<li><a href="#/8">Refs</a></li>
<li><a href="#/9">Forms</a></li>
<li><a href="#/10">Communication</a></li>
<li><a href="#/11">Styling</a></li>
<li><a href="#/12">Exercise 3</a></li>
</ol>
</div>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 1/10</div>
<h3>Class syntax</h3>
<p>When plain clothes just won't cut it</p>
</div>
</section>
<section class="slide" data-pos="5-1-1">
<span class="pos">5-1-1</span>
<div class="slidecontent"><p>So far we have <strong>defined components</strong> as <strong>plain functions</strong>:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> User = <span class="hljs-function"><span class="hljs-params">props</span> =></span> <div>Name: {props.name}<<span class="hljs-regexp">/div>;</span>
</code></pre>
</div></section>
<section class="slide" data-pos="5-1-2">
<span class="pos">5-1-2</span>
<div class="slidecontent"><p>But there are actually <strong>two ways to define a component</strong>. The plain function components (or PFC:s) are a <strong>shorthand</strong> for this <strong>full definition</strong>:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
render() {
<span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag"><<span class="hljs-name">div</span>></span>Name: {this.props.name}<span class="hljs-tag"></<span class="hljs-name">div</span>></span></span>;
}
}
</code></pre>
</div></section>
<section class="slide" data-pos="5-1-3">
<span class="pos">5-1-3</span>
<div class="slidecontent"><p>Our <strong>initial plain function</strong> ended up as a <strong><code>render</code> method</strong> in the class definition.</p>
<p>...with <strong>one key difference</strong> however. Did you notice it?</p>
</div></section>
<section class="slide" data-pos="5-1-4">
<span class="pos">5-1-4</span>
<div class="slidecontent"><p>The <strong>props</strong> are <strong>not passed in</strong> to the render method. Instead we <strong>access them using <code>this.props</code></strong>.</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
render() {
<span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag"><<span class="hljs-name">div</span>></span>Name: {this.props.name}<span class="hljs-tag"></<span class="hljs-name">div</span>></span></span>;
}
}
</code></pre>
</div></section>
<section class="slide question" data-pos="5-1-5">
<span class="pos">5-1-5</span>
<div class="slidecontent">
<p>This syntax seems <strong>clunkier in every way</strong>. Why would we ever want to use it?</p>
</div></section>
<section class="slide answer" data-pos="5-1-6">
<span class="pos">5-1-6</span>
<div class="slidecontent">
<p>The reason is that for more advanced components, there is <strong>more to the story than the render method</strong>.</p>
<p>If we need to <strong>say more than one thing</strong>, we must have a <strong>definition object</strong> - or a <strong>class</strong> - to encapsulate those things.</p>
</div></section>
<section class="slide" data-pos="5-1-7">
<span class="pos">5-1-7</span>
<div class="slidecontent"><p>There's <strong>more details</strong> on classes in the <strong>ES6 appendix</strong>, but there's one thing we should cover here that will keep coming up: ES6 classes can <strong>only have methods</strong>, and <strong>not other properties</strong>.</p>
</div></section>
<section class="slide" data-pos="5-1-8">
<span class="pos">5-1-8</span>
<div class="slidecontent"><p>We can however <strong>fake properties</strong> by marking a method as a <strong>getter</strong>:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test</span>() </span>{
get someProp() {
<span class="hljs-keyword">return</span> <span class="hljs-number">42</span>;
}
}
<span class="hljs-keyword">let</span> test = <span class="hljs-keyword">new</span> Test();
<span class="hljs-built_in">console</span>.log(test.someProp); <span class="hljs-comment">// 42</span>
</code></pre>
</div></section>
<section class="slide" data-pos="5-1-9">
<span class="pos">5-1-9</span>
<div class="slidecontent"><p>Still, this is of course rather cumbersome. We want to be able to simply do this...</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test</span>() </span>{
someProp = <span class="hljs-number">42</span>;
}
</code></pre>
<p>...and have it work the same way!</p>
</div></section>
<section class="slide" data-pos="5-1-10">
<span class="pos">5-1-10</span>
<div class="slidecontent"><p>The good news is that we will soon be able to - there's a <a href="https://github.com/tc39/proposal-class-fields">proposal</a> currently at stage 3.</p>
<p>In the meantime, if you don't want to wait, you can use a <a href="https://www.npmjs.com/package/babel-plugin-transform-class-properties">Babel plugin</a> (or TypeScript)!</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 2/10</div>
<h3>Stateful components</h3>
<p>I have my secrets</p>
</div>
</section>
<section class="slide" data-pos="5-2-1">
<span class="pos">5-2-1</span>
<div class="slidecontent"><p>Remember when we said that <strong>a component UI</strong> is the <strong>result of its props</strong>?</p>
<p><img src="resources/5-2-1-79.svg" alt="dot"></p>
</div></section>
<section class="slide" data-pos="5-2-2">
<span class="pos">5-2-2</span>
<div class="slidecontent"><p>That was a <strong>lie</strong>. The UI is a result of props <strong>and state</strong>.</p>
<p><img src="resources/5-2-2-64.svg" alt="dot"></p>
</div></section>
<section class="slide" data-pos="5-2-3">
<span class="pos">5-2-3</span>
<div class="slidecontent"><p>So <strong>what is the difference</strong> between these two?</p>
<ul>
<li><strong>Props</strong> are <strong>passed down from the parent</strong>. They are <strong>immutable</strong>, from the perspective of the receiving component.</li>
<li><strong>State</strong> is <strong>data</strong> that is <strong>only of internal interest</strong>. It lives only inside the component, and the <strong>component is in control</strong> of it.</li>
</ul>
</div></section>
<section class="slide" data-pos="5-2-4">
<span class="pos">5-2-4</span>
<div class="slidecontent"><p>The <strong>similarity</strong> however is that when <strong>either of them change</strong>, we <strong>rerun the render</strong> method.</p>
</div></section>
<section class="slide" data-pos="5-2-5">
<span class="pos">5-2-5</span>
<div class="slidecontent"><p>Also, much like props, <strong>state is always an object</strong>.</p>
<pre><code>{
<span class="hljs-attribute">editing</span>: false,
currentValue: <span class="hljs-string">'Batman'</span>
}
</code></pre><p>We can think of the <strong>individual keys</strong> in that object as <strong>state variables</strong>.</p>
</div></section>
<section class="slide list" data-pos="5-2-6">
<span class="pos">5-2-6</span>
<div class="slidecontent">
<p>There are <strong>three parts</strong> to component state:</p>
<ul>
<li><span>a</span>setting initial state</li>
<li><span>b</span>reading current state</li>
<li><span>c</span>updating state</li>
</ul>
</div></section>
<section class="slide num numA" data-pos="5-2-7">
<span class="pos">5-2-7</span>
<div class="slidecontent">
<p>The <strong>initial state</strong> is set inside the <strong><code>constructor</code></strong>:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StateExample</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
<span class="hljs-keyword">constructor</span>(props) {
<span class="hljs-keyword">super</span>(props);
<span class="hljs-keyword">this</span>.state = {<span class="hljs-attr">data1</span>: <span class="hljs-string">'foo'</span>, <span class="hljs-attr">data2</span>: <span class="hljs-string">'bar'</span>};
}
<span class="hljs-comment">// ... more code here ...</span>
}
</code></pre>
</div></section>
<section class="slide" data-pos="5-2-8">
<span class="pos">5-2-8</span>
<div class="slidecontent"><p>If you have the ability to use <strong>class properties</strong> we can <strong>assign initial state directly</strong> instead:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StateExample</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
state = {<span class="hljs-attr">data1</span>: <span class="hljs-string">'foo'</span>, <span class="hljs-attr">data2</span>: <span class="hljs-string">'bar'</span>};
<span class="hljs-comment">// ... more code here ...</span>
}
</code></pre>
</div></section>
<section class="slide" data-pos="5-2-9">
<span class="pos">5-2-9</span>
<div class="slidecontent"><p>A <strong>PFC cannot have state</strong>, since they lack the ability to give information about instances.</p>
</div></section>
<section class="slide num numB" data-pos="5-2-10">
<span class="pos">5-2-10</span>
<div class="slidecontent">
<p>We <strong>read from state</strong> simply by <strong>accessing <code>this.state</code></strong>:</p>
<pre><code><span class="hljs-function"><span class="hljs-title">render</span><span class="hljs-params">()</span></span>{
<span class="hljs-keyword">if</span> (this<span class="hljs-selector-class">.state</span><span class="hljs-selector-class">.editing</span>){
<span class="hljs-comment">// return editing version</span>
} <span class="hljs-keyword">else</span> {
<span class="hljs-comment">// return view version</span>
}
}
</code></pre></div></section>
<section class="slide num numC" data-pos="5-2-11">
<span class="pos">5-2-11</span>
<div class="slidecontent">
<p>To <strong>update state</strong> we must call <strong><code>this.setState</code></strong>. That method expects an object with all parts we want to update:</p>
<pre><code class="lang-javascript">cancel() {
<span class="hljs-keyword">this</span>.setState({<span class="hljs-attr">editing</span>: <span class="hljs-literal">false</span>});
}
</code></pre>
<p>Note that any <strong>existing state keys not mentioned</strong> here will be <strong>unchanged</strong>.</p>
</div></section>
<section class="slide" data-pos="5-2-12">
<span class="pos">5-2-12</span>
<div class="slidecontent"><p>Also - we might need to do some gymnastics to <strong>make sure <code>this</code> is correct inside the method</strong>:</p>
<pre><code class="lang-javascript">cancel() {
<span class="hljs-keyword">this</span>.setState({<span class="hljs-attr">editing</span>: <span class="hljs-literal">false</span>});
}
</code></pre>
<p>But we'll address that in the <strong>upcoming section</strong>!</p>
</div></section>
<section class="slide" data-pos="5-2-13">
<span class="pos">5-2-13</span>
<div class="slidecontent"><p>Remember to <strong>never mutate <code>this.state</code> directly</strong>!</p>
<pre><code class="lang-javascript">cancel() {
<span class="hljs-keyword">this</span>.state.editing = <span class="hljs-literal">false</span>; <span class="hljs-comment">// this should use setState instead!</span>
}
</code></pre>
<p>The state will be updated, but the <strong>component will not rerender</strong> since React doesn't know something just happened!</p>
</div></section>
<section class="slide" data-pos="5-2-14">
<span class="pos">5-2-14</span>
<div class="slidecontent"><p>Let's internalize the <strong>concepts of state</strong> in a demo: <a href="resources/site/demos/state/index.html" target="_blank">State</a></p>
<p>There's <strong>more to say</strong> about <code>setState</code>, but we're saving that for a later chapter!</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 3/10</div>
<h3>Event handling</h3>
<p>Waiter, there are inline event handlers in my HTML!</p>
</div>
</section>
<section class="slide" data-pos="5-3-1">
<span class="pos">5-3-1</span>
<div class="slidecontent"><p>Something we haven't seen yet - how do we <strong>attach event handlers</strong> in React?</p>
<p>We can already realize that it is <strong>not done through regular means</strong>, since we <strong>don't have access to the real DOM</strong>.</p>
</div></section>
<section class="slide" data-pos="5-3-2">
<span class="pos">5-3-2</span>
<div class="slidecontent"><p>The answer will feel like <strong>going back in time</strong>: we simply declare <strong>inline event handlers</strong> as props!</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> ClickableWord = <span class="hljs-function"><span class="hljs-params">props</span> =></span> {
<span class="hljs-keyword">let</span> clickHandler = <span class="hljs-function"><span class="hljs-params">()</span> =></span> alert(<span class="hljs-string">"You clicked me!"</span>);
<span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag"><<span class="hljs-name">span</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{clickHandler}</span>></span>Click me!<span class="hljs-tag"></<span class="hljs-name">span</span>></span></span>;
}
</code></pre>
</div></section>
<section class="slide" data-pos="5-3-3">
<span class="pos">5-3-3</span>
<div class="slidecontent"><p>When rendering, React will <strong>notice the prop</strong> named <code>onClick</code>, and <strong>attach an event handler</strong> accordingly.</p>
<p>This works for <strong>all DOM events</strong>; <code>onSubmit</code>, <code>onFocus</code>, etc.</p>
</div></section>
<section class="slide" data-pos="5-3-4">
<span class="pos">5-3-4</span>
<div class="slidecontent"><p>Much like jQuery it will pass in a <strong>normalised event object</strong> to the callback.</p>
<p>For the full <strong>list of supported events</strong> and <strong>contents of the event object</strong>, see the <strong>API docs</strong> here: <a href="https://facebook.github.io/react/docs/events.html" class="link" target="_blank">Events</a></p>
</div></section>
<section class="slide question" data-pos="5-3-5">
<span class="pos">5-3-5</span>
<div class="slidecontent">
<p>Aren't we <strong>mixing behavior and visuals</strong> when we declare event handlers in the render function?</p>
</div></section>
<section class="slide answer" data-pos="5-3-6">
<span class="pos">5-3-6</span>
<div class="slidecontent">
<p>We are mixing technologies, but <strong>not concerns</strong>. Our component definition encapsulates everything about the component.</p>
</div></section>
<section class="slide" data-pos="5-3-7">
<span class="pos">5-3-7</span>
<div class="slidecontent"><p>When using <strong>Class components</strong> it is customary (but not technically necessary) to <strong>use component methods as event handlers</strong>:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ClickableWord</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
clickHandler() {
alert(<span class="hljs-string">"You clicked me!"</span>);
}
render() {
<span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag"><<span class="hljs-name">span</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.clickHandler}</span>></span>Click me!<span class="hljs-tag"></<span class="hljs-name">span</span>></span></span>;
}
}
</code></pre>
</div></section>
<section class="slide" data-pos="5-3-8">
<span class="pos">5-3-8</span>
<div class="slidecontent"><p>There's a trap here - the methods are <strong>not autobound to the instance</strong>, so we must be careful when we need to access <code>this</code>. This <strong>won't work</strong>:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ClickableWord</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
showExplanation() {
alert(<span class="hljs-keyword">this</span>.props.explanation); <span class="hljs-comment">// BOOM! `this.props` doesnt exist</span>
}
render() {
<span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag"><<span class="hljs-name">span</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.showExplanation}</span>></span>{this.props.word}<span class="hljs-tag"></<span class="hljs-name">span</span>></span></span>;
}
}
</code></pre>
</div></section>
<section class="slide list" data-pos="5-3-9">
<span class="pos">5-3-9</span>
<div class="slidecontent">
<p>There are <strong>many ways</strong> to solve this. Here are a few common ones:</p>
<ul>
<li><span>a</span>anonymous handler calling the method</li>
<li><span>b</span>bind the handler in the constructor</li>
<li><span>c</span>define the handler in the constructor</li>
<li><span>d</span>assign arrow funcs to class prop</li>
<li><span>e</span>using decorators</li>
</ul>
</div></section>
<section class="slide num numA" data-pos="5-3-10">
<span class="pos">5-3-10</span>
<div class="slidecontent">
<p>By <strong>calling the handler as a method</strong> in an arrow function, <code>this</code> will be correct:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ClickableWord</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
showExplanation(e) {
alert(<span class="hljs-keyword">this</span>.props.explanation);
}
render() {
<span class="hljs-keyword">let</span> handler = <span class="hljs-function"><span class="hljs-params">()</span> =></span> <span class="hljs-keyword">this</span>.showExplanation()
<span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag"><<span class="hljs-name">span</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{handler}</span>></span>{this.props.word}<span class="hljs-tag"></<span class="hljs-name">span</span>></span></span>;
}
}
</code></pre>
</div></section>
<section class="slide num numB" data-pos="5-3-11">
<span class="pos">5-3-11</span>
<div class="slidecontent">
<p>...or we can <strong>bind it in the constructor</strong>:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ClickableWord</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
<span class="hljs-keyword">constructor</span>(props){
<span class="hljs-keyword">super</span>(props); <span class="hljs-comment">// because we have to, see the ES6 appendix!</span>
<span class="hljs-keyword">this</span>.showExplanation = <span class="hljs-keyword">this</span>.showExplanation.bind(<span class="hljs-keyword">this</span>);
}
showExplanation(e) {
alert(<span class="hljs-keyword">this</span>.props.explanation);
}
render() {
<span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag"><<span class="hljs-name">span</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.showExplanation}</span>></span>{this.props.word}<span class="hljs-tag"></<span class="hljs-name">span</span>></span></span>;
}
}
</code></pre>
</div></section>
<section class="slide num numC" data-pos="5-3-12">
<span class="pos">5-3-12</span>
<div class="slidecontent">
<p>...or we simply define it as an <strong>arrow function in the constructor</strong>:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ClickableWord</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
<span class="hljs-keyword">constructor</span>(props){
<span class="hljs-keyword">super</span>(props);
<span class="hljs-keyword">this</span>.showExplanation = <span class="hljs-function"><span class="hljs-params">()</span> =></span> alert(<span class="hljs-keyword">this</span>.props.explanation);
}
render() {
<span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag"><<span class="hljs-name">span</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.showExplanation}</span>></span>{this.props.word}<span class="hljs-tag"></<span class="hljs-name">span</span>></span></span>;
}
}
</code></pre>
</div></section>
<section class="slide num numD" data-pos="5-3-13">
<span class="pos">5-3-13</span>
<div class="slidecontent">
<p>If you can use <strong>class properties</strong> we can <strong>assign event handlers as arrow functions</strong>:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ClickableWord</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
showExplanation = <span class="hljs-function"><span class="hljs-params">()</span> =></span> alert(<span class="hljs-keyword">this</span>.props.explanation)
render() {
<span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag"><<span class="hljs-name">span</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.showExplanation}</span>></span>{this.props.word}<span class="hljs-tag"></<span class="hljs-name">span</span>></span></span>;
}
}
</code></pre>
<p>The <code>this</code> context inside the arrow function will now be the instance, no further work required!</p>
</div></section>
<section class="slide num numE" data-pos="5-3-14">
<span class="pos">5-3-14</span>
<div class="slidecontent">
<p>Finally, perhaps less commonly used; if we're using Babel to allow <strong>decorators</strong>, we can employ the <a href="https://www.npmjs.com/package/autobind-decorator">AutoBind decorator</a>:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ClickableWord</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
@autobind
showExplanation() {
alert(<span class="hljs-keyword">this</span>.props.explanation);
}
render() {
<span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag"><<span class="hljs-name">span</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.showExplanation}</span>></span>{this.props.word}<span class="hljs-tag"></<span class="hljs-name">span</span>></span></span>;
}
}
</code></pre>
<p>Now autobind will make sure that <code>this</code> inside <code>showExplanation</code> always points to the correct <code>this</code>.</p>
</div></section>
<section class="slide" data-pos="5-3-15">
<span class="pos">5-3-15</span>
<div class="slidecontent"><p>You can <strong>explore the examples</strong> in the <a href="resources/site/demos/events/index.html" target="_blank">Events</a> demo.</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 4/10</div>
<h3>Default properties</h3>
<p>Fallback fun</p>
</div>
</section>
<section class="slide" data-pos="5-4-1">
<span class="pos">5-4-1</span>
<div class="slidecontent"><p>In some instances it makes sense for a component to provide a <strong>fallback value</strong> if a property wasn't passed in.</p>
</div></section>
<section class="slide" data-pos="5-4-2">
<span class="pos">5-4-2</span>
<div class="slidecontent"><p>We could just do it <strong>manually</strong>, as in this case for the <code>age</code> in the <code>User</code> component:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> User = <span class="hljs-function">(<span class="hljs-params">props</span>) =></span> (
<span class="xml"><span class="hljs-tag"><<span class="hljs-name">div</span>></span>Name: {props.name}, age: {props.age || 'unknown'}<span class="hljs-tag"></<span class="hljs-name">div</span>></span></span>
);
</code></pre>
<p>But it would be nice to explicitly declare our defaults, which is why React provides the <code>defaultProps</code> API:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> User = <span class="hljs-function">(<span class="hljs-params">props</span>) =></span> (
<span class="xml"><span class="hljs-tag"><<span class="hljs-name">div</span>></span>Name: {props.name}, age: {props.age}<span class="hljs-tag"></<span class="hljs-name">div</span>></span></span>
);
User.defaultProps = {
<span class="hljs-attr">age</span>: <span class="hljs-string">'unknown'</span>
};
</code></pre>
</div></section>
<section class="slide" data-pos="5-4-3">
<span class="pos">5-4-3</span>
<div class="slidecontent"><p>When using a class we put them in a static getter:</p>
<pre><code>export <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
static get defaultProps(){
<span class="hljs-keyword">return</span> {
age: <span class="hljs-symbol">'unknow</span>n'
};
}
render() {
<span class="hljs-keyword">return</span> (
<div><span class="hljs-type">Name</span>: {props.name}, age: {props.age}</div>
);
}
}
</code></pre></div></section>
<section class="slide" data-pos="5-4-4">
<span class="pos">5-4-4</span>
<div class="slidecontent"><p>Or, if we have access to class properties via TypeScript or Babel as mentioned earlier, we can <code>assign them as an object</code> like we did with state:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">export</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
<span class="hljs-keyword">static</span> defaultProps = {
<span class="hljs-attr">age</span>: <span class="hljs-string">'unknown'</span>
}
render() {
<span class="hljs-keyword">return</span> (
<span class="xml"><span class="hljs-tag"><<span class="hljs-name">div</span>></span>Name: {props.name}, age: {props.age}<span class="hljs-tag"></<span class="hljs-name">div</span>></span></span>
);
}
}
</code></pre>
</div></section>
<section class="slide" data-pos="5-4-5">
<span class="pos">5-4-5</span>
<div class="slidecontent"><p>See the API in action in the <a href="resources/site/demos/defaultprops/index.html" target="_blank">DefaultProps</a> demo.</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 5/10</div>
<h3>Lifecycle methods</h3>
<p>The circle of life</p>
</div>
</section>
<section class="slide" data-pos="5-5-1">
<span class="pos">5-5-1</span>
<div class="slidecontent"><p>There are several <strong>lifecycle methods</strong> that you can define to hook into certain moments in the life of a component.</p>
</div></section>
<section class="slide" data-pos="5-5-2">
<span class="pos">5-5-2</span>
<div class="slidecontent"><p>When the component is <strong>created</strong>, the following methods are called:</p>
<p><img src="resources/5-5-2-70.svg" alt="dot"></p>
</div></section>
<section class="slide" data-pos="5-5-3">
<span class="pos">5-5-3</span>
<div class="slidecontent"><p>Recently Deprecated:
<img src="resources/5-5-3-21.svg" alt="dot"></p>
<ul>
<li><code>componentWillMount()</code> is being deprecated and will be removed in 17.0</li>
<li><code>componentWillMount()</code> renamed to <code>UNSAFE_componentWillMount()</code></li>
</ul>
</div></section>
<section class="slide" data-pos="5-5-4">
<span class="pos">5-5-4</span>
<div class="slidecontent"><p>Then, when a component will update (because <strong>state</strong> or <strong>props</strong> <strong>changed</strong>), the following are called with the new props and state:</p>
<p><img src="resources/5-5-4-137.svg" alt="dot"></p>
</div></section>
<section class="slide" data-pos="5-5-5">
<span class="pos">5-5-5</span>
<div class="slidecontent"><p>Recently Deprecated:</p>
<p><img src="resources/5-5-5-22.svg" alt="dot"></p>
<ul>
<li><code>componentWillReceiveProps()</code> is being deprecated and will be removed in 17.0</li>
<li><code>componentWillUpdate()</code> is being deprecated and will be removed in 17.0</li>
<li>Both are renamed with <code>UNSAFE_</code> prefix</li>
</ul>
</div></section>
<section class="slide" data-pos="5-5-6">
<span class="pos">5-5-6</span>
<div class="slidecontent"><p>You can also <strong><code>forceUpdate</code></strong>:</p>
<p><img src="resources/5-5-6-34.svg" alt="dot">
But, that is <strong>rarely necessary</strong>, if ever.</p>
</div></section>
<section class="slide" data-pos="5-5-7">
<span class="pos">5-5-7</span>
<div class="slidecontent"><p>Finally, <strong>before</strong> a component is <strong>destroyed</strong>, <strong><code>componentWillUnmount</code></strong> is called. This is a good place to do listener and interval <strong>cleanup</strong>.</p>
</div></section>
<section class="slide" data-pos="5-5-8">
<span class="pos">5-5-8</span>
<div class="slidecontent"><p>Explore these methods in the <a href="resources/site/demos/lifecycle/index.html" class="demo" target="_blank">Lifecycle</a> demo.</p>
</div></section>
<section class="slide question" data-pos="5-5-9">
<span class="pos">5-5-9</span>
<div class="slidecontent">
<p>What are the <strong>common use cases</strong> for these methods?</p>
</div></section>
<section class="slide answer" data-pos="5-5-10">
<span class="pos">5-5-10</span>
<div class="slidecontent">
<p>Sometimes we might <strong>kick off something</strong> in <strong><code>componentDidMount</code></strong>, and there are times when implementing <strong><code>shouldComponentUpdate</code></strong> can <strong>help performance</strong>, but normally you <strong>don't need to bother</strong>.</p>
<p>There's a demo of <a href="resources/site/demos/shouldcomponentupdate/index.html" target="_blank">ShouldComponentUpdate</a> in action.</p>
</div></section>
<section class="slide" data-pos="5-5-11">
<span class="pos">5-5-11</span>
<div class="slidecontent"><p>In React v16 a <strong>new hook</strong>, <code>componentDidCatch(error, info)</code>, is introduced. With that we can <strong>catch errors thrown by children</strong> and grandchildren (but not by the current component).</p>
<p>See it happen in the <a href="resources/site/demos/errorcatching/index.html" target="_blank">ErrorCatching</a> demo.</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 6/10</div>
<h3>Refs</h3>
<p>There's a string attached</p>
</div>
</section>
<section class="slide" data-pos="5-6-1">
<span class="pos">5-6-1</span>
<div class="slidecontent"><p>Sometimes you want to <strong>keep a line open to something you have rendered</strong>. This is accomplished through <strong>the <code>ref</code> syntax</strong>.</p>
</div></section>
<section class="slide" data-pos="5-6-2">
<span class="pos">5-6-2</span>
<div class="slidecontent"><p>It works by <strong>attaching a function to the <code>ref</code> prop</strong> of the output part you are interested in:</p>
<pre><code><span class="hljs-tag"><<span class="hljs-name">Comp</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">{refFunc}</span> /></span>
</code></pre><p>Now, when the above is rendered, <strong><code>refFunc</code> will be called with <code>Comp</code></strong> as a parameter. See this in action in the <a href="resources/site/demos/refcomponent/index.html" target="_blank">Refcomponent</a> demo.</p>
</div></section>
<section class="slide question" data-pos="5-6-3">
<span class="pos">5-6-3</span>
<div class="slidecontent">
<p>But, why would I want to reference a child component like that? Doesn't it <strong>break uni-directional flow</strong> to do that instead of passing properties?</p>
</div></section>
<section class="slide answer" data-pos="5-6-4">
<span class="pos">5-6-4</span>
<div class="slidecontent">
<p>Absolutely correct! This is a tool for <strong>very special cases</strong>, for example if you are gradually converting a codebase to React.</p>
</div></section>
<section class="slide" data-pos="5-6-5">
<span class="pos">5-6-5</span>
<div class="slidecontent"><p><code>Refs</code> are much more often used on <strong>regular DOM elements</strong>, when we want to do something with a DOM node.</p>
<p>For example, in the <a href="resources/site/demos/refjquery/index.html" target="_blank">RefjQuery</a> demo we take a rendered node and execute a (silly) jQuery plugin on it.</p>
</div></section>
<section class="slide" data-pos="5-6-6">
<span class="pos">5-6-6</span>
<div class="slidecontent"><p>A common use case is to <strong>interact with inputs</strong>. If we render this:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">{</span> (<span class="hljs-attr">el</span>)=></span> this._input = el }/>
</code></pre>
<p>...then in <code>componentDidMount</code> we could do this...</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">this</span>._input.focus();
</code></pre>
<p>...and elsewhere we can get the input content like this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">this</span>._input.value
</code></pre>
</div></section>
<section class="slide" data-pos="5-6-7">
<span class="pos">5-6-7</span>
<div class="slidecontent"><p>There'll be more on form shenanigans in the upcoming Forms section, but for now you can try this example in the <a href="resources/site/demos/refinput/index.html" target="_blank">RefInput</a> demo.</p>
</div></section>
<section class="slide" data-pos="5-6-8">
<span class="pos">5-6-8</span>
<div class="slidecontent"><p>There is also a <strong>legacy syntax</strong> where you <strong>give <code>ref</code> a string</strong>. The previous example would become:</p>
<pre><code class="lang-jsx"><span class="hljs-comment">// render</span>
<input ref=<span class="hljs-string">"_input"</span>/>
<span class="hljs-comment">// interacting with the node</span>
<span class="hljs-keyword">this</span>.refs._input
</code></pre>
<p>This is demonstrated in the <a href="resources/site/demos/refbystring/index.html" target="_blank">RefByString</a> demo.</p>
</div></section>
<section class="slide" data-pos="5-6-9">
<span class="pos">5-6-9</span>
<div class="slidecontent"><p>At first glance the <strong>string syntax looks easier</strong>, it has been deemed <strong>unidiomatic</strong> and <strong>will eventually be deprecated</strong>.</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 7/10</div>
<h3>Forms</h3>
<p>Sign here and here and here and</p>
</div>
</section>
<section class="slide" data-pos="5-7-1">
<span class="pos">5-7-1</span>
<div class="slidecontent"><p>It might not be immediately apparent, but <strong>React isn't a good fit for forms</strong>. </p>
<p>The reason is that the <strong>HTML model</strong> for forms is built on <strong>node mutation</strong>, which doesn't (or at least shouldn't) exist in React!</p>
</div></section>
<section class="slide" data-pos="5-7-2">
<span class="pos">5-7-2</span>
<div class="slidecontent"><p>Consider this vanilla <strong>HTML input element</strong>:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"name"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"John Doe"</span>/></span>
</code></pre>
<p>The provided <strong><code>value</code> prop</strong> will set the <strong>initial value</strong>. When the <strong>user types</strong> in the field, the <code>value</code> prop of the node will be <strong>updated</strong>. So we can <strong>query the current value</strong> like this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> val = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"name"</span>).value;
</code></pre>
</div></section>
<section class="slide" data-pos="5-7-3">
<span class="pos">5-7-3</span>
<div class="slidecontent"><p>This <strong>doesn't make sense</strong> in a React setting. The <strong>props are set at render</strong>, and will <strong>never mutate</strong>. If <strong>state or props for the component change</strong>, we simply <strong>rerender</strong> it, but in-between renders nothing should happen!</p>
</div></section>
<section class="slide" data-pos="5-7-4">
<span class="pos">5-7-4</span>
<div class="slidecontent"><p>For example, this means that if we <strong>give an input a <code>value</code></strong>, that value cannot be changed! See the <a href="resources/site/demos/petrifiedinput/index.html" target="_blank">PetrifiedInput</a> demo.</p>
</div></section>
<section class="slide list" data-pos="5-7-5">
<span class="pos">5-7-5</span>
<div class="slidecontent">
<p>So <strong>how do we do it</strong> in React? Well, there are <strong>two different patterns</strong>!</p>
<ul>
<li><span>a</span>Uncontrolled</li>
<li><span>b</span>Controlled</li>
</ul>
</div></section>
<section class="slide num numA" data-pos="5-7-6">
<span class="pos">5-7-6</span>
<div class="slidecontent">
<p>The first is the one you've already seen in the <a href="resources/site/demos/refinput/index.html" target="_blank">RefInput</a> demo, which is called having <strong>uncontrolled inputs</strong>.</p>
<p>This pattern closely resembles what happens in a <strong>regular DOM form</strong>, where we let the DOM node keep its own state.</p>
</div></section>
<section class="slide" data-pos="5-7-7">
<span class="pos">5-7-7</span>
<div class="slidecontent"><p>Having an <strong>uncontrolled input</strong> involves the following:</p>
<ul>
<li>We set <strong>initial value</strong> using the <strong><code>defaultValue</code></strong> prop</li>
<li>The form control will <strong>mutate its <code>value</code> prop</strong> when the user makes changes</li>
<li>We can <strong>read the value</strong> using the <strong>event object</strong> in the <strong>change event</strong>.</li>
<li>Or we <strong>fetch the value</strong> later by using a <strong><code>ref</code></strong>!</li>
</ul>
<p>Check out the dedicated <a href="resources/site/demos/uncontrolled/index.html" target="_blank">Uncontrolled</a> demo.</p>
</div></section>
<section class="slide num numB" data-pos="5-7-8">
<span class="pos">5-7-8</span>
<div class="slidecontent">
<p>In contrast, the more idiomatic pattern is called having <strong>controlled inputs</strong>. It works like this:</p>
<ul>
<li>We set <strong>initial value</strong> using the <strong><code>value</code> prop</strong> as usual</li>
<li>That value is <strong>provided by component prop or state</strong></li>
<li>We update that value in the <strong>change event</strong></li>
<li>This <strong>causes a rerender</strong> which will <strong>update the component</strong>!</li>
</ul>
<p>Check it out in the <a href="resources/site/demos/controlled/index.html" target="_blank">Controlled</a> demo.</p>
</div></section>
<section class="slide question" data-pos="5-7-9">
<span class="pos">5-7-9</span>
<div class="slidecontent">
<p>So, <strong>comparing uncontrolled and controlled</strong> inputs - is it fair to say that...</p>
<ul>
<li>with an <strong>uncontrolled input</strong> we store the <strong>state</strong> in the <strong>DOM</strong>?</li>
<li>with a <strong>controlled input</strong> we store the <strong>state</strong> in the <strong>component state</strong></li>
</ul>
</div></section>
<section class="slide answer" data-pos="5-7-10">
<span class="pos">5-7-10</span>
<div class="slidecontent">
<p>Yes, exactly! :)</p>
<p>Also note that it <strong>never makes sense</strong> to have a <strong><code>ref</code> on a controlled input</strong> to access its value, since their values are in the state or props of the component. There's no need to read the value from the DOM node.</p>
</div></section>
<section class="slide" data-pos="5-7-11">
<span class="pos">5-7-11</span>
<div class="slidecontent"><p>You can read <strong>all the detail about forms</strong> on the <a href="https://facebook.github.io/react/docs/forms.html" class="link" target="_blank">Forms</a> documentation, but we'll mention <strong>two more things</strong> that are different about using forms in React:</p>
</div></section>
<section class="slide" data-pos="5-7-12">
<span class="pos">5-7-12</span>
<div class="slidecontent"><p>First off: In <strong>regular HTML</strong> you give <strong>default options</strong> a <strong><code>selected</code> attribute</strong>:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">select</span>></span>
<span class="hljs-tag"><<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"U"</span>></span>Urd; that what was<span class="hljs-tag"></<span class="hljs-name">option</span>></span>
<span class="hljs-tag"><<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"V"</span> <span class="hljs-attr">selected</span>></span>Verdandi; that which is<span class="hljs-tag"></<span class="hljs-name">option</span>></span>
<span class="hljs-tag"><<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"S"</span>></span>Skuld; That which will be<span class="hljs-tag"></<span class="hljs-name">option</span>></span>
<span class="hljs-tag"></<span class="hljs-name">select</span>></span>
</code></pre>
</div></section>
<section class="slide" data-pos="5-7-13">
<span class="pos">5-7-13</span>
<div class="slidecontent"><p>In <strong>React</strong> we instead set <code>value</code> or <code>defaultValue</code> on the <strong><code>select</code></strong> tag itself (which makes more semantic sense anyway):</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">select</span> <span class="hljs-attr">defaultValue</span>=<span class="hljs-string">"V"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"U"</span>></span>Urd; that what was<span class="hljs-tag"></<span class="hljs-name">option</span>></span>
<span class="hljs-tag"><<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"V"</span>></span>Verdandi; that which is<span class="hljs-tag"></<span class="hljs-name">option</span>></span>
<span class="hljs-tag"><<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"S"</span>></span>Skuld; That which will be<span class="hljs-tag"></<span class="hljs-name">option</span>></span>
<span class="hljs-tag"></<span class="hljs-name">select</span>></span>
</code></pre>
<p><strong>Try this out</strong> in the <a href="resources/site/demos/select/index.html" target="_blank">Select</a> demo.</p>
</div></section>
<section class="slide" data-pos="5-7-14">
<span class="pos">5-7-14</span>
<div class="slidecontent"><p>Finally <strong>the second difference</strong> we wanted to highlight: in <strong>regular HTML</strong> we provide <strong>initial value</strong> to a <strong><code>textarea</code></strong> by passing it as a <strong>child</strong>:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">textarea</span>></span>Enter your comments here<span class="hljs-tag"></<span class="hljs-name">textarea</span>></span>
</code></pre>
<p>In React you should instead pass it as a <strong><code>value</code> or <code>defaultValue</code> prop</strong>.</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">textarea</span> <span class="hljs-attr">defaultValue</span>=<span class="hljs-string">"Enter your comments here"</span>/></span>
</code></pre>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 8/10</div>
<h3>Communication</h3>
<p>Parent-child interaction</p>
</div>
</section>
<section class="slide" data-pos="5-8-1">
<span class="pos">5-8-1</span>
<div class="slidecontent"><p>A <strong>parent</strong> can <strong>give data to a child</strong> easy enough by <strong>passing props</strong>.</p>
<p>But how does a <strong>child give data to a parent</strong>?</p>
</div></section>
<section class="slide" data-pos="5-8-2">
<span class="pos">5-8-2</span>
<div class="slidecontent"><p>An easy and decoupling pattern is for the <strong>parent</strong> to <strong>pass a callback as a prop</strong>.</p>
<p>When the child wants to <strong>give data back to the parent</strong>, it can simply <strong>invoke the callback</strong> with the data.</p>
</div></section>
<section class="slide" data-pos="5-8-3">
<span class="pos">5-8-3</span>
<div class="slidecontent"><p>Thus the <strong>communication cycle</strong> can be described like this:</p>
<p><img src="resources/5-8-3-62.svg" alt="dot"></p>
</div></section>
<section class="slide" data-pos="5-8-4">
<span class="pos">5-8-4</span>
<div class="slidecontent"><p>Look at this <strong>parent</strong> teaching his child to count:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Parent</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
<span class="hljs-keyword">constructor</span>(props){
<span class="hljs-keyword">super</span>(props);
<span class="hljs-keyword">this</span>.state = {<span class="hljs-attr">count</span>:<span class="hljs-number">1</span>, <span class="hljs-attr">feedback</span>: <span class="hljs-string">"Let's begin!"</span>};
<span class="hljs-keyword">this</span>.receiveAnswer = <span class="hljs-keyword">this</span>.receiveAnswer.bind(<span class="hljs-keyword">this</span>);
}
receiveAnswer(answer) { <span class="hljs-comment">/* next slide */</span>}
render() {
<span class="hljs-keyword">return</span> (
<span class="xml"><span class="hljs-tag"><<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">strong</span>></span>Parent:<span class="hljs-tag"></<span class="hljs-name">strong</span>></span>
{this.state.feedback} What comes after {this.state.count}?
<span class="hljs-tag"><<span class="hljs-name">hr</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">Child</span> <span class="hljs-attr">reply</span>=<span class="hljs-string">{this.receiveAnswer}</span> <span class="hljs-attr">number</span>=<span class="hljs-string">{this.state.count}/</span>></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
);
}
};</span>
</code></pre>
</div></section>
<section class="slide" data-pos="5-8-5">
<span class="pos">5-8-5</span>
<div class="slidecontent"><p>The parent <strong>passes <code>this.receiveAnswer</code> as a callback to <code>Child</code></strong>, along with <code>this.state.count</code>. Here's the code for <code>receiveAnswer</code>:</p>
<pre><code>receiveAnswer(answer) {
<span class="hljs-keyword">if</span> (answer === <span class="hljs-keyword">this</span>.state.count + <span class="hljs-number">1</span>){
<span class="hljs-keyword">this</span>.setState({
count: <span class="hljs-keyword">this</span>.state.count+<span class="hljs-number">1</span>,
feedback: <span class="hljs-string">'Good! Lets try the next one:'</span>
});
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">this</span>.setState({
feedback: <span class="hljs-string">'No, try again!'</span>
});
}
}
</code></pre></div></section>
<section class="slide" data-pos="5-8-6">
<span class="pos">5-8-6</span>
<div class="slidecontent"><p>And here's the corresponding <strong>child</strong>, who will <strong>call the callback</strong> as appropriate.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> Child = <span class="hljs-function"><span class="hljs-params">props</span> =></span> {
<span class="hljs-keyword">let</span> correctAnswer = props.number + <span class="hljs-number">1</span>;
<span class="hljs-keyword">let</span> cb = props.reply;
<span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag"><<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">strong</span>></span>Child: <span class="hljs-tag"></<span class="hljs-name">strong</span>></span>
<span class="hljs-tag"><<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{e</span>=></span> cb(correctAnswer)}>{correctAnswer}!<span class="hljs-tag"></<span class="hljs-name">button</span>></span>
<span class="hljs-tag"><<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{e</span>=></span> cb(correctAnswer+1)}>{correctAnswer+1}!<span class="hljs-tag"></<span class="hljs-name">button</span>></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span></span>;
}
</code></pre>
<p><strong>Try this example</strong> in the <a href="resources/site/demos/communication/index.html" target="_blank">Communication</a> demo.</p>
</div></section>
<section class="slide" data-pos="5-8-7">
<span class="pos">5-8-7</span>
<div class="slidecontent"><p>There is also a <strong>more complex</strong> (and <strong>less silly</strong>) example of the same pattern in the <a href="resources/site/demos/communication2/index.html" target="_blank">Communication2</a> demo.</p>
<p>And finally, <a href="resources/site/demos/communication3/index.html" target="_blank">Communication3</a> is a communication version of the button race in the <a href="resources/site/demos/state/index.html" target="_blank">State</a> demo.</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 9/10</div>
<h3>Styling</h3>
<p>Waiter, there's CSS in my HTML in my JS!</p>
</div>
</section>
<section class="slide" data-pos="5-9-1">
<span class="pos">5-9-1</span>
<div class="slidecontent"><p>Not only does React make you mix <strong>JS</strong> and <strong>HTML</strong>, it also gives you an opportunity to throw <strong>CSS</strong> into the same messy mix!</p>
</div></section>
<section class="slide" data-pos="5-9-2">
<span class="pos">5-9-2</span>
<div class="slidecontent"><p>The motivation is the same as before - <strong>separating technologies</strong> is <strong>not the same</strong> as <strong>separating concerns</strong>.</p>
<p>If some <strong>styles</strong> are <strong>only of concern to the component</strong>, then they <strong>belong inside the component definition</strong>.</p>
</div></section>
<section class="slide" data-pos="5-9-3">
<span class="pos">5-9-3</span>
<div class="slidecontent"><p>The styles are applied through giving a <strong>style object</strong> to the <strong><code>style</code></strong> prop of a tag:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> myStyles = {
<span class="hljs-attr">backgroundColor</span>: <span class="hljs-string">'blue'</span>,