-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter8.html
More file actions
672 lines (550 loc) · 31.8 KB
/
chapter8.html
File metadata and controls
672 lines (550 loc) · 31.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Learning React - React and Redux</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 8 of 14 </div>
<h1>React and Redux</h1>
<span>sitting in a tree</span>
</div>
</section>
<section class="slide" data-pos="8-0-1">
<span class="pos">8-0-1</span>
<div class="slidecontent"><p>We have looked at <strong>React</strong> and <strong>Redux</strong> in <strong>isolation</strong>. The time has now come to <strong>marry the two</strong>!</p>
</div></section>
<section class="slide sectionlist">
<div class="slidecontent">
<h3>Sections in this chapter</h3>
<ol>
<li><a href="#/3">Presenting the app</a></li>
<li><a href="#/4">Redux parts</a></li>
<li><a href="#/5">React parts</a></li>
<li><a href="#/6">Vanilla integration</a></li>
<li><a href="#/7">Examining React‑Redux</a></li>
<li><a href="#/8">React‑Redux integration</a></li>
<li><a href="#/9">Exercise 5</a></li>
</ol>
</div>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 1/7</div>
<h3>Presenting the app</h3>
<p>Prepare to be dazzled!</p>
</div>
</section>
<section class="slide" data-pos="8-1-1">
<span class="pos">8-1-1</span>
<div class="slidecontent"><p>Here's what we're going to build:</p>
<p><img src="resources/images/reactredux-example-screen.png" alt="screen"></p>
</div></section>
<section class="slide" data-pos="8-1-2">
<span class="pos">8-1-2</span>
<div class="slidecontent"><p>The functionality is very simple; there is a <strong>list of quotes</strong>, and the user can also <strong>add new quotes</strong>.</p>
</div></section>
<section class="slide" data-pos="8-1-3">
<span class="pos">8-1-3</span>
<div class="slidecontent"><p>In the upcoming sections we will...</p>
<ul>
<li>first build the <strong>Redux parts</strong>,</li>
<li>then the <strong>React parts</strong>, and finally we'll</li>
<li>look at how to <strong>merge the two together</strong>.</li>
</ul>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 2/7</div>
<h3>Redux parts</h3>
<p>Installing the plumbing</p>
</div>
</section>
<section class="slide" data-pos="8-2-1">
<span class="pos">8-2-1</span>
<div class="slidecontent"><p>Our <strong>app state</strong> object has a single <strong><code>quotes</code></strong> key, will contain an <strong>array of strings</strong>.</p>
<p>Let's make our <strong>initial state</strong> prepopulated with a quote to set the tone:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> initial = {
<span class="hljs-attr">quotes</span>: [<span class="hljs-string">'Carpe diem'</span>]
};
</code></pre>
</div></section>
<section class="slide" data-pos="8-2-2">
<span class="pos">8-2-2</span>
<div class="slidecontent"><p>The single <strong>action</strong> that can happen, <strong>adding a quote</strong>, should look like this:</p>
<pre><code class="lang-javascript">{
<span class="hljs-attr">type</span>: <span class="hljs-string">'ADD'</span>,
<span class="hljs-attr">text</span>: <span class="hljs-string">'Do unto others etc etc'</span>
}
</code></pre>
</div></section>
<section class="slide" data-pos="8-2-3">
<span class="pos">8-2-3</span>
<div class="slidecontent"><p>Here's the <strong>reducer</strong> to handle this setup:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> reducer = <span class="hljs-function">(<span class="hljs-params">state,action</span>)=></span> {
<span class="hljs-keyword">switch</span>(action.type){
<span class="hljs-keyword">case</span> <span class="hljs-string">'ADD'</span>: <span class="hljs-keyword">return</span> {
...state, <span class="hljs-comment">// only needed if we had other keys in the state</span>
quotes: [...state.quotes, action.text]
};
<span class="hljs-keyword">default</span>: <span class="hljs-keyword">return</span> state;
}
};
</code></pre>
</div></section>
<section class="slide" data-pos="8-2-4">
<span class="pos">8-2-4</span>
<div class="slidecontent"><p>We <strong>instantiate the store</strong> as per usual:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> store = Redux.createStore(reducer,initial);
</code></pre>
</div></section>
<section class="slide" data-pos="8-2-5">
<span class="pos">8-2-5</span>
<div class="slidecontent"><p>We only need <strong>one action creator</strong> to interact with our store:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> actionCreators = {
addQuote(text) {
<span class="hljs-keyword">return</span> {<span class="hljs-attr">type</span>:<span class="hljs-string">'ADD'</span>,<span class="hljs-attr">text</span>:text};
}
};
</code></pre>
</div></section>
<section class="slide" data-pos="8-2-6">
<span class="pos">8-2-6</span>
<div class="slidecontent"><p>Now <strong>all the Redux parts</strong> needed to support our functionality <strong>are in place</strong>!</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 3/7</div>
<h3>React parts</h3>
<p>Dressing the doll</p>
</div>
</section>
<section class="slide list" data-pos="8-3-1">
<span class="pos">8-3-1</span>
<div class="slidecontent">
<p>Let's say your team has prepared <strong>a couple of React components</strong> for the app:</p>
<ul>
<li><span>a</span><code>QuoteList</code> to <strong>display the quotes</strong></li>
<li><span>b</span><code>QuoteForm</code> to hold the <strong>form for new quotes</strong></li>
</ul>
</div></section>
<section class="slide" data-pos="8-3-2">
<span class="pos">8-3-2</span>
<div class="slidecontent"><p>Because they didn't know what data handling solution the app would use, they've made the components <strong>portable</strong>.</p>
<p>This of course is <strong>good practice</strong> in any scenario, as it makes the code <strong>easier to test</strong> and <strong>less coupled</strong>.</p>
</div></section>
<section class="slide num numA" data-pos="8-3-3">
<span class="pos">8-3-3</span>
<div class="slidecontent">
<p>Let's check out <strong><code>QuoteList</code></strong>, responsible for <strong>rendering the list of quotes</strong>.</p>
<p>It <strong>expects to receive an array of quotes</strong> as a property.</p>
<p><img src="resources/8-3-3-152.svg" alt="dot"></p>
</div></section>
<section class="slide" data-pos="8-3-4">
<span class="pos">8-3-4</span>
<div class="slidecontent"><p>Here's the <strong>code</strong> for the <code>QuoteList</code> component:</p>
<pre><code class="lang-javascript">let QuoteList = props => {
let list = props.quotes.map((q,n) => <li key={n}>{q}</li>);
return (
<div className="quoteslist">
<h3>Words of Wisdom</h3>
<ul>{list}</ul>
</div>
);
};
</code></pre>
</div></section>
<section class="slide num numB" data-pos="8-3-5">
<span class="pos">8-3-5</span>
<div class="slidecontent">
<p>And then we have <strong><code>QuoteForm</code></strong>, which <strong>renders the form</strong> where the user enters a new quote.</p>
<p>This components <strong>expects to receive a callback</strong> which will be invoked with the text of the new quote when the user clicks the button.</p>
<p><img src="resources/8-3-5-241.svg" alt="dot"></p>
</div></section>
<section class="slide" data-pos="8-3-6">
<span class="pos">8-3-6</span>
<div class="slidecontent"><p>Here's the source code:</p>
<pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">QuoteForm</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
submit(e){
<span class="hljs-keyword">this</span>.props.callback(<span class="hljs-keyword">this</span>.field.value);
<span class="hljs-keyword">this</span>.field.value = <span class="hljs-string">''</span>;
e.preventDefault();
}
render(){
<span class="hljs-keyword">return</span> (
<span class="xml"><span class="hljs-tag"><<span class="hljs-name">form</span> <span class="hljs-attr">onSubmit</span>=<span class="hljs-string">{e</span>=></span> this.submit(e)}>
<span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">{i</span>=></span> this.field = i} />
<span class="hljs-tag"><<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'submit'</span>></span>Add<span class="hljs-tag"></<span class="hljs-name">button</span>></span>
<span class="hljs-tag"></<span class="hljs-name">form</span>></span>
);
}
}</span>
</code></pre>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 4/7</div>
<h3>Vanilla integration</h3>
<p>Marriage attempt, take 1</p>
</div>
</section>
<section class="slide list" data-pos="8-4-1">
<span class="pos">8-4-1</span>
<div class="slidecontent">
<p>It is customary to create <strong>container components</strong> around <strong>portable components</strong> like we have now, in order to <strong>provide them what they need</strong>. So let's do that for our two components:</p>
<ul>
<li><span>a</span>QuoteListContainer</li>
<li><span>b</span>QuoteFormContainer</li>
</ul>
</div></section>
<section class="slide num numA" data-pos="8-4-2">
<span class="pos">8-4-2</span>
<div class="slidecontent">
<p>First we have <code>QuoteList</code>, which expects to be given an <strong>array of quotes</strong> as a prop. Since we must also <strong>update when the data changes</strong>, we need to create a component that <strong>has Redux data as state</strong>.</p>
</div></section>
<section class="slide" data-pos="8-4-3">
<span class="pos">8-4-3</span>
<div class="slidecontent"><pre><code class="lang-javascript"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">QuoteListContainer</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">quotes</span>:[]};
}
componentDidMount() {
store.subscribe(<span class="hljs-function"><span class="hljs-params">()</span>=></span>
<span class="hljs-keyword">this</span>.setState({<span class="hljs-attr">quotes</span>: store.getState().quotes})
);
}
render() {
<span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag"><<span class="hljs-name">QuoteList</span> <span class="hljs-attr">quotes</span>=<span class="hljs-string">{this.state.quotes}</span> /></span>;
}
}</span>
</code></pre>
</div></section>
<section class="slide" data-pos="8-4-4">
<span class="pos">8-4-4</span>
<div class="slidecontent"><p>In the <code>componentDidMount</code> lifecycle hook we <strong>initiate a store subscription</strong>, which will <strong>update the component state</strong> on every change.</p>
</div></section>
<section class="slide num numB" data-pos="8-4-5">
<span class="pos">8-4-5</span>
<div class="slidecontent">
<p>Moving on to <code>QuoteForm</code> - that component needs to be given a <strong>callback</strong> which it <strong>invokes with new quotes</strong>. This should of course be <strong>passed along to the store</strong>.</p>
</div></section>
<section class="slide" data-pos="8-4-6">
<span class="pos">8-4-6</span>
<div class="slidecontent"><p>First we create <strong>bound action creators</strong> for our component to use:</p>
<pre><code>let <span class="hljs-keyword">boundActionCreators </span>= Redux.<span class="hljs-keyword">bindActionCreators(
</span> actionCreators,
store.<span class="hljs-keyword">dispatch
</span>)<span class="hljs-comment">;</span>
</code></pre></div></section>
<section class="slide" data-pos="8-4-7">
<span class="pos">8-4-7</span>
<div class="slidecontent"><p>We then make a container that passes in the bound action creator:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> QuoteFormContainer = <span class="hljs-function"><span class="hljs-params">props</span> =></span> (
<span class="xml"><span class="hljs-tag"><<span class="hljs-name">QuoteForm</span> <span class="hljs-attr">callback</span>=<span class="hljs-string">{boundActionCreators.addQuote}/</span>></span>;
);</span>
</code></pre>
</div></section>
<section class="slide" data-pos="8-4-8">
<span class="pos">8-4-8</span>
<div class="slidecontent"><p><strong>Initialising this app</strong> is pretty straightforward:</p>
<pre><code>ReactDOM.render(
<span class="xml"><span class="hljs-tag"><<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">QuoteListContainer</span> /></span>
<span class="hljs-tag"><<span class="hljs-name">QuoteFormContainer</span> /></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span></span>,
<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"container"</span>)
);
store.dispatch({<span class="hljs-attr">type</span>:<span class="hljs-string">'BOGUSEVENT'</span>}); <span class="hljs-comment">// triggers initial render</span>
</code></pre><p>Try it in a demo here: <a href="resources/site/demos/vanilla/index.html" target="_blank">Vanilla</a></p>
</div></section>
<section class="slide" data-pos="8-4-9">
<span class="pos">8-4-9</span>
<div class="slidecontent"><p>This vanilla solution <strong>works just fine</strong>. The only downside is that we had to <strong>access our store to create our containers</strong>;</p>
<ul>
<li><strong><code>QuoteListContainer</code></strong> needed <strong><code>store.subscribe</code></strong> and <strong><code>store.getState</code></strong>.</li>
<li><strong><code>QuoteFormContainer</code></strong> needed <strong><code>store.dispatch</code></strong>.</li>
</ul>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 5/7</div>
<h3>Examining React-Redux</h3>
<p>Meeting the matchmaker</p>
</div>
</section>
<section class="slide" data-pos="8-5-1">
<span class="pos">8-5-1</span>
<div class="slidecontent"><p>The <a href="https://github.com/reactjs/react-redux" class="link" target="_blank">React-Redux</a> provide <strong>official bindings</strong> that <strong>generates wrapper components for us</strong> for us, saving us from having to create our own and spread around references to the store.</p>
</div></section>
<section class="slide" data-pos="8-5-2">
<span class="pos">8-5-2</span>
<div class="slidecontent"><p><code>React-Redux</code> works through a <strong><code>.connect</code></strong> method with the following signature:</p>
<pre><code class="lang-javascript">ReactRedux.connect(
mapStateToProps, <span class="hljs-comment">// connecting to store state</span>
mapDispatchToProps, <span class="hljs-comment">// connecting to store dispatch</span>
mergeProps <span class="hljs-comment">// baking all props together</span>
)(ComponentToWrap) <span class="hljs-comment">// the component to be wrapped</span>
</code></pre>
</div></section>
<section class="slide list" data-pos="8-5-3">
<span class="pos">8-5-3</span>
<div class="slidecontent">
<p>Let's <strong>explore the three parameters</strong> one at a time!</p>
<ul>
<li><span>a</span><code>mapStateToProps</code></li>
<li><span>b</span><code>mapDispatchToProps</code></li>
<li><span>c</span><code>mergeProps</code></li>
</ul>
</div></section>
<section class="slide num numA" data-pos="8-5-4">
<span class="pos">8-5-4</span>
<div class="slidecontent">
<p>First, <strong><code>mapStateToProps</code></strong>. It is a <strong>function</strong> which will be <strong>invoked with the store state</strong>, and what you <strong>return</strong> from the method will become <strong>additional props on the component</strong>.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> mapStateToProps = <span class="hljs-function"><span class="hljs-params">appstate</span> =></span> ({
<span class="hljs-attr">numberOfPosts</span>: appstate.posts.length
});
</code></pre>
</div></section>
<section class="slide num numB" data-pos="8-5-5">
<span class="pos">8-5-5</span>
<div class="slidecontent">
<p>Then <strong><code>mapDispatchToProps</code></strong> It can be an object or a function.</p>
<p>If it is an <strong>object</strong> it is assumed to <strong>contain action creators</strong>. They'll be made <strong>available as props on the component</strong> which will <strong>automatically pipe what they're returning to the store dispatch</strong>.</p>
</div></section>
<section class="slide" data-pos="8-5-6">
<span class="pos">8-5-6</span>
<div class="slidecontent"><p>If <code>mapDispatchToProps</code> is a <strong>function</strong> then it'll be <strong>invoked with the store dispatch</strong>, and you must manually return props just like with <code>mapStateToProps</code>.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> mapDispatchToProps = <span class="hljs-function"><span class="hljs-params">dispatch</span> =></span> ({
addPost(text) {
<span class="hljs-keyword">let</span> action = actionCreators.addPost(text);
dispatch(action);
}
})
</code></pre>
</div></section>
<section class="slide" data-pos="8-5-7">
<span class="pos">8-5-7</span>
<div class="slidecontent"><p>Note how <strong><code>mapDispatchToProps</code></strong> serves the exact <strong>same purpose</strong> as <strong><code>bindActionCreators</code></strong>, namely <strong>passing actions</strong> from the view to the store <strong>without</strong> needing a <strong>store reference</strong>!</p>
</div></section>
<section class="slide num numC" data-pos="8-5-8">
<span class="pos">8-5-8</span>
<div class="slidecontent">
<p>Finally the <strong><code>mergeProps</code></strong> function handles the fact that a connected component <strong>receives props from 3 sources</strong> which must somehow be <strong>baked together</strong>:</p>
<p><img src="resources/8-5-8-166.svg" alt="dot"></p>
</div></section>
<section class="slide" data-pos="8-5-9">
<span class="pos">8-5-9</span>
<div class="slidecontent"><p>If <strong><code>mergeProps</code> isn't supplied</strong> then <code>ReactRedux</code> will do the following <strong>by default</strong>:</p>
<pre><code class="lang-javascript">props = <span class="hljs-built_in">Object</span>.assign({}, parentProps, stateProps, dispatchProps)
</code></pre>
<p>For the majority of cases this is fine.</p>
</div></section>
<section class="slide" data-pos="8-5-10">
<span class="pos">8-5-10</span>
<div class="slidecontent"><p>But if you want control, <strong>provide <code>mergeProps</code></strong> and do your own baking. It is <strong>called with all three sources</strong> like this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> mergeProps = <span class="hljs-function">(<span class="hljs-params">fromState,fromDispatch,fromParent</span>)=></span> {
<span class="hljs-comment">// do your own baking and return it</span>
<span class="hljs-keyword">return</span> myBakedProps;
}
</code></pre>
</div></section>
<section class="slide" data-pos="8-5-11">
<span class="pos">8-5-11</span>
<div class="slidecontent"><p>The protocol of the <strong><code>.connect</code></strong> method and the mapping functions is actually <strong>more versatile</strong> than we have detailed here. See the <a href="https://github.com/reactjs/react-redux" class="link" target="_blank">React-Redux</a> homepage for the full details.</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 6/7</div>
<h3>React-Redux integration</h3>
<p>Employing the matchmaker</p>
</div>
</section>
<section class="slide list" data-pos="8-6-1">
<span class="pos">8-6-1</span>
<div class="slidecontent">
<p>Now we must again create...</p>
<ul>
<li><span>a</span><strong><code>QuoteListContainer</code></strong></li>
<li><span>b</span><strong><code>QuoteFormContainer</code></strong></li>
</ul>
</div></section>
<section class="slide num numA" data-pos="8-6-2">
<span class="pos">8-6-2</span>
<div class="slidecontent">
<p>We start with <strong>making the <code>QuoteListContainer</code></strong>.</p>
<p>Remember, <strong><code>QuoteList</code></strong> expected the array of <strong>quotes as a props</strong>, which must also be <strong>kept live</strong> as the data updates.</p>
<p>Thus we'll <strong>need to use the <code>mapStateToProps</code></strong> function.</p>
</div></section>
<section class="slide" data-pos="8-6-3">
<span class="pos">8-6-3</span>
<div class="slidecontent"><p>That function will be called with the <strong>entire state</strong>, and should return a props object containing the array of quotes:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> mapStateToQuoteListProps = <span class="hljs-function"><span class="hljs-params">appState</span> =></span> ({<span class="hljs-attr">quotes</span>: appState.quotes});
</code></pre>
</div></section>
<section class="slide" data-pos="8-6-4">
<span class="pos">8-6-4</span>
<div class="slidecontent"><p>Now we <strong>create the container</strong> using the <strong><code>.connect</code></strong> method:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> QuoteListContainer = ReactRedux.connect(
mapStateToQuoteListProps
<span class="hljs-comment">// we don't need `mapDispatch` or a custom `mergeProps`</span>
)(QuoteList);
</code></pre>
<p>This <code>QuoteListContainer</code> will <strong>act exactly like the vanilla version</strong>, including keeping the data updated.</p>
</div></section>
<section class="slide num numB" data-pos="8-6-5">
<span class="pos">8-6-5</span>
<div class="slidecontent">
<p>Now for <strong><code>QuoteFormContainer</code></strong>.</p>
<p>It doesn't need to map state, but it <strong>expects the <code>callback</code> prop</strong> to be a method whose <strong>return value should be <code>dispatch</code>ed</strong>.</p>
</div></section>
<section class="slide" data-pos="8-6-6">
<span class="pos">8-6-6</span>
<div class="slidecontent"><p>Thus it suffices to simply <strong>rename the <code>addQuote</code> action creator</strong> to "callback":</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> QuoteFormContainer = ReactRedux.connect(
<span class="hljs-literal">null</span>, <span class="hljs-comment">// don't need state</span>
{<span class="hljs-attr">callback</span>: actionCreators.addQuote}
)(QuoteForm);
</code></pre>
</div></section>
<section class="slide" data-pos="8-6-7">
<span class="pos">8-6-7</span>
<div class="slidecontent"><p>If we had been <strong>allowed to edit</strong> the code of <code>QuoteForm</code> to <strong>use <code>props.addQuote</code> instead</strong> of <code>props.callback</code>, then we could have <strong>passed in the actionCreators object</strong> directly:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> QuoteFormContainer = ReactRedux.connect(
<span class="hljs-literal">null</span>,
actionCreators
)(QuoteForm);
</code></pre>
</div></section>
<section class="slide" data-pos="8-6-8">
<span class="pos">8-6-8</span>
<div class="slidecontent"><p>But, wait - now there's <strong>no single reference to the store</strong> anywhere in our code. How does <code>.connect</code> <strong>actually connect</strong> our components to the store?</p>
</div></section>
<section class="slide" data-pos="8-6-9">
<span class="pos">8-6-9</span>
<div class="slidecontent"><p>Here's the <strong>final piece</strong> of the puzzle: we must <strong>wrap our entire app in a <code>Provider</code></strong> component which is <strong>given a store reference</strong>:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> Provider = ReactRedux.Provider;
ReactDOM.render(
<span class="xml"><span class="hljs-tag"><<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{store}</span>></span>
<span class="hljs-tag"><<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">QuoteListContainer</span> /></span>
<span class="hljs-tag"><<span class="hljs-name">QuoteFormContainer</span> /></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
<span class="hljs-tag"></<span class="hljs-name">Provider</span>></span></span>,
<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"container"</span>)
);
</code></pre>
</div></section>
<section class="slide" data-pos="8-6-10">
<span class="pos">8-6-10</span>
<div class="slidecontent"><p>Behind the scenes the Provider component uses something called <strong><code>Context</code></strong> to supply the store to interested children.</p>
<p>We'll <strong>take a closer look at <code>Context</code></strong> in the <strong>Advanced React</strong> chapter.</p>
</div></section>
<section class="slide" data-pos="8-6-11">
<span class="pos">8-6-11</span>
<div class="slidecontent"><p>Here's a demo with this <strong>library-helped integration</strong> version of the app:
<a href="resources/site/demos/usinglib/index.html" target="_blank">Usinglib</a></p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 7/7</div>
<h3>Exercise 5</h3>
<p>The final boss</p>
</div>
</section>
<section class="slide" data-pos="8-7-1">
<span class="pos">8-7-1</span>
<div class="slidecontent"><p>As the final test of your new 1337 skillz - <strong>return to the solution you made for exercise 4</strong>, and:</p>
<ul>
<li><strong>make it use React for UI</strong> instead of the jQuery/vanilla solution you have now</li>
<li><strong>use ReactRedux</strong> as a bridge between React and Redux!</li>
</ul>
</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=7";
} else if (code >= 49 && code <= 7 + 48) {
window.location.hash = "#/" + (code-48+1+1);
}
if (8 < 14) {
if (code === 99 || e.key === 'c') { // the letter C for next Chapter
window.location.href = basehref+"chapter9.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>