-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
2417 lines (2324 loc) · 110 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>React Workshop</title>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/black.css">
<link rel="stylesheet" href="css/app.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section data-markdown>
<script type="text/template">
# React In a Day
## Justin McCandless
<img alt="Dev Meetings Logo" src="./img/devmeetings_logo.png" width="500px" />
#### [justinmc.github.io/react-workshop](https://justinmc.github.io/react-workshop)
</script>#
<aside class="notes">
- Let Rithm School introduce themselves
</aside>
</section>
<section data-markdown>
<script type="text/template">
## Hi I'm Justin
<ul class="fragment">
<li>Engineer at Autodesk Life Sciences</li>
<li><3 React since 2015</li>
<li>@justinjmcc | justinmccandless.com</li>
</ul>
</script>
<aside class="notes">
- Gauge React experience in audience
</aside>
</section>
<section data-markdown>
<script type="text/template">
## What to Expect
<ul>
<li class="fragment">We're each going to build our own React app over the course of the day.</li>
<li class="fragment">We'll work in 5 separate "sprints" covering core React functionality.</li>
<li class="fragment">I'll present the sprint's topics while working through my app, then turn it over to you guys and help out in person.</li>
</ul>
<aside class="notes">
- Some discussion and you guys can show your work at the end of each sprint.
- How DevMeetings does things.
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Quick Think of an App Idea
![Direct DOM Manipulation Example](./img/full_app_flow.gif)
- That's what I'll be building ([full code on Github](https://github.com/justinmc/react-workshop-app)).
- Your app was intended to be something like a social network, but anything with a feed will work.</li>
<aside class="notes">
- If you have an idea you actually want to put in production some day, feel free to get started here!
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Outline for the day
- What is React?
- Sprint 0: Creating a static app
- Sprint 1: Dynamic state and props
- Lunch
- Sprint 2: Forms and mutating state
- Sprint 3: Asynchronous data (integrating an API)
- Sprint 4: Performance and Architecture
- References and how to keep learning
<aside class="notes">
- Hands on learning practical stuff, but also understand fundamentals.
</aside>
</script>
</section>
<section>
<section data-markdown>
<script type="text/template">
## What is React?
<aside class="notes">
- What this section is about:
- Understanding what React is, behind the curtain
- No magic
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Introduction: What is React?</div>
## Roots: Static HTML
![HTML5 Boilerplate](./img/html.png)
<br />
Source: [https://html5boilerplate.com/](https://html5boilerplate.com/)
<aside class="notes">
- History lesson </aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Introduction: What is React?</div>
## Roots: Direct DOM Manipulation
Source: [http://codepen.io/justinmc/pen/oBRKeG](http://codepen.io/justinmc/pen/oBRKeG)
```javascript
<button class="adder">+1</button>
<div class="count">0</div>
```
```javascript
var adderEl = document.querySelector('.adder');
var countEl = document.querySelector('.count');
adderEl.addEventListener('click', function() {
var currentValue = countEl.innerHTML;
countEl.innerHTML = parseInt(currentValue) + 1;
});
```
![Direct DOM Manipulation Example](./img/increment.gif)
<aside class="notes">
- I was originally going to call this slide "jQuery Spaghetti".
- This is great for simple interactivity, but is hard to maintain in large applications.
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Introduction: What is React?</div>
## Roots: MVC
![MVC Diagram](./img/MVC-basic.svg)
<aside class="notes">
- Keep state in javascript, mutate state, and rerender.
- Backbone is a great example.
- Potential performance problems with totally rerendering too much.
- Potential architecture problem with needing to be wary of rerendering away temporary state
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Introduction: What is React?</div>
## Enter React
<ol>
<li class="fragment">Render HTML based on data.</li>
<li class="fragment">Do subsequent renders incredibly quickly.</li>
<li class="fragment">That's pretty much it.</li>
</ol>
<aside class="notes">
- Very minimal.
- Not a full MVC setup.
- Plays well with others.
- Solves the performance problem of rerendering too much.
</aside>
</script>
</section>
<section data-markdown data-transition="none">
<script type="text/template">
<div class="section">Introduction: What is React?</div>
## React: Data => HTML
```javascript
function MyReactComponent() {
const myButtonLabel = 'Click Me!';
const myButtonIsDisabled = false;
return (
<button
className="pretty-button"
disabled={myButtonIsDisabled}
>
{myButtonLabel}
</button>
);
}
```
![React Button Component Enabled](./img/button_component_enabled.png)
<aside class="notes">
- This is JSX (coming up next).
</aside>
</script>
</section>
<section data-markdown data-transition="none">
<script type="text/template">
<div class="section">Introduction: What is React?</div>
## React: Data => HTML
```javascript
function MyReactComponent() {
// Let's change this data
const myButtonLabel = 'Don\'t Click Me :(';
const myButtonIsDisabled = true;
return (
<button
className="pretty-button"
disabled={myButtonIsDisabled}
>
{myButtonLabel}
</button>
);
}
```
![React Button Component Enabled](./img/button_component_disabled.png)
<aside class="notes">
- If the data changes, the output html changes to match it.
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Introduction: What is React?</div>
## Wait but how are you writing HTML in your JS?
<ul class="fragment">
<li>It's called JSX!</li>
<li>It's a preprocessing language that gets compiled to plain old javascript.</li>
</ul>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Introduction: What is React?</div>
## JSX
```javascript
return (
<p className="prose">Lorem Ipsum</p>
);
```
=>
```javascript
return React.createElement(
"p",
{ className: "prose" },
"Lorem Ipsum"
);
```
If you want to play around with compiling to JSX, try [Babel's repl](https://babeljs.io/repl) (don't forget to turn on the "react" preset).
<aside class="notes">
- Very similar to document.createElement!
- We're going to use jsx in this workshop because it is very prevalent.
- But, take the time to convince yourself that there is no magic here.
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Introduction: What is React?</div>
## You said React was fast?
<ul>
<li class="fragment">React isn't just reinserting a whole new element each time something changes.</li>
<li class="fragment">It's doing DOM DIFFING!</li>
<li class="fragment">On a rerender, compares the current state of the DOM to the new state, then makes minimum number of operations needed to transition to the new state.</li>
</ul>
<aside class="notes">
- From what we've seen so far, you could imagine writing a library to insert these components into the dom yourself in a few hundred lines.
- There are some libraries that do this!
- Actually not bad performance in most cases.
- Think about how this solves the problem of rerendering a text input and having the cursor position change.
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Introduction: What is React?</div>
## One more performance buzz word for you
<ul>
<li class="fragment">React doesn't actually look at the actual DOM to make it's comparison. It looks at the VIRTUAL DOM.</li>
<li class="fragment">The virtual DOM is React's own internal representation of the DOM designed to be super quick to inspect and make operations on.</li>
<li class="fragment">Why does React go through all this trouble to avoid working with the DOM? Because the DOM is a slow legacy dinosaur.</li>
</ul>
<aside class="notes">
- Not to be confused with the Shadow DOM.
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Ok you guys pretty much know React now, let's build something.
<aside class="notes">
- Or at least I hope everyone understands what React is!
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
## Tips!
- Whenever you finish a task, commit!
- That way, if we have to move on while you're still working, you can stash and get back to a working state.
- Aim to finish green Basic tasks first, then polish up or pick and choose other tasks you want to try.
</script>
</section>
<section data-markdown>
<script type="text/template">
## [The Sprints](#/6)
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## [Sprint 0](https://github.com/justinmc/react-workshop-app/tree/sprint0)
1. <span class="task-basic">Join chat: https://discord.gg/gpQ68nm</span>
1. <span class="task-basic">Generate a React app with [create-react-app](https://github.com/facebookincubator/create-react-app).</span>
1. <span class="task-basic">Initialize a git repository.
1. <span class="task-basic">Edit the big title on the page and see it update in your browser.</span>
1. <span class="task-basic">Create your own nested React component.</span>
1. <span class="task-basic">Make it awesome with CSS and static info.</span>
1. <span class="task-advanced">Install a linter like the [airbnb eslint config](https://www.npmjs.com/package/eslint-config-airbnb) and make your jsx code comply!</span>
1. <span class="task-extra">Install [redux](http://redux.js.org/), create a Container above your "Post" component, and feed it dummy data.</span>
<aside class="notes">
- Note that the title here links to the code in the github repo for this sprint.
- Write finish time on the board.
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 0</div>
## create-react-app
- We're using it because it's easy and very widely used.
- It will be compiling our jsx, es7, and serving the compiled static assets.
- You can do fancier things with tools like Webpack and your own setup, though, so check those out if you end up with more build requirements.
<aside class="notes">
- We're using es7 for the same reason we're using jsx: it's very prevalent in the React community.
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 0</div>
## Default App.js component
![Rendering of default component](./img/default_component.png)
<aside class="notes">
- This is what create-react-app generates for you.
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 0</div>
## Default App.js component
```javascript
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
render() {
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
</div>
);
}
}
export default App;
```
<aside class="notes">
- Note that we're using a class here instead of a function. Both are ok!
- Note the importing of css and an svg. create-react-app will make sure both get bundled where they need to.
- Note React must be imported into the page, even though it's never referenced (when compiled from JSX, it will use React!).
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 0</div>
## Components as Classes and Functions
- The previous default component is equivalent to:
```javascript
function Component() {
return (
...
);
}
```
- The difference? Functions are purely functional, classes can be stateful (as we'll see).
<aside class="notes">
- Also, in a class it's possible to use React's built in lifecycle methods.
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 0</div>
## Customize it
```
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to Justin's Awesome App!</h2>
</div>
```
![Rendering of customized component](./img/default_component_customized.png)
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 0</div>
## Customized it
```
return (
<div className="App">
<div
className="app-bar"
>
<div>Story Time</div>
</div>
<h2>Your Books</h2>
<ul>
<li>Treasure Island by Robert Louis Stevenson</li>
<li>Go Dog Go by Dr. Seuss</li>
</ul>
</div>
);
```
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 0</div>
## Customized it
![Rendering of fully customized component](./img/default_component_fully_customized.png)
<aside class="notes">
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 0</div>
## Components in Components
```
<ul>
<li>Treasure Island by Robert Louis Stevenson</li>
</ul>
```
<div class="fragment">
```
<ul>
<Book />
</ul>
```
</div>
<aside class="notes">
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 0</div>
## Components in Components
`components/Book.js`
```
import React, { Component } from 'react';
class Book extends Component {
render() {
return (
<li>Treasure Island by Robert Louis Stevenson</li>
);
}
}
export default Book;
```
<aside class="notes">
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 0</div>
## Components in Components
Back in `App.js`
```
import React, { Component } from 'react';
import Book from './components/Book';
import './css/App.css';
```
...
```
<ul>
<Book />
<Book />
<Book />
</ul>
```
<aside class="notes">
- Go Dog Go is gone, but that's a problem for next sprint!
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 0</div>
## Questions before we [get started](#/6)?
<aside class="notes">
</aside>
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## [Sprint 1](https://github.com/justinmc/react-workshop-app/tree/sprint1)
1. <span class="task-basic">Feed your nested components data via props.</span>
1. <span class="task-basic">Add some local dynamic interactions with state.</span>
1. <span class="task-basic">Install the [React Dev Tools](https://chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi?hl=en) in your browser and use it to play with your props/state.</span>
1. <span class="task-advanced">Build some generic UI components for your app like a `<Button />`.</span>
1. <span class="task-extra">When the user performs an action like the one controlled by state in this sprint, call an action, dispatch an event, and modify state via a reducer.</span>
<aside class="notes">
- We'll get into more complicated dynamic stuff like text forms next sprint.
- Can do some really great frontend arch stuff by building generic reusable components!
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 1</div>
## Props and State
- Props is an object containing all data passed in to the component.
- State is an object representing all state internal to the component.
- Anytime either one changes, the component will attempt to rerender.
<aside class="notes">
- Component will kick off its whole process of DOM diffing and modifying the DOM if necessary.
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 1</div>
## Passing Props to the Book Component
`App.js`
```javascript
<ul>
<Book
title="Treasure Island"
author="Robert Louis Stevenson"
/>
<Book
title="Go Dog Go"
author="Dr. Seuss"
/>
</ul>
```
<aside class="notes">
- You can pass any javascript primitives or variables as props!
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 1</div>
## Passing Props to the Book Component
`Book.js`
```javascript
class Book extends Component {
render() {
return (
<li
className="book"
>
<div>
{this.props.title}
</div>
<div>
by {this.props.author}
</div>
</li>
);
}
}
```
<aside class="notes">
- What would this look like for a pure function component?
- (See next slide)
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 1</div>
## Passing Props to the Book Component (function)
`Book.js`
```javascript
function Book(props) {
return (
<li
className="book"
>
<div>
{props.title}
</div>
<div>
by {props.author}
</div>
</li>
);
}
```
<aside class="notes">
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 1</div>
## Prop Types and Default Props
You can tell React what types to expect for each prop value, whether or not they're required, and what value to use as a default if not. There is a nice [list of all available prop types](https://facebook.github.io/react/docs/typechecking-with-proptypes.html) in the React docs.
```javascript
static defaultProps = {}
static propTypes = {
title: React.PropTypes.string.isRequired,
author: React.PropTypes.string.isRequired,
}
```
<aside class="notes">
- This syntax is equivalent to `Book.defaultProps = {};` etc.
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
## State
I'll add a little "favorite" indicator on books that works like this:
<br />
![State UI](./img/state.gif)
<div class="section">Sprint 1</div>
<aside class="notes">
- Doesn't actually have any broader functionality, just toggles :)
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 1</div>
## State
`Book.js` and `Book.css`
```javascript
<div>
<span className="favorite" />
{this.props.title}
</div>
```
```css
.favorite {
display: inline-block;
background-color: #dfdfdf;
height: 16px;
width: 16px;
border-radius: 8px;
margin: 6px;
}
.favorite.enabled {
background-color: tomato;
}
```
<aside class="notes">
- What will this render?
- If I add the "enabled" class to the span, it will show filled red.
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 1</div>
## State
`Book.js`
```javascript
const favoriteClass = this.state.favorited ? 'enabled' : '';
return (
<li
className="book"
>
<div>
<span className={`favorite ${favoriteClass}`} />
{this.props.title}
</div>
```
<aside class="notes">
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 1</div>
## Initial State
`Book.js`
```javascript
constructor(props) {
super(props);
this.state = {
favorited: false,
};
}
```
<aside class="notes">
- Previously was inheriting its constructor from React.Component.
- Must call super with props if you have a constructor.
- If you're not using es6 classes, you can use getInitialState.
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 1</div>
## Make it Dynamic
`Book.js`
```javascript
onClickFavorite = () => {
this.setState({
favorited: !this.state.favorited,
});
}
```
```
<span
className={`favorite ${favoriteClass}`}
onClick={this.onClickFavorite}
/>
```
<aside class="notes">
- `setState` will merge old state with new state and attempt a rerender.
- All supported events like `onClick`: https://facebook.github.io/react/docs/events.html#supported-events
- This is es7 class property declaration syntax.
- Otherwise, `onClickFavorite() {` and `this.onClickFavorite = this.onClickFavorite.bind(this)` in constructor.
- React doesn't want you to call `.bind` inside `render` for performance reasons.
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 1</div>
## State All Done
`Book.js`
```
import React, { Component } from 'react';
import '../css/Book.css';
class Book extends Component {
static propTypes = {
title: React.PropTypes.string.isRequired,
author: React.PropTypes.string.isRequired,
}
constructor(props) {
super(props);
this.state = {
favorited: false,
};
}
onClickFavorite = () => {
this.setState({
favorited: !this.state.favorited,
});
}
render() {
const favoriteClass = this.state.favorited ? 'enabled' : '';
return (
<li
className="book"
>
<div>
<span
className={`favorite ${favoriteClass}`}
onClick={this.onClickFavorite}
/>
{this.props.title}
</div>
<div>
{`by ${this.props.author}`}
</div>
</li>
);
}
}
export default Book;
```
<aside class="notes">
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 1</div>
## Questions before we [get started](#/7)?
<aside class="notes">
- Text inputs will come next.
- Is there a `setProps`? No!
- Basically, props comes from state somewhere up the chain, so modify state and let it propagate.
- Can pass a function into child component to be called to modify parent state.
- We'll cover in more detail next sprint.
</aside>
</script>
</section>
</section>
<section>
<section data-markdown>
<script type="text/template">
## [Sprint 2](https://github.com/justinmc/react-workshop-app/tree/sprint2)
1. <span class="task-basic">Render an array of data by looping over it.</span>
1. <span class="task-basic">Build a controlled text input with state.</span>
1. <span class="task-advanced">Add validation to your form input.</span>
1. <span class="task-advanced">Add a few different types of form elements (radio buttons, checkboxes, dropdowns, etc.) and map their values to state.</span>
1. <span class="task-advanced">Try building an uncontrolled input.</span>
1. <span class="task-advanced">Play around with [refs](https://facebook.github.io/react/docs/refs-and-the-dom.html) for more direct DOM access.</span>
1. <span class="task-extra">Update your Flux/Redux store on form submission.</span>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 2</div>
## Looping in JSX
- JSX is capable of rendering arrays of elements too!
- Just make sure each element has a unique `key` attribute.
<div class="fragment">
```javascript
<ul>
{
['a', 'b', 'c'].map((element) => (
<li key={element}>{element}</li>
))
}
</ul>
```
</div>
<div class="fragment">
![Render of loop code](./img/list_loop.png)
</div>
<aside class="notes">
- React uses the `key` attribute to uniquely identify elements in order to do the most efficient DOM manipulations when your data changes.
- If you forget it in a loop, React will log a warning.
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 2</div>
## Looping in JSX
<ul>
<li>Why is `key` important?</li>
<li class="fragment">When rerendering, React uses `key` to determine how to reconcile the new and old array of elements:</li>
<ul>
<li class="fragment">If a key exists in the new elements and not in the old, add.</li>
<li class="fragment">If a key exists in the old elements and not in the new, remove.</li>
<li class="fragment">If a key exists in both, reconcile their differences.</li>
<li class="fragment">If a new element has no key, use its position</li>
</ul>
<li class="fragment">[Article on this topic](http://buildwithreact.com/article/in-depth-diffing)</li>
</ul>
<aside class="notes">
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 2</div>
## Looping in JSX
- Using keys, the minimum DOM operation happens.
![DOM modifications with keys](./img/add_key.gif)
<aside class="notes">
- Only 1 li insertion
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 2</div>
## Looping in JSX
- Without using keys, not so efficient :(
![DOM modifications with keys](./img/add_no_keys.gif)
<aside class="notes">
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 2</div>
## Books from an array
- Get our books into `state` as an array.
```javascript
constructor(props) {
super(props);
this.state = {
books: [
{ title: 'Treasure Island', author: 'Robert Louis Stevenson' },
{ title: 'Go Dog Go', author: 'Dr. Seuss' },
],
};
}
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Books from an array
- Render a `Book` component for each book in the array.
```
<ul>
{
this.state.books.map(book =>
<Book
key={book.title}
title={book.title}
author={book.author}
/>
)
}
</ul>
```
<aside class="notes">
- Now we're nicely set up to render any number of books.
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 2</div>
## Inputs
What does this render? (in a jsx React component)
```javascript
<input type="text" value="words go here" />
```
<div class="fragment">
![Input with value set to string](./img/input_with_value.png)
</div>
<aside class="notes">
- And what happens if the user tries to change the input?
- Nothing! It won't change. React will rerender it immediately.
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 2</div>
## Inputs
What is the value of `event.target.value`?
```javascript
onChangeInput = (event) => {
console.log(event.target.value);
}
...
<input
type="text"
value="words go here"
onChange={this.onChangeInput}
/>
```
<div class="fragment">
Answer: The value that the user updated the input to (after first keystroke)
</div>
<aside class="notes">
</aside>
</script>
</section>
<section data-markdown>
<script type="text/template">
<div class="section">Sprint 2</div>
## [Controlled Inputs](https://facebook.github.io/react/docs/forms.html)
```javascript
constructor(props) {
super(props);
this.state = {
inputValue: 'words go here',
};
}
onChangeInput = (event) => {
this.setState({