-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter12.html
889 lines (729 loc) · 47.6 KB
/
chapter12.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Learning React - Advanced React</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 12 of 14 </div>
<h1>Advanced React</h1>
<span>The final final boss</span>
</div>
</section>
<section class="slide" data-pos="12-0-1">
<span class="pos">12-0-1</span>
<div class="slidecontent"><p>As a <strong>final challenge</strong>, let's look at <strong>leveling up our React fu</strong> even further!</p>
</div></section>
<section class="slide sectionlist">
<div class="slidecontent">
<h3>Sections in this chapter</h3>
<ol>
<li><a href="#/3">setState take 2</a></li>
<li><a href="#/4">The React Context API</a></li>
<li><a href="#/5">Context in React‑Redux</a></li>
<li><a href="#/6">Firebase</a></li>
<li><a href="#/7">Recompose</a></li>
<li><a href="#/8">Isomorphic apps</a></li>
<li><a href="#/9">Performance</a></li>
<li><a href="#/10">Working with the DOM</a></li>
</ol>
</div>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 1/8</div>
<h3>setState take 2</h3>
<p>First blood 2</p>
</div>
</section>
<section class="slide" data-pos="12-1-1">
<span class="pos">12-1-1</span>
<div class="slidecontent"><p>There's something we didn't tell you about <code>setState</code>; React will <strong>batch all calls</strong> that happen in the same tick.</p>
<p>In other words, <strong><code>setState</code> is asynchronous</strong>.</p>
</div></section>
<section class="slide question" data-pos="12-1-2">
<span class="pos">12-1-2</span>
<div class="slidecontent">
<p>In other words - <strong>what will this handler do</strong>?</p>
<pre><code class="lang-javascript">increaseTwice() {
<span class="hljs-keyword">this</span>.setState({<span class="hljs-attr">counter</span>: <span class="hljs-keyword">this</span>.state.counter + <span class="hljs-number">1</span>});
<span class="hljs-keyword">this</span>.setState({<span class="hljs-attr">counter</span>: <span class="hljs-keyword">this</span>.state.counter + <span class="hljs-number">1</span>});
}
</code></pre>
</div></section>
<section class="slide answer" data-pos="12-1-3">
<span class="pos">12-1-3</span>
<div class="slidecontent">
<p>It will actually just <strong>increase by 1</strong>.</p>
<p>See it happen in the <a href="resources/site/demos/statefail/index.html" target="_blank">StateFail</a> demo.</p>
</div></section>
<section class="slide list" data-pos="12-1-4">
<span class="pos">12-1-4</span>
<div class="slidecontent">
<p>So, how can we <strong>safely make several state update calls in the same tick</strong>?</p>
<p>The <code>setState</code> method offers <strong>two special syntaxes for this</strong>:</p>
<ul>
<li><span>a</span>callback</li>
<li><span>b</span>transactional</li>
</ul>
</div></section>
<section class="slide num numA" data-pos="12-1-5">
<span class="pos">12-1-5</span>
<div class="slidecontent">
<p>We can <strong>pass a callback as a second argument</strong>. This will only be invoked <strong>after the update is complete</strong>:</p>
<pre><code class="lang-javascript">increaseTwice() {
<span class="hljs-keyword">this</span>.setState({<span class="hljs-attr">counter</span>: <span class="hljs-keyword">this</span>.state.counter + <span class="hljs-number">1</span>}, () => {
<span class="hljs-keyword">this</span>.setState({<span class="hljs-attr">counter</span>: <span class="hljs-keyword">this</span>.state.counter + <span class="hljs-number">1</span>});
});
}
</code></pre>
<p>We use this in the <a href="resources/site/demos/statefailfixcallback/index.html" target="_blank">StateFailFixCallback</a> demo.</p>
</div></section>
<section class="slide num numB" data-pos="12-1-6">
<span class="pos">12-1-6</span>
<div class="slidecontent">
<p>We can also provide a <strong>function instead of an object</strong>. That function will be <strong>passed the current state</strong>, and expected to <strong>return a change object</strong>:</p>
<pre><code class="lang-javascript">increaseTwice() {
<span class="hljs-keyword">this</span>.setState(<span class="hljs-function"><span class="hljs-params">state</span> =></span> ({<span class="hljs-attr">counter</span>: state.counter + <span class="hljs-number">1</span>}));
<span class="hljs-keyword">this</span>.setState(<span class="hljs-function"><span class="hljs-params">state</span> =></span> ({<span class="hljs-attr">counter</span>: state.counter + <span class="hljs-number">1</span>}));
}
</code></pre>
</div></section>
<section class="slide" data-pos="12-1-7">
<span class="pos">12-1-7</span>
<div class="slidecontent"><p>Even though the calls are batched together and performed in the same tick, the function will be passed the <strong>previously updated state</strong> and it will work as expected.</p>
<p>See it happen in the <a href="resources/site/demos/statefailfixfunc/index.html" target="_blank">StateFailFixFunc</a> demo.</p>
</div></section>
<section class="slide" data-pos="12-1-8">
<span class="pos">12-1-8</span>
<div class="slidecontent"><p>This last pattern is frequently used for <strong>transactional updates to a database</strong>, from which you might recognize it.</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 2/8</div>
<h3>The React Context API</h3>
<p>low-level globality</p>
</div>
</section>
<section class="slide" data-pos="12-2-1">
<span class="pos">12-2-1</span>
<div class="slidecontent"><p>Remember how we first said that a <strong>React component UI</strong> was the <strong>result of its props</strong>...</p>
<p><img src="resources/12-2-1-93.svg" alt="dot"></p>
</div></section>
<section class="slide" data-pos="12-2-2">
<span class="pos">12-2-2</span>
<div class="slidecontent"><p>...and then admitted that we lied, and that the UI is in fact derived from props <strong>and state</strong>?</p>
<p><img src="resources/12-2-2-97.svg" alt="dot"></p>
</div></section>
<section class="slide" data-pos="12-2-3">
<span class="pos">12-2-3</span>
<div class="slidecontent"><p>That was actually a lie too - the UI is derive from props, state <strong>and context</strong>:</p>
<p><img src="resources/12-2-3-83.svg" alt="dot"></p>
</div></section>
<section class="slide" data-pos="12-2-4">
<span class="pos">12-2-4</span>
<div class="slidecontent"><p>So here's the proper <strong>signatures for the life cycle methods</strong>:</p>
<pre><code class="lang-javascript">componentWillReceiveProps(nextProps, nextContext)
shouldComponentUpdate(nextProps, nextState, nextContext)
componentWillUpdate(nextProps, nextState, nextContext)
componentDidUpdate(prevProps, prevState, prevContext)
</code></pre>
</div></section>
<section class="slide" data-pos="12-2-5">
<span class="pos">12-2-5</span>
<div class="slidecontent"><p>There are <strong>very few times</strong> when you actually <strong>need to use context</strong>. It corresponds to <strong>global state</strong>, which is mostly a <strong>bad idea</strong>.</p>
<p>A hint to that is that up until recently the <strong>API wasn't documented</strong> at all!</p>
</div></section>
<section class="slide" data-pos="12-2-6">
<span class="pos">12-2-6</span>
<div class="slidecontent"><p>So, <strong>what then is the point</strong> of context?</p>
<p>Context is mostly <strong>used in libraries</strong>. Later we'll peek at how <code>React-Redux</code> uses context to do their magic, but first we should explore the API!</p>
</div></section>
<section class="slide" data-pos="12-2-7">
<span class="pos">12-2-7</span>
<div class="slidecontent"><p>Ok, enough of the why - on to the how! What does the <strong>API</strong> for Context look like?</p>
</div></section>
<section class="slide list" data-pos="12-2-8">
<span class="pos">12-2-8</span>
<div class="slidecontent">
<p>The <strong>concept behind Context</strong> builds on the idea of <strong>providers</strong> and <strong>consumers</strong>:</p>
<ul>
<li><span>a</span>A <strong>provider</strong> component can <strong>provide</strong> context...</li>
<li><span>b</span>which <strong>consumer</strong> components positioned <strong>further up the tree</strong> can <strong>consume</strong>.</li>
</ul>
<p>Thus providers are often found at the <strong>root</strong> of the app.</p>
</div></section>
<section class="slide num numA" data-pos="12-2-9">
<span class="pos">12-2-9</span>
<div class="slidecontent">
<p>We'll first look at how to <strong>define a Provider</strong>. To provide context<strong> to downstream components, a Provider must do two things</strong>:</p>
<ul>
<li>Define the <strong>shape of the context</strong> in <code>childContextTypes</code>, using the same syntax as for <code>PropTypes</code> definitions.</li>
<li>Define a <code>getChildContext</code> method which should <strong>return a context matching that shape</strong>.</li>
</ul>
</div></section>
<section class="slide" data-pos="12-2-10">
<span class="pos">12-2-10</span>
<div class="slidecontent"><p>Here is <strong>an example Provider</strong> which will expose a <code>title</code> to all children as <code>this.context.title</code>:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ContextProvider</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> get childContextTypes() {
<span class="hljs-keyword">return</span> {
<span class="hljs-attr">title</span>: React.PropTypes.string
};
}
getChildContext() {
<span class="hljs-keyword">return</span> {<span class="hljs-attr">title</span>: <span class="hljs-keyword">this</span>.props.headline};
}
render(){
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.props.children;
}
}
</code></pre>
</div></section>
<section class="slide" data-pos="12-2-11">
<span class="pos">12-2-11</span>
<div class="slidecontent"><p>The <strong><code>childContextTypes</code> definition object</strong> is served as a <strong>static property</strong>, exactly like <code>propTypes</code> and <code>defaultProps</code>.</p>
</div></section>
<section class="slide num numB" data-pos="12-2-12">
<span class="pos">12-2-12</span>
<div class="slidecontent">
<p>And now the <strong>Consumers</strong>! Since we <strong>don't want to spam every component</strong> with context provided by upstream providers, Consumers have to <strong>actively opt in</strong> to gain access to the context data.</p>
<p>This is done through defining <code>contextTypes</code>, again in the same way as we did for <code>propTypes</code>.</p>
</div></section>
<section class="slide" data-pos="12-2-13">
<span class="pos">12-2-13</span>
<div class="slidecontent"><p>Here is an <strong>example Consumer</strong> to catch the context from our previous Provider:</p>
<pre><code class="lang-javascript">let ContextConsumer = (props, context) => <h4>{context.title}</h4>;
ContextConsumer.contextTypes = {
title: React.PropTypes.string
};
</code></pre>
</div></section>
<section class="slide" data-pos="12-2-14">
<span class="pos">12-2-14</span>
<div class="slidecontent"><p>Our example components could be rendered something like this:</p>
<pre><code><span class="hljs-tag"><<span class="hljs-name">Provider</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"Of global importance"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">Consumer</span>/></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"></<span class="hljs-name">Provider</span>></span>,
</code></pre><p>The important thing is that the <strong>Consumer must be positioned inside the Provider</strong>.</p>
</div></section>
<section class="slide" data-pos="12-2-15">
<span class="pos">12-2-15</span>
<div class="slidecontent"><p>You can <strong>play around</strong> with the above example in the
<a href="resources/site/demos/context/index.html" target="_blank">Context</a> demo.</p>
</div></section>
<section class="slide" data-pos="12-2-16">
<span class="pos">12-2-16</span>
<div class="slidecontent"><p>More details are in the <strong>official documentation</strong> for the Context API:
<a href="https://facebook.github.io/react/docs/context.html" class="link" target="_blank">Context</a></p>
</div></section>
<section class="slide" data-pos="12-2-17">
<span class="pos">12-2-17</span>
<div class="slidecontent"><p>As said before, <strong>Context is a bit experimental</strong> (even though it is now officially documented).</p>
<p>See this <a href="https://medium.com/react-ecosystem/how-to-handle-react-context-a7592dfdcbc" class="link" target="_blank">Context blog post</a> for a closer look as to <strong>why context is dangerous</strong>.</p>
</div></section>
<section class="slide" data-pos="12-2-18">
<span class="pos">12-2-18</span>
<div class="slidecontent"><p>A final note; context is an <strong>excellent place for style objects</strong> if you are using inline styles as in the <a href="resources/site/demos/style/index.html" target="_blank">Style</a> demo!</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 3/8</div>
<h3>Context in React-Redux</h3>
<p>A real-life case study</p>
</div>
</section>
<section class="slide" data-pos="12-3-1">
<span class="pos">12-3-1</span>
<div class="slidecontent"><p>We'll now walk through implementing a <strong>home-made version of React-Redux</strong>, in order to understand how it works.</p>
<p>In other words, we'll make our own <strong><code>Provider</code> component</strong> and <strong><code>connect</code> function</strong>!</p>
</div></section>
<section class="slide" data-pos="12-3-2">
<span class="pos">12-3-2</span>
<div class="slidecontent"><p>As you might suspect, the top-level <strong>Provider</strong> that we fed the <strong>store</strong> simply <strong>exposes this to the context</strong>:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Provider</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> get childContextTypes(){
<span class="hljs-keyword">return</span> { <span class="hljs-attr">store</span>: React.PropTypes.object };
}
getChildContext(){
<span class="hljs-keyword">return</span> { <span class="hljs-attr">store</span>: <span class="hljs-keyword">this</span>.props.store };
}
render(){
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.props.children;
}
}
</code></pre>
</div></section>
<section class="slide" data-pos="12-3-3">
<span class="pos">12-3-3</span>
<div class="slidecontent"><p>That was easy enough - <code>connect</code> will be slightly harder!</p>
</div></section>
<section class="slide" data-pos="12-3-4">
<span class="pos">12-3-4</span>
<div class="slidecontent"><p>Remember;</p>
<ol>
<li>the <strong>connect function</strong> is called with <code>mapState</code>, <code>mapDispatch</code> and <code>bakeProps</code>.</li>
<li>That <strong>returns</strong> a <strong>wrapper function</strong> which is called with the component we want to wrap...</li>
<li>...which finally <strong>returns</strong> the generated <strong>container class</strong> which will be called with some props from the parent.</li>
</ol>
</div></section>
<section class="slide" data-pos="12-3-5">
<span class="pos">12-3-5</span>
<div class="slidecontent"><p>Here's a <strong>diagram</strong> of this flow:</p>
<p><img src="resources/12-3-5-36.svg" alt="dot"></p>
</div></section>
<section class="slide" data-pos="12-3-6">
<span class="pos">12-3-6</span>
<div class="slidecontent"><p>Here's the <strong>same chain in code format</strong>:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> connect = <span class="hljs-function">(<span class="hljs-params">mapState,mapDispatch,bake</span>) =></span> Component => <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Container</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
<span class="hljs-comment">// container definition here</span>
}
</code></pre>
<p>Now we'll walk through how we define <code>Container</code>!</p>
</div></section>
<section class="slide list" data-pos="12-3-7">
<span class="pos">12-3-7</span>
<div class="slidecontent">
<p>We need to do a <strong>number of things</strong> in there;</p>
<ul>
<li><span>a</span>catch <code>store</code> from the context</li>
<li><span>b</span>subscribe to store data</li>
<li><span>c</span>render the child</li>
</ul>
</div></section>
<section class="slide num numA" data-pos="12-3-8">
<span class="pos">12-3-8</span>
<div class="slidecontent">
<p>First off, the definition will <strong>use the Context API</strong> to fetch a <strong>reference to the store</strong> passed down from the <strong>Provider</strong>: </p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Container</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> get contextTypes: { <span class="hljs-attr">store</span>: React.PropTypes.object },
<span class="hljs-comment">// rest of definition can now access `this.context.store`</span>
}
</code></pre>
</div></section>
<section class="slide" data-pos="12-3-9">
<span class="pos">12-3-9</span>
<div class="slidecontent"><p>Side note; you've now seen <strong>all Context-related code</strong>. But we'll follow through anyway since that will help <strong>dispel eventual magic</strong> and <strong>further our understanding of React itself</strong>!</p>
</div></section>
<section class="slide num numB" data-pos="12-3-10">
<span class="pos">12-3-10</span>
<div class="slidecontent">
<p>We'll use the <strong>same approach</strong> as in our <strong>vanilla Redux integration</strong> - we make our wrapper <strong>save the store data in its state</strong> every time the store is updated:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Container</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">childprops</span>:{}};
}
componentDidMount() {
<span class="hljs-keyword">let</span> store = <span class="hljs-keyword">this</span>.context.store;
<span class="hljs-keyword">let</span> callback = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{ ... }; <span class="hljs-comment">// <-- shown soon</span>
store.subscribe(callback);
callback();
}
<span class="hljs-comment">// rest removed</span>
}
</code></pre>
</div></section>
<section class="slide" data-pos="12-3-11">
<span class="pos">12-3-11</span>
<div class="slidecontent"><p>Here's the <strong>callback</strong> we run when the store updates!</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> callback = <span class="hljs-function"><span class="hljs-params">()</span> =></span> {
<span class="hljs-keyword">let</span> childprops = bake(
mapState(store.getState()),
mapDispatch(store.dispatch),
<span class="hljs-keyword">this</span>.props
);
<span class="hljs-keyword">this</span>.setState({<span class="hljs-attr">childprops</span>:childprops});
};
</code></pre>
<p>It simply <strong>bakes the new child props</strong> and <strong>saves them to state</strong>!</p>
</div></section>
<section class="slide num numC" data-pos="12-3-12">
<span class="pos">12-3-12</span>
<div class="slidecontent">
<p>Now we can simply <strong>pass on</strong> <code>this.state.childprops</code> as properties to 'Component' in the render method:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Container</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
<span class="hljs-comment">// ...rest removed ...</span>
render() {
<span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag"><<span class="hljs-name">Component</span> {<span class="hljs-attr">...this.state.childprops</span>} /></span>;
}
}</span>
</code></pre>
</div></section>
<section class="slide" data-pos="12-3-13">
<span class="pos">12-3-13</span>
<div class="slidecontent"><p>Now we've <strong>fully implemented React-Redux</strong>!</p>
<p>Well, except that we <strong>always</strong> expect <code>connect</code> to be <strong>called like this</strong>:</p>
<pre><code class="lang-javascript">connect(mapStateFunction,mapDispatchFunction,bakeFunction)(Component);
</code></pre>
</div></section>
<section class="slide" data-pos="12-3-14">
<span class="pos">12-3-14</span>
<div class="slidecontent"><p>You can <strong>try out</strong> this homemade React-Redux version applied to our old quotes app in the
<a href="resources/site/demos/homemade/index.html" target="_blank">Homemade</a> demo.</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 4/8</div>
<h3>Firebase</h3>
<p>Live updates from the cloud</p>
</div>
</section>
<section class="slide" data-pos="12-4-1">
<span class="pos">12-4-1</span>
<div class="slidecontent"><p><span class='freeimg'><img src="resources/images/firebase-logo.png" alt="firebase"></span></p>
<p><a href="https://firebase.google.com/" class="link" target="_blank">Firebase</a> is a <strong>cloud-based database</strong>. It stores data in a <strong>JSON format</strong> and offers <strong>realtime updates</strong>, which makes it an <strong>ideal match with React</strong>!</p>
</div></section>
<section class="slide" data-pos="12-4-2">
<span class="pos">12-4-2</span>
<div class="slidecontent"><p>The <strong>basic idea is simple</strong>: After having <strong>set up our database</strong> at the Firebase homepage, we use their web lib to <strong>initialize a local connection</strong>:</p>
<pre><code class="lang-javascript">firebase.initializeApp({
<span class="hljs-attr">apiKey</span>: <span class="hljs-string">"<mysecretkey>"</span>,
<span class="hljs-attr">databaseURL</span>: <span class="hljs-string">"https://<databasename>.firebaseio.com"</span>
});
</code></pre>
</div></section>
<section class="slide" data-pos="12-4-3">
<span class="pos">12-4-3</span>
<div class="slidecontent"><p>We then <strong>create a reference</strong> to a spot in our database. Given data that looks like this:</p>
<pre><code class="lang-json">{
blog: {
comments: {
// lots of stuff
}
},
// and below lots of other stuffs
}
</code></pre>
<p>...we could connect to <code>blog.comments</code> like this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> ref = firebase.database().ref(<span class="hljs-string">"blog/comments"</span>)
</code></pre>
</div></section>
<section class="slide" data-pos="12-4-4">
<span class="pos">12-4-4</span>
<div class="slidecontent"><p>With that reference we can <strong>add a change listener</strong>:</p>
<pre><code class="lang-javascript">DB.on(<span class="hljs-string">'value'</span>, snapshot => {
<span class="hljs-keyword">let</span> data = snapshot.val();
<span class="hljs-comment">// now do something with `data`</span>
});
</code></pre>
<p>Now our <strong>callback will be called whenever new data is pushed from the Firebase servers</strong>.</p>
</div></section>
<section class="slide" data-pos="12-4-5">
<span class="pos">12-4-5</span>
<div class="slidecontent"><p>We can also <strong>add new data</strong> at the reference point:</p>
<pre><code class="lang-javascript">ref.push({
<span class="hljs-attr">author</span>: <span class="hljs-string">'John Doe'</span>,
<span class="hljs-attr">msg</span>: <span class="hljs-string">'What trickery is this?!'</span>
});
</code></pre>
<p>And of course there are similar functionality for <strong>editing</strong> and <strong>deleting</strong>.</p>
</div></section>
<section class="slide" data-pos="12-4-6">
<span class="pos">12-4-6</span>
<div class="slidecontent"><p>As previously hinted, <strong>Firebase is a very good fit for React</strong>. To highlight this, see how easy it is to build a <strong>chat application</strong> in the <a href="resources/site/demos/firebase/index.html" target="_blank">Firebase</a> demo.</p>
<p>There is also a <strong>more advanced version</strong>, <a href="resources/site/demos/firebase2/index.html" target="_blank">Firebase2</a>, which lets you pick a username and chat in different rooms.</p>
</div></section>
<section class="slide" data-pos="12-4-7">
<span class="pos">12-4-7</span>
<div class="slidecontent"><p>A final warning: after <strong>Google bought Firebase</strong> in the <strong>spring of 2016</strong> there's been some <strong>major API updates</strong>, so be aware that material found online <strong>might be out of date</strong>.</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 5/8</div>
<h3>Recompose</h3>
<p>A React utility belt</p>
</div>
</section>
<section class="slide" data-pos="12-5-1">
<span class="pos">12-5-1</span>
<div class="slidecontent"><p><a href="https://facebook.github.io/react-native/" class="link" target="_blank">Recompose</a> is branded as a "lodash but for React".</p>
<p>The primary use case is to <strong>transform components defined as plain functions</strong>.</p>
</div></section>
<section class="slide" data-pos="12-5-2">
<span class="pos">12-5-2</span>
<div class="slidecontent"><p>The end result is that you get <strong>advanced components</strong> that <strong>would normally require the <code>class</code></strong> syntax, but without abandoning the convenient plain functions.</p>
</div></section>
<section class="slide" data-pos="12-5-3">
<span class="pos">12-5-3</span>
<div class="slidecontent"><p>As an example, take a look at this <strong>simple Clicker demo app</strong>:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Clicker</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">3</span>};
<span class="hljs-keyword">this</span>.more = <span class="hljs-function"><span class="hljs-params">()</span> =></span> <span class="hljs-keyword">this</span>.setState({<span class="hljs-attr">count</span>: <span class="hljs-keyword">this</span>.state.count + <span class="hljs-number">1</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">p</span>></span>{this.state.count} bottles of beer on the wall<span class="hljs-tag"></<span class="hljs-name">p</span>></span>
<span class="hljs-tag"><<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.more}</span>></span>Buy more<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>
</div></section>
<section class="slide" data-pos="12-5-4">
<span class="pos">12-5-4</span>
<div class="slidecontent"><p>This can be <strong>expressed in Recompose</strong> like this instead:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> enhance = withState(<span class="hljs-string">'count'</span>, <span class="hljs-string">'more'</span>, <span class="hljs-number">3</span>);
<span class="hljs-keyword">let</span> Clicker = enhance(<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>
<span class="hljs-tag"><<span class="hljs-name">p</span>></span>{props.count} bottles of beer on the wall<span class="hljs-tag"></<span class="hljs-name">p</span>></span>
<span class="hljs-tag"><<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =></span> props.more(n => n + 1)}>Buy more<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>
</div></section>
<section class="slide" data-pos="12-5-5">
<span class="pos">12-5-5</span>
<div class="slidecontent"><p>As another example, let's say we have a <strong><code>User</code></strong> plain function component with a <strong>very expensive render</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>User: {props.name}<<span class="hljs-regexp">/div>;</span>
</code></pre>
</div></section>
<section class="slide" data-pos="12-5-6">
<span class="pos">12-5-6</span>
<div class="slidecontent"><p>Normally, to make sure <code>User</code> doesn't rerender when non-vital props are changed, we would have to use a class and implement <strong><code>shouldComponentUpdate</code></strong>.</p>
</div></section>
<section class="slide" data-pos="12-5-7">
<span class="pos">12-5-7</span>
<div class="slidecontent"><p>But using <strong>Recompose</strong> we can <strong>instead do this</strong>:</p>
<pre><code><span class="hljs-attribute">let PerformantUser</span> = Recompose.onlyUpdateForKeys([<span class="hljs-string">'name'</span>])(User);
</code></pre><p>Although the "double invocation" can look weird the <strong>functionality is clear</strong>, and we can keep the clean definition of <code>User</code>.</p>
</div></section>
<section class="slide" data-pos="12-5-8">
<span class="pos">12-5-8</span>
<div class="slidecontent"><p>The usefulness of Recompose can be discussed, but if nothing else, using it will <strong>improve your understanding</strong> for <strong>how components are defined</strong>!</p>
<p>There are some more examples in the <a href="resources/site/demos/recompose/index.html" target="_blank">Recompose</a> demo.</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 6/8</div>
<h3>Isomorphic apps</h3>
<p>because buzzword</p>
</div>
</section>
<section class="slide" data-pos="12-6-1">
<span class="pos">12-6-1</span>
<div class="slidecontent"><p>First off; by <strong>isomorphic apps</strong>, or <strong>universal apps</strong>, we mean apps that <strong>can be rendered outside the browser</strong>.</p>
<p>Specifically this often means <strong>rendering on the server</strong>, which means that the <strong>initial rendering is much faster</strong>.</p>
</div></section>
<section class="slide" data-pos="12-6-2">
<span class="pos">12-6-2</span>
<div class="slidecontent"><p>With React this is easy, since <strong>rendering outputs a virtual tree</strong> describing the UI. </p>
<p>This tree can then be <strong>digested by whatever system</strong>. So far we've usually used <strong>ReactDOM</strong>, which takes the virtual tree and created DOM nodes:</p>
<p><img src="resources/12-6-2-237.svg" alt="dot"></p>
</div></section>
<section class="slide" data-pos="12-6-3">
<span class="pos">12-6-3</span>
<div class="slidecontent"><p>But we can also use <strong>other rendering targets</strong>:</p>
<p><img src="resources/12-6-3-50.svg" alt="dot"></p>
<p>As you can see, <a href="https://facebook.github.io/react/docs/react-dom-server.html">ReactDOMServer</a> <strong>outputs a string</strong>, which makes it very useful on the server.</p>
</div></section>
<section class="slide" data-pos="12-6-4">
<span class="pos">12-6-4</span>
<div class="slidecontent"><p>Building on the idea of being able to <strong>render on the server</strong> is <a href="https://zeit.co/blog/next">NextJS</a>, a very interesting mini-framework for <strong>isomorphic apps</strong> built on top of React.</p>
<p>The creator, Guillermo Rauch, made a <strong><a href="https://www.youtube.com/watch?v=evaMpdSiZKk">good overview of NextJS</a></strong> at ReactConf 2017.</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 7/8</div>
<h3>Performance</h3>
<p>because the fastest app wins</p>
</div>
</section>
<section class="slide list" data-pos="12-7-1">
<span class="pos">12-7-1</span>
<div class="slidecontent">
<p>Let's go over some libraries and concepts that <strong>help improve React performance</strong>!</p>
<ul>
<li><span>a</span>the <strong><code>shouldComponentUpdate</code></strong> method</li>
<li><span>b</span>the <strong><code>React-Perimeter</code></strong> library</li>
<li><span>c</span>the <strong><code>React-virtualized</code></strong> library</li>
<li><span>d</span>the <strong><code>React Fiber</code></strong> rewrite</li>
<li><span>e</span>the <strong><code>React-Canvas</code></strong> library</li>
</ul>
</div></section>
<section class="slide num numA" data-pos="12-7-2">
<span class="pos">12-7-2</span>
<div class="slidecontent">
<p>We've already mentioned <strong><code>shouldComponentUpdate</code></strong> as a <strong>lifecycle method</strong>, which lets us <strong>short-circuit the rerender</strong>.</p>
<p>But we repeat it here since it is the <strong>single most important thing</strong> you can do to improve app performance.</p>
</div></section>
<section class="slide" data-pos="12-7-3">
<span class="pos">12-7-3</span>
<div class="slidecontent"><p>The logic behind when to short-circuit is <strong>very context specific</strong>, but React contains a ready-made <a href="https://facebook.github.io/react/docs/react-api.html#react.purecomponent">PureComponent class to inherit from</a> (or a <a href="https://facebook.github.io/react/docs/pure-render-mixin.html">PureRender mixin</a> if you're not using classes) that implements a <strong>good default</strong>: it <strong>returns false if state and props are the same</strong>.</p>
</div></section>
<section class="slide" data-pos="12-7-4">
<span class="pos">12-7-4</span>
<div class="slidecontent"><p>Note that the <strong>comparison is shallow</strong>, so for complex data structures we <strong>might need additional logic</strong>.</p>
<p>Also consider using something like <a href="https://facebook.github.io/immutable-js/">ImmutableJS</a> which <strong>helps make identical objects have the same reference</strong>.</p>
<p>This <strong>saves you from having to do recursive comparisons</strong>, and can be a significant performance boost.</p>
</div></section>
<section class="slide num numB" data-pos="12-7-5">
<span class="pos">12-7-5</span>
<div class="slidecontent">
<p>The <a href="https://github.com/aweary/react-perimeter">React-Perimeter library</a> can further help us to increase perceived performance.</p>
<p>It lets us <strong>execute logic</strong> when the <strong>cursor breaches the perimeter</strong> of a given component.</p>
<p>This <strong>can be used for a lot of things</strong>, but it is <strong>especially interesting for performance enhancements</strong>.</p>
</div></section>
<section class="slide" data-pos="12-7-6">
<span class="pos">12-7-6</span>
<div class="slidecontent"><p>Here's a <strong>simple example</strong> from the official page:</p>
<pre><code><span class="hljs-keyword">import</span> perimeter <span class="hljs-keyword">from</span> <span class="hljs-string">'react-perimeter'</span>
<span class="hljs-keyword">const</span> LoadMoreButton = (
<span class="xml"><span class="hljs-tag"><<span class="hljs-name">Perimeter</span>
<span class="hljs-attr">onBreach</span>=<span class="hljs-string">{this.prefetch}</span>
<span class="hljs-attr">padding</span>=<span class="hljs-string">{80}</span>></span>
<span class="hljs-tag"><<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.fetch}</span>></span>Load more<span class="hljs-tag"></<span class="hljs-name">button</span>></span>
<span class="hljs-tag"></<span class="hljs-name">Perimeter</span>></span></span>
);
</code></pre></div></section>
<section class="slide" data-pos="12-7-7">
<span class="pos">12-7-7</span>
<div class="slidecontent"><p>There is also a <a href="resources/site/demos/perimeter/index.html" target="_blank">Perimeter</a> demo.</p>
<p>Do note how <code>React-Perimeter</code> is a <strong>prime example of the power of the component abstraction</strong>!</p>
</div></section>
<section class="slide num numC" data-pos="12-7-8">
<span class="pos">12-7-8</span>
<div class="slidecontent">
<p>The <a href="https://github.com/bvaughn/react-virtualized"><code>React-Virtualized</code> library</a> contains <strong>ready-made components</strong> for dealing with <strong>huge lists</strong>, which is a common performance bottleneck.</p>
<p>It accomplishes this through <strong>windowing</strong>, in essence not rendering stuff that isn't on screen.</p>
</div></section>
<section class="slide num numD" data-pos="12-7-9">
<span class="pos">12-7-9</span>
<div class="slidecontent">
<p>For React v16, the team did a <strong>complete rewrite of the core reconciliation engine</strong>. This rewrite was called <strong><code>React Fiber</code></strong>, and is in essence an <strong>implementation of the call stack in a render sense</strong>.</p>
<p>We don't have to change a thing to capitalize on this - just by <strong>upgrading to v16</strong>, everything will be slightly faster!</p>
</div></section>
<section class="slide" data-pos="12-7-10">
<span class="pos">12-7-10</span>
<div class="slidecontent"><p>The inner workings of the Fiber rewrite are rather fascinating - for an entrypoint, see <a href="https://www.youtube.com/watch?v=ZCuYPiUIONs&t=6s">Lin Clark's talk</a> from ReactConf 2017.</p>
</div></section>
<section class="slide num numE" data-pos="12-7-11">
<span class="pos">12-7-11</span>
<div class="slidecontent">
<p>The <a href="https://github.com/Flipboard/react-canvas">React-Canvas</a> library is really just <strong>another rendering target</strong>, much like React-SVG and the others we mentioned previously.</p>
</div></section>
<section class="slide" data-pos="12-7-12">
<span class="pos">12-7-12</span>
<div class="slidecontent"><p>However, for <strong>complex components</strong>, using a canvas element can mean a <strong>performance boost</strong>. </p>
<p>This is not an app-wide decision - you can <strong>render a particularly heavy component onto a canvas</strong>, but leave the surrounding app as a regular DOM app.</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 8/8</div>
<h3>Working with the DOM</h3>
<p>DOM DOM DOM</p>
</div>
</section>
<section class="slide" data-pos="12-8-1">
<span class="pos">12-8-1</span>
<div class="slidecontent"><p>We've already touched on it several times during the course: since <strong>React wants ownership of the DOM</strong>, it can be awkward when <strong>others want to play with the DOM too</strong>.</p>
</div></section>
<section class="slide" data-pos="12-8-2">
<span class="pos">12-8-2</span>
<div class="slidecontent"><p>This can be <strong>especially problematic</strong> when you want to <strong>include DOM-operating libraries in a React app</strong>.</p>
<p>For example, the <a href="https://d3js.org/">D3 animation library</a>, which expects to be fed a DOM element to render the beautiful graphs into. How do we fit this into React?</p>
</div></section>
<section class="slide" data-pos="12-8-3">
<span class="pos">12-8-3</span>
<div class="slidecontent"><p>A good, general case solution is to use <a href="https://github.com/Olical/react-faux-dom">React-faux-DOM</a>. It <strong>creates a faux DOM element</strong> which you can give to a DOM manipulation library.</p>
<p><img src="resources/12-8-3-185.svg" alt="dot"></p>
<p>The fake node has a <code>.toReact</code> method which <strong>converts it to Virtual DOM</strong>, ready to be rendered by React!</p>
</div></section>
</section>
</div>
</div>
<script type="text/javascript">
var basehref = window.location.href.replace(/chapter\d.*?$/,'')
document.addEventListener("keydown", function(e) {
var code = e.which || e.keyCode;
if (code === 13) {
window.location.href = basehref+"index.html?from=11";
} else if (code >= 49 && code <= 8 + 48) {
window.location.hash = "#/" + (code-48+1+1);
}
if (12 < 14) {
if (code === 99 || e.key === 'c') { // the letter C for next Chapter
window.location.href = basehref+"chapter13.html"
}
}
});
</script>
<script src="mc2/scripts/head.js" type="text/javascript"></script>
<script src="mc2/scripts/reveal.js" type="text/javascript"></script>
<script src="mc2/scripts/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
showNotes: false,
transition: 'slide',
dependencies: [
{ src: 'mc2/scripts/plugin/notes/notes.js', async: true }
]
});
window.onload = function() {
var links = document.querySelectorAll("a.link");
for(var i = 0; i < links.length; i++){
var link = links[i];
link.innerHTML = link.innerHTML.replace(/ /g,' ')
}
var posElem = document.getElementById('pos')
function updateReference() {
setTimeout(function() {
var currentpos = document.querySelector('section.present[data-pos]')
if (currentpos) {
posElem.innerHTML = currentpos.getAttribute('data-pos')
} else {
posElem.innerHTML = ''
}
if (document.querySelector('.present.chaptertitle')) {
document.body.classList.add('atchaptertitle');
} else {
document.body.classList.remove('atchaptertitle');
}
}, 10);
}
window.addEventListener("hashchange",updateReference);
updateReference();
};
</script>
</body>
</html>