-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter3.html
569 lines (453 loc) · 29.3 KB
/
chapter3.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Learning React - Rendering lessons</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 3 of 14 </div>
<h1>Rendering lessons</h1>
<span>reindeering</span>
</div>
</section>
<section class="slide" data-pos="3-0-1">
<span class="pos">3-0-1</span>
<div class="slidecontent"><p><strong>Before</strong> we turn to <strong>more involved components</strong>, let's <strong>master</strong> the art of <strong>rendering</strong>!</p>
</div></section>
<section class="slide sectionlist">
<div class="slidecontent">
<h3>Sections in this chapter</h3>
<ol>
<li><a href="#/3">Single VS double braces</a></li>
<li><a href="#/4">Reserved names</a></li>
<li><a href="#/5">Spacing trick</a></li>
<li><a href="#/6">Conditional render</a></li>
<li><a href="#/7">Component children</a></li>
<li><a href="#/8">Component props VS Element props</a></li>
<li><a href="#/9">Rendering HTML strings</a></li>
<li><a href="#/10">Exercise 2</a></li>
</ol>
</div>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 1/8</div>
<h3>Single VS double braces</h3>
<p>The more the merrier?</p>
</div>
</section>
<section class="slide" data-pos="3-1-1">
<span class="pos">3-1-1</span>
<div class="slidecontent"><p>As you've seen, we go from JSX to JS through using a <strong>single pair of curly braces</strong>. In many other templating languages (Angular, Handlebars) we use double braces.</p>
</div></section>
<section class="slide" data-pos="3-1-2">
<span class="pos">3-1-2</span>
<div class="slidecontent"><p>In JSX, <strong>double braces</strong> would mean <strong>switching to JS</strong> (the outer braces) and then <strong>defining an object</strong> (the inner braces).</p>
<p>This is sometimes useful if we want to <strong>pass an object to a prop on a component</strong>:</p>
<pre><code class="lang-javascript"><User data={{<span class="hljs-attr">name</span>:<span class="hljs-string">'Steve'</span>,<span class="hljs-attr">age</span>:<span class="hljs-number">7</span>}} />
</code></pre>
</div></section>
<section class="slide" data-pos="3-1-3">
<span class="pos">3-1-3</span>
<div class="slidecontent"><p>A <strong>more clear</strong> but also <strong>verbose</strong> way is to <strong>first define the object</strong>...</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> user = {<span class="hljs-attr">name</span>:<span class="hljs-string">'Steve'</span>,<span class="hljs-attr">age</span>:<span class="hljs-number">7</span>}
</code></pre>
<p>...and then <strong>pass in the variable</strong> instead:</p>
<pre><code class="lang-javascript"><User data={user} />
</code></pre>
</div></section>
<section class="slide question" data-pos="3-1-4">
<span class="pos">3-1-4</span>
<div class="slidecontent">
<p>What happens if you forget yourself and <strong>just use single braces</strong> when you <strong>declare an object in a property</strong>?</p>
<pre><code class="lang-javascript"><User data={<span class="hljs-attr">name</span>:<span class="hljs-string">'Steve'</span>,<span class="hljs-attr">age</span>:<span class="hljs-number">7</span>} />
</code></pre>
</div></section>
<section class="slide answer" data-pos="3-1-5">
<span class="pos">3-1-5</span>
<div class="slidecontent">
<p>An <strong>error is thrown</strong> when the JSX is being parsed, since this...</p>
<pre><code class="lang-javascript"><User data={<span class="hljs-attr">name</span>:<span class="hljs-string">'Steve'</span>,<span class="hljs-attr">age</span>:<span class="hljs-number">7</span>} />
</code></pre>
<p>...means that we try to do this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> dataprop = name:<span class="hljs-string">'Steve'</span>,<span class="hljs-attr">age</span>:<span class="hljs-number">7</span> <span class="hljs-comment">// syntax error!</span>
</code></pre>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 2/8</div>
<h3>Reserved names</h3>
<p>Lost in translation</p>
</div>
</section>
<section class="slide" data-pos="3-2-1">
<span class="pos">3-2-1</span>
<div class="slidecontent"><p>Because <strong>JSX translates to JavaScript</strong>, words that are <strong>reserved in JS</strong> are <strong>avoided as attribute names</strong>.</p>
</div></section>
<section class="slide" data-pos="3-2-2">
<span class="pos">3-2-2</span>
<div class="slidecontent"><p>Remember how this JSX string...</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">foo</span>=<span class="hljs-string">"bar"</span>></span>Hello<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
</code></pre>
<p>...is translated to this JS call:</p>
<pre><code class="lang-javascript">React.createElement(<span class="hljs-string">"div"</span>,{<span class="hljs-attr">foo</span>:<span class="hljs-string">"bar"</span>},<span class="hljs-string">"Hello"</span>);
</code></pre>
</div></section>
<section class="slide" data-pos="3-2-3">
<span class="pos">3-2-3</span>
<div class="slidecontent"><p>The <strong>attribute names</strong> in JSX become <strong>object keys</strong> in JS.</p>
<p>So if we write...</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"heading"</span>></span>Hello<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
</code></pre>
<p>...then the transpiler would output...</p>
<pre><code class="lang-javascript">React.createElement(<span class="hljs-string">"div"</span>,{<span class="hljs-attr">class</span>:<span class="hljs-string">"heading"</span>},<span class="hljs-string">"Hello"</span>);
</code></pre>
<p>...which would <strong>blow up</strong> in older browsers since <strong><code>class</code> previously wasn't allowed as a key name</strong>.</p>
</div></section>
<section class="slide" data-pos="3-2-4">
<span class="pos">3-2-4</span>
<div class="slidecontent"><p>To work around that we <strong>use <code>className</code> instead</strong>:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"heading"</span>></span>Hello<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
</code></pre>
<p><strong>React</strong> will <strong>take care of making sure</strong> that the resulting DOM node gets the <strong>correct <code>class</code> attribute</strong>.</p>
</div></section>
<section class="slide" data-pos="3-2-5">
<span class="pos">3-2-5</span>
<div class="slidecontent"><p>For the same reason we also <strong>use <code>htmlFor</code> instead of <code>for</code></strong> in form labels.</p>
<p>Forgetting about all this and typing <code>class</code> instead of <code>className</code> is a <strong>very common mistake</strong> among beginners and veterans alike, so be warned!</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 3/8</div>
<h3>Spacing trick</h3>
<p>Room to breathe</p>
</div>
</section>
<section class="slide" data-pos="3-3-1">
<span class="pos">3-3-1</span>
<div class="slidecontent"><p>Here's what happens. You define your structure like this:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">Comp1</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">Comp2</span>/></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
</code></pre>
<p>...but since both <code>Comp1</code> and <code>Comp2</code> are <strong>inline elements</strong>, you need a
<strong>space between them</strong>. There won't be.</p>
</div></section>
<section class="slide" data-pos="3-3-2">
<span class="pos">3-3-2</span>
<div class="slidecontent"><p>Solve through this little trick:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">div</span>></span>
<span class="hljs-tag"><<span class="hljs-name">Comp1</span>/></span>{' '}
<span class="hljs-tag"><<span class="hljs-name">Comp2</span>/></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
</code></pre>
<p>The little <code>{' '}</code> spacing will result in a <strong>one-space string</strong> at the correct location:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">div</span>></span><span class="hljs-tag"><<span class="hljs-name">Comp1</span>/></span> <span class="hljs-tag"><<span class="hljs-name">Comp2</span>/></span><span class="hljs-tag"></<span class="hljs-name">div</span>></span>
</code></pre>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 4/8</div>
<h3>Conditional render</h3>
<p>It depends</p>
</div>
</section>
<section class="slide" data-pos="3-4-1">
<span class="pos">3-4-1</span>
<div class="slidecontent"><p>Let's talk a bit about how to <strong>return different things</strong> depending on the data!</p>
<p>First off, in React, this is a <strong>pure JavaScript problem</strong>. We don't have to look up any specific logic helpers tacked on to the side of HTML, as in Handlebars.</p>
</div></section>
<section class="slide" data-pos="3-4-2">
<span class="pos">3-4-2</span>
<div class="slidecontent"><p>The <strong>simplest solution</strong>, as shown in the <a href="resources/site/demos/conditionalreturn/index.html" target="_blank">ConditionalReturn</a> demo, is to simply <strong>branch the return statement</strong>:</p>
<pre><code class="lang-javascript">Mirror = <span class="hljs-function"><span class="hljs-params">props</span> =></span> {
<span class="hljs-keyword">if</span> (props.betruthful){
<span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag"><<span class="hljs-name">div</span>></span>The fairest of all is <span class="hljs-tag"><<span class="hljs-name">strong</span>></span>Snow-white<span class="hljs-tag"></<span class="hljs-name">strong</span>></span><span class="hljs-tag"></<span class="hljs-name">div</span>></span></span>
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag"><<span class="hljs-name">div</span>></span>The fairest of all is <span class="hljs-tag"><<span class="hljs-name">strong</span>></span>You<span class="hljs-tag"></<span class="hljs-name">strong</span>></span><span class="hljs-tag"></<span class="hljs-name">div</span>></span></span>;
}
};
</code></pre>
</div></section>
<section class="slide" data-pos="3-4-3">
<span class="pos">3-4-3</span>
<div class="slidecontent"><p>Often, as in this case, it is more convenient to do the <strong>branching inside the return statement</strong>:</p>
<pre><code class="lang-javascript">Mirror = <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>
The fairest of all is
<span class="hljs-tag"><<span class="hljs-name">strong</span>></span>{props.betruthful ? "Snow-white" : "You"}<span class="hljs-tag"></<span class="hljs-name">strong</span>></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span></span>
);
</code></pre>
<p>We show this in the <a href="resources/site/demos/conditionalreturn2/index.html" target="_blank">ConditionalReturn2</a> demo.</p>
</div></section>
<section class="slide" data-pos="3-4-4">
<span class="pos">3-4-4</span>
<div class="slidecontent"><p>Some prefer to <strong>do the branching before returning</strong> by using <strong>variables</strong>, which also works fine:</p>
<pre><code class="lang-javascript">Mirror = <span class="hljs-function"><span class="hljs-params">props</span> =></span> {
<span class="hljs-keyword">let</span> prettiest = props.betruthful ? <span class="hljs-string">"Snow-white"</span> : <span class="hljs-string">"You"</span>;
<span class="hljs-keyword">return</span> (
<span class="xml"><span class="hljs-tag"><<span class="hljs-name">div</span>></span>
The fairest of all is <span class="hljs-tag"><<span class="hljs-name">strong</span>></span>{prettiest}<span class="hljs-tag"></<span class="hljs-name">strong</span>></span>
<span class="hljs-tag"></<span class="hljs-name">div</span>></span></span>
);
};
</code></pre>
</div></section>
<section class="slide" data-pos="3-4-5">
<span class="pos">3-4-5</span>
<div class="slidecontent"><p>Sometimes we don't need to branch between different output, but instead display <strong>something or nothing</strong>.</p>
<p>A convenient solution is to use the <strong>JavaScript <code>&&</code> operator</strong> inside JSX as in the <a href="resources/site/demos/onlyiftrue/index.html" target="_blank">OnlyIfTrue</a> demo:</p>
<pre><code class="lang-javascript">Mirror = <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>The fairest of all is you!<span class="hljs-tag"></<span class="hljs-name">p</span>></span>
{props.betruthful && <span class="hljs-tag"><<span class="hljs-name">p</span>></span>...no, actually it's Snow-white.<span class="hljs-tag"></<span class="hljs-name">p</span>></span>}
<span class="hljs-tag"></<span class="hljs-name">div</span>></span></span>
);
</code></pre>
</div></section>
<section class="slide question" data-pos="3-4-6">
<span class="pos">3-4-6</span>
<div class="slidecontent">
<p>There's a <strong>common related mistake</strong>; can you spot the bug below?</p>
<pre><code><span class="hljs-built_in">return</span> (
<<span class="hljs-keyword">div</span>>
{generalInfo}
{reviews.<span class="hljs-built_in">length</span> && (
<p>There are {reviews.<span class="hljs-built_in">length</span>} reviews <span class="hljs-keyword">for</span> you <span class="hljs-keyword">to</span> <span class="hljs-built_in">read</span>!</p>
)}
</<span class="hljs-keyword">div</span>>
);
</code></pre></div></section>
<section class="slide answer" data-pos="3-4-7">
<span class="pos">3-4-7</span>
<div class="slidecontent">
<p>Since...</p>
<ul>
<li><strong>numbers become text nodes</strong></li>
<li>the <code>&&</code> operator <strong>returns the falsy value</strong></li>
<li>the <strong>falsy value is 0</strong> in our case</li>
</ul>
<p>...the component will <strong>print the number 0</strong> if there are no reviews! See it happen in the <a href="resources/site/demos/conditionalreturnfail/index.html" target="_blank">ConditionalFail</a> demo.</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 5/8</div>
<h3>Component children</h3>
<p>What happens to the little ones?</p>
</div>
</section>
<section class="slide" data-pos="3-5-1">
<span class="pos">3-5-1</span>
<div class="slidecontent"><p>It is a <strong>very natural thing</strong> to wrap elements in other elements. That is how we <strong>express the nested structure</strong> of what we want.</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">ul</span>></span>
<span class="hljs-tag"><<span class="hljs-name">li</span>></span>milk<span class="hljs-tag"></<span class="hljs-name">li</span>></span>
<span class="hljs-tag"><<span class="hljs-name">li</span>></span>toilet paper<span class="hljs-tag"></<span class="hljs-name">li</span>></span>
<span class="hljs-tag"><<span class="hljs-name">li</span>></span>bread<span class="hljs-tag"></<span class="hljs-name">li</span>></span>
<span class="hljs-tag"></<span class="hljs-name">ul</span>></span>
</code></pre>
</div></section>
<section class="slide" data-pos="3-5-2">
<span class="pos">3-5-2</span>
<div class="slidecontent"><p>But what happens when we <strong>wrap elements inside a component</strong>?</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">Component</span>></span>
<span class="hljs-tag"><<span class="hljs-name">span</span>></span>What will happen to me?<span class="hljs-tag"></<span class="hljs-name">span</span>></span>
<span class="hljs-tag"></<span class="hljs-name">Component</span>></span>
</code></pre>
</div></section>
<section class="slide" data-pos="3-5-3">
<span class="pos">3-5-3</span>
<div class="slidecontent"><p>Inside a component, we can <strong>access elements</strong> that were passed in like this through <strong><code>props.children</code></strong>:</p>
<pre><code class="lang-javascript">Highlighter = <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 class="hljs-attr">className</span>=<span class="hljs-string">"highlight"</span>></span>{props.children}<span class="hljs-tag"></<span class="hljs-name">div</span>></span></span>;
);
<span class="hljs-keyword">let</span> output = (
<span class="xml"><span class="hljs-tag"><<span class="hljs-name">Highlighter</span>></span>
Clean up <span class="hljs-tag"><<span class="hljs-name">strong</span>></span>NOW<span class="hljs-tag"></<span class="hljs-name">strong</span>></span>!
<span class="hljs-tag"></<span class="hljs-name">Highlighter</span>></span></span>
);
<span class="hljs-comment">// output is <div className="highlight">Clean... </div></span>
</code></pre>
<p>As shown in the <a href="resources/site/demos/propschildren/index.html" target="_blank">PropsChildren</a> demo</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 6/8</div>
<h3>Component props VS Element props</h3>
<p>You say tomato</p>
</div>
</section>
<section class="slide" data-pos="3-6-1">
<span class="pos">3-6-1</span>
<div class="slidecontent"><p>When we <strong>render an element</strong>...</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">'something'</span> <span class="hljs-attr">data-myvar</span>=<span class="hljs-string">'sth else'</span>></span>...<span class="hljs-tag"></<span class="hljs-name">div</span>></span></span>;
</code></pre>
<p>the props will end up on the actual DOM node:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">'something'</span> <span class="hljs-attr">data-myvar</span>=<span class="hljs-string">'sth else'</span>></span>...<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
</code></pre>
</div></section>
<section class="slide" data-pos="3-6-2">
<span class="pos">3-6-2</span>
<div class="slidecontent"><p>But when we render a <strong>component</strong>, the <strong>attributes become properties</strong> that are passed to the component, as we've seen many times already.</p>
<pre><code class="lang-javascript">let User = props => <span>Name: {props.name}</span>;
let output = <User name="David"/>; // ---> Name: David
</code></pre>
</div></section>
<section class="slide" data-pos="3-6-3">
<span class="pos">3-6-3</span>
<div class="slidecontent"><p>A <strong>common mistake</strong> is to confuse this and type:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">MyComp</span> <span class="hljs-attr">className</span>=<span class="hljs-string">'someclass'</span>></span>
</code></pre>
<p>This <strong>won't put the <code>className</code> on the DOM node</strong>, unless <code>MyComp.render</code> explicitly passes it on to its output:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">{props.className}</span>></span>
</code></pre>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 7/8</div>
<h3>Rendering HTML strings</h3>
<p>microwave meals ftw</p>
</div>
</section>
<section class="slide" data-pos="3-7-1">
<span class="pos">3-7-1</span>
<div class="slidecontent"><p>Imagine you got a string of HTML from somewhere:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> longList = <span class="hljs-string">"<ul><li>...</li></ul>"</span>;
</code></pre>
<p>How do we <strong>render that from inside a component</strong>?</p>
</div></section>
<section class="slide" data-pos="3-7-2">
<span class="pos">3-7-2</span>
<div class="slidecontent"><p>Simply <strong>outputting the string doesn't work</strong>, it will just render as plain text.</p>
<pre><code class="lang-javascript">let Static = props => <div>{props.static}</div>;
let output = <Static static="<h2>Hello!</h2>"/>;
// ---> "<h2>Hello!</h2>"
</code></pre>
</div></section>
<section class="slide" data-pos="3-7-3">
<span class="pos">3-7-3</span>
<div class="slidecontent"><p>Here's how to do it: We <strong>put the string in the <code>__html</code> prop</strong> of an object:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> htmlObj = {<span class="hljs-attr">__html</span>:longList};
</code></pre>
<p>...and then we <strong>feed that to the <code>dangerouslySetInnerHTML</code> attribute</strong> of a container tag:</p>
<pre><code><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">dangerouslySetInnerHTML</span>=<span class="hljs-string">{htmlObj}</span> /></span>
</code></pre></div></section>
<section class="slide" data-pos="3-7-4">
<span class="pos">3-7-4</span>
<div class="slidecontent"><p>Or we can <strong>do it in one swoop</strong> without going via <code>htmlObj</code>:</p>
<pre><code><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">dangerouslySetInnerHTML</span>=<span class="hljs-string">{{__html:longList}}</span> /></span>
</code></pre><p>Note how we end up with <strong>double braces</strong> - the <strong>outer pair</strong> is to <strong>shift from JSX to JavaScript</strong>, and the <strong>inner pair declares an object</strong>.</p>
</div></section>
<section class="slide question" data-pos="3-7-5">
<span class="pos">3-7-5</span>
<div class="slidecontent">
<p>Wasn't that <strong>needlessly complicated</strong>?</p>
</div></section>
<section class="slide answer" data-pos="3-7-6">
<span class="pos">3-7-6</span>
<div class="slidecontent">
<p>Yes. This is <strong>by design</strong> as injecting html like this is often a bad (and unsafe) idea.</p>
</div></section>
</section>
<section>
<section class="slide sectiontitle">
<div class="slidecontent">
<div class='sectioncount'>Section 8/8</div>
<h3>Exercise 2</h3>
<p>Turning up the heat</p>
</div>
</section>
<section class="slide" data-pos="3-8-1">
<span class="pos">3-8-1</span>
<div class="slidecontent"><p>Let us now hone our rendering skills by taking on <strong>Exercise 2</strong>!</p>
<p>Find it in the course resources with the name <a href="resources/site/exercises/02_renderchallenges/" target="_blank">RenderChallenges</a>.</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=2";
} else if (code >= 49 && code <= 8 + 48) {
window.location.hash = "#/" + (code-48+1+1);
}
if (3 < 14) {
if (code === 99 || e.key === 'c') { // the letter C for next Chapter
window.location.href = basehref+"chapter4.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>