-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1820 lines (1775 loc) · 68.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
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 lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>ReactJS Technical Documentation</title>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha256-L/W5Wfqfa0sdBNIKN9cG6QA5F2qx4qICmU2VgLruv9Y="
crossorigin="anonymous"
/>
<link rel="stylesheet" href="./src/styles.css" />
</head>
<body>
<div class="p-0">
<header class="header fixed-top">
<nav class="navbar container navbar-expand-sm px-4 px-sm-0">
<a class="navbar-brand" href="#">
<img
class="brand-logo"
src="./public/image/logo.svg"
alt="ReactJS logo"
/>
</a>
<div class="connections">
<input
class="d-none d-sm-inline form-control col-3 my-3 my-sm-1"
type="search"
placeholder="Search"
/>
<a href="https://codepen.io/amarjeetrao/" class="codepen">
<svg
class="social-icon"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 22.834"
>
<path
class="a"
d="M17.531,0H6.469A6.318,6.318,0,0,0,0,6.155V16.68a6.318,6.318,0,0,0,6.469,6.155H17.531A6.318,6.318,0,0,0,24,16.68V6.155A6.318,6.318,0,0,0,17.531,0Zm-.141,13a.827.827,0,0,1-.4.691l-4.552,2.859a.918.918,0,0,1-.966,0L7.008,13.745a.827.827,0,0,1-.4-.691V9.923a.832.832,0,0,1,.384-.682l4.387-2.908a.914.914,0,0,1,.984-.013l4.627,2.912a.827.827,0,0,1,.4.691Z"
></path>
</svg>
</a>
<a href="https://github.com/Amarjeetrao" class="github">
<svg
class="social-icon"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 23.428"
>
<path
class="a"
d="M22.4,11.378a11.942,11.942,0,0,0-4.369-4.369A11.73,11.73,0,0,0,12.005,5.4,11.75,11.75,0,0,0,5.978,7.009a11.942,11.942,0,0,0-4.369,4.369A11.745,11.745,0,0,0,0,17.4a11.661,11.661,0,0,0,2.288,7.057,11.724,11.724,0,0,0,5.918,4.336.689.689,0,0,0,.624-.109.606.606,0,0,0,.2-.471c0-.033,0-.312-.005-.843s-.005-1-.005-1.391l-.361.06a4.406,4.406,0,0,1-.865.055,6.863,6.863,0,0,1-1.084-.109,2.376,2.376,0,0,1-1.046-.471,1.99,1.99,0,0,1-.69-.964l-.159-.361a3.965,3.965,0,0,0-.493-.8,1.886,1.886,0,0,0-.679-.591l-.109-.077a1.3,1.3,0,0,1-.2-.186.993.993,0,0,1-.142-.219c-.033-.071-.005-.131.077-.181a1.015,1.015,0,0,1,.454-.06l.312.049a2.232,2.232,0,0,1,.772.378,2.552,2.552,0,0,1,.755.81,2.756,2.756,0,0,0,.865.974,1.834,1.834,0,0,0,1.024.334,4.672,4.672,0,0,0,.892-.077,3.2,3.2,0,0,0,.7-.235A2.518,2.518,0,0,1,9.81,22.7a11.169,11.169,0,0,1-1.6-.279,6.424,6.424,0,0,1-1.467-.608A4.217,4.217,0,0,1,5.48,20.767a5.005,5.005,0,0,1-.821-1.642,7.794,7.794,0,0,1-.323-2.343,4.549,4.549,0,0,1,1.237-3.219,4.2,4.2,0,0,1,.109-3.186,2.2,2.2,0,0,1,1.347.214,9.423,9.423,0,0,1,1.3.6c.274.164.5.307.662.422a11.311,11.311,0,0,1,6,0l.591-.372a8.215,8.215,0,0,1,1.44-.69,2.008,2.008,0,0,1,1.265-.17,4.158,4.158,0,0,1,.126,3.186,4.573,4.573,0,0,1,1.237,3.219,7.894,7.894,0,0,1-.323,2.354,4.8,4.8,0,0,1-.827,1.642,4.379,4.379,0,0,1-1.265,1.04,6.282,6.282,0,0,1-1.467.608,10.341,10.341,0,0,1-1.6.279,2.785,2.785,0,0,1,.81,2.217v3.3a.628.628,0,0,0,.2.471.67.67,0,0,0,.619.109,11.787,11.787,0,0,0,5.918-4.336A11.661,11.661,0,0,0,24,17.411,11.7,11.7,0,0,0,22.4,11.378Z"
transform="translate(0 -5.4)"
></path>
</svg>
</a>
<button
class="navbar-toggler d-block d-lg-none"
type="button"
data-toggle="collapse"
data-target="#navCollapse"
aria-controls="navCollapse"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="icon-toggler"></span>
</button>
</div>
</nav>
</header>
<aside
class="sidebar collapse navbar-collapse fixed-left d-lg-block pb-5"
id="navCollapse"
>
<nav class="navbar flex-column p-0 pb-5" id="navbar">
<header class="navbar-header align-self-start">
Getting Familiar with ReactJS
</header>
<ul class="list-group w-100 pb-5">
<li class="list-group-item px-4 px-lg-3">
<a href="#Hello_World" class="nav-link sidebar-active"
>Hello World</a
>
</li>
<li class="list-group-item px-4 px-lg-3">
<a href="#Introducing_JSX" class="nav-link">Introducing JSX</a>
</li>
<li class="list-group-item px-4 px-lg-3">
<a href="#Rendering_Elements" class="nav-link"
>Rendering Elements</a
>
</li>
<li class="list-group-item px-4 px-lg-3">
<a href="#Components_and_Props" class="nav-link"
>Components and Props</a
>
</li>
<li class="list-group-item px-4 px-lg-3">
<a href="#State_and_Lifecycle" class="nav-link"
>State and Lifecycle</a
>
</li>
<li class="list-group-item px-4 px-lg-3">
<a href="#Handling_Events" class="nav-link">Handling Events</a>
</li>
<li class="list-group-item px-4 px-lg-3">
<a href="#Conditional_Rendering" class="nav-link"
>Conditional Rendering</a
>
</li>
</ul>
</nav>
</aside>
<main class="main px-4 px-lg-0 mr-md-5" id="main-doc">
<section class="main-section" id="Hello_World">
<header class="section-header">
<h1 class="section-heading">Hello World</h1>
</header>
<article class="section-content">
<section class="article-sections">
<p class="article-overview my-4">
The smallest React example looks like this:
</p>
<pre class="code-container">
<code class="code-example">
ReactDOM.render(
<h1>Hello, world!</h1>,
document.getElementById('root')
);</code>
</pre>
<p>
<a
class="article-link"
href="https://reactjs.org/redirect-to-codepen/hello-world"
target="_blank"
>Try it on CodePen</a
>
</p>
<p>
Click the link above to open an online editor. Feel free to make
some changes, and see how they affect the output. Most pages in
this guide will have editable examples like this one.
</p>
</section>
<section class="article-sections py-2">
<h2 class="article-heading my-4">How to Read This Guide</h2>
<p>
This is the first chapter in a step-by-step guide about main
React concepts. You can find a list of all its chapters in the
navigation sidebar. If you’re reading this from a mobile device,
you can access the navigation by pressing the button in the
bottom right corner of your screen.
</p>
<p>
Every chapter in this guide builds on the knowledge introduced
in earlier chapters.
<strong
>You can learn most of React by reading the
<q>Main Concepts</q> guide chapters in the order they appear
in the sidebar.</strong
>
For example, <a href="#Introducing_JSX">Introducing JSX</a> is
the next chapter after this one.
</p>
</section>
<section class="article-sections py-2">
<h2 class="article-heading my-4">Knowledge Level Assumptions</h2>
<p>
React is a JavaScript library, and so we’ll assume you have a
basic understanding of the JavaScript language.
<strong
>If you don’t feel very confident, we recommend
<a
href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript"
>going through a JavaScript tutorial</a
>
to check your knowledge level</strong
>
and enable you to follow along this guide without getting lost.
It might take you between 30 minutes and an hour, but as a
result you won’t have to feel like you’re learning both React
and JavaScript at the same time.
</p>
</section>
<section class="article-sections last-article-section py-2">
<h2 class="article-heading my-4">Let’s Get Started!</h2>
<p>
Now you done with this page let go to the
<a href="#Introducing_JSX">next chapter</a>.
</p>
</section>
</article>
</section>
<section class="main-section" id="Introducing_JSX">
<header class="section-header">
<h1 class="section-heading">Introducing JSX</h1>
</header>
<article class="section-content">
<section class="article-sections">
<p class="article-overview my-4">
Consider this variable declaration:
</p>
<pre class="code-container">
<code class="code-example">
const element = <h1>Hello, world!</h1>;</code>
</pre>
<p>
This funny tag syntax is neither a string nor HTML.
</p>
<p>
It is called JSX, and it is a syntax extension to JavaScript. We
recommend using it with React to describe what the UI should
look like. JSX may remind you of a template language, but it
comes with the full power of JavaScript.
</p>
<p>
JSX produces React “elements”. We will explore rendering them to
the DOM in the next section. Below, you can find the basics of
JSX necessary to get you started.
</p>
</section>
<section class="article-sections py-2">
<h2 class="article-heading my-4">Why JSX?</h2>
<p>
React embraces the fact that rendering logic is inherently
coupled with other UI logic: how events are handled, how the
state changes over time, and how the data is prepared for
display.
</p>
<p>
Instead of artificially separating technologies by putting
markup and logic in separate files, React separates concerns
with loosely coupled units called <q>components</q> that contain
both. We will come back to components in a further section, but
if you’re not yet comfortable putting markup in JS, this talk
might convince you otherwise.
</p>
<p>
React doesn’t require using JSX, but most people find it helpful
as a visual aid when working with UI inside the JavaScript code.
It also allows React to show more useful error and warning
messages.
</p>
<p>
With that out of the way, let’s get started!
</p>
</section>
<section class="article-sections py-2">
<h2 class="article-heading my-4">Embedding Expressions in JSX</h2>
<p>
In the example below, we declare a variable called name and then
use it inside JSX by wrapping it in curly braces:
</p>
<pre class="code-container">
<code class="code-example">
const name = 'Josh Perez';
const element = <h1>Hello, {name}</h1>;
ReactDOM.render(
element,
document.getElementById('root')
);</code>
</pre>
<p>
You can put any valid JavaScript expression inside the curly
braces in JSX. For example, 2 + 2, user.firstName, or
formatName(user) are all valid JavaScript expressions.
</p>
<p>
In the example below, we embed the result of calling a
JavaScript function, formatName(user), into an <h1>
element.
</p>
<pre class="code-container">
<code class="code-example">
function formatName(user) {
return user.firstName + ' ' + user.lastName;
}
const user = {
firstName: 'Harper',
lastName: 'Perez'
};
const element = (
<h1>
Hello, {formatName(user)}!
</h>
);
ReactDOM.render(
element,
document.getElementById('root')
);</code>
</pre>
<p>
<a
class="article-link"
href="https://reactjs.org/redirect-to-codepen/introducing-jsx"
target="_blank"
>Try it on CodePen</a
>
</p>
<p>
We split JSX over multiple lines for readability. While it isn’t
required, when doing this, we also recommend wrapping it in
parentheses to avoid the pitfalls of automatic semicolon
insertion.
</p>
</section>
<section class="article-sections py-2">
<h2 class="article-heading my-4">JSX is an Expression Too</h2>
<p>
After compilation, JSX expressions become regular JavaScript
function calls and evaluate to JavaScript objects.
</p>
<p>
This means that you can use JSX inside of if statements and for
loops, assign it to variables, accept it as arguments, and
return it from functions:
</p>
<pre class="code-container">
<code class="code-example">
function getGreeting(user) {
if (user) {
return <h1>Hello, {formatName(user)}!</h1>;
}
return <h1>Hello, Stranger.</h1>;
}</code>
</pre>
</section>
<section class="article-sections py-2">
<h2 class="article-heading my-4">
Specifying Attributes with JSX
</h2>
<p>
You may use quotes to specify string literals as attributes:
</p>
<pre class="code-container">
<code class="code-example">
const element = <div tabIndex="0"></div>;</code>
</pre>
<p>
You may also use curly braces to embed a JavaScript expression
in an attribute:
</p>
<pre class="code-container">
<code class="code-example">
const element = <img src={user.avatarUrl}></img>;</code>
</pre>
<p>
Don’t put quotes around curly braces when embedding a JavaScript
expression in an attribute. You should either use quotes (for
string values) or curly braces (for expressions), but not both
in the same attribute.
</p>
</section>
<section class="article-sections py-2">
<h2 class="article-heading my-4">
Specifying Children with JSX
</h2>
<p>
If a tag is empty, you may close it immediately with />, like
XML:
</p>
<pre class="code-container">
<code class="code-example">
const element = <img src={user.avatarUrl} />;</code>
</pre>
<p>
JSX tags may contain children:
</p>
<pre class="code-container">
<code class="code-example">
const element = (
<div>
<h1>Hello!</h1>
<h2>Good to see you here.</h2>
</div>
);</code>
</pre>
</section>
<section class="article-sections py-2">
<h2 class="article-heading my-4">
JSX Prevents Injection Attacks
</h2>
<p>
It is safe to embed user input in JSX:
</p>
<pre class="code-container">
<code class="code-example">
const title = response.potentiallyMaliciousInput;
// This is safe:
const element = >h1>{title}>/h1>;</code>
</pre>
<p>
By default, React DOM escapes any values embedded in JSX before
rendering them. Thus it ensures that you can never inject
anything that’s not explicitly written in your application.
Everything is converted to a string before being rendered. This
helps prevent XSS (cross-site-scripting) attacks.
</p>
</section>
<section class="article-sections last-article-section py-2">
<h2 class="article-heading my-4">
JSX Represents Objects
</h2>
<p>
Babel compiles JSX down to React.createElement() calls.
</p>
<p>
These two examples are identical:
</p>
<pre class="code-container">
<code class="code-example">
const element = (
<h1 className="greeting">
Hello, world!
</h1>
);</code>
</pre>
<pre class="code-container">
<code class="code-example">
const element = React.createElement(
'h1',
{className: 'greeting'},
'Hello, world!'
);</code>
</pre>
<p>
React.createElement() performs a few checks to help you write
bug-free code but essentially it creates an object like this:
</p>
<pre class="code-container">
<code class="code-example">
// Note: this structure is simplified
const element = {
type: 'h1',
props: {
className: 'greeting',
children: 'Hello, world!'
}
};</code>
</pre>
<p>
These objects are called “React elements”. You can think of them
as descriptions of what you want to see on the screen. React
reads these objects and uses them to construct the DOM and keep
it up to date.
</p>
<p>
We will explore rendering React elements to the DOM in the next
section.
</p>
</section>
</article>
</section>
<section class="main-section" id="Rendering_Elements">
<header class="section-header">
<h1 class="section-heading">Rendering Elements</h1>
</header>
<article class="section-content">
<section class="article-sections">
<p class="article-overview my-4">
Elements are the smallest building blocks of React apps.
</p>
<p>
An element describes what you want to see on the screen:
</p>
<pre class="code-container">
<code class="code-example">
const element = <h1>Hello, world</h1>;</code>
</pre>
<p>
Unlike browser DOM elements, React elements are plain objects,
and are cheap to create. React DOM takes care of updating the
DOM to match the React elements.
</p>
</section>
<section class="article-sections py-2">
<h2 class="article-heading my-4">
Rendering an Element into the DOM
</h2>
<p>
Let’s say there is a <div> somewhere in your HTML file:
</p>
<pre class="code-container">
<code class="code-example">
<div id="root"></div></code>
</pre>
<p>
We call this a “root” DOM node because everything inside it will
be managed by React DOM.
</p>
<p>
Applications built with just React usually have a single root
DOM node. If you are integrating React into an existing app, you
may have as many isolated root DOM nodes as you like.
</p>
<p>
To render a React element into a root DOM node, pass both to
ReactDOM.render():
</p>
<pre class="code-container">
<code class="code-example">
const element = <h1>Hello, world</h1>;
ReactDOM.render(element, document.getElementById('root'));</code>
</pre>
<p>
It displays “Hello, world” on the page.
</p>
</section>
<section class="article-sections py-2">
<h2 class="article-heading my-4">
Updating the Rendered Element
</h2>
<p>
React elements are immutable. Once you create an element, you
can’t change its children or attributes. An element is like a
single frame in a movie: it represents the UI at a certain point
in time.
</p>
<p>
With our knowledge so far, the only way to update the UI is to
create a new element, and pass it to ReactDOM.render().
</p>
<p>
Consider this ticking clock example:
</p>
<pre class="code-container">
<code class="code-example">
function tick() {
const element = (
<div>
<h1>Hello, world!</h1>
<h2>It is {new Date().toLocaleTimeString()}.</h2>
</div>
);
ReactDOM.render(element, document.getElementById('root'));
}
setInterval(tick, 1000);</code>
</pre>
<p>
It calls ReactDOM.render() every second from a setInterval()
callback.
</p>
</section>
<section class="article-sections last-article-section py-2">
<h2 class="article-heading my-4">
React Only Updates What’s Necessary
</h2>
<p>
React DOM compares the element and its children to the previous
one, and only applies the DOM updates necessary to bring the DOM
to the desired state
</p>
<p>
You can verify by inspecting the last example with the browser
tools:
</p>
<img
class="mb-4"
src="https://reactjs.org/granular-dom-updates-c158617ed7cc0eac8f58330e49e48224.gif"
alt="DOM inspector showing granular updates"
/>
<p>
Even though we create an element describing the whole UI tree on
every tick, only the text node whose contents has changed gets
updated by React DOM.
</p>
<p>
In our experience, thinking about how the UI should look at any
given moment rather than how to change it over time eliminates a
whole class of bugs.
</p>
</section>
</article>
</section>
<section class="main-section" id="Components_and_Props">
<header class="section-header">
<h1 class="section-heading">Components and Props</h1>
</header>
<article class="section-content">
<section class="article-sections">
<p class="article-overview my-4">
Components let you split the UI into independent, reusable
pieces, and think about each piece in isolation. This page
provides an introduction to the idea of components.
</p>
<p>
Conceptually, components are like JavaScript functions. They
accept arbitrary inputs (called “props”) and return React
elements describing what should appear on the screen.
</p>
</section>
<section class="article-sections py-2">
<h2 class="article-heading my-4">
Function and Class Components
</h2>
<p>
The simplest way to define a component is to write a JavaScript
function:
</p>
<pre class="code-container">
<code class="code-example">
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}</code>
</pre>
<p>
This function is a valid React component because it accepts a
single “props” (which stands for properties) object argument
with data and returns a React element. We call such components
“function components” because they are literally JavaScript
functions.
</p>
<p>
You can also use an ES6 class to define a component:
</p>
<pre class="code-container">
<code class="code-example">
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}</code>
</pre>
<p>
The above two components are equivalent from React’s point of
view.
</p>
<p>
Classes have some additional features that we will discuss in
the next sections. Until then, we will use function components
for their conciseness.
</p>
</section>
<section class="article-sections py-2">
<h2 class="article-heading my-4">
Rendering a Component
</h2>
<p>
Previously, we only encountered React elements that represent
DOM tags:
</p>
<pre class="code-container">
<code class="code-example">
const element = <div />;</code>
</pre>
<p>
However, elements can also represent user-defined components:
</p>
<pre class="code-container">
<code class="code-example">
const element = <Welcome name="Sara" />;</code>
</pre>
<p>
When React sees an element representing a user-defined
component, it passes JSX attributes to this component as a
single object. We call this object “props”.
</p>
<p>
For example, this code renders “Hello, Sara” on the page:
</p>
<pre class="code-container">
<code class="code-example">
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
const element = <Welcome name="Sara" />;
ReactDOM.render(
element,
document.getElementById('root')
);</code>
</pre>
<p>
Let’s recap what happens in this example:
</p>
<ol start="1">
<li>
We call ReactDOM.render() with the <Welcome name="Sara"
/> element.
</li>
<li>
React calls the Welcome component with {name: 'Sara'} as the
props.
</li>
<li>
Our Welcome component returns a <h1>Hello,
Sara</h1> element as the result.
</li>
<li>
React DOM efficiently updates the DOM to match
<h1>Hello, Sara</h1>.
</li>
</ol>
</section>
<section class="article-sections py-2">
<h2 class="article-heading my-4">
Composing Components
</h2>
<p>
Components can refer to other components in their output. This
lets us use the same component abstraction for any level of
detail. A button, a form, a dialog, a screen: in React apps, all
those are commonly expressed as components.
</p>
<p>
For example, we can create an App component that renders Welcome
many times:
</p>
<pre class="code-container">
<code class="code-example">
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
function App() {
return (
<div>
<Welcome name="Sara" />
<Welcome name="Cahal" />
<Welcome name="Edite" />
</div>
);
}
ReactDOM.render(
<App />,
document.getElementById('root')
);</code>
</pre>
<p>
Typically, new React apps have a single App component at the
very top. However, if you integrate React into an existing app,
you might start bottom-up with a small component like Button and
gradually work your way to the top of the view hierarchy.
</p>
</section>
<section class="article-sections py-2">
<h2 class="article-heading my-4">
Extracting Components
</h2>
<p>
Don’t be afraid to split components into smaller components.
</p>
<p>
For example, consider this Comment component:
</p>
<pre class="code-container">
<code class="code-example">
function Comment(props) {
return (
<div className="Comment">
<div className="UserInfo">
<img className="Avatar"
src={props.author.avatarUrl}
alt={props.author.name}
/>
<div className="UserInfo-name">
{props.author.name}
</div>
</div>
<div className="Comment-text">
{props.text}
</div>
<div className="Comment-date">
{formatDate(props.date)}
</div>
</div>
);
}</code>
</pre>
<p>
It accepts author (an object), text (a string), and date (a
date) as props, and describes a comment on a social media
website.
</p>
<p>
This component can be tricky to change because of all the
nesting, and it is also hard to reuse individual parts of it.
Let’s extract a few components from it.
</p>
<p>
First, we will extract Avatar:
</p>
<pre class="code-container">
<code class="code-example">
function Avatar(props) {
return (
<img className="Avatar"
src={props.user.avatarUrl}
alt={props.user.name}
/>
);
}</code>
</pre>
<p>
The Avatar doesn’t need to know that it is being rendered inside
a Comment. This is why we have given its prop a more generic
name: user rather than author.
</p>
<p>
We recommend naming props from the component’s own point of view
rather than the context in which it is being used.
</p>
<p>
We can now simplify Comment a tiny bit:
</p>
<pre class="code-container">
<code class="code-example">
function Comment(props) {
return (
<div className="Comment">
<div className="UserInfo">
<Avatar user={props.author} />
<div className="UserInfo-name">
{props.author.name}
</div>
</div>
<div className="Comment-text">
{props.text}
</div>
<div className="Comment-date">
{formatDate(props.date)}
</div>
</div>
);
}</code>
</pre>
<p>
Next, we will extract a UserInfo component that renders an
Avatar next to the user’s name:
</p>
<pre class="code-container">
<code class="code-example">
function UserInfo(props) {
return (
<div className="UserInfo">
<Avatar user={props.user} />
<div className="UserInfo-name">
{props.user.name}
</div>
</div>
);
}</code>
</pre>
<p>
This lets us simplify Comment even further:
</p>
<pre class="code-container">
<code class="code-example">
function Comment(props) {
return (
<div className="Comment">
<UserInfo user={props.author} />
<div className="Comment-text">
{props.text}
</div>
<div className="Comment-date">
{formatDate(props.date)}
</div>
</div>
);
}</code>
</pre>
<p>
<a
class="article-link"
href="https://reactjs.org/redirect-to-codepen/components-and-props/extracting-components-continued"
target="_blank"
>Try it on CodePen</a
>
</p>
<p>
Extracting components might seem like grunt work at first, but
having a palette of reusable components pays off in larger apps.
A good rule of thumb is that if a part of your UI is used
several times (Button, Panel, Avatar), or is complex enough on
its own (App, FeedStory, Comment), it is a good candidate to be
a reusable component.
</p>
</section>
<section class="article-sections last-article-section py-2">
<h2 class="article-heading my-4">
Props are Read-Only
</h2>
<p>
Whether you declare a component as a function or a class, it
must never modify its own props. Consider this sum function:
</p>
<pre class="code-container">
<code class="code-example">
function sum(a, b) {
return a + b;
}</code>
</pre>
<p>
Such functions are called “pure” because they do not attempt to
change their inputs, and always return the same result for the
same inputs.
</p>
<p>
In contrast, this function is impure because it changes its own
input:
</p>
<pre class="code-container">
<code class="code-example">
function withdraw(account, amount) {
account.total -= amount;
}</code>
</pre>
<p>
React is pretty flexible but it has a single strict rule:
</p>
<p>
<strong>
All React components must act like pure functions with respect
to their props.
</strong>
</p>
<p>
Of course, application UIs are dynamic and change over time. In
the next section, we will introduce a new concept of “state”.
State allows React components to change their output over time
in response to user actions, network responses, and anything
else, without violating this rule.
</p>
</section>
</article>
</section>
<section class="main-section" id="State_and_Lifecycle">
<header class="section-header">
<h1 class="section-heading">State and Lifecycle</h1>
</header>
<article class="section-content">
<section class="article-sections">
<p class="article-overview my-4">
This page introduces the concept of state and lifecycle in a
React component.
</p>
<p>
Consider the ticking clock example from one of the previous
sections. In Rendering Elements, we have only learned one way to
update the UI. We call ReactDOM.render() to change the rendered
output:
</p>
<pre class="code-container">
<code class="code-example">
function tick() {
const element = (
<div>
<h1>Hello, world!</h1>
<h2>It is {new Date().toLocaleTimeString()}.</h2>
</div>
);
ReactDOM.render(
element,
document.getElementById('root')
);
}
setInterval(tick, 1000);</code>
</pre>
<p>
In this section, we will learn how to make the Clock component
truly reusable and encapsulated. It will set up its own timer
and update itself every second.
</p>
<p>
We can start by encapsulating how the clock looks:
</p>
<pre class="code-container">
<code class="code-example">
function Clock(props) {
return (
<div>
<h1>Hello, world!</h1>
<h2>It is {props.date.toLocaleTimeString()}.</h2>
</div>
);
}
function tick() {
ReactDOM.render(
<Clock date={new Date()} />,
document.getElementById('root')
);
}
setInterval(tick, 1000);</code>
</pre>
<p>
However, it misses a crucial requirement: the fact that the
Clock sets up a timer and updates the UI every second should be
an implementation detail of the Clock.
</p>
<p>
Ideally we want to write this once and have the Clock update