forked from arilaen/gdi-intro-react-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
913 lines (825 loc) · 37.2 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GDI Slides Template - The GDI HTML Presentation Framework</title>
<meta name="description" content="Girl Develop It framework for easily creating beautiful presentations using HTML in GDI theme. Forked from Hakim El Hattab's reveal.js">
<meta name="author" content="Girl Develop It">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="dist/css/reveal.css">
<link rel="stylesheet" href="dist/css/default.css" id="theme">
<!-- For syntax highlighting -->
<!-- light editor <link rel="stylesheet" href="dist/css/light.css">-->
<!-- dark editor --><link rel="stylesheet" href="dist/css/dark.css">
<!-- <link rel="stylesheet" href="dist/css/zenburn.css"> -->
<link rel="stylesheet" href="plugin/accessibility-helper/css/accessibility-helper.css">
<!-- If the query includes 'print-pdf', include the PDF print sheet -->
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'css/print/pdf.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script>
<!-- If use the PDF print sheet so students can print slides-->
<link rel="stylesheet" href="dist/css/print/pdf.css" type="text/css" media="print">
<link rel="icon" type="image/x-icon" href="favicon.ico" />
<!--[if lt IE 9]>
<script src="dist/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<!-- Opening -->
<section>
<img src="dist/img/circle-gdi-logo.png" alt="GDI Logo" class="noborder"/>
<h1>Introduction to React.js</h1>
<h2>Up and running, and beyond</h2>
<p>
<small>Found at <a href ="https://github.com/arilaen/gdi-intro-react-js">github</a>.</small>
</p>
<p>
<small>Marcela Rodriguez / <a href="http://twitter.com/arilaen">@arilaen</a></small>
</p>
</section>
<!-- Compatibility-->
<section>
<h2>Heads Up</h2>
<p>
Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.</p>
<p class="green">Some "rules"</p>
<ul>
<li>We are here for you!</li>
<li>Every question is important</li>
<li>Help each other</li>
<li>Have fun</li>
</ul>
</section>
<section>
<h2>Introductions</h2>
<h3>Tell us about yourself.</h3>
<p>Which of your web projects are you most proud of?</p>
<ul>
<li>Who are you?</li>
<li>What do you hope to get out of the class?</li>
<li>If you could make any website, what would it be?</li>
</ul>
</section>
<section>
<h2>Prerequisites</h2>
<ul>
<li>Javascript</li>
<li>ES6 (will cover the basics here)</li>
<li>HTML/CSS</li>
</ul>
</section>
<section>
<h2>You should have:</h2>
<ul>
<li>A <a href="https://codepen.io">Codepen account</a></li>
<li>A text editor (<a href="https://atom.io">Atom</a>, <a href="https://sublimetext.com">Sublime Text</a>)</li>
<li>Google Chrome with <a href="https://chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi">React Developer Tools</a></li>
</ul>
</section>
<section>
<h2>Four Day Agenda</h2>
<ol>
<li>Hello World! Components, Props, State, JSX, ES6</li>
<li>More ES6, Inline Styles, form basics</li>
<li>Best practices, view containers, more on forms</li>
<li>Recreate famous sites in two hours, Detour into Flux pattern/Redux/the React of tomorrow</li>
</ol>
</section>
<section>
<h2>Recommended Resources</h2>
<ul>
<li><a href="https://facebook.github.io/react/">React Docs</a></li>
<li><a href="https://www.codecademy.com/learn/react-101">Codecademy courses</a></li>
<li><a href="https://www.codeschool.com/courses/powering-up-with-react">Codeschool course</a></li>
<li><a href="https://egghead.io/courses/start-using-react-to-build-web-applications">Egghead: Start Using React to Build Web Applications</a></li>
<li><a href="https://github.com/bmorelli25/Become-A-Full-Stack-Web-Developer/#learn-react-js">And many more here</a></li>
</ul>
</section>
<section>
<h2>By the end of day 1 you'll know how to use:</h2>
<ul>
<li>Components</li>
<li>JSX</li>
<li>Props</li>
<li>State + setState</li>
</ul>
</section>
<section>
<h2>What is React?</h2>
<ul>
<li>The V in MVC</li>
<li>Developed by Facebook and open-sourced</li>
</ul>
</section>
<section>
<h2>Why use React?</h2>
<ul>
<li>Fast</li>
<li>Declarative</li>
<li>Easy to understand and maintain at scale</li>
<li>Flexibility - can be used with your favorite logic/routing libraries. (But React router is great)</li>
<li>In fact, it's so flexible that it can be used on the server side as well as the client side (for isomorphic apps)!</li>
<li>And of course it can be used for cross-platform mobile development with React Native</li>
</ul>
</section>
<section>
<h2>Should I use React?</h2>
<ul>
<li>Great for dynamic sites with lots of data</li>
<li>Also great if you want to use vanilla JS and ES6, rather than learn a new DSL (domain specific language)</li>
<li>May be overkill for very small, static sites, since it requires more explicit code than other frameworks</li>
<li>Some people prefer to keep JS and HTML separate (and prefer Vue or AngularJS) - but don't knock JSX until you've tried it!</li>
</ul>
</section>
<section>
<h2>Some vocab to get started</h2>
<ul>
<li>Component</li>
<li>Class</li>
<li>JSX</li>
<li>Transpiling</li>
<li>Virtual DOM</li>
</ul>
</section>
<section>
<h4>What is a <strong>Component</strong>?</h4>
<ul>
<li>Components are the building blocks of React</li>
<li>They are classes that extend React.Component</li>
<li>They always implement a render function, which outputs HTML</li>
</ul>
<pre><code class="javascript">
class Greeting extends React.Component {
render() {}
}
</code></pre>
</section>
<section>
<h4>What is a <strong>Class</strong>?</h4>
<ul>
<li>A class is a new Javascript feature, added in ES6 (ECMA2015).</li>
<li>It is literally a function with syntatic sugar, that behaves similarly to classes in object-oriented languages (like Java).</li>
<li>A class can have a constructor function, which is run on initialization.<li>
<li>A class can extend another class with the "extends" keyword. If it does so, it's constructor needs to use the word super().</li>
<li><em>We'll review all of this later!</em></li>
</ul>
<pre><code class="javascript">
class Greeting extends React.Component {
constructor() {
super();
}
}
</code></pre>
<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes">MDN docs</a></p>
</section>
<section>
<h2>What is <strong>JSX</strong>?</h2>
<ul>
<li>Think of it as code that looks like HTML, and is <strong>transpiled</strong> into Javascript that generates HTML.</li>
<li>(If you're curious about the X, the name is short for Javascript XML.)</li>
<li>One (big) benefit is that you can catch more template errors at compile time, rather than on page load<li>
<li>Making smaller, reusable components is also easier, versus other frameworks where a new template file and JS file need to be created for each new component.</li>
<li><a href="https://jsx.github.io/">Read more about the benefits here</a></li>
</ul>
</section>
<section>
<h2>What is <strong>JSX</strong>?</h2>
<ul>
<li>Almost fully identical in appearance to HTML - except:</li>
<li><strong>className</strong> - Instead of writing <li class="red">, you would write <li className="red">. This is because class is now a reserved word in Javascript.</li>
<li><strong>key</strong> - To optimize list display performance, where lists can be reordered/changed, React asks for a key attribute that points to a unique ID for each list element.</li>
<li><strong>Synthetic events</strong> - Some event handlers (onSubmit, onClick) have different names or implementations in different browsers. The HTML created by your code will be browser specific (onSubmit will turn into submit, etc), simplifying cross browser compatibility.</li>
<li>And some other exceptions we might get to later!</li>
</ul>
</section>
<section>
<h2>What is <strong>JSX</strong>?</h2>
<h4>Syntax - Hello World example</h4>
<pre><code class="javascript">
class Greeting extends React.Component {
constructor() {
super();
}
render() {
return (<div>Hello World!</div>);
}
}
</code></pre>
<p>Now, in another component, we can use the Greeting component, with the self-closing tag:</p>
<pre><code class="javascript">
class Layout extends React.Component {
render() {
return (<Greeting />);
}
}
</code></pre>
</section>
<section>
<h2>What is <strong>JSX</strong>?</h2>
<h4>Syntax - Using JS in JSX</h4>
<p>Javascript can be used in JSX as long as it is surrounded by single curly braces ({}).</p>
<pre><code class="javascript">
class Greeting extends React.Component {
constructor() {
super();
}
render() {
const message = 'Hello World!';
return (<div>{message}</div>);
}
}
</code></pre>
</section>
<section>
<h2>What are <strong>transpiling</strong> and <strong>compiling</strong>?</h2>
<p>Transpilation is the process of turning Javascript that cannot be understood by the browser (ES6, JSX) into the Javascript understood by browsers today (ES5).<p>
<p>It is usually done with <a href="https://babeljs.io">BabelJS</a>.</p>
<p>Compilation is the process of turning one language into another, transpiling is compiling between two languages that are similar. So you can use compilation and transpiling interchangably when talking about turning ES6/JSX into Javascript.</p>
</section>
<section>
<h2>What is the <strong>Virtual DOM</strong>?</h2>
<p>First things first: the browser DOM is an in-memory abstraction of HTML</p>
<p><em>DOM stands for <strong>Document Object Model</strong></em></p>
<p>Updating the DOM requires browser manipulation, which is slow</p>
<p>The virtual DOM is an in-memory representation of the DOM. Updating the virtual DOM does not change the browser, so it is much faster.</p>
</section>
<section>
<h2>What is the <strong>Virtual DOM</strong>?</h2>
<p>We use ReactDOM.render to add a component to the VirtualDOM.</p>
<p>Whenever the page loads, or data changes after a page load, React creates a new VirtualDOM, and compares it to the old one. It takes the changes between them and applies them to the real DOM. This results in less browser manipulation, and faster performance.
<pre><code class="javascript">
class Greeting extends React.Component {
render() {
return (<div>Hello World!</div>);
}
}
ReactDOM.render(<Greeting />, document.getElementById('Greeting'));
</code></pre>
<p>The first argument to ReactDOM.render() is the JSX to render, and the second is an element on the page to insert it in (selected with vanilla JS).</p>
<p><em>Note that we don't need the constructor method yet, but we'll start using it later.</em></p>
</section>
<section>
<h2>Let's play on CodePen!</h2>
<p>Visit <a href="https://codepen.io/arilaen/pen/mwbwqa">Hello World example</a> on Codepen: https://codepen.io/arilaen/pen/mwbwqa
</p>
</section>
<section>
<h2>Deconstructing the Hello World code</h2>
<h4>1. Generate HTML</h4>
<p>Create a function that represents an HTML component's state, and returns HTML in a render method</p>
<pre><code class="javascript">
class App extends React.Component {
render() {
return (<h1>Hello world!</h1>);
}
}
</code></pre>
</section>
<section>
<h2>Deconstructing the Hello World code</h2>
<h4>2. Add HTML to the page</h4>
<p>
Update the DOM, using a diff of the virtual DOM. First argument is new HTML, second is the element to insert it in.
</p>
<pre><code class="javascript">
ReactDOM.render(<App />, document.getElementById('root'));
</code></pre>
</section>
<section>
<h2>How to make your own React Hello World Codepen</h2>
<p>Demo: will demonstrate how to use Babel and add React and ReactDOM to a new CodePen.</p>
<p>Make sure you've created an account so you can create your own as well!</p>
</section>
<section>
<h2><a href="https://facebook.github.io/react">Official React site!</a></h2>
<p>Quick overview, checking out their docs and tutorial</p>
</section>
<section>
<h2>Some more vocab!</h2>
<ul>
<li>Props</li>
<li>State</li>
<li>setState</li>
</ul>
</section>
<section>
<h2>What are <strong>Props</strong>?</h2>
<ul>
<li>Props are attributes passed down from one component to another</li>
<li>A component's props can be accessed through this.props in the render function</li>
<li>A component's props are read-only. HOWEVER! Functions can be passed down as props as well. <em>We'll see why this is important soon.</em></li>
</ul>
</section>
<section>
<h2>Hello World with Props</h2>
<pre><code class="javascript">
class Greeting extends React.Component {
render() {
return (<div>{this.props.message}</div>);
}
}
class App extends React.Component {
render() {
const message = 'Hello World!';
return (<Greeting message={message} />);
}
}
ReactDOM.render(<App />, document.getElementById('app'));
</code></pre>
</section>
<section>
<h2>What is <strong>state</strong>?</h2>
<p>The idea behind React is that your code has everything you need to know to display your views, given a specific state.</p>
<p>State is set at the parent component, and then passed down to children as props.</p>
<p>A component's initial state is set in the constructor function.</p>
<pre><code class="javascript">
class Greeting extends React.Component {
render() {
return (<div>{this.props.message}</div>);
}
}
class App extends React.Component {
constructor() {
super();
this.state = {
message: 'Hello World!'
};
}
render() {
return (<Greeting message={this.state.message} />);
}
}
ReactDOM.render(<App />, document.getElementById('app'));
</code></pre>
</section>
<section>
<h2>What is <strong>setState</strong>?</h2>
<p>To change a component's state, use this.setState(newState).</p>
<p>But how can we do this from child components? Pass FUNCTIONS as props!</p>
<pre><code class="javascript">
class Greeting extends React.Component {
render() {
return (<div onClick={this.props.changeMessage.bind(this)}>{this.props.message}</div>);
}
}
class App extends React.Component {
constructor() {
super();
this.state = {
message: 'Hello World!'
};
}
render() {
return (<Greeting message={this.state.message} changeMessage={this.sayGoodbye} />);
}
sayGoodbye(event) {
event.preventDefault();
this.setState(Object.assign({}, this.state, { message: 'Goodbye' }));
// Equivalent to this.setState({ message: 'Goodbye' })
}
}
ReactDOM.render(<App />, document.getElementById('app'));
</code></pre>
</section>
<section>
<h2>Code Breakdown!</h2>
<p>Let's go over the last example piece by piece:</p>
<ul>
<li>We added a sayGoodbye method to the App class, and used it as an event handler in the Greeting class. Event handlers are functions called from onClick, onSubmit, etc.
<pre><code class="javascript">
sayGoodbye(event) {
event.preventDefault();
this.setState({message: 'Goodbye'});
}
</code></pre></li>
<li>Event handlers do not need to be invoked (have parentheses at the end). But when a click happens, the event handler is called with an event as the argument. To prevent side effects (like form submissions from pressing a button in a form), use event.preventDefault() (above).
<pre><code class="javascript">
render() {
return (<div onClick={this.props.changeMessage.bind(this)}>{this.props.message}</div>);
}
</code></pre>
</li>
</ul>
</section>
<section>
<h2>Object.assign</h2>
<p>Object.assign is new to Javascript with ES6. It takes the first object (usually empty) and then updates it with attributes from any following objects, in order. This is a great way to create new objects based on existing ones concisely.</p>
<pre><code class="javascript">
this.setState(Object.assign({}, this.state, { message: 'Goodbye' }));
// Would be especially useful with a large state, with many attributes
</code></pre>
</section>
<section>
<h2>Let's practice on Codepen!</h2>
</section>
<section>
<h2>Creating lists with JS's map</h2>
<p><em>Reminder: We need to set the <strong>key</strong> attribute in each node in a list of nodes, for optimum React performance.</em></p>
<pre><code class="javascript">
class App extends React.Component {
constructor() {
super();
this.state = {
messages: [
{id: 'a', value: 'Good morning'},
{id: 'b', value: 'Good evening'},
{id: 'c', value: 'Good night'},
]
};
}
render() {
return (
<div>
<span>Greetings!<span>
<{this.state.messages.map(message =>
(<span key={message.id}>message.value</span>)
)}>
<div>
);
}
}
</code></pre>
</section>
<section>
<h2>For those newer to ES6: What are arrow functions?</h2>
<p>Arrow functions are a new, improved syntax for functions</p>
<pre><code class="javascript">
// Simplified (for when there's on argument and only one call in the function body)
this.state.messages.map(message =>
(<span key={message.id}>message.value</span>)
);
// More common
this.state.messages.map((message, index) => {
console.log(index);
return (<span key={message.id}>message.value</span>);
});
// ES5 equivalent
this.state.messages.map(function(message) {
return (<span key={message.id}>message.value</span>)
});
</code></pre>
<p>One benefit of using arrow functions is that they do not define their own `this`, so you don't need to use `var that=this` with them anymore. And they are more concise than ES5 functions. Yay!</p>
<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions">Read more about them at the MDN docs.</a></p>
</section>
<section>
<h2>Homework!</h2>
<ol>
<li>Read about ES6 if it is new to you:
<ul>
<li><a href="https://www.codeschool.com/courses/es2015-the-shape-of-javascript-to-come">Code School ES2015 course (first lesson is free)</a></li>
<li><a href="https://cantina.co/embracing-es6-part1/">Embracing Change By Using New ES6 Features (Cantina blog post)</a></li>
<li><a href="https://code.tutsplus.com/articles/use-ecmascript-6-today--net-31582">Use ECMAScript 6 Today</a></li>
</ul></li>
<li>Browse the <a href="https://facebook.github.io/react/">official React site</a> and complete the <a href="https://facebook.github.io/react/tutorial/tutorial.html">tutorial</a> (at least up to right before "Why Immutability Is Important").</li>
</ol>
</section>
<section>
<h2>Day 2!</h2>
<ul>
<li>Recap of Day 1: ES6, Props/State</li>
<li>Homework review</li>
<li>New terms: Lifecycle methods, functional components, composition</li>
<li>Forms</li>
<li>Practice - Todo app</li>
</ul>
</section>
<section>
<h2>Day 1 Review</h2>
<ul>
<li>Pros/cons of React</li>
<li>Classes + Components</li>
<li>Props</li>
<li>State</li>
<li>setState</li>
</section>
<section>
<h2>Pros and cons of React</h2>
<ul>
<li>Fast</li>
<li>Declarative - Easy to understand and maintain at scale (why?)</li>
<li>Flexible</li>
<li>Cross-platform mobile development (React Native)</li>
</ul>
</section>
<section>
<h2>Classes + Components</h2>
<ul>
<li>Classes: ES6 addition, allows object oriented programming (with JS functions behind the scenes)</li>
<li>Components: building blocks of React</li>
<li>Your components extend React.Component</li>
</ul>
</section>
<section>
<h2>JSX</h2>
<ul>
<li>Javascript that looks like HTML, and gets compiled to javascript that renders HTML</li>
<li><a href="https://babeljs.io/repl/">See what compiled JSX looks like!</a></li>
</ul>
</section>
<section>
<h2>Props</h2>
<ul>
<li>Attributes passed down from one component to another</li>
<li>A component's props can be accessed through this.props in the render function</li>
</ul>
</section>
<section>
<h2>State</h2>
<ul>
<li>Data stored in parent component, passed down to child components through props</li>
<li>Initialized in the constructor function</li>
<li>Updated with this.setState (this.state and this.props are immutable)</li>
</ul>
</section>
<section>
<h2>Why is immutability important?</h2>
<ul>
<li>Makes it easier to keep track of changes, test, and use time travel!</li>
<li>Thus we use <strong>this.setState</strong> instead of <strong>this.state = ...</strong></li>
<li>Helpful methods for copying data: Array.slice, Array.concat, and Object.assign</li>
</ul>
</section>
<section>
<h2>Homework review + practice!</h2>
<ul>
<li>Walkthrough of <a href="https://facebook.github.io/react/tutorial/tutorial.html">official React tutorial</a></li>
<li><a href="https://codepen.io/gaearon/pen/gWWZgR">Final result (official)</li>
<li><a href="https://codepen.io/gaearon/pen/gWWZgR">My final result (will demo here)</a></li>
</section>
<section>
<h2>Today's new topics!</h2>
<ul>
<li>Lifecycle hooks</li>
<li>Functional Components</li>
<li>Composition vs Inheritance</li>
<li>Forms (Controlled and uncontrolled components)</li>
</ul>
</section>
<section>
<h2>Lifecycle hooks</h2>
<ul>
<li><a href="https://facebook.github.io/react/docs/state-and-lifecycle.html">Docs</a></li>
<li><strong>componentWillMount**</strong>: Function called before component is rendered. This is the most commonly used lifecycle hook (AJAX calls, timers)</li>
<li><strong>componentDidMount</strong>: Function called after component is rendered. Useful for third party integrations if really needed (jQuery plugins)</li>
<li><strong>componentWillUnmount</strong>: Function called before component is removed from the DOM</li>
<li><strong>componentDidUnmount*</strong>: Function called after removal from DOM. Useful for cleanup (removing timers, etc.)</li>
<li><a href="https://codepen.io/gaearon/pen/vXdGmd">Official codepen</a></li>
<li><a href="https://codepen.io/arilaen/pen/OgNobO">My codepen</a></li>
</ul>
</section>
<section>
<h2>Functional Components</h2>
<p>For child components where you only need the render function, and do not need to modify the constructor or lifecycle methods, you can write your component more concisely! (<a href="https://facebook.github.io/react/docs/components-and-props.html">Docs</a>)</p>
<p><pre><code class="javascript">
function Welcome(props) {
return <h1>Hello, {props.name}</h1></h1>;
}
const element = <Welcome name="Sara" />;
ReactDOM.render(
element,
document.getElementById('root')
);
</code></pre></p>
<p><a href="http://codepen.io/gaearon/pen/YGYmEG?editors=0010">Official Codepen</a></p>
</section>
<section>
<h2>Composition vs Inheritance</h2>
<ul>
<li><a href="https://facebook.github.io/react/docs/composition-vs-inheritance.html">Docs</a></li>
<li>Components are very flexible - you can even pass in their contents with <strong><a href="https://codepen.io/gaearon/pen/ozqNOV">props.children</a></strong>!</li>
<li>You can also pass <a href="https://codepen.io/gaearon/pen/gwZOJp">JSX elements as your own props</a></li>
<li>And you can <a href="http://codepen.io/gaearon/pen/kkEaOZ?editors=0010">extend</a> your own <a href="http://codepen.io/gaearon/pen/gwZbYa?editors=0010">components</a>!</li>
<li>Composition is always preferred over inheritance in React - make components as reusable as possible!</li>
</ul>
</section>
<section>
<h2>Forms</h2>
<ul>
<li><a href="https://facebook.github.io/react/docs/forms.html">Docs</a></li>
<li>Controlled Components (recommended!)</li>
<li>Uncontrolled Components</li>
</ul>
</section>
<section>
<h2>Controlled Components</h2>
<ul>
<li>Forms have internal state, but controlled components keep React state in sync with form state</li>
<li>In practice: <strong>value</strong> for all types of inputs (including initial/default values), and run setState <strong>onChange</strong></li>
<li>Examples: <pre><code class="javascript">
<input type="text" value={this.state.value} onChange={this.handleChange} />
<textarea value={this.state.value} onChange={this.handleChange} />
<select value={this.state.value} onChange={this.handleChange}>
<option value="grapefruit">Grapefruit</option>...
</select>
</code></pre>
</li>
<li>Official codepen <a href="https://codepen.io/gaearon/pen/VmmPgp?editors=0010">1</a> and <a href="https://codepen.io/gaearon/pen/wgedvV?editors=0010">2</a></li>
<li>My codepen <a href="https://codepen.io/arilaen/pen/xrVjNR?editors=0010">1</a> and <a href="https://codepen.io/arilaen/pen/OgNZoE?editors=0010">2</a></li>
<li><a href="https://facebook.github.io/react/docs/forms.html">(Form Docs)</a></li>
</ul>
</section>
<section>
<h2>Uncontrolled Components</h2>
<ul>
<li><a href="https://facebook.github.io/react/docs/uncontrolled-components.html">Docs</a></li>
<li>Use <strong>refs</strong> to store internal form state, and use setState on submit</li>
<li>Use <strong>defaultValue</strong> to set initial values in form elements (input, select, etc.)</li>
<li>Usually not recommended if you can use controlled components (or Redux)</li>
<li><a href="https://goshakkk.name/controlled-vs-uncontrolled-inputs-react/">"Controlled and uncontrolled form inputs in React don't have to be complicated"</a></li>
<li><a href="https://codepen.io/gaearon/pen/WooRWa?editors=0010">Official codepen</a></li>
<li><a href="https://codepen.io/arilaen/pen/NgNMOE?editors=0010">My codepen</a></li>
</ul>
</section>
<section>
<h2>Homework</h2>
<ul>
<li><a href="https://facebook.github.io/react/docs/thinking-in-react.html">Thinking in React</a> exercise</li>
<li>and/or: Customize your todo app in codepen</li>
</ul>
</section>
<section>
<h2>Day 3 Agenda:</h2>
<ul>
<li>Recap of the basics and homework</li>
<li>Create todo app on Codepen + locally</li>
<li>CreateReactApp - create the todo app locally!</li>
<li>Personal projects</li>
</ul>
</section>
<section>
<h2>Day 1 + 2 Review</h2>
<ul>
<li>Props, State, setState</li>
<li>Classes + Components vs. Functional Components</li>
<li>Lifecycle hooks</li>
<li>Composition vs Inheritance</li>
<li>Forms (Controlled and uncontrolled components)</li>
</ul>
</section>
<section>
<h2>Lifecycle hooks (review)</h2>
<ul>
<li><a href="https://facebook.github.io/react/docs/state-and-lifecycle.html">Docs</a></li>
<li><strong>componentWillMount**</strong>: Function called before component is rendered. This is the most commonly used lifecycle hook (AJAX calls, timers)</li>
<li><strong>componentDidMount</strong>: Function called after component is rendered. Useful for third party integrations if really needed (jQuery plugins)</li>
<li><strong>componentWillUnmount</strong>: Function called before component is removed from the DOM</li>
<li><strong>componentDidUnmount*</strong>: Function called after removal from DOM. Useful for cleanup (removing timers, etc.)</li>
<li><a href="https://codepen.io/gaearon/pen/vXdGmd">Official codepen</a></li>
<li><a href="https://codepen.io/arilaen/pen/OgNobO">My codepen</a></li>
</ul>
</section>
<section>
<h2>Functional Components (review)</h2>
<p>For child components where you only need the render function, and do not need to modify the constructor or lifecycle methods, you can write your component more concisely! (<a href="https://facebook.github.io/react/docs/components-and-props.html">Docs</a>)</p>
<p><pre><code class="javascript">
function Welcome(props) {
return <h1>Hello, {props.name}</h1></h1>;
}
const element = <Welcome name="Sara" />;
ReactDOM.render(
element,
document.getElementById('root')
);
</code></pre></p>
<p><a href="http://codepen.io/gaearon/pen/YGYmEG?editors=0010">Official Codepen</a></p>
</section>
<section>
<h2>Composition vs Inheritance (review)</h2>
<ul>
<li><a href="https://facebook.github.io/react/docs/composition-vs-inheritance.html">Docs</a></li>
<li>Components are very flexible - you can even pass in their contents with <strong><a href="https://codepen.io/gaearon/pen/ozqNOV">props.children</a></strong>!</li>
<li>You can also pass <a href="https://codepen.io/gaearon/pen/gwZOJp">JSX elements as your own props</a></li>
<li>And you can <a href="http://codepen.io/gaearon/pen/kkEaOZ?editors=0010">extend</a> your own <a href="http://codepen.io/gaearon/pen/gwZbYa?editors=0010">components</a>!</li>
<li>Composition is always preferred over inheritance in React - make components as reusable as possible!</li>
</ul>
</section>
<section>
<h2>Controlled Components (review)</h2>
<ul>
<li>Forms have internal state, but controlled components keep React state in sync with form state</li>
<li>In practice: <strong>value</strong> for all types of inputs (including initial/default values), and run setState <strong>onChange</strong></li>
<li>Examples: <pre><code class="javascript">
<input type="text" value={this.state.value} onChange={this.handleChange} />
<textarea value={this.state.value} onChange={this.handleChange} />
<select value={this.state.value} onChange={this.handleChange}>
<option value="grapefruit">Grapefruit</option>...
</select>
</code></pre>
</li>
<li>Official codepen <a href="https://codepen.io/gaearon/pen/VmmPgp?editors=0010">1</a> and <a href="https://codepen.io/gaearon/pen/wgedvV?editors=0010">2</a></li>
<li>My codepen <a href="https://codepen.io/arilaen/pen/xrVjNR?editors=0010">1</a> and <a href="https://codepen.io/arilaen/pen/OgNZoE?editors=0010">2</a></li>
<li><a href="https://facebook.github.io/react/docs/forms.html">(Form Docs)</a></li>
</ul>
</section>
<section>
<h2>Uncontrolled Components (review)</h2>
<ul>
<li><a href="https://facebook.github.io/react/docs/uncontrolled-components.html">Docs</a></li>
<li>Use <strong>refs</strong> to store internal form state, and use setState on submit</li>
<li>Use <strong>defaultValue</strong> to set initial values in form elements (input, select, etc.)</li>
<li>Usually not recommended if you can use controlled components (or Redux)</li>
<li><a href="https://goshakkk.name/controlled-vs-uncontrolled-inputs-react/">"Controlled and uncontrolled form inputs in React don't have to be complicated"</a></li>
<li><a href="https://codepen.io/gaearon/pen/WooRWa?editors=0010">Official codepen</a></li>
<li><a href="https://codepen.io/arilaen/pen/NgNMOE?editors=0010">My codepen</a></li>
</ul>
</section>
<section>
<h2>Practice time!</h2>
<ul>
<li>We'll create a todo app, with initial data from <a href="http://jsonplaceholder.typicode.com/">http://jsonplaceholder.typicode.com</a></li>
<li>Follow along <a href="https://codepen.io/arilaen/pen/qjZJBm?editors=0110">here</a> (starting from scratch) or on your own Codepen</li>
<li>See the finished codepen <a href="https://codepen.io/arilaen/pen/bRgXNG?editors=0110">here</a></li>
</ul>
</section>
<section>
<h2>Make your own React project on your computer with <a href="https://github.com/facebookincubator/create-react-app">CreateReactApp</a></h2>
<ul>
<li>Abstracts away build system</li>
<li>Great for getting started and making personal sites</li>
</ul>
</section>
<section>
<h2>Getting started with CreateReactApp</h2>
<ul>
<li>Install npm <a href="https://github.com/creationix/nvm">(I recommend doing this with nvm)</a></li>
<li>npm install -g create-react-app</li>
<li>create-react-app my-app;cd my-app</li>
<li>npm start</li>
</ul>
</section>
<section>
<h2>Practice - create todo app locally!</h2>
<p>We'll do this together, and go over some things to know when coding locally, such as:</p>
<ul>
<li>Using the <strong>import</strong> and <strong>export</strong> statements</li>
<li>Adding a dependency (like jQuery)</li>
<li>Organizing files</li>
<li>Understanding compile and linter errors</li>
<li>Working example <a href="https://github.com/arilaen/create-react-app-demo">here on github</a></li>
</ul>
</section>
<section>
<h2>For next class:</h2>
<p>Start working on your own React project, or brainstorming ideas! Here are some to choose from:
<ul>
<li>Expand the todo app <em>(remake Trello?)</em></li>
<li>Create an album of things (movies, photos, songs, or books), allow the user to vote and/or choose favorites <em>(recreate Netflix?)</em></li>
<li>Create a store, allow user to add items to their cart, add reviews <em>(recreate Amazon?)</em></li>
<li>Display a newsfeed <em>(recreate Twitter?)</em></li>
</ul>
</section>
<section>
<h2>Project tips</h2>
<p>Remember that you can use <a href="http://jsonplaceholder.typicode.com/">http://jsonplaceholder.typicode.com</a> as a resource for mock data!</p>
<p>Or you can get fancy if you want to use Twitter data (<a href="https://dev.twitter.com/rest/public/search">docs for creating Twitter queries</a>)
<p>You can try a small project or something that you might expand after class - it's up to you!</p>
</section>
<section>
<h2>Advanced resources</h2>
<li>Egghead.io: <a href="https://egghead.io/courses/build-your-first-production-quality-react-app">Build Your First Production Quality React App</a>, <a href="https://egghead.io/courses/getting-started-with-redux">Getting Started with Redux</a>, <a href="https://egghead.io/courses/building-react-applications-with-idiomatic-redux">Building React Applications with Idiomatic Redux</a></li>
</section>
<section>
<h1>THE END</h1>
<h4>Special thanks to Girl Develop It and Hakim El Hattab / hakim.se for reveal.js!</h4>
</section>
</div>
<footer>
<div class="copyright">
Introduction to React.js -- Girl Develop It Boston
</div>
</footer>
</div>
<script src="dist/js/head.min.js"></script>
<script src="dist/js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{ src: 'dist/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, condition: function() { return !!document.querySelector( 'pre code' ); }, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/accessibility-helper/js/accessibility-helper.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>