-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReact.html
2310 lines (2213 loc) · 73.5 KB
/
React.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>
<!-- ad sense tag-->
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1733919132650092"
crossorigin="anonymous"></script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-FBJL1SB5QM"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "G-FBJL1SB5QM");
</script>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="title" content="Coding-nation" />
<meta name="description"
content="CodingNation is a comprehensive code tutorial website, providing beginner to advanced coding tutorials, coding exercises, and coding challenges to help you learn to code and build your skills." />
<meta name="keywords"
content="Coding-nation, CodeNation, HTML tutorials, python tutorials, css tutorials, c tutorials,React js tutorials, javascript tutorials,java tutorials, php tutorials, code editor, online code editor, Codebot,chatbot,quizzes" />
<meta name="robots" content="index, follow" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="English" />
<meta name="author" content="coding-nation.com" />
<title>React Js | Coding-nation</title>
<link rel="icon" type="image/png" sizes="32x32" href="../images/favicon.png" />
<!-- Latest compiled and minified CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" />
<!-- Latest compiled JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://unpkg.com/[email protected]/css/boxicons.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css" rel="stylesheet" />
<link rel="stylesheet" href="../Styles/Notes.css" />
<script>
function changeTitle(newTitle) {
document.title = newTitle;
}
</script>
</head>
<body>
<!--for whole page-->
<div class="contain">
<!--navigation bar-->
<header class="fixed-navbar">
<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
<div class="container-fluid">
<a class="navbar-brand" href="../index.html">CodingNation</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto" id="navbarNavLinks">
<li class="nav-item">
<a class="nav-link" href="../index.html">Home</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">Courses</a>
<ul class="dropdown-menu">
<li>
<a class="dropdown-item" href="../Python.html">Python</a>
</li>
<li>
<a class="dropdown-item" href="../React.html">React JS</a>
</li>
<li>
<a class="dropdown-item" href="../html.html">HTML</a>
</li>
<li><a class="dropdown-item" href="../css.html">CSS</a></li>
<li>
<a class="dropdown-item" href="../jascri.html">JavaScript</a>
</li>
<li><a class="dropdown-item" href="../cpro.html">C</a></li>
<li>
<a class="dropdown-item" href="../java.html">Java</a>
</li>
<li><a class="dropdown-item" href="../php.html">PHP</a></li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown">Practice</a>
<ul class="dropdown-menu">
<li>
<a class="dropdown-item" href="../HTMLQuiz.html" target="_blank">HTML
Quiz</a>
</li>
<li>
<a class="dropdown-item" href="../CSSQuiz.html" target="_blank">CSS
Quiz</a>
</li>
<li>
<a class="dropdown-item" href="../JSQuiz.html" target="_blank">JavaScript
Quiz</a>
</li>
<li>
<a class="dropdown-item" href="../CQuiz.html" target="_blank">C Quiz</a>
</li>
<li>
<a class="dropdown-item" href="../JAVAQuiz.html" target="_blank">Java
Quiz</a>
</li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button"
data-bs-toggle="dropdown">Exercises</a>
<ul class="dropdown-menu">
<li>
<a class="dropdown-item" href="../HTMLExer.html" target="_blank">HTML Exercise</a>
</li>
<li>
<a class="dropdown-item" href="../CSSExer.html" target="_blank">CSS Exercise</a>
</li>
<li>
<a class="dropdown-item" href="../JsExer.html" target="_blank">JavaScript
Exercise</a>
</li>
<li>
<a class="dropdown-item" href="../CExer.html" target="_blank">C Exercise</a>
</li>
<li>
<a class="dropdown-item" href="../JAVAExer.html" target="_blank">Java Exercise</a>
</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="../ codebot.html" target="_blank">CodeBot</a>
</li>
<li class="nav-item">
<a class="nav-link" href="../ Editor.html" target="_blank">Editor</a>
</li>
<li class="nav-item">
<a class="nav-link" href="../ About.html">About</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<!--sidebar-->
<button class="topics" onclick="toggleSidebar()">Topics▼</button>
<aside class="fixed-sidebar" id="myNav">
<div class="menu">
<h2>Introduction</h2>
<button id="ReactHome" class="single" data-index="0" onclick="changeTitle('React Home | CodingNation')">
React Home
</button>
<button id="reactSetup" class="single" data-index="1" onclick="changeTitle('react Setup | CodingNation')">
React Setup
</button>
<button id="ReactGettingStarted" class="single" data-index="2"
onclick="changeTitle('React Getting Started | CodingNation')">
React Getting Started
</button>
<button id="ReactES6" class="single" data-index="3" onclick="changeTitle('React ES6 | CodingNation')">
React ES6
</button>
<button id="ReactRenderHTML" class="single" data-index="4"
onclick="changeTitle('React Render HTML | CodingNation')">
React Render HTML
</button>
<button id="ReactJSX" class="single" data-index="5" onclick="changeTitle('React JSX | CodingNation')">
React JSX
</button>
<button id="ReactComponents" class="single" data-index="6"
onclick="changeTitle('React Components | CodingNation')">
React Components
</button>
<button id="ReactClass" class="single" data-index="7" onclick="changeTitle('React Class | CodingNation')">
React Class
</button>
<button id="ReactProps" class="single" data-index="8" onclick="changeTitle('React Props | CodingNation')">
React Props
</button>
<button id="ReactEvents" class="single" data-index="9" onclick="changeTitle('React Events | CodingNation')">
React Events
</button>
<button id="ReactConditional" class="single" data-index="10"
onclick="changeTitle('React Conditional | CodingNation')">
React Conditional
</button>
<button id="ReactLists" class="single" data-index="11" onclick="changeTitle('React Lists | CodingNation')">
React Lists
</button>
<button id="ReactForms" class="single" data-index="12" onclick="changeTitle('React Forms | CodingNation')">
React Forms
</button>
<button id="ReactRouter" class="single" data-index="13" onclick="changeTitle('React Router | CodingNation')">
React Router
</button>
<button id="ReactMemo" class="single" data-index="14" onclick="changeTitle('React Memo | CodingNation')">
React Memo
</button>
<button id="ReactCSSStyling" class="single" data-index="15"
onclick="changeTitle('React CSS Styling | CodingNation')">
React CSS Styling
</button>
<h2>React Hooks</h2>
<button id="WhatisaHook" class="single" data-index="16" onclick="changeTitle('What is a Hook? | CodingNation')">
What is a Hook?
</button>
<button id="ReactuseStateHook" class="single" data-index="17"
onclick="changeTitle('React useState Hook | CodingNation')">
React useState Hook
</button>
<button id="ReactuseEffectHook" class="single" data-index="18"
onclick="changeTitle('React useEffect Hook | CodingNation')">
React useEffect Hook
</button>
<button id="ReactuseContext" class="single" data-index="19"
onclick="changeTitle('React useContext | CodingNation')">
React useContext
</button>
<button id="ReactuseRef" class="single" data-index="20" onclick="changeTitle('React useRef | CodingNation')">
React useRef
</button>
<button id="ReactuseReducer" class="single" data-index="21"
onclick="changeTitle('React useReducer | CodingNation')">
React useReducer
</button>
<button id="ReactuseCallback" class="single" data-index="22"
onclick="changeTitle('React useCallback | CodingNation')">
React useCallback
</button>
<button id="ReactuseMemo" class="single" data-index="23" onclick="changeTitle('React useMemo | CodingNation')">
React useMemo
</button>
</div>
</aside>
<!--for content side-->
<main class="main-content">
<div class="content">
<!--previous and next buttons-->
<button id="previous">Previous</button>
<button id="next">Next</button>
<!--Notes-->
<div class="notes" id="div1">
<h2>React Home</h2>
<br />
<h3>What is ReactJS?</h3>
<br />
<p>
ReactJS is a <b>JavaScript</b> library used to build User
Interfaces(UI). It significantly decreases the code with it's
components, states i.e. hooks, etc.
</p>
<br />
<h3>Creating react app</h3>
<br />
<p>
Open your terminal in the directory you would like to create your
application. Run this command to create a React application named
my-react-app:
</p>
<br />
<pre class="highlight">
<code class="language-JS">
npx create-react-app my-react-app
</code>
</pre>
<br />
<br />
<p>
OR, you can directly make your application without specifying a
name, like this:
</p>
<br />
<pre class="highlight">
<code class="language-JS">
npx create-react-app .
</code>
</pre>
<br />
<br />
<p>
In this case, all files will be kept in the current directory.
<br />
<b>Note:</b>When choosing folder name, make sure there are no
spaces or capital letters because of npm naming restrictions.
<br />
Once base application is created, if folder specified you just
have to enter the folder. You can use this command to enter:
</p>
<br />
<pre class="highlight">
<code class="language-JS">
cd directory-name
</code>
</pre>
<br />
<br />
<p>Then just start up the application with this command:</p>
<br />
<pre class="highlight">
<code class="language-JS">
npm start
</code>
</pre>
<br />
<br />
<p>and you are good to go!</p>
<br />
<h3>Hello World</h3>
<br />
<pre class="highlight">
<code class="language-JS">
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<h1>Hello, world!</h1>);
</code>
</pre>
<br />
<br />
<p>
In this we are just putting <h1> tag in a div with id
'root'. That's it! In div with id 'root' everything will be
rendered. We can also change it from 'root' to something else, as
we are just getting an element and putting HTML in it.
</p>
<br />
<br />
</div>
<div class="notes" id="div2" style="display: none">
<h2>React Setup</h2>
<br />
<h3>Node.js</h3>
<br />
<p>You can download it from: https://nodejs.org/en/</p>
<br />
<h3>Creating our first react app</h3>
<br />
<p>
Open your terminal in the directory you would like to create your
application. Run this command to create a React application named
my-react-app:
</p>
<br />
<pre class="highlight">
<code class="language-JS">
npx create-react-app my-react-app
</code>
</pre>
<br />
<br />
<p>
OR, you can directly make your application without specifying a
name, like this:
</p>
<br />
<pre class="highlight">
<code class="language-JS">
npx create-react-app .
</code>
</pre>
<br />
<br />
<p>
In this case, all files will be kept in the current directory.
<br />
<b>Note:</b> When choosing folder name, make sure there are no
spaces or capital letters because of npm naming restrictions.
<br />
Once base application is created, if folder specified you just
have to enter the folder. You can use this command to enter:
</p>
<br />
<pre class="highlight">
<code class="language-JS">
cd directory-name
</code>
</pre>
<br />
<br />
<p>Then just start up the application with this command:</p>
<br />
<pre class="highlight">
<code class="language-JS">
npm start
</code>
</pre>
<br />
<br />
<p>and you are good to go!</p>
<br />
<br />
</div>
<div class="notes" id="div3" style="display: none">
<h2>React Getting Started</h2>
<br />
<h3>Run and Check</h3>
<br />
<p>
Run the React Application with this command:
<br />
A new browser window will pop up, if it does't then go on
http://localhost:3000/. Check if it is showing the same page:
</p>
<br />
<img
src="https://cwh-full-next-space.fra1.digitaloceanspaces.com/tutorial/react-getting-started/react-intro-page.png"
alt="" /><br />
<p>If it's the same page then you are good to go!</p>
<br />
<h3>Hello World</h3>
<br />
<p>
For this first you need to navigate to src/App.js, it will look
like:
</p>
<br />
<pre class="highlight">
<code class="language-JS">
import logo from './logo.svg';
import './App.css';
function App() {
return (
<div className="App"
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code>
and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
export default App;
</code>
</pre>
<br />
<br />
<p>
Whatever you put in return will be rendered as HTML on the page,
you can change it like:
</p>
<br />
<pre class="highlight">
<code class="language-JS">
function App() {
return (
<div className="App">
Hello World
</div>
);
}
</code>
</pre>
<br />
<br />
<p>
<b>Note:</b>Remember to wrap whole return value in an HTML element
as you can't return multiple elements but you can return multiple
elements in one element.
<br />
Page would look like this:
</p>
<br />
<img
src="https://cwh-full-next-space.fra1.digitaloceanspaces.com/tutorial/react-getting-started/react-hello-world.png"
alt="" /><br />
<br />
</div>
<div class="notes" id="div4" style="display: none">
<h2>React ES6</h2>
<br />
<h3>What is ES6?</h3>
<br />
<p>
ES6 stands for ECMAScript 6. ECMAScript is a JavaScript standard
intended to ensure a common language across different browsers.
ES6 is the 6th version of ECMAScript.
</p>
<br />
<h3>Why ES6? / Features of ES6 / Upgrades in ES6</h3>
<br />
<p>
React uses ES6 and all of these new features will make your coding
experience in react much much better. You will be able to do
things with much more ease and in very less lines! Features like:
</p>
<br />
<li><b>Arrow Functions:</b></li>
<br />
<pre class="highlight">
<code class="language-JS">
const hello = () => {
return "Hello World!";
}
</code>
</pre>
<br />
<br />
<p>Or</p>
<br />
<pre class="highlight">
<code class="language-JS">
const hello = () => "Hello World!";
</code>
</pre>
<br />
<br />
<li>
<b>.map():</b>
<color>.map</color> can be used for alot of things, one of it's
use case is, we can make any number of cards through loop and just
put it in jsx, like this:
</li>
<br />
<pre class="highlight">
<code class="language-JS">
const data = ['title1', 'title2', 'title3'];
let cards = data.map((item) => <card>{item}</card>)
</code>
</pre>
<br />
<br />
<li><b>Destructuring:</b></li>
<br />
<p>Old way:</p>
<br />
<pre class="highlight">
<code class="language-JS">
const languages = ['JS', 'Python', 'Java'];
const js = languages[0]
const python = languages[1]
const java = languages[2]
</code>
</pre>
<br />
<br />
<p>New way:</p>
<br />
<pre class="highlight">
<code class="language-JS">
const languages = ['JS', 'Python', 'Java'];
const [ js, python, java ] = languages
</code>
</pre>
<br />
<br />
<li>
<b>Ternary Operator:</b> With this, you can write if/else
conditions in one line. It's syntax is fairly simple like this:
</li>
<br />
<pre class="highlight">
<code class="language-JS">
condition ? <expression if true> : <expression if false>
</code>
</pre>
<br />
<br />
<h4>Example:</h4>
<br />
<pre class="highlight">
<code class="language-JS">
let loading = false;
const data = loading ? <div>Loading...</div> : <div>Data</div>
</code>
</pre>
<br />
<br />
<li><b>Spread Operator:</b></li>
<br />
<pre class="highlight">
<code class="language-JS">
const languages = ['JS', 'Python', 'Java'];
const morelanguages = ['C', 'C++', 'C#']
const allLanguages = [...languages, ...morelanguages]
</code>
</pre>
<br />
<br />
<h4>Output:</h4>
<br />
<pre class="highlight">
<code class="language-JS">
["JS","Python","Java","C","C++","C#"]
</code>
</pre>
<br />
<br />
<p>and many more like, classes, modules.</p>
<br />
<br />
</div>
<div class="notes" id="div5" style="display: none">
<h2>React Render HTML</h2>
<br />
<p>
React renders HTML to the web page by using a function called
<color>ReactDOM.render().</color>
</p>
<br />
<h3>ReactDOM.render()</h3>
<br />
<p>
This function takes two arguments, HTML content which you want to
show on page and HTML element where you want to put the HTML
content(first argument). But where will it find that element? It
will find it inside "index.html" located in "public" folder. There
you will notice a <color>div</color> with id "root". That is where
all this will be rendered!
</p>
<br />
<pre class="highlight">
<code class="language-JS">
ReactDOM.render(<p>Hello</p>, document.getElementById('root'));
</code>
</pre>
<br />
<br />
<img src="https://cwh-full-next-space.fra1.digitaloceanspaces.com/tutorial/react-render-html/id-root.png"
alt="" /><br />
<br />
</div>
<div class="notes" id="div6" style="display: none">
<h2>React JSX</h2>
<br />
<h3>What is JSX?</h3>
<br />
<p>
JSX stands for JavaScript XML. It is similar in appearance to
HTML, hence provides a way to easily write HTML in react.
</p>
<br />
<h3>Coding in JSX</h3>
<br />
<p>
Earlier we had to make an HTML element or append it into existing
ones with methods like
<color>createElement() / appendChild().</color>
</p>
<br />
<pre class="highlight">
<code class="language-JS">
const elem = React.createElement('h1', {}, 'Hello World!');
</code>
</pre>
<br />
<br />
<p>Now we can just do it directly, like this:</p>
<br />
<pre class="highlight">
<code class="language-JS">
const elem = <h1>Hello World!</h1>
</code>
</pre>
<br />
<br />
<h3>Expressions in JSX</h3>
<br />
<p>
You can write the expression in {}
<br />
You can write simple mathematical operations to variable to states
to complicated operations with ternary operators and it will
return the result, like:
</p>
<br />
<h4>Mathematical Operations:</h4>
<br />
<pre class="highlight">
<code class="language-JS">
const elem = <h1>React was released in {2010+3}</h1>
</code>
</pre>
<br />
<br />
<h4>Variables/states:</h4>
<br />
<pre class="highlight">
<code class="language-JS">
const name = "CWH"
const elem = <h1>My name is {name}</h1>
</code>
</pre>
<br />
<br />
<h4>Ternary Operators:</h4>
<br />
<pre class="highlight">
<code class="language-JS">
const elem = <h1>Hello {name ? name : 'World'}</h1>
</code>
</pre>
<br />
<br />
</div>
<div class="notes" id="div7" style="display: none">
<h2>React Components</h2>
<br />
<p>There are two types of components:</p>
<br />
<li>Class Based Components</li>
<li>Function Based Components</li>
<br />
<h3>Class Based Components</h3>
<br />
<p>
Before making class based component we need to inherit functions
from <color>React.Component</color> and this can be done with
<color>extends,</color> like this:
</p>
<br />
<pre class="highlight">
<code class="language-JS">
class Cat extends React.Component {
render() {
return <h1>Meow</h1>;
}
}
</code>
</pre>
<br />
<br />
<p>
it also requires a <color>render</color> method which returns
HTML.
</p>
<br />
<h3>Function Based Components</h3>
<br />
<p>
In function it's simpler, we just need to return the HTML, like
this:
</p>
<br />
<pre class="highlight">
<code class="language-JS">
function Cat() {
return <h1>Meow</h1>;
}
</code>
</pre>
<br />
<br />
<p>
<b>Note:</b>Component's name must start with uppercase letter.
</p>
<br />
<h3>Rendering a Component</h3>
<br />
<p>
We made a component, now we want to render/use it. Syntax for
using a component is:
</p>
<br />
<pre class="highlight">
<code class="language-JS">
<ComponentName />
</code>
</pre>
<br />
<br />
<h3>Components in Files</h3>
<br />
<p>
To have less mess inside main file(with all the components in the
same file) and to resuse components on different pages, we have to
make them separately. So that we can just import them in any file
and use them!
<br />
For that we will just make a new file called
<color>Cat.js</color>, make class or function based component
there and <color>export default</color> that class/function! Like
this:
</p>
<br />
<pre class="highlight">
<code class="language-JS">
function Cat() {
return <h1>Meow</h1>;
}
export default Cat;
</code>
</pre>
<br />
<br />
<p><b>Note:</b> File name must start with uppercase letter.</p>
<br />
<h3>Props</h3>
<br />
<p>
As mentioned earlier, we can import the same component in
different files and use it, but maybe in different files some
changes in the component is needed. For that, we can use props!
Like this:
</p>
<br />
<h4>Component:</h4>
<br />
<pre class="highlight">
<code class="language-JS">
function Cat(props) {
return <h1>Meow's color is {props.color}</h1>;
}
</code>
</pre>
<br />
<br />
<h4>Main file:</h4>
<br />
<pre class="highlight">
<code class="language-JS">
<Cat color="purple" />
</code>
</pre>
<br />
<br />
</div>
<div class="notes" id="div8" style="display: none">
<h2>React Class</h2>
<br />
<h3>Class Based Components</h3>
<br />
<p>
Before making class based component we need to inherit functions
from React.Component and this can be done with extends, like this:
</p>
<br />
<pre class="highlight">
<code class="language-JS">
class Cat extends React.Component {
render() {
return <h1> Meow</h1> ;
}
}
</code>
</pre>
<br />
<br />
<p>
it also requires a render method which returns HTML.
<br />
Note: Component's name must start with uppercase letter.
</p>
<br />
<h3>Component Constructor</h3>
<br />
<p>
Constructor gets called when the component is initiated. This is
where you initiate the component's properties. In React we have
states which update on page without reload. Constructor properties
are kept in state.
<br />
We also need to add super() statement, which executes the parent
component's constructor and component gets access to all the
functions of the parent component, like this:
</p>
<br />
<pre class="highlight">
<code class="language-JS">
class Cat extends React.Component {
constructor() {
super();
this.state = { color: "orange" };
}
render() {
return <h1>Meow's color is {this.state.color}</h1>;
}
}
</code>
</pre>
<br />
<br />
</div>
<div class="notes" id="div9" style="display: none">
<h2>React Props</h2>
<br />
<p>
Props are arguments passed to React components via HTML
attributes. Example:
</p>
<br />
<h4>Component:</h4>
<br />
<pre class="highlight">
<code class="language-JS">
function Cat(props) {
return <h1>Meow's color is {props.color}</h1>;
}
</code>
</pre>
<br />
<br />
<h4>Main file:</h4>
<br />
<pre class="highlight">
<code class="language-JS">
<Cat color="purple" />
</code>
</pre>
<br />
<br />
<h4>Output:</h4>
<br />
<img src="https://cwh-full-next-space.fra1.digitaloceanspaces.com/tutorial/react-props/react-props.png"
alt="" /><br />
<br />
</div>
<div class="notes" id="div10" style="display: none">
<h2>React Events</h2>
<br />
<p>
If you have coded even a little bit in javascript, you know the
importance of events.
</p>
<br />
<h3>Events</h3>
<br />
<p>
Every HTML attribute in React is written in camelCase syntax.
Event is also an attribute. Hence, written in camelCase.
<br />
As we learnt variables, states, javascript operations are written
in curly braces {}, same is true with React event handlers too!
Like this: <color>onClick={show}</color>
</p>
<br />
<pre class="highlight">
<code class="language-JS">
<button onClick={show}>Show</button>
</code>
</pre>
<br />
<br />
<h3>Arguments in events</h3>
<br />
<p>
We can't pass arguments just like that, it will give syntax error.
First, we need to put the whole function in arrow function, like
this:
</p>
<br />
<pre class="highlight">
<code class="language-JS">
<button onClick={ ()=>show('true')}>Show</button>
</code>
</pre>
<br />
<br />
<h3>React Event Object</h3>
<br />
<p>Event handler can be provided to the function like this:</p>
<br />
<pre class="highlight">
<code class="language-JS">
<button onClick={ (event)=>show('true', event) }>Show</button>
</code>
</pre>
<br />
<br />
</div>
<div class="notes" id="div11" style="display: none">
<h2>React Conditional</h2>
<br />
<p>
One of the best thing in React is in React we can conditionally
render elements/components!
</p>
<br />
<h3>&& Operator</h3>
<br />
<p>This is one of the way to conditionally render:</p>
<br />
<pre class="highlight">
<code class="language-JS">
function App() {
const loading = true;