-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter11.html
466 lines (370 loc) · 21.6 KB
/
chapter11.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Learning React - Redux 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 11 of 14 </div>
<h1>Redux level 2</h1>
<span>Redux redux</span>
</div>
</section>
<section class="slide sectionlist">
<div class="slidecontent">
<h3>Sections in this chapter</h3>
<ol>
<li><a href="#/2">Redux devtools</a></li>
<li><a href="#/3">Combined reducers</a></li>
<li><a href="#/4">Caching with Reselect</a></li>
<li><a href="#/5">Where to put logic</a></li>
<li><a href="#/6">Flavours of state</a></li>
</ol>
</div>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 1/5</div>
<h3>Redux devtools</h3>
<p>Activating the flux capacitator</p>
</div>
</section>
<section class="slide" data-pos="11-1-1">
<span class="pos">11-1-1</span>
<div class="slidecontent"><p>We hinted earlier that <strong>the Redux model allows time travel</strong>, which can be a <strong>powerful debug tool</strong>.</p>
<p>This is implemented in the <a href="https://github.com/gaearon/redux-devtools/" class="link" target="_blank">Redux Dev Tools</a>, which are available as a <strong>library</strong> or a <strong>chrome plugin</strong>.</p>
</div></section>
<section class="slide" data-pos="11-1-2">
<span class="pos">11-1-2</span>
<div class="slidecontent"><p><strong>Hot module replacement</strong> means that you can <strong>make changes to your code and reload the app <em>without</em> losing state</strong>.</p>
</div></section>
<section class="slide" data-pos="11-1-3">
<span class="pos">11-1-3</span>
<div class="slidecontent"><p><strong>Time travel</strong> is very powerful for <strong>tracking down erroneous behavior</strong>. It also <strong>looks really cool</strong>!</p>
<p>If you <strong>install the devtools as a Chrome extension</strong>, you can see for yourself in the <a href="resources/site/demos/reduxdevtools/index.html" target="_blank">ReduxDevtools</a> demo. There we've simply <strong>added the dev tools to the Quotes app</strong>.</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 2/5</div>
<h3>Combined reducers</h3>
<p>The more the merrier</p>
</div>
</section>
<section class="slide" data-pos="11-2-1">
<span class="pos">11-2-1</span>
<div class="slidecontent"><p>In an <strong>app with lots going on</strong>, the <code>switch</code> in our reducer can quickly <strong>grow out of control</strong>.</p>
<p>The remedy is to have <strong>one reducer per key in our state</strong>, and then <strong>combine these</strong> into a single, app-wide reducer!</p>
</div></section>
<section class="slide" data-pos="11-2-2">
<span class="pos">11-2-2</span>
<div class="slidecontent"><p>That means we'll get a <strong>structure</strong> like this:</p>
<p><img src="resources/11-2-2-49.svg" alt="dot"></p>
</div></section>
<section class="slide" data-pos="11-2-3">
<span class="pos">11-2-3</span>
<div class="slidecontent"><p>We will explore that by <strong>adding more state</strong> to our previous <strong>Quotes app example</strong>, which has now received a footer with <strong>passed time</strong>:</p>
<p><img src="resources/images/quotesapp-footer.png" alt="withfooter"></p>
</div></section>
<section class="slide" data-pos="11-2-4">
<span class="pos">11-2-4</span>
<div class="slidecontent"><p>The <strong>updated app state</strong> looks like this: </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> initialstate = {
<span class="hljs-attr">quotes</span>: [<span class="hljs-string">'Carpe diem'</span>],
<span class="hljs-attr">time</span>: <span class="hljs-number">0</span>
};
</code></pre>
<p>The quotes-related data lives in <code>state.quotes</code>, and the time-related data lives in <code>state.time</code>. We'll now make <strong>one reducer for each of these keys</strong>.</p>
</div></section>
<section class="slide" data-pos="11-2-5">
<span class="pos">11-2-5</span>
<div class="slidecontent"><p>Each of these reducers will <strong>act only on their part of the state</strong>, which means that we can reuse the previous reducer as a <code>quotesReducer</code>:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> quotesReducer = <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, action.text];
<span class="hljs-keyword">default</span>: <span class="hljs-keyword">return</span> state || initialstate.quotes;
}
};
</code></pre>
<p>The only change is the <strong>default <code>initialstate</code> return</strong>, which is <strong>customary in combined reducers</strong>.</p>
</div></section>
<section class="slide" data-pos="11-2-6">
<span class="pos">11-2-6</span>
<div class="slidecontent"><p>Now we must add a <strong><code>timeReducer</code></strong>, which will be very simple:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> timeReducer = <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">'TICK'</span>: <span class="hljs-keyword">return</span> state + <span class="hljs-number">1</span>;
<span class="hljs-keyword">default</span>: <span class="hljs-keyword">return</span> state || initialstate.time;
}
}
</code></pre>
</div></section>
<section class="slide" data-pos="11-2-7">
<span class="pos">11-2-7</span>
<div class="slidecontent"><p>Finally these reducers are <strong>combined</strong> and passed to the store:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> reducer = Redux.combineReducers({
<span class="hljs-attr">quotes</span>: quotesReducer,
<span class="hljs-attr">time</span>: timeReducer
});
<span class="hljs-keyword">let</span> store = Redux.createStore(reducer);
</code></pre>
<p>Note that we <strong>no longer need to pass <code>initialstate</code> to <code>createStore</code></strong>, since we dealt with that in the separate reducers.</p>
</div></section>
<section class="slide" data-pos="11-2-8">
<span class="pos">11-2-8</span>
<div class="slidecontent"><p>You can try the augmented Quotes app in the <a href="resources/site/demos/combinedreducers/index.html" target="_blank">CombinedReducers</a> demo.</p>
<p>Of course, for this simple app, it would have been perfectly fine to have one reducer work on the entire state object. But as things grow more complex, <strong>having separate reducers will really increase code readability</strong>.</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 3/5</div>
<h3>Caching with Reselect</h3>
<p>Memoizing the bindings</p>
</div>
</section>
<section class="slide" data-pos="11-3-1">
<span class="pos">11-3-1</span>
<div class="slidecontent"><p>A popular <strong>companion library</strong> to Redux is <a href="https://github.com/reactjs/reselect" class="link" target="_blank">Reselect</a>, which helps with:</p>
<ul>
<li>computing <strong>derived data</strong> so that you don't need to store it in Redux</li>
<li><strong>memoizing</strong> expensive lookups to make Redux more efficient</li>
</ul>
</div></section>
<section class="slide" data-pos="11-3-2">
<span class="pos">11-3-2</span>
<div class="slidecontent"><p>We will <strong>explore using Reselect for derived data</strong> by adding the <strong>average seconds per quote</strong> to the footer of our Quotes app:</p>
<p><img src="resources/images/quotesapp-reselect.png" alt="average"></p>
</div></section>
<section class="slide" data-pos="11-3-3">
<span class="pos">11-3-3</span>
<div class="slidecontent"><p>Here are the <strong>main ideas</strong> of Reselect:</p>
<ul>
<li>We work with the concept of <strong>selectors</strong>.</li>
<li>You make a selector for <strong>each piece of data you need from the state</strong>.</li>
<li>The selectors can then be <strong>combined</strong>, and these combinations will be memoized.</li>
</ul>
</div></section>
<section class="slide" data-pos="11-3-4">
<span class="pos">11-3-4</span>
<div class="slidecontent"><p>In our case we need to select <strong>the number of quotes</strong> and <strong>seconds passed</strong>, from which we can then select <strong>average time per quote</strong> which we show in the app.</p>
<p><img src="resources/11-3-4-163.svg" alt="dot"></p>
</div></section>
<section class="slide" data-pos="11-3-5">
<span class="pos">11-3-5</span>
<div class="slidecontent"><p>Here's the <strong>code for <code>numQuotesSelector</code></strong>. It is a <strong>plain function</strong> that is <strong>called with the app state</strong>, and then <strong>returns the length of <code>state.quotes</code></strong>:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> numQuotesSelector = <span class="hljs-function"><span class="hljs-params">state</span> =></span> state.quotes.length;
</code></pre>
</div></section>
<section class="slide" data-pos="11-3-6">
<span class="pos">11-3-6</span>
<div class="slidecontent"><p>And <strong><code>timeSelector</code></strong> is similarly simple:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> timeSelector = <span class="hljs-function"><span class="hljs-params">state</span> =></span> state.time;
</code></pre>
</div></section>
<section class="slide" data-pos="11-3-7">
<span class="pos">11-3-7</span>
<div class="slidecontent"><p>The interesting part comes now when we create <strong><code>averageSelector</code></strong>:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> averageSelector = Reselect.createSelector(
[numQuotesSelector,timeSelector],
(numquotes,secs) => secs / numquotes
);
</code></pre>
<p><strong><code>Reselect.createSelector</code></strong> takes an <strong>array of selectors</strong> and a <strong>constructor function</strong> which is <strong>invoked with the results from the passed selectors</strong>.</p>
</div></section>
<section class="slide" data-pos="11-3-8">
<span class="pos">11-3-8</span>
<div class="slidecontent"><p>We can now <strong>use <code>averageSelector</code> inside <code>mapStateToProps</code></strong> when we create a container for the footer:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> mapStateToFooterProps = <span class="hljs-function"><span class="hljs-params">state</span> =></span> ({
<span class="hljs-attr">time</span>: state.time,
<span class="hljs-attr">avg</span>: averageSelector(state)
});
</code></pre>
<p>See the whole thing running in the <a href="resources/site/demos/reselect/index.html" target="_blank">Reselect</a> demo!</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 4/5</div>
<h3>Where to put logic</h3>
<p>app or action creators or reducer?</p>
</div>
</section>
<section class="slide" data-pos="11-4-1">
<span class="pos">11-4-1</span>
<div class="slidecontent"><p>Events pass through a Redux app like this:</p>
<p><img src="resources/11-4-1-44.svg" alt="dot"></p>
</div></section>
<section class="slide list" data-pos="11-4-2">
<span class="pos">11-4-2</span>
<div class="slidecontent">
<p>...which means that we often have to ask us <strong>where to put a piece of logic</strong> -</p>
<ul>
<li><span>a</span>in our <strong>components</strong>?</li>
<li><span>b</span>in our <strong>action creators</strong>?</li>
<li><span>c</span>in our <strong>middlewares</strong>?</li>
<li><span>d</span>in our <strong>reducers</strong>?</li>
</ul>
</div></section>
<section class="slide num numA" data-pos="11-4-3">
<span class="pos">11-4-3</span>
<div class="slidecontent">
<p>The <strong>components</strong> are <strong>rarely a good answer</strong>, unless the logic is <strong>only relevant to this particular component</strong>.</p>
</div></section>
<section class="slide num numB" data-pos="11-4-4">
<span class="pos">11-4-4</span>
<div class="slidecontent">
<p>The <strong>action creators</strong> are <strong>often a good answer</strong>. They can be <strong>full of side effects</strong>, and give rise to different actions depending on the circumstances.</p>
</div></section>
<section class="slide num numC" data-pos="11-4-5">
<span class="pos">11-4-5</span>
<div class="slidecontent">
<p>The <strong>middlewares</strong> are only useful in a very special circumstance - we have logic that wants to deal with <strong>every action</strong>.</p>
<p>But in that particular case, they are a perfect fit!</p>
</div></section>
<section class="slide num numD" data-pos="11-4-6">
<span class="pos">11-4-6</span>
<div class="slidecontent">
<p>The <strong>reducers must be pure</strong>, but even apart from that, it is often advantageous to have <strong>complex logic in the action creator instead</strong>.</p>
</div></section>
<section class="slide" data-pos="11-4-7">
<span class="pos">11-4-7</span>
<div class="slidecontent"><p>Especially <strong>beware of <code>if-else</code> constructs</strong> in reducers - it is often more flexible to <strong>move the branching to the action creators</strong> and letting that fire different actions for each branch.</p>
<p>So <strong>don't be afraid of fat action creators</strong>, even if it means skinny reducers!</p>
</div></section>
<section class="slide num numE" data-pos="11-4-8">
<span class="pos">11-4-8</span>
<div class="slidecontent">
<p>There is also a fifth option, namely <strong>somewhere else</strong> entirely.</p>
<p><img src="resources/11-4-8-74.svg" alt="dot"></p>
</div></section>
<section class="slide" data-pos="11-4-9">
<span class="pos">11-4-9</span>
<div class="slidecontent"><p>Imagine a board game app; instead of having an action for attacking with each unit type, have a single attacking action and let a helper library do the heavy computing.</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 5/5</div>
<h3>Flavours of state</h3>
<p>Lime and chocolate don't mix</p>
</div>
</section>
<section class="slide list" data-pos="11-5-1">
<span class="pos">11-5-1</span>
<div class="slidecontent">
<p>It's not immediately obvious, but when we introduce Redux to an app, we <strong>need to make choices on where to store what</strong>. There are three approaches:</p>
<ul>
<li><span>a</span>Everything in Redux</li>
<li><span>b</span>Some in React</li>
<li><span>c</span>Some also in a router</li>
</ul>
</div></section>
<section class="slide num numA" data-pos="11-5-2">
<span class="pos">11-5-2</span>
<div class="slidecontent">
<p>The easiest to define is to store <strong>everything in Redux</strong>. We <strong>don't use <code>setState</code> anywhere in our app</strong>, except for subscribing to Redux state.</p>
<p><img src="resources/11-5-2-155.svg" alt="dot"></p>
<p><strong>Even trivial UI state</strong>, such as a flag for whether a table cell is being edited or not, goes into Redux.</p>
</div></section>
<section class="slide" data-pos="11-5-3">
<span class="pos">11-5-3</span>
<div class="slidecontent"><p>An advantage of this is that it <strong>allows full time travel</strong> and logging.</p>
<p>A disadvantage is that it now means you must be very careful not to <strong>mix UI and app state</strong>. They must be clearly separated within the state object.</p>
</div></section>
<section class="slide num numB" data-pos="11-5-4">
<span class="pos">11-5-4</span>
<div class="slidecontent">
<p>If we do allow for <strong>some state inside components</strong>, the previous truths are flipped - we <strong>can no longer do (full) time travel</strong>, but app and UI state separation is free.</p>
<p><img src="resources/11-5-4-181.svg" alt="dot"></p>
</div></section>
<section class="slide num numC" data-pos="11-5-5">
<span class="pos">11-5-5</span>
<div class="slidecontent">
<p>Finally, if you have a router with URL parameters, you must be wary of the fact that those <strong>URL parameters are state</strong>.</p>
<p><img src="resources/11-5-5-129.svg" alt="dot"></p>
</div></section>
<section class="slide" data-pos="11-5-6">
<span class="pos">11-5-6</span>
<div class="slidecontent"><p>There are libraries to <strong>mirror parameters into Redux</strong>. The best option today is probably <a href="https://github.com/reactjs/react-router-redux">React-Router-Redux</a>, which has been semi-adopted by React-Router and will support the new v4 router.</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=10";
} else if (code >= 49 && code <= 5 + 48) {
window.location.hash = "#/" + (code-48+1+0);
}
if (11 < 14) {
if (code === 99 || e.key === 'c') { // the letter C for next Chapter
window.location.href = basehref+"chapter12.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>