All language subtitles for English (auto-generated)_en_0_React Tutorial Full Course - Beginner to Pro (React 19_ 2025)(1080P_60FPS)
Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (SoranĂ®)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,480 --> 00:00:04,880
Welcome to the React full course. In
2
00:00:03,120 --> 00:00:07,839
this course, we'll learn how to build
3
00:00:04,880 --> 00:00:09,760
complex websites with React. And by the
4
00:00:07,839 --> 00:00:12,800
end of this course, we'll build two
5
00:00:09,760 --> 00:00:14,480
projects, a chatbot and an e-commerce
6
00:00:12,800 --> 00:00:16,560
project.
7
00:00:14,480 --> 00:00:19,119
We'll start from the very basics of
8
00:00:16,560 --> 00:00:21,520
React like how to set up React with
9
00:00:19,119 --> 00:00:24,800
JavaScript and we'll learn each feature
10
00:00:21,520 --> 00:00:27,519
in detail step by step. We'll learn the
11
00:00:24,800 --> 00:00:30,160
major features of React as well as
12
00:00:27,519 --> 00:00:33,120
advanced features like how to use React
13
00:00:30,160 --> 00:00:35,440
with a backend, deploy React to the
14
00:00:33,120 --> 00:00:38,879
internet with AWS,
15
00:00:35,440 --> 00:00:40,879
React with TypeScript, and React with
16
00:00:38,879 --> 00:00:43,200
AI.
17
00:00:40,879 --> 00:00:47,680
You can find the sections of this course
18
00:00:43,200 --> 00:00:49,680
below the video here and here.
19
00:00:47,680 --> 00:00:53,160
You can also change the speed of the
20
00:00:49,680 --> 00:00:53,160
video here.
21
00:00:53,440 --> 00:00:59,320
And you can find subtitles for different
22
00:00:55,520 --> 00:00:59,320
languages here.
23
00:00:59,440 --> 00:01:03,600
And after each lesson, I'll give you a
24
00:01:01,520 --> 00:01:06,000
bunch of exercises that you can do on
25
00:01:03,600 --> 00:01:10,000
your own to practice the skills that we
26
00:01:06,000 --> 00:01:12,799
learned. In total, this course has over
27
00:01:10,000 --> 00:01:14,640
125 exercises.
28
00:01:12,799 --> 00:01:17,640
So, with that being said, let's get
29
00:01:14,640 --> 00:01:17,640
started.
30
00:01:25,040 --> 00:01:28,960
I'm going to be doing this course on
31
00:01:26,720 --> 00:01:31,759
Windows, but the steps are the same if
32
00:01:28,960 --> 00:01:33,520
you're using a Mac. Before we begin,
33
00:01:31,759 --> 00:01:36,880
we're going to need two pieces of
34
00:01:33,520 --> 00:01:40,079
software. Number one is a web browser,
35
00:01:36,880 --> 00:01:41,759
and number two is a code editor. The
36
00:01:40,079 --> 00:01:44,159
most popular web browser for web
37
00:01:41,759 --> 00:01:45,920
development is Google Chrome and the
38
00:01:44,159 --> 00:01:49,040
most popular code editor for web
39
00:01:45,920 --> 00:01:51,360
development is VS Code. So I recommend
40
00:01:49,040 --> 00:01:53,280
using these. If you need to install
41
00:01:51,360 --> 00:01:55,360
these, you can search for them on
42
00:01:53,280 --> 00:01:57,920
Google.
43
00:01:55,360 --> 00:02:02,680
Click the link and follow the
44
00:01:57,920 --> 00:02:02,680
instructions to download and install.
45
00:02:02,880 --> 00:02:07,119
Now you'll also need to know the basics
46
00:02:05,280 --> 00:02:09,840
of JavaScript.
47
00:02:07,119 --> 00:02:11,680
React is built on JavaScript and we're
48
00:02:09,840 --> 00:02:14,640
going to be writing a lot of JavaScript
49
00:02:11,680 --> 00:02:16,800
when using React. Don't worry, you don't
50
00:02:14,640 --> 00:02:18,480
need to be an expert in JavaScript. You
51
00:02:16,800 --> 00:02:21,360
just need to know some basic features
52
00:02:18,480 --> 00:02:23,920
like the ones listed here. And I'll also
53
00:02:21,360 --> 00:02:26,560
try to review JavaScript features as we
54
00:02:23,920 --> 00:02:28,879
go through this course. If you want to
55
00:02:26,560 --> 00:02:31,200
learn JavaScript in more detail, you can
56
00:02:28,879 --> 00:02:34,560
check out my JavaScript full course in
57
00:02:31,200 --> 00:02:36,959
the video description.
58
00:02:34,560 --> 00:02:38,879
Finally, you'll need to know the basics
59
00:02:36,959 --> 00:02:40,879
of HTML.
60
00:02:38,879 --> 00:02:43,519
When using React, we're going to be
61
00:02:40,879 --> 00:02:45,599
writing a lot of HTML.
62
00:02:43,519 --> 00:02:48,319
Again, don't worry. You don't need to be
63
00:02:45,599 --> 00:02:50,879
an expert in HTML. You just need to know
64
00:02:48,319 --> 00:02:53,519
some basic features like the ones listed
65
00:02:50,879 --> 00:02:56,000
here. And I'll try my best to review
66
00:02:53,519 --> 00:02:57,360
HTML features as we go through this
67
00:02:56,000 --> 00:02:59,440
course.
68
00:02:57,360 --> 00:03:02,319
If you want to learn HTML in more
69
00:02:59,440 --> 00:03:04,720
detail, you can check out my HTML and
70
00:03:02,319 --> 00:03:06,560
CSS full course in the video
71
00:03:04,720 --> 00:03:08,800
description.
72
00:03:06,560 --> 00:03:10,640
If you have any issues in this course,
73
00:03:08,800 --> 00:03:13,280
you can check the troubleshooting guide
74
00:03:10,640 --> 00:03:15,040
in the description.
75
00:03:13,280 --> 00:03:17,280
Now that we have all the requirements
76
00:03:15,040 --> 00:03:20,280
that we need, we're ready to learn
77
00:03:17,280 --> 00:03:20,280
React.
78
00:03:21,040 --> 00:03:25,200
On our computer, we're going to go to
79
00:03:22,879 --> 00:03:28,000
our desktop and we're going to create a
80
00:03:25,200 --> 00:03:33,360
new folder. So here I'm going to
81
00:03:28,000 --> 00:03:35,360
rightclick and create a new folder.
82
00:03:33,360 --> 00:03:38,000
And this folder is going to contain all
83
00:03:35,360 --> 00:03:42,799
the code that we write in this course.
84
00:03:38,000 --> 00:03:45,440
So let's name it react dashc course and
85
00:03:42,799 --> 00:03:47,599
press enter.
86
00:03:45,440 --> 00:03:51,280
Next we're going to open this folder in
87
00:03:47,599 --> 00:03:53,680
our code editor VS Code. So, we'll open
88
00:03:51,280 --> 00:03:56,239
VS Code
89
00:03:53,680 --> 00:03:58,560
and then click File,
90
00:03:56,239 --> 00:04:01,120
Open Folder
91
00:03:58,560 --> 00:04:04,560
and select the folder we just created.
92
00:04:01,120 --> 00:04:06,799
So, I created my folder on the desktop
93
00:04:04,560 --> 00:04:09,360
and it's this folder right here. So,
94
00:04:06,799 --> 00:04:12,159
we'll select this folder and then click
95
00:04:09,360 --> 00:04:15,280
select folder
96
00:04:12,159 --> 00:04:18,239
and we can close this welcome page.
97
00:04:15,280 --> 00:04:20,720
So, now let's create a new file to learn
98
00:04:18,239 --> 00:04:24,080
React. So up here, we're going to create
99
00:04:20,720 --> 00:04:27,080
a new file and we're going to name it
100
00:04:24,080 --> 00:04:27,080
react-basics.html
101
00:04:28,320 --> 00:04:35,840
and press enter.
102
00:04:32,000 --> 00:04:39,280
As a review, a file that ends inh
103
00:04:35,840 --> 00:04:42,080
contains HTML code and this creates a
104
00:04:39,280 --> 00:04:44,320
website.
105
00:04:42,080 --> 00:04:46,720
Next, we're going to create a website
106
00:04:44,320 --> 00:04:49,680
and we're going to set up React on this
107
00:04:46,720 --> 00:04:51,600
website. Now, to save time, we're
108
00:04:49,680 --> 00:04:54,240
actually going to download some starting
109
00:04:51,600 --> 00:04:56,160
code.
110
00:04:54,240 --> 00:04:58,639
So, we're going to open our web browser,
111
00:04:56,160 --> 00:05:01,759
Google Chrome.
112
00:04:58,639 --> 00:05:05,000
And at the top, we're going to type
113
00:05:01,759 --> 00:05:07,520
super simple.dev/react-basics
114
00:05:05,000 --> 00:05:10,400
[Music]
115
00:05:07,520 --> 00:05:13,120
and press enter.
116
00:05:10,400 --> 00:05:16,400
So, this page has some starting HTML
117
00:05:13,120 --> 00:05:18,479
code we can use to learn React. So now
118
00:05:16,400 --> 00:05:20,639
let's click on this page and we're going
119
00:05:18,479 --> 00:05:24,880
to select all this code by pressing
120
00:05:20,639 --> 00:05:27,039
control A on Windows or command A on Mac
121
00:05:24,880 --> 00:05:30,960
and we're going to copy paste this code
122
00:05:27,039 --> 00:05:34,160
into our file. So we'll right click
123
00:05:30,960 --> 00:05:39,039
copy. We can also press control C on
124
00:05:34,160 --> 00:05:42,000
Windows or command C on Mac
125
00:05:39,039 --> 00:05:44,479
and then open our code editor and then
126
00:05:42,000 --> 00:05:48,080
inside this HTML file we're going to
127
00:05:44,479 --> 00:05:51,120
rightclick and paste. We can also press
128
00:05:48,080 --> 00:05:54,120
control +VV on Windows or commandV on
129
00:05:51,120 --> 00:05:54,120
Mac.
130
00:05:54,960 --> 00:06:00,400
And finally let's save this file by
131
00:05:57,520 --> 00:06:03,520
pressing control S on Windows or command
132
00:06:00,400 --> 00:06:03,520
S on Mac.
133
00:06:03,680 --> 00:06:08,400
Next, we're going to open this file in
134
00:06:05,919 --> 00:06:11,199
our web browser to see what the website
135
00:06:08,400 --> 00:06:13,919
looks like. To make this easier, we're
136
00:06:11,199 --> 00:06:16,720
going to use a VS Code extension called
137
00:06:13,919 --> 00:06:19,280
Live Server. So, the benefit of live
138
00:06:16,720 --> 00:06:21,440
server is it will automatically refresh
139
00:06:19,280 --> 00:06:23,280
the web page whenever we change some
140
00:06:21,440 --> 00:06:24,880
code. So, it's going to make our coding
141
00:06:23,280 --> 00:06:27,039
easier.
142
00:06:24,880 --> 00:06:30,000
If you don't have live server installed,
143
00:06:27,039 --> 00:06:35,039
you can click the extension section and
144
00:06:30,000 --> 00:06:38,919
then search for live server at the top
145
00:06:35,039 --> 00:06:38,919
and then click install.
146
00:06:39,759 --> 00:06:44,479
Once liveer is installed, we're going to
147
00:06:42,240 --> 00:06:46,880
go back to our code and we're going to
148
00:06:44,479 --> 00:06:50,160
open this file in live server by
149
00:06:46,880 --> 00:06:54,039
rightclicking in the file and click open
150
00:06:50,160 --> 00:06:54,039
with live server.
151
00:06:55,280 --> 00:07:00,400
So live server will open the HTML file
152
00:06:58,319 --> 00:07:03,120
using our browser and it's going to
153
00:07:00,400 --> 00:07:05,520
create a website and we should see the
154
00:07:03,120 --> 00:07:09,199
message welcome to super simple dev
155
00:07:05,520 --> 00:07:12,720
react course on this website.
156
00:07:09,199 --> 00:07:14,880
So this message is displayed by React
157
00:07:12,720 --> 00:07:18,000
and this means that we successfully set
158
00:07:14,880 --> 00:07:22,240
up React on this website.
159
00:07:18,000 --> 00:07:24,880
Now, let's go back to VS Code.
160
00:07:22,240 --> 00:07:28,080
And this is the minimum amount of code
161
00:07:24,880 --> 00:07:29,840
that we need to set up React. We're
162
00:07:28,080 --> 00:07:32,160
going to go through this code step by
163
00:07:29,840 --> 00:07:35,360
step to make sure that you understand
164
00:07:32,160 --> 00:07:39,599
everything here.
165
00:07:35,360 --> 00:07:41,840
First of all, what is React? React is an
166
00:07:39,599 --> 00:07:44,319
external library that helps us create
167
00:07:41,840 --> 00:07:46,960
websites easier. Now, there are two
168
00:07:44,319 --> 00:07:50,160
parts to this statement. Number one,
169
00:07:46,960 --> 00:07:52,720
React is an external library. And number
170
00:07:50,160 --> 00:07:55,280
two, React helps us create websites
171
00:07:52,720 --> 00:07:58,800
easier. Let's go through these one by
172
00:07:55,280 --> 00:08:02,080
one. So number one, React is an external
173
00:07:58,800 --> 00:08:05,759
library. First, let's review what an
174
00:08:02,080 --> 00:08:09,599
external library is. So here we have a
175
00:08:05,759 --> 00:08:12,400
bunch of HTML code. An HTML code creates
176
00:08:09,599 --> 00:08:15,280
a website. And now, how do we load
177
00:08:12,400 --> 00:08:17,280
JavaScript onto our website? There are
178
00:08:15,280 --> 00:08:20,479
several ways to load JavaScript on a
179
00:08:17,280 --> 00:08:23,680
website. One way is to use an HTML
180
00:08:20,479 --> 00:08:26,639
element called a script element.
181
00:08:23,680 --> 00:08:29,440
For example, under this div element,
182
00:08:26,639 --> 00:08:33,039
let's create a script element using less
183
00:08:29,440 --> 00:08:35,519
than script greater than and less
184
00:08:33,039 --> 00:08:38,560
than/cript greater than. But our code
185
00:08:35,519 --> 00:08:41,039
editor might autocomplete this for us.
186
00:08:38,560 --> 00:08:44,399
Now, inside this script element, we can
187
00:08:41,039 --> 00:08:46,399
write some JavaScript code. For example,
188
00:08:44,399 --> 00:08:49,680
console.log
189
00:08:46,399 --> 00:08:52,720
the string hello.
190
00:08:49,680 --> 00:08:58,080
As a review, this code displays the text
191
00:08:52,720 --> 00:08:59,920
hello in the console. So now let's save
192
00:08:58,080 --> 00:09:04,080
and we're going to open the console in
193
00:08:59,920 --> 00:09:06,240
our browser. So we'll open Google Chrome
194
00:09:04,080 --> 00:09:08,560
and Liveer is going to automatically
195
00:09:06,240 --> 00:09:10,399
refresh the page and run that JavaScript
196
00:09:08,560 --> 00:09:13,839
code. And now we're going to check the
197
00:09:10,399 --> 00:09:15,760
console by right-clicking and then click
198
00:09:13,839 --> 00:09:18,880
inspect
199
00:09:15,760 --> 00:09:20,959
and open the console tab.
200
00:09:18,880 --> 00:09:24,080
And you'll see that inside the console
201
00:09:20,959 --> 00:09:26,720
we displayed the text hello. And that's
202
00:09:24,080 --> 00:09:29,120
exactly what this code does. So we
203
00:09:26,720 --> 00:09:31,519
successfully loaded this code onto our
204
00:09:29,120 --> 00:09:35,440
website.
205
00:09:31,519 --> 00:09:38,480
We'll go back to VS Code. So this is one
206
00:09:35,440 --> 00:09:40,800
way to run JavaScript code on a website.
207
00:09:38,480 --> 00:09:44,480
First, we create a script element and
208
00:09:40,800 --> 00:09:46,720
then we put our JavaScript code inside.
209
00:09:44,480 --> 00:09:49,920
Another way to run JavaScript is to save
210
00:09:46,720 --> 00:09:53,120
our JavaScript code in a file. So on the
211
00:09:49,920 --> 00:09:57,399
left, let's create a new file
212
00:09:53,120 --> 00:09:57,399
and we'll name it React-basics.js
213
00:09:57,680 --> 00:10:03,120
and press enter.
214
00:10:00,080 --> 00:10:05,920
So when we end a file with js, this file
215
00:10:03,120 --> 00:10:08,800
contains JavaScript code. So now let's
216
00:10:05,920 --> 00:10:11,519
move that code into this file. So we'll
217
00:10:08,800 --> 00:10:15,200
go to our HTML file. We're going to
218
00:10:11,519 --> 00:10:18,399
select this code and then rightclick
219
00:10:15,200 --> 00:10:23,480
cut. We can also press controlX on
220
00:10:18,399 --> 00:10:23,480
Windows or command X on Mac.
221
00:10:23,680 --> 00:10:30,240
And inside our JavaScript file, we're
222
00:10:26,160 --> 00:10:33,360
going to rightclick and paste
223
00:10:30,240 --> 00:10:36,160
and save this file.
224
00:10:33,360 --> 00:10:40,000
Now to run the code inside this file,
225
00:10:36,160 --> 00:10:42,640
we're going to go back to our HTML
226
00:10:40,000 --> 00:10:45,839
and we're going to add an HTML attribute
227
00:10:42,640 --> 00:10:48,880
to this script element. So after this
228
00:10:45,839 --> 00:10:53,680
script, we're going to type space and
229
00:10:48,880 --> 00:10:56,240
then type src equals double quotes. So
230
00:10:53,680 --> 00:10:58,959
the src attribute lets us load
231
00:10:56,240 --> 00:11:00,880
JavaScript from a file.
232
00:10:58,959 --> 00:11:02,959
Inside these double quotes, we're going
233
00:11:00,880 --> 00:11:05,600
to tell the computer which JavaScript
234
00:11:02,959 --> 00:11:08,600
file to load. So we'll type
235
00:11:05,600 --> 00:11:08,600
react-basics.js.
236
00:11:10,320 --> 00:11:14,560
So this will look for a file called
237
00:11:12,320 --> 00:11:18,000
react-basics.js
238
00:11:14,560 --> 00:11:20,000
beside the current HTML file. So it's
239
00:11:18,000 --> 00:11:22,320
going to find the file here, and it's
240
00:11:20,000 --> 00:11:25,839
going to run all the code inside this
241
00:11:22,320 --> 00:11:27,839
file. So now let's save
242
00:11:25,839 --> 00:11:29,920
and open our browser and check the
243
00:11:27,839 --> 00:11:32,079
result.
244
00:11:29,920 --> 00:11:34,880
Live server will automatically refresh
245
00:11:32,079 --> 00:11:37,839
the page. And inside the console you can
246
00:11:34,880 --> 00:11:40,240
see that we display the message hello.
247
00:11:37,839 --> 00:11:43,600
So we're successfully loading code from
248
00:11:40,240 --> 00:11:46,399
inside a file.
249
00:11:43,600 --> 00:11:48,240
We'll go back to VS Code.
250
00:11:46,399 --> 00:11:50,880
And this is a second way to load
251
00:11:48,240 --> 00:11:55,360
JavaScript. We save our JavaScript in a
252
00:11:50,880 --> 00:11:57,839
file and then we load it from the file.
253
00:11:55,360 --> 00:12:01,040
Next, the script element can actually
254
00:11:57,839 --> 00:12:04,079
load code from anywhere on the internet,
255
00:12:01,040 --> 00:12:06,720
not just on our computer. For example,
256
00:12:04,079 --> 00:12:09,279
we're going to open Google Chrome. And
257
00:12:06,720 --> 00:12:12,079
then we're going to create a new tab.
258
00:12:09,279 --> 00:12:15,079
And at the top we're going to type
259
00:12:12,079 --> 00:12:15,079
unpkg.com/sups
260
00:12:17,920 --> 00:12:20,920
simpledev/external
261
00:12:22,160 --> 00:12:29,639
dash library.js
262
00:12:26,000 --> 00:12:29,639
and press enter.
263
00:12:30,480 --> 00:12:35,360
So on this website we have some
264
00:12:32,480 --> 00:12:38,240
JavaScript code. So this is just normal
265
00:12:35,360 --> 00:12:40,880
JavaScript code. it displays a message.
266
00:12:38,240 --> 00:12:44,240
This is an external library inside the
267
00:12:40,880 --> 00:12:46,320
console. So using the script element, we
268
00:12:44,240 --> 00:12:50,079
can actually take this code from this
269
00:12:46,320 --> 00:12:54,480
website and run it on our own website.
270
00:12:50,079 --> 00:12:58,240
To do that, we're going to open VS Code
271
00:12:54,480 --> 00:13:00,720
and inside the src attribute, instead of
272
00:12:58,240 --> 00:13:02,480
loading the JavaScript code from a file,
273
00:13:00,720 --> 00:13:05,360
we're going to load the JavaScript code
274
00:13:02,480 --> 00:13:08,160
from a website. So, first we need the
275
00:13:05,360 --> 00:13:11,680
URL of the website. So, we'll go back to
276
00:13:08,160 --> 00:13:15,839
Google Chrome and at the top this is a
277
00:13:11,680 --> 00:13:19,120
URL and we're going to copy
278
00:13:15,839 --> 00:13:24,399
and VS Code we're going to remove this
279
00:13:19,120 --> 00:13:27,519
file and we're going to paste the URL.
280
00:13:24,399 --> 00:13:30,560
And now let's save.
281
00:13:27,519 --> 00:13:32,160
So now let's go back to our website and
282
00:13:30,560 --> 00:13:33,839
we're going to go to the tab for our
283
00:13:32,160 --> 00:13:36,079
website.
284
00:13:33,839 --> 00:13:38,320
And we're going to check the console.
285
00:13:36,079 --> 00:13:40,160
And inside the console, you can see we
286
00:13:38,320 --> 00:13:42,240
displayed the message, this is an
287
00:13:40,160 --> 00:13:44,240
external library.
288
00:13:42,240 --> 00:13:47,519
So we successfully loaded JavaScript
289
00:13:44,240 --> 00:13:50,880
code from another website.
290
00:13:47,519 --> 00:13:53,440
Let's go back to VS Code.
291
00:13:50,880 --> 00:13:55,920
So using the script element, we can load
292
00:13:53,440 --> 00:13:59,360
JavaScript code from anywhere on the
293
00:13:55,920 --> 00:14:03,199
internet. So this code is called an
294
00:13:59,360 --> 00:14:05,519
external library. An external library is
295
00:14:03,199 --> 00:14:08,000
just code that is outside of our
296
00:14:05,519 --> 00:14:10,800
computer. It's code that someone else
297
00:14:08,000 --> 00:14:15,199
wrote and we can load that code into our
298
00:14:10,800 --> 00:14:19,199
website using the script element.
299
00:14:15,199 --> 00:14:21,680
Now, React is an external library. So,
300
00:14:19,199 --> 00:14:23,680
React is just a bunch of code that is
301
00:14:21,680 --> 00:14:26,480
outside our computer that someone else
302
00:14:23,680 --> 00:14:29,360
wrote and we can load this code on our
303
00:14:26,480 --> 00:14:32,560
website and use it.
304
00:14:29,360 --> 00:14:35,680
So inside our starting file at the
305
00:14:32,560 --> 00:14:38,880
bottom here, these two script elements
306
00:14:35,680 --> 00:14:40,880
load the React external library. So
307
00:14:38,880 --> 00:14:44,320
loads React.js
308
00:14:40,880 --> 00:14:46,720
and React-dom.js.
309
00:14:44,320 --> 00:14:49,040
So let's open these URLs in our browser
310
00:14:46,720 --> 00:14:54,240
and take a look. So we're going to
311
00:14:49,040 --> 00:14:56,560
select this URL and we're going to copy.
312
00:14:54,240 --> 00:14:59,360
And inside Google Chrome, we're going to
313
00:14:56,560 --> 00:15:04,560
create a new tab. And at the top, we're
314
00:14:59,360 --> 00:15:06,959
going to paste the URL and press enter.
315
00:15:04,560 --> 00:15:09,920
And on this website, you can see that we
316
00:15:06,959 --> 00:15:13,040
have a bunch of JavaScript code. So,
317
00:15:09,920 --> 00:15:15,519
this is the code for React. We're just
318
00:15:13,040 --> 00:15:18,320
taking all of this code and then loading
319
00:15:15,519 --> 00:15:21,680
it onto our website.
320
00:15:18,320 --> 00:15:25,120
Let's go back to VS Code. And the same
321
00:15:21,680 --> 00:15:29,519
thing applies to this second URL. If we
322
00:15:25,120 --> 00:15:33,440
select it and then copy
323
00:15:29,519 --> 00:15:36,560
and in our browser create a new tab and
324
00:15:33,440 --> 00:15:39,279
then at the top paste
325
00:15:36,560 --> 00:15:41,680
and press enter.
326
00:15:39,279 --> 00:15:44,720
You can see that this website also has a
327
00:15:41,680 --> 00:15:48,560
bunch of JavaScript code
328
00:15:44,720 --> 00:15:51,680
and this is also the code for React. So,
329
00:15:48,560 --> 00:15:54,160
as you can see, React is an external
330
00:15:51,680 --> 00:15:56,560
library. It's just a bunch of code that
331
00:15:54,160 --> 00:15:59,440
is outside of our computer that someone
332
00:15:56,560 --> 00:16:03,839
else wrote. And we can load this code on
333
00:15:59,440 --> 00:16:05,759
our website and use it.
334
00:16:03,839 --> 00:16:08,079
Now, before we move on, there's two
335
00:16:05,759 --> 00:16:10,639
things you might be wondering about.
336
00:16:08,079 --> 00:16:13,360
Number one, why are we loading React
337
00:16:10,639 --> 00:16:17,120
from Super Simple Dev instead of some
338
00:16:13,360 --> 00:16:20,000
official React URL? So, this just lets
339
00:16:17,120 --> 00:16:22,880
me control the version of React. If we
340
00:16:20,000 --> 00:16:24,800
loaded React from the official URL, you
341
00:16:22,880 --> 00:16:26,959
might get a different version of React
342
00:16:24,800 --> 00:16:29,759
than the one I'm using in this video,
343
00:16:26,959 --> 00:16:32,000
which might cause issues. So, for the
344
00:16:29,759 --> 00:16:34,480
video, we're going to use a super simple
345
00:16:32,000 --> 00:16:37,120
dev URL to make sure that we have the
346
00:16:34,480 --> 00:16:39,519
same version of React and you're able to
347
00:16:37,120 --> 00:16:41,199
follow along.
348
00:16:39,519 --> 00:16:44,079
The second thing you might be wondering
349
00:16:41,199 --> 00:16:49,120
is why are there two external libraries
350
00:16:44,079 --> 00:16:50,880
for React React and React DOM. So React
351
00:16:49,120 --> 00:16:53,839
is designed to be used in different
352
00:16:50,880 --> 00:16:56,880
places like websites and also mobile
353
00:16:53,839 --> 00:16:59,279
apps. So the creators of React decided
354
00:16:56,880 --> 00:17:02,560
to divide it into two external
355
00:16:59,279 --> 00:17:04,640
libraries. React which contains all the
356
00:17:02,560 --> 00:17:08,480
shared features that are used in both
357
00:17:04,640 --> 00:17:10,559
websites and mobile apps and React DOM
358
00:17:08,480 --> 00:17:13,839
which contain features that are specific
359
00:17:10,559 --> 00:17:16,400
to websites. So if we're using React to
360
00:17:13,839 --> 00:17:20,160
create websites, we need to load the
361
00:17:16,400 --> 00:17:23,280
React external library and the React DOM
362
00:17:20,160 --> 00:17:25,919
external library. If we were using React
363
00:17:23,280 --> 00:17:28,640
to create mobile apps, we would load the
364
00:17:25,919 --> 00:17:32,080
React external library and a different
365
00:17:28,640 --> 00:17:34,160
library called React Native.
366
00:17:32,080 --> 00:17:36,320
In this course, we're going to use React
367
00:17:34,160 --> 00:17:41,559
to create websites. So, we're going to
368
00:17:36,320 --> 00:17:41,559
load React and React DOM.
369
00:17:41,760 --> 00:17:46,480
Okay, so we learned that React is an
370
00:17:44,080 --> 00:17:48,799
external library. It's just a bunch of
371
00:17:46,480 --> 00:17:51,840
code that is outside of our computer and
372
00:17:48,799 --> 00:17:54,720
we're going to load it onto our website.
373
00:17:51,840 --> 00:17:57,600
The next part is React helps us create
374
00:17:54,720 --> 00:18:00,240
websites easier. So now let's learn how
375
00:17:57,600 --> 00:18:04,360
to use React.
376
00:18:00,240 --> 00:18:04,360
We'll go back to VS Code
377
00:18:04,559 --> 00:18:10,160
and we're going to scroll down and these
378
00:18:07,760 --> 00:18:13,200
two lines of JavaScript at the bottom
379
00:18:10,160 --> 00:18:16,400
sets up React. So let's go through this
380
00:18:13,200 --> 00:18:19,280
step by step. In the first line, we're
381
00:18:16,400 --> 00:18:22,559
using document.query query selector to
382
00:18:19,280 --> 00:18:26,000
get an HTML element from the website and
383
00:18:22,559 --> 00:18:27,840
put it into our JavaScript. As a review,
384
00:18:26,000 --> 00:18:31,360
this is a feature of JavaScript called
385
00:18:27,840 --> 00:18:33,600
the DOM or the document object model.
386
00:18:31,360 --> 00:18:36,400
Basically, JavaScript gives us this
387
00:18:33,600 --> 00:18:39,760
document object which sort of combines
388
00:18:36,400 --> 00:18:42,880
JavaScript and HTML together. It lets us
389
00:18:39,760 --> 00:18:45,520
get HTML elements from the page and put
390
00:18:42,880 --> 00:18:48,799
them into our JavaScript and it also
391
00:18:45,520 --> 00:18:52,400
lets our JavaScript modify HTML elements
392
00:18:48,799 --> 00:18:55,360
on the page. So the DOM or the document
393
00:18:52,400 --> 00:18:57,919
object sort of combines HTML and
394
00:18:55,360 --> 00:19:00,799
JavaScript together and it lets the two
395
00:18:57,919 --> 00:19:02,880
languages interact with each other.
396
00:19:00,799 --> 00:19:05,520
Don't worry, you don't need to know a
397
00:19:02,880 --> 00:19:07,840
lot about the DOM to learn React. I'm
398
00:19:05,520 --> 00:19:10,960
just explaining what this code does so
399
00:19:07,840 --> 00:19:14,000
you understand what's going on here.
400
00:19:10,960 --> 00:19:17,440
So the code document.query selector lets
401
00:19:14,000 --> 00:19:20,240
us get an HTML element from the page and
402
00:19:17,440 --> 00:19:22,240
put it into our JavaScript.
403
00:19:20,240 --> 00:19:25,039
Inside these brackets, we tell the
404
00:19:22,240 --> 00:19:28,080
computer which HTML element we want to
405
00:19:25,039 --> 00:19:29,919
get from the page. So if we start with a
406
00:19:28,080 --> 00:19:32,720
dot, this means we're going to look for
407
00:19:29,919 --> 00:19:35,760
a class. And here we're looking for the
408
00:19:32,720 --> 00:19:39,120
class js-container.
409
00:19:35,760 --> 00:19:40,799
Up here, this div element has the class
410
00:19:39,120 --> 00:19:42,880
js-container.
411
00:19:40,799 --> 00:19:45,919
So, it's going to take this element and
412
00:19:42,880 --> 00:19:48,160
put it into our JavaScript. And finally,
413
00:19:45,919 --> 00:19:51,840
we're going to save it inside a variable
414
00:19:48,160 --> 00:19:53,840
so we can use it later.
415
00:19:51,840 --> 00:19:57,280
Next, let's go to the second line of
416
00:19:53,840 --> 00:20:01,440
code and react.create
417
00:19:57,280 --> 00:20:04,080
routt basically sets up React
418
00:20:01,440 --> 00:20:06,559
To set up React, we have to give it an
419
00:20:04,080 --> 00:20:08,720
HTML element. So, we're going to give it
420
00:20:06,559 --> 00:20:11,679
the element that we saved up here, which
421
00:20:08,720 --> 00:20:14,240
is this div element. So, basically the
422
00:20:11,679 --> 00:20:17,360
way this works is that this element is
423
00:20:14,240 --> 00:20:19,760
going to act as a container.
424
00:20:17,360 --> 00:20:21,760
Everything that we create with React is
425
00:20:19,760 --> 00:20:25,200
going to be displayed inside this
426
00:20:21,760 --> 00:20:28,080
element or inside this container. The
427
00:20:25,200 --> 00:20:30,320
reason React uses a container is because
428
00:20:28,080 --> 00:20:32,960
it keeps things organized.
429
00:20:30,320 --> 00:20:35,600
Everything inside this element will be
430
00:20:32,960 --> 00:20:38,640
controlled by React and React will not
431
00:20:35,600 --> 00:20:40,960
affect anything outside of this element.
432
00:20:38,640 --> 00:20:43,679
So it keeps things isolated and
433
00:20:40,960 --> 00:20:45,919
organized.
434
00:20:43,679 --> 00:20:48,000
So after we set up React with a
435
00:20:45,919 --> 00:20:51,880
container, we can start using the
436
00:20:48,000 --> 00:20:51,880
features of React.
437
00:20:52,480 --> 00:21:00,080
One feature of React is render. Render
438
00:20:56,640 --> 00:21:02,960
means to display something. So render
439
00:21:00,080 --> 00:21:06,720
lets us display something inside this
440
00:21:02,960 --> 00:21:09,200
container or inside this element. Now
441
00:21:06,720 --> 00:21:11,760
between the brackets of render, we're
442
00:21:09,200 --> 00:21:14,880
going to tell React what to display
443
00:21:11,760 --> 00:21:17,520
inside the container. So right now we
444
00:21:14,880 --> 00:21:21,039
told it to display the text welcome to
445
00:21:17,520 --> 00:21:24,159
super simple dev react course. And if we
446
00:21:21,039 --> 00:21:26,080
go back to our website and open the tab
447
00:21:24,159 --> 00:21:28,960
for our website
448
00:21:26,080 --> 00:21:32,080
and after we open the console, this time
449
00:21:28,960 --> 00:21:34,559
we're going to go to the elements tab.
450
00:21:32,080 --> 00:21:36,880
So this lets us see the HTML that's
451
00:21:34,559 --> 00:21:40,320
currently on the page. We're going to
452
00:21:36,880 --> 00:21:42,960
open this body element and inside we can
453
00:21:40,320 --> 00:21:46,240
see our container and you can see that
454
00:21:42,960 --> 00:21:49,200
React rendered or displayed the message
455
00:21:46,240 --> 00:21:52,320
welcome to super simple dev react course
456
00:21:49,200 --> 00:21:54,159
inside our container. So we'll go back
457
00:21:52,320 --> 00:21:57,440
to our code
458
00:21:54,159 --> 00:22:00,480
and that's how we set up and use React.
459
00:21:57,440 --> 00:22:03,679
First we give it a container and then we
460
00:22:00,480 --> 00:22:06,799
can display things like a message inside
461
00:22:03,679 --> 00:22:09,200
this container.
462
00:22:06,799 --> 00:22:11,520
Before we continue, let's review the
463
00:22:09,200 --> 00:22:13,440
rest of the code in this file to make
464
00:22:11,520 --> 00:22:18,880
sure that we understand every line of
465
00:22:13,440 --> 00:22:23,480
code. So, the HTML at the top here
466
00:22:18,880 --> 00:22:23,480
as well as the bottom here.
467
00:22:25,440 --> 00:22:32,159
This HTML is called the HTML structure.
468
00:22:30,000 --> 00:22:34,080
As a review, this is a standard
469
00:22:32,159 --> 00:22:36,400
structure that we follow when writing
470
00:22:34,080 --> 00:22:39,840
HTML.
471
00:22:36,400 --> 00:22:42,159
At the top, dock type HTML tells a
472
00:22:39,840 --> 00:22:44,080
computer to use a modern version of
473
00:22:42,159 --> 00:22:47,840
HTML.
474
00:22:44,080 --> 00:22:50,080
The HTML element represents a website.
475
00:22:47,840 --> 00:22:53,840
So all the code for our website should
476
00:22:50,080 --> 00:22:56,960
go inside the HTML element. Now every
477
00:22:53,840 --> 00:23:00,880
website is split into two sections. the
478
00:22:56,960 --> 00:23:02,799
head section and the body section. The
479
00:23:00,880 --> 00:23:05,760
head section generally contains things
480
00:23:02,799 --> 00:23:08,640
that are not visible on the page. For
481
00:23:05,760 --> 00:23:11,200
example, the title element sets the text
482
00:23:08,640 --> 00:23:13,760
at the top of the tab. So, this is not
483
00:23:11,200 --> 00:23:16,080
visible on the page. So, we put this
484
00:23:13,760 --> 00:23:18,480
inside the head.
485
00:23:16,080 --> 00:23:21,679
The body section generally contains
486
00:23:18,480 --> 00:23:24,559
things that are visible on the page. For
487
00:23:21,679 --> 00:23:27,360
example, the div element will be visible
488
00:23:24,559 --> 00:23:29,360
on the page. So we put it inside the
489
00:23:27,360 --> 00:23:31,440
body.
490
00:23:29,360 --> 00:23:33,760
Finally, we usually put our script
491
00:23:31,440 --> 00:23:36,480
elements at the bottom of the body
492
00:23:33,760 --> 00:23:39,360
because we want our HTML to be created
493
00:23:36,480 --> 00:23:41,280
first and then our JavaScript can modify
494
00:23:39,360 --> 00:23:43,679
the HTML.
495
00:23:41,280 --> 00:23:46,080
So that's a review of the HTML
496
00:23:43,679 --> 00:23:50,679
structure. This is a standard structure
497
00:23:46,080 --> 00:23:50,679
that we follow when writing HTML.
498
00:23:51,679 --> 00:23:57,919
So now we understand almost every line
499
00:23:54,640 --> 00:24:00,799
of code in this file. This is just the
500
00:23:57,919 --> 00:24:02,799
HTML structure that we have to follow.
501
00:24:00,799 --> 00:24:05,200
This is a container that we give to
502
00:24:02,799 --> 00:24:06,799
React to keep things isolated and
503
00:24:05,200 --> 00:24:08,880
organized.
504
00:24:06,799 --> 00:24:12,400
This is an example of an external
505
00:24:08,880 --> 00:24:14,960
library and this loads the React
506
00:24:12,400 --> 00:24:17,279
external library or loads the code for
507
00:24:14,960 --> 00:24:21,520
React.
508
00:24:17,279 --> 00:24:23,440
And this sets up and uses React.
509
00:24:21,520 --> 00:24:26,159
The last two lines that we haven't
510
00:24:23,440 --> 00:24:29,600
learned yet are these two lines right
511
00:24:26,159 --> 00:24:32,159
here. So here we're loading another
512
00:24:29,600 --> 00:24:34,960
external library. And this external
513
00:24:32,159 --> 00:24:37,760
library is called Babel.
514
00:24:34,960 --> 00:24:41,039
So again if I open this URL in my
515
00:24:37,760 --> 00:24:44,000
browser copy
516
00:24:41,039 --> 00:24:48,240
and in my browser going to create a new
517
00:24:44,000 --> 00:24:50,720
tab and at the top paste and press
518
00:24:48,240 --> 00:24:52,960
enter.
519
00:24:50,720 --> 00:24:55,679
You'll see that this website contains a
520
00:24:52,960 --> 00:24:58,480
bunch of JavaScript code and this is the
521
00:24:55,679 --> 00:25:00,400
code for another external library called
522
00:24:58,480 --> 00:25:03,279
Babel.
523
00:25:00,400 --> 00:25:05,760
So we'll go back to VS Code and let's
524
00:25:03,279 --> 00:25:07,360
learn what is the Babel external
525
00:25:05,760 --> 00:25:10,799
library.
526
00:25:07,360 --> 00:25:12,880
Babel is known as a JavaScript compiler.
527
00:25:10,799 --> 00:25:17,200
So it basically translates other
528
00:25:12,880 --> 00:25:20,080
languages into JavaScript.
529
00:25:17,200 --> 00:25:22,960
So why do we need Babel? When using
530
00:25:20,080 --> 00:25:26,000
React, we actually don't use normal
531
00:25:22,960 --> 00:25:29,600
JavaScript. We use an enhanced version
532
00:25:26,000 --> 00:25:33,520
of JavaScript called JSX.
533
00:25:29,600 --> 00:25:35,679
JSX stands for JavaScript XML. It's the
534
00:25:33,520 --> 00:25:38,720
same as JavaScript, but we can also
535
00:25:35,679 --> 00:25:41,520
write HTML directly in our JavaScript
536
00:25:38,720 --> 00:25:44,320
code. So, it's just JavaScript with
537
00:25:41,520 --> 00:25:48,559
extra features. So, let's do an example
538
00:25:44,320 --> 00:25:51,440
of JSX to learn how to use it. Inside
539
00:25:48,559 --> 00:25:54,320
the script element at the bottom, we're
540
00:25:51,440 --> 00:25:58,240
going to create a new variable using
541
00:25:54,320 --> 00:26:01,919
const and let's call it button
542
00:25:58,240 --> 00:26:05,200
equals. And now let's create a button
543
00:26:01,919 --> 00:26:09,279
HTML element. So here we're going to
544
00:26:05,200 --> 00:26:12,880
type HTML directly less than button
545
00:26:09,279 --> 00:26:14,720
greater than and then less than /button
546
00:26:12,880 --> 00:26:18,000
greater than.
547
00:26:14,720 --> 00:26:20,000
So this is actually HTML code and we're
548
00:26:18,000 --> 00:26:24,080
writing it directly inside our
549
00:26:20,000 --> 00:26:27,279
JavaScript. So this HTML code creates a
550
00:26:24,080 --> 00:26:30,240
button and then inside our button we can
551
00:26:27,279 --> 00:26:34,240
put the text hello.
552
00:26:30,240 --> 00:26:37,520
So this code here is actually using JSX.
553
00:26:34,240 --> 00:26:40,720
So JSX works the same way as JavaScript
554
00:26:37,520 --> 00:26:43,679
except we can also write HTML directly
555
00:26:40,720 --> 00:26:46,720
in our JavaScript code.
556
00:26:43,679 --> 00:26:48,880
So, what does this code do? It's
557
00:26:46,720 --> 00:26:52,320
actually pretty straightforward. This
558
00:26:48,880 --> 00:26:55,279
code creates a new button element inside
559
00:26:52,320 --> 00:26:57,279
our JavaScript and then we save it in a
560
00:26:55,279 --> 00:26:59,120
variable.
561
00:26:57,279 --> 00:27:02,559
We can actually do this with normal
562
00:26:59,120 --> 00:27:04,799
JavaScript as well. We can use the DOM
563
00:27:02,559 --> 00:27:08,000
and we can create a button element in
564
00:27:04,799 --> 00:27:11,600
our JavaScript like this. So you can
565
00:27:08,000 --> 00:27:14,320
think of this JSX code as a shortcut for
566
00:27:11,600 --> 00:27:18,240
this JavaScript code.
567
00:27:14,320 --> 00:27:20,880
So that's JSX. JSX is an enhanced
568
00:27:18,240 --> 00:27:23,360
version of JavaScript. So it's the same
569
00:27:20,880 --> 00:27:27,200
as JavaScript, but we can also write
570
00:27:23,360 --> 00:27:30,559
HTML directly in our JavaScript code. So
571
00:27:27,200 --> 00:27:33,200
when using React, we usually use JSX
572
00:27:30,559 --> 00:27:38,159
instead of normal JavaScript because, as
573
00:27:33,200 --> 00:27:41,520
you can see, JSX is simpler.
574
00:27:38,159 --> 00:27:44,400
Now there is one problem with JSX which
575
00:27:41,520 --> 00:27:47,520
is our web browser doesn't understand
576
00:27:44,400 --> 00:27:49,200
JSX. It only understands normal
577
00:27:47,520 --> 00:27:52,720
JavaScript.
578
00:27:49,200 --> 00:27:56,240
In order to use JSX we need to translate
579
00:27:52,720 --> 00:27:58,880
it into normal JavaScript first and then
580
00:27:56,240 --> 00:28:00,480
our web browser can understand this
581
00:27:58,880 --> 00:28:03,200
code.
582
00:28:00,480 --> 00:28:07,440
To do that we use the Babel external
583
00:28:03,200 --> 00:28:10,399
library. Babel translates JSX into
584
00:28:07,440 --> 00:28:13,679
normal JavaScript so our web browser can
585
00:28:10,399 --> 00:28:17,440
understand it. To use Babel, we need to
586
00:28:13,679 --> 00:28:19,919
do two steps. First, we load the Babel
587
00:28:17,440 --> 00:28:23,120
external library, which is what we do
588
00:28:19,919 --> 00:28:25,440
here. And then when we want to translate
589
00:28:23,120 --> 00:28:28,080
something into JavaScript, we have to
590
00:28:25,440 --> 00:28:31,520
give the script element an attribute
591
00:28:28,080 --> 00:28:34,080
type equals text/babel.
592
00:28:31,520 --> 00:28:37,279
So this attribute tells Babel to
593
00:28:34,080 --> 00:28:39,279
translate all the code inside here into
594
00:28:37,279 --> 00:28:41,520
normal JavaScript.
595
00:28:39,279 --> 00:28:44,559
So that's what these two lines of code
596
00:28:41,520 --> 00:28:47,120
do. First, we load the Babel external
597
00:28:44,559 --> 00:28:50,399
library and then we tell Babel to
598
00:28:47,120 --> 00:28:52,640
translate the code from JSX into normal
599
00:28:50,399 --> 00:28:56,559
JavaScript so our web browser can
600
00:28:52,640 --> 00:28:59,919
understand it. So now we understand all
601
00:28:56,559 --> 00:29:02,240
of the code in our starting file. And
602
00:28:59,919 --> 00:29:06,480
again, this is the minimum amount of
603
00:29:02,240 --> 00:29:08,960
code that we need to set up React.
604
00:29:06,480 --> 00:29:12,640
Next, let's learn some more features of
605
00:29:08,960 --> 00:29:15,200
React. So, we'll scroll back down. And
606
00:29:12,640 --> 00:29:19,120
currently, we're using React to display
607
00:29:15,200 --> 00:29:21,520
some text inside this container. Now, in
608
00:29:19,120 --> 00:29:25,360
addition to text, we can also display
609
00:29:21,520 --> 00:29:27,600
HTML elements inside this container. So
610
00:29:25,360 --> 00:29:29,360
to do that, we're going to remove this
611
00:29:27,600 --> 00:29:31,520
string
612
00:29:29,360 --> 00:29:34,240
and instead we're going to give render
613
00:29:31,520 --> 00:29:37,440
an HTML element.
614
00:29:34,240 --> 00:29:39,919
So up here, we actually created an HTML
615
00:29:37,440 --> 00:29:42,799
element or a button. So we're going to
616
00:29:39,919 --> 00:29:46,080
give this to the render function. So
617
00:29:42,799 --> 00:29:47,840
here we're going to give it the button.
618
00:29:46,080 --> 00:29:50,960
And now it's going to display this
619
00:29:47,840 --> 00:29:55,520
button inside our container or inside
620
00:29:50,960 --> 00:29:57,200
this div here. Let's save and we'll open
621
00:29:55,520 --> 00:30:00,399
the browser
622
00:29:57,200 --> 00:30:02,159
and open the tab for our website. And
623
00:30:00,399 --> 00:30:05,360
you'll see it displayed the button with
624
00:30:02,159 --> 00:30:08,320
the text hello on the website.
625
00:30:05,360 --> 00:30:10,720
And if we check the elements tab, you
626
00:30:08,320 --> 00:30:13,120
can see that React displayed the button
627
00:30:10,720 --> 00:30:15,600
inside our container.
628
00:30:13,120 --> 00:30:18,240
We'll go back to our code.
629
00:30:15,600 --> 00:30:20,640
So that's what this code does. It
630
00:30:18,240 --> 00:30:23,200
displays an element inside this
631
00:30:20,640 --> 00:30:25,760
container.
632
00:30:23,200 --> 00:30:28,880
Now let's do another example. Let's
633
00:30:25,760 --> 00:30:32,399
display a different HTML element inside
634
00:30:28,880 --> 00:30:35,279
this container. So below this button,
635
00:30:32,399 --> 00:30:37,760
let's use JSX to create a paragraph
636
00:30:35,279 --> 00:30:40,880
element. So first we'll create a new
637
00:30:37,760 --> 00:30:43,679
variable using const and we'll name the
638
00:30:40,880 --> 00:30:46,799
variable paragraph
639
00:30:43,679 --> 00:30:50,399
equals and we'll create a paragraph
640
00:30:46,799 --> 00:30:54,880
element using the code less than p
641
00:30:50,399 --> 00:30:58,480
greater than less than /p greater than.
642
00:30:54,880 --> 00:31:01,440
So this html code creates a paragraph or
643
00:30:58,480 --> 00:31:05,279
a paragraph of text. Inside our
644
00:31:01,440 --> 00:31:06,880
paragraph, let's just put paragraph of
645
00:31:05,279 --> 00:31:09,440
text.
646
00:31:06,880 --> 00:31:12,640
And now let's display this paragraph on
647
00:31:09,440 --> 00:31:15,520
our website. So inside render, instead
648
00:31:12,640 --> 00:31:17,120
of the button, we're going to render the
649
00:31:15,520 --> 00:31:20,000
paragraph
650
00:31:17,120 --> 00:31:22,559
and save.
651
00:31:20,000 --> 00:31:25,039
If we open our website,
652
00:31:22,559 --> 00:31:28,720
you can see it displayed a paragraph of
653
00:31:25,039 --> 00:31:31,520
text. If we go into the elements tab and
654
00:31:28,720 --> 00:31:33,760
we open this div container,
655
00:31:31,520 --> 00:31:37,440
we can see that React displayed this
656
00:31:33,760 --> 00:31:40,240
paragraph element inside this container.
657
00:31:37,440 --> 00:31:43,120
So let's go back to our code. And that's
658
00:31:40,240 --> 00:31:45,360
basically how we use React. We give
659
00:31:43,120 --> 00:31:48,640
React a container and then we can
660
00:31:45,360 --> 00:31:51,120
display an HTML element inside this
661
00:31:48,640 --> 00:31:53,600
container.
662
00:31:51,120 --> 00:31:56,080
Next, we're currently using React to
663
00:31:53,600 --> 00:31:58,960
display one element at a time on our
664
00:31:56,080 --> 00:32:00,320
website, which is not that useful. So
665
00:31:58,960 --> 00:32:02,880
now we're going to learn how to use
666
00:32:00,320 --> 00:32:06,399
React to display multiple elements on
667
00:32:02,880 --> 00:32:09,279
our website. So one thing we can't do is
668
00:32:06,399 --> 00:32:12,240
give render multiple elements. So this
669
00:32:09,279 --> 00:32:15,120
is not supported. We also can't save
670
00:32:12,240 --> 00:32:18,399
multiple elements inside a variable.
671
00:32:15,120 --> 00:32:21,039
This is also not supported. Instead, we
672
00:32:18,399 --> 00:32:24,880
need to use another HTML element called
673
00:32:21,039 --> 00:32:28,320
a div element. So, under our paragraph,
674
00:32:24,880 --> 00:32:32,000
let's create a new variable using const
675
00:32:28,320 --> 00:32:35,120
and let's name it div and make it equal.
676
00:32:32,000 --> 00:32:38,080
And we'll create an HTML div element
677
00:32:35,120 --> 00:32:42,640
using the code less than div greater
678
00:32:38,080 --> 00:32:46,080
than less than /d greater than.
679
00:32:42,640 --> 00:32:49,679
So this is the HTML code to create a div
680
00:32:46,080 --> 00:32:52,720
element. As a review, the div element
681
00:32:49,679 --> 00:32:55,840
represents a container. The purpose of a
682
00:32:52,720 --> 00:32:58,880
div element is to contain other elements
683
00:32:55,840 --> 00:33:01,039
like buttons and paragraphs. So that's
684
00:32:58,880 --> 00:33:04,240
actually why we used a div element up
685
00:33:01,039 --> 00:33:05,679
here. This element is a container. It's
686
00:33:04,240 --> 00:33:08,799
going to contain everything that we
687
00:33:05,679 --> 00:33:10,720
create using React. So let's go back
688
00:33:08,799 --> 00:33:13,360
down.
689
00:33:10,720 --> 00:33:17,279
Inside our div element, we can put other
690
00:33:13,360 --> 00:33:22,799
HTML elements like a button. So, let's
691
00:33:17,279 --> 00:33:27,200
actually select this code and we'll copy
692
00:33:22,799 --> 00:33:29,519
and then inside this div, paste.
693
00:33:27,200 --> 00:33:32,000
And let's also put a paragraph element
694
00:33:29,519 --> 00:33:35,039
inside our div. So we'll select this
695
00:33:32,000 --> 00:33:37,519
code for the paragraph element and we'll
696
00:33:35,039 --> 00:33:42,159
copy
697
00:33:37,519 --> 00:33:45,279
and our div paste.
698
00:33:42,159 --> 00:33:47,919
So this code works because a div is
699
00:33:45,279 --> 00:33:50,480
considered as one element. So we're just
700
00:33:47,919 --> 00:33:52,559
taking one element and saving it in one
701
00:33:50,480 --> 00:33:54,960
variable which is supported in
702
00:33:52,559 --> 00:33:57,200
JavaScript. The only thing special about
703
00:33:54,960 --> 00:34:00,320
this is that this one element can
704
00:33:57,200 --> 00:34:02,880
contain other elements inside.
705
00:34:00,320 --> 00:34:05,919
Next, we're going to give this div to
706
00:34:02,880 --> 00:34:07,919
the render function here. So, instead of
707
00:34:05,919 --> 00:34:12,879
the paragraph, we're going to give
708
00:34:07,919 --> 00:34:15,839
render our div and save.
709
00:34:12,879 --> 00:34:17,760
And if we check our website,
710
00:34:15,839 --> 00:34:20,240
you can see that React displayed
711
00:34:17,760 --> 00:34:23,359
multiple elements on the website, a
712
00:34:20,240 --> 00:34:25,919
button and a paragraph. If we look at
713
00:34:23,359 --> 00:34:28,000
the elements tab and open this div
714
00:34:25,919 --> 00:34:30,960
container,
715
00:34:28,000 --> 00:34:33,760
you can see that React displayed our div
716
00:34:30,960 --> 00:34:36,320
inside this container.
717
00:34:33,760 --> 00:34:38,480
Let's go back to our code. And that's
718
00:34:36,320 --> 00:34:41,119
how we display multiple elements on a
719
00:34:38,480 --> 00:34:43,919
website using React.
720
00:34:41,119 --> 00:34:46,960
First, we group the elements inside a
721
00:34:43,919 --> 00:34:49,280
div element. And then we render the div
722
00:34:46,960 --> 00:34:51,200
element.
723
00:34:49,280 --> 00:34:54,320
Before we continue, we're going to do
724
00:34:51,200 --> 00:34:57,520
some code cleanup. When we have multiple
725
00:34:54,320 --> 00:35:00,880
lines of HTML like this, a best practice
726
00:34:57,520 --> 00:35:02,560
is to add brackets around it. So, at the
727
00:35:00,880 --> 00:35:05,200
front, we're going to add an open
728
00:35:02,560 --> 00:35:06,720
bracket and at the end, a closed
729
00:35:05,200 --> 00:35:08,880
bracket.
730
00:35:06,720 --> 00:35:11,680
And we're going to put the HTML on its
731
00:35:08,880 --> 00:35:14,800
own line. So, here we have JavaScript
732
00:35:11,680 --> 00:35:17,280
and HTML together. We're going to add a
733
00:35:14,800 --> 00:35:19,119
new line here by pressing enter. and
734
00:35:17,280 --> 00:35:22,880
we're going to put the HTML on its own
735
00:35:19,119 --> 00:35:26,640
line. Same thing here. Click here and
736
00:35:22,880 --> 00:35:28,160
press enter to put the HTML on its own
737
00:35:26,640 --> 00:35:30,400
line.
738
00:35:28,160 --> 00:35:32,800
Next, we have two elements that are
739
00:35:30,400 --> 00:35:35,440
inside a div. So, we're also going to
740
00:35:32,800 --> 00:35:37,839
give this line an indent by pressing
741
00:35:35,440 --> 00:35:41,040
tab. And then, we're going to give this
742
00:35:37,839 --> 00:35:43,280
line an indent by pressing tab. So that
743
00:35:41,040 --> 00:35:46,800
it's easy to see that these two elements
744
00:35:43,280 --> 00:35:48,960
are inside this div element.
745
00:35:46,800 --> 00:35:52,400
So this is how we should format our code
746
00:35:48,960 --> 00:35:54,720
when we have multiple lines of HTML.
747
00:35:52,400 --> 00:35:56,640
Another way to think about this is
748
00:35:54,720 --> 00:35:59,920
inside here, you're actually just
749
00:35:56,640 --> 00:36:02,000
writing HTML normally.
750
00:35:59,920 --> 00:36:04,160
So don't worry, the more that you use
751
00:36:02,000 --> 00:36:05,680
React, the more you'll get used to this
752
00:36:04,160 --> 00:36:09,040
format.
753
00:36:05,680 --> 00:36:11,599
So let's save this file.
754
00:36:09,040 --> 00:36:14,320
Now that we learn the basics of React,
755
00:36:11,599 --> 00:36:17,280
let's talk about how React helps us
756
00:36:14,320 --> 00:36:19,599
create websites easier.
757
00:36:17,280 --> 00:36:23,040
If we look at this code, creating a
758
00:36:19,599 --> 00:36:25,359
website with React feels natural. It's a
759
00:36:23,040 --> 00:36:28,160
lot simpler than using the DOM feature
760
00:36:25,359 --> 00:36:31,520
in JavaScript. We can just create the
761
00:36:28,160 --> 00:36:34,240
HTML elements we want directly and then
762
00:36:31,520 --> 00:36:36,480
give it to React and React will display
763
00:36:34,240 --> 00:36:40,160
it on the website.
764
00:36:36,480 --> 00:36:42,320
Another benefit is JSX lets us find
765
00:36:40,160 --> 00:36:45,280
errors easier.
766
00:36:42,320 --> 00:36:47,680
For example, if I misspell button, it
767
00:36:45,280 --> 00:36:51,119
will actually tell me that these two
768
00:36:47,680 --> 00:36:53,839
don't match because this is an actual
769
00:36:51,119 --> 00:36:56,560
element rather than just text or a
770
00:36:53,839 --> 00:36:59,359
string. So, it makes it easier to find
771
00:36:56,560 --> 00:37:02,000
errors when we're coding. So, I'll add
772
00:36:59,359 --> 00:37:05,119
the O back.
773
00:37:02,000 --> 00:37:09,119
Another benefit is we can insert values
774
00:37:05,119 --> 00:37:11,200
inside our JSX elements. For example,
775
00:37:09,119 --> 00:37:13,760
inside this paragraph, we're going to
776
00:37:11,200 --> 00:37:16,880
type space and we're going to insert a
777
00:37:13,760 --> 00:37:20,240
value here by typing open curly bracket
778
00:37:16,880 --> 00:37:22,640
close curly bracket. And inside here, we
779
00:37:20,240 --> 00:37:27,760
can give it any JavaScript code. For
780
00:37:22,640 --> 00:37:30,560
example, 2 + 2. So this will calculate 2
781
00:37:27,760 --> 00:37:34,560
+ 2 and then insert it into this
782
00:37:30,560 --> 00:37:36,960
element. If we save
783
00:37:34,560 --> 00:37:39,200
and then check our website,
784
00:37:36,960 --> 00:37:42,320
we can see that it calculated 2 + 2
785
00:37:39,200 --> 00:37:45,119
which is four and inserted it into this
786
00:37:42,320 --> 00:37:48,240
paragraph element. So we'll go back to
787
00:37:45,119 --> 00:37:50,960
our code and that's another benefit of
788
00:37:48,240 --> 00:37:54,800
using React. We can insert JavaScript
789
00:37:50,960 --> 00:37:57,200
code directly into our JSX elements. So
790
00:37:54,800 --> 00:38:01,359
this is similar to string interpolation
791
00:37:57,200 --> 00:38:04,079
where we can insert values into a string
792
00:38:01,359 --> 00:38:07,680
except here we're inserting values into
793
00:38:04,079 --> 00:38:10,160
actual HTML elements. So those are three
794
00:38:07,680 --> 00:38:13,280
ways that React helps us create websites
795
00:38:10,160 --> 00:38:15,440
easier. Number one, it's more natural.
796
00:38:13,280 --> 00:38:17,440
We can create the elements that we want
797
00:38:15,440 --> 00:38:20,240
and then just give it to React and it
798
00:38:17,440 --> 00:38:22,560
will put it on the website. Number two,
799
00:38:20,240 --> 00:38:25,200
it helps us find errors easier because
800
00:38:22,560 --> 00:38:27,760
these are actual elements. And number
801
00:38:25,200 --> 00:38:30,720
three, we can insert our JavaScript code
802
00:38:27,760 --> 00:38:33,839
into our elements. So this helps us use
803
00:38:30,720 --> 00:38:35,680
JavaScript and HTML together more
804
00:38:33,839 --> 00:38:37,839
easily.
805
00:38:35,680 --> 00:38:40,000
Now, React has many other features that
806
00:38:37,839 --> 00:38:41,359
helps us create websites easier, but
807
00:38:40,000 --> 00:38:44,880
we're going to learn those features
808
00:38:41,359 --> 00:38:46,880
later on as we go through the course.
809
00:38:44,880 --> 00:38:49,680
The last thing we're going to do is
810
00:38:46,880 --> 00:38:51,920
we're going to scroll up and we're just
811
00:38:49,680 --> 00:38:54,160
going to remove this example external
812
00:38:51,920 --> 00:38:56,400
library
813
00:38:54,160 --> 00:38:58,160
and save
814
00:38:56,400 --> 00:39:01,880
so that we just have the code that we
815
00:38:58,160 --> 00:39:01,880
need for React.
816
00:39:02,160 --> 00:39:07,200
And that's the end of this lesson. In
817
00:39:04,480 --> 00:39:09,760
this lesson, we learned that React is an
818
00:39:07,200 --> 00:39:12,800
external library that helps us create
819
00:39:09,760 --> 00:39:15,760
websites easier. We learned how to load
820
00:39:12,800 --> 00:39:19,599
the React external library. We learned
821
00:39:15,760 --> 00:39:22,320
how to set up React and use render. We
822
00:39:19,599 --> 00:39:23,839
reviewed the basics of HTML and
823
00:39:22,320 --> 00:39:27,119
JavaScript.
824
00:39:23,839 --> 00:39:28,640
We learned JSX, an enhanced version of
825
00:39:27,119 --> 00:39:32,320
JavaScript.
826
00:39:28,640 --> 00:39:35,040
We created elements directly with JSX.
827
00:39:32,320 --> 00:39:38,480
And we learned how to insert values into
828
00:39:35,040 --> 00:39:40,640
our JSX elements. Here are some
829
00:39:38,480 --> 00:39:43,040
exercises to help you practice what we
830
00:39:40,640 --> 00:39:45,200
learned in this lesson. You can find the
831
00:39:43,040 --> 00:39:48,520
solutions to these exercises in the
832
00:39:45,200 --> 00:39:48,520
video description.
833
00:40:20,640 --> 00:40:25,839
In this lesson, we're going to learn two
834
00:40:22,880 --> 00:40:29,359
of the most important features of React,
835
00:40:25,839 --> 00:40:31,839
which are components and props. To learn
836
00:40:29,359 --> 00:40:34,400
this, I actually prepared a project that
837
00:40:31,839 --> 00:40:36,720
we can do for this lesson. We're going
838
00:40:34,400 --> 00:40:39,119
to go to our browser
839
00:40:36,720 --> 00:40:42,079
and we can delete these extra tabs for
840
00:40:39,119 --> 00:40:45,359
now.
841
00:40:42,079 --> 00:40:48,240
And we're going to create a new tab.
842
00:40:45,359 --> 00:40:52,040
And at the top, we're going to go to
843
00:40:48,240 --> 00:40:52,040
super simple.dev/projects/
844
00:40:54,160 --> 00:41:02,560
chatbot and press enter.
845
00:40:58,880 --> 00:41:05,599
This is a chatbot project. We can send a
846
00:41:02,560 --> 00:41:08,160
message to a robot up here and the robot
847
00:41:05,599 --> 00:41:11,760
will respond to our message. For
848
00:41:08,160 --> 00:41:18,000
example, we can click in here and type
849
00:41:11,760 --> 00:41:20,640
hello chatbot and then press send.
850
00:41:18,000 --> 00:41:24,160
And the robot will respond with hello,
851
00:41:20,640 --> 00:41:26,480
how can I help you? Now, if we scroll to
852
00:41:24,160 --> 00:41:28,640
the bottom of this page, we can also
853
00:41:26,480 --> 00:41:31,680
move the text box to the bottom by
854
00:41:28,640 --> 00:41:33,920
clicking this link.
855
00:41:31,680 --> 00:41:36,400
So, a lot of chatbot applications use
856
00:41:33,920 --> 00:41:39,359
this design where the text box is at the
857
00:41:36,400 --> 00:41:42,400
bottom. However, because we're watching
858
00:41:39,359 --> 00:41:44,400
a video, this text box might get cut off
859
00:41:42,400 --> 00:41:47,359
or it might be covered by the video
860
00:41:44,400 --> 00:41:50,000
progress bar. So, just for this video,
861
00:41:47,359 --> 00:41:52,000
we're going to click this again and
862
00:41:50,000 --> 00:41:53,760
we're going to start with a text box at
863
00:41:52,000 --> 00:41:57,040
the top.
864
00:41:53,760 --> 00:41:59,599
Next, this is a simple chatbot and
865
00:41:57,040 --> 00:42:02,640
currently it can only do three things.
866
00:41:59,599 --> 00:42:05,920
get today's date, flip a coin, and roll
867
00:42:02,640 --> 00:42:12,960
a dice. For example, up here, we can
868
00:42:05,920 --> 00:42:16,240
type, "Can you get me today's date?"
869
00:42:12,960 --> 00:42:19,920
and press send.
870
00:42:16,240 --> 00:42:25,280
And it will respond with today's date.
871
00:42:19,920 --> 00:42:30,240
And let's try typing, "How about flip a
872
00:42:25,280 --> 00:42:34,960
coin?" and press send.
873
00:42:30,240 --> 00:42:39,440
and it will respond with heads or tails.
874
00:42:34,960 --> 00:42:42,560
Finally, let's type okay, thank you and
875
00:42:39,440 --> 00:42:46,880
press send.
876
00:42:42,560 --> 00:42:49,040
And it responds with no problem.
877
00:42:46,880 --> 00:42:50,400
So, this is a chatbot project we're
878
00:42:49,040 --> 00:42:52,960
going to work on for the next few
879
00:42:50,400 --> 00:42:55,200
lessons. Now, this is a simple chatbot
880
00:42:52,960 --> 00:42:57,280
and it can only do these three things.
881
00:42:55,200 --> 00:42:59,839
But in the future, we can also switch
882
00:42:57,280 --> 00:43:03,680
this to a more powerful AI that can
883
00:42:59,839 --> 00:43:05,760
respond to any message we send.
884
00:43:03,680 --> 00:43:09,200
But for now, we'll just use this simple
885
00:43:05,760 --> 00:43:12,240
chatbot to learn React. Now, let's use
886
00:43:09,200 --> 00:43:15,839
React to build this chatbot project.
887
00:43:12,240 --> 00:43:19,280
We'll go back to VS Code
888
00:43:15,839 --> 00:43:21,920
and let's make a copy of React Basics.
889
00:43:19,280 --> 00:43:24,240
So, first we'll select this file and
890
00:43:21,920 --> 00:43:26,560
then rightclick,
891
00:43:24,240 --> 00:43:28,400
copy.
892
00:43:26,560 --> 00:43:32,560
And inside here, we're going to
893
00:43:28,400 --> 00:43:34,880
rightclick and paste.
894
00:43:32,560 --> 00:43:38,480
And let's rename this file. So, we'll
895
00:43:34,880 --> 00:43:42,400
rightclick and rename
896
00:43:38,480 --> 00:43:46,040
and rename it to chatbot.html
897
00:43:42,400 --> 00:43:46,040
and press enter.
898
00:43:46,240 --> 00:43:51,520
And let's close the React Basics files
899
00:43:48,560 --> 00:43:52,880
for now.
900
00:43:51,520 --> 00:43:56,079
So, we're going to start with the same
901
00:43:52,880 --> 00:43:58,079
code as the React Basics file, except
902
00:43:56,079 --> 00:44:01,760
we're just going to change the title up
903
00:43:58,079 --> 00:44:05,040
here. So, we'll remove React Basics and
904
00:44:01,760 --> 00:44:07,760
just change it to chatbot
905
00:44:05,040 --> 00:44:09,599
and save.
906
00:44:07,760 --> 00:44:12,240
And now, let's open this file in our
907
00:44:09,599 --> 00:44:16,000
browser. We'll rightclick,
908
00:44:12,240 --> 00:44:16,000
open with a live server.
909
00:44:16,400 --> 00:44:22,480
And it should say chatbot in the tab up
910
00:44:19,040 --> 00:44:27,119
here. So now we can close the React
911
00:44:22,480 --> 00:44:30,400
basics tabs which are these two here.
912
00:44:27,119 --> 00:44:32,800
And we'll go back to VS Code.
913
00:44:30,400 --> 00:44:35,800
And now we're ready to begin the chatbot
914
00:44:32,800 --> 00:44:35,800
project.
915
00:44:35,920 --> 00:44:40,960
First we're going to learn one of the
916
00:44:37,839 --> 00:44:44,319
most important features of React called
917
00:44:40,960 --> 00:44:47,440
components. A component is just a piece
918
00:44:44,319 --> 00:44:49,839
of the website.
919
00:44:47,440 --> 00:44:52,560
For example, let's go back to our
920
00:44:49,839 --> 00:44:54,480
browser and open the final chatbot
921
00:44:52,560 --> 00:44:57,040
project.
922
00:44:54,480 --> 00:45:00,400
So looking at this project, we can split
923
00:44:57,040 --> 00:45:02,640
it up into different pieces. One piece
924
00:45:00,400 --> 00:45:05,440
can be the text box and the button at
925
00:45:02,640 --> 00:45:07,680
the top and another piece are each of
926
00:45:05,440 --> 00:45:10,480
these chat messages.
927
00:45:07,680 --> 00:45:12,400
So these are called components. A
928
00:45:10,480 --> 00:45:15,680
component is just a piece of the
929
00:45:12,400 --> 00:45:18,160
website. Now, when building websites,
930
00:45:15,680 --> 00:45:21,040
it's better to split up the website into
931
00:45:18,160 --> 00:45:23,440
pieces or components so that we can work
932
00:45:21,040 --> 00:45:26,440
on a small piece of the website at a
933
00:45:23,440 --> 00:45:26,440
time.
934
00:45:26,880 --> 00:45:31,520
Now that we know what components are,
935
00:45:29,119 --> 00:45:34,640
let's create our first component in
936
00:45:31,520 --> 00:45:37,599
React. So, we mentioned that one way we
937
00:45:34,640 --> 00:45:40,240
can split up this website is to have one
938
00:45:37,599 --> 00:45:42,720
component at the top for this text box
939
00:45:40,240 --> 00:45:45,839
and the button. So, let's create a
940
00:45:42,720 --> 00:45:48,400
component for this in React. We'll go
941
00:45:45,839 --> 00:45:51,680
back to our code
942
00:45:48,400 --> 00:45:54,000
and we'll scroll down and first let's
943
00:45:51,680 --> 00:45:57,760
remove the example button and paragraph
944
00:45:54,000 --> 00:46:00,160
that we used. So, we'll select this and
945
00:45:57,760 --> 00:46:02,640
remove it.
946
00:46:00,160 --> 00:46:05,200
And to create a component in React,
947
00:46:02,640 --> 00:46:08,240
we're going to use a function. So we'll
948
00:46:05,200 --> 00:46:11,200
type function. And now we're going to
949
00:46:08,240 --> 00:46:13,599
name this component. Since this piece of
950
00:46:11,200 --> 00:46:16,079
the website is for inputting a message
951
00:46:13,599 --> 00:46:19,920
and sending it to the chatbot, let's
952
00:46:16,079 --> 00:46:23,440
name this component capital C chat
953
00:46:19,920 --> 00:46:25,359
capital I input brackets and curly
954
00:46:23,440 --> 00:46:28,160
brackets.
955
00:46:25,359 --> 00:46:30,480
Now in React the component name must
956
00:46:28,160 --> 00:46:32,480
start with a capital letter. So make
957
00:46:30,480 --> 00:46:36,079
sure that chat input starts with a
958
00:46:32,480 --> 00:46:38,720
capital C. This is also known as Pascal
959
00:46:36,079 --> 00:46:42,599
case where each word in the name starts
960
00:46:38,720 --> 00:46:42,599
with a capital letter.
961
00:46:43,119 --> 00:46:50,000
Next this function will return some
962
00:46:45,920 --> 00:46:52,800
HTML. So we're going to type return and
963
00:46:50,000 --> 00:46:55,200
we're going to return the HTML for this
964
00:46:52,800 --> 00:46:58,240
piece of the website. So this piece of
965
00:46:55,200 --> 00:47:01,599
the website has a text box and a button.
966
00:46:58,240 --> 00:47:04,160
So let's type brackets and press enter
967
00:47:01,599 --> 00:47:06,240
between these brackets.
968
00:47:04,160 --> 00:47:10,480
And to create a text box, we're going to
969
00:47:06,240 --> 00:47:12,880
use the HTML element less than input
970
00:47:10,480 --> 00:47:17,200
greater than. And we also need to type
971
00:47:12,880 --> 00:47:20,720
less than /input greater than. Now you
972
00:47:17,200 --> 00:47:24,160
may notice that in normal HTML input
973
00:47:20,720 --> 00:47:28,079
doesn't need this closing tag. However,
974
00:47:24,160 --> 00:47:30,400
JSX is more strict than normal HTML. So,
975
00:47:28,079 --> 00:47:33,440
all elements need a closing tag,
976
00:47:30,400 --> 00:47:36,000
including input. Now, there's actually a
977
00:47:33,440 --> 00:47:38,480
shortcut we can use here. If there's
978
00:47:36,000 --> 00:47:42,000
nothing inside an element, we can
979
00:47:38,480 --> 00:47:45,040
actually remove the closing tag.
980
00:47:42,000 --> 00:47:48,720
And at the end of this opening tag, we
981
00:47:45,040 --> 00:47:52,240
can just type space and then slash. So,
982
00:47:48,720 --> 00:47:54,160
this code is a shortcut for this code.
983
00:47:52,240 --> 00:47:57,520
And this is called a self-closing
984
00:47:54,160 --> 00:48:00,000
element. So in JSX we have to either
985
00:47:57,520 --> 00:48:03,040
have a closing tag or use this
986
00:48:00,000 --> 00:48:05,599
self-closing element shortcut.
987
00:48:03,040 --> 00:48:08,640
Next, this piece of the website also has
988
00:48:05,599 --> 00:48:11,839
a button. So let's also create a button
989
00:48:08,640 --> 00:48:16,400
element using less than button greater
990
00:48:11,839 --> 00:48:18,960
than less than /button greater than. And
991
00:48:16,400 --> 00:48:20,720
inside this button we have the text
992
00:48:18,960 --> 00:48:22,640
send.
993
00:48:20,720 --> 00:48:24,720
And now we're going to get an error here
994
00:48:22,640 --> 00:48:26,720
because we're trying to return two
995
00:48:24,720 --> 00:48:29,040
values or two elements from this
996
00:48:26,720 --> 00:48:31,599
function. But we can only return one
997
00:48:29,040 --> 00:48:34,400
value from a function. To solve this
998
00:48:31,599 --> 00:48:36,880
problem, we can use a div element like
999
00:48:34,400 --> 00:48:38,400
down here to group these two elements
1000
00:48:36,880 --> 00:48:40,880
together.
1001
00:48:38,400 --> 00:48:43,119
So above this, let's create a div
1002
00:48:40,880 --> 00:48:47,040
element.
1003
00:48:43,119 --> 00:48:50,400
And at the bottom, less than /d greater
1004
00:48:47,040 --> 00:48:53,040
than. And let's also add indents before
1005
00:48:50,400 --> 00:48:56,160
these two lines. So we'll click here and
1006
00:48:53,040 --> 00:48:57,839
press tab. And also click here and press
1007
00:48:56,160 --> 00:48:59,839
tab.
1008
00:48:57,839 --> 00:49:02,960
So this shows that these two elements
1009
00:48:59,839 --> 00:49:05,359
are inside this div element. And now
1010
00:49:02,960 --> 00:49:07,599
this code no longer has an error because
1011
00:49:05,359 --> 00:49:09,520
we're returning one value from this
1012
00:49:07,599 --> 00:49:11,760
function.
1013
00:49:09,520 --> 00:49:13,920
Now that we created this component,
1014
00:49:11,760 --> 00:49:18,000
let's learn how to display it on the
1015
00:49:13,920 --> 00:49:21,520
website. If we scroll down, right now
1016
00:49:18,000 --> 00:49:24,400
we're using React to display this div on
1017
00:49:21,520 --> 00:49:27,440
the website. So all we have to do is to
1018
00:49:24,400 --> 00:49:30,240
insert our component inside this div and
1019
00:49:27,440 --> 00:49:32,559
it will be displayed on the website. So
1020
00:49:30,240 --> 00:49:35,119
one way to do this is to use curly
1021
00:49:32,559 --> 00:49:38,559
brackets to insert something like we did
1022
00:49:35,119 --> 00:49:41,119
before. So here we'll type curly
1023
00:49:38,559 --> 00:49:44,240
brackets. And now we're just going to
1024
00:49:41,119 --> 00:49:49,440
run this function
1025
00:49:44,240 --> 00:49:52,079
capital C chat input and brackets.
1026
00:49:49,440 --> 00:49:55,280
So this will run the function and it
1027
00:49:52,079 --> 00:49:57,440
will return this HTML from the function
1028
00:49:55,280 --> 00:50:00,400
and then the curly brackets will insert
1029
00:49:57,440 --> 00:50:02,400
the HTML into the div and then display
1030
00:50:00,400 --> 00:50:04,640
it on the page.
1031
00:50:02,400 --> 00:50:06,800
So, as you can see, we can put any
1032
00:50:04,640 --> 00:50:09,359
JavaScript code between these curly
1033
00:50:06,800 --> 00:50:12,240
brackets. As long as this code results
1034
00:50:09,359 --> 00:50:14,400
in a value, we can insert it into this
1035
00:50:12,240 --> 00:50:16,400
HTML.
1036
00:50:14,400 --> 00:50:19,760
Let's remove the button and the
1037
00:50:16,400 --> 00:50:22,640
paragraph that we used as an example
1038
00:50:19,760 --> 00:50:24,720
and save.
1039
00:50:22,640 --> 00:50:27,280
And let's check our website. We'll open
1040
00:50:24,720 --> 00:50:30,640
the browser and go to our chatbot
1041
00:50:27,280 --> 00:50:32,720
project. And now we created the elements
1042
00:50:30,640 --> 00:50:35,280
for the top section of the chatbot
1043
00:50:32,720 --> 00:50:37,760
project, the text box and the send
1044
00:50:35,280 --> 00:50:40,000
button. So don't worry about how it
1045
00:50:37,760 --> 00:50:42,480
looks for now. Later in the course,
1046
00:50:40,000 --> 00:50:46,640
we're going to style this with CSS and
1047
00:50:42,480 --> 00:50:50,240
make it look like the final project.
1048
00:50:46,640 --> 00:50:53,839
Let's go back to our code.
1049
00:50:50,240 --> 00:50:57,359
And next, React has a special syntax we
1050
00:50:53,839 --> 00:50:59,920
can use to insert a component. Instead
1051
00:50:57,359 --> 00:51:03,359
of using curly brackets and running the
1052
00:50:59,920 --> 00:51:06,640
function, we can type this code less
1053
00:51:03,359 --> 00:51:11,280
than and then the name of the component
1054
00:51:06,640 --> 00:51:16,240
capital C chat input greater than and
1055
00:51:11,280 --> 00:51:17,760
then less than slash chat input greater
1056
00:51:16,240 --> 00:51:21,440
than.
1057
00:51:17,760 --> 00:51:23,680
So this is called the component syntax.
1058
00:51:21,440 --> 00:51:27,440
This does the same thing as the code
1059
00:51:23,680 --> 00:51:30,079
above. It runs the chat input function
1060
00:51:27,440 --> 00:51:31,760
and then inserts the result into the
1061
00:51:30,079 --> 00:51:34,079
HTML.
1062
00:51:31,760 --> 00:51:36,960
If we save
1063
00:51:34,079 --> 00:51:39,359
and we check our project, you'll notice
1064
00:51:36,960 --> 00:51:43,280
that that syntax does the same thing and
1065
00:51:39,359 --> 00:51:46,480
it creates another text box and button.
1066
00:51:43,280 --> 00:51:49,119
Let's go back to our code.
1067
00:51:46,480 --> 00:51:53,760
And again, this code does the same thing
1068
00:51:49,119 --> 00:51:55,359
as this code. Now this syntax has a big
1069
00:51:53,760 --> 00:51:58,160
advantage.
1070
00:51:55,359 --> 00:52:01,359
This actually looks like an HTML
1071
00:51:58,160 --> 00:52:03,839
element. It has an opening tag and a
1072
00:52:01,359 --> 00:52:07,599
closing tag. So you can think of this
1073
00:52:03,839 --> 00:52:10,160
like we're creating our own HTML element
1074
00:52:07,599 --> 00:52:12,400
called chat input.
1075
00:52:10,160 --> 00:52:16,640
HTML provides us with some default
1076
00:52:12,400 --> 00:52:19,119
elements like div, input and button. And
1077
00:52:16,640 --> 00:52:23,599
now using React, we can actually create
1078
00:52:19,119 --> 00:52:26,960
our own HTML element like chat input.
1079
00:52:23,599 --> 00:52:29,200
Then we can use our own HTML elements to
1080
00:52:26,960 --> 00:52:32,400
build the website instead of just the
1081
00:52:29,200 --> 00:52:35,440
default HTML elements. And this is
1082
00:52:32,400 --> 00:52:39,599
actually one of the main ideas of React.
1083
00:52:35,440 --> 00:52:42,240
We can create our own HTML elements. So
1084
00:52:39,599 --> 00:52:44,880
when using React, we should use this
1085
00:52:42,240 --> 00:52:47,440
component syntax instead of running the
1086
00:52:44,880 --> 00:52:50,880
component function directly because this
1087
00:52:47,440 --> 00:52:52,400
syntax lets us create our own HTML
1088
00:52:50,880 --> 00:52:55,280
element.
1089
00:52:52,400 --> 00:52:58,559
And again, we can use the self-closing
1090
00:52:55,280 --> 00:53:01,520
element shortcut like we did up here. If
1091
00:52:58,559 --> 00:53:04,400
there is nothing inside this element, we
1092
00:53:01,520 --> 00:53:07,359
can remove this closing tag.
1093
00:53:04,400 --> 00:53:11,280
And inside the opening tag at the end,
1094
00:53:07,359 --> 00:53:13,920
we can type space and slash.
1095
00:53:11,280 --> 00:53:16,079
And now we can remove this example code
1096
00:53:13,920 --> 00:53:21,280
above.
1097
00:53:16,079 --> 00:53:23,520
And let's save and check our project.
1098
00:53:21,280 --> 00:53:27,160
And that creates the top section of the
1099
00:53:23,520 --> 00:53:27,160
chatbot project.
1100
00:53:27,200 --> 00:53:32,319
So go back to VS Code.
1101
00:53:30,079 --> 00:53:34,640
And we mentioned in the previous lesson
1102
00:53:32,319 --> 00:53:37,040
that React helps us create websites
1103
00:53:34,640 --> 00:53:40,079
easier. And this is another way that
1104
00:53:37,040 --> 00:53:42,240
React helps us create websites easier.
1105
00:53:40,079 --> 00:53:45,119
It lets us split our website into
1106
00:53:42,240 --> 00:53:47,520
smaller pieces or components. And this
1107
00:53:45,119 --> 00:53:51,440
lets us work on a small piece of the
1108
00:53:47,520 --> 00:53:53,200
website at a time.
1109
00:53:51,440 --> 00:53:55,599
Before we create the rest of the
1110
00:53:53,200 --> 00:53:59,440
project, we're going to improve our code
1111
00:53:55,599 --> 00:54:02,480
a little bit. First, notice that in this
1112
00:53:59,440 --> 00:54:05,839
component, we're using a div to group
1113
00:54:02,480 --> 00:54:08,240
these two elements together. However,
1114
00:54:05,839 --> 00:54:11,599
this is also going to insert an extra
1115
00:54:08,240 --> 00:54:15,040
div element into our website. So, if we
1116
00:54:11,599 --> 00:54:19,760
go to our website
1117
00:54:15,040 --> 00:54:21,280
and rightclick and click inspect,
1118
00:54:19,760 --> 00:54:24,079
and we're going to go to the elements
1119
00:54:21,280 --> 00:54:25,920
tab, and we're going to go to our
1120
00:54:24,079 --> 00:54:27,839
container here, and we're going to
1121
00:54:25,920 --> 00:54:30,240
expand it.
1122
00:54:27,839 --> 00:54:33,520
So notice here that we have a bunch of
1123
00:54:30,240 --> 00:54:35,359
extra divs on this website. So what if
1124
00:54:33,520 --> 00:54:38,319
we don't want to insert these extra
1125
00:54:35,359 --> 00:54:41,920
divs? What if we just want to insert the
1126
00:54:38,319 --> 00:54:43,920
text box and the button on our website?
1127
00:54:41,920 --> 00:54:46,559
Let's go back to our code and learn how
1128
00:54:43,920 --> 00:54:49,119
to do that.
1129
00:54:46,559 --> 00:54:52,640
React has another way to group elements
1130
00:54:49,119 --> 00:54:56,640
together called a fragment. To use a
1131
00:54:52,640 --> 00:55:00,000
fragment, we can remove the word div.
1132
00:54:56,640 --> 00:55:02,480
and div from the closing tag and just
1133
00:55:00,000 --> 00:55:05,359
have a less than greater than and a less
1134
00:55:02,480 --> 00:55:08,160
than slashgreater than. So this is
1135
00:55:05,359 --> 00:55:10,319
called a fragment and it groups the two
1136
00:55:08,160 --> 00:55:13,119
elements together. So we can return it
1137
00:55:10,319 --> 00:55:17,200
from the function but it won't insert an
1138
00:55:13,119 --> 00:55:19,839
extra div. So, if we save this code and
1139
00:55:17,200 --> 00:55:22,559
then go to our website
1140
00:55:19,839 --> 00:55:25,119
and take a look at the HTML,
1141
00:55:22,559 --> 00:55:27,520
you'll notice now we have one less extra
1142
00:55:25,119 --> 00:55:29,680
div on the website.
1143
00:55:27,520 --> 00:55:31,760
Let's go back
1144
00:55:29,680 --> 00:55:34,880
and we can also use a fragment down
1145
00:55:31,760 --> 00:55:38,559
here. If we don't want to insert this
1146
00:55:34,880 --> 00:55:40,960
extra div, we can remove it and remove
1147
00:55:38,559 --> 00:55:44,880
it at the bottom and just use a
1148
00:55:40,960 --> 00:55:47,200
fragment. If we save and check the
1149
00:55:44,880 --> 00:55:49,920
website,
1150
00:55:47,200 --> 00:55:52,319
now we no longer have those extra divs.
1151
00:55:49,920 --> 00:55:56,319
We have our container and we just
1152
00:55:52,319 --> 00:55:58,799
inserted the text box and the button.
1153
00:55:56,319 --> 00:56:01,280
So that's fragments. It allows us to
1154
00:55:58,799 --> 00:56:04,480
group elements together without creating
1155
00:56:01,280 --> 00:56:07,680
an extra div.
1156
00:56:04,480 --> 00:56:10,240
Let's go back to our code.
1157
00:56:07,680 --> 00:56:13,440
And since this variable no longer
1158
00:56:10,240 --> 00:56:15,359
contains a div, let's rename it. So
1159
00:56:13,440 --> 00:56:18,559
right now we're building a chatbot
1160
00:56:15,359 --> 00:56:22,240
application or an app. So let's rename
1161
00:56:18,559 --> 00:56:25,359
this variable to app instead. And here
1162
00:56:22,240 --> 00:56:28,319
as well, we're going to render the app
1163
00:56:25,359 --> 00:56:30,960
and save.
1164
00:56:28,319 --> 00:56:33,200
So again in React, when returning
1165
00:56:30,960 --> 00:56:36,240
multiple elements or saving multiple
1166
00:56:33,200 --> 00:56:39,280
elements, we can use a fragment to group
1167
00:56:36,240 --> 00:56:41,119
them together. And this makes our HTML a
1168
00:56:39,280 --> 00:56:45,280
little bit cleaner because it doesn't
1169
00:56:41,119 --> 00:56:47,920
create an extra div element.
1170
00:56:45,280 --> 00:56:50,640
Next, another improvement we can make is
1171
00:56:47,920 --> 00:56:53,599
to add a placeholder to the text box at
1172
00:56:50,640 --> 00:56:56,559
the top. So the gray text inside the
1173
00:56:53,599 --> 00:56:59,200
text box is called a placeholder. It
1174
00:56:56,559 --> 00:57:02,000
tells us what to type inside the text
1175
00:56:59,200 --> 00:57:03,520
box. And when we start typing, it will
1176
00:57:02,000 --> 00:57:06,240
disappear.
1177
00:57:03,520 --> 00:57:09,119
To create a placeholder, we give this
1178
00:57:06,240 --> 00:57:12,720
input element an attribute. So at the
1179
00:57:09,119 --> 00:57:14,640
end, we're going to type the attribute
1180
00:57:12,720 --> 00:57:17,280
placeholder
1181
00:57:14,640 --> 00:57:19,280
equals double quotes.
1182
00:57:17,280 --> 00:57:21,119
And then inside these double quotes,
1183
00:57:19,280 --> 00:57:26,640
we're going to put the text that we
1184
00:57:21,119 --> 00:57:29,280
want, which is send a message to
1185
00:57:26,640 --> 00:57:31,839
chatbot.
1186
00:57:29,280 --> 00:57:34,720
If we save this code
1187
00:57:31,839 --> 00:57:36,880
and look at our website,
1188
00:57:34,720 --> 00:57:40,240
now we have a placeholder in the text
1189
00:57:36,880 --> 00:57:42,559
box. And when we start typing in it, it
1190
00:57:40,240 --> 00:57:45,760
will disappear. So the placeholder is
1191
00:57:42,559 --> 00:57:48,480
working. Now there is one problem here,
1192
00:57:45,760 --> 00:57:50,400
which is if we remove this text, you'll
1193
00:57:48,480 --> 00:57:53,200
notice that this placeholder is too long
1194
00:57:50,400 --> 00:57:56,240
and it's getting cut off. To fix this,
1195
00:57:53,200 --> 00:57:58,559
let's make this text box wider. We'll go
1196
00:57:56,240 --> 00:58:00,880
back to our code.
1197
00:57:58,559 --> 00:58:03,200
And to make this input wider, we're
1198
00:58:00,880 --> 00:58:06,160
going to give it another attribute. So
1199
00:58:03,200 --> 00:58:10,079
at the end, we'll type another attribute
1200
00:58:06,160 --> 00:58:12,960
called size equals double quotes. And
1201
00:58:10,079 --> 00:58:16,160
this sets how many characters can fit in
1202
00:58:12,960 --> 00:58:19,440
this input. By the way, a character is
1203
00:58:16,160 --> 00:58:23,040
just a letter, number, or symbol.
1204
00:58:19,440 --> 00:58:26,880
Let's set the size to 30 characters and
1205
00:58:23,040 --> 00:58:29,920
save and take a look.
1206
00:58:26,880 --> 00:58:32,559
Okay, so that looks pretty good for now.
1207
00:58:29,920 --> 00:58:34,720
Again, don't worry about how this looks.
1208
00:58:32,559 --> 00:58:37,359
We'll style it later in the course using
1209
00:58:34,720 --> 00:58:40,079
CSS.
1210
00:58:37,359 --> 00:58:42,720
So, we just created our first component
1211
00:58:40,079 --> 00:58:45,520
or our first piece of the website, which
1212
00:58:42,720 --> 00:58:47,760
is the section at the top. Now, let's
1213
00:58:45,520 --> 00:58:49,520
get some practice and create more
1214
00:58:47,760 --> 00:58:51,599
components.
1215
00:58:49,520 --> 00:58:53,440
Let's go back to the final chatbot
1216
00:58:51,599 --> 00:58:56,319
project
1217
00:58:53,440 --> 00:59:00,000
and let's ask ourselves how can we split
1218
00:58:56,319 --> 00:59:02,240
up the rest of this website into pieces.
1219
00:59:00,000 --> 00:59:05,440
Another piece of this website could be
1220
00:59:02,240 --> 00:59:08,640
one chat message here. So let's create a
1221
00:59:05,440 --> 00:59:12,240
component for this piece of the website.
1222
00:59:08,640 --> 00:59:14,640
We'll go back to VS Code
1223
00:59:12,240 --> 00:59:18,480
and to create a component we're going to
1224
00:59:14,640 --> 00:59:21,200
use a function. So down here, let's
1225
00:59:18,480 --> 00:59:25,440
create a function
1226
00:59:21,200 --> 00:59:27,760
and let's name it capital C chat capital
1227
00:59:25,440 --> 00:59:31,280
M message
1228
00:59:27,760 --> 00:59:33,599
brackets and curly brackets. So remember
1229
00:59:31,280 --> 00:59:36,480
a component name must start with a
1230
00:59:33,599 --> 00:59:38,400
capital letter and we use Pascal case.
1231
00:59:36,480 --> 00:59:40,400
So each word starts with a capital
1232
00:59:38,400 --> 00:59:43,280
letter.
1233
00:59:40,400 --> 00:59:46,559
Now inside this function, we're going to
1234
00:59:43,280 --> 00:59:49,040
return the HTML for this piece of the
1235
00:59:46,559 --> 00:59:52,799
website or this component.
1236
00:59:49,040 --> 00:59:55,280
So let's also type brackets and enter.
1237
00:59:52,799 --> 00:59:59,599
So we can write the HTML on multiple
1238
00:59:55,280 --> 01:00:03,599
lines. And now each chat message has two
1239
00:59:59,599 --> 01:00:05,599
things, some text and a profile image.
1240
01:00:03,599 --> 01:00:07,760
Since we have multiple things inside
1241
01:00:05,599 --> 01:00:10,559
this component, let's group them
1242
01:00:07,760 --> 01:00:13,599
together using a fragment. So we can
1243
01:00:10,559 --> 01:00:16,799
return one value from this function. So
1244
01:00:13,599 --> 01:00:19,839
we'll type less than greater than enter
1245
01:00:16,799 --> 01:00:24,079
and then less than /g greater than to
1246
01:00:19,839 --> 01:00:26,880
create a fragment. And now inside here
1247
01:00:24,079 --> 01:00:29,359
each message has some text. So let's
1248
01:00:26,880 --> 01:00:31,680
create the first chat message in our
1249
01:00:29,359 --> 01:00:33,440
project. We'll go back to the final
1250
01:00:31,680 --> 01:00:35,920
project.
1251
01:00:33,440 --> 01:00:38,079
And let's just copy paste the text from
1252
01:00:35,920 --> 01:00:41,760
the first message here which is hello
1253
01:00:38,079 --> 01:00:44,640
chatbot. So we'll select this and then
1254
01:00:41,760 --> 01:00:47,440
copy by pressing control C on Windows or
1255
01:00:44,640 --> 01:00:50,160
command C on Mac.
1256
01:00:47,440 --> 01:00:52,799
And then inside our code
1257
01:00:50,160 --> 01:00:55,440
in the chat message component we're
1258
01:00:52,799 --> 01:00:59,520
going to paste by pressing controlV on
1259
01:00:55,440 --> 01:01:01,839
Windows or commandV on Mac.
1260
01:00:59,520 --> 01:01:03,839
And let's fix the indent by pressing tab
1261
01:01:01,839 --> 01:01:06,880
at the start.
1262
01:01:03,839 --> 01:01:10,000
Next, each chat message has a profile
1263
01:01:06,880 --> 01:01:13,680
image. To create an image, we can use an
1264
01:01:10,000 --> 01:01:15,760
HTML element called img. So, let's
1265
01:01:13,680 --> 01:01:19,839
create a new line. And we're going to
1266
01:01:15,760 --> 01:01:23,680
type less than img greater than and then
1267
01:01:19,839 --> 01:01:26,640
less than / img greater than. So,
1268
01:01:23,680 --> 01:01:30,000
remember that in JSX, every element has
1269
01:01:26,640 --> 01:01:32,559
to have a closing tag. And again, we can
1270
01:01:30,000 --> 01:01:34,720
use the self-closing element shortcut
1271
01:01:32,559 --> 01:01:37,520
like we did down here. If there's
1272
01:01:34,720 --> 01:01:40,640
nothing inside this element, we can
1273
01:01:37,520 --> 01:01:43,680
remove the closing tag and then just add
1274
01:01:40,640 --> 01:01:46,559
a space and a slash at the end of the
1275
01:01:43,680 --> 01:01:48,400
opening tag. And now we have the two
1276
01:01:46,559 --> 01:01:53,599
things that we need for each chat
1277
01:01:48,400 --> 01:01:56,079
message, the text and an image.
1278
01:01:53,599 --> 01:01:59,280
Next, we need to tell the computer which
1279
01:01:56,079 --> 01:02:02,400
image to load. Right now, if we look at
1280
01:01:59,280 --> 01:02:04,720
our final chatbot project, we have two
1281
01:02:02,400 --> 01:02:06,799
images that we use, but we currently
1282
01:02:04,720 --> 01:02:09,200
don't have these in our project. So,
1283
01:02:06,799 --> 01:02:12,400
let's download them. We're going to
1284
01:02:09,200 --> 01:02:15,520
rightclick this first image and then
1285
01:02:12,400 --> 01:02:17,040
click save image.
1286
01:02:15,520 --> 01:02:19,680
And we're going to find the folder that
1287
01:02:17,040 --> 01:02:22,400
contains all of our code. So, for me,
1288
01:02:19,680 --> 01:02:25,680
it's on the desktop
1289
01:02:22,400 --> 01:02:27,440
and it's in the folder React-Course.
1290
01:02:25,680 --> 01:02:30,240
and we're going to save it inside of
1291
01:02:27,440 --> 01:02:32,880
this folder.
1292
01:02:30,240 --> 01:02:35,280
And same things for this robot image.
1293
01:02:32,880 --> 01:02:38,400
We're going to rightclick
1294
01:02:35,280 --> 01:02:41,680
and click save image.
1295
01:02:38,400 --> 01:02:46,079
Go to the React-Course folder. And we're
1296
01:02:41,680 --> 01:02:48,880
also going to save the robot image.
1297
01:02:46,079 --> 01:02:51,599
And now if we look at our code,
1298
01:02:48,880 --> 01:02:57,760
we should have two images inside our
1299
01:02:51,599 --> 01:03:00,559
project. User.png png and robot.png.
1300
01:02:57,760 --> 01:03:03,440
And now let's go back to our code.
1301
01:03:00,559 --> 01:03:05,440
To load an image on the website, we need
1302
01:03:03,440 --> 01:03:08,319
to tell the computer which image to
1303
01:03:05,440 --> 01:03:11,520
load. To do that, we're going to give
1304
01:03:08,319 --> 01:03:16,720
this image element an attribute called
1305
01:03:11,520 --> 01:03:18,400
src or source equals double quotes. And
1306
01:03:16,720 --> 01:03:21,680
inside these double quotes, we're going
1307
01:03:18,400 --> 01:03:24,319
to tell the computer which image to load
1308
01:03:21,680 --> 01:03:27,760
for the first chat message. The image is
1309
01:03:24,319 --> 01:03:32,119
the user image. So let's type this file
1310
01:03:27,760 --> 01:03:32,119
name. userpng.
1311
01:03:32,160 --> 01:03:36,079
So this will look for a file called
1312
01:03:34,559 --> 01:03:40,160
user.png
1313
01:03:36,079 --> 01:03:42,640
beside the current HTML file. This is a
1314
01:03:40,160 --> 01:03:46,160
current HTML file. So we'll look for
1315
01:03:42,640 --> 01:03:48,000
user.png beside it, which is right here.
1316
01:03:46,160 --> 01:03:50,319
and then it's going to load this image
1317
01:03:48,000 --> 01:03:53,119
on the website.
1318
01:03:50,319 --> 01:03:56,079
Okay, so we created a component for a
1319
01:03:53,119 --> 01:03:58,720
chat message. Now let's insert this
1320
01:03:56,079 --> 01:04:00,559
component on the website. We're going to
1321
01:03:58,720 --> 01:04:03,440
scroll down
1322
01:04:00,559 --> 01:04:05,839
to the app and let's insert this
1323
01:04:03,440 --> 01:04:08,559
component using the component syntax
1324
01:04:05,839 --> 01:04:11,839
like we did up here. So we'll type a new
1325
01:04:08,559 --> 01:04:14,799
line and type less than and then the
1326
01:04:11,839 --> 01:04:16,559
name of the component which is capital C
1327
01:04:14,799 --> 01:04:19,440
chat
1328
01:04:16,559 --> 01:04:21,440
capital M message.
1329
01:04:19,440 --> 01:04:24,400
And currently we don't have anything to
1330
01:04:21,440 --> 01:04:26,960
put inside this component. So we use the
1331
01:04:24,400 --> 01:04:31,039
self-closing element shortcut like we
1332
01:04:26,960 --> 01:04:33,760
did up here. So now let's save
1333
01:04:31,039 --> 01:04:36,720
and check our website
1334
01:04:33,760 --> 01:04:40,079
which is this tab. And let's close the
1335
01:04:36,720 --> 01:04:43,039
console so we can see it. And we created
1336
01:04:40,079 --> 01:04:45,599
a chat message.
1337
01:04:43,039 --> 01:04:47,359
However, we have a few problems here.
1338
01:04:45,599 --> 01:04:50,480
The first problem is that the profile
1339
01:04:47,359 --> 01:04:53,119
image is too big. To make it smaller,
1340
01:04:50,480 --> 01:04:56,240
we're going to go back to our code
1341
01:04:53,119 --> 01:04:59,200
and inside the img element, we're going
1342
01:04:56,240 --> 01:05:03,200
to give it another attribute called
1343
01:04:59,200 --> 01:05:06,640
width equals double quotes.
1344
01:05:03,200 --> 01:05:10,720
And let's try setting the width to 50
1345
01:05:06,640 --> 01:05:14,319
and save. And then check our website.
1346
01:05:10,720 --> 01:05:17,200
And that looks close enough.
1347
01:05:14,319 --> 01:05:19,520
Another problem is the chat message is
1348
01:05:17,200 --> 01:05:22,319
appearing on the same line as the chat
1349
01:05:19,520 --> 01:05:25,760
input, but in the final project, we want
1350
01:05:22,319 --> 01:05:28,480
it to appear below. To fix this, we'll
1351
01:05:25,760 --> 01:05:31,520
go back to our code
1352
01:05:28,480 --> 01:05:33,599
and inside the chat message component,
1353
01:05:31,520 --> 01:05:35,599
instead of using a fragment to group
1354
01:05:33,599 --> 01:05:38,400
these elements together, we're going to
1355
01:05:35,599 --> 01:05:44,079
change it back to a div. So, inside
1356
01:05:38,400 --> 01:05:46,400
we'll type div and inside here /d.
1357
01:05:44,079 --> 01:05:49,920
The reason we're using a div is because
1358
01:05:46,400 --> 01:05:53,200
in HTML, a div is known as a block
1359
01:05:49,920 --> 01:05:56,640
element. A block element will always
1360
01:05:53,200 --> 01:05:59,520
take up an entire line by itself. So if
1361
01:05:56,640 --> 01:06:02,480
we group this in a div element, instead
1362
01:05:59,520 --> 01:06:04,640
of appearing beside the chat input, the
1363
01:06:02,480 --> 01:06:06,799
div is going to take up an entire line
1364
01:06:04,640 --> 01:06:10,240
by itself. So it's going to take up the
1365
01:06:06,799 --> 01:06:13,200
entire line under the chat input. So
1366
01:06:10,240 --> 01:06:15,119
let's save this code and then check our
1367
01:06:13,200 --> 01:06:18,000
website.
1368
01:06:15,119 --> 01:06:23,920
So now this div is taking up this entire
1369
01:06:18,000 --> 01:06:26,000
line. If we rightclick and click inspect
1370
01:06:23,920 --> 01:06:30,319
and then click this arrow icon in the
1371
01:06:26,000 --> 01:06:32,799
top left and then click on this div,
1372
01:06:30,319 --> 01:06:35,760
you can see that this div now takes up
1373
01:06:32,799 --> 01:06:39,039
this entire line. So now we have the
1374
01:06:35,760 --> 01:06:42,559
layout that we want. So in React, we
1375
01:06:39,039 --> 01:06:45,119
still use divs for layout purposes.
1376
01:06:42,559 --> 01:06:48,079
If you want to learn divs, layout, and
1377
01:06:45,119 --> 01:06:52,240
HTML in more detail, you can check out
1378
01:06:48,079 --> 01:06:54,559
my HTML and CSS full course in the video
1379
01:06:52,240 --> 01:06:56,960
description.
1380
01:06:54,559 --> 01:06:59,960
So now we created our first chat
1381
01:06:56,960 --> 01:06:59,960
message.
1382
01:07:00,319 --> 01:07:07,119
Next, let's create the second chat
1383
01:07:03,039 --> 01:07:09,119
message. We'll go back to our code
1384
01:07:07,119 --> 01:07:12,000
and instead of creating a whole new
1385
01:07:09,119 --> 01:07:14,960
component, we can actually reuse our
1386
01:07:12,000 --> 01:07:18,799
chat message component. Components are
1387
01:07:14,960 --> 01:07:21,440
actually designed to be reusable.
1388
01:07:18,799 --> 01:07:23,920
To reuse this component, we don't want
1389
01:07:21,440 --> 01:07:26,400
the message to be hello chatbot every
1390
01:07:23,920 --> 01:07:29,680
time. We want to be able to display
1391
01:07:26,400 --> 01:07:32,160
different messages in this component. To
1392
01:07:29,680 --> 01:07:35,280
do that, we can give our component an
1393
01:07:32,160 --> 01:07:37,920
attribute. So down here, we're going to
1394
01:07:35,280 --> 01:07:40,240
go to the chat message component and
1395
01:07:37,920 --> 01:07:43,920
we're going to give it an attribute. So
1396
01:07:40,240 --> 01:07:45,520
this works just like HTML attributes.
1397
01:07:43,920 --> 01:07:49,359
We're going to give our attribute a
1398
01:07:45,520 --> 01:07:52,960
name. Let's call it message
1399
01:07:49,359 --> 01:07:54,720
equals double quotes. And inside these
1400
01:07:52,960 --> 01:07:57,200
double quotes, we're going to give the
1401
01:07:54,720 --> 01:07:59,680
attribute a value.
1402
01:07:57,200 --> 01:08:02,640
Let's put the text for the first message
1403
01:07:59,680 --> 01:08:06,240
which is hello chatbot. So let's select
1404
01:08:02,640 --> 01:08:10,240
this code and then copy
1405
01:08:06,240 --> 01:08:12,240
and we're going to paste it inside here.
1406
01:08:10,240 --> 01:08:15,839
So now we have an attribute called
1407
01:08:12,240 --> 01:08:18,400
message with a value hello chatbot. So
1408
01:08:15,839 --> 01:08:21,520
again you can see that this syntax looks
1409
01:08:18,400 --> 01:08:23,440
like HTML. We can even add our own
1410
01:08:21,520 --> 01:08:24,960
attributes.
1411
01:08:23,440 --> 01:08:27,839
Now that we added this message
1412
01:08:24,960 --> 01:08:31,440
attribute, how do we access it inside
1413
01:08:27,839 --> 01:08:33,279
this component and use it? To do that,
1414
01:08:31,440 --> 01:08:34,799
we're going to use a feature called
1415
01:08:33,279 --> 01:08:37,359
props.
1416
01:08:34,799 --> 01:08:42,560
Every component function gets one
1417
01:08:37,359 --> 01:08:46,640
parameter up here called props.
1418
01:08:42,560 --> 01:08:49,199
This props parameter is an object and it
1419
01:08:46,640 --> 01:08:52,560
contains all the attributes that we give
1420
01:08:49,199 --> 01:08:55,759
to this component. So up here, let's
1421
01:08:52,560 --> 01:08:59,600
actually console.log
1422
01:08:55,759 --> 01:09:03,759
props to see what it looks like. Let's
1423
01:08:59,600 --> 01:09:06,239
save and then open our website and check
1424
01:09:03,759 --> 01:09:08,719
the console.
1425
01:09:06,239 --> 01:09:10,799
And you'll see that props is an object
1426
01:09:08,719 --> 01:09:13,120
and it contains the attributes that we
1427
01:09:10,799 --> 01:09:15,759
gave to our component. So here we have
1428
01:09:13,120 --> 01:09:18,640
the message attribute and it has a value
1429
01:09:15,759 --> 01:09:20,960
hello chatbot. So the attribute name is
1430
01:09:18,640 --> 01:09:23,199
saved as a property and the attributes
1431
01:09:20,960 --> 01:09:24,719
value is saved as a value of this
1432
01:09:23,199 --> 01:09:27,759
object.
1433
01:09:24,719 --> 01:09:30,560
So let's go back to VS code
1434
01:09:27,759 --> 01:09:34,480
and using this props object we can
1435
01:09:30,560 --> 01:09:37,359
access our attributes. By the way props
1436
01:09:34,480 --> 01:09:39,759
is short for properties. We call this
1437
01:09:37,359 --> 01:09:42,799
props because the attributes are saved
1438
01:09:39,759 --> 01:09:45,600
as properties in this object.
1439
01:09:42,799 --> 01:09:50,799
So now to access the message attribute
1440
01:09:45,600 --> 01:09:54,239
we can remove this and type props
1441
01:09:50,799 --> 01:09:57,239
dot and the attribute name which is
1442
01:09:54,239 --> 01:09:57,239
message
1443
01:09:57,440 --> 01:10:02,560
and let's save this in a variable so we
1444
01:09:59,760 --> 01:10:05,120
can use it later. So at the front we'll
1445
01:10:02,560 --> 01:10:07,199
create a variable using const name name
1446
01:10:05,120 --> 01:10:10,320
it message
1447
01:10:07,199 --> 01:10:12,400
equals props dossage
1448
01:10:10,320 --> 01:10:14,560
and we'll add a new line to separate the
1449
01:10:12,400 --> 01:10:17,440
code a little bit.
1450
01:10:14,560 --> 01:10:20,239
Now instead of displaying hello chatbot
1451
01:10:17,440 --> 01:10:23,040
every time let's display whatever is in
1452
01:10:20,239 --> 01:10:24,640
the message attribute. So we'll remove
1453
01:10:23,040 --> 01:10:27,520
this
1454
01:10:24,640 --> 01:10:30,960
and let's insert this variable into this
1455
01:10:27,520 --> 01:10:34,320
element. To do that we can use curly
1456
01:10:30,960 --> 01:10:36,880
brackets and type the variable name
1457
01:10:34,320 --> 01:10:39,440
message.
1458
01:10:36,880 --> 01:10:41,920
So again we can put any JavaScript code
1459
01:10:39,440 --> 01:10:44,400
between these curly brackets and as long
1460
01:10:41,920 --> 01:10:48,800
as it results in a value it will insert
1461
01:10:44,400 --> 01:10:51,840
it into this element. So now let's save
1462
01:10:48,800 --> 01:10:55,280
and we'll check the website
1463
01:10:51,840 --> 01:10:57,360
and it displays a message hello chatbot.
1464
01:10:55,280 --> 01:11:00,080
That's the message attribute that we
1465
01:10:57,360 --> 01:11:02,719
gave it.
1466
01:11:00,080 --> 01:11:06,480
Let's go back to our code.
1467
01:11:02,719 --> 01:11:09,040
So that's props. Just like a normal HTML
1468
01:11:06,480 --> 01:11:11,280
element, we can give attributes to our
1469
01:11:09,040 --> 01:11:15,120
component and then access these
1470
01:11:11,280 --> 01:11:18,000
attributes using this props object.
1471
01:11:15,120 --> 01:11:20,880
So props are really useful because props
1472
01:11:18,000 --> 01:11:23,120
make our component reusable.
1473
01:11:20,880 --> 01:11:26,560
For example, let's try creating the
1474
01:11:23,120 --> 01:11:28,640
second chat message. To do that, we can
1475
01:11:26,560 --> 01:11:32,880
just create another chat message
1476
01:11:28,640 --> 01:11:37,520
component. So down here, let's type less
1477
01:11:32,880 --> 01:11:39,040
than chat message/grater
1478
01:11:37,520 --> 01:11:41,120
than.
1479
01:11:39,040 --> 01:11:44,400
And we just need to give it a different
1480
01:11:41,120 --> 01:11:47,760
message attribute. So here we'll give it
1481
01:11:44,400 --> 01:11:51,120
an attribute message equals double
1482
01:11:47,760 --> 01:11:53,760
quotes. And we'll copy paste the second
1483
01:11:51,120 --> 01:11:55,840
message in the chatbot project. We'll go
1484
01:11:53,760 --> 01:11:58,880
to our browser
1485
01:11:55,840 --> 01:12:01,360
and go to the chatbot project and let's
1486
01:11:58,880 --> 01:12:05,280
copy and paste this second message.
1487
01:12:01,360 --> 01:12:08,320
We'll select it and then copy.
1488
01:12:05,280 --> 01:12:12,800
And inside our code in the message
1489
01:12:08,320 --> 01:12:15,199
attribute, we're going to paste.
1490
01:12:12,800 --> 01:12:19,120
So this will save the message hello how
1491
01:12:15,199 --> 01:12:21,920
can I help you inside props and then we
1492
01:12:19,120 --> 01:12:26,080
get it out of props and insert it into
1493
01:12:21,920 --> 01:12:28,239
the component. Let's save and check the
1494
01:12:26,080 --> 01:12:30,719
website
1495
01:12:28,239 --> 01:12:33,040
and go to our tab.
1496
01:12:30,719 --> 01:12:37,280
And now we created the second chat
1497
01:12:33,040 --> 01:12:39,360
message. So let's go back to our code.
1498
01:12:37,280 --> 01:12:43,040
And you can see that props make our
1499
01:12:39,360 --> 01:12:45,280
component reusable. We can reuse this
1500
01:12:43,040 --> 01:12:48,400
component, but just give it a different
1501
01:12:45,280 --> 01:12:50,640
attribute or prop and it will display a
1502
01:12:48,400 --> 01:12:53,199
different message.
1503
01:12:50,640 --> 01:12:56,239
Let's get some practice and use props to
1504
01:12:53,199 --> 01:12:58,239
create two more chat messages. Feel free
1505
01:12:56,239 --> 01:12:59,840
to pause if you want to try it yourself
1506
01:12:58,239 --> 01:13:02,159
first.
1507
01:12:59,840 --> 01:13:05,280
So down here, let's create our third
1508
01:13:02,159 --> 01:13:07,760
message using less than chat
1509
01:13:05,280 --> 01:13:11,199
message/grater
1510
01:13:07,760 --> 01:13:14,239
than. and we'll give it a message
1511
01:13:11,199 --> 01:13:16,960
attribute equals double quotes and we'll
1512
01:13:14,239 --> 01:13:20,080
copy paste the third message. We'll go
1513
01:13:16,960 --> 01:13:23,280
to the chatbot project
1514
01:13:20,080 --> 01:13:25,440
and select this third message and then
1515
01:13:23,280 --> 01:13:28,320
copy.
1516
01:13:25,440 --> 01:13:31,280
And in our code inside the attribute,
1517
01:13:28,320 --> 01:13:33,199
we're going to paste.
1518
01:13:31,280 --> 01:13:37,840
And finally, let's create the fourth
1519
01:13:33,199 --> 01:13:40,400
message less than chat message
1520
01:13:37,840 --> 01:13:43,679
slashgreater than and then the message
1521
01:13:40,400 --> 01:13:46,480
attribute equals double quotes and we'll
1522
01:13:43,679 --> 01:13:48,800
copy paste the fourth message which is
1523
01:13:46,480 --> 01:13:50,800
this one. So don't worry if your
1524
01:13:48,800 --> 01:13:53,440
messages are different from mine. We're
1525
01:13:50,800 --> 01:13:56,960
just doing an example. So we'll select
1526
01:13:53,440 --> 01:14:00,800
this and copy.
1527
01:13:56,960 --> 01:14:03,120
And inside here, we're going to paste
1528
01:14:00,800 --> 01:14:06,320
and save.
1529
01:14:03,120 --> 01:14:09,440
And now, if we check our website
1530
01:14:06,320 --> 01:14:12,080
and go to our tab, you'll see that we
1531
01:14:09,440 --> 01:14:15,280
now have four chat messages in our
1532
01:14:12,080 --> 01:14:19,199
project. So, that's how we use props to
1533
01:14:15,280 --> 01:14:22,400
reuse the chat message component.
1534
01:14:19,199 --> 01:14:24,960
Next, in the final chatbot project,
1535
01:14:22,400 --> 01:14:27,679
notice that these two messages are
1536
01:14:24,960 --> 01:14:30,159
different. These two messages are from
1537
01:14:27,679 --> 01:14:33,440
the chatbot. So there are two
1538
01:14:30,159 --> 01:14:37,600
differences here. The profile image is a
1539
01:14:33,440 --> 01:14:40,560
robot and the image is on the left. To
1540
01:14:37,600 --> 01:14:46,080
create this, we can add another prop to
1541
01:14:40,560 --> 01:14:48,800
chat message. Let's go back to VS Code
1542
01:14:46,080 --> 01:14:51,360
and for each chat message, we're going
1543
01:14:48,800 --> 01:14:55,440
to add another attribute.
1544
01:14:51,360 --> 01:14:58,000
And let's call this attribute the sender
1545
01:14:55,440 --> 01:15:00,640
equals double quotes.
1546
01:14:58,000 --> 01:15:02,719
This attribute tells us who sent the
1547
01:15:00,640 --> 01:15:05,440
message. And we can use this to
1548
01:15:02,719 --> 01:15:08,560
determine which image to load and where
1549
01:15:05,440 --> 01:15:13,520
to place the image. So if we send the
1550
01:15:08,560 --> 01:15:16,000
message, let's make the sender the user.
1551
01:15:13,520 --> 01:15:19,040
And for the second chat message, we're
1552
01:15:16,000 --> 01:15:21,679
going to add another sender attribute
1553
01:15:19,040 --> 01:15:24,640
equals double quotes. And the second
1554
01:15:21,679 --> 01:15:28,560
message is from the chatbot. So let's
1555
01:15:24,640 --> 01:15:30,080
make the sender attribute the robot.
1556
01:15:28,560 --> 01:15:32,719
And we'll do the same thing for the
1557
01:15:30,080 --> 01:15:35,760
third and fourth message. We'll give it
1558
01:15:32,719 --> 01:15:38,400
a sender attribute.
1559
01:15:35,760 --> 01:15:41,199
The third message is from the user. So
1560
01:15:38,400 --> 01:15:45,040
let's make the sender user.
1561
01:15:41,199 --> 01:15:47,920
And the fourth message sender equals
1562
01:15:45,040 --> 01:15:53,040
double quotes. This message is from the
1563
01:15:47,920 --> 01:15:56,239
robot. So let's make the sender robot.
1564
01:15:53,040 --> 01:15:59,679
Next, let's access the sender attribute
1565
01:15:56,239 --> 01:16:02,320
inside our component. To do that, all
1566
01:15:59,679 --> 01:16:05,360
the attributes are saved inside this
1567
01:16:02,320 --> 01:16:11,199
props object. To access the sender
1568
01:16:05,360 --> 01:16:13,760
attribute, we can type props dot sender.
1569
01:16:11,199 --> 01:16:17,440
So this gives us the value of the sender
1570
01:16:13,760 --> 01:16:19,600
attribute which is either user or robot.
1571
01:16:17,440 --> 01:16:23,120
So let's save this in a variable so we
1572
01:16:19,600 --> 01:16:28,320
can use it later. Type const and let's
1573
01:16:23,120 --> 01:16:30,000
call it sender equals props.
1574
01:16:28,320 --> 01:16:33,040
So now that we have the sender
1575
01:16:30,000 --> 01:16:35,679
attribute, let's use it to decide which
1576
01:16:33,040 --> 01:16:38,800
profile image to load and where to place
1577
01:16:35,679 --> 01:16:41,040
this image. To do that, we can just use
1578
01:16:38,800 --> 01:16:45,360
an if statement.
1579
01:16:41,040 --> 01:16:47,840
So inside here, we can type if brackets
1580
01:16:45,360 --> 01:16:49,760
and curly brackets.
1581
01:16:47,840 --> 01:16:53,280
And inside these brackets, we're going
1582
01:16:49,760 --> 01:16:57,920
to check if the sender
1583
01:16:53,280 --> 01:17:01,040
is equal to the string robot.
1584
01:16:57,920 --> 01:17:03,280
By the way, by default, if the attribute
1585
01:17:01,040 --> 01:17:06,239
just has double quotes like this, the
1586
01:17:03,280 --> 01:17:08,000
value is a string. So here the value is
1587
01:17:06,239 --> 01:17:10,080
going to be the string user or the
1588
01:17:08,000 --> 01:17:12,719
string robot.
1589
01:17:10,080 --> 01:17:15,040
So now inside our component, if the
1590
01:17:12,719 --> 01:17:17,840
sender is the robot, we're going to
1591
01:17:15,040 --> 01:17:21,920
return some different HTML.
1592
01:17:17,840 --> 01:17:25,199
So let's type return brackets. So we can
1593
01:17:21,920 --> 01:17:27,600
write this HTML on multiple lines. And
1594
01:17:25,199 --> 01:17:31,040
let's just copy paste the HTML from down
1595
01:17:27,600 --> 01:17:34,719
here and modify it. So we'll select this
1596
01:17:31,040 --> 01:17:37,360
HTML and then copy
1597
01:17:34,719 --> 01:17:40,080
and then inside this return we're going
1598
01:17:37,360 --> 01:17:42,880
to paste.
1599
01:17:40,080 --> 01:17:46,960
And let's also select these lines and
1600
01:17:42,880 --> 01:17:48,719
press tab to fix the indenting.
1601
01:17:46,960 --> 01:17:51,280
So the first thing we need to change if
1602
01:17:48,719 --> 01:17:54,320
the sender is a robot is that we want to
1603
01:17:51,280 --> 01:17:56,400
load the robot image instead.
1604
01:17:54,320 --> 01:17:59,840
So, we already downloaded the robot
1605
01:17:56,400 --> 01:18:02,480
image here. To load it, we're just going
1606
01:17:59,840 --> 01:18:05,120
to change the src attribute or the
1607
01:18:02,480 --> 01:18:07,760
source attribute of this image. So,
1608
01:18:05,120 --> 01:18:10,640
we're going to change this from user to
1609
01:18:07,760 --> 01:18:13,280
robot.png.
1610
01:18:10,640 --> 01:18:16,159
And we also want this image to be on the
1611
01:18:13,280 --> 01:18:18,719
left. So, let's just move this image
1612
01:18:16,159 --> 01:18:22,320
before the message. So we'll select this
1613
01:18:18,719 --> 01:18:26,159
code and then cut by pressing control X
1614
01:18:22,320 --> 01:18:28,400
on Windows or command X on Mac.
1615
01:18:26,159 --> 01:18:31,199
And then before the message we're going
1616
01:18:28,400 --> 01:18:34,159
to paste by pressing controlV on Windows
1617
01:18:31,199 --> 01:18:37,520
or commandV on Mac.
1618
01:18:34,159 --> 01:18:40,960
And we can remove this extra new line.
1619
01:18:37,520 --> 01:18:43,840
So as you can see inside a component we
1620
01:18:40,960 --> 01:18:46,560
can use any JavaScript feature like if
1621
01:18:43,840 --> 01:18:50,080
statement and we can return a different
1622
01:18:46,560 --> 01:18:54,000
HTML and whatever we return will be
1623
01:18:50,080 --> 01:18:56,800
inserted inside the element here. So now
1624
01:18:54,000 --> 01:18:59,760
let's save this code
1625
01:18:56,800 --> 01:19:02,560
and then check our website
1626
01:18:59,760 --> 01:19:04,480
and then open our tab
1627
01:19:02,560 --> 01:19:07,360
and you can see that now we have two
1628
01:19:04,480 --> 01:19:09,920
messages from the chatbot. that loads
1629
01:19:07,360 --> 01:19:12,320
the robot image and it puts the image on
1630
01:19:09,920 --> 01:19:15,199
the left.
1631
01:19:12,320 --> 01:19:18,480
We'll go back to our code
1632
01:19:15,199 --> 01:19:21,360
and using this sender prop, we're able
1633
01:19:18,480 --> 01:19:25,120
to reuse this component to create a chat
1634
01:19:21,360 --> 01:19:28,239
message from the robot. So again, props
1635
01:19:25,120 --> 01:19:31,840
make our component reusable.
1636
01:19:28,239 --> 01:19:34,000
And that's it. If we scroll down,
1637
01:19:31,840 --> 01:19:37,600
using components, we split up the
1638
01:19:34,000 --> 01:19:40,239
website or the app into smaller pieces
1639
01:19:37,600 --> 01:19:43,360
like the chat input section at the top
1640
01:19:40,239 --> 01:19:45,920
and each chat message. And we created
1641
01:19:43,360 --> 01:19:48,080
all the HTML that we need for this
1642
01:19:45,920 --> 01:19:49,440
project.
1643
01:19:48,080 --> 01:19:51,840
The last thing we're going to learn in
1644
01:19:49,440 --> 01:19:54,640
this lesson are some shortcuts that are
1645
01:19:51,840 --> 01:19:57,280
commonly used in React, and we can use
1646
01:19:54,640 --> 01:20:00,080
these to clean up our code.
1647
01:19:57,280 --> 01:20:02,000
First, let's scroll up to the chat
1648
01:20:00,080 --> 01:20:04,960
message component.
1649
01:20:02,000 --> 01:20:07,520
So, instead of doing con message equals
1650
01:20:04,960 --> 01:20:10,400
props message, we actually have a
1651
01:20:07,520 --> 01:20:14,159
shortcut for this in JavaScript. So,
1652
01:20:10,400 --> 01:20:19,600
down here we can just type const curly
1653
01:20:14,159 --> 01:20:24,080
brackets and inside we can type message
1654
01:20:19,600 --> 01:20:27,360
and make it equal to props.
1655
01:20:24,080 --> 01:20:29,760
So this takes the message property out
1656
01:20:27,360 --> 01:20:32,880
of this object and saves it in a
1657
01:20:29,760 --> 01:20:36,080
variable called message. So this does
1658
01:20:32,880 --> 01:20:39,920
the same thing as this line above. This
1659
01:20:36,080 --> 01:20:42,640
code is a shortcut for this code. And
1660
01:20:39,920 --> 01:20:44,880
this shortcut is called dstructuring
1661
01:20:42,640 --> 01:20:48,320
because we're taking stuff out of this
1662
01:20:44,880 --> 01:20:50,719
object. We're deconstructing it. We can
1663
01:20:48,320 --> 01:20:54,239
also use this shortcut with the sender
1664
01:20:50,719 --> 01:20:56,880
property to add another property to take
1665
01:20:54,239 --> 01:20:59,840
out of this object. After the first
1666
01:20:56,880 --> 01:21:03,280
property, we can type comma and the
1667
01:20:59,840 --> 01:21:05,520
second property sender.
1668
01:21:03,280 --> 01:21:08,000
So this will get the message property
1669
01:21:05,520 --> 01:21:10,719
and the sender property out of this
1670
01:21:08,000 --> 01:21:14,560
object and it will save it in a message
1671
01:21:10,719 --> 01:21:17,600
variable and a sender variable. So this
1672
01:21:14,560 --> 01:21:19,840
line of code is a shortcut for these two
1673
01:21:17,600 --> 01:21:21,920
lines above.
1674
01:21:19,840 --> 01:21:24,960
So now let's actually turn these two
1675
01:21:21,920 --> 01:21:28,159
lines into comments or comment out these
1676
01:21:24,960 --> 01:21:31,280
lines. At the front we're going to type
1677
01:21:28,159 --> 01:21:34,320
double slash and at the front here we'll
1678
01:21:31,280 --> 01:21:36,000
type double slash.
1679
01:21:34,320 --> 01:21:38,320
By the way, if we start with double
1680
01:21:36,000 --> 01:21:40,640
slash, it will turn this code into
1681
01:21:38,320 --> 01:21:42,960
comments and it will be ignored by the
1682
01:21:40,640 --> 01:21:44,640
computer.
1683
01:21:42,960 --> 01:21:47,199
So, we'll just keep this here as a
1684
01:21:44,640 --> 01:21:49,199
reminder that this is a shortcut for
1685
01:21:47,199 --> 01:21:52,159
these two.
1686
01:21:49,199 --> 01:21:55,760
Next, JavaScript has an even shorter
1687
01:21:52,159 --> 01:21:58,080
shortcut for this. Since props is a
1688
01:21:55,760 --> 01:22:01,920
parameter up here, we can just
1689
01:21:58,080 --> 01:22:04,639
dstructure it directly up here. So here
1690
01:22:01,920 --> 01:22:07,520
instead of the props object we can just
1691
01:22:04,639 --> 01:22:11,360
type curly brackets directly
1692
01:22:07,520 --> 01:22:14,480
and then type message
1693
01:22:11,360 --> 01:22:16,480
and sender.
1694
01:22:14,480 --> 01:22:18,719
This takes the message and sender
1695
01:22:16,480 --> 01:22:20,719
properties directly out of the first
1696
01:22:18,719 --> 01:22:23,040
parameter and then saves them in a
1697
01:22:20,719 --> 01:22:25,600
variable called message and a variable
1698
01:22:23,040 --> 01:22:28,719
called sender. So it does the same thing
1699
01:22:25,600 --> 01:22:31,600
as this code down here, but it's an even
1700
01:22:28,719 --> 01:22:34,560
shorter shortcut. We're dstructuring the
1701
01:22:31,600 --> 01:22:36,639
parameter directly at the top. So this
1702
01:22:34,560 --> 01:22:39,280
shortcut is used a lot in React
1703
01:22:36,639 --> 01:22:43,440
tutorials and documentation. So it's
1704
01:22:39,280 --> 01:22:46,239
useful to know how this works.
1705
01:22:43,440 --> 01:22:49,199
So let's comment out this line here at
1706
01:22:46,239 --> 01:22:52,000
the front. We'll type double slash.
1707
01:22:49,199 --> 01:22:55,199
So we remember that this code is a
1708
01:22:52,000 --> 01:22:57,679
shortcut for this line of code and these
1709
01:22:55,199 --> 01:23:01,520
two lines of code.
1710
01:22:57,679 --> 01:23:02,719
Now, let's save and check our website to
1711
01:23:01,520 --> 01:23:06,400
make sure that everything is still
1712
01:23:02,719 --> 01:23:08,719
working. So, that looks good.
1713
01:23:06,400 --> 01:23:11,199
We'll go back. And that is a
1714
01:23:08,719 --> 01:23:13,360
dstructuring shortcut. It allows us to
1715
01:23:11,199 --> 01:23:16,000
take properties directly out of an
1716
01:23:13,360 --> 01:23:18,159
object.
1717
01:23:16,000 --> 01:23:21,920
Next, we're going to learn how to use if
1718
01:23:18,159 --> 01:23:25,600
statements directly inside JSX.
1719
01:23:21,920 --> 01:23:28,159
So currently we have some JSX here and
1720
01:23:25,600 --> 01:23:29,760
we put an if statement outside of this
1721
01:23:28,159 --> 01:23:31,920
JSX.
1722
01:23:29,760 --> 01:23:36,880
However, this means we're duplicating
1723
01:23:31,920 --> 01:23:39,679
some code like the div and the message.
1724
01:23:36,880 --> 01:23:42,880
To make this code more reusable, we can
1725
01:23:39,679 --> 01:23:46,800
actually move this if statement directly
1726
01:23:42,880 --> 01:23:50,480
inside the JSX down here. So what we
1727
01:23:46,800 --> 01:23:52,560
want to do is if the sender is a robot,
1728
01:23:50,480 --> 01:23:55,520
we want to put the image before the
1729
01:23:52,560 --> 01:23:58,800
message. So down here, let's go before
1730
01:23:55,520 --> 01:24:01,760
the message and press enter. And we're
1731
01:23:58,800 --> 01:24:03,920
going to type curly brackets and we're
1732
01:24:01,760 --> 01:24:08,080
going to insert the if statement
1733
01:24:03,920 --> 01:24:11,679
directly inside here. So here before the
1734
01:24:08,080 --> 01:24:16,800
message we want to do something like if
1735
01:24:11,679 --> 01:24:20,800
the sender is equal to the string robot
1736
01:24:16,800 --> 01:24:23,360
we're going to insert this image.
1737
01:24:20,800 --> 01:24:26,320
However we can't actually use the word
1738
01:24:23,360 --> 01:24:28,719
if inside of these curly brackets.
1739
01:24:26,320 --> 01:24:32,400
Instead we need to use another feature
1740
01:24:28,719 --> 01:24:34,080
of JavaScript called a guard operator or
1741
01:24:32,400 --> 01:24:37,280
and.
1742
01:24:34,080 --> 01:24:40,960
In JavaScript, you can write value one
1743
01:24:37,280 --> 01:24:43,920
and value two. If value one is true,
1744
01:24:40,960 --> 01:24:46,400
then the result will be value two. So
1745
01:24:43,920 --> 01:24:48,800
this works just like an if statement
1746
01:24:46,400 --> 01:24:51,840
except we can write it on one line and
1747
01:24:48,800 --> 01:24:53,679
it results in a value. So if you want to
1748
01:24:51,840 --> 01:24:55,760
learn more about this feature, I cover
1749
01:24:53,679 --> 01:24:58,000
it in the booleans lesson of my
1750
01:24:55,760 --> 01:25:00,800
JavaScript course and you can check out
1751
01:24:58,000 --> 01:25:03,120
that in the video description.
1752
01:25:00,800 --> 01:25:06,159
For now, the only thing we need to know
1753
01:25:03,120 --> 01:25:08,960
is we can use and as sort of an if
1754
01:25:06,159 --> 01:25:12,080
statement in our JSX.
1755
01:25:08,960 --> 01:25:14,080
So, first let's remove this
1756
01:25:12,080 --> 01:25:17,040
and then we're going to use and to
1757
01:25:14,080 --> 01:25:19,120
insert this if statement directly inside
1758
01:25:17,040 --> 01:25:21,360
the JSX.
1759
01:25:19,120 --> 01:25:25,120
So, the first value is going to be our
1760
01:25:21,360 --> 01:25:29,520
if condition, which is sender equals the
1761
01:25:25,120 --> 01:25:33,840
robot. So we'll check if the sender is
1762
01:25:29,520 --> 01:25:36,880
equal to the string robot
1763
01:25:33,840 --> 01:25:39,920
and then we're going to use and.
1764
01:25:36,880 --> 01:25:43,760
So if this is true, the result is going
1765
01:25:39,920 --> 01:25:46,639
to be the second value here. So here if
1766
01:25:43,760 --> 01:25:49,679
the sender is a robot, we want to insert
1767
01:25:46,639 --> 01:25:52,000
the robot image. So let's just copy and
1768
01:25:49,679 --> 01:25:53,840
paste this code. We're going to select
1769
01:25:52,000 --> 01:25:56,320
this
1770
01:25:53,840 --> 01:25:58,960
and then copy it.
1771
01:25:56,320 --> 01:26:01,760
And then down here after and we're going
1772
01:25:58,960 --> 01:26:04,800
to paste.
1773
01:26:01,760 --> 01:26:07,360
So again the way this works is if the
1774
01:26:04,800 --> 01:26:10,159
value on the left is true then the
1775
01:26:07,360 --> 01:26:13,360
result will be the value on the right.
1776
01:26:10,159 --> 01:26:16,239
So this and is sort of a shortcut for
1777
01:26:13,360 --> 01:26:20,400
this if statement and it lets us insert
1778
01:26:16,239 --> 01:26:22,560
if statements directly inside our JSX.
1779
01:26:20,400 --> 01:26:25,199
So let's actually comment out this if
1780
01:26:22,560 --> 01:26:29,440
statement out here. So we remember that
1781
01:26:25,199 --> 01:26:32,000
this code is a shortcut for this code.
1782
01:26:29,440 --> 01:26:35,760
So here before the if statement we'll
1783
01:26:32,000 --> 01:26:39,600
type slash and star and after the if
1784
01:26:35,760 --> 01:26:42,320
statement we'll type star and slash and
1785
01:26:39,600 --> 01:26:45,600
remove the indent. So this is known as a
1786
01:26:42,320 --> 01:26:48,320
multi-line comment. All the code between
1787
01:26:45,600 --> 01:26:52,159
these two stars will be ignored by the
1788
01:26:48,320 --> 01:26:55,840
computer. So now let's save
1789
01:26:52,159 --> 01:26:58,719
and check our website.
1790
01:26:55,840 --> 01:27:01,280
And as you can see, if the sender is a
1791
01:26:58,719 --> 01:27:04,719
robot, we're going to add an image on
1792
01:27:01,280 --> 01:27:07,360
the left before the message. So now, one
1793
01:27:04,719 --> 01:27:10,000
problem here is that we're also showing
1794
01:27:07,360 --> 01:27:12,000
the user's profile image at the back,
1795
01:27:10,000 --> 01:27:16,320
but we only want to show one profile
1796
01:27:12,000 --> 01:27:19,600
image. We'll go back to our code
1797
01:27:16,320 --> 01:27:23,280
and here we only want to show the user
1798
01:27:19,600 --> 01:27:25,679
profile image if the sender is the user.
1799
01:27:23,280 --> 01:27:28,719
So let's insert another if statement
1800
01:27:25,679 --> 01:27:32,080
down here. Feel free to pause this video
1801
01:27:28,719 --> 01:27:34,400
if you want to try it yourself first.
1802
01:27:32,080 --> 01:27:37,280
So at the front we'll type open curly
1803
01:27:34,400 --> 01:27:38,880
bracket. At the end a close curly
1804
01:27:37,280 --> 01:27:41,840
bracket.
1805
01:27:38,880 --> 01:27:45,280
And at the front we're going to type the
1806
01:27:41,840 --> 01:27:50,159
if condition. So we'll check if the
1807
01:27:45,280 --> 01:27:54,800
sender is equal to the string user and
1808
01:27:50,159 --> 01:27:57,360
then we'll type ant. So if this is true
1809
01:27:54,800 --> 01:28:00,000
then we will show this image. If this is
1810
01:27:57,360 --> 01:28:02,639
not true, we will not show this image.
1811
01:28:00,000 --> 01:28:05,639
So this code is a shortcut for this
1812
01:28:02,639 --> 01:28:05,639
code.
1813
01:28:05,920 --> 01:28:13,280
Let's save and check our website.
1814
01:28:10,480 --> 01:28:18,719
And it works. The robot image is on the
1815
01:28:13,280 --> 01:28:20,960
left and the user image is on the right.
1816
01:28:18,719 --> 01:28:24,159
So let's go back.
1817
01:28:20,960 --> 01:28:26,639
Using the guard operator or and we can
1818
01:28:24,159 --> 01:28:29,760
insert an if statement directly inside
1819
01:28:26,639 --> 01:28:31,600
our JSX.
1820
01:28:29,760 --> 01:28:34,960
Next, we're going to do some code
1821
01:28:31,600 --> 01:28:37,120
cleanup. So, let's scroll up and we're
1822
01:28:34,960 --> 01:28:40,639
going to go to the chat input component
1823
01:28:37,120 --> 01:28:43,840
first. So, you notice that in here, this
1824
01:28:40,639 --> 01:28:46,400
line is a little bit too long. To make
1825
01:28:43,840 --> 01:28:49,760
our code easier to read, we can actually
1826
01:28:46,400 --> 01:28:52,639
put each attribute on its own line. So,
1827
01:28:49,760 --> 01:28:55,199
here we'll type enter to put placeholder
1828
01:28:52,639 --> 01:28:58,239
on its own line. And here we'll type
1829
01:28:55,199 --> 01:29:01,120
enter to put size on its own line. And
1830
01:28:58,239 --> 01:29:04,000
let's also type enter at the back. So
1831
01:29:01,120 --> 01:29:06,000
the attributes are on their own lines.
1832
01:29:04,000 --> 01:29:08,320
And then let's add an indent in front of
1833
01:29:06,000 --> 01:29:11,199
these attributes by pressing tab and
1834
01:29:08,320 --> 01:29:13,600
pressing tab. This makes it clear that
1835
01:29:11,199 --> 01:29:16,000
these attributes are part of this
1836
01:29:13,600 --> 01:29:19,120
element. And we also want to make sure
1837
01:29:16,000 --> 01:29:21,040
that these two are vertically aligned.
1838
01:29:19,120 --> 01:29:24,880
This makes it easy to see where this
1839
01:29:21,040 --> 01:29:27,520
element starts and where it ends.
1840
01:29:24,880 --> 01:29:31,360
Next, let's scroll down to the chat
1841
01:29:27,520 --> 01:29:34,080
message component. And notice that these
1842
01:29:31,360 --> 01:29:37,280
two lines of code with the and are also
1843
01:29:34,080 --> 01:29:40,080
a bit too long. To improve this, we can
1844
01:29:37,280 --> 01:29:43,199
surround this value with brackets. So
1845
01:29:40,080 --> 01:29:46,080
we'll type open bracket and at the end
1846
01:29:43,199 --> 01:29:49,280
close bracket and then we can put this
1847
01:29:46,080 --> 01:29:53,360
code on its own line. So here we'll type
1848
01:29:49,280 --> 01:29:55,600
enter and at the end we'll type enter.
1849
01:29:53,360 --> 01:29:57,600
So this will still have the same value,
1850
01:29:55,600 --> 01:30:00,400
but the brackets allow us to put the
1851
01:29:57,600 --> 01:30:03,120
code on multiple lines. We'll do the
1852
01:30:00,400 --> 01:30:05,360
same thing for the second image.
1853
01:30:03,120 --> 01:30:08,239
We'll surround it in brackets, open
1854
01:30:05,360 --> 01:30:11,440
bracket, and close bracket. And we'll
1855
01:30:08,239 --> 01:30:15,360
put this value on a separate line
1856
01:30:11,440 --> 01:30:18,560
so that it's no longer too long.
1857
01:30:15,360 --> 01:30:21,520
Next, let's scroll down even more to the
1858
01:30:18,560 --> 01:30:24,080
app variable. And you'll notice that
1859
01:30:21,520 --> 01:30:26,960
these lines are also a bit too long
1860
01:30:24,080 --> 01:30:30,000
because of the attributes. Again, we can
1861
01:30:26,960 --> 01:30:32,239
put each attribute on its own line. So
1862
01:30:30,000 --> 01:30:35,199
here we'll put message on its own line
1863
01:30:32,239 --> 01:30:37,600
and sender on its own line. And we'll
1864
01:30:35,199 --> 01:30:40,000
put the end of the component on its own
1865
01:30:37,600 --> 01:30:42,960
line so that this is vertically aligned
1866
01:30:40,000 --> 01:30:44,719
with this. And let's also add indents
1867
01:30:42,960 --> 01:30:48,320
before the attributes. So we'll press
1868
01:30:44,719 --> 01:30:50,480
tab and tab. So it's easy to see that
1869
01:30:48,320 --> 01:30:52,880
these attributes are part of this
1870
01:30:50,480 --> 01:30:55,600
component. And we'll do the same thing
1871
01:30:52,880 --> 01:30:58,560
for the other three chat messages. So
1872
01:30:55,600 --> 01:31:02,000
we'll put this on its own line
1873
01:30:58,560 --> 01:31:05,840
and add indents.
1874
01:31:02,000 --> 01:31:08,560
And put this on its own line.
1875
01:31:05,840 --> 01:31:12,719
Add indents.
1876
01:31:08,560 --> 01:31:15,920
And put this on its own line.
1877
01:31:12,719 --> 01:31:19,120
And add indents.
1878
01:31:15,920 --> 01:31:21,600
and save
1879
01:31:19,120 --> 01:31:23,199
and check our website and everything
1880
01:31:21,600 --> 01:31:26,080
should still work because we just
1881
01:31:23,199 --> 01:31:28,960
changed the formatting of our code. So
1882
01:31:26,080 --> 01:31:31,440
let's go back and that's how we avoid
1883
01:31:28,960 --> 01:31:34,560
our lines being too long. We can put
1884
01:31:31,440 --> 01:31:36,560
each attribute on its own line
1885
01:31:34,560 --> 01:31:41,040
and we can also surround a value with
1886
01:31:36,560 --> 01:31:44,880
brackets and put it on its own line.
1887
01:31:41,040 --> 01:31:47,600
Finally, down here, instead of using a
1888
01:31:44,880 --> 01:31:50,400
variable to save our app, a best
1889
01:31:47,600 --> 01:31:52,560
practice is to use a component to create
1890
01:31:50,400 --> 01:31:56,400
this app.
1891
01:31:52,560 --> 01:31:58,320
So here, let's create a component using
1892
01:31:56,400 --> 01:32:02,400
function
1893
01:31:58,320 --> 01:32:05,760
and we'll name it capital A app
1894
01:32:02,400 --> 01:32:09,120
brackets and curly brackets.
1895
01:32:05,760 --> 01:32:11,120
And then this component will return all
1896
01:32:09,120 --> 01:32:14,880
of this HTML.
1897
01:32:11,120 --> 01:32:17,840
So we'll return brackets so we can write
1898
01:32:14,880 --> 01:32:19,920
it on multiple lines.
1899
01:32:17,840 --> 01:32:23,760
And we're going to move all this code up
1900
01:32:19,920 --> 01:32:26,239
here. So let's select this code. And
1901
01:32:23,760 --> 01:32:28,960
we'll cut
1902
01:32:26,239 --> 01:32:32,560
and inside the return
1903
01:32:28,960 --> 01:32:36,560
we're going to paste.
1904
01:32:32,560 --> 01:32:40,000
And we can select these lines
1905
01:32:36,560 --> 01:32:42,560
and press tab to fix the indenting.
1906
01:32:40,000 --> 01:32:44,719
So now we're going to use this component
1907
01:32:42,560 --> 01:32:48,400
instead of the variable to create our
1908
01:32:44,719 --> 01:32:50,639
app. So down here, let's remove this
1909
01:32:48,400 --> 01:32:53,440
variable.
1910
01:32:50,639 --> 01:32:57,440
And then inside render, we're going to
1911
01:32:53,440 --> 01:33:01,360
create the app. We can type less than
1912
01:32:57,440 --> 01:33:05,280
capital A app greater than and then less
1913
01:33:01,360 --> 01:33:07,840
than slash app greater than.
1914
01:33:05,280 --> 01:33:10,320
So this creates an entire chatbot
1915
01:33:07,840 --> 01:33:13,760
application. And then we give this to
1916
01:33:10,320 --> 01:33:16,159
React to display on the website.
1917
01:33:13,760 --> 01:33:18,239
And again since there's nothing inside
1918
01:33:16,159 --> 01:33:21,600
this component, we can use the
1919
01:33:18,239 --> 01:33:24,719
self-closing element shortcut. So we can
1920
01:33:21,600 --> 01:33:29,040
remove this closing tag. And then at the
1921
01:33:24,719 --> 01:33:31,679
end here we can type space and slash.
1922
01:33:29,040 --> 01:33:34,239
And now let's save
1923
01:33:31,679 --> 01:33:38,239
and check our website.
1924
01:33:34,239 --> 01:33:41,679
And it displays the app on the website.
1925
01:33:38,239 --> 01:33:44,719
Let's go back to our code.
1926
01:33:41,679 --> 01:33:47,920
So a best practice is to use a component
1927
01:33:44,719 --> 01:33:50,480
called app to create the website because
1928
01:33:47,920 --> 01:33:53,280
it gives us access to extra features of
1929
01:33:50,480 --> 01:33:55,679
components like props.
1930
01:33:53,280 --> 01:33:59,199
And also notice that we can have
1931
01:33:55,679 --> 01:34:01,679
components inside a component. So this
1932
01:33:59,199 --> 01:34:04,080
lets us split up a website into smaller
1933
01:34:01,679 --> 01:34:06,239
and smaller components or smaller and
1934
01:34:04,080 --> 01:34:09,440
smaller pieces.
1935
01:34:06,239 --> 01:34:12,080
So again, React helps us create websites
1936
01:34:09,440 --> 01:34:17,239
easier because it lets us work on a
1937
01:34:12,080 --> 01:34:17,239
small piece of the website at a time.
1938
01:34:17,520 --> 01:34:22,480
In the next lesson, we're going to learn
1939
01:34:19,600 --> 01:34:25,920
a very important feature of React called
1940
01:34:22,480 --> 01:34:28,719
state. State lets us make our website
1941
01:34:25,920 --> 01:34:32,400
interactive, and it will let us add new
1942
01:34:28,719 --> 01:34:34,800
chat messages to our website.
1943
01:34:32,400 --> 01:34:37,440
And that's the end of this lesson. In
1944
01:34:34,800 --> 01:34:40,719
this lesson, we learned that a component
1945
01:34:37,440 --> 01:34:43,199
is just a piece of the website. We also
1946
01:34:40,719 --> 01:34:47,280
learned that components let us create
1947
01:34:43,199 --> 01:34:50,719
our own HTML elements. We started the
1948
01:34:47,280 --> 01:34:53,600
chatbot project. We split the chatbot
1949
01:34:50,719 --> 01:34:56,880
into chat input and chat message
1950
01:34:53,600 --> 01:34:59,679
components. We learned props which let
1951
01:34:56,880 --> 01:35:02,320
us insert values into components and
1952
01:34:59,679 --> 01:35:04,880
make components reusable.
1953
01:35:02,320 --> 01:35:07,920
We learned the destructuring and guard
1954
01:35:04,880 --> 01:35:10,800
operator shortcuts. We did some code
1955
01:35:07,920 --> 01:35:14,239
cleanup and split longer lines into
1956
01:35:10,800 --> 01:35:17,120
multiple lines. And we created an app
1957
01:35:14,239 --> 01:35:19,920
component which represents the entire
1958
01:35:17,120 --> 01:35:22,080
application.
1959
01:35:19,920 --> 01:35:23,840
Thanks for watching so far. If you find
1960
01:35:22,080 --> 01:35:26,080
this course valuable, you can support
1961
01:35:23,840 --> 01:35:28,960
the channel by liking this video,
1962
01:35:26,080 --> 01:35:30,719
leaving a comment, and subscribing.
1963
01:35:28,960 --> 01:35:32,719
You can also check out the premium
1964
01:35:30,719 --> 01:35:35,280
version of this course where you'll get
1965
01:35:32,719 --> 01:35:38,080
all the lessons ads free broken down
1966
01:35:35,280 --> 01:35:40,159
into small videos as well as additional
1967
01:35:38,080 --> 01:35:42,960
lessons and more projects that are not
1968
01:35:40,159 --> 01:35:45,440
in the public React course. Currently,
1969
01:35:42,960 --> 01:35:47,440
the premium course contains 11 lessons
1970
01:35:45,440 --> 01:35:50,159
and I'll be adding more lessons in the
1971
01:35:47,440 --> 01:35:52,719
future. You can also get a certificate
1972
01:35:50,159 --> 01:35:55,120
of completion, create comments or
1973
01:35:52,719 --> 01:35:57,440
questions on specific topics, get
1974
01:35:55,120 --> 01:35:59,920
progress tracking, search and course
1975
01:35:57,440 --> 01:36:02,080
notes. You can also check out the
1976
01:35:59,920 --> 01:36:05,520
membership where you can get this course
1977
01:36:02,080 --> 01:36:08,400
ads free separated by each lesson, AI
1978
01:36:05,520 --> 01:36:10,320
videos, backend tutorials, and more.
1979
01:36:08,400 --> 01:36:13,440
Thanks again, and let's continue with
1980
01:36:10,320 --> 01:36:15,440
the course. Here are some exercises to
1981
01:36:13,440 --> 01:36:18,679
help you practice what we learned in
1982
01:36:15,440 --> 01:36:18,679
this lesson.
1983
01:36:54,320 --> 01:36:59,600
In this lesson, we're going to learn two
1984
01:36:56,560 --> 01:37:02,960
of the most important features of React
1985
01:36:59,600 --> 01:37:04,880
called state and event handlers. And
1986
01:37:02,960 --> 01:37:07,280
we're going to use these features to
1987
01:37:04,880 --> 01:37:09,199
create the functionality for the chatbot
1988
01:37:07,280 --> 01:37:11,040
project.
1989
01:37:09,199 --> 01:37:14,320
Before we learn state, we're going to
1990
01:37:11,040 --> 01:37:17,119
improve our code a bit. Currently to add
1991
01:37:14,320 --> 01:37:20,320
a new chat message we have to manually
1992
01:37:17,119 --> 01:37:22,880
type a new chat message component.
1993
01:37:20,320 --> 01:37:25,440
Instead of manually typing this code we
1994
01:37:22,880 --> 01:37:27,280
can actually use JavaScript to generate
1995
01:37:25,440 --> 01:37:30,320
these components.
1996
01:37:27,280 --> 01:37:34,480
To do that we're going to do two steps.
1997
01:37:30,320 --> 01:37:36,639
The first step is to save the data. Data
1998
01:37:34,480 --> 01:37:39,920
means information.
1999
01:37:36,639 --> 01:37:42,880
Here the data is the information about
2000
01:37:39,920 --> 01:37:46,239
our chat messages like the message and
2001
01:37:42,880 --> 01:37:50,159
the sender. So we need to save this data
2002
01:37:46,239 --> 01:37:53,679
or this information in our JavaScript.
2003
01:37:50,159 --> 01:37:56,320
Let's use a variable to save this data.
2004
01:37:53,679 --> 01:37:59,520
At the top of this component, we're
2005
01:37:56,320 --> 01:38:03,119
going to create a variable using const
2006
01:37:59,520 --> 01:38:06,400
and we'll name it chat messages
2007
01:38:03,119 --> 01:38:09,280
equals and we'll save it as an array by
2008
01:38:06,400 --> 01:38:12,320
typing square brackets.
2009
01:38:09,280 --> 01:38:15,840
As a review, an array is a list of
2010
01:38:12,320 --> 01:38:18,560
values. So here we want to save a list
2011
01:38:15,840 --> 01:38:21,040
of chat messages. So it makes sense to
2012
01:38:18,560 --> 01:38:23,920
save it as an array.
2013
01:38:21,040 --> 01:38:27,199
Next, each chat message has multiple
2014
01:38:23,920 --> 01:38:30,639
pieces of data, like the message and the
2015
01:38:27,199 --> 01:38:33,040
sender. So, inside this array, we're
2016
01:38:30,639 --> 01:38:37,040
going to save each chat message as an
2017
01:38:33,040 --> 01:38:40,159
object by typing curly brackets.
2018
01:38:37,040 --> 01:38:42,800
As a review, an object groups related
2019
01:38:40,159 --> 01:38:45,520
values together. So, it's going to help
2020
01:38:42,800 --> 01:38:47,280
us group the message and the sender
2021
01:38:45,520 --> 01:38:49,760
together.
2022
01:38:47,280 --> 01:38:53,280
And then inside this object, we're going
2023
01:38:49,760 --> 01:38:56,400
to create a property called message
2024
01:38:53,280 --> 01:38:59,679
colon and a string. And we're going to
2025
01:38:56,400 --> 01:39:02,639
use this to save the message. So let's
2026
01:38:59,679 --> 01:39:05,679
copy the first message. We'll select it
2027
01:39:02,639 --> 01:39:07,679
and copy. And we're going to paste it in
2028
01:39:05,679 --> 01:39:10,480
here.
2029
01:39:07,679 --> 01:39:14,480
And then we'll add a comma. And we're
2030
01:39:10,480 --> 01:39:16,880
going to add a sender property colon and
2031
01:39:14,480 --> 01:39:21,040
a string. and we're going to save the
2032
01:39:16,880 --> 01:39:23,360
first sender which is the user.
2033
01:39:21,040 --> 01:39:27,040
So that's how we save the data for the
2034
01:39:23,360 --> 01:39:29,360
first chat message in our JavaScript.
2035
01:39:27,040 --> 01:39:31,280
Now let's save the data for the second
2036
01:39:29,360 --> 01:39:33,920
chat message.
2037
01:39:31,280 --> 01:39:38,400
So after the first object, we're going
2038
01:39:33,920 --> 01:39:40,880
to type comma and create another object.
2039
01:39:38,400 --> 01:39:44,960
I will give this one a message property
2040
01:39:40,880 --> 01:39:47,360
as well colon a string. and we'll save
2041
01:39:44,960 --> 01:39:51,360
the second message.
2042
01:39:47,360 --> 01:39:54,480
We'll select this copy and then inside
2043
01:39:51,360 --> 01:39:58,560
here we're going to paste.
2044
01:39:54,480 --> 01:40:01,600
Add a comma and a sender property colon
2045
01:39:58,560 --> 01:40:04,639
and a string. And the second chat
2046
01:40:01,600 --> 01:40:08,159
message is from the robot. So inside the
2047
01:40:04,639 --> 01:40:10,000
sender, we'll save robot.
2048
01:40:08,159 --> 01:40:12,480
Let's do the same thing for the third
2049
01:40:10,000 --> 01:40:14,960
and fourth chat messages. we'll save
2050
01:40:12,480 --> 01:40:16,800
them in our JavaScript. Feel free to
2051
01:40:14,960 --> 01:40:18,480
pause the video if you want to try it
2052
01:40:16,800 --> 01:40:20,639
yourself.
2053
01:40:18,480 --> 01:40:22,880
So, for the third chat message, we're
2054
01:40:20,639 --> 01:40:27,119
going to add a comma at the end and
2055
01:40:22,880 --> 01:40:29,520
another object, a message property, a
2056
01:40:27,119 --> 01:40:31,360
string,
2057
01:40:29,520 --> 01:40:33,199
and we're going to copy the third
2058
01:40:31,360 --> 01:40:37,239
message,
2059
01:40:33,199 --> 01:40:37,239
and paste it in here.
2060
01:40:37,360 --> 01:40:43,679
and a sender property. The third message
2061
01:40:40,639 --> 01:40:46,159
is from the user.
2062
01:40:43,679 --> 01:40:49,679
And then we're going to type comma and
2063
01:40:46,159 --> 01:40:52,880
another object the message property
2064
01:40:49,679 --> 01:40:56,639
colon and a string. And we'll copy paste
2065
01:40:52,880 --> 01:41:00,080
the fourth message. So we'll select it,
2066
01:40:56,639 --> 01:41:02,719
copy, and then inside here we're going
2067
01:41:00,080 --> 01:41:05,840
to paste
2068
01:41:02,719 --> 01:41:08,080
and a sender property.
2069
01:41:05,840 --> 01:41:09,920
And the fourth message is from the
2070
01:41:08,080 --> 01:41:12,800
robot.
2071
01:41:09,920 --> 01:41:17,239
So that's how we save the data or the
2072
01:41:12,800 --> 01:41:17,239
information for the chat messages.
2073
01:41:17,440 --> 01:41:23,760
After we save the data, the second step
2074
01:41:20,320 --> 01:41:27,119
is to generate the HTML. So we're going
2075
01:41:23,760 --> 01:41:29,600
to use JavaScript to convert this data
2076
01:41:27,119 --> 01:41:32,960
into these components. So we don't have
2077
01:41:29,600 --> 01:41:36,639
to type these components manually.
2078
01:41:32,960 --> 01:41:38,480
To do that, after the data,
2079
01:41:36,639 --> 01:41:43,040
we're going to type the name of the
2080
01:41:38,480 --> 01:41:46,239
data, which is chat messages.
2081
01:41:43,040 --> 01:41:51,520
And this is an array. And arrays have a
2082
01:41:46,239 --> 01:41:55,119
feature called dot map and brackets.
2083
01:41:51,520 --> 01:41:58,320
As a review, do map lets us go through
2084
01:41:55,119 --> 01:42:01,840
each value of an array and convert it
2085
01:41:58,320 --> 01:42:04,400
into a new value.
2086
01:42:01,840 --> 01:42:07,440
To use dot map, we need to give it a
2087
01:42:04,400 --> 01:42:10,159
function. So between these brackets,
2088
01:42:07,440 --> 01:42:14,960
we're going to type a function using
2089
01:42:10,159 --> 01:42:18,880
brackets, arrow, and curly brackets.
2090
01:42:14,960 --> 01:42:21,520
As a review, this is an arrow function.
2091
01:42:18,880 --> 01:42:25,040
It mostly works the same way as a
2092
01:42:21,520 --> 01:42:28,719
function, but it's shorter. When we give
2093
01:42:25,040 --> 01:42:31,040
a function to another function like map,
2094
01:42:28,719 --> 01:42:34,080
we usually use an arrow function because
2095
01:42:31,040 --> 01:42:35,920
it makes the code a bit cleaner.
2096
01:42:34,080 --> 01:42:39,199
This is what React tutorials and
2097
01:42:35,920 --> 01:42:42,800
documentations use.
2098
01:42:39,199 --> 01:42:45,440
Next, this function gets a parameter. So
2099
01:42:42,800 --> 01:42:49,679
at the top, we'll create a new parameter
2100
01:42:45,440 --> 01:42:53,440
and name it chat message.
2101
01:42:49,679 --> 01:42:58,159
So now dom takes each value in this
2102
01:42:53,440 --> 01:43:00,560
array and converts it into a new value.
2103
01:42:58,159 --> 01:43:03,119
The way it works is it starts with the
2104
01:43:00,560 --> 01:43:06,400
first value and then it saves it in this
2105
01:43:03,119 --> 01:43:08,639
parameter and runs this function and
2106
01:43:06,400 --> 01:43:11,920
whatever we return from this function
2107
01:43:08,639 --> 01:43:14,719
will be the new first value. Then it
2108
01:43:11,920 --> 01:43:17,440
takes the second value. It saves it in
2109
01:43:14,719 --> 01:43:19,679
this parameter and runs the function.
2110
01:43:17,440 --> 01:43:22,800
And whatever we return from the function
2111
01:43:19,679 --> 01:43:26,080
will be the new second value. Then it
2112
01:43:22,800 --> 01:43:28,719
takes the third value, saves it, runs a
2113
01:43:26,080 --> 01:43:33,440
function and whatever we return will be
2114
01:43:28,719 --> 01:43:35,520
the new third value and so on. So map
2115
01:43:33,440 --> 01:43:38,400
basically goes through each value in
2116
01:43:35,520 --> 01:43:42,560
this array, runs a function and converts
2117
01:43:38,400 --> 01:43:42,560
it into a new value.
2118
01:43:44,560 --> 01:43:50,320
So here we want to convert each chat
2119
01:43:47,520 --> 01:43:53,119
message object into a chat message
2120
01:43:50,320 --> 01:43:56,800
component. So inside this inner
2121
01:43:53,119 --> 01:44:00,880
function, we're going to return a chat
2122
01:43:56,800 --> 01:44:04,480
message component by typing brackets
2123
01:44:00,880 --> 01:44:06,719
and then here less than chat
2124
01:44:04,480 --> 01:44:11,520
message/grater
2125
01:44:06,719 --> 01:44:14,639
than to return a chat message component.
2126
01:44:11,520 --> 01:44:18,400
Next, each chat message component has
2127
01:44:14,639 --> 01:44:20,000
two props, message and sender. So we're
2128
01:44:18,400 --> 01:44:21,440
going to add those props to this
2129
01:44:20,000 --> 01:44:23,360
component.
2130
01:44:21,440 --> 01:44:26,719
So here
2131
01:44:23,360 --> 01:44:28,239
we'll create a prop message
2132
01:44:26,719 --> 01:44:31,920
equals
2133
01:44:28,239 --> 01:44:35,520
and now what do we save in this prop?
2134
01:44:31,920 --> 01:44:38,080
Remember that map takes each object in
2135
01:44:35,520 --> 01:44:41,520
this array and saves it in this
2136
01:44:38,080 --> 01:44:44,880
parameter. So this parameter contains a
2137
01:44:41,520 --> 01:44:47,360
chat message object and each chat
2138
01:44:44,880 --> 01:44:50,560
message object has a message and a
2139
01:44:47,360 --> 01:44:52,960
sender. So we want to save the message
2140
01:44:50,560 --> 01:44:55,920
inside this prop.
2141
01:44:52,960 --> 01:44:59,440
To do that we're going to type curly
2142
01:44:55,920 --> 01:45:04,159
brackets and inside we'll type this
2143
01:44:59,440 --> 01:45:08,800
object chat message and get the message
2144
01:45:04,159 --> 01:45:11,679
property which is dot message.
2145
01:45:08,800 --> 01:45:15,440
This gets the message out of this object
2146
01:45:11,679 --> 01:45:18,159
and saves it in the prop.
2147
01:45:15,440 --> 01:45:21,199
So why do we use curly brackets here
2148
01:45:18,159 --> 01:45:24,159
instead of double quotes? If we use
2149
01:45:21,199 --> 01:45:26,480
double quotes, it will save a string
2150
01:45:24,159 --> 01:45:30,239
into this prop, which is not what we
2151
01:45:26,480 --> 01:45:33,040
want. But if we use curly brackets, it
2152
01:45:30,239 --> 01:45:36,159
will save the result of this code into
2153
01:45:33,040 --> 01:45:38,880
this prop.
2154
01:45:36,159 --> 01:45:42,159
So for the first chat message, the
2155
01:45:38,880 --> 01:45:45,040
result of chat message dossage is going
2156
01:45:42,159 --> 01:45:47,679
to be the string hello chatbot. So it's
2157
01:45:45,040 --> 01:45:51,119
going to take this and save it into the
2158
01:45:47,679 --> 01:45:54,239
message prop here. For the second chat
2159
01:45:51,119 --> 01:45:56,560
message, the result of this code is
2160
01:45:54,239 --> 01:45:59,920
hello, how can I help you? And it will
2161
01:45:56,560 --> 01:46:03,520
save this into the message prop and so
2162
01:45:59,920 --> 01:46:06,560
on. So using curly brackets we can put
2163
01:46:03,520 --> 01:46:09,199
any JavaScript code inside and it will
2164
01:46:06,560 --> 01:46:12,480
save the result of this code into the
2165
01:46:09,199 --> 01:46:15,520
prop not just a string.
2166
01:46:12,480 --> 01:46:19,440
So next each chat message component has
2167
01:46:15,520 --> 01:46:22,239
a sender prop. So here we're also going
2168
01:46:19,440 --> 01:46:25,600
to add a sender
2169
01:46:22,239 --> 01:46:28,159
equals and again we want to save the
2170
01:46:25,600 --> 01:46:32,080
sender property of this chat message
2171
01:46:28,159 --> 01:46:34,159
object. So we'll type curly brackets and
2172
01:46:32,080 --> 01:46:38,960
the object
2173
01:46:34,159 --> 01:46:41,440
chat message dot sender.
2174
01:46:38,960 --> 01:46:44,480
So this will get the sender property out
2175
01:46:41,440 --> 01:46:48,639
of each chat message object and save it
2176
01:46:44,480 --> 01:46:51,520
in here. And that's it. Now map will
2177
01:46:48,639 --> 01:46:53,520
take each value of this array and
2178
01:46:51,520 --> 01:46:55,360
convert it into a chat message
2179
01:46:53,520 --> 01:47:00,080
component.
2180
01:46:55,360 --> 01:47:00,080
And here's what the result looks like.
2181
01:47:02,719 --> 01:47:08,800
Next, we're going to save the result of
2182
01:47:04,960 --> 01:47:11,600
this code in a variable. So, up here,
2183
01:47:08,800 --> 01:47:17,600
let's type const and we'll name the
2184
01:47:11,600 --> 01:47:19,920
variable chat message components equals
2185
01:47:17,600 --> 01:47:23,199
this code.
2186
01:47:19,920 --> 01:47:26,320
And now we have an array of components
2187
01:47:23,199 --> 01:47:28,639
or a list of components.
2188
01:47:26,320 --> 01:47:31,679
Finally, we're going to insert this
2189
01:47:28,639 --> 01:47:34,480
array into the JSX.
2190
01:47:31,679 --> 01:47:36,719
So, down here, we can actually remove
2191
01:47:34,480 --> 01:47:40,400
all of these chat message components
2192
01:47:36,719 --> 01:47:42,880
because we're generating them now.
2193
01:47:40,400 --> 01:47:46,159
And we're going to type curly brackets,
2194
01:47:42,880 --> 01:47:50,639
and we're going to insert this array.
2195
01:47:46,159 --> 01:47:52,960
In here, we'll type chat message
2196
01:47:50,639 --> 01:47:56,159
components.
2197
01:47:52,960 --> 01:47:58,320
If we insert an array of components,
2198
01:47:56,159 --> 01:48:03,360
React will just display all of these
2199
01:47:58,320 --> 01:48:06,000
components on the page. If we save
2200
01:48:03,360 --> 01:48:08,239
and then go to our project,
2201
01:48:06,000 --> 01:48:10,400
it displays all the chat message
2202
01:48:08,239 --> 01:48:11,920
components that we generated with our
2203
01:48:10,400 --> 01:48:14,880
JavaScript.
2204
01:48:11,920 --> 01:48:17,280
So now we're generating this HTML with
2205
01:48:14,880 --> 01:48:20,800
JavaScript and we no longer have to type
2206
01:48:17,280 --> 01:48:22,159
out each component manually.
2207
01:48:20,800 --> 01:48:24,719
Now, there's one more thing that's
2208
01:48:22,159 --> 01:48:27,040
useful to know. Let's go back to our
2209
01:48:24,719 --> 01:48:29,280
code.
2210
01:48:27,040 --> 01:48:32,400
And there's actually a shortcut that we
2211
01:48:29,280 --> 01:48:35,199
can use here. Instead of saving this
2212
01:48:32,400 --> 01:48:37,920
result in a variable, we can actually
2213
01:48:35,199 --> 01:48:39,679
just insert this result directly in the
2214
01:48:37,920 --> 01:48:42,400
JSX.
2215
01:48:39,679 --> 01:48:45,119
So, we'll select this code
2216
01:48:42,400 --> 01:48:47,199
and then cut.
2217
01:48:45,119 --> 01:48:49,840
And between the curly brackets, we're
2218
01:48:47,199 --> 01:48:52,639
going to remove the variable and just
2219
01:48:49,840 --> 01:48:54,880
paste that code.
2220
01:48:52,639 --> 01:48:56,560
And we'll remove the semicolon at the
2221
01:48:54,880 --> 01:48:59,440
end.
2222
01:48:56,560 --> 01:49:02,560
And we'll select this code and press tab
2223
01:48:59,440 --> 01:49:05,199
a few times to fix the indenting.
2224
01:49:02,560 --> 01:49:08,440
And finally, we can remove this variable
2225
01:49:05,199 --> 01:49:08,440
up here.
2226
01:49:09,040 --> 01:49:14,960
Now we're just inserting the result of
2227
01:49:11,440 --> 01:49:17,440
this code directly in the JSX.
2228
01:49:14,960 --> 01:49:20,000
This shortcut is used a lot in React
2229
01:49:17,440 --> 01:49:23,280
tutorials and documentation. So it's
2230
01:49:20,000 --> 01:49:25,600
useful to know how it works.
2231
01:49:23,280 --> 01:49:28,560
Now let's save
2232
01:49:25,600 --> 01:49:30,880
and then check our website.
2233
01:49:28,560 --> 01:49:35,239
And everything still works. So that's
2234
01:49:30,880 --> 01:49:35,239
how we generate the HTML.
2235
01:49:35,440 --> 01:49:40,480
Before we move on, there's actually an
2236
01:49:37,600 --> 01:49:43,600
issue that we need to fix here. If we
2237
01:49:40,480 --> 01:49:46,480
look at our console and we scroll up,
2238
01:49:43,600 --> 01:49:50,400
we'll see an error saying each child in
2239
01:49:46,480 --> 01:49:53,280
a list should have a unique key prop. In
2240
01:49:50,400 --> 01:49:55,440
React, if we insert an array of
2241
01:49:53,280 --> 01:49:59,760
components, we need to give each
2242
01:49:55,440 --> 01:50:03,040
component a prop called key. The key
2243
01:49:59,760 --> 01:50:06,480
helps React track changes in the array.
2244
01:50:03,040 --> 01:50:10,159
So, we should give each component a key.
2245
01:50:06,480 --> 01:50:12,960
Let's go back to VS Code
2246
01:50:10,159 --> 01:50:17,760
and for each chat message, we're going
2247
01:50:12,960 --> 01:50:21,040
to add a prop called key equals.
2248
01:50:17,760 --> 01:50:23,920
And this key has to be unique. So, what
2249
01:50:21,040 --> 01:50:27,520
should we use as the key?
2250
01:50:23,920 --> 01:50:30,400
Usually when we have some data like this
2251
01:50:27,520 --> 01:50:33,360
in the real world, each piece of data
2252
01:50:30,400 --> 01:50:36,960
should have a unique ID that lets us
2253
01:50:33,360 --> 01:50:39,199
identify it. So let's add a unique ID to
2254
01:50:36,960 --> 01:50:41,920
each message to match what we would see
2255
01:50:39,199 --> 01:50:44,960
in the real world. So for the first
2256
01:50:41,920 --> 01:50:49,199
message after the sender, we'll add a
2257
01:50:44,960 --> 01:50:53,760
property called ID colon. And let's just
2258
01:50:49,199 --> 01:50:56,880
make it a string ID1 to keep it simple.
2259
01:50:53,760 --> 01:51:00,400
For the second message, we'll add an ID
2260
01:50:56,880 --> 01:51:04,880
with the string ID 2. For the third
2261
01:51:00,400 --> 01:51:07,920
message, we'll make the ID ID3. And for
2262
01:51:04,880 --> 01:51:12,080
the fourth message, we'll add an ID
2263
01:51:07,920 --> 01:51:16,159
string ID 4. So, we'll use these IDs for
2264
01:51:12,080 --> 01:51:19,280
practice. They just have to be unique.
2265
01:51:16,159 --> 01:51:22,880
And now we'll use this unique ID as the
2266
01:51:19,280 --> 01:51:26,159
key. So down here we'll set the key to
2267
01:51:22,880 --> 01:51:30,320
be equal to curly brackets and we'll get
2268
01:51:26,159 --> 01:51:33,600
the chat message do ID.
2269
01:51:30,320 --> 01:51:35,360
So chat message
2270
01:51:33,600 --> 01:51:38,480
id.
2271
01:51:35,360 --> 01:51:41,280
Again we use curly brackets here because
2272
01:51:38,480 --> 01:51:44,800
we want to save the result of this code
2273
01:51:41,280 --> 01:51:48,480
as the key. And now each component in
2274
01:51:44,800 --> 01:51:52,239
this array will have a unique key. Let's
2275
01:51:48,480 --> 01:51:54,320
save and check our website.
2276
01:51:52,239 --> 01:51:57,199
And now the error is gone from the
2277
01:51:54,320 --> 01:52:00,560
console.
2278
01:51:57,199 --> 01:52:04,000
Let's go back to our code.
2279
01:52:00,560 --> 01:52:05,840
And now we have a lot of code in our app
2280
01:52:04,000 --> 01:52:08,239
component.
2281
01:52:05,840 --> 01:52:10,480
To make our code easier to work with,
2282
01:52:08,239 --> 01:52:12,560
we're going to separate this code into
2283
01:52:10,480 --> 01:52:16,880
smaller pieces.
2284
01:52:12,560 --> 01:52:19,840
Notice that this code and this code are
2285
01:52:16,880 --> 01:52:22,239
related to chat messages.
2286
01:52:19,840 --> 01:52:25,199
So it makes sense to separate these two
2287
01:52:22,239 --> 01:52:26,719
pieces of code into a chat messages
2288
01:52:25,199 --> 01:52:29,199
component.
2289
01:52:26,719 --> 01:52:32,880
So we'll scroll up
2290
01:52:29,199 --> 01:52:36,639
above app. Let's create a new component
2291
01:52:32,880 --> 01:52:41,440
using function and we'll name it capital
2292
01:52:36,639 --> 01:52:44,639
C chat capital M messages
2293
01:52:41,440 --> 01:52:46,560
brackets and curly brackets.
2294
01:52:44,639 --> 01:52:49,280
And now we're going to move all the code
2295
01:52:46,560 --> 01:52:54,760
related to chat messages into this new
2296
01:52:49,280 --> 01:52:54,760
component. So we'll select this code
2297
01:52:54,960 --> 01:53:02,239
and cut.
2298
01:52:57,040 --> 01:53:04,800
And inside here, we're going to paste
2299
01:53:02,239 --> 01:53:08,000
and create some new lines. And we're
2300
01:53:04,800 --> 01:53:10,480
going to select this code
2301
01:53:08,000 --> 01:53:15,920
and cut.
2302
01:53:10,480 --> 01:53:18,080
And inside here, we're going to paste.
2303
01:53:15,920 --> 01:53:20,639
And at the end of this component, we
2304
01:53:18,080 --> 01:53:24,560
need to return the HTML.
2305
01:53:20,639 --> 01:53:27,040
So at the end we're going to return open
2306
01:53:24,560 --> 01:53:29,679
bracket and we'll remove the closed
2307
01:53:27,040 --> 01:53:31,679
bracket for now and we're going to use a
2308
01:53:29,679 --> 01:53:34,480
fragment to group all of these
2309
01:53:31,679 --> 01:53:38,080
components together. So we'll type less
2310
01:53:34,480 --> 01:53:41,760
than greater than and at the end of this
2311
01:53:38,080 --> 01:53:45,760
less than/greater than and we'll add the
2312
01:53:41,760 --> 01:53:48,239
close bracket at the end. And finally,
2313
01:53:45,760 --> 01:53:51,520
we can fix the indenting by pressing
2314
01:53:48,239 --> 01:53:54,080
tab. And we'll remove the indent here
2315
01:53:51,520 --> 01:53:56,560
and here as well, so that these are
2316
01:53:54,080 --> 01:53:59,199
vertically aligned.
2317
01:53:56,560 --> 01:54:01,199
Finally, now that we have this chat
2318
01:53:59,199 --> 01:54:04,080
messages component, we're going to
2319
01:54:01,199 --> 01:54:07,760
insert this into our app. So, we'll
2320
01:54:04,080 --> 01:54:10,239
scroll down into app and remove these
2321
01:54:07,760 --> 01:54:12,560
lines.
2322
01:54:10,239 --> 01:54:14,719
And to insert the chat messages
2323
01:54:12,560 --> 01:54:19,599
component, we'll use the component
2324
01:54:14,719 --> 01:54:22,480
syntax again. Less than capital C chat
2325
01:54:19,599 --> 01:54:25,040
capital M messages
2326
01:54:22,480 --> 01:54:27,679
slash greater than.
2327
01:54:25,040 --> 01:54:30,400
So now we just separated all that code
2328
01:54:27,679 --> 01:54:34,320
into its own component and it makes our
2329
01:54:30,400 --> 01:54:37,199
code much cleaner. Let's save
2330
01:54:34,320 --> 01:54:39,440
and check our website.
2331
01:54:37,199 --> 01:54:41,679
And everything still works. We just
2332
01:54:39,440 --> 01:54:44,639
reorganized our code into a chat
2333
01:54:41,679 --> 01:54:48,400
messages component.
2334
01:54:44,639 --> 01:54:51,840
Okay, so far we saved the data and we
2335
01:54:48,400 --> 01:54:53,920
generated the HTML. The last step to
2336
01:54:51,840 --> 01:54:55,599
building a website is to make it
2337
01:54:53,920 --> 01:54:58,320
interactive.
2338
01:54:55,599 --> 01:55:00,880
For example, when we click this send
2339
01:54:58,320 --> 01:55:03,440
button, we want to add a chat message to
2340
01:55:00,880 --> 01:55:06,000
the website. To make this website
2341
01:55:03,440 --> 01:55:07,599
interactive, we're going to use event
2342
01:55:06,000 --> 01:55:11,040
handlers.
2343
01:55:07,599 --> 01:55:14,080
An event handler lets us run a function
2344
01:55:11,040 --> 01:55:16,639
when we interact with a website.
2345
01:55:14,080 --> 01:55:19,280
Let's do an example. We'll go back to
2346
01:55:16,639 --> 01:55:21,440
our code
2347
01:55:19,280 --> 01:55:23,520
and we're going to scroll up to the chat
2348
01:55:21,440 --> 01:55:26,239
messages component
2349
01:55:23,520 --> 01:55:28,800
and then inside this fragment we're
2350
01:55:26,239 --> 01:55:32,320
going to create a new line and let's
2351
01:55:28,800 --> 01:55:36,239
create a new button using less than
2352
01:55:32,320 --> 01:55:39,440
button greater than and less than/button
2353
01:55:36,239 --> 01:55:44,000
greater than and inside this button
2354
01:55:39,440 --> 01:55:45,679
let's put the text send message.
2355
01:55:44,000 --> 01:55:47,520
So, we'll create a new button for
2356
01:55:45,679 --> 01:55:49,760
practice.
2357
01:55:47,520 --> 01:55:53,440
Next, we're going to give this button a
2358
01:55:49,760 --> 01:55:59,119
special prop. So, here we'll add a new
2359
01:55:53,440 --> 01:56:01,440
prop called on capital C click equals
2360
01:55:59,119 --> 01:56:05,119
curly brackets.
2361
01:56:01,440 --> 01:56:08,400
So, on click lets us run a function when
2362
01:56:05,119 --> 01:56:11,520
we click this button. So, above this,
2363
01:56:08,400 --> 01:56:16,159
let's create a function to run. So we'll
2364
01:56:11,520 --> 01:56:21,199
type function and let's name it send
2365
01:56:16,159 --> 01:56:23,599
message brackets and curly brackets
2366
01:56:21,199 --> 01:56:25,760
and inside this function let's just
2367
01:56:23,599 --> 01:56:30,800
console.log
2368
01:56:25,760 --> 01:56:32,719
the string send message.
2369
01:56:30,800 --> 01:56:35,360
Now that we have this function we're
2370
01:56:32,719 --> 01:56:38,400
going to save this function inside on
2371
01:56:35,360 --> 01:56:39,760
click. So between these curly brackets,
2372
01:56:38,400 --> 01:56:43,920
we're going to give the name of the
2373
01:56:39,760 --> 01:56:46,400
function send message.
2374
01:56:43,920 --> 01:56:49,199
So notice that again we used curly
2375
01:56:46,400 --> 01:56:52,639
brackets. Curly brackets lets us save
2376
01:56:49,199 --> 01:56:54,560
any type of value in a prop including a
2377
01:56:52,639 --> 01:56:57,520
function.
2378
01:56:54,560 --> 01:56:59,840
Now that we gave on click this function,
2379
01:56:57,520 --> 01:57:02,719
when we click this button, it's going to
2380
01:56:59,840 --> 01:57:06,239
run this function and console.log this
2381
01:57:02,719 --> 01:57:09,040
message. Let's save
2382
01:57:06,239 --> 01:57:11,520
and we'll go to our website
2383
01:57:09,040 --> 01:57:13,440
and try clicking this new send message
2384
01:57:11,520 --> 01:57:15,599
button
2385
01:57:13,440 --> 01:57:18,000
and this will run the function and
2386
01:57:15,599 --> 01:57:19,760
display the message send message in the
2387
01:57:18,000 --> 01:57:23,440
console.
2388
01:57:19,760 --> 01:57:25,840
Let's go back to our code
2389
01:57:23,440 --> 01:57:27,520
and we'll learn more details about event
2390
01:57:25,840 --> 01:57:31,440
handlers.
2391
01:57:27,520 --> 01:57:33,920
So on click is known as the event and
2392
01:57:31,440 --> 01:57:36,960
send message is known as the event
2393
01:57:33,920 --> 01:57:41,280
handler. It does something when this
2394
01:57:36,960 --> 01:57:43,760
event happens or it handles the event.
2395
01:57:41,280 --> 01:57:47,440
Now there's two other things to note.
2396
01:57:43,760 --> 01:57:50,719
The event prop always starts with on and
2397
01:57:47,440 --> 01:57:54,400
then the name of the event like click
2398
01:57:50,719 --> 01:57:57,360
and the event prop must use camelc case.
2399
01:57:54,400 --> 01:58:00,159
As a review, camelc case means the first
2400
01:57:57,360 --> 01:58:03,280
word is lowercase and all other words
2401
01:58:00,159 --> 01:58:05,920
start with a capital. So that's why on
2402
01:58:03,280 --> 01:58:08,320
is lowercase and click starts with a
2403
01:58:05,920 --> 01:58:10,159
capital C.
2404
01:58:08,320 --> 01:58:13,360
Another thing we should know is that
2405
01:58:10,159 --> 01:58:17,119
here we should not run the event handler
2406
01:58:13,360 --> 01:58:19,840
function. So we should not use brackets.
2407
01:58:17,119 --> 01:58:21,440
So this will run the function and then
2408
01:58:19,840 --> 01:58:24,239
the result of the function will be
2409
01:58:21,440 --> 01:58:27,440
undefined. and then it's going to save
2410
01:58:24,239 --> 01:58:30,320
undefined into on click which doesn't
2411
01:58:27,440 --> 01:58:33,199
work. Instead, we should just give the
2412
01:58:30,320 --> 01:58:35,040
function directly to on click without
2413
01:58:33,199 --> 01:58:37,840
running it.
2414
01:58:35,040 --> 01:58:41,360
So that's event handlers and event
2415
01:58:37,840 --> 01:58:43,840
handler lets us run a function when we
2416
01:58:41,360 --> 01:58:47,719
interact with a website and they make
2417
01:58:43,840 --> 01:58:47,719
the website interactive.
2418
01:58:47,760 --> 01:58:54,000
Next, when we click this button, instead
2419
01:58:50,800 --> 01:58:57,840
of just console.log, log. Let's add a
2420
01:58:54,000 --> 01:59:00,800
new chat message. To do that, we can add
2421
01:58:57,840 --> 01:59:04,560
a new object to the chat messages array
2422
01:59:00,800 --> 01:59:06,639
up here. So, inside this function, let's
2423
01:59:04,560 --> 01:59:10,400
remove console.log
2424
01:59:06,639 --> 01:59:15,040
and then type the name of the array chat
2425
01:59:10,400 --> 01:59:17,840
messages and then use the code.push
2426
01:59:15,040 --> 01:59:21,599
and brackets.
2427
01:59:17,840 --> 01:59:25,119
As a review, dot push adds a value to
2428
01:59:21,599 --> 01:59:27,920
the end of an array. So inside here,
2429
01:59:25,119 --> 01:59:30,159
we're going to add another object using
2430
01:59:27,920 --> 01:59:32,400
curly brackets.
2431
01:59:30,159 --> 01:59:37,119
And each object will have a message
2432
01:59:32,400 --> 01:59:39,760
property. So we'll type message colon
2433
01:59:37,119 --> 01:59:42,239
and then a string. And for our new
2434
01:59:39,760 --> 01:59:43,920
message, let's just make the message
2435
01:59:42,239 --> 01:59:46,880
test.
2436
01:59:43,920 --> 01:59:51,440
And we'll add a comma. And we'll add a
2437
01:59:46,880 --> 01:59:54,880
sender property sender colon a string
2438
01:59:51,440 --> 01:59:58,960
and this message will be from the user
2439
01:59:54,880 --> 02:00:02,800
comma and we'll give it an ID colon and
2440
01:59:58,960 --> 02:00:05,679
let's just use the string ID5.
2441
02:00:02,800 --> 02:00:07,840
Now there's one problem with using ID5
2442
02:00:05,679 --> 02:00:10,000
in this situation.
2443
02:00:07,840 --> 02:00:12,080
Every message that we create when we
2444
02:00:10,000 --> 02:00:17,119
click this button is going to have the
2445
02:00:12,080 --> 02:00:20,000
ID ID5. But the ID should be unique. To
2446
02:00:17,119 --> 02:00:23,599
create a unique ID, we can use some
2447
02:00:20,000 --> 02:00:28,000
JavaScript code. So instead of ID5,
2448
02:00:23,599 --> 02:00:32,560
we're going to use the code crypto dot
2449
02:00:28,000 --> 02:00:36,159
random capital U, capital U, capital I,
2450
02:00:32,560 --> 02:00:38,560
capital D, and brackets.
2451
02:00:36,159 --> 02:00:41,840
So this is some JavaScript code that
2452
02:00:38,560 --> 02:00:44,400
will generate a unique ID string every
2453
02:00:41,840 --> 02:00:46,719
time we run this code. So now every
2454
02:00:44,400 --> 02:00:50,320
message that we create using this button
2455
02:00:46,719 --> 02:00:53,040
will have a unique ID
2456
02:00:50,320 --> 02:00:56,000
at the end. Let's create some new lines
2457
02:00:53,040 --> 02:01:00,400
and let's console.log
2458
02:00:56,000 --> 02:01:04,239
the chat messages array to see if it has
2459
02:01:00,400 --> 02:01:06,719
a new message. Let's save
2460
02:01:04,239 --> 02:01:10,320
and we'll go to our website
2461
02:01:06,719 --> 02:01:13,679
and we'll click send message.
2462
02:01:10,320 --> 02:01:16,560
And now inside chat messages, we have
2463
02:01:13,679 --> 02:01:20,400
five messages. So we added this last one
2464
02:01:16,560 --> 02:01:23,599
here. It has the message test. And
2465
02:01:20,400 --> 02:01:26,239
notice it has a unique ID. So we
2466
02:01:23,599 --> 02:01:30,080
successfully added a chat message when
2467
02:01:26,239 --> 02:01:33,199
clicking this button.
2468
02:01:30,080 --> 02:01:36,719
Next, notice that even though we updated
2469
02:01:33,199 --> 02:01:39,199
the data, it did not update the website.
2470
02:01:36,719 --> 02:01:42,719
So, it did not display a new chat
2471
02:01:39,199 --> 02:01:44,480
message component on the website. To fix
2472
02:01:42,719 --> 02:01:47,920
this, we're going to learn one of the
2473
02:01:44,480 --> 02:01:52,159
most important features of React called
2474
02:01:47,920 --> 02:01:56,000
state. State is data that is connected
2475
02:01:52,159 --> 02:02:00,000
to the HTML. When we update this data,
2476
02:01:56,000 --> 02:02:03,040
it will update the HTML.
2477
02:02:00,000 --> 02:02:06,159
Let's go back to our code.
2478
02:02:03,040 --> 02:02:09,599
And if we scroll up,
2479
02:02:06,159 --> 02:02:13,119
currently chat messages is just a normal
2480
02:02:09,599 --> 02:02:16,400
variable. When we update chat messages,
2481
02:02:13,119 --> 02:02:19,360
it will not update the HTML.
2482
02:02:16,400 --> 02:02:22,080
However, if we convert chat messages
2483
02:02:19,360 --> 02:02:25,760
into state, when we update chat
2484
02:02:22,080 --> 02:02:27,840
messages, it will update the HTML. So
2485
02:02:25,760 --> 02:02:31,440
now let's learn how to convert chat
2486
02:02:27,840 --> 02:02:33,360
messages into state.
2487
02:02:31,440 --> 02:02:37,840
At the top of this component, we're
2488
02:02:33,360 --> 02:02:44,800
going to use this code capital R react
2489
02:02:37,840 --> 02:02:47,040
dot use capital S state and brackets.
2490
02:02:44,800 --> 02:02:50,239
And between the brackets, we're going to
2491
02:02:47,040 --> 02:02:53,760
give it the chat messages data. So,
2492
02:02:50,239 --> 02:02:56,800
we're going to select this data
2493
02:02:53,760 --> 02:02:58,800
and cut.
2494
02:02:56,800 --> 02:03:02,119
and between these brackets we're going
2495
02:02:58,800 --> 02:03:02,119
to paste.
2496
02:03:05,920 --> 02:03:12,800
So this will convert this data into
2497
02:03:08,800 --> 02:03:16,239
state. So now if we update this data, it
2498
02:03:12,800 --> 02:03:18,639
will also update the HTML.
2499
02:03:16,239 --> 02:03:21,520
So at the bottom here, we can remove the
2500
02:03:18,639 --> 02:03:25,280
chat messages variable because we
2501
02:03:21,520 --> 02:03:28,560
converted it into state.
2502
02:03:25,280 --> 02:03:31,599
Next, react doouse state returns an
2503
02:03:28,560 --> 02:03:36,880
array. So at the front, let's save the
2504
02:03:31,599 --> 02:03:40,400
result in a variable const array equals
2505
02:03:36,880 --> 02:03:43,679
react. State.
2506
02:03:40,400 --> 02:03:46,239
This array has two values. To get the
2507
02:03:43,679 --> 02:03:49,360
first value, we can click here to
2508
02:03:46,239 --> 02:03:51,280
collapse the code for now. And to get
2509
02:03:49,360 --> 02:03:54,639
the first value, we're going to type the
2510
02:03:51,280 --> 02:03:57,199
name of the array. Array and then square
2511
02:03:54,639 --> 02:03:59,280
brackets zero.
2512
02:03:57,199 --> 02:04:02,080
So remember, an array is a list of
2513
02:03:59,280 --> 02:04:04,880
values and square bracket zero gets us
2514
02:04:02,080 --> 02:04:06,480
the first value in the list or in the
2515
02:04:04,880 --> 02:04:09,280
array.
2516
02:04:06,480 --> 02:04:12,880
So the first value we get from use state
2517
02:04:09,280 --> 02:04:16,400
is the current data.
2518
02:04:12,880 --> 02:04:18,719
So here chat messages can change over
2519
02:04:16,400 --> 02:04:22,239
time. It's going to start off as this
2520
02:04:18,719 --> 02:04:24,960
value which is the initial value and
2521
02:04:22,239 --> 02:04:27,119
then we might add new chat messages in
2522
02:04:24,960 --> 02:04:29,679
the future.
2523
02:04:27,119 --> 02:04:32,800
So the first value in this array gives
2524
02:04:29,679 --> 02:04:35,840
us the current value of chat messages or
2525
02:04:32,800 --> 02:04:37,920
the current data.
2526
02:04:35,840 --> 02:04:41,360
Let's save this in a variable so we can
2527
02:04:37,920 --> 02:04:47,119
use it later. We'll do const and let's
2528
02:04:41,360 --> 02:04:49,199
name it chat messages equals array zero.
2529
02:04:47,119 --> 02:04:52,159
So now we have the most current chat
2530
02:04:49,199 --> 02:04:55,360
messages and we can use it down here to
2531
02:04:52,159 --> 02:04:58,719
generate the HTML.
2532
02:04:55,360 --> 02:05:02,000
Next use state gives us a second value
2533
02:04:58,719 --> 02:05:04,880
in this array. To get the second value
2534
02:05:02,000 --> 02:05:07,280
we're going to type array square bracket
2535
02:05:04,880 --> 02:05:10,080
1.
2536
02:05:07,280 --> 02:05:14,960
And the second value of this array is a
2537
02:05:10,080 --> 02:05:16,639
function that updates this data.
2538
02:05:14,960 --> 02:05:20,400
So let's save this function in a
2539
02:05:16,639 --> 02:05:23,040
variable using const. And the naming
2540
02:05:20,400 --> 02:05:26,560
convention for this function is a use
2541
02:05:23,040 --> 02:05:32,320
set. And then the name of the data which
2542
02:05:26,560 --> 02:05:35,280
is capital C chat capital M messages
2543
02:05:32,320 --> 02:05:37,840
equals this value. And we should also
2544
02:05:35,280 --> 02:05:40,080
use camel case here. So the first word
2545
02:05:37,840 --> 02:05:43,040
starts with a lowercase and every other
2546
02:05:40,080 --> 02:05:45,920
word starts with a capital.
2547
02:05:43,040 --> 02:05:49,520
So this function lets us update the
2548
02:05:45,920 --> 02:05:52,000
data. In React, we should not update the
2549
02:05:49,520 --> 02:05:55,440
data directly like we did down here
2550
02:05:52,000 --> 02:05:58,560
using push. We should always use this
2551
02:05:55,440 --> 02:06:01,280
function to update the data. And that's
2552
02:05:58,560 --> 02:06:05,119
because this function tells React that
2553
02:06:01,280 --> 02:06:07,679
the data changed and to update the HTML.
2554
02:06:05,119 --> 02:06:10,800
If we update the data directly, React
2555
02:06:07,679 --> 02:06:13,599
will not update the HTML.
2556
02:06:10,800 --> 02:06:17,599
If we use this function to update the
2557
02:06:13,599 --> 02:06:19,679
data, React will update the HTML. So we
2558
02:06:17,599 --> 02:06:22,560
should always use this function to
2559
02:06:19,679 --> 02:06:26,760
update the data. This is also known as
2560
02:06:22,560 --> 02:06:26,760
the updater function.
2561
02:06:27,040 --> 02:06:32,480
So now let's give it a try. Instead of
2562
02:06:29,520 --> 02:06:36,239
updating this data directly, we're going
2563
02:06:32,480 --> 02:06:42,079
to use this updater function by typing
2564
02:06:36,239 --> 02:06:44,400
set chat messages and brackets.
2565
02:06:42,079 --> 02:06:48,639
And between the brackets, we're going to
2566
02:06:44,400 --> 02:06:50,719
put the new value of chat messages.
2567
02:06:48,639 --> 02:06:53,280
So the way that this updator function
2568
02:06:50,719 --> 02:06:56,400
works is it's going to replace the
2569
02:06:53,280 --> 02:07:00,800
entire chat messages array with a new
2570
02:06:56,400 --> 02:07:04,079
array. So let's give it a try. First, we
2571
02:07:00,800 --> 02:07:06,880
need to make a copy of this array.
2572
02:07:04,079 --> 02:07:09,679
In React, we should not modify the data
2573
02:07:06,880 --> 02:07:13,280
directly. We should always create a copy
2574
02:07:09,679 --> 02:07:16,880
of the data and then modify the copy.
2575
02:07:13,280 --> 02:07:19,280
This helps React be more efficient.
2576
02:07:16,880 --> 02:07:22,000
To create a copy, we're going to click
2577
02:07:19,280 --> 02:07:26,079
between these brackets and create a new
2578
02:07:22,000 --> 02:07:29,280
array using square brackets.
2579
02:07:26,079 --> 02:07:33,119
And to copy chat messages, we can use
2580
02:07:29,280 --> 02:07:38,480
this code dot dot dot and the array that
2581
02:07:33,119 --> 02:07:41,280
we want to copy, which is chat messages.
2582
02:07:38,480 --> 02:07:44,960
As a review, this is a JavaScript
2583
02:07:41,280 --> 02:07:47,760
feature called the spread operator. It
2584
02:07:44,960 --> 02:07:51,360
basically takes the values in an array
2585
02:07:47,760 --> 02:07:53,199
and then copies them into a new array.
2586
02:07:51,360 --> 02:07:55,679
You can think of it like we're taking
2587
02:07:53,199 --> 02:07:58,639
the values in this array and then
2588
02:07:55,679 --> 02:08:00,560
spreading them out into this new array.
2589
02:07:58,639 --> 02:08:03,280
That's why it's called the spread
2590
02:08:00,560 --> 02:08:05,920
operator.
2591
02:08:03,280 --> 02:08:08,800
The spread operator is used a lot in
2592
02:08:05,920 --> 02:08:12,960
React tutorials and documentation. So,
2593
02:08:08,800 --> 02:08:16,639
it's useful to know how it works.
2594
02:08:12,960 --> 02:08:19,119
So, we created a copy of chat messages.
2595
02:08:16,639 --> 02:08:22,000
And now we want to add a new chat
2596
02:08:19,119 --> 02:08:24,960
message at the end of this copy because
2597
02:08:22,000 --> 02:08:27,760
we're trying to send a new message. To
2598
02:08:24,960 --> 02:08:30,560
do that, we can just add a comma at the
2599
02:08:27,760 --> 02:08:34,639
end and then add a new chat message
2600
02:08:30,560 --> 02:08:36,880
directly using curly brackets.
2601
02:08:34,639 --> 02:08:39,840
So each chat message has a message
2602
02:08:36,880 --> 02:08:43,280
property colon and we'll give it the
2603
02:08:39,840 --> 02:08:46,639
string test for now. And each chat
2604
02:08:43,280 --> 02:08:49,760
message has a sender property colon. And
2605
02:08:46,639 --> 02:08:53,360
let's make the sender the user.
2606
02:08:49,760 --> 02:08:56,480
And we'll give each chat message an ID.
2607
02:08:53,360 --> 02:08:58,239
And to create a unique ID we can use
2608
02:08:56,480 --> 02:09:03,119
crypto
2609
02:08:58,239 --> 02:09:07,040
random capital U capital U capital I
2610
02:09:03,119 --> 02:09:10,159
capital D and brackets.
2611
02:09:07,040 --> 02:09:13,199
So using the spread operator we made a
2612
02:09:10,159 --> 02:09:16,000
copy of the chat messages array and then
2613
02:09:13,199 --> 02:09:17,840
we added a value at the end of this new
2614
02:09:16,000 --> 02:09:20,800
array.
2615
02:09:17,840 --> 02:09:23,119
And now this updater function will
2616
02:09:20,800 --> 02:09:26,320
replace chat messages with this new
2617
02:09:23,119 --> 02:09:29,199
array. And because we use this updater
2618
02:09:26,320 --> 02:09:32,639
function, React will regenerate the
2619
02:09:29,199 --> 02:09:34,800
HTML. So it will basically rerun all
2620
02:09:32,639 --> 02:09:38,880
this code and it will update the
2621
02:09:34,800 --> 02:09:41,520
website. So now let's save
2622
02:09:38,880 --> 02:09:44,880
and let's check our website
2623
02:09:41,520 --> 02:09:47,440
and we'll press send message.
2624
02:09:44,880 --> 02:09:51,199
it will create a new chat message in our
2625
02:09:47,440 --> 02:09:53,360
data and it will also update the HTML.
2626
02:09:51,199 --> 02:09:57,520
So it will create a new chat message
2627
02:09:53,360 --> 02:10:01,040
component and display it on the website.
2628
02:09:57,520 --> 02:10:04,639
Now let's go back
2629
02:10:01,040 --> 02:10:07,920
and that is how state works. Let's do a
2630
02:10:04,639 --> 02:10:11,360
short review. State is basically data
2631
02:10:07,920 --> 02:10:14,400
that is connected to the HTML. When we
2632
02:10:11,360 --> 02:10:17,280
update this data or we update the state,
2633
02:10:14,400 --> 02:10:20,639
it will update the HTML.
2634
02:10:17,280 --> 02:10:23,920
To convert some data into state, we use
2635
02:10:20,639 --> 02:10:27,840
react doouse state. So here it will
2636
02:10:23,920 --> 02:10:31,520
convert this data into state and use
2637
02:10:27,840 --> 02:10:35,119
state gives us two values.
2638
02:10:31,520 --> 02:10:37,920
The first value is the current data and
2639
02:10:35,119 --> 02:10:41,599
then the second value is a function to
2640
02:10:37,920 --> 02:10:45,920
update this data. If we use the updater
2641
02:10:41,599 --> 02:10:48,159
function, it will also update the HTML.
2642
02:10:45,920 --> 02:10:50,320
So don't worry if state is a little bit
2643
02:10:48,159 --> 02:10:52,480
confusing at first. The more you work
2644
02:10:50,320 --> 02:10:54,960
with state, the more you'll get used to
2645
02:10:52,480 --> 02:10:57,920
it. And we'll do more examples of state
2646
02:10:54,960 --> 02:11:01,760
in this course and in the exercises so
2647
02:10:57,920 --> 02:11:03,920
you can get more practice.
2648
02:11:01,760 --> 02:11:06,719
Next, we're going to learn a shortcut
2649
02:11:03,920 --> 02:11:09,760
called array destructuring.
2650
02:11:06,719 --> 02:11:12,800
Here we get the first value out of this
2651
02:11:09,760 --> 02:11:14,800
array and save it in a variable called
2652
02:11:12,800 --> 02:11:17,520
chat messages.
2653
02:11:14,800 --> 02:11:22,560
Now JavaScript actually has a shortcut
2654
02:11:17,520 --> 02:11:25,360
for this. Above this we can type const
2655
02:11:22,560 --> 02:11:27,440
and then square bracket equals this
2656
02:11:25,360 --> 02:11:29,840
array
2657
02:11:27,440 --> 02:11:33,840
and inside the square brackets we can
2658
02:11:29,840 --> 02:11:35,920
type chat messages.
2659
02:11:33,840 --> 02:11:39,040
So this is similar to object
2660
02:11:35,920 --> 02:11:41,520
dstructuring. This gets the first value
2661
02:11:39,040 --> 02:11:44,639
out of this array and saves it in a
2662
02:11:41,520 --> 02:11:48,639
variable called chat messages. So this
2663
02:11:44,639 --> 02:11:51,199
code is a shortcut for this code. We can
2664
02:11:48,639 --> 02:11:55,199
also get the second value out of this
2665
02:11:51,199 --> 02:11:57,840
array. At the end here we can type comma
2666
02:11:55,199 --> 02:12:02,960
and then another variable name. Let's
2667
02:11:57,840 --> 02:12:05,760
use set chat messages.
2668
02:12:02,960 --> 02:12:08,320
So this gets the second value out of
2669
02:12:05,760 --> 02:12:12,400
this array and saves it in a variable
2670
02:12:08,320 --> 02:12:15,040
called set chat messages. So this code
2671
02:12:12,400 --> 02:12:18,400
is a shortcut for these two lines of
2672
02:12:15,040 --> 02:12:21,199
code. And this shortcut is called array
2673
02:12:18,400 --> 02:12:24,000
dstructuring because we're taking stuff
2674
02:12:21,199 --> 02:12:28,159
out of this array. We're deconstructing
2675
02:12:24,000 --> 02:12:31,119
it. Now in array dstructuring the order
2676
02:12:28,159 --> 02:12:33,920
matters. So this will always get the
2677
02:12:31,119 --> 02:12:36,239
first value out of the array and this
2678
02:12:33,920 --> 02:12:38,159
will always get the second value out of
2679
02:12:36,239 --> 02:12:40,239
the array.
2680
02:12:38,159 --> 02:12:42,560
So now let's comment out these two
2681
02:12:40,239 --> 02:12:45,280
lines. So we remember that this is just
2682
02:12:42,560 --> 02:12:48,400
a shortcut. So we'll type double slash
2683
02:12:45,280 --> 02:12:50,000
at the front and here double slash at
2684
02:12:48,400 --> 02:12:52,639
the front.
2685
02:12:50,000 --> 02:12:55,040
Now another shortcut we can use is
2686
02:12:52,639 --> 02:12:57,440
instead of saving the result of use
2687
02:12:55,040 --> 02:13:00,079
state in this variable and then
2688
02:12:57,440 --> 02:13:02,239
dstructuring it we can just dstructure
2689
02:13:00,079 --> 02:13:05,040
this up here.
2690
02:13:02,239 --> 02:13:08,480
So instead of array we can just directly
2691
02:13:05,040 --> 02:13:11,040
dstructure using square brackets and
2692
02:13:08,480 --> 02:13:14,320
then the first value of u state is going
2693
02:13:11,040 --> 02:13:16,880
to be chat messages
2694
02:13:14,320 --> 02:13:20,560
and the second value from state is going
2695
02:13:16,880 --> 02:13:23,360
to be the updater function which is set
2696
02:13:20,560 --> 02:13:26,079
chat messages.
2697
02:13:23,360 --> 02:13:28,320
So this code is a shortcut for this
2698
02:13:26,079 --> 02:13:31,520
code. We just don't save it in a
2699
02:13:28,320 --> 02:13:33,760
variable in the middle.
2700
02:13:31,520 --> 02:13:37,599
So at the front of this code, let's also
2701
02:13:33,760 --> 02:13:40,480
comment it out using double slash.
2702
02:13:37,599 --> 02:13:43,199
So that is array dstructuring. It gives
2703
02:13:40,480 --> 02:13:45,440
us an easier way to take values out of
2704
02:13:43,199 --> 02:13:47,840
an array.
2705
02:13:45,440 --> 02:13:50,320
Now this shortcut is used a lot in React
2706
02:13:47,840 --> 02:13:54,679
tutorials and documentation. So it's
2707
02:13:50,320 --> 02:13:54,679
useful to know how it works.
2708
02:13:54,960 --> 02:14:00,719
Next, instead of adding the same message
2709
02:13:57,840 --> 02:14:03,440
test every time, we should add whatever
2710
02:14:00,719 --> 02:14:07,280
message is in the text box. So, we'll
2711
02:14:03,440 --> 02:14:09,840
learn how to get the text in a text box.
2712
02:14:07,280 --> 02:14:13,040
In normal JavaScript, we might use the
2713
02:14:09,840 --> 02:14:16,320
DOM to get the input element and then
2714
02:14:13,040 --> 02:14:21,040
use a property called dot value to get
2715
02:14:16,320 --> 02:14:24,560
the text inside the text box.
2716
02:14:21,040 --> 02:14:27,679
However, in React, a best practice is do
2717
02:14:24,560 --> 02:14:30,639
not use the DOM manually. That's because
2718
02:14:27,679 --> 02:14:33,520
React is managing the website. So, if we
2719
02:14:30,639 --> 02:14:36,480
use the DOM manually, it might interfere
2720
02:14:33,520 --> 02:14:39,280
with React.
2721
02:14:36,480 --> 02:14:42,639
Instead, we should use React features to
2722
02:14:39,280 --> 02:14:46,639
get the text from a text box. One way to
2723
02:14:42,639 --> 02:14:50,159
do this is we're going to scroll up to
2724
02:14:46,639 --> 02:14:52,719
the text box which is inside the chat
2725
02:14:50,159 --> 02:14:55,920
input component and it's the input
2726
02:14:52,719 --> 02:15:00,719
element here. So to get the text in this
2727
02:14:55,920 --> 02:15:06,480
text box we can use a prop called on
2728
02:15:00,719 --> 02:15:09,199
capital C change equals curly brackets.
2729
02:15:06,480 --> 02:15:11,840
Earlier we learned about the event on
2730
02:15:09,199 --> 02:15:15,679
click which runs a function when we
2731
02:15:11,840 --> 02:15:18,560
click an element. On change is another
2732
02:15:15,679 --> 02:15:22,000
event we can use in React. You can tell
2733
02:15:18,560 --> 02:15:24,800
it's an event because it starts with on.
2734
02:15:22,000 --> 02:15:28,880
So onchange runs a function when we
2735
02:15:24,800 --> 02:15:31,840
change the text inside an input.
2736
02:15:28,880 --> 02:15:35,360
So above this let's create a function to
2737
02:15:31,840 --> 02:15:40,560
run. We'll type function
2738
02:15:35,360 --> 02:15:43,679
and let's name it save input text
2739
02:15:40,560 --> 02:15:46,400
brackets and curly brackets because
2740
02:15:43,679 --> 02:15:48,000
we're going to save the text inside this
2741
02:15:46,400 --> 02:15:50,719
input.
2742
02:15:48,000 --> 02:15:53,599
And now let's save this function inside
2743
02:15:50,719 --> 02:15:57,679
on change. So between the curly brackets
2744
02:15:53,599 --> 02:16:01,199
we'll give it the function save input
2745
02:15:57,679 --> 02:16:04,079
text. So again we use curly brackets
2746
02:16:01,199 --> 02:16:06,719
here because we want to save a function
2747
02:16:04,079 --> 02:16:10,639
inside this prop rather than just a
2748
02:16:06,719 --> 02:16:13,679
string. So now every time we change the
2749
02:16:10,639 --> 02:16:15,679
text in this input it will run this
2750
02:16:13,679 --> 02:16:19,199
function.
2751
02:16:15,679 --> 02:16:21,199
Next when onchange runs this function
2752
02:16:19,199 --> 02:16:26,239
it's going to give this function one
2753
02:16:21,199 --> 02:16:28,880
parameter up here called event.
2754
02:16:26,239 --> 02:16:31,920
event is an object and it contains
2755
02:16:28,880 --> 02:16:37,359
details about the event or about the
2756
02:16:31,920 --> 02:16:39,519
change. So one detail is event dot
2757
02:16:37,359 --> 02:16:41,840
target
2758
02:16:39,519 --> 02:16:45,120
and this gives us the element that we're
2759
02:16:41,840 --> 02:16:47,439
typing in or that we changed. So in this
2760
02:16:45,120 --> 02:16:50,559
case it's going to give us this input
2761
02:16:47,439 --> 02:16:53,359
element. So React gives us access to
2762
02:16:50,559 --> 02:16:55,840
this input element automatically using
2763
02:16:53,359 --> 02:16:58,160
event.target. target.
2764
02:16:55,840 --> 02:17:01,519
And now to get the text inside this
2765
02:16:58,160 --> 02:17:03,120
input, we're going to use the property
2766
02:17:01,519 --> 02:17:05,439
value.
2767
02:17:03,120 --> 02:17:10,120
So now let's console.log this to see
2768
02:17:05,439 --> 02:17:10,120
what it looks like. console.log
2769
02:17:10,960 --> 02:17:17,120
and let's save our code
2770
02:17:14,080 --> 02:17:19,679
and we'll go back to our website
2771
02:17:17,120 --> 02:17:22,639
and we'll give it a try. So we'll click
2772
02:17:19,679 --> 02:17:24,559
this text box and we're going to type
2773
02:17:22,639 --> 02:17:26,559
hello.
2774
02:17:24,559 --> 02:17:29,760
And you'll see that every time we type
2775
02:17:26,559 --> 02:17:33,439
or change the text, it will console.log
2776
02:17:29,760 --> 02:17:36,240
the text in the text box. So that is how
2777
02:17:33,439 --> 02:17:40,760
we get the text inside the text box
2778
02:17:36,240 --> 02:17:40,760
using an onchange event.
2779
02:17:40,800 --> 02:17:48,080
Let's go back to our code.
2780
02:17:44,479 --> 02:17:50,319
And now we need to save this text so we
2781
02:17:48,080 --> 02:17:53,439
can use it later when creating a chat
2782
02:17:50,319 --> 02:17:56,960
message. To save this text, we're going
2783
02:17:53,439 --> 02:17:59,840
to use state. In React, we should use
2784
02:17:56,960 --> 02:18:03,359
state to save data that changes over
2785
02:17:59,840 --> 02:18:06,319
time, like the text in the text box. So,
2786
02:18:03,359 --> 02:18:11,760
at the top of this component,
2787
02:18:06,319 --> 02:18:14,559
let's use the code react dot use state
2788
02:18:11,760 --> 02:18:16,479
and brackets.
2789
02:18:14,559 --> 02:18:20,240
And between the brackets, we're going to
2790
02:18:16,479 --> 02:18:23,359
put the initial value. In this case, the
2791
02:18:20,240 --> 02:18:26,639
initial value in the text box.
2792
02:18:23,359 --> 02:18:28,800
The text box originally starts as empty.
2793
02:18:26,639 --> 02:18:30,719
So let's set the value to the empty
2794
02:18:28,800 --> 02:18:34,399
string.
2795
02:18:30,719 --> 02:18:37,679
Next, use state returns an array with
2796
02:18:34,399 --> 02:18:41,359
two values. So at the front, let's use
2797
02:18:37,679 --> 02:18:46,000
array dstructuring using const and we'll
2798
02:18:41,359 --> 02:18:48,559
type square brackets equals this result.
2799
02:18:46,000 --> 02:18:51,679
And now to get the first value out of
2800
02:18:48,559 --> 02:18:56,559
this array inside the square brackets
2801
02:18:51,679 --> 02:18:59,920
we're going to type input text.
2802
02:18:56,559 --> 02:19:03,040
So this is the current data or the
2803
02:18:59,920 --> 02:19:06,160
current text in the text box. So that's
2804
02:19:03,040 --> 02:19:08,240
why we name it input text.
2805
02:19:06,160 --> 02:19:12,240
To get the second value out of this
2806
02:19:08,240 --> 02:19:15,359
array after this we can type comma and
2807
02:19:12,240 --> 02:19:18,960
give a name for the second value. So the
2808
02:19:15,359 --> 02:19:22,080
naming convention is to use the word set
2809
02:19:18,960 --> 02:19:27,519
and then the name of the data which is
2810
02:19:22,080 --> 02:19:30,080
capital I input capital T text.
2811
02:19:27,519 --> 02:19:34,559
So this second value is an updator
2812
02:19:30,080 --> 02:19:37,359
function. It lets us update the data.
2813
02:19:34,559 --> 02:19:39,760
Now that we set up the state, every time
2814
02:19:37,359 --> 02:19:43,599
we change the text, we're going to save
2815
02:19:39,760 --> 02:19:46,639
it up here inside input text. To do
2816
02:19:43,599 --> 02:19:49,439
that, instead of console.log,
2817
02:19:46,639 --> 02:19:53,520
we're going to use the updater function
2818
02:19:49,439 --> 02:19:56,560
set input text.
2819
02:19:53,520 --> 02:19:58,720
And now, every time we change the text,
2820
02:19:56,560 --> 02:20:02,240
it's going to run this function. And
2821
02:19:58,720 --> 02:20:05,240
it's going to save the text inside input
2822
02:20:02,240 --> 02:20:05,240
text.
2823
02:20:05,439 --> 02:20:11,040
Next, when we click the send button, we
2824
02:20:08,479 --> 02:20:14,240
want to use this text to create a new
2825
02:20:11,040 --> 02:20:16,560
chat message. First, we need to make the
2826
02:20:14,240 --> 02:20:20,000
send button interactive.
2827
02:20:16,560 --> 02:20:23,680
To do that, inside this button element,
2828
02:20:20,000 --> 02:20:27,040
we're going to add the prop on click
2829
02:20:23,680 --> 02:20:30,560
equals curly brackets.
2830
02:20:27,040 --> 02:20:33,040
Again, on click lets us run a function
2831
02:20:30,560 --> 02:20:35,600
when we click this button.
2832
02:20:33,040 --> 02:20:39,920
And now above this, let's create a
2833
02:20:35,600 --> 02:20:44,000
function to run. We'll use function
2834
02:20:39,920 --> 02:20:46,800
and let's name it send message brackets
2835
02:20:44,000 --> 02:20:49,600
and curly brackets and we're going to
2836
02:20:46,800 --> 02:20:52,880
give this function to on click. So down
2837
02:20:49,600 --> 02:20:55,840
here we'll give the function name send
2838
02:20:52,880 --> 02:20:58,399
message.
2839
02:20:55,840 --> 02:21:00,560
Now when we click this button it will
2840
02:20:58,399 --> 02:21:03,520
run this function and let's just
2841
02:21:00,560 --> 02:21:09,359
console.log input text to see what the
2842
02:21:03,520 --> 02:21:15,520
value is. So in here we'll console.log
2843
02:21:09,359 --> 02:21:18,640
brackets the input text and save.
2844
02:21:15,520 --> 02:21:22,160
And we'll go back to our website
2845
02:21:18,640 --> 02:21:26,800
and in the text box we're going to type
2846
02:21:22,160 --> 02:21:29,280
hello and then press send.
2847
02:21:26,800 --> 02:21:34,200
And you'll see that it console.logs the
2848
02:21:29,280 --> 02:21:34,200
text inside the text box.
2849
02:21:34,240 --> 02:21:40,240
Next, we're going to create a new chat
2850
02:21:36,880 --> 02:21:42,960
message using this text. Let's go back
2851
02:21:40,240 --> 02:21:47,800
to our code.
2852
02:21:42,960 --> 02:21:47,800
And if we scroll down in our code
2853
02:21:47,840 --> 02:21:54,080
to the chat messages component, we
2854
02:21:51,040 --> 02:21:57,680
already have code here that adds a new
2855
02:21:54,080 --> 02:22:01,680
message to chat messages. So, let's just
2856
02:21:57,680 --> 02:22:04,240
select this code and we'll copy.
2857
02:22:01,680 --> 02:22:07,600
And at the top
2858
02:22:04,240 --> 02:22:09,920
inside send message, we're going to
2859
02:22:07,600 --> 02:22:12,800
paste. And we can remove this
2860
02:22:09,920 --> 02:22:16,760
console.log.
2861
02:22:12,800 --> 02:22:16,760
And we'll scroll back down.
2862
02:22:16,880 --> 02:22:21,040
And we created this function and this
2863
02:22:18,960 --> 02:22:23,120
button for practice. But now we don't
2864
02:22:21,040 --> 02:22:25,760
need it anymore. So we can actually
2865
02:22:23,120 --> 02:22:30,280
remove this code
2866
02:22:25,760 --> 02:22:30,280
and remove this practice button.
2867
02:22:30,880 --> 02:22:35,000
Now, let's scroll back up
2868
02:22:35,120 --> 02:22:40,240
and we're going to use this code to add
2869
02:22:37,520 --> 02:22:43,120
a new chat message.
2870
02:22:40,240 --> 02:22:45,600
However, we have a problem here. We
2871
02:22:43,120 --> 02:22:49,040
don't actually have access to set chat
2872
02:22:45,600 --> 02:22:52,479
messages inside this component because
2873
02:22:49,040 --> 02:22:54,240
this is inside a different component. To
2874
02:22:52,479 --> 02:22:59,280
solve this problem, we're going to learn
2875
02:22:54,240 --> 02:23:01,520
a technique called lifting the state up.
2876
02:22:59,280 --> 02:23:05,200
First we need to visualize the
2877
02:23:01,520 --> 02:23:07,520
components in our app. So we have an app
2878
02:23:05,200 --> 02:23:10,880
component which represents the entire
2879
02:23:07,520 --> 02:23:13,600
application and inside we have a chat
2880
02:23:10,880 --> 02:23:16,160
input component and a chat messages
2881
02:23:13,600 --> 02:23:18,880
component. Inside the chat messages
2882
02:23:16,160 --> 02:23:20,399
component we have some chat message
2883
02:23:18,880 --> 02:23:22,960
components.
2884
02:23:20,399 --> 02:23:25,200
This is called the component tree
2885
02:23:22,960 --> 02:23:28,240
because it looks like the branch of a
2886
02:23:25,200 --> 02:23:32,000
tree and this helps us visualize the
2887
02:23:28,240 --> 02:23:35,200
structure of the components in our app.
2888
02:23:32,000 --> 02:23:38,319
Currently, chat messages and set chat
2889
02:23:35,200 --> 02:23:41,200
messages are located inside the chat
2890
02:23:38,319 --> 02:23:45,120
messages component, but we want to use
2891
02:23:41,200 --> 02:23:47,040
it in the chat input component as well.
2892
02:23:45,120 --> 02:23:50,000
To solve this problem, we're going to
2893
02:23:47,040 --> 02:23:53,280
move this state up into the app
2894
02:23:50,000 --> 02:23:56,399
component and then app can share chat
2895
02:23:53,280 --> 02:23:58,479
messages and set chat messages between
2896
02:23:56,399 --> 02:24:00,880
these two components.
2897
02:23:58,479 --> 02:24:04,479
This technique is called lifting the
2898
02:24:00,880 --> 02:24:07,680
state up. By moving the state up to an
2899
02:24:04,479 --> 02:24:11,880
outer component, it lets us share state
2900
02:24:07,680 --> 02:24:11,880
between multiple components.
2901
02:24:11,920 --> 02:24:18,080
So, let's give this a try. First, we're
2902
02:24:14,880 --> 02:24:22,000
going to move the chat messages state up
2903
02:24:18,080 --> 02:24:24,240
into app. So, in our code, we're going
2904
02:24:22,000 --> 02:24:26,880
to scroll down
2905
02:24:24,240 --> 02:24:29,359
to the chat messages state, which is
2906
02:24:26,880 --> 02:24:32,640
right here, and we're going to move it
2907
02:24:29,359 --> 02:24:34,720
into the app component. So, we'll select
2908
02:24:32,640 --> 02:24:37,040
all this code,
2909
02:24:34,720 --> 02:24:40,160
and then cut
2910
02:24:37,040 --> 02:24:43,600
and remove these lines.
2911
02:24:40,160 --> 02:24:48,720
And then inside the app component,
2912
02:24:43,600 --> 02:24:52,240
we'll create some new lines and paste.
2913
02:24:48,720 --> 02:24:54,240
So now we lifted the state up into the
2914
02:24:52,240 --> 02:24:56,479
app component.
2915
02:24:54,240 --> 02:25:02,120
Next, we're going to share the state
2916
02:24:56,479 --> 02:25:02,120
between the two components using props.
2917
02:25:02,240 --> 02:25:10,160
So first, the chat input component needs
2918
02:25:05,600 --> 02:25:14,080
set chat messages and chat messages.
2919
02:25:10,160 --> 02:25:16,640
So down here, let's create two props for
2920
02:25:14,080 --> 02:25:21,200
this component.
2921
02:25:16,640 --> 02:25:23,600
We'll create a prop called chat messages
2922
02:25:21,200 --> 02:25:26,560
equals curly brackets. And we're going
2923
02:25:23,600 --> 02:25:31,760
to save the value of chat messages from
2924
02:25:26,560 --> 02:25:34,080
the state. So we'll save chat messages.
2925
02:25:31,760 --> 02:25:39,200
And we'll give it another prop called
2926
02:25:34,080 --> 02:25:41,840
set chat messages equals curly brackets.
2927
02:25:39,200 --> 02:25:45,760
And we'll give it the set chat messages
2928
02:25:41,840 --> 02:25:50,720
function from the state.
2929
02:25:45,760 --> 02:25:53,520
So set chat messages.
2930
02:25:50,720 --> 02:25:56,800
By the way, notice that chat messages is
2931
02:25:53,520 --> 02:26:00,240
an array. So using curly brackets, we
2932
02:25:56,800 --> 02:26:02,640
can save an array into this prop. So we
2933
02:26:00,240 --> 02:26:05,439
can save any type of value inside a
2934
02:26:02,640 --> 02:26:08,240
prop.
2935
02:26:05,439 --> 02:26:13,359
And then inside chat input, we can
2936
02:26:08,240 --> 02:26:16,080
access these two props by scrolling up.
2937
02:26:13,359 --> 02:26:19,359
And in the chat input component at the
2938
02:26:16,080 --> 02:26:24,479
top, we'll type curly brackets and we'll
2939
02:26:19,359 --> 02:26:28,960
get the chat messages prop,
2940
02:26:24,479 --> 02:26:31,439
and we'll also get the set chat messages
2941
02:26:28,960 --> 02:26:34,399
prop.
2942
02:26:31,439 --> 02:26:39,200
And now this code has access to set chat
2943
02:26:34,399 --> 02:26:42,479
messages and chat messages.
2944
02:26:39,200 --> 02:26:45,600
Next, let's scroll back down
2945
02:26:42,479 --> 02:26:48,479
to the chat messages component.
2946
02:26:45,600 --> 02:26:52,319
So here we just need the chat messages
2947
02:26:48,479 --> 02:26:55,600
array to generate the HTML. So let's
2948
02:26:52,319 --> 02:26:58,160
provide chat messages using a prop at
2949
02:26:55,600 --> 02:27:00,800
the bottom
2950
02:26:58,160 --> 02:27:04,960
at the chat messages component. We're
2951
02:27:00,800 --> 02:27:07,760
going to add a prop. chat messages
2952
02:27:04,960 --> 02:27:12,800
equals curly brackets and we'll give it
2953
02:27:07,760 --> 02:27:15,120
the chat messages array. So here chat
2954
02:27:12,800 --> 02:27:17,200
messages.
2955
02:27:15,120 --> 02:27:19,840
Now I want to talk about the naming
2956
02:27:17,200 --> 02:27:23,280
convention used here. You'll notice that
2957
02:27:19,840 --> 02:27:26,399
the component, the prop and the data all
2958
02:27:23,280 --> 02:27:30,359
use the same name. Instead of using
2959
02:27:26,399 --> 02:27:30,359
names like this,
2960
02:27:30,399 --> 02:27:35,200
this is the naming convention used in
2961
02:27:32,880 --> 02:27:37,439
the React documentation.
2962
02:27:35,200 --> 02:27:40,240
The documentation tries to use the same
2963
02:27:37,439 --> 02:27:43,200
name for many things like the component,
2964
02:27:40,240 --> 02:27:45,840
the props, and the data. This keeps
2965
02:27:43,200 --> 02:27:47,439
things simple and consistent. We don't
2966
02:27:45,840 --> 02:27:49,760
have to come up with different names
2967
02:27:47,439 --> 02:27:52,240
every time. And when we look at other
2968
02:27:49,760 --> 02:27:54,399
people's React code, the names will be
2969
02:27:52,240 --> 02:27:56,399
the same.
2970
02:27:54,399 --> 02:27:58,960
So, don't worry. It might be a bit
2971
02:27:56,399 --> 02:28:01,600
confusing at first because many things
2972
02:27:58,960 --> 02:28:03,600
use the same name. The more you work
2973
02:28:01,600 --> 02:28:06,160
with React, the more you'll get used to
2974
02:28:03,600 --> 02:28:10,600
this naming convention, and we'll get
2975
02:28:06,160 --> 02:28:10,600
lots of practice in this course.
2976
02:28:10,720 --> 02:28:16,960
Now to access this prop, we're going to
2977
02:28:13,680 --> 02:28:20,560
scroll up
2978
02:28:16,960 --> 02:28:24,960
and up here we'll type curly bracket
2979
02:28:20,560 --> 02:28:27,120
and we'll get the chat messages prop.
2980
02:28:24,960 --> 02:28:30,240
And now this code has access to
2981
02:28:27,120 --> 02:28:34,560
everything that it needs. Let's save our
2982
02:28:30,240 --> 02:28:36,479
code and then check our website.
2983
02:28:34,560 --> 02:28:40,000
And you'll notice that everything still
2984
02:28:36,479 --> 02:28:43,040
works. We just lifted the state up into
2985
02:28:40,000 --> 02:28:48,200
the app component and shared it between
2986
02:28:43,040 --> 02:28:48,200
chat input and chat messages.
2987
02:28:50,800 --> 02:28:56,800
Let's go back to our code
2988
02:28:53,760 --> 02:28:59,439
and we're going to scroll up to the chat
2989
02:28:56,800 --> 02:29:02,560
input component.
2990
02:28:59,439 --> 02:29:06,240
So now that chat input has access to set
2991
02:29:02,560 --> 02:29:10,000
chat messages, let's use it to create a
2992
02:29:06,240 --> 02:29:12,880
new chat message. So when we click send,
2993
02:29:10,000 --> 02:29:15,120
we're going to run set chat messages and
2994
02:29:12,880 --> 02:29:18,240
we're going to give it a copy of the
2995
02:29:15,120 --> 02:29:22,160
chat messages array with a new message
2996
02:29:18,240 --> 02:29:24,560
at the end. In this new message, instead
2997
02:29:22,160 --> 02:29:27,439
of test every time, we're going to use
2998
02:29:24,560 --> 02:29:30,560
the text inside the text box, which is
2999
02:29:27,439 --> 02:29:36,720
saved in input text. So, we'll remove
3000
02:29:30,560 --> 02:29:39,439
this and replace it with input text.
3001
02:29:36,720 --> 02:29:41,760
The sender will be the user because we
3002
02:29:39,439 --> 02:29:45,680
sent this message. And we'll give the
3003
02:29:41,760 --> 02:29:48,399
message a unique ID. And that's it. This
3004
02:29:45,680 --> 02:29:51,760
will update the chat messages array with
3005
02:29:48,399 --> 02:29:54,960
this new message. And because we use the
3006
02:29:51,760 --> 02:29:56,800
updater function, React will also update
3007
02:29:54,960 --> 02:29:59,439
the HTML.
3008
02:29:56,800 --> 02:30:02,080
So, let's save.
3009
02:29:59,439 --> 02:30:06,160
And we'll go to our project.
3010
02:30:02,080 --> 02:30:09,359
Inside this text box, let's type hello
3011
02:30:06,160 --> 02:30:12,160
and press send.
3012
02:30:09,359 --> 02:30:14,720
It will take the text in the text box,
3013
02:30:12,160 --> 02:30:18,600
create a new chat message, and it will
3014
02:30:14,720 --> 02:30:18,600
update the HTML.
3015
02:30:19,600 --> 02:30:25,760
Next, there is one improvement that we
3016
02:30:22,000 --> 02:30:28,160
can make at this point. If we scroll up
3017
02:30:25,760 --> 02:30:31,120
after we press send, we should also
3018
02:30:28,160 --> 02:30:33,120
remove the text in the text box. So, we
3019
02:30:31,120 --> 02:30:35,920
don't have to manually remove it if we
3020
02:30:33,120 --> 02:30:37,680
want to send another message.
3021
02:30:35,920 --> 02:30:42,000
To do that, we're going to learn a
3022
02:30:37,680 --> 02:30:45,280
feature called controlled inputs.
3023
02:30:42,000 --> 02:30:48,560
Let's go back to our code.
3024
02:30:45,280 --> 02:30:50,640
And after adding a new message, we're
3025
02:30:48,560 --> 02:30:54,240
going to remove the text in the text
3026
02:30:50,640 --> 02:30:59,200
box. So after this, let's use the
3027
02:30:54,240 --> 02:31:00,960
updater function. Set input text
3028
02:30:59,200 --> 02:31:03,840
brackets
3029
02:31:00,960 --> 02:31:05,280
will change the input text to the empty
3030
02:31:03,840 --> 02:31:08,319
string.
3031
02:31:05,280 --> 02:31:11,600
So this will update the input text up
3032
02:31:08,319 --> 02:31:14,640
here to be empty.
3033
02:31:11,600 --> 02:31:16,640
However, it will not update the HTML
3034
02:31:14,640 --> 02:31:19,840
yet.
3035
02:31:16,640 --> 02:31:24,000
To update the HTML, we need to use this
3036
02:31:19,840 --> 02:31:25,920
input text inside this HTML.
3037
02:31:24,000 --> 02:31:30,800
To do that, we're going to give the
3038
02:31:25,920 --> 02:31:34,240
input element a prop called value equals
3039
02:31:30,800 --> 02:31:37,359
curly brackets.
3040
02:31:34,240 --> 02:31:40,160
So value lets us change the value of
3041
02:31:37,359 --> 02:31:42,399
this input or the text inside this
3042
02:31:40,160 --> 02:31:44,560
input.
3043
02:31:42,399 --> 02:31:48,479
So we're going to set the value to
3044
02:31:44,560 --> 02:31:51,120
whatever input text is.
3045
02:31:48,479 --> 02:31:54,399
So now if we set the input text to be
3046
02:31:51,120 --> 02:31:56,960
empty, this will set the value to be
3047
02:31:54,399 --> 02:31:59,600
empty as well and it will make the text
3048
02:31:56,960 --> 02:32:01,600
box empty.
3049
02:31:59,600 --> 02:32:04,240
Let's save.
3050
02:32:01,600 --> 02:32:07,200
We'll go back to our website.
3051
02:32:04,240 --> 02:32:10,960
Inside the text box, we're going to type
3052
02:32:07,200 --> 02:32:12,880
hello and press send.
3053
02:32:10,960 --> 02:32:16,399
And you'll notice that it makes the text
3054
02:32:12,880 --> 02:32:19,280
box empty after we send. So let's go
3055
02:32:16,399 --> 02:32:22,800
back to our code.
3056
02:32:19,280 --> 02:32:26,240
And this is called a controlled input.
3057
02:32:22,800 --> 02:32:29,280
Using the value prop, we can control the
3058
02:32:26,240 --> 02:32:32,280
text inside this text box using our
3059
02:32:29,280 --> 02:32:32,280
code.
3060
02:32:32,399 --> 02:32:38,160
The last feature we're going to work on
3061
02:32:33,920 --> 02:32:40,720
in this lesson is if we scroll up after
3062
02:32:38,160 --> 02:32:44,920
we send a message, we're going to get a
3063
02:32:40,720 --> 02:32:44,920
response from the chatbot.
3064
02:32:45,120 --> 02:32:50,479
To get a response, we're going to use
3065
02:32:47,359 --> 02:32:53,600
another external library. So, we'll
3066
02:32:50,479 --> 02:32:56,319
scroll up to the script elements up
3067
02:32:53,600 --> 02:32:58,560
here. And to add another external
3068
02:32:56,319 --> 02:33:00,960
library, we're going to add another
3069
02:32:58,560 --> 02:33:02,880
script element.
3070
02:33:00,960 --> 02:33:07,359
And we're going to give this element the
3071
02:33:02,880 --> 02:33:09,520
source attribute equals double quotes.
3072
02:33:07,359 --> 02:33:12,479
And we're going to give it the URL of
3073
02:33:09,520 --> 02:33:15,280
the external library. Now, this is
3074
02:33:12,479 --> 02:33:17,680
another external library that I wrote.
3075
02:33:15,280 --> 02:33:20,640
So, let's just copy one of these URLs up
3076
02:33:17,680 --> 02:33:22,479
here with super simple dev. And we'll
3077
02:33:20,640 --> 02:33:25,840
copy.
3078
02:33:22,479 --> 02:33:27,680
And inside here, we're going to paste.
3079
02:33:25,840 --> 02:33:29,359
And then at the end, instead of
3080
02:33:27,680 --> 02:33:33,200
react.js,
3081
02:33:29,359 --> 02:33:36,000
we're going to load chatbot.js
3082
02:33:33,200 --> 02:33:38,240
and save.
3083
02:33:36,000 --> 02:33:40,080
So now, let's take a look at the code in
3084
02:33:38,240 --> 02:33:42,479
this URL.
3085
02:33:40,080 --> 02:33:44,640
On Windows, you can press control and
3086
02:33:42,479 --> 02:33:47,840
click this link. And on Mac, you can
3087
02:33:44,640 --> 02:33:49,920
press command and click this link.
3088
02:33:47,840 --> 02:33:52,960
If it doesn't open, you can also type
3089
02:33:49,920 --> 02:33:55,760
the URL in the browser.
3090
02:33:52,960 --> 02:33:58,240
So again, an external library is just
3091
02:33:55,760 --> 02:34:00,800
normal JavaScript code that someone else
3092
02:33:58,240 --> 02:34:03,680
wrote. In this case, it's some code that
3093
02:34:00,800 --> 02:34:06,880
I wrote. And this JavaScript code
3094
02:34:03,680 --> 02:34:09,680
creates a simple chatbot.
3095
02:34:06,880 --> 02:34:12,880
This is an object and it has a method
3096
02:34:09,680 --> 02:34:15,439
called get response.
3097
02:34:12,880 --> 02:34:19,920
We just have to give it our message and
3098
02:34:15,439 --> 02:34:23,600
it will return a response.
3099
02:34:19,920 --> 02:34:26,800
So now let's use this external library.
3100
02:34:23,600 --> 02:34:30,319
We'll go back to VS Code
3101
02:34:26,800 --> 02:34:35,120
and we'll scroll down to chat input. And
3102
02:34:30,319 --> 02:34:38,000
now after we send a message below this,
3103
02:34:35,120 --> 02:34:40,960
let's use the chatbot external library
3104
02:34:38,000 --> 02:34:43,600
to get a response. So the external
3105
02:34:40,960 --> 02:34:47,040
library creates a variable called
3106
02:34:43,600 --> 02:34:49,760
capital C chatbot.
3107
02:34:47,040 --> 02:34:56,319
This is an object and it has a method
3108
02:34:49,760 --> 02:34:58,240
called dot get response and brackets.
3109
02:34:56,319 --> 02:35:00,640
Between the brackets, we're just going
3110
02:34:58,240 --> 02:35:04,319
to put the message that we sent, which
3111
02:35:00,640 --> 02:35:07,040
is input text. So in here, we'll type
3112
02:35:04,319 --> 02:35:10,640
input text
3113
02:35:07,040 --> 02:35:13,280
and get response will return a response.
3114
02:35:10,640 --> 02:35:16,720
So let's save it in a variable using
3115
02:35:13,280 --> 02:35:20,960
const and we'll name it response.
3116
02:35:16,720 --> 02:35:23,439
equals this result. And next, let's just
3117
02:35:20,960 --> 02:35:26,720
console.log the response to see what it
3118
02:35:23,439 --> 02:35:29,359
looks like. console.log
3119
02:35:26,720 --> 02:35:32,160
the response
3120
02:35:29,359 --> 02:35:35,280
and save.
3121
02:35:32,160 --> 02:35:37,200
And now let's go to our website
3122
02:35:35,280 --> 02:35:39,920
and we're going to close the external
3123
02:35:37,200 --> 02:35:42,560
library for now. And then we're going to
3124
02:35:39,920 --> 02:35:47,680
click this text box and we'll type the
3125
02:35:42,560 --> 02:35:50,080
message hello and press send.
3126
02:35:47,680 --> 02:35:52,800
And now inside our console it will
3127
02:35:50,080 --> 02:35:55,760
display the response from the chatbot.
3128
02:35:52,800 --> 02:35:58,960
And the chatbot responded with hello how
3129
02:35:55,760 --> 02:36:01,520
can I help you? So using the chatbot
3130
02:35:58,960 --> 02:36:05,800
external library we're able to get a
3131
02:36:01,520 --> 02:36:05,800
response from the chatbot.
3132
02:36:05,840 --> 02:36:12,800
The last step is we're going to display
3133
02:36:08,080 --> 02:36:16,399
this chatbot response on the website.
3134
02:36:12,800 --> 02:36:18,479
Let's go back to VS Code.
3135
02:36:16,399 --> 02:36:21,280
And to display the response on the
3136
02:36:18,479 --> 02:36:25,200
website, we can just add another chat
3137
02:36:21,280 --> 02:36:27,600
message using set chat messages.
3138
02:36:25,200 --> 02:36:30,479
Because this is an updater function from
3139
02:36:27,600 --> 02:36:34,319
the state, React will automatically
3140
02:36:30,479 --> 02:36:38,200
update the website as well. So down here
3141
02:36:34,319 --> 02:36:38,200
we'll remove console.log
3142
02:36:38,319 --> 02:36:43,840
and let's just copy paste this code up
3143
02:36:40,880 --> 02:36:49,600
here to create a new message. So we'll
3144
02:36:43,840 --> 02:36:52,240
copy and then down here we'll paste
3145
02:36:49,600 --> 02:36:54,640
and here the message should be the
3146
02:36:52,240 --> 02:36:58,319
response from the chatbot. So we'll
3147
02:36:54,640 --> 02:37:01,439
replace this with response
3148
02:36:58,319 --> 02:37:04,080
and the sender is going to be the robot
3149
02:37:01,439 --> 02:37:06,160
this time. and we'll give it a unique
3150
02:37:04,080 --> 02:37:09,120
ID.
3151
02:37:06,160 --> 02:37:12,479
So now let's save this code and try it
3152
02:37:09,120 --> 02:37:16,560
out. We'll open the website and then in
3153
02:37:12,479 --> 02:37:19,359
the text box we'll type hello and press
3154
02:37:16,560 --> 02:37:22,240
send.
3155
02:37:19,359 --> 02:37:24,560
And if we look at the website, it sort
3156
02:37:22,240 --> 02:37:28,000
of worked. It displayed the response
3157
02:37:24,560 --> 02:37:30,479
from the chatbot on the website.
3158
02:37:28,000 --> 02:37:33,760
However, it also removed our hello
3159
02:37:30,479 --> 02:37:36,960
message. So why did this happen? Let's
3160
02:37:33,760 --> 02:37:39,920
go back to VS Code.
3161
02:37:36,960 --> 02:37:43,200
And if we look at this code
3162
02:37:39,920 --> 02:37:46,319
here, we updated chat messages by making
3163
02:37:43,200 --> 02:37:50,560
a copy and adding a new message at the
3164
02:37:46,319 --> 02:37:54,240
end. However, in React, state does not
3165
02:37:50,560 --> 02:37:59,080
update immediately. The state is updated
3166
02:37:54,240 --> 02:37:59,080
after all of the code is finished.
3167
02:37:59,520 --> 02:38:06,560
So here all of the code is not finished
3168
02:38:02,800 --> 02:38:09,520
yet. So chat messages is not updated to
3169
02:38:06,560 --> 02:38:11,520
this new array immediately.
3170
02:38:09,520 --> 02:38:15,120
So when we get to this point in the
3171
02:38:11,520 --> 02:38:17,600
code, this chat messages is still the
3172
02:38:15,120 --> 02:38:20,399
old array without the new message that
3173
02:38:17,600 --> 02:38:22,960
we added up here. And then we take the
3174
02:38:20,399 --> 02:38:26,160
old array and we add the chatbots
3175
02:38:22,960 --> 02:38:28,080
response. And we essentially lost our
3176
02:38:26,160 --> 02:38:30,960
message.
3177
02:38:28,080 --> 02:38:33,520
So one way to fix this issue is to save
3178
02:38:30,960 --> 02:38:37,120
this updated chat messages in a
3179
02:38:33,520 --> 02:38:40,479
variable. So above this we're going to
3180
02:38:37,120 --> 02:38:45,280
create a new variable using const and
3181
02:38:40,479 --> 02:38:47,040
let's name it new chat messages
3182
02:38:45,280 --> 02:38:49,600
equals
3183
02:38:47,040 --> 02:38:52,080
and we're going to save this value. So,
3184
02:38:49,600 --> 02:38:54,399
we'll select this code
3185
02:38:52,080 --> 02:38:59,560
and then cut.
3186
02:38:54,399 --> 02:38:59,560
And up here, we're going to paste.
3187
02:39:00,000 --> 02:39:07,680
And we'll set the chat messages to this
3188
02:39:02,240 --> 02:39:10,399
variable, new chat messages.
3189
02:39:07,680 --> 02:39:13,520
So, this does the same thing as before,
3190
02:39:10,399 --> 02:39:15,359
except we saved the new chat messages in
3191
02:39:13,520 --> 02:39:18,080
this variable.
3192
02:39:15,359 --> 02:39:20,800
And then down here, instead of making a
3193
02:39:18,080 --> 02:39:25,760
copy of chat messages, we're going to
3194
02:39:20,800 --> 02:39:28,000
make a copy of new chat messages, which
3195
02:39:25,760 --> 02:39:31,520
contains our new message that we just
3196
02:39:28,000 --> 02:39:33,920
added. So now it will have our message,
3197
02:39:31,520 --> 02:39:38,080
and we're going to add the chatbots
3198
02:39:33,920 --> 02:39:41,120
response. So now let's save this code
3199
02:39:38,080 --> 02:39:44,080
and go to our website.
3200
02:39:41,120 --> 02:39:48,080
In the text box, we're going to type
3201
02:39:44,080 --> 02:39:50,960
hello and press send.
3202
02:39:48,080 --> 02:39:53,840
And it displays our message, hello, and
3203
02:39:50,960 --> 02:39:56,399
the chatbot's response, hello, how can I
3204
02:39:53,840 --> 02:39:59,040
help you?
3205
02:39:56,399 --> 02:40:05,520
Let's try another example. In the text
3206
02:39:59,040 --> 02:40:08,240
box, we can type, "Can you flip a coin?"
3207
02:40:05,520 --> 02:40:11,280
and press send.
3208
02:40:08,240 --> 02:40:13,280
It will add and display our message and
3209
02:40:11,280 --> 02:40:16,399
it will add and display the chatbot's
3210
02:40:13,280 --> 02:40:19,120
response which is sure you got heads or
3211
02:40:16,399 --> 02:40:20,800
tails and everything is working
3212
02:40:19,120 --> 02:40:24,000
correctly.
3213
02:40:20,800 --> 02:40:26,479
So as you can see state is another way
3214
02:40:24,000 --> 02:40:30,000
that React helps us create websites
3215
02:40:26,479 --> 02:40:32,720
easier. All we have to do is update the
3216
02:40:30,000 --> 02:40:35,200
state and React will automatically
3217
02:40:32,720 --> 02:40:40,120
update the website. We don't have to
3218
02:40:35,200 --> 02:40:40,120
update the website manually ourselves.
3219
02:40:41,439 --> 02:40:47,040
And that's it. Using state, we created
3220
02:40:44,560 --> 02:40:50,240
the functionality that we need for this
3221
02:40:47,040 --> 02:40:53,040
project. In the next lesson, we'll style
3222
02:40:50,240 --> 02:40:56,640
this with CSS to make it look like the
3223
02:40:53,040 --> 02:40:59,200
final chatbot project.
3224
02:40:56,640 --> 02:41:02,080
And that's the end of this lesson. In
3225
02:40:59,200 --> 02:41:06,399
this lesson, we saved the data for our
3226
02:41:02,080 --> 02:41:11,040
website using arrays and objects. We
3227
02:41:06,399 --> 02:41:14,399
generated the HTML using map and the key
3228
02:41:11,040 --> 02:41:18,319
prop. We made the website interactive
3229
02:41:14,399 --> 02:41:21,520
using on click and onchange. We learned
3230
02:41:18,319 --> 02:41:25,200
state which is data that changes over
3231
02:41:21,520 --> 02:41:28,240
time and is connected to the HTML. We
3232
02:41:25,200 --> 02:41:31,359
used the updater function to update the
3233
02:41:28,240 --> 02:41:34,880
state and update the HTML at the same
3234
02:41:31,359 --> 02:41:37,760
time. We learned the array destructuring
3235
02:41:34,880 --> 02:41:40,880
shortcut. We learned a technique called
3236
02:41:37,760 --> 02:41:44,160
lifting the state up which lets us share
3237
02:41:40,880 --> 02:41:47,200
state between components. We made the
3238
02:41:44,160 --> 02:41:50,560
chat input component interactive and
3239
02:41:47,200 --> 02:41:53,280
created new chat messages. And we got
3240
02:41:50,560 --> 02:41:55,520
responses from the chatbot.
3241
02:41:53,280 --> 02:41:57,760
Here
3242
02:41:55,520 --> 02:42:01,439
are some exercises to help you practice
3243
02:41:57,760 --> 02:42:01,439
what we learned in this lesson.
3244
02:42:46,560 --> 02:42:52,080
In this lesson, we're going to learn how
3245
02:42:48,399 --> 02:42:55,200
to use CSS with React and we'll finish
3246
02:42:52,080 --> 02:42:57,200
the chatbot project. Let's go back to
3247
02:42:55,200 --> 02:43:00,720
our code
3248
02:42:57,200 --> 02:43:04,160
and we'll start by reviewing CSS.
3249
02:43:00,720 --> 02:43:07,439
CSS is another language and it lets us
3250
02:43:04,160 --> 02:43:11,040
change the appearance of the website. To
3251
02:43:07,439 --> 02:43:14,399
use CSS with React, we just use CSS
3252
02:43:11,040 --> 02:43:18,000
normally. So, one way to use CSS is to
3253
02:43:14,399 --> 02:43:21,040
create a style element.
3254
02:43:18,000 --> 02:43:23,840
So, we'll scroll to the top of our code
3255
02:43:21,040 --> 02:43:29,359
to the head element and then inside
3256
02:43:23,840 --> 02:43:31,520
here, we'll create a style element.
3257
02:43:29,359 --> 02:43:34,000
So, the style element doesn't appear on
3258
02:43:31,520 --> 02:43:36,319
the website. That's why we put it in the
3259
02:43:34,000 --> 02:43:38,000
head section.
3260
02:43:36,319 --> 02:43:42,880
And the special thing about the style
3261
02:43:38,000 --> 02:43:46,800
element is we can write CSS code inside.
3262
02:43:42,880 --> 02:43:50,319
In here we'll type the CSS code button
3263
02:43:46,800 --> 02:43:55,120
and curly brackets. And inside we'll
3264
02:43:50,319 --> 02:43:57,920
type background dash color
3265
02:43:55,120 --> 02:44:01,680
green and semicolon.
3266
02:43:57,920 --> 02:44:04,560
So this is some CSS code. As a review,
3267
02:44:01,680 --> 02:44:07,680
this code at the front is called the CSS
3268
02:44:04,560 --> 02:44:09,439
selector. This tells a computer which
3269
02:44:07,680 --> 02:44:13,200
elements we want to change the
3270
02:44:09,439 --> 02:44:16,319
appearance. And inside we have CSS
3271
02:44:13,200 --> 02:44:19,840
styles. This tells a computer how to
3272
02:44:16,319 --> 02:44:22,240
change the appearance of these elements.
3273
02:44:19,840 --> 02:44:25,359
The left side is called the CSS
3274
02:44:22,240 --> 02:44:27,520
property. This is what we're changing.
3275
02:44:25,359 --> 02:44:29,840
And the right side is called the CSS
3276
02:44:27,520 --> 02:44:34,160
value. This is what we're changing the
3277
02:44:29,840 --> 02:44:36,800
property to. So this code changes all
3278
02:44:34,160 --> 02:44:41,520
buttons on the website and changes their
3279
02:44:36,800 --> 02:44:45,200
background color to green. If we save
3280
02:44:41,520 --> 02:44:46,640
and then check our website,
3281
02:44:45,200 --> 02:44:50,560
you can see that it changed the
3282
02:44:46,640 --> 02:44:53,600
background color of the button to green.
3283
02:44:50,560 --> 02:44:56,960
Let's go back to our code.
3284
02:44:53,600 --> 02:45:00,080
And one problem here is button will
3285
02:44:56,960 --> 02:45:02,960
style all buttons on the website. But
3286
02:45:00,080 --> 02:45:06,880
usually we want to just style a specific
3287
02:45:02,960 --> 02:45:09,680
button. For example, let's scroll down
3288
02:45:06,880 --> 02:45:12,160
to the chat input component and we're
3289
02:45:09,680 --> 02:45:15,200
going to scroll down to this send button
3290
02:45:12,160 --> 02:45:19,200
here. So let's say we just want to style
3291
02:45:15,200 --> 02:45:22,319
this send button. As a review to style a
3292
02:45:19,200 --> 02:45:26,479
specific element, we can add an HTML
3293
02:45:22,319 --> 02:45:28,640
attribute called class. In React to set
3294
02:45:26,479 --> 02:45:33,040
the class attribute, we're going to give
3295
02:45:28,640 --> 02:45:38,399
this element a prop called class capital
3296
02:45:33,040 --> 02:45:41,680
N name equals double quotes.
3297
02:45:38,399 --> 02:45:43,439
And we'll give it the class send dash
3298
02:45:41,680 --> 02:45:46,560
button.
3299
02:45:43,439 --> 02:45:49,600
In React, we use class name to set the
3300
02:45:46,560 --> 02:45:52,479
class. That's because React is actually
3301
02:45:49,600 --> 02:45:55,600
just JavaScript code and JavaScript
3302
02:45:52,479 --> 02:45:58,319
already has a feature called class. So
3303
02:45:55,600 --> 02:46:01,840
class is a reserved word and we can't
3304
02:45:58,319 --> 02:46:04,399
use it. That's why React uses class name
3305
02:46:01,840 --> 02:46:06,560
to set the class.
3306
02:46:04,399 --> 02:46:08,080
Now that this button has the class
3307
02:46:06,560 --> 02:46:11,279
send-button,
3308
02:46:08,080 --> 02:46:15,000
we can style this specific element.
3309
02:46:11,279 --> 02:46:15,000
We'll go to our CSS
3310
02:46:15,520 --> 02:46:22,080
and in here we're going to remove button
3311
02:46:18,880 --> 02:46:26,319
and we're going to type dot and then the
3312
02:46:22,080 --> 02:46:28,800
class send dash button.
3313
02:46:26,319 --> 02:46:31,920
As a review, if we start the CSS
3314
02:46:28,800 --> 02:46:35,600
selector with a dot, it will only style
3315
02:46:31,920 --> 02:46:37,760
elements with this class. So this will
3316
02:46:35,600 --> 02:46:42,080
only style elements with the class
3317
02:46:37,760 --> 02:46:44,800
send-ash button. If we save
3318
02:46:42,080 --> 02:46:47,200
and check our website,
3319
02:46:44,800 --> 02:46:50,640
it styles our send button because we
3320
02:46:47,200 --> 02:46:52,880
gave it the send-button class.
3321
02:46:50,640 --> 02:46:55,920
If we open the console and go to the
3322
02:46:52,880 --> 02:46:59,920
elements tab and then we look at our
3323
02:46:55,920 --> 02:47:02,000
HTML here and we find the send button,
3324
02:46:59,920 --> 02:47:05,760
you'll notice that it has the class
3325
02:47:02,000 --> 02:47:10,319
attribute. So class name in React is
3326
02:47:05,760 --> 02:47:13,359
converted to class in HTML.
3327
02:47:10,319 --> 02:47:16,399
Now that we reviewed CSS, we're going to
3328
02:47:13,359 --> 02:47:19,120
style this button to look like the final
3329
02:47:16,399 --> 02:47:21,520
design. So, first we're going to put the
3330
02:47:19,120 --> 02:47:24,720
final design and our website side by
3331
02:47:21,520 --> 02:47:27,760
side so it's easier to compare. So, I'll
3332
02:47:24,720 --> 02:47:30,479
take the final design here and I'll put
3333
02:47:27,760 --> 02:47:33,840
it on the bottom left and I'll take our
3334
02:47:30,479 --> 02:47:37,279
website and put it in the top left. And
3335
02:47:33,840 --> 02:47:42,240
I'm going to resize our code editor
3336
02:47:37,279 --> 02:47:45,680
so we can see everything side by side.
3337
02:47:42,240 --> 02:47:48,800
And I'll hide the sidebar.
3338
02:47:45,680 --> 02:47:51,359
Next, to style our send button to look
3339
02:47:48,800 --> 02:47:55,359
like the final project, we just have to
3340
02:47:51,359 --> 02:47:58,240
style it one by one with CSS. So, we
3341
02:47:55,359 --> 02:48:01,439
already made the background color green.
3342
02:47:58,240 --> 02:48:04,880
Next, let's make the text color white.
3343
02:48:01,439 --> 02:48:09,920
To do that, we'll type the CSS property
3344
02:48:04,880 --> 02:48:12,560
color colon and set it to white and
3345
02:48:09,920 --> 02:48:16,000
save.
3346
02:48:12,560 --> 02:48:19,520
And that will make the text color white.
3347
02:48:16,000 --> 02:48:22,319
Next, let's make our button bigger. To
3348
02:48:19,520 --> 02:48:26,080
do that, we'll add space on the inside
3349
02:48:22,319 --> 02:48:29,439
of the button. As a review, space on the
3350
02:48:26,080 --> 02:48:34,160
inside of an element is called padding.
3351
02:48:29,439 --> 02:48:36,640
So in our CSS we're going to add padding
3352
02:48:34,160 --> 02:48:42,640
colon and we'll give the padding two
3353
02:48:36,640 --> 02:48:46,160
values 12 px and 20 px.
3354
02:48:42,640 --> 02:48:48,479
So the px here stands for pixels. A
3355
02:48:46,160 --> 02:48:51,520
pixel is a unit of measurement in
3356
02:48:48,479 --> 02:48:55,920
computers. For example, my computer
3357
02:48:51,520 --> 02:48:59,760
screen is about 3,800 pixels wide. So 12
3358
02:48:55,920 --> 02:49:02,479
pixels and 20 pixels looks like this.
3359
02:48:59,760 --> 02:49:06,000
The first value is the vertical space
3360
02:49:02,479 --> 02:49:08,800
inside this button. And the second value
3361
02:49:06,000 --> 02:49:11,279
is the horizontal space inside this
3362
02:49:08,800 --> 02:49:14,160
button. So we're going to add space all
3363
02:49:11,279 --> 02:49:16,800
around the inside of this button. So
3364
02:49:14,160 --> 02:49:19,760
let's save.
3365
02:49:16,800 --> 02:49:22,800
And it adds space on the inside.
3366
02:49:19,760 --> 02:49:25,279
And I think that looks close enough.
3367
02:49:22,800 --> 02:49:28,560
Next, let's add some space on the
3368
02:49:25,279 --> 02:49:31,040
outside of this button on the left. As a
3369
02:49:28,560 --> 02:49:33,920
review, space on the outside of an
3370
02:49:31,040 --> 02:49:38,720
element is called margin.
3371
02:49:33,920 --> 02:49:42,800
So, in our CSS, we'll add margin dash
3372
02:49:38,720 --> 02:49:48,399
and we'll add it on the left colon. And
3373
02:49:42,800 --> 02:49:51,279
let's add 10 pixels of margin. And save.
3374
02:49:48,399 --> 02:49:54,160
And that looks close enough.
3375
02:49:51,279 --> 02:49:56,640
Next, our button has a border around the
3376
02:49:54,160 --> 02:49:59,680
button, but in the final design, there
3377
02:49:56,640 --> 02:50:03,760
is no border. To remove the borders,
3378
02:49:59,680 --> 02:50:08,720
we'll type the property border colon and
3379
02:50:03,760 --> 02:50:11,920
set it to none and save.
3380
02:50:08,720 --> 02:50:14,720
And that will remove the borders.
3381
02:50:11,920 --> 02:50:18,000
Next, the corners of the button in the
3382
02:50:14,720 --> 02:50:20,080
final design are round. To make our
3383
02:50:18,000 --> 02:50:26,319
corners round, we're going to use the
3384
02:50:20,080 --> 02:50:28,720
CSS property border dash radius colon
3385
02:50:26,319 --> 02:50:35,439
and then how round we want the corners
3386
02:50:28,720 --> 02:50:38,800
to be. Let's try 10 pixels and save.
3387
02:50:35,439 --> 02:50:42,000
And I think that looks close enough.
3388
02:50:38,800 --> 02:50:44,640
Next, the text in the button is a little
3389
02:50:42,000 --> 02:50:47,439
bigger in the final design. To change
3390
02:50:44,640 --> 02:50:52,880
the size of the text, we'll use the CSS
3391
02:50:47,439 --> 02:50:58,479
property font dash size colon and let's
3392
02:50:52,880 --> 02:51:01,920
try 15 pixels and save.
3393
02:50:58,479 --> 02:51:04,560
And that looks pretty good. Next, if we
3394
02:51:01,920 --> 02:51:07,279
put our mouse over our button, we no
3395
02:51:04,560 --> 02:51:10,800
longer have the hand pointer icon that
3396
02:51:07,279 --> 02:51:13,840
we see here. to use this hand pointer
3397
02:51:10,800 --> 02:51:18,640
icon. We're going to set the property
3398
02:51:13,840 --> 02:51:21,120
cursor colon pointer
3399
02:51:18,640 --> 02:51:23,200
and save.
3400
02:51:21,120 --> 02:51:25,760
And now if we put our mouse over the
3401
02:51:23,200 --> 02:51:27,359
button, we have the hand pointer icon
3402
02:51:25,760 --> 02:51:29,840
again.
3403
02:51:27,359 --> 02:51:32,240
Finally, the background color of our
3404
02:51:29,840 --> 02:51:35,760
button is a little different from the
3405
02:51:32,240 --> 02:51:38,640
final design. So to get this exact green
3406
02:51:35,760 --> 02:51:40,479
color, we can go to the final project.
3407
02:51:38,640 --> 02:51:45,120
And then we're going to rightclick this
3408
02:51:40,479 --> 02:51:47,760
button and then click inspect.
3409
02:51:45,120 --> 02:51:51,359
And we can click these three dots here
3410
02:51:47,760 --> 02:51:54,000
and click this icon to open the console
3411
02:51:51,359 --> 02:51:55,600
in a different window.
3412
02:51:54,000 --> 02:51:58,399
And we're going to go to the elements
3413
02:51:55,600 --> 02:52:00,720
tab and we'll find the send button in
3414
02:51:58,399 --> 02:52:03,600
the HTML. And we're going to look at
3415
02:52:00,720 --> 02:52:06,319
these styles. So you can see that this
3416
02:52:03,600 --> 02:52:09,840
button here has a background color of
3417
02:52:06,319 --> 02:52:12,880
this RGB value. So we're going to select
3418
02:52:09,840 --> 02:52:14,560
this RGB value and we're going to copy
3419
02:52:12,880 --> 02:52:17,439
it.
3420
02:52:14,560 --> 02:52:21,200
And in our code, instead of the color
3421
02:52:17,439 --> 02:52:22,800
green, we're going to paste this RGB
3422
02:52:21,200 --> 02:52:27,040
color.
3423
02:52:22,800 --> 02:52:30,080
As a review, RGB stands for red, green,
3424
02:52:27,040 --> 02:52:33,040
blue, and it's a way to measure color in
3425
02:52:30,080 --> 02:52:36,080
computers. Computers can create any
3426
02:52:33,040 --> 02:52:39,600
color using a combination of red, green,
3427
02:52:36,080 --> 02:52:42,399
and blue. The first value here is how
3428
02:52:39,600 --> 02:52:44,640
much red we want in the color. The
3429
02:52:42,399 --> 02:52:47,279
second value is how much green we want
3430
02:52:44,640 --> 02:52:50,319
in the color. And the third value is how
3431
02:52:47,279 --> 02:52:52,880
much blue we want. And each value goes
3432
02:52:50,319 --> 02:52:55,520
from 0 to 255.
3433
02:52:52,880 --> 02:52:59,040
So this color combines a little bit of
3434
02:52:55,520 --> 02:53:02,560
red, a lot of green, and a bit of blue
3435
02:52:59,040 --> 02:53:04,960
to get this exact green color. So let's
3436
02:53:02,560 --> 02:53:07,520
save our code.
3437
02:53:04,960 --> 02:53:09,439
And now our button matches the final
3438
02:53:07,520 --> 02:53:12,080
project.
3439
02:53:09,439 --> 02:53:15,600
And that's how we style the send button.
3440
02:53:12,080 --> 02:53:18,720
We just style it one by one using CSS
3441
02:53:15,600 --> 02:53:21,600
until it looks like the final design. So
3442
02:53:18,720 --> 02:53:24,080
this is just a review of CSS. If you
3443
02:53:21,600 --> 02:53:26,960
want to learn CSS in more detail, you
3444
02:53:24,080 --> 02:53:31,200
can check out my HTML and CSS full
3445
02:53:26,960 --> 02:53:34,000
course in the video description.
3446
02:53:31,200 --> 02:53:37,279
Next, we're going to style this text box
3447
02:53:34,000 --> 02:53:39,600
to look like the final design. Again,
3448
02:53:37,279 --> 02:53:41,760
we're just going to style it one by one
3449
02:53:39,600 --> 02:53:44,160
with CSS.
3450
02:53:41,760 --> 02:53:47,760
First, we need to add a class to this
3451
02:53:44,160 --> 02:53:49,920
element so we can select it in our CSS.
3452
02:53:47,760 --> 02:53:52,479
We'll go to our code and we're going to
3453
02:53:49,920 --> 02:53:56,479
scroll down to this text box which is
3454
02:53:52,479 --> 02:53:59,920
inside the chat input component and it's
3455
02:53:56,479 --> 02:54:03,439
this input element here. So to add a
3456
02:53:59,920 --> 02:54:06,479
class we're going to use class name. So
3457
02:54:03,439 --> 02:54:10,720
inside the input element we'll add a
3458
02:54:06,479 --> 02:54:16,080
class name equals double quotes and
3459
02:54:10,720 --> 02:54:20,399
let's give it the class chat dash input.
3460
02:54:16,080 --> 02:54:22,960
Now, let's scroll back up to the CSS.
3461
02:54:20,399 --> 02:54:25,920
And below these styles,
3462
02:54:22,960 --> 02:54:27,760
we're going to type dot to select a
3463
02:54:25,920 --> 02:54:31,439
class. And we're going to select the
3464
02:54:27,760 --> 02:54:35,439
class we just added, which is chat dash
3465
02:54:31,439 --> 02:54:38,399
input and curly brackets. And now we're
3466
02:54:35,439 --> 02:54:40,399
ready to style the text box.
3467
02:54:38,399 --> 02:54:42,880
The first thing we'll change is there's
3468
02:54:40,399 --> 02:54:46,319
a lot of space on the inside of this
3469
02:54:42,880 --> 02:54:48,640
text box in the final design. So again,
3470
02:54:46,319 --> 02:54:51,200
space on the inside of an element is
3471
02:54:48,640 --> 02:54:56,399
called padding.
3472
02:54:51,200 --> 02:54:59,520
In our CSS, we'll add padding colon. And
3473
02:54:56,399 --> 02:55:03,840
let's do 12 pixels for the vertical
3474
02:54:59,520 --> 02:55:08,319
space and 15 pixels for the horizontal
3475
02:55:03,840 --> 02:55:10,880
space. Now let's save.
3476
02:55:08,319 --> 02:55:13,439
And that adds space on the inside of our
3477
02:55:10,880 --> 02:55:16,560
text box. And I think that looks close
3478
02:55:13,439 --> 02:55:21,760
enough. Next, we'll make the corners
3479
02:55:16,560 --> 02:55:27,120
round. To do that, we'll use border dash
3480
02:55:21,760 --> 02:55:29,520
radius colon. And let's try 10 pixels
3481
02:55:27,120 --> 02:55:32,960
and save.
3482
02:55:29,520 --> 02:55:35,279
And that looks pretty good. Next, our
3483
02:55:32,960 --> 02:55:38,479
border is too thick compared to the
3484
02:55:35,279 --> 02:55:44,080
final design. To change the thickness of
3485
02:55:38,479 --> 02:55:48,160
the border, we'll use border dash width
3486
02:55:44,080 --> 02:55:50,720
colon. And let's change it to 1 pixel
3487
02:55:48,160 --> 02:55:53,840
and save.
3488
02:55:50,720 --> 02:55:56,960
So that looks pretty good. Finally, the
3489
02:55:53,840 --> 02:56:00,000
text is bigger in the final design. To
3490
02:55:56,960 --> 02:56:05,040
change the size of the text, we can use
3491
02:56:00,000 --> 02:56:08,640
font dash size colon. And let's use 15
3492
02:56:05,040 --> 02:56:11,680
pixels again. So 15px
3493
02:56:08,640 --> 02:56:13,600
and save.
3494
02:56:11,680 --> 02:56:17,200
And I think that looks good enough for
3495
02:56:13,600 --> 02:56:21,920
now. So we styled the text box one by
3496
02:56:17,200 --> 02:56:25,279
one to look like the final design.
3497
02:56:21,920 --> 02:56:28,640
Next in the final design, the text box
3498
02:56:25,279 --> 02:56:31,840
is wider. And this text box is also
3499
02:56:28,640 --> 02:56:36,640
flexible. That means if we resize the
3500
02:56:31,840 --> 02:56:39,439
browser, the text box will also resize.
3501
02:56:36,640 --> 02:56:42,160
To make our text box flexible, we're
3502
02:56:39,439 --> 02:56:43,760
going to review a feature of CSS called
3503
02:56:42,160 --> 02:56:46,800
flexbox.
3504
02:56:43,760 --> 02:56:50,479
Flexbox lets us create a flexible
3505
02:56:46,800 --> 02:56:53,920
layout. Layout means how the elements
3506
02:56:50,479 --> 02:56:57,120
are positioned on the website.
3507
02:56:53,920 --> 02:57:00,000
Here we have a horizontal layout where
3508
02:56:57,120 --> 02:57:03,359
the elements are beside each other. And
3509
02:57:00,000 --> 02:57:06,880
also this element is flexible. So
3510
02:57:03,359 --> 02:57:10,240
flexbox helps us create this flexible
3511
02:57:06,880 --> 02:57:13,040
layout. To use flexbox we need to do
3512
02:57:10,240 --> 02:57:16,080
three steps. The first step is to create
3513
02:57:13,040 --> 02:57:18,640
a container around the elements that we
3514
02:57:16,080 --> 02:57:22,080
want to position. So let's go back to
3515
02:57:18,640 --> 02:57:25,200
our code and we're going to scroll down
3516
02:57:22,080 --> 02:57:27,920
to the chat input component and we'll
3517
02:57:25,200 --> 02:57:30,960
scroll down to the HTML.
3518
02:57:27,920 --> 02:57:34,080
So here we want to position the text box
3519
02:57:30,960 --> 02:57:37,120
and the button. So we need to create a
3520
02:57:34,080 --> 02:57:40,000
container around these two elements.
3521
02:57:37,120 --> 02:57:43,200
Right now we have a fragment around
3522
02:57:40,000 --> 02:57:46,160
these two elements. However, we cannot
3523
02:57:43,200 --> 02:57:48,560
style a fragment with CSS since
3524
02:57:46,160 --> 02:57:50,640
fragments don't actually appear on the
3525
02:57:48,560 --> 02:57:52,479
website.
3526
02:57:50,640 --> 02:57:55,439
Instead, we're going to switch this
3527
02:57:52,479 --> 02:57:58,720
container to a div element because we
3528
02:57:55,439 --> 02:58:02,640
can style a div element. So, inside
3529
02:57:58,720 --> 02:58:06,560
here, we'll type div and at the end,
3530
02:58:02,640 --> 02:58:09,760
we'll do slash div to convert this
3531
02:58:06,560 --> 02:58:12,479
container to a div element.
3532
02:58:09,760 --> 02:58:14,080
And now we have a container around the
3533
02:58:12,479 --> 02:58:16,880
elements.
3534
02:58:14,080 --> 02:58:20,000
The second step is to add the property
3535
02:58:16,880 --> 02:58:22,479
display flex to the container.
3536
02:58:20,000 --> 02:58:24,720
First, let's add a class name to this
3537
02:58:22,479 --> 02:58:30,399
container so we can select it in our
3538
02:58:24,720 --> 02:58:33,200
CSS. In here, we'll type class name
3539
02:58:30,399 --> 02:58:36,960
equals double quotes and we'll give it
3540
02:58:33,200 --> 02:58:39,840
the class chat-input
3541
02:58:36,960 --> 02:58:44,200
dash container.
3542
02:58:39,840 --> 02:58:44,200
And then we'll scroll up to our CSS.
3543
02:58:45,040 --> 02:58:52,080
And at the end we'll type dot to select
3544
02:58:49,040 --> 02:58:54,640
a class and we'll select the class
3545
02:58:52,080 --> 02:58:59,359
chat-input
3546
02:58:54,640 --> 02:59:02,080
dash container and curly brackets.
3547
02:58:59,359 --> 02:59:06,720
And finally we'll add the property
3548
02:59:02,080 --> 02:59:10,479
display colon flex and that will turn
3549
02:59:06,720 --> 02:59:13,200
this container into a flexbox.
3550
02:59:10,479 --> 02:59:16,479
The third step is to use flexbox
3551
02:59:13,200 --> 02:59:19,359
features. Flexbox has many features we
3552
02:59:16,479 --> 02:59:21,439
can use to control the layout or to
3553
02:59:19,359 --> 02:59:24,880
control how these elements are
3554
02:59:21,439 --> 02:59:29,040
positioned. One feature is called flex
3555
02:59:24,880 --> 02:59:32,880
grow. Flex grow makes an element grow
3556
02:59:29,040 --> 02:59:36,080
and take up the remaining space. So here
3557
02:59:32,880 --> 02:59:39,680
we want this text box to grow and take
3558
02:59:36,080 --> 02:59:41,840
up this remaining space.
3559
02:59:39,680 --> 02:59:45,040
So we'll go to the styles for the chat
3560
02:59:41,840 --> 02:59:48,479
input or the text box
3561
02:59:45,040 --> 02:59:50,160
and we'll add the property flex dash
3562
02:59:48,479 --> 02:59:54,800
grow
3563
02:59:50,160 --> 02:59:58,160
1 and save.
3564
02:59:54,800 --> 03:00:00,880
And now the text box will grow and take
3565
02:59:58,160 --> 03:00:03,600
up the remaining space.
3566
03:00:00,880 --> 03:00:06,160
If we resize the browser, the text box
3567
03:00:03,600 --> 03:00:08,080
will also resize to take up the
3568
03:00:06,160 --> 03:00:10,560
remaining space.
3569
03:00:08,080 --> 03:00:13,040
So now we created the layout for the
3570
03:00:10,560 --> 03:00:16,040
chat input that we see in the final
3571
03:00:13,040 --> 03:00:16,040
design.
3572
03:00:16,160 --> 03:00:22,479
Next we'll create the layout for the
3573
03:00:18,479 --> 03:00:25,200
overall app. Notice in the final project
3574
03:00:22,479 --> 03:00:28,800
if we resize the browser and make it
3575
03:00:25,200 --> 03:00:32,319
wider the app is limited to a certain
3576
03:00:28,800 --> 03:00:34,880
width. So it's not going to get wider.
3577
03:00:32,319 --> 03:00:38,720
To create this in our project, we'll set
3578
03:00:34,880 --> 03:00:41,359
a maximum width for our app.
3579
03:00:38,720 --> 03:00:44,800
So, let's go to our code and we're going
3580
03:00:41,359 --> 03:00:47,760
to scroll down to the app component,
3581
03:00:44,800 --> 03:00:49,920
which is down here, and we'll go down to
3582
03:00:47,760 --> 03:00:52,720
the HTML.
3583
03:00:49,920 --> 03:00:55,920
And currently, the app is contained in a
3584
03:00:52,720 --> 03:00:58,240
fragment. And remember, we cannot style
3585
03:00:55,920 --> 03:01:01,439
a fragment since fragments don't
3586
03:00:58,240 --> 03:01:03,680
actually appear on the website.
3587
03:01:01,439 --> 03:01:07,600
So again, we're going to switch this to
3588
03:01:03,680 --> 03:01:11,439
a div element so we can style it. So in
3589
03:01:07,600 --> 03:01:15,760
here, we'll type div and at the bottom
3590
03:01:11,439 --> 03:01:17,680
we'll type /ash div. And now we can
3591
03:01:15,760 --> 03:01:20,720
style this.
3592
03:01:17,680 --> 03:01:25,760
First we'll add a class to this div
3593
03:01:20,720 --> 03:01:29,279
using class name equals double quotes.
3594
03:01:25,760 --> 03:01:30,960
And let's give it the class app dash
3595
03:01:29,279 --> 03:01:35,279
container.
3596
03:01:30,960 --> 03:01:37,920
and we'll scroll up to our CSS.
3597
03:01:35,279 --> 03:01:41,040
And at the bottom here,
3598
03:01:37,920 --> 03:01:43,920
we'll type dot to select a class. And
3599
03:01:41,040 --> 03:01:47,680
we'll select the class app dash
3600
03:01:43,920 --> 03:01:50,640
container and curly brackets.
3601
03:01:47,680 --> 03:01:54,160
And to set a maximum width of our app,
3602
03:01:50,640 --> 03:02:00,399
we can just use the property max dash
3603
03:01:54,160 --> 03:02:03,520
width colon. and let's try 600 pixels
3604
03:02:00,399 --> 03:02:06,720
and save.
3605
03:02:03,520 --> 03:02:11,040
So now if we resize our browser the app
3606
03:02:06,720 --> 03:02:14,720
will be limited to 600 pixels just like
3607
03:02:11,040 --> 03:02:17,680
the final design. Next in the final
3608
03:02:14,720 --> 03:02:20,399
design notice that the app is centered
3609
03:02:17,680 --> 03:02:23,920
on the website. Now there are several
3610
03:02:20,399 --> 03:02:27,359
ways to center an element in CSS.
3611
03:02:23,920 --> 03:02:29,520
One way is to use margin. So we'll go
3612
03:02:27,359 --> 03:02:32,560
back to our code
3613
03:02:29,520 --> 03:02:38,319
and in the app container, we're going to
3614
03:02:32,560 --> 03:02:41,840
add the property margin dash left colon
3615
03:02:38,319 --> 03:02:46,960
and we'll set it to auto. And we'll also
3616
03:02:41,840 --> 03:02:48,800
set the margin dash right colon
3617
03:02:46,960 --> 03:02:52,000
auto.
3618
03:02:48,800 --> 03:02:55,040
So this will automatically add a space
3619
03:02:52,000 --> 03:02:58,640
on the left and right of this element
3620
03:02:55,040 --> 03:03:02,399
until it is centered. So if we save our
3621
03:02:58,640 --> 03:03:04,640
code and check our website, you'll
3622
03:03:02,399 --> 03:03:07,200
notice that the entire app is now
3623
03:03:04,640 --> 03:03:12,000
centered on the website. And now we
3624
03:03:07,200 --> 03:03:14,080
created the layout for the overall app.
3625
03:03:12,000 --> 03:03:17,359
Next, we're going to create the layout
3626
03:03:14,080 --> 03:03:20,080
for the chat messages. If you don't have
3627
03:03:17,359 --> 03:03:23,040
any chat messages in your final project,
3628
03:03:20,080 --> 03:03:26,560
you can add some by typing in here and
3629
03:03:23,040 --> 03:03:29,279
then pressing send. So notice that the
3630
03:03:26,560 --> 03:03:31,840
chat messages from the user are on the
3631
03:03:29,279 --> 03:03:34,960
right while the chat messages from the
3632
03:03:31,840 --> 03:03:38,720
robot are on the left. So we're going to
3633
03:03:34,960 --> 03:03:41,520
create this layout. First, we need to
3634
03:03:38,720 --> 03:03:44,640
give class names to these chat messages
3635
03:03:41,520 --> 03:03:47,520
so we can style them.
3636
03:03:44,640 --> 03:03:50,000
So, let's resize our browser for now.
3637
03:03:47,520 --> 03:03:52,640
And we'll go to our code. And we're
3638
03:03:50,000 --> 03:03:56,319
going to scroll down
3639
03:03:52,640 --> 03:03:59,040
to the chat message component. And we'll
3640
03:03:56,319 --> 03:04:02,800
scroll down to the HTML.
3641
03:03:59,040 --> 03:04:05,920
And we'll give each chat message a class
3642
03:04:02,800 --> 03:04:08,319
name equals. And we're going to do
3643
03:04:05,920 --> 03:04:12,080
something different this time. We'll
3644
03:04:08,319 --> 03:04:15,040
type curly brackets. So here we want to
3645
03:04:12,080 --> 03:04:18,080
give a different class name depending on
3646
03:04:15,040 --> 03:04:20,880
if the sender is a user or the sender is
3647
03:04:18,080 --> 03:04:23,120
a robot. This will let us position the
3648
03:04:20,880 --> 03:04:26,560
chat messages differently on the right
3649
03:04:23,120 --> 03:04:28,800
or on the left. To give this a different
3650
03:04:26,560 --> 03:04:30,160
class name depending on the sender.
3651
03:04:28,800 --> 03:04:33,920
We're going to use a feature of
3652
03:04:30,160 --> 03:04:36,880
JavaScript called the turnary operator.
3653
03:04:33,920 --> 03:04:40,479
As a review, the turnary operator looks
3654
03:04:36,880 --> 03:04:42,000
like this. And it works like an if else
3655
03:04:40,479 --> 03:04:45,200
statement.
3656
03:04:42,000 --> 03:04:48,800
If value one is true, then the result is
3657
03:04:45,200 --> 03:04:50,319
value two. Else the result is value
3658
03:04:48,800 --> 03:04:53,439
three.
3659
03:04:50,319 --> 03:04:57,600
The turnary operator lets us insert an
3660
03:04:53,439 --> 03:05:00,560
if else statement directly in the JSX.
3661
03:04:57,600 --> 03:05:03,520
So using a turnary operator, we can give
3662
03:05:00,560 --> 03:05:05,279
a different class name depending on the
3663
03:05:03,520 --> 03:05:07,920
sender.
3664
03:05:05,279 --> 03:05:12,000
Inside here, we're going to type the if
3665
03:05:07,920 --> 03:05:16,560
condition. So, we'll check if the sender
3666
03:05:12,000 --> 03:05:19,680
is equal to the string user. And then
3667
03:05:16,560 --> 03:05:22,880
we'll type question mark. If the sender
3668
03:05:19,680 --> 03:05:25,760
is the user, we'll set the class name to
3669
03:05:22,880 --> 03:05:29,279
the string chat-
3670
03:05:25,760 --> 03:05:32,319
message dash user.
3671
03:05:29,279 --> 03:05:35,920
And then we'll type a colon. So, this
3672
03:05:32,319 --> 03:05:38,319
means else. So if the sender is not the
3673
03:05:35,920 --> 03:05:41,600
user, we're going to give the class name
3674
03:05:38,319 --> 03:05:45,359
the string chat-
3675
03:05:41,600 --> 03:05:48,800
message dash robot.
3676
03:05:45,359 --> 03:05:51,920
So again the way this code works is if
3677
03:05:48,800 --> 03:05:55,439
the sender is equal to user, we'll set
3678
03:05:51,920 --> 03:05:57,840
the class name to chat message user.
3679
03:05:55,439 --> 03:06:00,880
Else we'll set the class name to chat
3680
03:05:57,840 --> 03:06:04,240
message robot. As you can see, the
3681
03:06:00,880 --> 03:06:08,160
turnary operator lets us insert an if
3682
03:06:04,240 --> 03:06:10,960
else statement directly in the JSX. And
3683
03:06:08,160 --> 03:06:13,120
notice that we used curly brackets here
3684
03:06:10,960 --> 03:06:16,240
because we want to save the result of
3685
03:06:13,120 --> 03:06:19,200
this code into class name instead of
3686
03:06:16,240 --> 03:06:21,680
just a string.
3687
03:06:19,200 --> 03:06:23,840
Before we continue, we can add some new
3688
03:06:21,680 --> 03:06:26,560
lines to this code to make it easier to
3689
03:06:23,840 --> 03:06:29,359
read. So, at the beginning, we can add a
3690
03:06:26,560 --> 03:06:33,359
new line. And then we'll add a new line
3691
03:06:29,359 --> 03:06:35,040
before the question mark and press tab.
3692
03:06:33,359 --> 03:06:38,000
And we'll add a new line before the
3693
03:06:35,040 --> 03:06:40,319
colon. And we'll add a new line at the
3694
03:06:38,000 --> 03:06:43,439
back.
3695
03:06:40,319 --> 03:06:46,319
And remove some spaces so that this is
3696
03:06:43,439 --> 03:06:48,399
vertically aligned.
3697
03:06:46,319 --> 03:06:51,359
So, this makes the code a bit easier to
3698
03:06:48,399 --> 03:06:54,399
read. We have the if condition and then
3699
03:06:51,359 --> 03:06:58,399
the first value and then else the second
3700
03:06:54,399 --> 03:07:01,040
value. Now let's save
3701
03:06:58,399 --> 03:07:04,560
and we'll go to our website and let's
3702
03:07:01,040 --> 03:07:07,040
check the HTML. So in our console, we
3703
03:07:04,560 --> 03:07:10,160
can click these three dots here and then
3704
03:07:07,040 --> 03:07:12,399
click this icon to open the console in
3705
03:07:10,160 --> 03:07:15,359
another window. And we'll go to the
3706
03:07:12,399 --> 03:07:18,399
elements tab and we'll click this
3707
03:07:15,359 --> 03:07:22,640
pointer on the top left and we'll select
3708
03:07:18,399 --> 03:07:24,880
one of these chat messages and click it.
3709
03:07:22,640 --> 03:07:28,479
And if we scroll down here, you can see
3710
03:07:24,880 --> 03:07:30,960
that each chat message now has a class.
3711
03:07:28,479 --> 03:07:33,760
And messages from the user have the
3712
03:07:30,960 --> 03:07:36,479
class chat message user. And messages
3713
03:07:33,760 --> 03:07:39,040
from the robot have the class chat
3714
03:07:36,479 --> 03:07:41,359
message robot.
3715
03:07:39,040 --> 03:07:44,160
So now that we added a class to each of
3716
03:07:41,359 --> 03:07:46,080
these chat messages, let's style these
3717
03:07:44,160 --> 03:07:49,279
chat messages.
3718
03:07:46,080 --> 03:07:52,080
We'll go to our code and we'll scroll up
3719
03:07:49,279 --> 03:07:55,920
to our CSS.
3720
03:07:52,080 --> 03:07:59,680
And at the end, let's type a dot. And
3721
03:07:55,920 --> 03:08:06,439
first let's select the class chat dash
3722
03:07:59,680 --> 03:08:06,439
message dash user and curly brackets.
3723
03:08:06,479 --> 03:08:12,720
And now for chat message user we want to
3724
03:08:09,760 --> 03:08:16,240
position these elements the text and the
3725
03:08:12,720 --> 03:08:19,760
profile image on the right. To create
3726
03:08:16,240 --> 03:08:21,279
this flexible layout we can use flexbox.
3727
03:08:19,760 --> 03:08:23,840
Again
3728
03:08:21,279 --> 03:08:27,040
remember there are three steps to use
3729
03:08:23,840 --> 03:08:30,240
flexbox. First, we already have a
3730
03:08:27,040 --> 03:08:32,880
container around the elements. It has a
3731
03:08:30,240 --> 03:08:34,560
class chat message user or chat message
3732
03:08:32,880 --> 03:08:37,279
robot.
3733
03:08:34,560 --> 03:08:39,760
Next, we need to add display flex to
3734
03:08:37,279 --> 03:08:42,399
this container. So, for chat message
3735
03:08:39,760 --> 03:08:46,880
user, we'll add
3736
03:08:42,399 --> 03:08:51,600
display colon flex.
3737
03:08:46,880 --> 03:08:54,640
And now we can use flexbox features.
3738
03:08:51,600 --> 03:08:59,200
Another feature of flexbox is we're
3739
03:08:54,640 --> 03:09:01,279
going to type justify dashcontent
3740
03:08:59,200 --> 03:09:03,760
colon.
3741
03:09:01,279 --> 03:09:08,000
Justify content controls how the
3742
03:09:03,760 --> 03:09:09,840
elements are displayed horizontally.
3743
03:09:08,000 --> 03:09:12,080
If we want these elements to be
3744
03:09:09,840 --> 03:09:15,359
displayed on the right, we're going to
3745
03:09:12,080 --> 03:09:17,680
give this a value end.
3746
03:09:15,359 --> 03:09:20,800
So it will display these elements at the
3747
03:09:17,680 --> 03:09:24,160
end of this row or on the right.
3748
03:09:20,800 --> 03:09:26,560
So now let's save our code.
3749
03:09:24,160 --> 03:09:29,760
And now the chat messages from the user
3750
03:09:26,560 --> 03:09:33,920
are on the right. And we created the
3751
03:09:29,760 --> 03:09:36,560
layout for the chat messages.
3752
03:09:33,920 --> 03:09:38,640
Next, we're going to style the text in
3753
03:09:36,560 --> 03:09:41,279
the chat messages.
3754
03:09:38,640 --> 03:09:44,640
First, we need to add a class name to
3755
03:09:41,279 --> 03:09:47,120
this text so we can style it.
3756
03:09:44,640 --> 03:09:50,160
Let's go to our code and we're going to
3757
03:09:47,120 --> 03:09:52,000
scroll down to the chat message
3758
03:09:50,160 --> 03:09:53,920
component
3759
03:09:52,000 --> 03:09:57,760
and we're going to scroll to the text
3760
03:09:53,920 --> 03:10:01,680
which is here. So currently this text is
3761
03:09:57,760 --> 03:10:04,560
not inside an element. So first we're
3762
03:10:01,680 --> 03:10:08,160
going to put this text inside an element
3763
03:10:04,560 --> 03:10:11,200
so that we can style just the text.
3764
03:10:08,160 --> 03:10:15,359
Above this, we'll put this in a div by
3765
03:10:11,200 --> 03:10:20,080
typing less than div greater than and
3766
03:10:15,359 --> 03:10:22,640
below it less than slashd greater than.
3767
03:10:20,080 --> 03:10:26,720
And we'll add an indent at the front to
3768
03:10:22,640 --> 03:10:29,760
show that this text is inside this div.
3769
03:10:26,720 --> 03:10:32,880
And now let's add a class to this div so
3770
03:10:29,760 --> 03:10:37,279
we can style just the text. So, we'll
3771
03:10:32,880 --> 03:10:41,279
give this a class name equals double
3772
03:10:37,279 --> 03:10:45,840
quotes. And let's give it the class chat
3773
03:10:41,279 --> 03:10:49,279
dash message dash text.
3774
03:10:45,840 --> 03:10:51,920
So now let's style this with CSS. We'll
3775
03:10:49,279 --> 03:10:55,760
scroll up to our CSS
3776
03:10:51,920 --> 03:10:59,680
and at the end we'll type a dot and
3777
03:10:55,760 --> 03:11:04,640
we'll style the class chat dash message
3778
03:10:59,680 --> 03:11:07,040
dash text and curly brackets.
3779
03:11:04,640 --> 03:11:09,120
So the first thing we'll style is we're
3780
03:11:07,040 --> 03:11:12,240
going to change the background color to
3781
03:11:09,120 --> 03:11:14,720
this gray color. To get this exact
3782
03:11:12,240 --> 03:11:19,200
color, we're going to rightclick on this
3783
03:11:14,720 --> 03:11:21,279
text and then click inspect.
3784
03:11:19,200 --> 03:11:23,279
and we'll find this text in the elements
3785
03:11:21,279 --> 03:11:25,279
tab and we're going to go into the
3786
03:11:23,279 --> 03:11:30,640
styles and we'll get the background
3787
03:11:25,279 --> 03:11:33,760
color. So, we'll copy this RGB color
3788
03:11:30,640 --> 03:11:38,720
and we'll go into our code and we'll set
3789
03:11:33,760 --> 03:11:43,920
the background dash color colon and
3790
03:11:38,720 --> 03:11:46,160
we'll paste the RGB color and save.
3791
03:11:43,920 --> 03:11:48,479
And now we'll have the exact color in
3792
03:11:46,160 --> 03:11:51,200
our final design.
3793
03:11:48,479 --> 03:11:54,000
However, after we saved, it kind of
3794
03:11:51,200 --> 03:11:56,720
messed up our layout. So, the chat
3795
03:11:54,000 --> 03:11:59,439
message user looks okay, but the text
3796
03:11:56,720 --> 03:12:02,080
for chat message robot is now appearing
3797
03:11:59,439 --> 03:12:04,640
below the profile image and it's taking
3798
03:12:02,080 --> 03:12:07,840
up the entire line.
3799
03:12:04,640 --> 03:12:11,520
So, what happened? Remember that we just
3800
03:12:07,840 --> 03:12:14,399
surrounded this text in a div element.
3801
03:12:11,520 --> 03:12:18,080
Normally a div element takes up an
3802
03:12:14,399 --> 03:12:20,560
entire line by itself because a div is a
3803
03:12:18,080 --> 03:12:23,600
block element.
3804
03:12:20,560 --> 03:12:27,680
However, remember that we also made chat
3805
03:12:23,600 --> 03:12:30,640
message user a flexbox. When a block
3806
03:12:27,680 --> 03:12:33,520
element is inside a flexbox, it no
3807
03:12:30,640 --> 03:12:36,640
longer takes up the entire line. It only
3808
03:12:33,520 --> 03:12:39,760
takes up as much space as it needs to.
3809
03:12:36,640 --> 03:12:42,319
So that's why this div only takes up as
3810
03:12:39,760 --> 03:12:45,040
much space as it needs because it's
3811
03:12:42,319 --> 03:12:48,560
inside a flexbox.
3812
03:12:45,040 --> 03:12:52,000
One way to fix this layout problem is we
3813
03:12:48,560 --> 03:12:55,600
can make chat message robot a flexbox as
3814
03:12:52,000 --> 03:12:59,680
well. We'll go back to our code and
3815
03:12:55,600 --> 03:13:04,000
below chat message user let's type dot
3816
03:12:59,680 --> 03:13:08,080
and we'll style the chat- message dash
3817
03:13:04,000 --> 03:13:12,160
robot and curly brackets
3818
03:13:08,080 --> 03:13:14,399
and we'll also give it display colon
3819
03:13:12,160 --> 03:13:16,640
flex
3820
03:13:14,399 --> 03:13:18,560
so it will match the layout for the chat
3821
03:13:16,640 --> 03:13:21,279
message user.
3822
03:13:18,560 --> 03:13:24,640
Let's save.
3823
03:13:21,279 --> 03:13:27,680
And now because this div is inside a
3824
03:13:24,640 --> 03:13:31,200
flexbox, it only takes up as much space
3825
03:13:27,680 --> 03:13:35,359
as it needs. So now the background color
3826
03:13:31,200 --> 03:13:38,720
and the layout looks good. Next, the
3827
03:13:35,359 --> 03:13:41,040
style of text or the font is different
3828
03:13:38,720 --> 03:13:44,640
in the final design.
3829
03:13:41,040 --> 03:13:48,560
To change the font, we'll go to our code
3830
03:13:44,640 --> 03:13:52,720
and we'll add the property font dash
3831
03:13:48,560 --> 03:13:56,319
family colon And in the final project, I
3832
03:13:52,720 --> 03:13:58,880
use the font Ariel.
3833
03:13:56,319 --> 03:14:01,120
Let's save.
3834
03:13:58,880 --> 03:14:02,960
And now our font matches the final
3835
03:14:01,120 --> 03:14:06,239
design.
3836
03:14:02,960 --> 03:14:08,960
Now, usually we don't set the font for
3837
03:14:06,239 --> 03:14:12,000
just one element. We set the font for
3838
03:14:08,960 --> 03:14:14,960
the entire website because we want the
3839
03:14:12,000 --> 03:14:18,319
entire website to use the same font or
3840
03:14:14,960 --> 03:14:21,520
the same style of text. to set the font
3841
03:14:18,319 --> 03:14:24,319
for the entire website. We can style the
3842
03:14:21,520 --> 03:14:27,200
body element. So, we'll scroll to the
3843
03:14:24,319 --> 03:14:31,200
top of our CSS.
3844
03:14:27,200 --> 03:14:35,120
And here we'll style the body element
3845
03:14:31,200 --> 03:14:40,880
curly brackets and we'll set the font
3846
03:14:35,120 --> 03:14:44,319
dash family colon to area.
3847
03:14:40,880 --> 03:14:47,120
So this sets the font for all elements
3848
03:14:44,319 --> 03:14:50,160
inside the body, which is basically the
3849
03:14:47,120 --> 03:14:52,720
entire website. So now we can scroll
3850
03:14:50,160 --> 03:14:55,520
back down
3851
03:14:52,720 --> 03:14:57,200
and we can remove this style
3852
03:14:55,520 --> 03:15:00,160
because we're going to set the font for
3853
03:14:57,200 --> 03:15:02,960
the entire website instead.
3854
03:15:00,160 --> 03:15:05,200
Let's save.
3855
03:15:02,960 --> 03:15:08,399
And now we set the font for the entire
3856
03:15:05,200 --> 03:15:11,760
website to Ariel.
3857
03:15:08,399 --> 03:15:15,120
Next, let's add space on the inside of
3858
03:15:11,760 --> 03:15:19,359
this text, which is padding.
3859
03:15:15,120 --> 03:15:23,359
So, here we'll add padding
3860
03:15:19,359 --> 03:15:26,720
colon. And let's try 15 pixels of
3861
03:15:23,359 --> 03:15:31,200
vertical space and 20 pixels of
3862
03:15:26,720 --> 03:15:34,160
horizontal space. And save.
3863
03:15:31,200 --> 03:15:37,439
And that looks pretty good.
3864
03:15:34,160 --> 03:15:42,319
Next, we'll make the corners round.
3865
03:15:37,439 --> 03:15:48,239
To do that, we'll use border dash radius
3866
03:15:42,319 --> 03:15:51,359
colon and 10 pixels and save.
3867
03:15:48,239 --> 03:15:54,319
And that looks pretty good. Next, let's
3868
03:15:51,359 --> 03:15:59,920
add space on the outside of the text on
3869
03:15:54,319 --> 03:16:05,680
the right. So, we'll add margin dash
3870
03:15:59,920 --> 03:16:07,760
right colon and let's try 10 pixels and
3871
03:16:05,680 --> 03:16:10,640
save.
3872
03:16:07,760 --> 03:16:13,279
And that looks close enough.
3873
03:16:10,640 --> 03:16:16,080
And here, if we scroll down to a chat
3874
03:16:13,279 --> 03:16:19,200
message from the robot, we'll add some
3875
03:16:16,080 --> 03:16:24,560
space on the left of the text. So, in
3876
03:16:19,200 --> 03:16:30,880
our code, we'll add margin dash left.
3877
03:16:24,560 --> 03:16:34,000
And let's try 10 pixels and save.
3878
03:16:30,880 --> 03:16:36,080
And that looks pretty good. Finally,
3879
03:16:34,000 --> 03:16:39,840
let's add some space on the bottom of
3880
03:16:36,080 --> 03:16:46,000
the text so we can separate them. Here
3881
03:16:39,840 --> 03:16:51,920
we'll add margin dash bottom colon. And
3882
03:16:46,000 --> 03:16:54,960
let's try 20 pixels and save.
3883
03:16:51,920 --> 03:16:57,840
And I think that looks close enough.
3884
03:16:54,960 --> 03:17:00,800
However, we now have a problem here,
3885
03:16:57,840 --> 03:17:02,399
which is the profile image seems to be
3886
03:17:00,800 --> 03:17:05,680
stretching.
3887
03:17:02,399 --> 03:17:09,279
This happens because this container is a
3888
03:17:05,680 --> 03:17:13,200
flexbox. And by default, elements inside
3889
03:17:09,279 --> 03:17:15,680
a flexbox will stretch vertically.
3890
03:17:13,200 --> 03:17:18,880
So, if this text gets longer because we
3891
03:17:15,680 --> 03:17:20,720
added some margin, this image will
3892
03:17:18,880 --> 03:17:23,680
stretch.
3893
03:17:20,720 --> 03:17:26,479
To fix this, flexbox has another feature
3894
03:17:23,680 --> 03:17:30,239
called align items.
3895
03:17:26,479 --> 03:17:35,200
So in our CSS in chat message user,
3896
03:17:30,239 --> 03:17:36,800
we'll add the property align dash items
3897
03:17:35,200 --> 03:17:39,520
colon
3898
03:17:36,800 --> 03:17:42,560
and align items controls how the
3899
03:17:39,520 --> 03:17:46,160
elements are displayed vertically. By
3900
03:17:42,560 --> 03:17:49,600
default, align items is stretch. That's
3901
03:17:46,160 --> 03:17:51,520
why the elements stretch vertically.
3902
03:17:49,600 --> 03:17:55,040
Instead, we're going to change align
3903
03:17:51,520 --> 03:17:57,040
items to start.
3904
03:17:55,040 --> 03:18:00,479
This will display the elements at the
3905
03:17:57,040 --> 03:18:03,439
start or at the top vertically, and it
3906
03:18:00,479 --> 03:18:05,760
will no longer stretch. Same thing for
3907
03:18:03,439 --> 03:18:12,640
the chat message robot. We're going to
3908
03:18:05,760 --> 03:18:14,960
give it align dash items colon start and
3909
03:18:12,640 --> 03:18:17,279
save.
3910
03:18:14,960 --> 03:18:19,600
And now the elements in the flexbox will
3911
03:18:17,279 --> 03:18:21,760
appear at the start or at the top
3912
03:18:19,600 --> 03:18:24,399
vertically.
3913
03:18:21,760 --> 03:18:27,520
The last thing we'll style for the text
3914
03:18:24,399 --> 03:18:31,200
is if we go to our final project and
3915
03:18:27,520 --> 03:18:34,479
inside the text box we type test and
3916
03:18:31,200 --> 03:18:36,080
press send.
3917
03:18:34,479 --> 03:18:40,239
You'll notice that the response from the
3918
03:18:36,080 --> 03:18:42,319
chatbot has a maximum width. So in our
3919
03:18:40,239 --> 03:18:46,080
project, we're also going to add a
3920
03:18:42,319 --> 03:18:49,279
maximum width to our text. So in our
3921
03:18:46,080 --> 03:18:54,239
code, we'll go to the chat message text
3922
03:18:49,279 --> 03:19:00,399
and let's add a max dash width colon.
3923
03:18:54,239 --> 03:19:02,880
And let's try 300 pixels and save.
3924
03:19:00,399 --> 03:19:07,520
And now if we go to our text box and
3925
03:19:02,880 --> 03:19:11,359
type test and press send and scroll
3926
03:19:07,520 --> 03:19:14,479
down, the text has a maximum width and
3927
03:19:11,359 --> 03:19:17,120
it matches the final design.
3928
03:19:14,479 --> 03:19:20,080
Now we finish styling the text in the
3929
03:19:17,120 --> 03:19:23,680
chat messages.
3930
03:19:20,080 --> 03:19:26,479
Next, we'll style the profile images. So
3931
03:19:23,680 --> 03:19:30,080
in our code, let's scroll down to the
3932
03:19:26,479 --> 03:19:32,239
profile image which is inside the chat
3933
03:19:30,080 --> 03:19:37,120
message component
3934
03:19:32,239 --> 03:19:39,760
and it's these two image elements here.
3935
03:19:37,120 --> 03:19:43,359
Currently, we're using an attribute to
3936
03:19:39,760 --> 03:19:46,960
set the width of this image. However, a
3937
03:19:43,359 --> 03:19:50,560
best practice is to use CSS for styling
3938
03:19:46,960 --> 03:19:53,120
instead of attributes. So let's remove
3939
03:19:50,560 --> 03:19:56,880
this attribute and we'll remove it down
3940
03:19:53,120 --> 03:19:59,120
here as well and we'll add a class to
3941
03:19:56,880 --> 03:20:03,439
these profile images so that we can
3942
03:19:59,120 --> 03:20:07,680
style them with CSS. So here we'll add
3943
03:20:03,439 --> 03:20:10,880
class name equals double quotes and
3944
03:20:07,680 --> 03:20:14,960
we'll give it the class chat-
3945
03:20:10,880 --> 03:20:16,960
message dash profile.
3946
03:20:14,960 --> 03:20:19,600
And we'll also add this class to the
3947
03:20:16,960 --> 03:20:23,600
profile image for the user.
3948
03:20:19,600 --> 03:20:25,680
We'll give it the class name equals
3949
03:20:23,600 --> 03:20:30,720
double quotes
3950
03:20:25,680 --> 03:20:33,920
chat dash message dash profile.
3951
03:20:30,720 --> 03:20:37,200
So now we can style this with our CSS.
3952
03:20:33,920 --> 03:20:41,359
We'll scroll up to our CSS
3953
03:20:37,200 --> 03:20:45,600
and at the end we'll type a dot and
3954
03:20:41,359 --> 03:20:50,960
we'll style the class chat dash message
3955
03:20:45,600 --> 03:20:53,760
dash profile and curly brackets.
3956
03:20:50,960 --> 03:20:56,479
So previously we set the width to 50
3957
03:20:53,760 --> 03:21:01,359
pixels. So let's do that again using
3958
03:20:56,479 --> 03:21:03,840
CSS. This time we'll set the width colon
3959
03:21:01,359 --> 03:21:07,479
to 50 pixels.
3960
03:21:03,840 --> 03:21:07,479
and save.
3961
03:21:07,600 --> 03:21:13,680
Now, if we look at the final design,
3962
03:21:11,200 --> 03:21:16,000
it looks like the profile images here
3963
03:21:13,680 --> 03:21:18,560
are a little bit smaller than our
3964
03:21:16,000 --> 03:21:22,319
profile images. So, let's make this
3965
03:21:18,560 --> 03:21:23,920
width smaller. Let's try 45 pixels
3966
03:21:22,319 --> 03:21:26,640
instead.
3967
03:21:23,920 --> 03:21:29,279
And save.
3968
03:21:26,640 --> 03:21:32,000
And I think that looks close enough. And
3969
03:21:29,279 --> 03:21:35,520
now our profile images are the same size
3970
03:21:32,000 --> 03:21:37,120
as the final design.
3971
03:21:35,520 --> 03:21:39,279
The last thing we're going to change in
3972
03:21:37,120 --> 03:21:41,840
this design is we're going to move the
3973
03:21:39,279 --> 03:21:44,239
text box to the bottom so that it
3974
03:21:41,840 --> 03:21:45,920
matches the design of other popular
3975
03:21:44,239 --> 03:21:48,720
chatbots.
3976
03:21:45,920 --> 03:21:51,520
In the final project, we're going to
3977
03:21:48,720 --> 03:21:54,160
make this a little taller. Now, we can
3978
03:21:51,520 --> 03:21:58,160
move this text box to the bottom of the
3979
03:21:54,160 --> 03:22:01,120
page by clicking this link.
3980
03:21:58,160 --> 03:22:02,720
And now let's create this design in our
3981
03:22:01,120 --> 03:22:05,680
project.
3982
03:22:02,720 --> 03:22:08,720
First I'll open our project and then
3983
03:22:05,680 --> 03:22:10,800
move it back to the same window so that
3984
03:22:08,720 --> 03:22:14,080
we can see this side by side a little
3985
03:22:10,800 --> 03:22:16,880
easier. And to put the text box at the
3986
03:22:14,080 --> 03:22:19,520
bottom of the page, the first step is to
3987
03:22:16,880 --> 03:22:22,800
move the text box below the chat
3988
03:22:19,520 --> 03:22:25,040
messages. We'll go to our code and we're
3989
03:22:22,800 --> 03:22:27,120
going to scroll down to the app
3990
03:22:25,040 --> 03:22:30,000
component
3991
03:22:27,120 --> 03:22:33,120
which is here and we'll scroll to the
3992
03:22:30,000 --> 03:22:36,479
HTML and we're just going to move this
3993
03:22:33,120 --> 03:22:39,279
chat input below the chat messages. So
3994
03:22:36,479 --> 03:22:41,920
we'll select this component and we'll
3995
03:22:39,279 --> 03:22:46,399
cut
3996
03:22:41,920 --> 03:22:49,600
and below chat messages we'll paste
3997
03:22:46,399 --> 03:22:52,239
and save.
3998
03:22:49,600 --> 03:22:54,720
And now the chat input will be below the
3999
03:22:52,239 --> 03:22:57,920
chat messages. And that looks pretty
4000
03:22:54,720 --> 03:23:00,880
good. Next, we also want to put this
4001
03:22:57,920 --> 03:23:03,439
chat input at the bottom of the browser
4002
03:23:00,880 --> 03:23:07,840
here. So there are several ways to do
4003
03:23:03,439 --> 03:23:11,600
this in CSS. One way is to use flexbox
4004
03:23:07,840 --> 03:23:14,319
again. Now before we use flexbox, we're
4005
03:23:11,600 --> 03:23:17,439
going to make the app container stretch
4006
03:23:14,319 --> 03:23:20,560
to the bottom of the browser. So right
4007
03:23:17,439 --> 03:23:24,479
now if we click inside here and we
4008
03:23:20,560 --> 03:23:27,359
rightclick and inspect
4009
03:23:24,479 --> 03:23:31,279
and we go to the elements tab and we
4010
03:23:27,359 --> 03:23:34,000
scroll up and look at the app container.
4011
03:23:31,279 --> 03:23:36,479
Notice that the app container stops at
4012
03:23:34,000 --> 03:23:38,800
the chat input section. And that's
4013
03:23:36,479 --> 03:23:41,520
because this is all the content that we
4014
03:23:38,800 --> 03:23:44,640
have. So the app container only has the
4015
03:23:41,520 --> 03:23:46,640
height that it needs. However, we want
4016
03:23:44,640 --> 03:23:49,040
to stretch the app container to the
4017
03:23:46,640 --> 03:23:52,160
bottom of the browser so that we can put
4018
03:23:49,040 --> 03:23:54,479
the chat input at the bottom. To do
4019
03:23:52,160 --> 03:23:57,279
that, we're going to set the height of
4020
03:23:54,479 --> 03:23:59,439
the app container. So, we'll go back to
4021
03:23:57,279 --> 03:24:04,040
our code
4022
03:23:59,439 --> 03:24:04,040
and we'll scroll up to our CSS
4023
03:24:04,239 --> 03:24:09,439
and we're going to find the styles for
4024
03:24:06,160 --> 03:24:11,920
the app container which is right here.
4025
03:24:09,439 --> 03:24:15,040
And at the end, we're going to set the
4026
03:24:11,920 --> 03:24:17,439
height colon.
4027
03:24:15,040 --> 03:24:20,800
And now we want the height of the app
4028
03:24:17,439 --> 03:24:24,239
container to stretch to be the same as
4029
03:24:20,800 --> 03:24:26,720
the height of the browser here. To do
4030
03:24:24,239 --> 03:24:31,040
that, we're going to give this a special
4031
03:24:26,720 --> 03:24:35,520
value 100 VH.
4032
03:24:31,040 --> 03:24:38,239
So VH stands for viewport height. This
4033
03:24:35,520 --> 03:24:41,760
gives us the height of the browser or
4034
03:24:38,239 --> 03:24:46,160
the part of the page that is visible.
4035
03:24:41,760 --> 03:24:49,520
100 VH means 100% of the height of the
4036
03:24:46,160 --> 03:24:55,319
browser. So this means the app container
4037
03:24:49,520 --> 03:24:55,319
will be 100% of this height.
4038
03:24:55,520 --> 03:25:00,880
So now let's save
4039
03:24:58,720 --> 03:25:04,000
and we'll go to our website and we'll
4040
03:25:00,880 --> 03:25:06,000
open the console again by right-clicking
4041
03:25:04,000 --> 03:25:08,720
and inspect
4042
03:25:06,000 --> 03:25:12,160
and go to the elements tab and we'll
4043
03:25:08,720 --> 03:25:14,800
scroll up to the app container again.
4044
03:25:12,160 --> 03:25:18,560
And now you'll notice the app container
4045
03:25:14,800 --> 03:25:21,359
stretches to the bottom of the browser.
4046
03:25:18,560 --> 03:25:23,680
However, the app container is actually a
4047
03:25:21,359 --> 03:25:26,560
little too tall. So it actually
4048
03:25:23,680 --> 03:25:29,760
stretches below the bottom of the page.
4049
03:25:26,560 --> 03:25:31,920
And now we can scroll a bit here. The
4050
03:25:29,760 --> 03:25:35,520
reason it's a little bit too tall is
4051
03:25:31,920 --> 03:25:39,439
because by default the body element here
4052
03:25:35,520 --> 03:25:42,800
has a default margin of 8 pixels on all
4053
03:25:39,439 --> 03:25:45,439
sides. So the app container is the same
4054
03:25:42,800 --> 03:25:50,000
height as the browser, but then we add
4055
03:25:45,439 --> 03:25:52,640
another 8 pixels on the top and bottom.
4056
03:25:50,000 --> 03:25:55,760
To fix this issue, we can just remove
4057
03:25:52,640 --> 03:25:58,640
the default margin from the body. So,
4058
03:25:55,760 --> 03:26:01,680
we'll go back to our code
4059
03:25:58,640 --> 03:26:05,040
and scroll up to the styles for the
4060
03:26:01,680 --> 03:26:08,479
body. And at the end, let's remove the
4061
03:26:05,040 --> 03:26:12,960
margin at the top and bottom by typing
4062
03:26:08,479 --> 03:26:16,800
margin- top colon. And we'll set it to
4063
03:26:12,960 --> 03:26:19,920
zero pixels. And we'll also set the
4064
03:26:16,800 --> 03:26:26,239
margin dashbottom
4065
03:26:19,920 --> 03:26:28,800
colon to zero pixels and save.
4066
03:26:26,239 --> 03:26:33,279
So now after we remove the default
4067
03:26:28,800 --> 03:26:36,720
margin from the top and the bottom,
4068
03:26:33,279 --> 03:26:40,000
the app container is now exactly the
4069
03:26:36,720 --> 03:26:42,160
same height as the browser.
4070
03:26:40,000 --> 03:26:45,040
Now that the app container stretches to
4071
03:26:42,160 --> 03:26:48,640
the bottom of the browser, we can use a
4072
03:26:45,040 --> 03:26:52,399
flexbox to make the chat messages grow
4073
03:26:48,640 --> 03:26:55,200
and push the chat input to the bottom.
4074
03:26:52,399 --> 03:26:56,720
Again, there are three steps to use a
4075
03:26:55,200 --> 03:26:59,520
flexbox.
4076
03:26:56,720 --> 03:27:03,040
So here we want to position the chat
4077
03:26:59,520 --> 03:27:06,160
messages and the chat input. So we need
4078
03:27:03,040 --> 03:27:08,479
a container around these elements and we
4079
03:27:06,160 --> 03:27:11,040
already have a container for this which
4080
03:27:08,479 --> 03:27:14,000
is the app container.
4081
03:27:11,040 --> 03:27:16,640
The second step is to add display flex
4082
03:27:14,000 --> 03:27:19,520
to the container. So we'll go back to
4083
03:27:16,640 --> 03:27:22,319
our code and we're going to scroll to
4084
03:27:19,520 --> 03:27:27,520
the app container styles which is right
4085
03:27:22,319 --> 03:27:33,520
here and below this we'll add display
4086
03:27:27,520 --> 03:27:33,520
colon flex. Let's save our code.
4087
03:27:34,080 --> 03:27:40,080
And unfortunately, it totally messed up
4088
03:27:36,720 --> 03:27:42,880
our design. So what happened? By
4089
03:27:40,080 --> 03:27:45,520
default, flexbox positions our elements
4090
03:27:42,880 --> 03:27:48,319
horizontally. So it takes all the
4091
03:27:45,520 --> 03:27:50,720
elements inside the container and then
4092
03:27:48,319 --> 03:27:52,239
squeezes them into a row like we see
4093
03:27:50,720 --> 03:27:54,800
here.
4094
03:27:52,239 --> 03:27:57,200
However, this time we want to position
4095
03:27:54,800 --> 03:27:58,720
the elements vertically, not
4096
03:27:57,200 --> 03:28:01,279
horizontally.
4097
03:27:58,720 --> 03:28:03,520
To position the elements vertically,
4098
03:28:01,279 --> 03:28:07,200
we're going to use a flexbox feature
4099
03:28:03,520 --> 03:28:11,680
called flex direction.
4100
03:28:07,200 --> 03:28:14,640
So in here, we'll type flex dash
4101
03:28:11,680 --> 03:28:17,040
direction. colon
4102
03:28:14,640 --> 03:28:20,239
flex direction determines which
4103
03:28:17,040 --> 03:28:23,840
direction the elements are positioned.
4104
03:28:20,239 --> 03:28:26,000
So flex direction is row by default.
4105
03:28:23,840 --> 03:28:29,520
That means it positions the elements
4106
03:28:26,000 --> 03:28:31,520
horizontally like we see here.
4107
03:28:29,520 --> 03:28:34,560
Now we're going to change flex direction
4108
03:28:31,520 --> 03:28:36,560
to column.
4109
03:28:34,560 --> 03:28:40,239
So this positions the elements
4110
03:28:36,560 --> 03:28:41,920
vertically in a column. If we save our
4111
03:28:40,239 --> 03:28:44,560
code
4112
03:28:41,920 --> 03:28:46,479
and check the website, the elements in
4113
03:28:44,560 --> 03:28:50,000
the flexbox are now positioned
4114
03:28:46,479 --> 03:28:52,640
vertically from top to bottom.
4115
03:28:50,000 --> 03:28:56,479
Next, we're going to use flex grow.
4116
03:28:52,640 --> 03:28:59,439
Again, as a review, flex grow makes an
4117
03:28:56,479 --> 03:29:01,120
element grow and take up the remaining
4118
03:28:59,439 --> 03:29:04,000
space.
4119
03:29:01,120 --> 03:29:06,640
Here, we want to make the chat messages
4120
03:29:04,000 --> 03:29:09,520
section grow and take up all the
4121
03:29:06,640 --> 03:29:13,279
remaining vertical space. And this will
4122
03:29:09,520 --> 03:29:15,840
push the chat input down to the bottom.
4123
03:29:13,279 --> 03:29:19,120
To use Flex Grow, we're going to add a
4124
03:29:15,840 --> 03:29:21,520
class to the chat messages section.
4125
03:29:19,120 --> 03:29:23,359
We'll go to our code and we'll scroll
4126
03:29:21,520 --> 03:29:26,720
down
4127
03:29:23,359 --> 03:29:29,840
to the chat messages component which is
4128
03:29:26,720 --> 03:29:33,279
right here. And currently this component
4129
03:29:29,840 --> 03:29:36,239
uses a fragment. And remember, we cannot
4130
03:29:33,279 --> 03:29:39,439
style a fragment since fragments don't
4131
03:29:36,239 --> 03:29:42,960
appear on the website. So again, we're
4132
03:29:39,439 --> 03:29:47,359
going to switch this to a div element.
4133
03:29:42,960 --> 03:29:49,200
and at the end slash div so that we can
4134
03:29:47,359 --> 03:29:52,080
style it.
4135
03:29:49,200 --> 03:29:57,040
And now we'll give this div a class
4136
03:29:52,080 --> 03:30:00,800
using class name equals double quotes
4137
03:29:57,040 --> 03:30:05,200
and we'll give it the class chat dash
4138
03:30:00,800 --> 03:30:07,920
messages dash container.
4139
03:30:05,200 --> 03:30:10,880
And now let's style this class. We'll
4140
03:30:07,920 --> 03:30:15,359
scroll up in our CSS
4141
03:30:10,880 --> 03:30:19,920
and at the end here we'll type dot and
4142
03:30:15,359 --> 03:30:22,160
we'll style the class chat dash messages
4143
03:30:19,920 --> 03:30:24,960
dash container
4144
03:30:22,160 --> 03:30:28,479
and curly brackets
4145
03:30:24,960 --> 03:30:31,120
and we'll use the flexbox feature flex
4146
03:30:28,479 --> 03:30:32,880
dash grow
4147
03:30:31,120 --> 03:30:36,239
1.
4148
03:30:32,880 --> 03:30:38,080
So now let's save.
4149
03:30:36,239 --> 03:30:41,200
And this makes the chat message
4150
03:30:38,080 --> 03:30:43,840
container grow and take up the remaining
4151
03:30:41,200 --> 03:30:47,200
vertical space. And this will push the
4152
03:30:43,840 --> 03:30:49,680
chat input to the bottom of the browser.
4153
03:30:47,200 --> 03:30:52,960
So that's how we use a flexbox to
4154
03:30:49,680 --> 03:30:56,319
position the chat input at the bottom.
4155
03:30:52,960 --> 03:30:59,359
Finally, if we look at the final design,
4156
03:30:56,319 --> 03:31:02,399
we have some space at the top of the app
4157
03:30:59,359 --> 03:31:04,960
and at the bottom of the app. So let's
4158
03:31:02,399 --> 03:31:06,880
add that space at the top and bottom to
4159
03:31:04,960 --> 03:31:09,200
our website.
4160
03:31:06,880 --> 03:31:12,479
We'll go to our CSS
4161
03:31:09,200 --> 03:31:16,160
and currently the app container has a
4162
03:31:12,479 --> 03:31:19,520
special height of 100 VH. So let's avoid
4163
03:31:16,160 --> 03:31:22,880
changing this. Instead, let's add space
4164
03:31:19,520 --> 03:31:25,920
above the Chad messages container. So,
4165
03:31:22,880 --> 03:31:28,399
we'll scroll down to the chat messages
4166
03:31:25,920 --> 03:31:34,160
container and we'll add some space at
4167
03:31:28,399 --> 03:31:36,080
the top using margin dash top colon 20
4168
03:31:34,160 --> 03:31:38,479
pixels.
4169
03:31:36,080 --> 03:31:41,680
And we'll also add some space below the
4170
03:31:38,479 --> 03:31:45,200
chat input container. So, we'll scroll
4171
03:31:41,680 --> 03:31:48,000
up to the chat input container which is
4172
03:31:45,200 --> 03:31:53,840
right here. And we'll add space at the
4173
03:31:48,000 --> 03:32:00,399
bottom using margin dash bottom colon.
4174
03:31:53,840 --> 03:32:03,279
And let's try 60 pixels and save.
4175
03:32:00,399 --> 03:32:05,520
And now that looks pretty good. And now
4176
03:32:03,279 --> 03:32:07,920
if we look at our website and compare it
4177
03:32:05,520 --> 03:32:09,840
to the final design,
4178
03:32:07,920 --> 03:32:12,239
you can see that it looks the same as
4179
03:32:09,840 --> 03:32:14,560
the final design. So, we finished
4180
03:32:12,239 --> 03:32:19,359
styling our project to look like the
4181
03:32:14,560 --> 03:32:21,359
final design using CSS.
4182
03:32:19,359 --> 03:32:24,239
Next, we're going to make the chat
4183
03:32:21,359 --> 03:32:27,520
messages scrollable. In the final
4184
03:32:24,239 --> 03:32:30,080
project, if we have more chat messages
4185
03:32:27,520 --> 03:32:32,880
than we can see in the browser, we can
4186
03:32:30,080 --> 03:32:35,439
actually scroll to see all of the chat
4187
03:32:32,880 --> 03:32:38,080
messages. Now, you may need to add more
4188
03:32:35,439 --> 03:32:40,640
chat messages down here in order to see
4189
03:32:38,080 --> 03:32:44,239
this feature. So, we're going to create
4190
03:32:40,640 --> 03:32:47,439
this feature in our project. If we go to
4191
03:32:44,239 --> 03:32:52,160
our project and we add some more
4192
03:32:47,439 --> 03:32:55,040
messages like test and press send,
4193
03:32:52,160 --> 03:32:58,800
notice if we have too many messages, it
4194
03:32:55,040 --> 03:33:01,600
starts to push the chat input down.
4195
03:32:58,800 --> 03:33:04,239
To make our chat messages scrollable,
4196
03:33:01,600 --> 03:33:05,920
we're going to go to the CSS and we're
4197
03:33:04,239 --> 03:33:09,279
going to find the chat messages
4198
03:33:05,920 --> 03:33:12,479
container, which is right here.
4199
03:33:09,279 --> 03:33:16,720
And at the end we'll add a property
4200
03:33:12,479 --> 03:33:19,520
overflow colon scroll.
4201
03:33:16,720 --> 03:33:22,080
So now if the chat messages overflow or
4202
03:33:19,520 --> 03:33:24,399
there's too many to be displayed, it
4203
03:33:22,080 --> 03:33:29,359
will start to scroll instead of pushing
4204
03:33:24,399 --> 03:33:33,200
things down. Let's save our code
4205
03:33:29,359 --> 03:33:35,120
and then add another message test and
4206
03:33:33,200 --> 03:33:36,720
press send.
4207
03:33:35,120 --> 03:33:39,840
And you may need to add a few more
4208
03:33:36,720 --> 03:33:42,239
messages to see this feature. Now, if we
4209
03:33:39,840 --> 03:33:44,800
have too many messages, we're able to
4210
03:33:42,239 --> 03:33:46,319
scroll up and down to see all the
4211
03:33:44,800 --> 03:33:49,359
messages.
4212
03:33:46,319 --> 03:33:52,000
Now, before we continue on Windows, we
4213
03:33:49,359 --> 03:33:54,319
have a small problem, which is we start
4214
03:33:52,000 --> 03:33:57,359
to see these thick scroll bars on the
4215
03:33:54,319 --> 03:33:59,840
sides, which doesn't look very good. To
4216
03:33:57,359 --> 03:34:03,279
hide these scroll bars, we can go back
4217
03:33:59,840 --> 03:34:09,520
to our CSS and at the end we can use the
4218
03:34:03,279 --> 03:34:11,920
property scroll bar dash width colon
4219
03:34:09,520 --> 03:34:15,359
nut.
4220
03:34:11,920 --> 03:34:18,800
So now let's save.
4221
03:34:15,359 --> 03:34:21,520
And if we add a message test and press
4222
03:34:18,800 --> 03:34:24,479
send and you might need to add more
4223
03:34:21,520 --> 03:34:27,040
messages for your project. Now, if we
4224
03:34:24,479 --> 03:34:31,120
have more messages than we can see, we
4225
03:34:27,040 --> 03:34:33,359
can scroll and we hide the scroll bars.
4226
03:34:31,120 --> 03:34:36,359
So, that's how we make the chat messages
4227
03:34:33,359 --> 03:34:36,359
scrollable.
4228
03:34:37,359 --> 03:34:42,880
The last feature we're going to add to
4229
03:34:39,120 --> 03:34:46,080
our project is auto scrolling. For
4230
03:34:42,880 --> 03:34:50,239
example, if we go down here and add the
4231
03:34:46,080 --> 03:34:52,319
message test and press send,
4232
03:34:50,239 --> 03:34:55,439
notice we have to manually scroll down
4233
03:34:52,319 --> 03:34:59,040
to the bottom ourselves. But if we go to
4234
03:34:55,439 --> 03:35:04,560
the final project and at the bottom add
4235
03:34:59,040 --> 03:35:07,040
a message test and press send,
4236
03:35:04,560 --> 03:35:09,760
notice that it automatically scrolls to
4237
03:35:07,040 --> 03:35:12,160
the bottom of the chat messages. So,
4238
03:35:09,760 --> 03:35:13,840
we're going to add this autoscrolling
4239
03:35:12,160 --> 03:35:16,640
feature.
4240
03:35:13,840 --> 03:35:19,359
Let's go back to our website.
4241
03:35:16,640 --> 03:35:21,359
And to create autoscrolling, we're going
4242
03:35:19,359 --> 03:35:25,520
to learn one of the most important
4243
03:35:21,359 --> 03:35:29,200
features of React called hooks.
4244
03:35:25,520 --> 03:35:32,160
Hooks let us insert React features into
4245
03:35:29,200 --> 03:35:35,520
our component. And we've actually used
4246
03:35:32,160 --> 03:35:39,040
hooks before. React.US
4247
03:35:35,520 --> 03:35:42,720
state is a hook. It inserts a React
4248
03:35:39,040 --> 03:35:46,640
feature called state into our component
4249
03:35:42,720 --> 03:35:50,319
and state automatically updates the HTML
4250
03:35:46,640 --> 03:35:52,960
when the data changes. Now, React has
4251
03:35:50,319 --> 03:35:56,239
other hooks that we can use and every
4252
03:35:52,960 --> 03:36:00,160
hook starts with a word use.
4253
03:35:56,239 --> 03:36:03,680
Another hook is called use effect. Use
4254
03:36:00,160 --> 03:36:07,279
effect lets us run some code after the
4255
03:36:03,680 --> 03:36:10,880
component is created or updated.
4256
03:36:07,279 --> 03:36:13,760
So in our project after we add a chat
4257
03:36:10,880 --> 03:36:16,720
message we want to run some code to
4258
03:36:13,760 --> 03:36:20,640
scroll to the bottom. So this is a
4259
03:36:16,720 --> 03:36:22,880
perfect situation for use effect.
4260
03:36:20,640 --> 03:36:27,479
Inside our code we're going to scroll
4261
03:36:22,880 --> 03:36:27,479
down to the chat messages component
4262
03:36:27,840 --> 03:36:32,319
which is here.
4263
03:36:30,000 --> 03:36:36,239
And at the top of the component, we're
4264
03:36:32,319 --> 03:36:41,120
going to type the code capital R react
4265
03:36:36,239 --> 03:36:43,680
dot use capital E effect
4266
03:36:41,120 --> 03:36:46,239
and brackets.
4267
03:36:43,680 --> 03:36:48,960
By the way, in React, we should put our
4268
03:36:46,239 --> 03:36:51,520
hooks at the top of the component and
4269
03:36:48,960 --> 03:36:53,760
they should not be inside anything. So,
4270
03:36:51,520 --> 03:36:57,680
don't put a hook inside an if statement
4271
03:36:53,760 --> 03:37:01,279
or inside a function.
4272
03:36:57,680 --> 03:37:03,680
Next, we'll give use effect a function.
4273
03:37:01,279 --> 03:37:07,200
So, inside these brackets, we'll type a
4274
03:37:03,680 --> 03:37:09,120
function using brackets, arrow, and
4275
03:37:07,200 --> 03:37:11,439
curly brackets.
4276
03:37:09,120 --> 03:37:14,080
As a reminder, when we give a function
4277
03:37:11,439 --> 03:37:16,160
to another function, we usually use an
4278
03:37:14,080 --> 03:37:19,920
arrow function because it's a bit
4279
03:37:16,160 --> 03:37:23,520
cleaner. So, React will run this
4280
03:37:19,920 --> 03:37:28,720
function after the component is created
4281
03:37:23,520 --> 03:37:33,200
and every time the component is updated
4282
03:37:28,720 --> 03:37:36,399
inside here. Let's try console.log
4283
03:37:33,200 --> 03:37:39,680
the string updated
4284
03:37:36,399 --> 03:37:42,080
and save.
4285
03:37:39,680 --> 03:37:45,120
And now if we go to our project and we
4286
03:37:42,080 --> 03:37:46,800
look at the console. So we'll rightclick
4287
03:37:45,120 --> 03:37:50,479
inspect
4288
03:37:46,800 --> 03:37:52,720
and we go to the console.
4289
03:37:50,479 --> 03:37:55,680
You'll see that after the component is
4290
03:37:52,720 --> 03:37:58,319
created, it runs a function and displays
4291
03:37:55,680 --> 03:38:00,720
updated. And then we're going to go to
4292
03:37:58,319 --> 03:38:05,920
our website and we're going to add a new
4293
03:38:00,720 --> 03:38:09,200
message like hello and press send.
4294
03:38:05,920 --> 03:38:12,479
So now after the component is updated,
4295
03:38:09,200 --> 03:38:15,760
React runs the function again. So, as
4296
03:38:12,479 --> 03:38:19,200
you can see, use effect lets us run some
4297
03:38:15,760 --> 03:38:21,120
code after the component is created or
4298
03:38:19,200 --> 03:38:24,720
updated.
4299
03:38:21,120 --> 03:38:27,359
So, now let's go back into our code
4300
03:38:24,720 --> 03:38:30,239
and we'll give use effect a second
4301
03:38:27,359 --> 03:38:33,279
parameter. So, at the end we'll type a
4302
03:38:30,239 --> 03:38:35,760
comma and we'll give a second parameter
4303
03:38:33,279 --> 03:38:39,600
which is an array.
4304
03:38:35,760 --> 03:38:43,600
So, this array controls when use effect
4305
03:38:39,600 --> 03:38:46,560
runs. If we give it an empty array, use
4306
03:38:43,600 --> 03:38:49,600
effect will only run once after the
4307
03:38:46,560 --> 03:38:52,479
component is created.
4308
03:38:49,600 --> 03:38:56,239
Now, inside this array, we can also put
4309
03:38:52,479 --> 03:39:00,000
some data such as our chat messages. So,
4310
03:38:56,239 --> 03:39:02,160
we'll type chat messages.
4311
03:39:00,000 --> 03:39:05,520
And now, React will also run this
4312
03:39:02,160 --> 03:39:08,720
function every time our data changes or
4313
03:39:05,520 --> 03:39:11,520
every time our chat messages changes.
4314
03:39:08,720 --> 03:39:14,640
If we save
4315
03:39:11,520 --> 03:39:17,120
and we look at the console,
4316
03:39:14,640 --> 03:39:20,160
use effect runs after the component is
4317
03:39:17,120 --> 03:39:24,399
created. And now if we add a message
4318
03:39:20,160 --> 03:39:27,680
like hello and press send,
4319
03:39:24,399 --> 03:39:29,520
use effect also runs after chat messages
4320
03:39:27,680 --> 03:39:33,120
changes.
4321
03:39:29,520 --> 03:39:36,800
So now let's go back to our code.
4322
03:39:33,120 --> 03:39:41,120
And this is called a dependency array.
4323
03:39:36,800 --> 03:39:44,319
It lets us control when use effect runs.
4324
03:39:41,120 --> 03:39:47,359
A best practice is to give use effect a
4325
03:39:44,319 --> 03:39:50,160
dependency array to avoid running this
4326
03:39:47,359 --> 03:39:52,720
function too often.
4327
03:39:50,160 --> 03:39:55,279
So now we learned hooks and the use
4328
03:39:52,720 --> 03:40:00,600
effect hook. And now we can run some
4329
03:39:55,279 --> 03:40:00,600
code after the chat messages changes.
4330
03:40:00,800 --> 03:40:07,680
The last step is after the chat messages
4331
03:40:04,479 --> 03:40:11,520
changes, we want to automatically scroll
4332
03:40:07,680 --> 03:40:14,239
to the bottom. To scroll to the bottom,
4333
03:40:11,520 --> 03:40:16,479
first we need to get the HTML element
4334
03:40:14,239 --> 03:40:18,160
that we want to scroll into our
4335
03:40:16,479 --> 03:40:21,120
JavaScript.
4336
03:40:18,160 --> 03:40:23,359
Let's go back to our code. And inside
4337
03:40:21,120 --> 03:40:26,960
this function, instead of console.log,
4338
03:40:23,359 --> 03:40:30,800
log we need to get this div chat
4339
03:40:26,960 --> 03:40:33,520
messages container into our JavaScript.
4340
03:40:30,800 --> 03:40:36,800
Now in normal JavaScript we might use
4341
03:40:33,520 --> 03:40:39,520
the DOM to get this element but remember
4342
03:40:36,800 --> 03:40:42,800
in React we should not use the DOM
4343
03:40:39,520 --> 03:40:45,840
manually. Instead we should use React
4344
03:40:42,800 --> 03:40:48,640
features to get this element
4345
03:40:45,840 --> 03:40:52,080
to get this element using React. We're
4346
03:40:48,640 --> 03:40:56,160
going to use another hook called use
4347
03:40:52,080 --> 03:40:58,960
ref. So at the top of this component,
4348
03:40:56,160 --> 03:41:04,880
we're going to type the code capital R
4349
03:40:58,960 --> 03:41:06,880
react dot use capital R ref and
4350
03:41:04,880 --> 03:41:10,399
brackets.
4351
03:41:06,880 --> 03:41:13,920
Use ref lets us automatically save an
4352
03:41:10,399 --> 03:41:19,040
HTML element from the component.
4353
03:41:13,920 --> 03:41:22,880
So this code creates a ref. A ref is a
4354
03:41:19,040 --> 03:41:25,840
container with special React features.
4355
03:41:22,880 --> 03:41:29,840
One special feature is we can give this
4356
03:41:25,840 --> 03:41:33,520
ref or this container to React and React
4357
03:41:29,840 --> 03:41:36,319
can automatically save an HTML element
4358
03:41:33,520 --> 03:41:37,840
inside this ref or inside this
4359
03:41:36,319 --> 03:41:40,479
container.
4360
03:41:37,840 --> 03:41:43,920
So between these brackets, we're first
4361
03:41:40,479 --> 03:41:46,560
going to put the value null.
4362
03:41:43,920 --> 03:41:49,680
So this is the initial value that is
4363
03:41:46,560 --> 03:41:52,319
saved inside this ref or inside this
4364
03:41:49,680 --> 03:41:56,560
container. So the ref is going to start
4365
03:41:52,319 --> 03:42:00,000
out as empty. So that's why we use null.
4366
03:41:56,560 --> 03:42:04,160
Now let's save an HTML element inside
4367
03:42:00,000 --> 03:42:07,920
this ref. To do that, first we're going
4368
03:42:04,160 --> 03:42:10,720
to save this ref in a variable. So at
4369
03:42:07,920 --> 03:42:15,439
the front we'll create a variable using
4370
03:42:10,720 --> 03:42:19,600
const and we'll name it chat messages
4371
03:42:15,439 --> 03:42:22,000
ref equals this result.
4372
03:42:19,600 --> 03:42:24,800
And then we're going to go to the HTML
4373
03:42:22,000 --> 03:42:29,359
element we want to save, which is this
4374
03:42:24,800 --> 03:42:31,120
div, the chat messages container.
4375
03:42:29,359 --> 03:42:35,920
And we're going to give this element a
4376
03:42:31,120 --> 03:42:38,239
special prop called ref equals curly
4377
03:42:35,920 --> 03:42:40,560
brackets. And between the curly
4378
03:42:38,239 --> 03:42:44,880
brackets, we're going to give it the ref
4379
03:42:40,560 --> 03:42:48,080
that we created above, which is chat
4380
03:42:44,880 --> 03:42:52,080
messages ref.
4381
03:42:48,080 --> 03:42:55,920
So now React will take this HTML element
4382
03:42:52,080 --> 03:42:58,640
and save it inside this ref or inside
4383
03:42:55,920 --> 03:43:01,840
this container.
4384
03:42:58,640 --> 03:43:05,520
To access the HTML element saved inside
4385
03:43:01,840 --> 03:43:07,600
a ref, we're going to go to use effect
4386
03:43:05,520 --> 03:43:14,479
and we're going to type the name of the
4387
03:43:07,600 --> 03:43:16,239
ref, which is chat messages ref and then
4388
03:43:14,479 --> 03:43:18,880
current.
4389
03:43:16,239 --> 03:43:21,760
Now the reason we put this code in use
4390
03:43:18,880 --> 03:43:25,439
effect is because remember use effect
4391
03:43:21,760 --> 03:43:27,840
runs after the component is created. So
4392
03:43:25,439 --> 03:43:30,960
we need to wait for the component to be
4393
03:43:27,840 --> 03:43:33,840
created first and then we can access
4394
03:43:30,960 --> 03:43:37,120
this HTML element.
4395
03:43:33,840 --> 03:43:40,080
So now let's console.log this value to
4396
03:43:37,120 --> 03:43:45,199
see what's inside this ref. So at the
4397
03:43:40,080 --> 03:43:50,399
front we'll console.log log open bracket
4398
03:43:45,199 --> 03:43:52,160
and close bracket and save.
4399
03:43:50,399 --> 03:43:54,000
And we'll open the console for our
4400
03:43:52,160 --> 03:43:57,199
website.
4401
03:43:54,000 --> 03:44:01,359
And you'll see that React saved the chat
4402
03:43:57,199 --> 03:44:04,000
messages container inside our ref.
4403
03:44:01,359 --> 03:44:06,560
Now that we have the HTML element, we
4404
03:44:04,000 --> 03:44:09,680
need to scroll. Let's learn how to
4405
03:44:06,560 --> 03:44:11,760
scroll it to the bottom. Let's go back
4406
03:44:09,680 --> 03:44:14,640
to our code
4407
03:44:11,760 --> 03:44:17,680
and first we're going to save this HTML
4408
03:44:14,640 --> 03:44:20,560
element in a variable. So we'll remove
4409
03:44:17,680 --> 03:44:24,479
this and at the front we'll create a
4410
03:44:20,560 --> 03:44:26,000
variable const and let's name it
4411
03:44:24,479 --> 03:44:30,720
container
4412
03:44:26,000 --> 03:44:33,680
lm equals this result.
4413
03:44:30,720 --> 03:44:36,479
So ending the variable name with lm
4414
03:44:33,680 --> 03:44:39,199
tells us that this variable contains an
4415
03:44:36,479 --> 03:44:41,840
HTML element.
4416
03:44:39,199 --> 03:44:44,319
and I'll resize my code editor so we can
4417
03:44:41,840 --> 03:44:47,520
see more of the code.
4418
03:44:44,319 --> 03:44:49,680
Next, we'll create a new line. And it's
4419
03:44:47,520 --> 03:44:53,120
a good idea to check if this element
4420
03:44:49,680 --> 03:44:59,520
actually exists before we scroll it. So,
4421
03:44:53,120 --> 03:45:03,040
we'll check if brackets container lm.
4422
03:44:59,520 --> 03:45:05,199
Finally, we'll type curly brackets.
4423
03:45:03,040 --> 03:45:09,520
And to scroll this element to the
4424
03:45:05,199 --> 03:45:14,160
bottom, we can use this code container
4425
03:45:09,520 --> 03:45:22,479
lm dot scroll top and we'll make it
4426
03:45:14,160 --> 03:45:26,160
equal container lm dot scroll height.
4427
03:45:22,479 --> 03:45:29,279
So scroll top means how far from the top
4428
03:45:26,160 --> 03:45:32,239
should we scroll and scroll height gives
4429
03:45:29,279 --> 03:45:34,720
us the total height of the element. So,
4430
03:45:32,239 --> 03:45:36,800
if we set the scroll top to the total
4431
03:45:34,720 --> 03:45:39,279
height, it's going to scroll all the way
4432
03:45:36,800 --> 03:45:42,080
down to the bottom, which is exactly
4433
03:45:39,279 --> 03:45:44,319
what we need. So, now let's save this
4434
03:45:42,080 --> 03:45:47,040
code
4435
03:45:44,319 --> 03:45:52,080
and we'll go to our website and we'll
4436
03:45:47,040 --> 03:45:54,720
add a message test and press send.
4437
03:45:52,080 --> 03:45:57,040
It runs the use effect function because
4438
03:45:54,720 --> 03:46:00,160
chat messages changed and it
4439
03:45:57,040 --> 03:46:01,760
automatically scrolls to the bottom.
4440
03:46:00,160 --> 03:46:03,840
Now, depending on the height of your
4441
03:46:01,760 --> 03:46:07,040
browser, you might need to add a few
4442
03:46:03,840 --> 03:46:10,239
more messages to see this effect. So, we
4443
03:46:07,040 --> 03:46:13,920
can also scroll up and then add another
4444
03:46:10,239 --> 03:46:16,720
message like thank you
4445
03:46:13,920 --> 03:46:19,439
and press send.
4446
03:46:16,720 --> 03:46:22,800
It runs use effect again and it
4447
03:46:19,439 --> 03:46:24,880
automatically scrolls to the bottom. And
4448
03:46:22,800 --> 03:46:27,680
that's all the major features that we
4449
03:46:24,880 --> 03:46:30,160
need for this project. I'll leave some
4450
03:46:27,680 --> 03:46:32,560
extra features like removing these
4451
03:46:30,160 --> 03:46:37,120
default messages and creating the
4452
03:46:32,560 --> 03:46:39,439
loading spinner as exercises.
4453
03:46:37,120 --> 03:46:42,399
And that's it. We created all the
4454
03:46:39,439 --> 03:46:45,439
styling and features that we need and we
4455
03:46:42,399 --> 03:46:47,760
finished the chatbot project. In the
4456
03:46:45,439 --> 03:46:50,800
next lesson, we'll learn how to do a
4457
03:46:47,760 --> 03:46:52,960
proper React setup so we can organize
4458
03:46:50,800 --> 03:46:55,199
our code into different files and
4459
03:46:52,960 --> 03:46:58,560
folders instead of putting everything
4460
03:46:55,199 --> 03:47:02,160
into one HTML file. And this will help
4461
03:46:58,560 --> 03:47:04,720
us work on bigger React projects.
4462
03:47:02,160 --> 03:47:07,600
And that's the end of this lesson. In
4463
03:47:04,720 --> 03:47:11,439
this lesson, we learned how to use CSS
4464
03:47:07,600 --> 03:47:15,040
with React. We styled the chatbot
4465
03:47:11,439 --> 03:47:18,319
project. We reviewed flexbox which lets
4466
03:47:15,040 --> 03:47:21,120
us create a flexible layout. We learned
4467
03:47:18,319 --> 03:47:24,319
the turnary operator which lets us
4468
03:47:21,120 --> 03:47:28,319
insert an if else statement directly in
4469
03:47:24,319 --> 03:47:30,960
the JSX. We learned hooks which lets us
4470
03:47:28,319 --> 03:47:34,080
insert React features into our
4471
03:47:30,960 --> 03:47:37,120
components. We learned the use effect
4472
03:47:34,080 --> 03:47:40,800
hook which lets us run some code after
4473
03:47:37,120 --> 03:47:43,840
the component is created or updated. We
4474
03:47:40,800 --> 03:47:47,359
learned the use ref hook which lets us
4475
03:47:43,840 --> 03:47:49,120
automatically save an HTML element from
4476
03:47:47,359 --> 03:47:52,160
the component
4477
03:47:49,120 --> 03:47:56,120
and we created the auto scroll feature
4478
03:47:52,160 --> 03:47:56,120
for the chat messages.
4479
03:47:56,239 --> 03:48:03,399
Here are some exercises to help you
4480
03:47:58,239 --> 03:48:03,399
practice what we learned in this lesson.
4481
03:48:53,920 --> 03:49:00,080
In this lesson, we're going to learn how
4482
03:48:55,840 --> 03:49:02,960
to do a proper React setup. Right now,
4483
03:49:00,080 --> 03:49:07,199
all of our HTML, CSS, and JavaScript
4484
03:49:02,960 --> 03:49:09,600
code is combined into one HTML file,
4485
03:49:07,199 --> 03:49:11,840
chatbot.html.
4486
03:49:09,600 --> 03:49:14,319
Now, it's not good to combine all of our
4487
03:49:11,840 --> 03:49:16,880
code in three different languages into
4488
03:49:14,319 --> 03:49:19,199
one file because if we work on bigger
4489
03:49:16,880 --> 03:49:21,439
projects, this file might contain
4490
03:49:19,199 --> 03:49:23,760
thousands of lines of code, and it will
4491
03:49:21,439 --> 03:49:27,439
be almost impossible to manage all of
4492
03:49:23,760 --> 03:49:29,840
that code if we put it all in one file.
4493
03:49:27,439 --> 03:49:32,479
Instead, we're going to do a proper
4494
03:49:29,840 --> 03:49:35,040
React setup that will let us split up
4495
03:49:32,479 --> 03:49:36,560
this code into different files and
4496
03:49:35,040 --> 03:49:39,120
folders.
4497
03:49:36,560 --> 03:49:42,560
To create this React setup, we'll need
4498
03:49:39,120 --> 03:49:45,520
to use the command line. So, first let's
4499
03:49:42,560 --> 03:49:48,640
review the command line. We're going to
4500
03:49:45,520 --> 03:49:50,560
go to VS Code and I'm going to make this
4501
03:49:48,640 --> 03:49:52,640
full screen.
4502
03:49:50,560 --> 03:49:55,120
And to open the command line, we're
4503
03:49:52,640 --> 03:49:57,359
going to click the menu at the top and
4504
03:49:55,120 --> 03:50:02,040
we're going to click the terminal menu
4505
03:49:57,359 --> 03:50:02,040
and we're going to open a new terminal.
4506
03:50:03,680 --> 03:50:10,800
So here we can give an instruction or a
4507
03:50:07,359 --> 03:50:15,800
command to the computer. For example,
4508
03:50:10,800 --> 03:50:15,800
we're going to type the command mkdir.
4509
03:50:16,000 --> 03:50:24,560
mkdir means make directory. A directory
4510
03:50:20,880 --> 03:50:28,960
is another name for a folder. So make
4511
03:50:24,560 --> 03:50:31,279
directory creates a new folder.
4512
03:50:28,960 --> 03:50:35,040
Then we'll type a space and we'll give
4513
03:50:31,279 --> 03:50:37,520
this folder a name like test.
4514
03:50:35,040 --> 03:50:40,960
If we press enter,
4515
03:50:37,520 --> 03:50:43,439
it creates a new folder named test. So
4516
03:50:40,960 --> 03:50:45,840
if we open our files on the left, you'll
4517
03:50:43,439 --> 03:50:47,600
see that it created a new folder named
4518
03:50:45,840 --> 03:50:49,840
test.
4519
03:50:47,600 --> 03:50:51,840
If that does not work for you, you can
4520
03:50:49,840 --> 03:50:54,319
check the troubleshooting steps in the
4521
03:50:51,840 --> 03:50:57,199
description.
4522
03:50:54,319 --> 03:51:00,720
So on this line here we can give
4523
03:50:57,199 --> 03:51:02,560
instructions or commands to our computer
4524
03:51:00,720 --> 03:51:04,399
and that's why this is called the
4525
03:51:02,560 --> 03:51:07,359
command line
4526
03:51:04,399 --> 03:51:09,600
and a terminal basically lets us use the
4527
03:51:07,359 --> 03:51:13,279
command line and it also lets us view
4528
03:51:09,600 --> 03:51:16,000
the output of the command line up here.
4529
03:51:13,279 --> 03:51:18,880
So we created a new folder using this
4530
03:51:16,000 --> 03:51:21,840
command. Now you might be wondering why
4531
03:51:18,880 --> 03:51:25,279
did the folder get created here inside
4532
03:51:21,840 --> 03:51:27,840
our project folder. As a review, the
4533
03:51:25,279 --> 03:51:29,680
command line runs inside a specific
4534
03:51:27,840 --> 03:51:32,160
folder.
4535
03:51:29,680 --> 03:51:35,199
To see which folder the command line is
4536
03:51:32,160 --> 03:51:40,160
currently running in, we can go to the
4537
03:51:35,199 --> 03:51:45,520
bottom and type this command pwd or
4538
03:51:40,160 --> 03:51:47,680
print working directory and press enter.
4539
03:51:45,520 --> 03:51:50,080
And this command tells us the command
4540
03:51:47,680 --> 03:51:53,520
line is currently running in the
4541
03:51:50,080 --> 03:51:56,560
react-course folder which is our project
4542
03:51:53,520 --> 03:51:58,720
folder right here. That means any
4543
03:51:56,560 --> 03:52:02,640
commands that we give to the computer
4544
03:51:58,720 --> 03:52:05,279
will be done inside this folder. If we
4545
03:52:02,640 --> 03:52:08,000
make a directory, it will create the new
4546
03:52:05,279 --> 03:52:11,760
directory inside this folder like we see
4547
03:52:08,000 --> 03:52:16,479
here. If we give it another command like
4548
03:52:11,760 --> 03:52:19,040
ls or list and press enter,
4549
03:52:16,479 --> 03:52:22,800
it will list everything inside this
4550
03:52:19,040 --> 03:52:25,040
folder. So it will list everything here.
4551
03:52:22,800 --> 03:52:28,960
As you can see, the command line runs
4552
03:52:25,040 --> 03:52:31,680
inside a specific folder.
4553
03:52:28,960 --> 03:52:34,080
Now this folder that the command line is
4554
03:52:31,680 --> 03:52:37,760
running in is called the working
4555
03:52:34,080 --> 03:52:39,439
directory or the working folder.
4556
03:52:37,760 --> 03:52:42,000
The last thing we need to review about
4557
03:52:39,439 --> 03:52:45,040
the command line is how to change the
4558
03:52:42,000 --> 03:52:47,199
working directory. To do that, we're
4559
03:52:45,040 --> 03:52:50,880
going to go to the bottom and we're
4560
03:52:47,199 --> 03:52:54,560
going to type the command cd or change
4561
03:52:50,880 --> 03:52:56,800
directory and then press space and then
4562
03:52:54,560 --> 03:53:00,000
the name of the folder we want to change
4563
03:52:56,800 --> 03:53:01,840
to like test.
4564
03:53:00,000 --> 03:53:04,239
the command line will look for this
4565
03:53:01,840 --> 03:53:06,960
folder in the working directory which is
4566
03:53:04,239 --> 03:53:11,120
here and then it's going to go into this
4567
03:53:06,960 --> 03:53:13,920
folder. So now if we press enter and
4568
03:53:11,120 --> 03:53:15,840
then type print working directory and
4569
03:53:13,920 --> 03:53:17,920
press enter
4570
03:53:15,840 --> 03:53:21,199
it will tell us the command line is now
4571
03:53:17,920 --> 03:53:23,439
running inside the test folder.
4572
03:53:21,199 --> 03:53:26,080
So the command line basically went
4573
03:53:23,439 --> 03:53:29,760
inside this test folder and it's running
4574
03:53:26,080 --> 03:53:33,120
inside here. By the way, this is known
4575
03:53:29,760 --> 03:53:35,600
as a file path. The slash here means
4576
03:53:33,120 --> 03:53:38,239
that the test folder is inside the
4577
03:53:35,600 --> 03:53:41,760
react-course folder.
4578
03:53:38,239 --> 03:53:43,600
So now if we give a command like ls and
4579
03:53:41,760 --> 03:53:46,239
press enter,
4580
03:53:43,600 --> 03:53:48,239
it will list everything inside the test
4581
03:53:46,239 --> 03:53:51,520
folder because the command line is
4582
03:53:48,239 --> 03:53:53,680
running inside this test folder and it
4583
03:53:51,520 --> 03:53:55,600
tells us there's nothing inside the test
4584
03:53:53,680 --> 03:53:58,960
folder.
4585
03:53:55,600 --> 03:54:02,960
Finally, to get out of the test folder,
4586
03:53:58,960 --> 03:54:07,040
we can type cd or change directory and
4587
03:54:02,960 --> 03:54:10,960
then space and use the special value dot
4588
03:54:07,040 --> 03:54:12,479
dot. Dot dot represents the outer
4589
03:54:10,960 --> 03:54:14,880
folder.
4590
03:54:12,479 --> 03:54:17,520
If we press enter,
4591
03:54:14,880 --> 03:54:20,640
the command line will go out of the test
4592
03:54:17,520 --> 03:54:23,439
folder and back into the react-course
4593
03:54:20,640 --> 03:54:26,560
folder.
4594
03:54:23,439 --> 03:54:30,399
If we type the command pwd or print
4595
03:54:26,560 --> 03:54:32,319
working directory and press enter,
4596
03:54:30,399 --> 03:54:36,000
it will tell us the command line is
4597
03:54:32,319 --> 03:54:39,520
running in the react-ourse folder again.
4598
03:54:36,000 --> 03:54:42,000
So using cd or change directory, we can
4599
03:54:39,520 --> 03:54:44,479
change which folder the command line is
4600
03:54:42,000 --> 03:54:47,680
running in and we can basically move
4601
03:54:44,479 --> 03:54:51,040
around our computer. So this is just a
4602
03:54:47,680 --> 03:54:53,199
review of the command line.
4603
03:54:51,040 --> 03:54:56,080
The second thing we need for the React
4604
03:54:53,199 --> 03:55:00,080
setup is NodeJS.
4605
03:54:56,080 --> 03:55:02,080
So, we're going to review NodeJS.
4606
03:55:00,080 --> 03:55:05,760
First, make sure you have the correct
4607
03:55:02,080 --> 03:55:07,600
version of NodeJS installed. To do that,
4608
03:55:05,760 --> 03:55:09,199
you can click this link in the
4609
03:55:07,600 --> 03:55:12,000
description.
4610
03:55:09,199 --> 03:55:14,160
Scroll down to lesson 5 and follow the
4611
03:55:12,000 --> 03:55:17,800
instructions here to install the correct
4612
03:55:14,160 --> 03:55:17,800
version of NodeJS.
4613
03:55:17,920 --> 03:55:23,520
After Node.js is installed, let's go
4614
03:55:20,800 --> 03:55:26,560
back to VS Code. And if you just
4615
03:55:23,520 --> 03:55:28,960
installed Node.js, we need to restart VS
4616
03:55:26,560 --> 03:55:30,479
Code to make sure that Node.js is
4617
03:55:28,960 --> 03:55:32,479
loaded.
4618
03:55:30,479 --> 03:55:34,800
So, we're going to click X in the top
4619
03:55:32,479 --> 03:55:38,239
right of the terminal
4620
03:55:34,800 --> 03:55:40,160
and then close VS Code.
4621
03:55:38,239 --> 03:55:43,160
And then we're going to open VS Code
4622
03:55:40,160 --> 03:55:43,160
again.
4623
03:55:43,199 --> 03:55:46,880
And we're going to open our project
4624
03:55:44,720 --> 03:55:49,680
folder, React-Course.
4625
03:55:46,880 --> 03:55:52,080
and we're going to open a new terminal.
4626
03:55:49,680 --> 03:55:54,720
So, in the top menu, we're going to go
4627
03:55:52,080 --> 03:55:58,040
to the terminal section and then open a
4628
03:55:54,720 --> 03:55:58,040
new terminal.
4629
03:55:58,080 --> 03:56:05,120
Now, when we install NodeJS, it's going
4630
03:56:01,279 --> 03:56:07,439
to add a new command to our command line
4631
03:56:05,120 --> 03:56:10,239
called node.
4632
03:56:07,439 --> 03:56:14,000
So, the node command lets us run
4633
03:56:10,239 --> 03:56:17,279
JavaScript code outside of a browser.
4634
03:56:14,000 --> 03:56:20,000
For example, here we have a file called
4635
03:56:17,279 --> 03:56:22,000
react-basics.js
4636
03:56:20,000 --> 03:56:24,399
and inside we have some simple
4637
03:56:22,000 --> 03:56:26,800
JavaScript code. If you don't have this
4638
03:56:24,399 --> 03:56:30,239
file, feel free to pause the video and
4639
03:56:26,800 --> 03:56:32,880
create this file. Now we can give this
4640
03:56:30,239 --> 03:56:36,239
file to node and it will run the
4641
03:56:32,880 --> 03:56:38,720
JavaScript code in this file. So here
4642
03:56:36,239 --> 03:56:42,000
we'll use the node command and type
4643
03:56:38,720 --> 03:56:45,479
space and give it the file to run which
4644
03:56:42,000 --> 03:56:45,479
is react-basics.js
4645
03:56:46,479 --> 03:56:52,080
and press enter.
4646
03:56:49,359 --> 03:56:56,160
So this will run the JavaScript inside
4647
03:56:52,080 --> 03:56:59,279
this file and a console.logs hello. So
4648
03:56:56,160 --> 03:57:02,000
as you can see the node command lets us
4649
03:56:59,279 --> 03:57:03,520
run JavaScript code outside of our
4650
03:57:02,000 --> 03:57:06,560
browser.
4651
03:57:03,520 --> 03:57:09,840
Now when we install Node.js, it also
4652
03:57:06,560 --> 03:57:13,199
adds another command to our command line
4653
03:57:09,840 --> 03:57:17,359
called npm.
4654
03:57:13,199 --> 03:57:19,840
npm stands for node package manager. A
4655
03:57:17,359 --> 03:57:24,399
package is basically an external
4656
03:57:19,840 --> 03:57:28,640
library. So npm lets us install external
4657
03:57:24,399 --> 03:57:32,319
libraries or packages into our project.
4658
03:57:28,640 --> 03:57:35,120
To do that, we can use the command npm
4659
03:57:32,319 --> 03:57:38,000
space install
4660
03:57:35,120 --> 03:57:41,040
and then space and then the name of an
4661
03:57:38,000 --> 03:57:44,239
external library or package.
4662
03:57:41,040 --> 03:57:48,000
As an example, we'll install the package
4663
03:57:44,239 --> 03:57:50,399
super simple dev.
4664
03:57:48,000 --> 03:57:56,040
This is a package that I created that we
4665
03:57:50,399 --> 03:57:56,040
can use as an example and press enter.
4666
03:57:57,600 --> 03:58:02,479
On Windows, if you get an error like
4667
03:58:00,000 --> 03:58:05,120
this, you'll need to run this command
4668
03:58:02,479 --> 03:58:08,080
first
4669
03:58:05,120 --> 03:58:10,960
and then run npm install super simple
4670
03:58:08,080 --> 03:58:13,279
dev again.
4671
03:58:10,960 --> 03:58:15,279
If that still does not work for you, you
4672
03:58:13,279 --> 03:58:18,520
can check the troubleshooting steps in
4673
03:58:15,279 --> 03:58:18,520
the description.
4674
03:58:18,880 --> 03:58:25,199
Once npm install finishes, npm will
4675
03:58:22,640 --> 03:58:28,080
download this external library or this
4676
03:58:25,199 --> 03:58:31,359
package into a folder called node
4677
03:58:28,080 --> 03:58:35,199
modules. If we open this folder, we see
4678
03:58:31,359 --> 03:58:37,120
the super simple dev package and inside
4679
03:58:35,199 --> 03:58:39,840
this contains the code that we've been
4680
03:58:37,120 --> 03:58:41,439
using in the chatbot project like
4681
03:58:39,840 --> 03:58:44,720
react.js
4682
03:58:41,439 --> 03:58:47,920
and chatbot.js.
4683
03:58:44,720 --> 03:58:50,880
npm contains millions of packages that
4684
03:58:47,920 --> 03:58:53,199
we can download and use in our project
4685
03:58:50,880 --> 03:58:56,920
and you can search for packages on the
4686
03:58:53,199 --> 03:58:56,920
npm website.
4687
03:58:57,120 --> 03:59:02,560
So this is just a review of NodeJS and
4688
03:59:00,239 --> 03:59:05,920
npm.
4689
03:59:02,560 --> 03:59:09,520
Now that we reviewed NodeJS and npm,
4690
03:59:05,920 --> 03:59:12,479
we're ready to do a proper React setup.
4691
03:59:09,520 --> 03:59:15,680
We just learned that npm has millions of
4692
03:59:12,479 --> 03:59:19,199
packages available to use. Some of these
4693
03:59:15,680 --> 03:59:22,080
packages also add a command to our
4694
03:59:19,199 --> 03:59:25,199
command line. For example, there's a
4695
03:59:22,080 --> 03:59:28,479
package called create-vit.
4696
03:59:25,199 --> 03:59:31,840
If we install this package, it also adds
4697
03:59:28,479 --> 03:59:33,600
a command to our command line called
4698
03:59:31,840 --> 03:59:37,120
create-vit.
4699
03:59:33,600 --> 03:59:40,319
The create-vit command helps us set up a
4700
03:59:37,120 --> 03:59:43,359
new React project.
4701
03:59:40,319 --> 03:59:46,640
To use the create-v command, we need to
4702
03:59:43,359 --> 03:59:49,840
do two steps. Number one, we need to
4703
03:59:46,640 --> 03:59:52,160
install the create-vt package. And
4704
03:59:49,840 --> 03:59:55,920
number two, we're going to run the
4705
03:59:52,160 --> 03:59:59,040
create-v command in the command line.
4706
03:59:55,920 --> 04:00:02,239
Now, npm actually has a shortcut for
4707
03:59:59,040 --> 04:00:06,880
this called npx.
4708
04:00:02,239 --> 04:00:10,720
npx does these two steps in one step.
4709
04:00:06,880 --> 04:00:13,040
The x means execute.
4710
04:00:10,720 --> 04:00:18,479
So let's go to our command line and
4711
04:00:13,040 --> 04:00:22,800
let's type the command npx create-vt.
4712
04:00:18,479 --> 04:00:25,359
And then we'll also type the at symbol.
4713
04:00:22,800 --> 04:00:28,960
At means we're going to use a specific
4714
04:00:25,359 --> 04:00:30,720
version of the create-vit command. For
4715
04:00:28,960 --> 04:00:33,040
this video, we're going to use a
4716
04:00:30,720 --> 04:00:35,840
specific version so that you have the
4717
04:00:33,040 --> 04:00:37,600
same code that I do and you can follow
4718
04:00:35,840 --> 04:00:40,160
along.
4719
04:00:37,600 --> 04:00:42,239
To get the version of create-vt we're
4720
04:00:40,160 --> 04:00:44,960
going to use, we're going to click this
4721
04:00:42,239 --> 04:00:48,319
link in the description.
4722
04:00:44,960 --> 04:00:51,520
Go to lesson 5 and here you'll find the
4723
04:00:48,319 --> 04:00:55,199
version of create-vt we're going to use.
4724
04:00:51,520 --> 04:00:57,520
You can also find the full command here.
4725
04:00:55,199 --> 04:00:59,840
By the way, the version that you see in
4726
04:00:57,520 --> 04:01:02,640
this video might be different than the
4727
04:00:59,840 --> 04:01:04,640
version you see on your page. You should
4728
04:01:02,640 --> 04:01:07,920
always use the version that you see on
4729
04:01:04,640 --> 04:01:11,199
your page. I did this because it's hard
4730
04:01:07,920 --> 04:01:16,000
to update the video. So, this lets me
4731
04:01:11,199 --> 04:01:19,520
easily update the version in the future.
4732
04:01:16,000 --> 04:01:21,439
So, select the version on your page.
4733
04:01:19,520 --> 04:01:23,520
Copy.
4734
04:01:21,439 --> 04:01:27,279
And in our command line, we're going to
4735
04:01:23,520 --> 04:01:30,640
paste it after the at.
4736
04:01:27,279 --> 04:01:33,680
So again, this command will install the
4737
04:01:30,640 --> 04:01:37,600
create-vt package and then it will run
4738
04:01:33,680 --> 04:01:40,720
the create-vit command. The create-v
4739
04:01:37,600 --> 04:01:42,239
command will help us set up a new react
4740
04:01:40,720 --> 04:01:44,720
project.
4741
04:01:42,239 --> 04:01:48,720
By the way, in some tutorials, they
4742
04:01:44,720 --> 04:01:50,960
might use the command npm create vit.
4743
04:01:48,720 --> 04:01:53,960
This is just a shortcut for
4744
04:01:50,960 --> 04:01:53,960
npxcreate-vit.
4745
04:01:55,199 --> 04:02:00,720
Now let's press enter.
4746
04:01:58,880 --> 04:02:03,840
If it says it needs to install this
4747
04:02:00,720 --> 04:02:07,160
package, we can press y for yes and
4748
04:02:03,840 --> 04:02:07,160
press enter.
4749
04:02:07,520 --> 04:02:14,000
Now the create-v command will ask us
4750
04:02:10,800 --> 04:02:18,720
some questions. First it will ask us for
4751
04:02:14,000 --> 04:02:23,120
a project name. So let's name it chatbot
4752
04:02:18,720 --> 04:02:25,279
dash project and press enter.
4753
04:02:23,120 --> 04:02:28,239
And then it will ask us what technology
4754
04:02:25,279 --> 04:02:31,199
we want to use in our project. So we're
4755
04:02:28,239 --> 04:02:34,800
going to press the down arrow and select
4756
04:02:31,199 --> 04:02:37,120
react and press enter.
4757
04:02:34,800 --> 04:02:39,199
And it will ask us what other tools we
4758
04:02:37,120 --> 04:02:42,720
want in our project. We're going to
4759
04:02:39,199 --> 04:02:46,000
press down and just select JavaScript
4760
04:02:42,720 --> 04:02:48,880
and press enter.
4761
04:02:46,000 --> 04:02:52,560
and create vit will create an empty
4762
04:02:48,880 --> 04:02:54,880
React project at this location or this
4763
04:02:52,560 --> 04:02:57,520
folder right here.
4764
04:02:54,880 --> 04:03:00,479
Next, we're going to go into this folder
4765
04:02:57,520 --> 04:03:03,520
with our command line. To do that, we
4766
04:03:00,479 --> 04:03:06,640
can use the command cd or change
4767
04:03:03,520 --> 04:03:12,880
directory and give it the folder name
4768
04:03:06,640 --> 04:03:17,439
chatbot dash project and press enter.
4769
04:03:12,880 --> 04:03:19,520
If we type pwd and press enter,
4770
04:03:17,439 --> 04:03:21,760
it will tell us the command line is now
4771
04:03:19,520 --> 04:03:27,040
running inside of the chatbot project
4772
04:03:21,760 --> 04:03:28,800
folder. So, it's running inside of here.
4773
04:03:27,040 --> 04:03:31,199
The next thing we're going to do is
4774
04:03:28,800 --> 04:03:33,199
we're going to run the command npm
4775
04:03:31,199 --> 04:03:35,760
install.
4776
04:03:33,199 --> 04:03:40,359
Inside our chatbot project folder, we
4777
04:03:35,760 --> 04:03:40,359
have a special file called package.json.
4778
04:03:40,479 --> 04:03:46,000
So, package.json JSON contains a list of
4779
04:03:43,680 --> 04:03:49,439
all the packages that are needed for
4780
04:03:46,000 --> 04:03:51,920
this project. So, npm install is going
4781
04:03:49,439 --> 04:03:54,080
to install all of these packages at the
4782
04:03:51,920 --> 04:03:56,000
same time.
4783
04:03:54,080 --> 04:03:58,399
So, we'll go back to the command line
4784
04:03:56,000 --> 04:04:05,439
and scroll down and we'll run the
4785
04:03:58,399 --> 04:04:07,199
command npm install and press enter.
4786
04:04:05,439 --> 04:04:09,840
Now, this might take a while because
4787
04:04:07,199 --> 04:04:11,600
it's going to install a lot of packages.
4788
04:04:09,840 --> 04:04:14,600
So, we just got to wait until it
4789
04:04:11,600 --> 04:04:14,600
finishes.
4790
04:04:15,359 --> 04:04:20,720
After npm install finishes, all of the
4791
04:04:18,319 --> 04:04:23,760
packages here are going to be downloaded
4792
04:04:20,720 --> 04:04:25,680
into the node modules folder. So, if we
4793
04:04:23,760 --> 04:04:28,000
open this, you'll see that we have a lot
4794
04:04:25,680 --> 04:04:30,640
of packages here. Now, you'll notice
4795
04:04:28,000 --> 04:04:33,600
that there's more packages here than we
4796
04:04:30,640 --> 04:04:36,080
have in the package.json file, and
4797
04:04:33,600 --> 04:04:39,600
that's because some packages require
4798
04:04:36,080 --> 04:04:43,640
other packages. So, npm helps us install
4799
04:04:39,600 --> 04:04:43,640
everything that we need.
4800
04:04:44,479 --> 04:04:50,239
The last step is to run the command npm
4801
04:04:48,479 --> 04:04:52,960
rundev.
4802
04:04:50,239 --> 04:04:57,439
So, this will start up our new React
4803
04:04:52,960 --> 04:04:59,920
website. Let's press enter.
4804
04:04:57,439 --> 04:05:02,880
And now we can view this website by
4805
04:04:59,920 --> 04:05:07,680
opening this URL in our browser. So,
4806
04:05:02,880 --> 04:05:10,479
we're going to select this URL and copy
4807
04:05:07,680 --> 04:05:12,160
and then open our browser.
4808
04:05:10,479 --> 04:05:14,560
And I'm going to make it full screen so
4809
04:05:12,160 --> 04:05:20,880
we can see more of it. We'll create a
4810
04:05:14,560 --> 04:05:23,840
new tab and paste and press enter.
4811
04:05:20,880 --> 04:05:26,720
And now we have a new React website that
4812
04:05:23,840 --> 04:05:29,520
we can work with. So that's how we do a
4813
04:05:26,720 --> 04:05:34,319
proper React setup using the command
4814
04:05:29,520 --> 04:05:36,640
line npm and vit.
4815
04:05:34,319 --> 04:05:40,479
Now that we created a proper React
4816
04:05:36,640 --> 04:05:42,880
setup, let's go back to VS Code
4817
04:05:40,479 --> 04:05:47,199
and we'll learn what each of these files
4818
04:05:42,880 --> 04:05:50,800
and folders do. As a review, the node
4819
04:05:47,199 --> 04:05:53,040
modules folder is where npm installs all
4820
04:05:50,800 --> 04:05:56,000
of the packages.
4821
04:05:53,040 --> 04:05:58,399
Next, we're going to look at the src or
4822
04:05:56,000 --> 04:06:01,120
the source folder.
4823
04:05:58,399 --> 04:06:04,479
This folder contains the code for our
4824
04:06:01,120 --> 04:06:07,760
website. So, all our JavaScript and CSS
4825
04:06:04,479 --> 04:06:11,439
code goes in here. And remember, when
4826
04:06:07,760 --> 04:06:14,080
using React, we write JSX code rather
4827
04:06:11,439 --> 04:06:20,160
than normal JavaScript code. So, the
4828
04:06:14,080 --> 04:06:23,920
files here end with JSX instead of JS.
4829
04:06:20,160 --> 04:06:26,640
Next, this assets folder contains images
4830
04:06:23,920 --> 04:06:28,960
that we want to display on our website.
4831
04:06:26,640 --> 04:06:33,199
So, here we have an image of the React
4832
04:06:28,960 --> 04:06:35,680
logo in SVG format.
4833
04:06:33,199 --> 04:06:39,279
The last folder here is the public
4834
04:06:35,680 --> 04:06:42,720
folder. The public folder contains files
4835
04:06:39,279 --> 04:06:45,840
that should be available to the public.
4836
04:06:42,720 --> 04:06:48,720
Public means we can access these using a
4837
04:06:45,840 --> 04:06:52,000
URL. That means that when this website
4838
04:06:48,720 --> 04:06:54,720
is on the internet, I can give you a URL
4839
04:06:52,000 --> 04:06:56,800
to this image and you'll be able to
4840
04:06:54,720 --> 04:06:59,600
access it.
4841
04:06:56,800 --> 04:07:02,960
So here they just put this vit image in
4842
04:06:59,600 --> 04:07:05,680
the public folder as an example. So now
4843
04:07:02,960 --> 04:07:09,040
we understand the folders that we have
4844
04:07:05,680 --> 04:07:12,239
in this setup.
4845
04:07:09,040 --> 04:07:16,319
Next let's learn about all the files in
4846
04:07:12,239 --> 04:07:20,319
this react setup. The first file.get Git
4847
04:07:16,319 --> 04:07:24,000
ignore is used by a tool called git. Git
4848
04:07:20,319 --> 04:07:26,399
helps us track changes in our code. For
4849
04:07:24,000 --> 04:07:28,880
this chatbot project, we're not going to
4850
04:07:26,399 --> 04:07:30,960
use git. So this file doesn't do
4851
04:07:28,880 --> 04:07:32,800
anything. So we don't need to change
4852
04:07:30,960 --> 04:07:37,720
this file.
4853
04:07:32,800 --> 04:07:37,720
The next file here is eslint.config.js.
4854
04:07:37,920 --> 04:07:44,960
So this is used by a tool called eslint.
4855
04:07:42,080 --> 04:07:49,199
eslint highlights problems in our
4856
04:07:44,960 --> 04:07:52,560
JavaScript code. Let's do an example. If
4857
04:07:49,199 --> 04:07:56,199
we open the source folder and we open
4858
04:07:52,560 --> 04:07:56,199
the file app.jsx
4859
04:07:56,319 --> 04:08:01,600
and let's say that here I accidentally
4860
04:07:58,640 --> 04:08:03,120
misspell setc count by removing the last
4861
04:08:01,600 --> 04:08:05,520
t.
4862
04:08:03,120 --> 04:08:09,439
My VS code doesn't really tell me that
4863
04:08:05,520 --> 04:08:11,439
there is a problem here. But ESLint will
4864
04:08:09,439 --> 04:08:15,040
tell me very clearly that there is a
4865
04:08:11,439 --> 04:08:18,479
problem. To use ESLint, we need to
4866
04:08:15,040 --> 04:08:21,520
install the ESLint extension. So we can
4867
04:08:18,479 --> 04:08:26,319
go to the extension section
4868
04:08:21,520 --> 04:08:30,279
and we'll install the extension ESLint
4869
04:08:26,319 --> 04:08:30,279
and we'll install it.
4870
04:08:31,040 --> 04:08:36,080
After it's installed, if we go back to
4871
04:08:33,680 --> 04:08:37,920
app.jsx
4872
04:08:36,080 --> 04:08:41,120
now, it's going to tell me with a red
4873
04:08:37,920 --> 04:08:44,479
underline that this variable name does
4874
04:08:41,120 --> 04:08:48,000
not match the variable name down here.
4875
04:08:44,479 --> 04:08:51,600
So, if we scroll up and we fix the
4876
04:08:48,000 --> 04:08:55,040
spelling by adding back the t, that red
4877
04:08:51,600 --> 04:08:57,680
underline or that error will go away.
4878
04:08:55,040 --> 04:09:00,479
As you can see, eslint is a really
4879
04:08:57,680 --> 04:09:03,680
useful tool that highlights problems in
4880
04:09:00,479 --> 04:09:07,160
our JavaScript code. So, if we go back
4881
04:09:03,680 --> 04:09:07,160
to the eslint.config.js,
4882
04:09:07,760 --> 04:09:13,680
this file configures eslint, it tells it
4883
04:09:11,040 --> 04:09:15,680
what to check for. So, this
4884
04:09:13,680 --> 04:09:17,920
configuration file is actually good
4885
04:09:15,680 --> 04:09:20,640
enough for now. So, we don't need to
4886
04:09:17,920 --> 04:09:23,840
change this file.
4887
04:09:20,640 --> 04:09:25,359
The next file in our setup is index.htm.
4888
04:09:23,840 --> 04:09:28,960
html.
4889
04:09:25,359 --> 04:09:32,319
This contains all the HTML code for our
4890
04:09:28,960 --> 04:09:35,760
React website. So, this is the same
4891
04:09:32,319 --> 04:09:38,080
thing as chatbot.html.
4892
04:09:35,760 --> 04:09:41,120
The main difference is instead of
4893
04:09:38,080 --> 04:09:44,000
putting all of our code inside the HTML
4894
04:09:41,120 --> 04:09:46,160
file, index.html
4895
04:09:44,000 --> 04:09:47,840
loads our JavaScript from a different
4896
04:09:46,160 --> 04:09:50,399
file.
4897
04:09:47,840 --> 04:09:53,760
So this is the one file that we may need
4898
04:09:50,399 --> 04:09:57,359
to change inside this setup. If we want
4899
04:09:53,760 --> 04:10:00,160
to change the HTML for our website,
4900
04:09:57,359 --> 04:10:04,399
for example, we can change the title at
4901
04:10:00,160 --> 04:10:08,080
the top of the tabs by removing this
4902
04:10:04,399 --> 04:10:09,680
and let's change it to chatbot
4903
04:10:08,080 --> 04:10:12,080
project.
4904
04:10:09,680 --> 04:10:14,720
If we save,
4905
04:10:12,080 --> 04:10:17,359
vit will automatically refresh our page
4906
04:10:14,720 --> 04:10:19,600
just like live server does. And if we
4907
04:10:17,359 --> 04:10:23,279
open our website, you'll see that the
4908
04:10:19,600 --> 04:10:25,520
title at the top changed.
4909
04:10:23,279 --> 04:10:27,920
Let's go back.
4910
04:10:25,520 --> 04:10:32,160
So the index.html
4911
04:10:27,920 --> 04:10:34,560
contains the HTML code for this website.
4912
04:10:32,160 --> 04:10:37,560
The next file in this setup is
4913
04:10:34,560 --> 04:10:37,560
package-lock.json.
4914
04:10:37,840 --> 04:10:44,880
As a review, package.json has a list of
4915
04:10:41,680 --> 04:10:48,399
packages that we need to install to run
4916
04:10:44,880 --> 04:10:52,160
this project. What package lock.json
4917
04:10:48,399 --> 04:10:55,520
does is it saves the version numbers of
4918
04:10:52,160 --> 04:10:58,720
all the packages that we installed. So
4919
04:10:55,520 --> 04:11:00,960
the next time we run npm install, this
4920
04:10:58,720 --> 04:11:04,640
file makes sure that we get the same
4921
04:11:00,960 --> 04:11:07,840
versions for all of these packages.
4922
04:11:04,640 --> 04:11:10,800
Now npm automatically updates package
4923
04:11:07,840 --> 04:11:14,479
lock.json. So we don't need to change
4924
04:11:10,800 --> 04:11:17,439
this file. The next file in the setup is
4925
04:11:14,479 --> 04:11:19,600
package.json. JSON and this basically
4926
04:11:17,439 --> 04:11:21,920
contains the list of packages that we
4927
04:11:19,600 --> 04:11:24,960
need in our project as well as some
4928
04:11:21,920 --> 04:11:28,160
other information about our project.
4929
04:11:24,960 --> 04:11:30,399
When we npm install a package, it will
4930
04:11:28,160 --> 04:11:32,720
automatically update this list of
4931
04:11:30,399 --> 04:11:34,560
packages. So we don't need to change
4932
04:11:32,720 --> 04:11:38,800
this file.
4933
04:11:34,560 --> 04:11:41,520
Next, we have the file readme.md.
4934
04:11:38,800 --> 04:11:44,160
So this is just text. It provides more
4935
04:11:41,520 --> 04:11:46,560
information about this project. And this
4936
04:11:44,160 --> 04:11:48,720
is not code. This is just forformational
4937
04:11:46,560 --> 04:11:50,560
purposes. So we don't need to change
4938
04:11:48,720 --> 04:11:54,239
this file.
4939
04:11:50,560 --> 04:11:58,319
Finally, vit.config.js
4940
04:11:54,239 --> 04:12:02,239
configures vit. Vit is the tool that we
4941
04:11:58,319 --> 04:12:04,960
used to set up this project. Vit also
4942
04:12:02,239 --> 04:12:07,680
helps us build the website. That means
4943
04:12:04,960 --> 04:12:11,439
it takes all the JavaScript and CSS and
4944
04:12:07,680 --> 04:12:15,040
then loads them into the HTML file. Vit
4945
04:12:11,439 --> 04:12:19,359
also creates a server. A server puts our
4946
04:12:15,040 --> 04:12:22,239
website at a URL. For example, here it
4947
04:12:19,359 --> 04:12:23,840
put our website at the URL localhost
4948
04:12:22,239 --> 04:12:26,319
5173.
4949
04:12:23,840 --> 04:12:29,920
So this lets us view the website in a
4950
04:12:26,319 --> 04:12:31,920
web browser like a real website. When we
4951
04:12:29,920 --> 04:12:34,479
were building the chatbot project in the
4952
04:12:31,920 --> 04:12:37,840
previous lessons, we used an extension
4953
04:12:34,479 --> 04:12:40,560
called live server. Live server
4954
04:12:37,840 --> 04:12:44,479
automatically refreshes the website when
4955
04:12:40,560 --> 04:12:46,640
we change some code. The VIT server also
4956
04:12:44,479 --> 04:12:49,600
refreshes the website when we change
4957
04:12:46,640 --> 04:12:52,080
some code. So the VIT server is actually
4958
04:12:49,600 --> 04:12:55,359
a replacement for the live server that
4959
04:12:52,080 --> 04:12:57,840
we used earlier. So those are three main
4960
04:12:55,359 --> 04:13:00,479
features of Vit and you can see that
4961
04:12:57,840 --> 04:13:03,840
it's really helpful for setting up and
4962
04:13:00,479 --> 04:13:06,319
developing a React website. We'll go
4963
04:13:03,840 --> 04:13:09,760
back to VS Code.
4964
04:13:06,319 --> 04:13:13,279
So the vit config.js file basically
4965
04:13:09,760 --> 04:13:15,520
configures vit. So this config file is
4966
04:13:13,279 --> 04:13:18,880
good enough. So we don't need to change
4967
04:13:15,520 --> 04:13:22,239
this file. And that's it. Now we
4968
04:13:18,880 --> 04:13:26,239
understand all the files and folders in
4969
04:13:22,239 --> 04:13:29,040
this react setup. As you can see most of
4970
04:13:26,239 --> 04:13:31,600
these files are just configuration files
4971
04:13:29,040 --> 04:13:33,600
and we don't need to change them. The
4972
04:13:31,600 --> 04:13:37,199
only files we need to focus on and
4973
04:13:33,600 --> 04:13:40,399
change are the HTML file and the files
4974
04:13:37,199 --> 04:13:44,239
in the source folder which is our CSS
4975
04:13:40,399 --> 04:13:47,040
and JavaScript files.
4976
04:13:44,239 --> 04:13:50,319
Now that we understand all the files and
4977
04:13:47,040 --> 04:13:53,520
folders in this React setup, let's move
4978
04:13:50,319 --> 04:13:57,359
our previous chatbot project into this
4979
04:13:53,520 --> 04:14:00,159
setup. First, we can close the chatbot
4980
04:13:57,359 --> 04:14:02,720
project folder for now. And let's
4981
04:14:00,159 --> 04:14:05,760
actually remove the node modules folder
4982
04:14:02,720 --> 04:14:09,800
that we created earlier for practice. So
4983
04:14:05,760 --> 04:14:09,800
we'll delete this folder
4984
04:14:10,720 --> 04:14:15,760
and let's also delete the test folder
4985
04:14:12,880 --> 04:14:18,479
that we created for practice
4986
04:14:15,760 --> 04:14:21,479
and also the package lock.json and
4987
04:14:18,479 --> 04:14:21,479
package.json.
4988
04:14:28,080 --> 04:14:34,159
Next, let's go into the chatbot project
4989
04:14:30,800 --> 04:14:38,640
folder and we'll open the source folder
4990
04:14:34,159 --> 04:14:41,439
and we'll start with this file app.jsx.
4991
04:14:38,640 --> 04:14:44,720
So notice that in this file we have a
4992
04:14:41,439 --> 04:14:47,199
react component called app and this
4993
04:14:44,720 --> 04:14:48,960
component represents the entire
4994
04:14:47,199 --> 04:14:52,479
application.
4995
04:14:48,960 --> 04:14:55,920
Now previously in chatbot.html
4996
04:14:52,479 --> 04:14:58,720
we also created an app component.
4997
04:14:55,920 --> 04:15:01,600
So all we have to do is to replace this
4998
04:14:58,720 --> 04:15:04,960
app component with our previous app
4999
04:15:01,600 --> 04:15:08,760
component. So let's go to our code and
5000
04:15:04,960 --> 04:15:08,760
we're going to open chatbot.html
5001
04:15:08,880 --> 04:15:14,640
and we're going to scroll down till we
5002
04:15:11,359 --> 04:15:17,359
see the app component which is down
5003
04:15:14,640 --> 04:15:20,159
here. So we're going to select the app
5004
04:15:17,359 --> 04:15:22,080
component
5005
04:15:20,159 --> 04:15:24,239
and make sure you don't select the code
5006
04:15:22,080 --> 04:15:27,439
down here. We just want to copy the
5007
04:15:24,239 --> 04:15:29,120
component over. So, we're going to copy
5008
04:15:27,439 --> 04:15:33,199
this
5009
04:15:29,120 --> 04:15:35,359
and then go back to app.jsx
5010
04:15:33,199 --> 04:15:39,760
and we're going to remove this app
5011
04:15:35,359 --> 04:15:42,720
component. So, we'll select it
5012
04:15:39,760 --> 04:15:44,720
and remove it and we're going to paste
5013
04:15:42,720 --> 04:15:46,960
the app component from our previous
5014
04:15:44,720 --> 04:15:50,080
project.
5015
04:15:46,960 --> 04:15:52,880
And now we're going to scroll up and
5016
04:15:50,080 --> 04:15:55,760
eslint is going to tell us we have some
5017
04:15:52,880 --> 04:15:58,560
errors here. So we'll go through these
5018
04:15:55,760 --> 04:16:02,000
errors one by one.
5019
04:15:58,560 --> 04:16:05,920
First for this error eslint is going to
5020
04:16:02,000 --> 04:16:08,960
tell us that react is not defined.
5021
04:16:05,920 --> 04:16:12,319
Before we loaded the react external
5022
04:16:08,960 --> 04:16:15,600
library using a script tag and this
5023
04:16:12,319 --> 04:16:19,040
provided a variable called react.
5024
04:16:15,600 --> 04:16:21,760
However, now that we're using NodeJS,
5025
04:16:19,040 --> 04:16:25,040
we usually don't use script tags to load
5026
04:16:21,760 --> 04:16:27,760
the external libraries. Instead, we
5027
04:16:25,040 --> 04:16:30,319
download these packages into Node
5028
04:16:27,760 --> 04:16:34,239
modules and then load it from node
5029
04:16:30,319 --> 04:16:37,439
modules. With node modules, we can use a
5030
04:16:34,239 --> 04:16:39,840
JavaScript feature called modules to
5031
04:16:37,439 --> 04:16:43,840
only load the parts of the external
5032
04:16:39,840 --> 04:16:46,000
library that we need. So when using npm,
5033
04:16:43,840 --> 04:16:48,880
a best practice is to load external
5034
04:16:46,000 --> 04:16:52,319
libraries from node modules. Instead of
5035
04:16:48,880 --> 04:16:55,199
using a script tag
5036
04:16:52,319 --> 04:16:58,960
to load an external library from node
5037
04:16:55,199 --> 04:17:01,359
modules, we use the word import. And
5038
04:16:58,960 --> 04:17:04,239
then we tell the computer which external
5039
04:17:01,359 --> 04:17:07,760
library or which package we want to load
5040
04:17:04,239 --> 04:17:11,520
from by typing from and then the name of
5041
04:17:07,760 --> 04:17:14,239
the package like the string react. If we
5042
04:17:11,520 --> 04:17:17,600
just type a name directly like this and
5043
04:17:14,239 --> 04:17:20,319
we don't start with dot slash or slash,
5044
04:17:17,600 --> 04:17:23,680
this tells the computer to look inside
5045
04:17:20,319 --> 04:17:25,600
node modules for the React package. So,
5046
04:17:23,680 --> 04:17:28,239
it's going to look in here and it's
5047
04:17:25,600 --> 04:17:31,840
going to find the React package in here
5048
04:17:28,239 --> 04:17:34,399
and load it from here.
5049
04:17:31,840 --> 04:17:37,120
Also, when importing from node modules,
5050
04:17:34,399 --> 04:17:39,920
we don't have to import the entire React
5051
04:17:37,120 --> 04:17:43,760
library. we can just import what we
5052
04:17:39,920 --> 04:17:47,760
need. For example, here we just import
5053
04:17:43,760 --> 04:17:50,880
the use state feature from React.
5054
04:17:47,760 --> 04:17:53,600
So now that we imported use state, we
5055
04:17:50,880 --> 04:17:57,040
can just use this directly here instead
5056
04:17:53,600 --> 04:18:03,359
of doing react dot use state. So we can
5057
04:17:57,040 --> 04:18:07,520
remove react dot and use state directly.
5058
04:18:03,359 --> 04:18:10,399
Next, these two lines import images that
5059
04:18:07,520 --> 04:18:12,960
were used in the example website. So,
5060
04:18:10,399 --> 04:18:16,319
we're no longer using these images. So,
5061
04:18:12,960 --> 04:18:20,080
ESLint displays an error. To fix this,
5062
04:18:16,319 --> 04:18:23,680
we can just remove these imports.
5063
04:18:20,080 --> 04:18:25,520
Next, let's scroll down
5064
04:18:23,680 --> 04:18:29,120
and it's going to tell us that the
5065
04:18:25,520 --> 04:18:31,600
components chat messages and chat input
5066
04:18:29,120 --> 04:18:34,399
are missing and they don't exist in this
5067
04:18:31,600 --> 04:18:38,199
file. So, let's actually just copy them
5068
04:18:34,399 --> 04:18:38,199
over from chatbot.html.
5069
04:18:38,479 --> 04:18:42,159
So, we'll go back to the previous
5070
04:18:40,399 --> 04:18:44,880
project
5071
04:18:42,159 --> 04:18:47,600
and in chatbot.html, HTML. We're going
5072
04:18:44,880 --> 04:18:50,000
to scroll up
5073
04:18:47,600 --> 04:18:52,000
and above the app component, we're going
5074
04:18:50,000 --> 04:18:53,920
to copy over all of our other
5075
04:18:52,000 --> 04:18:56,479
components. So, we're going to select
5076
04:18:53,920 --> 04:19:00,479
all this code
5077
04:18:56,479 --> 04:19:04,720
and we're going to copy
5078
04:19:00,479 --> 04:19:08,479
and let's go back to app.jsx
5079
04:19:04,720 --> 04:19:11,199
and we'll scroll up above the app. We're
5080
04:19:08,479 --> 04:19:14,399
going to paste all of the other
5081
04:19:11,199 --> 04:19:16,399
components that we created.
5082
04:19:14,399 --> 04:19:18,720
And now if we scroll down and look at
5083
04:19:16,399 --> 04:19:21,359
our app component, you can see that
5084
04:19:18,720 --> 04:19:24,720
there's no more errors because we moved
5085
04:19:21,359 --> 04:19:27,920
these components into this file. So now
5086
04:19:24,720 --> 04:19:31,680
we've moved the app component into this
5087
04:19:27,920 --> 04:19:34,000
new React setup.
5088
04:19:31,680 --> 04:19:37,199
After copying over all of our other
5089
04:19:34,000 --> 04:19:40,159
components, we now have some more ESLint
5090
04:19:37,199 --> 04:19:43,760
errors. So, let's scroll up to the top
5091
04:19:40,159 --> 04:19:46,319
and we'll fix these errors one by one.
5092
04:19:43,760 --> 04:19:48,880
First, you might see an error here for
5093
04:19:46,319 --> 04:19:51,680
the props.
5094
04:19:48,880 --> 04:19:54,560
ESLint tells us that these props are
5095
04:19:51,680 --> 04:19:57,279
missing props validations.
5096
04:19:54,560 --> 04:20:00,399
Props validation was an older feature of
5097
04:19:57,279 --> 04:20:02,640
React. Newer versions of React don't use
5098
04:20:00,399 --> 04:20:05,520
this feature. So, we can actually turn
5099
04:20:02,640 --> 04:20:08,479
off this check in eslint.
5100
04:20:05,520 --> 04:20:11,600
To turn off a check in eslint, we're
5101
04:20:08,479 --> 04:20:15,040
going to go to the eslint config.js
5102
04:20:11,600 --> 04:20:19,040
file, and we're going to scroll down to
5103
04:20:15,040 --> 04:20:22,720
the rules section here. A rule tells
5104
04:20:19,040 --> 04:20:25,600
ESLint what to check for. At the bottom
5105
04:20:22,720 --> 04:20:30,520
of this object, we're going to add the
5106
04:20:25,600 --> 04:20:30,520
string react slprop-types
5107
04:20:31,040 --> 04:20:38,399
colon and we'll set it to the string off
5108
04:20:35,120 --> 04:20:40,800
and save.
5109
04:20:38,399 --> 04:20:43,760
So, this will turn off this particular
5110
04:20:40,800 --> 04:20:47,520
rule or this particular check. And now,
5111
04:20:43,760 --> 04:20:50,000
if we go back to app.jsx, jsx. We no
5112
04:20:47,520 --> 04:20:52,640
longer have those eslint errors because
5113
04:20:50,000 --> 04:20:54,720
we turned off the check.
5114
04:20:52,640 --> 04:20:56,960
Next, remember that we're no longer
5115
04:20:54,720 --> 04:21:00,159
using react.
5116
04:20:56,960 --> 04:21:03,680
We can just use state directly. So, we
5117
04:21:00,159 --> 04:21:07,840
can remove react dot.
5118
04:21:03,680 --> 04:21:10,720
Next, let's scroll down and eslint will
5119
04:21:07,840 --> 04:21:14,399
tell us that chatbot does not exist in
5120
04:21:10,720 --> 04:21:17,439
this file. So, previously we loaded the
5121
04:21:14,399 --> 04:21:20,640
chatbot using a script tag. But
5122
04:21:17,439 --> 04:21:22,800
remember, now that we're using npm, a
5123
04:21:20,640 --> 04:21:25,359
best practice is to load the chatbot
5124
04:21:22,800 --> 04:21:27,920
from node modules.
5125
04:21:25,359 --> 04:21:30,720
First, we need to download the chatbot
5126
04:21:27,920 --> 04:21:32,640
into node modules. To do that, we're
5127
04:21:30,720 --> 04:21:35,279
going to install the package super
5128
04:21:32,640 --> 04:21:37,279
simple dev. Let's go back to the command
5129
04:21:35,279 --> 04:21:39,520
line.
5130
04:21:37,279 --> 04:21:41,680
And currently, it's running the vit
5131
04:21:39,520 --> 04:21:44,800
server. So, we're going to stop the
5132
04:21:41,680 --> 04:21:48,560
server by pressing control C on Windows
5133
04:21:44,800 --> 04:21:52,560
or control C on Mac. Note that on Mac,
5134
04:21:48,560 --> 04:21:55,439
it's control C, not command C. So, this
5135
04:21:52,560 --> 04:21:58,560
will stop the V server and bring us back
5136
04:21:55,439 --> 04:22:00,479
to the command line. Usually, to exit
5137
04:21:58,560 --> 04:22:04,800
something from the command line, we can
5138
04:22:00,479 --> 04:22:08,080
press control C in both Windows and Mac.
5139
04:22:04,800 --> 04:22:09,840
Next, we're going to npm
5140
04:22:08,080 --> 04:22:15,120
install
5141
04:22:09,840 --> 04:22:17,439
the package super simple dev. So, this
5142
04:22:15,120 --> 04:22:20,080
package contains the chatbot code that
5143
04:22:17,439 --> 04:22:23,040
we were using earlier. Let's press
5144
04:22:20,080 --> 04:22:24,960
enter.
5145
04:22:23,040 --> 04:22:28,720
It's going to install the super simple
5146
04:22:24,960 --> 04:22:31,520
dev package inside node modules. And now
5147
04:22:28,720 --> 04:22:34,560
to load it from node modules, we're
5148
04:22:31,520 --> 04:22:38,560
going to scroll up in our code and at
5149
04:22:34,560 --> 04:22:41,760
the top we're going to import
5150
04:22:38,560 --> 04:22:46,239
and type curly brackets. So we're going
5151
04:22:41,760 --> 04:22:50,239
to import the chatbot object and we're
5152
04:22:46,239 --> 04:22:53,600
going to import it from a string and
5153
04:22:50,239 --> 04:22:57,439
we'll import it from the package super
5154
04:22:53,600 --> 04:23:00,000
simple dev. So this will load the super
5155
04:22:57,439 --> 04:23:03,680
simple dev package from inside node
5156
04:23:00,000 --> 04:23:05,439
modules and get the chatbot out of it.
5157
04:23:03,680 --> 04:23:08,720
And now that we have this chatbot
5158
04:23:05,439 --> 04:23:10,640
object, if we scroll down, we have
5159
04:23:08,720 --> 04:23:13,120
access to it. So it's no longer giving
5160
04:23:10,640 --> 04:23:16,880
us an error.
5161
04:23:13,120 --> 04:23:20,720
Finally, we're going to scroll down
5162
04:23:16,880 --> 04:23:23,600
to the chat messages component.
5163
04:23:20,720 --> 04:23:27,359
And here it's telling us that React does
5164
04:23:23,600 --> 04:23:30,720
not exist. So just like with use state,
5165
04:23:27,359 --> 04:23:33,520
we can import use ref and use effect
5166
04:23:30,720 --> 04:23:37,600
directly as well.
5167
04:23:33,520 --> 04:23:41,439
So let's remove the react dot from here
5168
04:23:37,600 --> 04:23:44,880
and remove react dot from here.
5169
04:23:41,439 --> 04:23:47,600
And we're going to scroll up
5170
04:23:44,880 --> 04:23:50,399
and at the top we're already importing
5171
04:23:47,600 --> 04:23:53,040
from the React package. To import
5172
04:23:50,399 --> 04:23:58,239
multiple things we can just add a comma
5173
04:23:53,040 --> 04:24:04,399
at the end. and import use ref and add
5174
04:23:58,239 --> 04:24:06,479
another comma and import use effect.
5175
04:24:04,399 --> 04:24:09,040
This gets these two features out of
5176
04:24:06,479 --> 04:24:11,040
React and now we can use them directly
5177
04:24:09,040 --> 04:24:13,279
in our code.
5178
04:24:11,040 --> 04:24:15,920
And now we've fixed all the ESLint
5179
04:24:13,279 --> 04:24:18,560
errors in our app component. If you did
5180
04:24:15,920 --> 04:24:21,120
the exercises in the previous lesson,
5181
04:24:18,560 --> 04:24:23,359
you may have some more ESLint errors.
5182
04:24:21,120 --> 04:24:26,239
So, make sure you fix any other errors
5183
04:24:23,359 --> 04:24:29,479
that you have. And finally, let's save
5184
04:24:26,239 --> 04:24:29,479
this file.
5185
04:24:29,840 --> 04:24:34,880
Next, we're going to look at the file
5186
04:24:32,159 --> 04:24:38,720
main.jsx.
5187
04:24:34,880 --> 04:24:41,840
So, this code sets up React. It calls
5188
04:24:38,720 --> 04:24:45,040
create root, gives it a container, and
5189
04:24:41,840 --> 04:24:48,560
then renders the app component onto the
5190
04:24:45,040 --> 04:24:54,760
website. If we open our chatbot.html
5191
04:24:48,560 --> 04:24:54,760
HTML and we scroll to the very bottom.
5192
04:24:55,279 --> 04:25:00,960
You notice that we do the same thing. We
5193
04:24:58,000 --> 04:25:03,279
call create root, give it a container,
5194
04:25:00,960 --> 04:25:06,960
and then render the app onto the
5195
04:25:03,279 --> 04:25:09,920
website. So main.jsx
5196
04:25:06,960 --> 04:25:13,199
does the same thing as this code. It
5197
04:25:09,920 --> 04:25:14,960
sets up React and displays the app onto
5198
04:25:13,199 --> 04:25:17,359
the website.
5199
04:25:14,960 --> 04:25:20,080
Now, instead of copying this code over
5200
04:25:17,359 --> 04:25:22,800
to main.j jsx. We're just going to use
5201
04:25:20,080 --> 04:25:25,199
the code in this file because it
5202
04:25:22,800 --> 04:25:27,840
basically does the same thing. It just
5203
04:25:25,199 --> 04:25:30,880
uses an ID for the container instead of
5204
04:25:27,840 --> 04:25:33,600
a class. The one other difference in
5205
04:25:30,880 --> 04:25:36,720
this code is it wraps the app in a
5206
04:25:33,600 --> 04:25:39,600
component called strict mode. Strict
5207
04:25:36,720 --> 04:25:42,479
mode is a special component provided by
5208
04:25:39,600 --> 04:25:45,040
React. Strict mode basically gives us
5209
04:25:42,479 --> 04:25:47,840
some additional checks and warnings when
5210
04:25:45,040 --> 04:25:50,239
developing our app.
5211
04:25:47,840 --> 04:25:53,920
And that's it. We moved all of our
5212
04:25:50,239 --> 04:25:56,880
JavaScript code into this new setup.
5213
04:25:53,920 --> 04:25:59,600
Now, let's open this app in our browser
5214
04:25:56,880 --> 04:26:02,640
to see what it looks like. We'll go down
5215
04:25:59,600 --> 04:26:05,279
to the command line and let's make sure
5216
04:26:02,640 --> 04:26:10,080
we're running inside the chatbot project
5217
04:26:05,279 --> 04:26:12,640
folder by typing pwd and press enter.
5218
04:26:10,080 --> 04:26:17,040
So, that looks good. And we're going to
5219
04:26:12,640 --> 04:26:18,800
start the VIT server by typing npm
5220
04:26:17,040 --> 04:26:21,760
rundev
5221
04:26:18,800 --> 04:26:25,199
and press enter.
5222
04:26:21,760 --> 04:26:27,680
And we'll open this URL in our browser.
5223
04:26:25,199 --> 04:26:30,399
I already have it open.
5224
04:26:27,680 --> 04:26:33,840
So I'll open my browser and refresh this
5225
04:26:30,399 --> 04:26:38,000
page. And now the website displays our
5226
04:26:33,840 --> 04:26:41,359
previous chatbot app. in the text box.
5227
04:26:38,000 --> 04:26:43,520
If we type a message like hello and
5228
04:26:41,359 --> 04:26:45,840
press send,
5229
04:26:43,520 --> 04:26:47,920
it will display a response from the
5230
04:26:45,840 --> 04:26:50,239
chatbot.
5231
04:26:47,920 --> 04:26:52,560
However, obviously it doesn't look
5232
04:26:50,239 --> 04:26:56,239
right, and that's because we haven't
5233
04:26:52,560 --> 04:26:58,239
moved our CSS code and images over into
5234
04:26:56,239 --> 04:27:01,279
the new setup. So, we're going to do
5235
04:26:58,239 --> 04:27:04,159
that next.
5236
04:27:01,279 --> 04:27:06,000
Let's move our CSS code into this new
5237
04:27:04,159 --> 04:27:08,800
React setup.
5238
04:27:06,000 --> 04:27:13,159
We'll go back to VS Code
5239
04:27:08,800 --> 04:27:13,159
and we're going to open chatbot.html
5240
04:27:13,359 --> 04:27:18,560
and we're going to go to the top of this
5241
04:27:15,359 --> 04:27:21,199
file right here.
5242
04:27:18,560 --> 04:27:24,239
So at the top we have all of our CSS
5243
04:27:21,199 --> 04:27:27,359
code. We're going to move this CSS code
5244
04:27:24,239 --> 04:27:32,120
into app.css.
5245
04:27:27,359 --> 04:27:32,120
So, we'll select all of this CSS
5246
04:27:33,920 --> 04:27:39,199
all the way to the bottom and we're
5247
04:27:36,319 --> 04:27:42,800
going to copy it
5248
04:27:39,199 --> 04:27:44,880
and then open app.css
5249
04:27:42,800 --> 04:27:48,479
and we're going to remove this default
5250
04:27:44,880 --> 04:27:54,000
CSS. So, we'll remove this and then
5251
04:27:48,479 --> 04:27:56,720
paste our CSS code and save.
5252
04:27:54,000 --> 04:28:00,000
And now how does this CSS code get
5253
04:27:56,720 --> 04:28:02,399
loaded on the website? If we open
5254
04:28:00,000 --> 04:28:07,239
app.jsx,
5255
04:28:02,399 --> 04:28:07,239
notice at the top we import/app.css.
5256
04:28:08,239 --> 04:28:14,479
So dot slash means the current folder.
5257
04:28:11,920 --> 04:28:17,840
It means the folder that contains this
5258
04:28:14,479 --> 04:28:19,920
file, which is the source folder. So
5259
04:28:17,840 --> 04:28:23,040
it's going to start from source and look
5260
04:28:19,920 --> 04:28:25,920
for a file called app.css.
5261
04:28:23,040 --> 04:28:29,439
and it finds it here and it will load
5262
04:28:25,920 --> 04:28:32,399
all the CSS code in this file onto the
5263
04:28:29,439 --> 04:28:36,159
website. So you might be wondering how
5264
04:28:32,399 --> 04:28:38,640
can we import a CSS file like this?
5265
04:28:36,159 --> 04:28:42,239
Doesn't import only work for JavaScript
5266
04:28:38,640 --> 04:28:46,399
files. So this is actually a feature of
5267
04:28:42,239 --> 04:28:50,800
Vit. Vit lets us import any type of file
5268
04:28:46,399 --> 04:28:53,279
like a CSS file or even an image file.
5269
04:28:50,800 --> 04:28:57,120
Depending on the type of file, it will
5270
04:28:53,279 --> 04:29:01,120
behave differently. If we import a CSS
5271
04:28:57,120 --> 04:29:04,159
file, vit will load all the CSS code in
5272
04:29:01,120 --> 04:29:07,920
this file and apply it to the website.
5273
04:29:04,159 --> 04:29:12,479
So that's how this works. We use Vit to
5274
04:29:07,920 --> 04:29:15,040
load the CSS code inside app.css CSS.
5275
04:29:12,479 --> 04:29:18,159
And this allows us to separate our CSS
5276
04:29:15,040 --> 04:29:20,479
code into its own file to make our code
5277
04:29:18,159 --> 04:29:23,840
more organized.
5278
04:29:20,479 --> 04:29:25,920
Next, you'll also notice that in here we
5279
04:29:23,840 --> 04:29:27,760
have an index.css
5280
04:29:25,920 --> 04:29:32,319
file.
5281
04:29:27,760 --> 04:29:36,319
This CSS file is loaded by main.jsx
5282
04:29:32,319 --> 04:29:40,000
here using this import. So why do we
5283
04:29:36,319 --> 04:29:42,720
have two CSS files? A best practice is
5284
04:29:40,000 --> 04:29:47,199
to split up our CSS into different
5285
04:29:42,720 --> 04:29:51,520
files. All the CSS for the app component
5286
04:29:47,199 --> 04:29:54,800
go inside the app.css file. All the CSS
5287
04:29:51,520 --> 04:29:57,199
for the overall website should go inside
5288
04:29:54,800 --> 04:30:00,000
index.css.
5289
04:29:57,199 --> 04:30:02,960
If we open app.css
5290
04:30:00,000 --> 04:30:06,080
and we scroll to the top,
5291
04:30:02,960 --> 04:30:09,199
the body styles here are not related to
5292
04:30:06,080 --> 04:30:12,399
the app component. This styles the
5293
04:30:09,199 --> 04:30:16,239
overall website. So a best practice is
5294
04:30:12,399 --> 04:30:18,399
to split up this code and move this into
5295
04:30:16,239 --> 04:30:23,680
index.css.
5296
04:30:18,399 --> 04:30:25,920
So we'll select this code and then cut
5297
04:30:23,680 --> 04:30:29,600
and save
5298
04:30:25,920 --> 04:30:33,600
and then inside index.css CSS.
5299
04:30:29,600 --> 04:30:40,159
We're going to remove this default code
5300
04:30:33,600 --> 04:30:43,199
and paste those body styles and save.
5301
04:30:40,159 --> 04:30:45,680
If we go back to app.css,
5302
04:30:43,199 --> 04:30:48,319
the rest of this code is related to the
5303
04:30:45,680 --> 04:30:51,520
app component. So, it makes sense to
5304
04:30:48,319 --> 04:30:54,800
leave it in app.css.
5305
04:30:51,520 --> 04:30:58,080
And now we moved all of our CSS into our
5306
04:30:54,800 --> 04:31:00,399
new React setup. So, let's open our
5307
04:30:58,080 --> 04:31:02,560
project again in the browser to see what
5308
04:31:00,399 --> 04:31:04,960
it looks like.
5309
04:31:02,560 --> 04:31:06,800
And now it's looking pretty good. It's
5310
04:31:04,960 --> 04:31:09,199
looking like our previous chatbot
5311
04:31:06,800 --> 04:31:11,680
project. And we have the layout and
5312
04:31:09,199 --> 04:31:14,399
styling that we need. The last thing
5313
04:31:11,680 --> 04:31:18,080
we're missing here are the images. So,
5314
04:31:14,399 --> 04:31:21,680
we're going to move those. Next,
5315
04:31:18,080 --> 04:31:25,600
let's move the robot image and the user
5316
04:31:21,680 --> 04:31:28,080
image into the new React setup. We'll go
5317
04:31:25,600 --> 04:31:30,399
back to VS Code
5318
04:31:28,080 --> 04:31:34,479
and we're going to scroll down and we're
5319
04:31:30,399 --> 04:31:36,880
going to select the robot.png image and
5320
04:31:34,479 --> 04:31:40,960
we'll copy it by pressing control C on
5321
04:31:36,880 --> 04:31:43,520
Windows or command C on Mac.
5322
04:31:40,960 --> 04:31:46,399
And let's scroll up and we're going to
5323
04:31:43,520 --> 04:31:49,040
put it inside the source/assets
5324
04:31:46,399 --> 04:31:52,000
folder. So, this is typically where we
5325
04:31:49,040 --> 04:31:54,479
put our images. So, let's click on this
5326
04:31:52,000 --> 04:31:56,720
assets folder and we're going to paste
5327
04:31:54,479 --> 04:32:00,399
the image by pressing controlV on
5328
04:31:56,720 --> 04:32:02,960
Windows or commandV on Mac.
5329
04:32:00,399 --> 04:32:06,080
And now we have the robot image in our
5330
04:32:02,960 --> 04:32:09,520
new setup. Let's do the same for the
5331
04:32:06,080 --> 04:32:11,279
user image. So, we'll go down and select
5332
04:32:09,520 --> 04:32:14,080
user.png
5333
04:32:11,279 --> 04:32:17,199
and we'll copy
5334
04:32:14,080 --> 04:32:20,000
and then select the assets folder and
5335
04:32:17,199 --> 04:32:22,880
we'll paste.
5336
04:32:20,000 --> 04:32:28,080
And now we need to load these images
5337
04:32:22,880 --> 04:32:30,479
into our app. Let's open app.jsx.
5338
04:32:28,080 --> 04:32:34,399
And to load this image, we can actually
5339
04:32:30,479 --> 04:32:36,960
just import it. So at the top, let's
5340
04:32:34,399 --> 04:32:40,560
type import
5341
04:32:36,960 --> 04:32:46,399
and we'll give this image a name like
5342
04:32:40,560 --> 04:32:50,560
robot profile image. And we'll import it
5343
04:32:46,399 --> 04:32:54,640
from a string. and we're going to locate
5344
04:32:50,560 --> 04:32:57,840
this image file. So inside here, we'll
5345
04:32:54,640 --> 04:33:00,399
start by typing dot slash, which means
5346
04:32:57,840 --> 04:33:02,880
the current folder or the folder that
5347
04:33:00,399 --> 04:33:05,600
contains app.jsx,
5348
04:33:02,880 --> 04:33:07,760
which is the source folder. So we're
5349
04:33:05,600 --> 04:33:09,279
going to start from the source folder
5350
04:33:07,760 --> 04:33:13,279
and then we're going to go into the
5351
04:33:09,279 --> 04:33:15,760
assets folder. So, we'll type assets.
5352
04:33:13,279 --> 04:33:18,879
And to go into this folder, we're going
5353
04:33:15,760 --> 04:33:22,561
to type a slash. And then we're going to
5354
04:33:18,879 --> 04:33:26,840
look for the file robot.png.
5355
04:33:22,561 --> 04:33:26,840
So, we'll type robot.png.
5356
04:33:27,199 --> 04:33:33,279
So, this is a file path. It tells the
5357
04:33:29,760 --> 04:33:36,561
computer how to locate this file.
5358
04:33:33,279 --> 04:33:39,119
So also notice that in this import we
5359
04:33:36,561 --> 04:33:41,279
just gave it a name instead of using
5360
04:33:39,119 --> 04:33:44,240
curly brackets.
5361
04:33:41,279 --> 04:33:47,520
This is called a default export and
5362
04:33:44,240 --> 04:33:49,520
we'll review this later in the lesson.
5363
04:33:47,520 --> 04:33:52,480
But for now just remember that when we
5364
04:33:49,520 --> 04:33:54,879
import an image we just give it a name
5365
04:33:52,480 --> 04:33:57,760
without curly brackets.
5366
04:33:54,879 --> 04:34:01,199
Let's also load the user image. So,
5367
04:33:57,760 --> 04:34:05,840
we're going to import and give it a
5368
04:34:01,199 --> 04:34:09,119
name, user profile image, and we'll
5369
04:34:05,840 --> 04:34:12,959
import it from a string. And we're going
5370
04:34:09,119 --> 04:34:15,359
to locate the user.png file. So, we'll
5371
04:34:12,959 --> 04:34:18,959
start from the current folder using dot
5372
04:34:15,359 --> 04:34:22,000
slash. We'll look for the assets folder
5373
04:34:18,959 --> 04:34:27,000
and then slash to go into this folder.
5374
04:34:22,000 --> 04:34:27,000
And we'll look for the file user.png.
5375
04:34:27,359 --> 04:34:33,920
So remember this is a feature of vit.
5376
04:34:31,119 --> 04:34:37,600
Vit lets us import any type of file
5377
04:34:33,920 --> 04:34:41,279
including an image file. When we import
5378
04:34:37,600 --> 04:34:44,959
an image like this, vit gives us a file
5379
04:34:41,279 --> 04:34:48,879
path to that image. So this contains a
5380
04:34:44,959 --> 04:34:52,639
file path like this. And we can use this
5381
04:34:48,879 --> 04:34:56,400
directly in an images source attribute.
5382
04:34:52,639 --> 04:34:59,920
So, let's scroll down to the chat
5383
04:34:56,400 --> 04:35:02,959
message component. And in here, we have
5384
04:34:59,920 --> 04:35:07,119
our two image elements. So, in the
5385
04:35:02,959 --> 04:35:08,879
source attribute, instead of robot.png,
5386
04:35:07,119 --> 04:35:13,359
we're going to give it those images that
5387
04:35:08,879 --> 04:35:16,400
we imported by typing curly brackets.
5388
04:35:13,359 --> 04:35:22,160
And here for the robot, we'll insert the
5389
04:35:16,400 --> 04:35:24,879
variable robot profile image. And we'll
5390
04:35:22,160 --> 04:35:27,359
do the same thing for the user. We'll
5391
04:35:24,879 --> 04:35:30,080
remove the source attribute
5392
04:35:27,359 --> 04:35:35,840
and type curly brackets. And we'll
5393
04:35:30,080 --> 04:35:39,039
insert the user profile image. So that's
5394
04:35:35,840 --> 04:35:42,799
how we import images using Vit and use
5395
04:35:39,039 --> 04:35:45,680
them. Now let's save
5396
04:35:42,799 --> 04:35:48,799
and we'll check our website.
5397
04:35:45,680 --> 04:35:50,799
And now all of the images are loaded.
5398
04:35:48,799 --> 04:35:53,039
So, let's try adding a message to make
5399
04:35:50,799 --> 04:35:57,439
sure this is working. We'll type the
5400
04:35:53,039 --> 04:36:00,080
message hello and press send.
5401
04:35:57,439 --> 04:36:03,199
And that's it. We finished moving our
5402
04:36:00,080 --> 04:36:05,920
chatbot project into this new React
5403
04:36:03,199 --> 04:36:09,439
setup.
5404
04:36:05,920 --> 04:36:12,000
We'll go back to VS Code.
5405
04:36:09,439 --> 04:36:14,561
And the big advantage of this setup is
5406
04:36:12,000 --> 04:36:17,119
we can separate our code into different
5407
04:36:14,561 --> 04:36:20,799
files instead of combining everything
5408
04:36:17,119 --> 04:36:24,400
into one file like we had before. Right
5409
04:36:20,799 --> 04:36:27,119
now we already separated our code a bit.
5410
04:36:24,400 --> 04:36:28,959
We separated the app component into
5411
04:36:27,119 --> 04:36:31,359
app.jsx
5412
04:36:28,959 --> 04:36:35,039
and we separated the code that sets up
5413
04:36:31,359 --> 04:36:37,119
React into main.jsx.
5414
04:36:35,039 --> 04:36:41,680
Now we're going to separate our code
5415
04:36:37,119 --> 04:36:44,561
even more. So, inside app.jsx,
5416
04:36:41,680 --> 04:36:46,480
if we scroll to the top,
5417
04:36:44,561 --> 04:36:49,119
notice that there's many other
5418
04:36:46,480 --> 04:36:52,561
components in this file, like the chat
5419
04:36:49,119 --> 04:36:55,680
input component. A best practice is to
5420
04:36:52,561 --> 04:36:58,799
separate each component into its own
5421
04:36:55,680 --> 04:37:02,160
file. This makes our code easier to work
5422
04:36:58,799 --> 04:37:04,320
with because each file only focuses on
5423
04:37:02,160 --> 04:37:06,639
one component.
5424
04:37:04,320 --> 04:37:10,320
So, let's separate the chat input
5425
04:37:06,639 --> 04:37:12,240
component into its own file.
5426
04:37:10,320 --> 04:37:14,561
First, we're going to go to the source
5427
04:37:12,240 --> 04:37:17,680
folder and we're going to create a new
5428
04:37:14,561 --> 04:37:20,719
folder in here to group our components
5429
04:37:17,680 --> 04:37:23,520
together. So, we'll click this folder
5430
04:37:20,719 --> 04:37:26,799
and we're going to click this icon to
5431
04:37:23,520 --> 04:37:28,320
create a new folder and we'll name it
5432
04:37:26,799 --> 04:37:31,840
components
5433
04:37:28,320 --> 04:37:34,320
and press enter.
5434
04:37:31,840 --> 04:37:37,039
And inside this components folder, we're
5435
04:37:34,320 --> 04:37:40,240
going to create a new file for our chat
5436
04:37:37,039 --> 04:37:43,600
input component. So in here, we'll click
5437
04:37:40,240 --> 04:37:46,480
it and then click this icon to create a
5438
04:37:43,600 --> 04:37:49,439
new file. And we'll give it the same
5439
04:37:46,480 --> 04:37:55,879
name as the component. So we'll name it
5440
04:37:49,439 --> 04:37:55,879
capital C chat capital I input.jsx
5441
04:37:55,920 --> 04:38:01,279
and press enter.
5442
04:37:58,799 --> 04:38:04,799
And now we're going to move the chat
5443
04:38:01,279 --> 04:38:07,279
input component into this file. So we'll
5444
04:38:04,799 --> 04:38:10,959
open app.jsx
5445
04:38:07,279 --> 04:38:13,760
and we'll select the code for chat input
5446
04:38:10,959 --> 04:38:17,879
which is down here.
5447
04:38:13,760 --> 04:38:17,879
And we're going to cut
5448
04:38:17,920 --> 04:38:25,359
and then open chatinput.jsx.
5449
04:38:21,520 --> 04:38:28,160
And in here we'll paste.
5450
04:38:25,359 --> 04:38:31,760
and we'll scroll up and we're going to
5451
04:38:28,160 --> 04:38:34,400
fix these eslint errors. We'll start
5452
04:38:31,760 --> 04:38:37,039
with this one. Use state currently
5453
04:38:34,400 --> 04:38:39,760
doesn't exist in this file. So, we need
5454
04:38:37,039 --> 04:38:42,959
to import it again.
5455
04:38:39,760 --> 04:38:46,480
Let's go back to app.jsx.
5456
04:38:42,959 --> 04:38:50,639
And at the top, let's copy this import.
5457
04:38:46,480 --> 04:38:54,000
So, we'll select it and copy.
5458
04:38:50,639 --> 04:38:56,320
And we'll go to the chat input file. And
5459
04:38:54,000 --> 04:38:59,680
at the top of this,
5460
04:38:56,320 --> 04:39:02,000
we're going to paste the import.
5461
04:38:59,680 --> 04:39:04,080
Now that we imported the use state,
5462
04:39:02,000 --> 04:39:07,600
we're able to use it and there's no
5463
04:39:04,080 --> 04:39:10,639
longer an error. Now, eslint also tells
5464
04:39:07,600 --> 04:39:13,520
us that we're not using use ref or use
5465
04:39:10,639 --> 04:39:15,920
effect in this file. So, we can remove
5466
04:39:13,520 --> 04:39:18,240
these.
5467
04:39:15,920 --> 04:39:21,439
Next, we're going to scroll down in this
5468
04:39:18,240 --> 04:39:24,240
file and we'll see that the chatbot
5469
04:39:21,439 --> 04:39:29,439
object doesn't exist anymore in this
5470
04:39:24,240 --> 04:39:32,480
file. So again, we'll go back to app.jsx
5471
04:39:29,439 --> 04:39:35,359
and let's move this import. So we'll
5472
04:39:32,480 --> 04:39:38,240
select it and chatbot is not used
5473
04:39:35,359 --> 04:39:41,520
anymore in this file. So this time we're
5474
04:39:38,240 --> 04:39:46,240
going to cut
5475
04:39:41,520 --> 04:39:49,600
and inside chat input at the top we're
5476
04:39:46,240 --> 04:39:53,680
going to paste that input.
5477
04:39:49,600 --> 04:39:56,240
And now we have access to the chatbot.
5478
04:39:53,680 --> 04:39:59,840
The last error here is that the chat
5479
04:39:56,240 --> 04:40:02,160
input component is not being used. To
5480
04:39:59,840 --> 04:40:05,600
fix this, we need to review a feature of
5481
04:40:02,160 --> 04:40:08,718
JavaScript called modules.
5482
04:40:05,600 --> 04:40:12,400
By default, any variables or functions
5483
04:40:08,718 --> 04:40:17,360
that we create in a file only exist in
5484
04:40:12,400 --> 04:40:20,878
that file. So chat input only exists in
5485
04:40:17,360 --> 04:40:24,240
this file. It cannot be used outside
5486
04:40:20,878 --> 04:40:26,798
this file. This helps us avoid naming
5487
04:40:24,240 --> 04:40:29,280
conflicts because if we use the same
5488
04:40:26,798 --> 04:40:33,040
variable name in different files, it
5489
04:40:29,280 --> 04:40:35,840
won't cause an error. Now, if we want to
5490
04:40:33,040 --> 04:40:39,920
use this variable outside of this file,
5491
04:40:35,840 --> 04:40:42,400
we need to export it first. To do that
5492
04:40:39,920 --> 04:40:45,360
in front of this function, we can just
5493
04:40:42,400 --> 04:40:47,600
type export.
5494
04:40:45,360 --> 04:40:49,840
And now this error is gone because
5495
04:40:47,600 --> 04:40:53,520
ESLint assumes that we're going to use
5496
04:40:49,840 --> 04:40:56,320
this component outside of this file. So
5497
04:40:53,520 --> 04:40:59,600
now let's save.
5498
04:40:56,320 --> 04:41:02,718
And to use chat input in another file,
5499
04:40:59,600 --> 04:41:05,718
we need to import it. So we'll open
5500
04:41:02,718 --> 04:41:05,718
app.jsx.
5501
04:41:05,920 --> 04:41:14,000
And at the top, we're going to import
5502
04:41:10,240 --> 04:41:18,958
and type curly brackets. And in here,
5503
04:41:14,000 --> 04:41:22,638
we'll import the chat input component.
5504
04:41:18,958 --> 04:41:25,120
And we'll import it from a string. And
5505
04:41:22,638 --> 04:41:26,798
we're going to locate the chat input
5506
04:41:25,120 --> 04:41:30,080
file.
5507
04:41:26,798 --> 04:41:32,240
At the front, we'll type dot slash to
5508
04:41:30,080 --> 04:41:34,638
start in the current folder, which is
5509
04:41:32,240 --> 04:41:38,480
the source folder. And then we're going
5510
04:41:34,638 --> 04:41:41,120
to go to the components folder.
5511
04:41:38,480 --> 04:41:45,360
And we'll type a slash to go into this
5512
04:41:41,120 --> 04:41:48,360
folder. And we'll locate the file chat
5513
04:41:45,360 --> 04:41:48,360
input.jsx.
5514
04:41:49,280 --> 04:41:56,160
By the way, when importing a JavaScript
5515
04:41:51,920 --> 04:42:00,000
file or a JSX file, if we're using Vit,
5516
04:41:56,160 --> 04:42:02,320
we don't have to add.js or JSX at the
5517
04:42:00,000 --> 04:42:05,680
end. Vit will actually add this
5518
04:42:02,320 --> 04:42:08,638
automatically. So, as a shortcut, we can
5519
04:42:05,680 --> 04:42:11,440
remove this.
5520
04:42:08,638 --> 04:42:14,080
Also, a common practice is to import
5521
04:42:11,440 --> 04:42:16,718
from packages at the top and then
5522
04:42:14,080 --> 04:42:19,920
JavaScript files and then other types of
5523
04:42:16,718 --> 04:42:22,560
files. So now we learn how to create
5524
04:42:19,920 --> 04:42:25,360
these imports ourselves using the
5525
04:42:22,560 --> 04:42:29,120
JavaScript modules feature and the word
5526
04:42:25,360 --> 04:42:32,160
export. And we can use chat input in
5527
04:42:29,120 --> 04:42:34,718
this file.
5528
04:42:32,160 --> 04:42:38,480
Next, let's move the rest of these
5529
04:42:34,718 --> 04:42:41,520
components into separate files. First,
5530
04:42:38,480 --> 04:42:44,958
we'll move the chat message component.
5531
04:42:41,520 --> 04:42:46,958
In the components folder, we'll click it
5532
04:42:44,958 --> 04:42:48,718
and we'll click this icon to create a
5533
04:42:46,958 --> 04:42:50,560
new file.
5534
04:42:48,718 --> 04:42:54,718
And let's name it the same as the
5535
04:42:50,560 --> 04:42:57,718
component. Capital C chat, capital M
5536
04:42:54,718 --> 04:42:57,718
message.jsx,
5537
04:42:58,160 --> 04:43:02,638
and press enter.
5538
04:43:01,040 --> 04:43:06,638
And now we're going to move the code
5539
04:43:02,638 --> 04:43:10,600
over. So we'll open app.jsx JSX and
5540
04:43:06,638 --> 04:43:10,600
we'll select this component
5541
04:43:10,638 --> 04:43:14,280
and cut.
5542
04:43:14,320 --> 04:43:21,920
And then inside chat message.jsx,
5543
04:43:18,400 --> 04:43:24,718
we're going to paste the component.
5544
04:43:21,920 --> 04:43:28,240
And again, we'll scroll up and fix the
5545
04:43:24,718 --> 04:43:30,320
ESLint errors. So, first it's telling us
5546
04:43:28,240 --> 04:43:33,600
we're not using this component in this
5547
04:43:30,320 --> 04:43:36,878
file. To fix this at the front, we're
5548
04:43:33,600 --> 04:43:39,600
going to export this component so it can
5549
04:43:36,878 --> 04:43:43,280
be used in other files.
5550
04:43:39,600 --> 04:43:46,320
If we scroll down, ESLint tells us that
5551
04:43:43,280 --> 04:43:50,080
these images are missing. So, we need to
5552
04:43:46,320 --> 04:43:53,040
move those imports into this file. We'll
5553
04:43:50,080 --> 04:43:55,360
go back to app.jsx JSX
5554
04:43:53,040 --> 04:43:58,320
and at the top we're going to select
5555
04:43:55,360 --> 04:44:01,560
these two image imports and we're going
5556
04:43:58,320 --> 04:44:01,560
to cut
5557
04:44:02,000 --> 04:44:08,958
and inside chat message.jsx
5558
04:44:04,798 --> 04:44:12,400
we're going to scroll to the top
5559
04:44:08,958 --> 04:44:14,080
and paste.
5560
04:44:12,400 --> 04:44:16,718
Now one thing we need to be careful
5561
04:44:14,080 --> 04:44:21,040
about is that we may need to update
5562
04:44:16,718 --> 04:44:24,400
these file paths. So now this file is
5563
04:44:21,040 --> 04:44:26,560
inside the components folder. That means
5564
04:44:24,400 --> 04:44:28,718
dot slash is going to start at the
5565
04:44:26,560 --> 04:44:32,400
components folder. It's not going to be
5566
04:44:28,718 --> 04:44:35,040
able to find the assets folder anymore.
5567
04:44:32,400 --> 04:44:38,320
To fix this at the front, we're going to
5568
04:44:35,040 --> 04:44:41,120
use dot dot slash.
5569
04:44:38,320 --> 04:44:44,878
So just like in the command line, dot
5570
04:44:41,120 --> 04:44:47,040
dot represents the outer folder. This
5571
04:44:44,878 --> 04:44:49,280
means it's going to go out of this
5572
04:44:47,040 --> 04:44:51,600
components folder and into the source
5573
04:44:49,280 --> 04:44:53,920
folder. And then it's going to look for
5574
04:44:51,600 --> 04:44:56,000
the assets folder. It's going to find it
5575
04:44:53,920 --> 04:44:59,680
and it will go into the assets folder
5576
04:44:56,000 --> 04:45:01,600
and find robot.png.
5577
04:44:59,680 --> 04:45:05,040
And we need to make the same change for
5578
04:45:01,600 --> 04:45:08,240
the user image. At the front, we'll use
5579
04:45:05,040 --> 04:45:10,878
dot dot to go out of the components
5580
04:45:08,240 --> 04:45:13,840
folder into source. And then we can
5581
04:45:10,878 --> 04:45:15,760
locate the assets folder.
5582
04:45:13,840 --> 04:45:18,080
And now these imports will work
5583
04:45:15,760 --> 04:45:20,718
correctly. And we finished moving the
5584
04:45:18,080 --> 04:45:22,240
chat message component into its own
5585
04:45:20,718 --> 04:45:24,480
file.
5586
04:45:22,240 --> 04:45:28,600
Let's save.
5587
04:45:24,480 --> 04:45:28,600
And we'll go back to app.jsx.
5588
04:45:28,718 --> 04:45:34,240
And we'll move the chat messages
5589
04:45:30,638 --> 04:45:37,280
component into its own file. Inside the
5590
04:45:34,240 --> 04:45:41,120
components folder, we'll click it. We'll
5591
04:45:37,280 --> 04:45:42,878
click this icon to create a new file.
5592
04:45:41,120 --> 04:45:46,958
And let's name it the same as the
5593
04:45:42,878 --> 04:45:49,958
component capital C chat capital M
5594
04:45:46,958 --> 04:45:49,958
messages.jsx
5595
04:45:50,320 --> 04:45:56,878
and press enter.
5596
04:45:53,360 --> 04:46:00,080
We'll go back to app.jsx JSX and we'll
5597
04:45:56,878 --> 04:46:05,560
select the code for this component which
5598
04:46:00,080 --> 04:46:05,560
is till here and we're going to cut.
5599
04:46:07,360 --> 04:46:13,200
And inside chat messages we're going to
5600
04:46:10,958 --> 04:46:15,840
paste.
5601
04:46:13,200 --> 04:46:19,920
Let's scroll to the top.
5602
04:46:15,840 --> 04:46:23,280
And this component needs use ref and use
5603
04:46:19,920 --> 04:46:28,360
effect. So let's move those imports into
5604
04:46:23,280 --> 04:46:28,360
this file. We'll go to app.jsx.
5605
04:46:28,400 --> 04:46:34,958
We'll select this import at the top
5606
04:46:31,920 --> 04:46:37,920
and copy.
5607
04:46:34,958 --> 04:46:40,400
And in chat messages
5608
04:46:37,920 --> 04:46:44,000
at the top,
5609
04:46:40,400 --> 04:46:46,878
we're going to paste that input.
5610
04:46:44,000 --> 04:46:51,200
And in this component, we do not use use
5611
04:46:46,878 --> 04:46:53,760
state. So we can remove it.
5612
04:46:51,200 --> 04:46:56,400
Let's scroll down. And in this
5613
04:46:53,760 --> 04:46:59,680
component, we also use the chat message
5614
04:46:56,400 --> 04:47:04,480
component. So let's import that. We'll
5615
04:46:59,680 --> 04:47:09,040
scroll up and at the top, we'll import
5616
04:47:04,480 --> 04:47:12,958
curly brackets the chat message
5617
04:47:09,040 --> 04:47:17,280
component from a string
5618
04:47:12,958 --> 04:47:19,440
and we'll locate this file using dot
5619
04:47:17,280 --> 04:47:22,160
slash. So it starts in the current
5620
04:47:19,440 --> 04:47:27,600
folder which is the components folder
5621
04:47:22,160 --> 04:47:31,280
and we'll look for the file chat message
5622
04:47:27,600 --> 04:47:36,958
and remember we don't have to add JSX at
5623
04:47:31,280 --> 04:47:39,920
the end. Vit can automatically add it.
5624
04:47:36,958 --> 04:47:42,718
Finally eslint tells us this component
5625
04:47:39,920 --> 04:47:45,600
is not used in this file. So we need to
5626
04:47:42,718 --> 04:47:48,000
export it. However we're going to do
5627
04:47:45,600 --> 04:47:50,718
something different this time.
5628
04:47:48,000 --> 04:47:52,958
Instead of typing export at the front of
5629
04:47:50,718 --> 04:47:57,200
this function, we're going to scroll
5630
04:47:52,958 --> 04:48:01,520
down to the bottom and at the end, we're
5631
04:47:57,200 --> 04:48:04,080
going to type export default
5632
04:48:01,520 --> 04:48:09,280
and then what we want to export, which
5633
04:48:04,080 --> 04:48:12,560
is the chat messages component.
5634
04:48:09,280 --> 04:48:15,680
As a review, this is called a default
5635
04:48:12,560 --> 04:48:19,200
export. It's useful if we want to export
5636
04:48:15,680 --> 04:48:21,120
one thing from a file. So now let's
5637
04:48:19,200 --> 04:48:24,080
save.
5638
04:48:21,120 --> 04:48:26,320
And to import this, we're going to go to
5639
04:48:24,080 --> 04:48:30,798
app.jsx.
5640
04:48:26,320 --> 04:48:33,440
And at the top, we're going to import.
5641
04:48:30,798 --> 04:48:37,120
And for a default export, we can just
5642
04:48:33,440 --> 04:48:41,200
type a name without curly brackets. So
5643
04:48:37,120 --> 04:48:45,200
we can type chat messages.
5644
04:48:41,200 --> 04:48:47,920
We'll import it from a string
5645
04:48:45,200 --> 04:48:50,400
and we'll type dot slash to start in the
5646
04:48:47,920 --> 04:48:52,638
current folder. So for this file, we're
5647
04:48:50,400 --> 04:48:55,600
in the source folder and then we're
5648
04:48:52,638 --> 04:48:59,120
going to go into the components folder
5649
04:48:55,600 --> 04:49:02,120
components slash and locate chat
5650
04:48:59,120 --> 04:49:02,120
messages.
5651
04:49:02,958 --> 04:49:09,520
So this imports whatever the default
5652
04:49:06,160 --> 04:49:12,878
export is from this file and names it
5653
04:49:09,520 --> 04:49:16,400
chat messages. This feature is called a
5654
04:49:12,878 --> 04:49:19,520
default export. When we import and we
5655
04:49:16,400 --> 04:49:22,798
use curly brackets like up here, this is
5656
04:49:19,520 --> 04:49:24,798
called a named export.
5657
04:49:22,798 --> 04:49:28,000
So some people like using default
5658
04:49:24,798 --> 04:49:31,520
exports because it encourages each file
5659
04:49:28,000 --> 04:49:34,160
to focus on one thing. However, it's up
5660
04:49:31,520 --> 04:49:37,360
to you which style of export you want to
5661
04:49:34,160 --> 04:49:40,560
use. You'll also find that some packages
5662
04:49:37,360 --> 04:49:43,440
use named exports while other packages
5663
04:49:40,560 --> 04:49:46,160
use default exports.
5664
04:49:43,440 --> 04:49:48,240
So, this is just a review of JavaScript
5665
04:49:46,160 --> 04:49:50,480
modules. If you want to learn about
5666
04:49:48,240 --> 04:49:52,878
modules in more detail, you can check
5667
04:49:50,480 --> 04:49:55,680
out my JavaScript full course in the
5668
04:49:52,878 --> 04:49:58,718
video description.
5669
04:49:55,680 --> 04:50:01,680
Next, let's fix these last few errors.
5670
04:49:58,718 --> 04:50:04,480
So we're not using use ref or use effect
5671
04:50:01,680 --> 04:50:07,480
in this file anymore. So we can remove
5672
04:50:04,480 --> 04:50:07,480
these.
5673
04:50:07,680 --> 04:50:14,480
And finally, this app component is
5674
04:50:10,958 --> 04:50:17,600
already in a file called app.jsx.
5675
04:50:14,480 --> 04:50:20,560
So we can just leave it in this file.
5676
04:50:17,600 --> 04:50:23,680
And usually we put the app component
5677
04:50:20,560 --> 04:50:27,120
outside of the components folder because
5678
04:50:23,680 --> 04:50:29,680
app represents the entire application.
5679
04:50:27,120 --> 04:50:32,160
It's sort of the most outer component of
5680
04:50:29,680 --> 04:50:35,280
our application. So it makes sense to
5681
04:50:32,160 --> 04:50:38,560
put it in the outer folder.
5682
04:50:35,280 --> 04:50:40,480
So now let's save this file.
5683
04:50:38,560 --> 04:50:43,680
And let's make sure that all these other
5684
04:50:40,480 --> 04:50:47,840
files are saved. And now we'll open our
5685
04:50:43,680 --> 04:50:50,400
website in the browser and try it out.
5686
04:50:47,840 --> 04:50:53,120
And it looks pretty good. Let's refresh
5687
04:50:50,400 --> 04:50:56,080
just to be safe. And we'll give it a
5688
04:50:53,120 --> 04:51:01,200
try. We'll click this text box and type
5689
04:50:56,080 --> 04:51:03,360
the message hello and press send.
5690
04:51:01,200 --> 04:51:07,920
So, it's working correctly just like
5691
04:51:03,360 --> 04:51:11,200
before. So, we'll go back and now we
5692
04:51:07,920 --> 04:51:14,480
separated each component into its own
5693
04:51:11,200 --> 04:51:17,760
file. And this makes our code easier to
5694
04:51:14,480 --> 04:51:21,120
work with because each file only focuses
5695
04:51:17,760 --> 04:51:23,520
on one component and each file contains
5696
04:51:21,120 --> 04:51:25,120
less code.
5697
04:51:23,520 --> 04:51:28,798
The last thing we're going to do in this
5698
04:51:25,120 --> 04:51:33,760
lesson is to separate the CSS into
5699
04:51:28,798 --> 04:51:35,920
different files. So, let's open app.css.
5700
04:51:33,760 --> 04:51:39,760
And you'll notice that in here it
5701
04:51:35,920 --> 04:51:42,160
contains CSS for multiple components.
5702
04:51:39,760 --> 04:51:45,520
So, we're also going to separate the CSS
5703
04:51:42,160 --> 04:51:47,120
so that each component has its own CSS
5704
04:51:45,520 --> 04:51:50,798
file.
5705
04:51:47,120 --> 04:51:54,000
For example, this CSS code is related to
5706
04:51:50,798 --> 04:51:57,360
the chat input component. So, we'll go
5707
04:51:54,000 --> 04:51:59,760
to the components folder and click it.
5708
04:51:57,360 --> 04:52:02,400
And we'll click up here to create a new
5709
04:51:59,760 --> 04:52:05,920
file. And we're going to name it the
5710
04:52:02,400 --> 04:52:08,958
same as the component, capital C, chat,
5711
04:52:05,920 --> 04:52:13,520
capital I input
5712
04:52:08,958 --> 04:52:16,160
CSS, and press enter.
5713
04:52:13,520 --> 04:52:19,920
and we'll move all the CSS related to
5714
04:52:16,160 --> 04:52:22,160
the chat input component in here. So in
5715
04:52:19,920 --> 04:52:24,400
app.css,
5716
04:52:22,160 --> 04:52:27,440
we'll select this code for the send
5717
04:52:24,400 --> 04:52:29,520
button, the chat input, and the chat
5718
04:52:27,440 --> 04:52:32,798
input container.
5719
04:52:29,520 --> 04:52:35,760
And we'll cut
5720
04:52:32,798 --> 04:52:38,878
and chat input.css,
5721
04:52:35,760 --> 04:52:41,520
we're going to paste that code
5722
04:52:38,878 --> 04:52:44,878
and save.
5723
04:52:41,520 --> 04:52:48,600
And now to load this CSS code, we're
5724
04:52:44,878 --> 04:52:48,600
going to open chatinput.jsx.
5725
04:52:48,718 --> 04:52:54,560
And at the top, we're going to import a
5726
04:52:52,958 --> 04:52:57,200
string
5727
04:52:54,560 --> 04:53:01,280
and the CSS file we want to load, which
5728
04:52:57,200 --> 04:53:03,280
is this one. So we'll type dot slash to
5729
04:53:01,280 --> 04:53:07,280
start in the current folder, which is
5730
04:53:03,280 --> 04:53:10,280
components, and we'll load chat
5731
04:53:07,280 --> 04:53:10,280
input.css.
5732
04:53:11,040 --> 04:53:17,440
and save.
5733
04:53:13,760 --> 04:53:20,638
As a review, this is a feature of Vit.
5734
04:53:17,440 --> 04:53:23,600
If we import a CSS file, it will load
5735
04:53:20,638 --> 04:53:25,520
all the CSS in the file and apply it to
5736
04:53:23,600 --> 04:53:29,958
the website.
5737
04:53:25,520 --> 04:53:29,958
Next, let's go back to app.css.
5738
04:53:30,320 --> 04:53:34,958
And these styles are for the app
5739
04:53:32,638 --> 04:53:39,840
component. So, it actually makes sense
5740
04:53:34,958 --> 04:53:42,958
to leave them inside the app.css file.
5741
04:53:39,840 --> 04:53:45,760
Below this, these styles are for the
5742
04:53:42,958 --> 04:53:48,160
chat message component. So, let's create
5743
04:53:45,760 --> 04:53:50,878
a CSS file for the chat message
5744
04:53:48,160 --> 04:53:53,600
component. We'll go into the components
5745
04:53:50,878 --> 04:53:57,360
folder and click here to create a new
5746
04:53:53,600 --> 04:54:00,360
file. And we'll name this chat
5747
04:53:57,360 --> 04:54:00,360
message.css
5748
04:54:00,560 --> 04:54:07,600
and press enter.
5749
04:54:03,760 --> 04:54:09,600
And we'll go back to app.css CSS and
5750
04:54:07,600 --> 04:54:11,920
we're going to select the styles for the
5751
04:54:09,600 --> 04:54:15,280
chat message user, the chat message
5752
04:54:11,920 --> 04:54:17,200
robot, the text, as well as the chat
5753
04:54:15,280 --> 04:54:20,840
message profile
5754
04:54:17,200 --> 04:54:20,840
and cut.
5755
04:54:21,040 --> 04:54:28,080
And then inside chat message.css,
5756
04:54:24,400 --> 04:54:30,480
we're going to paste the CSS code and
5757
04:54:28,080 --> 04:54:32,480
save.
5758
04:54:30,480 --> 04:54:36,560
And we're going to load this by going
5759
04:54:32,480 --> 04:54:40,718
into chat message.jsx JSX and at the top
5760
04:54:36,560 --> 04:54:45,200
we're going to import a string and we'll
5761
04:54:40,718 --> 04:54:48,240
import this file using dot slash
5762
04:54:45,200 --> 04:54:53,120
chat message
5763
04:54:48,240 --> 04:54:56,480
CSS and save.
5764
04:54:53,120 --> 04:54:59,280
We'll go back to app.css.
5765
04:54:56,480 --> 04:55:01,840
Finally, these styles are for the chat
5766
04:54:59,280 --> 04:55:04,320
messages component.
5767
04:55:01,840 --> 04:55:07,440
First, we'll create a CSS file for this
5768
04:55:04,320 --> 04:55:10,320
component. So, we'll click this folder
5769
04:55:07,440 --> 04:55:15,520
and click here to create a new file and
5770
04:55:10,320 --> 04:55:18,320
we'll name it chat messages.css
5771
04:55:15,520 --> 04:55:20,400
and press enter.
5772
04:55:18,320 --> 04:55:25,040
And in app.css,
5773
04:55:20,400 --> 04:55:28,080
we're going to select this code and cut.
5774
04:55:25,040 --> 04:55:31,718
And in chat messages.css, CSS. We're
5775
04:55:28,080 --> 04:55:31,718
going to paste
5776
04:55:32,160 --> 04:55:37,360
and save.
5777
04:55:35,040 --> 04:55:39,440
And we'll load this by opening chat
5778
04:55:37,360 --> 04:55:45,600
messages.jsx.
5779
04:55:39,440 --> 04:55:50,240
And at the top, we will import a string.
5780
04:55:45,600 --> 04:55:53,240
And we'll locate the file dot slash chat
5781
04:55:50,240 --> 04:55:53,240
messages.css
5782
04:55:53,760 --> 04:56:00,400
and save.
5783
04:55:56,320 --> 04:56:02,798
And finally, we'll go back to app.css
5784
04:56:00,400 --> 04:56:05,440
and make sure that we save all of our
5785
04:56:02,798 --> 04:56:07,280
files.
5786
04:56:05,440 --> 04:56:09,840
Now, let's open our website in the
5787
04:56:07,280 --> 04:56:11,840
browser.
5788
04:56:09,840 --> 04:56:13,360
And everything should look and work
5789
04:56:11,840 --> 04:56:16,160
correctly.
5790
04:56:13,360 --> 04:56:18,878
Let's go back to our code.
5791
04:56:16,160 --> 04:56:21,840
And that's it. We split up our
5792
04:56:18,878 --> 04:56:25,120
JavaScript and our CSS code into
5793
04:56:21,840 --> 04:56:28,878
separate files. And now each component
5794
04:56:25,120 --> 04:56:32,240
has its own JavaScript file and its own
5795
04:56:28,878 --> 04:56:35,280
CSS file. This makes our code a lot
5796
04:56:32,240 --> 04:56:38,240
easier to work with because each file is
5797
04:56:35,280 --> 04:56:41,520
focused on only one component and each
5798
04:56:38,240 --> 04:56:43,840
file contains less code.
5799
04:56:41,520 --> 04:56:46,718
In the next lesson, we'll use this new
5800
04:56:43,840 --> 04:56:49,760
React setup to work on a bigger React
5801
04:56:46,718 --> 04:56:52,080
project, which is an ecommerce website
5802
04:56:49,760 --> 04:56:55,120
that has multiple pages and more
5803
04:56:52,080 --> 04:56:58,240
advanced features. And that's the end of
5804
04:56:55,120 --> 04:57:01,600
this lesson. In this lesson, we created
5805
04:56:58,240 --> 04:57:06,320
a proper React setup using the command
5806
04:57:01,600 --> 04:57:08,638
line, npm, and vit. We reviewed the
5807
04:57:06,320 --> 04:57:11,920
command line, which lets us give
5808
04:57:08,638 --> 04:57:15,360
commands to our computer. We reviewed
5809
04:57:11,920 --> 04:57:18,958
npm which helps us download and use
5810
04:57:15,360 --> 04:57:21,680
external libraries or packages. We
5811
04:57:18,958 --> 04:57:25,440
learned the create vit package which
5812
04:57:21,680 --> 04:57:28,560
helps us create a proper React setup. We
5813
04:57:25,440 --> 04:57:31,520
moved our chatbot project into the new
5814
04:57:28,560 --> 04:57:34,560
React setup. We learned how to use
5815
04:57:31,520 --> 04:57:37,920
eslint which highlights problems in our
5816
04:57:34,560 --> 04:57:41,040
JavaScript code. We reviewed JavaScript
5817
04:57:37,920 --> 04:57:44,400
modules which lets us separate our code
5818
04:57:41,040 --> 04:57:47,040
into different files. And we separated
5819
04:57:44,400 --> 04:57:52,000
each component in the chatbot project
5820
04:57:47,040 --> 04:57:54,080
into its own JavaScript and CSS files.
5821
04:57:52,000 --> 04:57:55,840
Thanks for watching so far. If you find
5822
04:57:54,080 --> 04:57:58,080
this course valuable, you can support
5823
04:57:55,840 --> 04:58:00,958
the channel by liking this video,
5824
04:57:58,080 --> 04:58:02,718
leaving a comment, and subscribing.
5825
04:58:00,958 --> 04:58:04,798
You can also check out the premium
5826
04:58:02,718 --> 04:58:07,280
version of this course where you'll get
5827
04:58:04,798 --> 04:58:10,080
all the lessons ads free broken down
5828
04:58:07,280 --> 04:58:12,240
into small videos as well as additional
5829
04:58:10,080 --> 04:58:15,040
lessons and more projects that are not
5830
04:58:12,240 --> 04:58:17,520
in the public react course. Currently,
5831
04:58:15,040 --> 04:58:19,440
the premium course contains 11 lessons
5832
04:58:17,520 --> 04:58:22,080
and I'll be adding more lessons in the
5833
04:58:19,440 --> 04:58:24,638
future. You can also get a certificate
5834
04:58:22,080 --> 04:58:27,120
of completion, create comments or
5835
04:58:24,638 --> 04:58:29,440
questions on specific topics, get
5836
04:58:27,120 --> 04:58:31,920
progress tracking, search and course
5837
04:58:29,440 --> 04:58:34,000
notes. You can also check out the
5838
04:58:31,920 --> 04:58:37,440
membership where you can get this course
5839
04:58:34,000 --> 04:58:40,400
ads free separated by each lesson, AI
5840
04:58:37,440 --> 04:58:42,240
videos, backend tutorials, and more.
5841
04:58:40,400 --> 04:58:45,520
Thanks again, and let's continue with
5842
04:58:42,240 --> 04:58:47,680
the course. Here are some exercises to
5843
04:58:45,520 --> 04:58:51,000
help you practice what we learned in
5844
04:58:47,680 --> 04:58:51,000
this lesson.
5845
04:59:34,080 --> 04:59:38,958
In this lesson, we're going to start a
5846
04:59:36,240 --> 04:59:41,040
bigger project using React and we'll
5847
04:59:38,958 --> 04:59:43,120
learn an important feature called
5848
04:59:41,040 --> 04:59:45,280
routing.
5849
04:59:43,120 --> 04:59:47,520
By the way, if something in this lesson
5850
04:59:45,280 --> 04:59:49,040
does not work for you, you can check the
5851
04:59:47,520 --> 04:59:51,360
troubleshooting guide in the
5852
04:59:49,040 --> 04:59:53,600
description.
5853
04:59:51,360 --> 04:59:57,440
First, let's take a look at the project
5854
04:59:53,600 --> 05:00:00,798
we're going to build. In our browser,
5855
04:59:57,440 --> 05:00:03,600
we're going to create a new tab.
5856
05:00:00,798 --> 05:00:06,600
And at the top, we're going to go to
5857
05:00:03,600 --> 05:00:06,600
supersimple.dev/proscommerce.
5858
05:00:13,040 --> 05:00:17,638
And we can close these other tabs.
5859
05:00:18,000 --> 05:00:23,840
This is an e-commerce project or an
5860
05:00:20,878 --> 05:00:26,400
online shopping project. We can add
5861
05:00:23,840 --> 05:00:28,798
products to our cart.
5862
05:00:26,400 --> 05:00:31,798
And then we can click here to view our
5863
05:00:28,798 --> 05:00:31,798
cart.
5864
05:00:32,000 --> 05:00:36,878
And this page will display all the
5865
05:00:33,760 --> 05:00:39,520
products in our cart. It will calculate
5866
05:00:36,878 --> 05:00:42,400
the total for our order. and it will
5867
05:00:39,520 --> 05:00:45,520
also calculate a delivery date. And then
5868
05:00:42,400 --> 05:00:47,440
we can create an order.
5869
05:00:45,520 --> 05:00:50,480
And this page will list all of the
5870
05:00:47,440 --> 05:00:53,920
orders that we created. We can also
5871
05:00:50,480 --> 05:00:55,760
click this button to track the order. So
5872
05:00:53,920 --> 05:00:58,958
depending on the delivery date, it will
5873
05:00:55,760 --> 05:01:00,560
show you the progress of the delivery.
5874
05:00:58,958 --> 05:01:02,878
And we'll click here to go back to the
5875
05:01:00,560 --> 05:01:05,360
orders page.
5876
05:01:02,878 --> 05:01:07,520
So notice that this project has multiple
5877
05:01:05,360 --> 05:01:10,560
pages. So, it's much bigger than the
5878
05:01:07,520 --> 05:01:13,440
chatbot project. And we're working with
5879
05:01:10,560 --> 05:01:16,320
more data than before. So, we have
5880
05:01:13,440 --> 05:01:18,240
products, we have a cart, and we have
5881
05:01:16,320 --> 05:01:20,400
orders.
5882
05:01:18,240 --> 05:01:23,200
By the way, if you ever need to reset
5883
05:01:20,400 --> 05:01:25,600
the cart or the orders, you can click
5884
05:01:23,200 --> 05:01:28,798
this reset button here in the bottom
5885
05:01:25,600 --> 05:01:32,080
right, and it will reset everything back
5886
05:01:28,798 --> 05:01:35,520
to the default data. So, let's click the
5887
05:01:32,080 --> 05:01:38,000
top left to go back to the main page.
5888
05:01:35,520 --> 05:01:41,120
And now that we introduced the project,
5889
05:01:38,000 --> 05:01:43,920
we're going to build this with React.
5890
05:01:41,120 --> 05:01:46,400
We'll go back to VS Code
5891
05:01:43,920 --> 05:01:48,080
and we'll collapse the chatbot project
5892
05:01:46,400 --> 05:01:50,400
folder.
5893
05:01:48,080 --> 05:01:53,120
And before we create a new React
5894
05:01:50,400 --> 05:01:56,958
project, let's group these older files
5895
05:01:53,120 --> 05:02:00,160
into a folder. We'll click this HTML
5896
05:01:56,958 --> 05:02:04,440
file and we'll create a folder. And
5897
05:02:00,160 --> 05:02:04,440
let's name it React-basics.
5898
05:02:05,040 --> 05:02:09,520
and we're going to move all these files
5899
05:02:06,718 --> 05:02:12,160
into this folder. Now, a shortcut here
5900
05:02:09,520 --> 05:02:14,560
is to click this file and then press
5901
05:02:12,160 --> 05:02:17,440
shift on your keyboard and click the
5902
05:02:14,560 --> 05:02:20,400
bottom file and it will select all the
5903
05:02:17,440 --> 05:02:24,200
files between those two and we can drag
5904
05:02:20,400 --> 05:02:24,200
this into the folder
5905
05:02:25,120 --> 05:02:31,120
and we'll collapse it. So, that just
5906
05:02:28,160 --> 05:02:33,760
helps us stay a little more organized.
5907
05:02:31,120 --> 05:02:37,200
Next, we're going to set up a new React
5908
05:02:33,760 --> 05:02:40,240
project using VIT. To do that, we're
5909
05:02:37,200 --> 05:02:42,718
going to go to our terminal.
5910
05:02:40,240 --> 05:02:44,878
If you have the VIT server running, you
5911
05:02:42,718 --> 05:02:48,718
can close it by pressing Ctrl + C on
5912
05:02:44,878 --> 05:02:50,240
Windows or control C on Mac.
5913
05:02:48,718 --> 05:02:52,480
So, if you're continuing from the
5914
05:02:50,240 --> 05:02:54,718
previous lesson, the terminal should be
5915
05:02:52,480 --> 05:02:57,600
running inside the chatbot project
5916
05:02:54,718 --> 05:03:01,280
folder. To confirm that, we can run
5917
05:02:57,600 --> 05:03:02,878
print working directory or pwd.
5918
05:03:01,280 --> 05:03:05,840
And it will tell us that we're inside
5919
05:03:02,878 --> 05:03:08,320
the chatbot project folder. Now, to set
5920
05:03:05,840 --> 05:03:11,600
up a new React project, we want to be
5921
05:03:08,320 --> 05:03:14,400
inside the React-Course folder. So, to
5922
05:03:11,600 --> 05:03:19,680
get out of the chatbot project folder,
5923
05:03:14,400 --> 05:03:22,320
we can run the command cd dot dot.
5924
05:03:19,680 --> 05:03:24,878
And if we press pwd,
5925
05:03:22,320 --> 05:03:27,840
we should be inside the react-course
5926
05:03:24,878 --> 05:03:32,040
folder. Another way to do this is to
5927
05:03:27,840 --> 05:03:32,040
just start a new terminal.
5928
05:03:33,520 --> 05:03:39,120
Now we'll use vit again to set up a new
5929
05:03:37,120 --> 05:03:41,200
react project.
5930
05:03:39,120 --> 05:03:43,760
In the command line, we'll run the
5931
05:03:41,200 --> 05:03:45,280
command npx
5932
05:03:43,760 --> 05:03:48,480
create-ash
5933
05:03:45,280 --> 05:03:50,798
vit and we'll type at and give it a
5934
05:03:48,480 --> 05:03:53,360
version. So, you can find the version in
5935
05:03:50,798 --> 05:03:55,680
the description by clicking this link
5936
05:03:53,360 --> 05:03:57,520
and going to lesson six and you'll find
5937
05:03:55,680 --> 05:04:00,958
the version of vit that we're going to
5938
05:03:57,520 --> 05:04:03,280
use as well as the full command.
5939
05:04:00,958 --> 05:04:05,600
By the way, the version that you see in
5940
05:04:03,280 --> 05:04:08,320
this video might be different than the
5941
05:04:05,600 --> 05:04:10,320
version you see on your page. You should
5942
05:04:08,320 --> 05:04:13,600
always use the version that you see on
5943
05:04:10,320 --> 05:04:16,878
your page. I did this because it's hard
5944
05:04:13,600 --> 05:04:20,560
to update the video. So, this lets me
5945
05:04:16,878 --> 05:04:23,680
easily update the version in the future.
5946
05:04:20,560 --> 05:04:25,680
So, select the version on your page.
5947
05:04:23,680 --> 05:04:28,000
Copy.
5948
05:04:25,680 --> 05:04:31,718
So, I'm going to paste the version here
5949
05:04:28,000 --> 05:04:31,718
and press enter.
5950
05:04:32,000 --> 05:04:37,360
For the project name, let's name it
5951
05:04:34,878 --> 05:04:40,480
ecommerce-
5952
05:04:37,360 --> 05:04:42,400
project. Press enter.
5953
05:04:40,480 --> 05:04:46,240
And from this list, we'll press the down
5954
05:04:42,400 --> 05:04:48,718
arrow and select React.
5955
05:04:46,240 --> 05:04:51,680
And from this list, we'll press down and
5956
05:04:48,718 --> 05:04:54,160
select JavaScript.
5957
05:04:51,680 --> 05:04:57,360
And it will set up a new React project
5958
05:04:54,160 --> 05:05:00,638
inside our React-Course folder, which we
5959
05:04:57,360 --> 05:05:04,718
see right here. And now we need to go
5960
05:05:00,638 --> 05:05:06,878
into this folder using the command cd
5961
05:05:04,718 --> 05:05:09,280
ecommerce-
5962
05:05:06,878 --> 05:05:11,920
project.
5963
05:05:09,280 --> 05:05:14,400
And we're going to run npm install to
5964
05:05:11,920 --> 05:05:16,240
install all of the packages inside
5965
05:05:14,400 --> 05:05:21,560
package.json.
5966
05:05:16,240 --> 05:05:21,560
So we'll run npm install.
5967
05:05:24,638 --> 05:05:30,240
Once we have everything installed, we
5968
05:05:26,878 --> 05:05:34,520
can run this project using the command
5969
05:05:30,240 --> 05:05:34,520
npm rundev.
5970
05:05:35,600 --> 05:05:40,480
And now we can open this project using
5971
05:05:37,680 --> 05:05:44,240
this URL in the browser. So we'll select
5972
05:05:40,480 --> 05:05:46,400
this URL and copy.
5973
05:05:44,240 --> 05:05:49,680
And in the browser,
5974
05:05:46,400 --> 05:05:53,878
we're going to create a new tab.
5975
05:05:49,680 --> 05:05:53,878
And then here we'll paste.
5976
05:05:54,798 --> 05:06:01,840
And now we have a new React project.
5977
05:05:59,440 --> 05:06:04,080
Before we start updating the code, we're
5978
05:06:01,840 --> 05:06:07,680
actually going to review a tool called
5979
05:06:04,080 --> 05:06:10,798
Git. Git helps us track changes in our
5980
05:06:07,680 --> 05:06:12,638
code, and it's useful in bigger projects
5981
05:06:10,798 --> 05:06:15,600
because we're going to be making lots of
5982
05:06:12,638 --> 05:06:18,400
changes, and it's easy to forget what we
5983
05:06:15,600 --> 05:06:20,240
changed. So, Git will help us track our
5984
05:06:18,400 --> 05:06:23,040
changes.
5985
05:06:20,240 --> 05:06:25,440
We're going to go to our code
5986
05:06:23,040 --> 05:06:27,520
and we're going to open the Git section
5987
05:06:25,440 --> 05:06:30,080
here.
5988
05:06:27,520 --> 05:06:32,958
If you already have Git installed, it
5989
05:06:30,080 --> 05:06:35,120
should say initialize repository. If you
5990
05:06:32,958 --> 05:06:37,440
do not have Git installed, you can click
5991
05:06:35,120 --> 05:06:39,360
this link in the description, go to
5992
05:06:37,440 --> 05:06:43,040
lesson six, and follow these
5993
05:06:39,360 --> 05:06:45,440
instructions to install Git.
5994
05:06:43,040 --> 05:06:48,798
Once you have git installed, we'll click
5995
05:06:45,440 --> 05:06:52,080
initialize repository.
5996
05:06:48,798 --> 05:06:56,000
Now, a repository is just a folder where
5997
05:06:52,080 --> 05:06:59,040
git is tracking changes. At first, git
5998
05:06:56,000 --> 05:07:02,160
will tell us that all of these files or
5999
05:06:59,040 --> 05:07:05,600
everything inside our react-ourse folder
6000
05:07:02,160 --> 05:07:08,638
is new. And that's because a repository
6001
05:07:05,600 --> 05:07:11,920
starts out as empty. So, git considers
6002
05:07:08,638 --> 05:07:14,718
all of these files as new files.
6003
05:07:11,920 --> 05:07:17,360
We'll go back to the git section
6004
05:07:14,718 --> 05:07:20,878
and we'll scroll up and we're going to
6005
05:07:17,360 --> 05:07:23,280
save all of these files into git. At the
6006
05:07:20,878 --> 05:07:26,560
top, we'll type a message to describe
6007
05:07:23,280 --> 05:07:33,120
our changes. So, let's type the message
6008
05:07:26,560 --> 05:07:34,878
add the starting files and press commit.
6009
05:07:33,120 --> 05:07:38,240
Commit means we're going to save these
6010
05:07:34,878 --> 05:07:40,080
changes into git.
6011
05:07:38,240 --> 05:07:42,080
Now, you might run into an error when
6012
05:07:40,080 --> 05:07:45,360
using git and it tells you you have to
6013
05:07:42,080 --> 05:07:47,440
configure your username and email. To do
6014
05:07:45,360 --> 05:07:49,440
that, you can run these two commands in
6015
05:07:47,440 --> 05:07:51,920
the command line and choose your
6016
05:07:49,440 --> 05:07:55,680
username and choose the email you want
6017
05:07:51,920 --> 05:07:58,638
to use and try to commit again. And now
6018
05:07:55,680 --> 05:08:01,040
it should work.
6019
05:07:58,638 --> 05:08:04,040
If it asks to confirm, you can press
6020
05:08:01,040 --> 05:08:04,040
yes.
6021
05:08:04,080 --> 05:08:10,160
And now git will start tracking any new
6022
05:08:06,798 --> 05:08:13,520
changes that we make. For example, if we
6023
05:08:10,160 --> 05:08:18,240
go into our code and open the e-commerce
6024
05:08:13,520 --> 05:08:21,680
project and change some of these files,
6025
05:08:18,240 --> 05:08:26,718
we type test and save.
6026
05:08:21,680 --> 05:08:29,440
And in the source app.jsx, we type test
6027
05:08:26,718 --> 05:08:32,718
and save.
6028
05:08:29,440 --> 05:08:34,958
And then we can go to the git section.
6029
05:08:32,718 --> 05:08:38,560
And now git will tell us everything that
6030
05:08:34,958 --> 05:08:41,440
we changed in these two files. So as you
6031
05:08:38,560 --> 05:08:43,840
can see git helps us track changes in
6032
05:08:41,440 --> 05:08:47,440
our code.
6033
05:08:43,840 --> 05:08:50,080
Also git lets us undo our changes. So to
6034
05:08:47,440 --> 05:08:53,360
undo this change we can click this arrow
6035
05:08:50,080 --> 05:08:55,840
button here.
6036
05:08:53,360 --> 05:09:00,840
And to undo all of our changes at the
6037
05:08:55,840 --> 05:09:00,840
top we can click this arrow button here.
6038
05:09:02,400 --> 05:09:07,520
So, that's a review of how Git works. It
6039
05:09:05,440 --> 05:09:10,958
can track our changes and it lets us
6040
05:09:07,520 --> 05:09:13,760
easily undo our changes. Let's go back
6041
05:09:10,958 --> 05:09:16,638
to our files
6042
05:09:13,760 --> 05:09:18,798
and remember that in our React setup, we
6043
05:09:16,638 --> 05:09:21,200
have a file called.git
6044
05:09:18,798 --> 05:09:23,440
ignore. So, now we can learn what this
6045
05:09:21,200 --> 05:09:26,320
file does.
6046
05:09:23,440 --> 05:09:29,440
Git ignore tells git which files and
6047
05:09:26,320 --> 05:09:33,280
folders to ignore or not to track
6048
05:09:29,440 --> 05:09:36,638
changes. For example, in this file we
6049
05:09:33,280 --> 05:09:38,878
can see the folder node modules. So this
6050
05:09:36,638 --> 05:09:41,520
tells git that if there are any changes
6051
05:09:38,878 --> 05:09:44,160
inside node modules it will not be
6052
05:09:41,520 --> 05:09:46,878
tracked. The reason we ignore node
6053
05:09:44,160 --> 05:09:49,920
modules is because this folder is very
6054
05:09:46,878 --> 05:09:53,440
large and none of the changes inside
6055
05:09:49,920 --> 05:09:56,958
this folder are changes that we make.
6056
05:09:53,440 --> 05:10:00,718
Instead, we generate this folder using a
6057
05:09:56,958 --> 05:10:03,280
package.json and npm install. So,
6058
05:10:00,718 --> 05:10:05,200
because this folder is generated, it
6059
05:10:03,280 --> 05:10:09,520
doesn't make a lot of sense to save this
6060
05:10:05,200 --> 05:10:12,878
in git. So, that's a review of Git. Git
6061
05:10:09,520 --> 05:10:15,520
helps us track changes in our code and
6062
05:10:12,878 --> 05:10:18,638
doggit ignore tells Git which files and
6063
05:10:15,520 --> 05:10:21,200
folders to ignore or to not track
6064
05:10:18,638 --> 05:10:24,400
changes.
6065
05:10:21,200 --> 05:10:26,400
Now we're ready to start the project.
6066
05:10:24,400 --> 05:10:30,718
First, we're actually going to download
6067
05:10:26,400 --> 05:10:33,440
the HTML and CSS for this project. We'll
6068
05:10:30,718 --> 05:10:36,878
click this link in the description and
6069
05:10:33,440 --> 05:10:40,480
then click here.
6070
05:10:36,878 --> 05:10:45,040
And this will open GitHub.
6071
05:10:40,480 --> 05:10:47,920
And this contains all the HTML and CSS
6072
05:10:45,040 --> 05:10:50,400
for the e-commerce project. The reason
6073
05:10:47,920 --> 05:10:53,760
we're going to use some starting code is
6074
05:10:50,400 --> 05:10:56,000
because this course is focused on React.
6075
05:10:53,760 --> 05:10:58,400
By using this code, we can focus on
6076
05:10:56,000 --> 05:11:01,760
learning React features rather than
6077
05:10:58,400 --> 05:11:04,638
spending our time writing HTML and CSS.
6078
05:11:01,760 --> 05:11:06,638
If you want to practice HTML and CSS,
6079
05:11:04,638 --> 05:11:09,360
you can try to recreate the design on
6080
05:11:06,638 --> 05:11:11,440
your own after this course.
6081
05:11:09,360 --> 05:11:14,878
Another reason we're using some starting
6082
05:11:11,440 --> 05:11:17,840
code is in the real world, we often work
6083
05:11:14,878 --> 05:11:19,920
on an existing project rather than a new
6084
05:11:17,840 --> 05:11:24,400
project. So this will give us some
6085
05:11:19,920 --> 05:11:26,958
practice working on an existing project.
6086
05:11:24,400 --> 05:11:29,760
So to download the starting code, we're
6087
05:11:26,958 --> 05:11:33,958
going to click this code button here and
6088
05:11:29,760 --> 05:11:33,958
we'll click download zip
6089
05:11:34,240 --> 05:11:42,000
and we'll open our downloads folder.
6090
05:11:38,560 --> 05:11:44,718
For me, it's here. And we're going to
6091
05:11:42,000 --> 05:11:49,120
unzip this file. On Windows, we're going
6092
05:11:44,718 --> 05:11:51,200
to rightclick and click extract all
6093
05:11:49,120 --> 05:11:53,440
and extract.
6094
05:11:51,200 --> 05:11:56,760
And on Mac, you can just doubleclick the
6095
05:11:53,440 --> 05:11:56,760
zip file.
6096
05:11:56,958 --> 05:12:00,160
And then we're going to open this
6097
05:11:58,320 --> 05:12:01,600
folder.
6098
05:12:00,160 --> 05:12:05,440
And we're going to move all of these
6099
05:12:01,600 --> 05:12:08,240
files and folders into VS Code. First,
6100
05:12:05,440 --> 05:12:10,000
let's go into VS Code.
6101
05:12:08,240 --> 05:12:13,200
And we're going to organize all of those
6102
05:12:10,000 --> 05:12:16,400
files into a new folder. So, we'll click
6103
05:12:13,200 --> 05:12:18,320
the e-commerce project and create a new
6104
05:12:16,400 --> 05:12:24,120
folder.
6105
05:12:18,320 --> 05:12:24,120
And we'll name this starting dashcode.
6106
05:12:24,718 --> 05:12:28,080
And we'll open our downloads folder
6107
05:12:26,400 --> 05:12:29,520
again.
6108
05:12:28,080 --> 05:12:32,878
And we're going to move all of these
6109
05:12:29,520 --> 05:12:35,840
files into this new folder. So we'll
6110
05:12:32,878 --> 05:12:40,040
select it. And we're just going to drag
6111
05:12:35,840 --> 05:12:40,040
it into starting code.
6112
05:12:40,878 --> 05:12:46,160
And let's save these changes into Git.
6113
05:12:43,760 --> 05:12:48,240
So then Git will start tracking any new
6114
05:12:46,160 --> 05:12:51,360
changes that we make.
6115
05:12:48,240 --> 05:12:54,240
We'll open the git section and here we
6116
05:12:51,360 --> 05:12:58,638
added all of the starting code and at
6117
05:12:54,240 --> 05:13:00,160
the top let's type the message add the
6118
05:12:58,638 --> 05:13:08,040
starting
6119
05:13:00,160 --> 05:13:08,040
HTML and CSS code and commit.
6120
05:13:10,878 --> 05:13:15,840
Let's go back to our files
6121
05:13:14,000 --> 05:13:20,000
and we're going to go to the starting
6122
05:13:15,840 --> 05:13:23,120
code and look for index.html
6123
05:13:20,000 --> 05:13:25,120
and we're going to rightclick this file
6124
05:13:23,120 --> 05:13:27,520
and we're going to open with live
6125
05:13:25,120 --> 05:13:30,638
server.
6126
05:13:27,520 --> 05:13:34,400
And you'll see we have all the HTML and
6127
05:13:30,638 --> 05:13:37,760
CSS for this project. So now we're going
6128
05:13:34,400 --> 05:13:41,120
to create this using React. We'll start
6129
05:13:37,760 --> 05:13:43,920
by creating this main page. We usually
6130
05:13:41,120 --> 05:13:47,600
call this the homepage.
6131
05:13:43,920 --> 05:13:50,638
By the way, a page or a web page is a
6132
05:13:47,600 --> 05:13:54,718
single page like the homepage or the
6133
05:13:50,638 --> 05:13:56,320
cart page. A website is a group of web
6134
05:13:54,718 --> 05:13:58,798
pages.
6135
05:13:56,320 --> 05:14:03,840
To create this homepage, we're going to
6136
05:13:58,798 --> 05:14:06,718
move all of the HTML and CSS into React.
6137
05:14:03,840 --> 05:14:09,440
We'll go back to VS Code
6138
05:14:06,718 --> 05:14:12,440
and we'll open the starting code
6139
05:14:09,440 --> 05:14:12,440
index.html.
6140
05:14:12,878 --> 05:14:18,718
And this file contains the HTML for the
6141
05:14:16,240 --> 05:14:21,920
homepage. And we're going to move this
6142
05:14:18,718 --> 05:14:25,200
HTML into the index.html
6143
05:14:21,920 --> 05:14:28,878
here for the e-commerce project. So,
6144
05:14:25,200 --> 05:14:31,840
let's actually drag this index.html HTML
6145
05:14:28,878 --> 05:14:35,760
and we'll open it at the bottom
6146
05:14:31,840 --> 05:14:38,160
so we can see these files side by side
6147
05:14:35,760 --> 05:14:42,160
and we'll move the HTML in the starting
6148
05:14:38,160 --> 05:14:45,280
code into our project one by one.
6149
05:14:42,160 --> 05:14:47,680
We already have the doc type HTML inside
6150
05:14:45,280 --> 05:14:52,560
here. So we don't need to move it. And
6151
05:14:47,680 --> 05:14:54,400
we also have the HTML and head elements.
6152
05:14:52,560 --> 05:14:57,200
The first thing that we do need to move
6153
05:14:54,400 --> 05:15:00,560
is this title. So we'll select this
6154
05:14:57,200 --> 05:15:03,920
title and then cut.
6155
05:15:00,560 --> 05:15:07,440
And inside the projects HTML file, let's
6156
05:15:03,920 --> 05:15:11,040
replace the title here.
6157
05:15:07,440 --> 05:15:14,878
And now we'll keep going one by one.
6158
05:15:11,040 --> 05:15:16,878
Next, this meta viewport element makes
6159
05:15:14,878 --> 05:15:19,840
the website look good on smaller
6160
05:15:16,878 --> 05:15:22,798
screens. Now, we already have this
6161
05:15:19,840 --> 05:15:25,440
inside our React setup, so we do not
6162
05:15:22,798 --> 05:15:29,280
need to move it over. Let's actually
6163
05:15:25,440 --> 05:15:32,320
select this code and delete it so that
6164
05:15:29,280 --> 05:15:36,480
we can keep track of our progress.
6165
05:15:32,320 --> 05:15:39,600
Next, this code loads a font or a style
6166
05:15:36,480 --> 05:15:41,360
of text called rooto.
6167
05:15:39,600 --> 05:15:43,360
We're going to use this font in the
6168
05:15:41,360 --> 05:15:47,680
e-commerce project. So, we're going to
6169
05:15:43,360 --> 05:15:50,080
move it over. Let's select this code
6170
05:15:47,680 --> 05:15:53,200
and cut.
6171
05:15:50,080 --> 05:15:55,840
And inside our e-commerce project, we're
6172
05:15:53,200 --> 05:15:57,920
going to paste.
6173
05:15:55,840 --> 05:16:00,638
And if you need to, you can fix this
6174
05:15:57,920 --> 05:16:04,560
spacing by selecting these lines and
6175
05:16:00,638 --> 05:16:08,480
press shift tab a few times.
6176
05:16:04,560 --> 05:16:11,200
Next, these lines of code load the CSS
6177
05:16:08,480 --> 05:16:14,480
for our project. Now, remember, when
6178
05:16:11,200 --> 05:16:16,560
using VIT, we actually import our CSS
6179
05:16:14,480 --> 05:16:18,080
files.
6180
05:16:16,560 --> 05:16:21,920
So, we're not going to move these over
6181
05:16:18,080 --> 05:16:25,760
for now, but we'll keep these for later.
6182
05:16:21,920 --> 05:16:28,638
Finally, if we scroll down, the HTML
6183
05:16:25,760 --> 05:16:32,000
inside the body creates the contents of
6184
05:16:28,638 --> 05:16:35,040
the page. Now, the contents of the page
6185
05:16:32,000 --> 05:16:37,360
should be created by React.
6186
05:16:35,040 --> 05:16:39,120
So, let's move this into a React
6187
05:16:37,360 --> 05:16:41,760
component.
6188
05:16:39,120 --> 05:16:45,920
First, we'll create a component for the
6189
05:16:41,760 --> 05:16:48,560
homepage. So, we'll scroll up to the
6190
05:16:45,920 --> 05:16:53,200
source folder and we'll click this
6191
05:16:48,560 --> 05:16:55,760
folder and we'll create a new file.
6192
05:16:53,200 --> 05:16:58,560
Usually, we create a component for each
6193
05:16:55,760 --> 05:17:05,320
page of the website. So, let's name this
6194
05:16:58,560 --> 05:17:05,320
file capital H home capital Page.jsx.
6195
05:17:07,600 --> 05:17:13,200
Now, this website has multiple pages.
6196
05:17:10,638 --> 05:17:16,000
So, let's create a folder to group all
6197
05:17:13,200 --> 05:17:21,440
the pages together. We'll create a
6198
05:17:16,000 --> 05:17:23,360
folder and we'll name it pages.
6199
05:17:21,440 --> 05:17:27,320
And we're going to move the homepage
6200
05:17:23,360 --> 05:17:27,320
into the pages folder.
6201
05:17:28,240 --> 05:17:35,040
And now let's create a component for the
6202
05:17:30,878 --> 05:17:39,280
homepage using function. and we'll name
6203
05:17:35,040 --> 05:17:42,400
it capital H home capital P page
6204
05:17:39,280 --> 05:17:46,480
brackets and curly brackets and we're
6205
05:17:42,400 --> 05:17:48,878
going to return brackets
6206
05:17:46,480 --> 05:17:52,240
we're going to return the HTML for the
6207
05:17:48,878 --> 05:17:55,360
homepage which is right here. So we're
6208
05:17:52,240 --> 05:17:58,160
going to copy all of this HTML into our
6209
05:17:55,360 --> 05:18:00,080
new component. So, one trick we can do
6210
05:17:58,160 --> 05:18:03,440
is on the left, we're going to click
6211
05:18:00,080 --> 05:18:05,680
this arrow to collapse this element. And
6212
05:18:03,440 --> 05:18:08,160
we're going to click this arrow to
6213
05:18:05,680 --> 05:18:12,320
collapse this element as well. And now
6214
05:18:08,160 --> 05:18:15,120
we can select all of this HTML code
6215
05:18:12,320 --> 05:18:18,080
and cut.
6216
05:18:15,120 --> 05:18:21,600
And inside homepage.jsx,
6217
05:18:18,080 --> 05:18:24,638
we're going to paste that code.
6218
05:18:21,600 --> 05:18:27,040
And now we're going to scroll up and fix
6219
05:18:24,638 --> 05:18:31,040
all of these errors.
6220
05:18:27,040 --> 05:18:33,520
First, if we collapse this code
6221
05:18:31,040 --> 05:18:35,840
and collapse this element,
6222
05:18:33,520 --> 05:18:37,920
you can see we're returning two elements
6223
05:18:35,840 --> 05:18:40,080
from this component.
6224
05:18:37,920 --> 05:18:43,600
But remember, we can only return one
6225
05:18:40,080 --> 05:18:47,280
element from a component. To fix this,
6226
05:18:43,600 --> 05:18:50,320
we can wrap this in a fragment. So, at
6227
05:18:47,280 --> 05:18:53,360
the top, we'll create a fragment using
6228
05:18:50,320 --> 05:18:57,440
less than, greater than, and delete this
6229
05:18:53,360 --> 05:18:59,840
closing tag. And at the bottom, we'll
6230
05:18:57,440 --> 05:19:03,680
create the closing tag less than
6231
05:18:59,840 --> 05:19:06,718
slashgreater than. And let's also fix
6232
05:19:03,680 --> 05:19:10,160
the indenting here. We can select all of
6233
05:19:06,718 --> 05:19:12,160
this code and press tab a few times
6234
05:19:10,160 --> 05:19:14,958
until there's an indent before the
6235
05:19:12,160 --> 05:19:17,760
fragment. And for the first line, we're
6236
05:19:14,958 --> 05:19:20,958
going to remove an indent. And that will
6237
05:19:17,760 --> 05:19:24,160
fix the indenting.
6238
05:19:20,958 --> 05:19:26,958
Next, we'll fix these errors. We'll
6239
05:19:24,160 --> 05:19:29,920
expand these elements.
6240
05:19:26,958 --> 05:19:33,120
And remember in React, we have to use
6241
05:19:29,920 --> 05:19:35,120
class name instead of class. So to fix
6242
05:19:33,120 --> 05:19:38,718
these errors, we need to change all of
6243
05:19:35,120 --> 05:19:41,920
these to use class name. To do that, we
6244
05:19:38,718 --> 05:19:45,600
can press CtrlF on Windows or command F
6245
05:19:41,920 --> 05:19:48,320
on Mac to find some code. And we're
6246
05:19:45,600 --> 05:19:50,878
going to search for the code class
6247
05:19:48,320 --> 05:19:53,440
equals. And then we're going to click
6248
05:19:50,878 --> 05:19:56,798
this arrow on the left. And this will
6249
05:19:53,440 --> 05:19:58,560
open the replace menu. So this lets us
6250
05:19:56,798 --> 05:20:02,560
replace class equals with something
6251
05:19:58,560 --> 05:20:06,718
else. We'll replace it with class
6252
05:20:02,560 --> 05:20:10,718
capital N name equals and then press
6253
05:20:06,718 --> 05:20:12,958
this button to replace all.
6254
05:20:10,718 --> 05:20:16,638
And now you'll see that we fixed all of
6255
05:20:12,958 --> 05:20:19,360
the ESLint errors. Finally, we're going
6256
05:20:16,638 --> 05:20:21,760
to scroll to the top and we need to
6257
05:20:19,360 --> 05:20:25,440
export this component so we can use it
6258
05:20:21,760 --> 05:20:29,680
in other files. At the front, we'll type
6259
05:20:25,440 --> 05:20:31,920
export and save.
6260
05:20:29,680 --> 05:20:34,718
So, we moved all the HTML from the
6261
05:20:31,920 --> 05:20:37,600
homepage into our component and we fixed
6262
05:20:34,718 --> 05:20:40,638
all these errors. Now, let's use this
6263
05:20:37,600 --> 05:20:44,480
component in our app. We're going to go
6264
05:20:40,638 --> 05:20:48,718
to app.jsx JSX
6265
05:20:44,480 --> 05:20:51,040
and we'll remove this default HTML.
6266
05:20:48,718 --> 05:20:53,840
So we'll select it
6267
05:20:51,040 --> 05:20:57,360
and delete it
6268
05:20:53,840 --> 05:21:01,280
and we'll insert our homepage component.
6269
05:20:57,360 --> 05:21:06,480
First we need to import it.
6270
05:21:01,280 --> 05:21:09,920
at the top. We'll import curly brackets
6271
05:21:06,480 --> 05:21:13,840
the home page
6272
05:21:09,920 --> 05:21:17,360
from a string and we'll locate this
6273
05:21:13,840 --> 05:21:19,760
file. So, we're going to type dot slash
6274
05:21:17,360 --> 05:21:22,000
to start in the current folder which is
6275
05:21:19,760 --> 05:21:23,840
the source folder and we'll go into
6276
05:21:22,000 --> 05:21:25,680
pages
6277
05:21:23,840 --> 05:21:28,000
slashhome
6278
05:21:25,680 --> 05:21:30,240
page.
6279
05:21:28,000 --> 05:21:34,400
And now we'll insert the homepage into
6280
05:21:30,240 --> 05:21:36,320
our app. So here we'll type less than
6281
05:21:34,400 --> 05:21:39,920
homepage
6282
05:21:36,320 --> 05:21:43,840
slashgreater than. And next we'll fix
6283
05:21:39,920 --> 05:21:46,878
these eslint errors. The react logo and
6284
05:21:43,840 --> 05:21:49,920
vit logo are not used anymore. So we can
6285
05:21:46,878 --> 05:21:52,638
remove them.
6286
05:21:49,920 --> 05:21:57,040
Count and set count are also not used
6287
05:21:52,638 --> 05:21:59,920
anymore. So we can remove them.
6288
05:21:57,040 --> 05:22:02,638
and use state is not used anymore. We
6289
05:21:59,920 --> 05:22:05,520
can remove this as well
6290
05:22:02,638 --> 05:22:07,440
and save.
6291
05:22:05,520 --> 05:22:10,560
And now let's make sure that we saved
6292
05:22:07,440 --> 05:22:12,240
all of our other files. So we'll open
6293
05:22:10,560 --> 05:22:14,958
index.html
6294
05:22:12,240 --> 05:22:17,040
and we'll save it.
6295
05:22:14,958 --> 05:22:18,878
And let's open the starting code
6296
05:22:17,040 --> 05:22:22,080
index.html
6297
05:22:18,878 --> 05:22:23,680
and we'll save this file.
6298
05:22:22,080 --> 05:22:28,080
And now let's take a look at our
6299
05:22:23,680 --> 05:22:28,080
website. We'll open the browser.
6300
05:22:28,160 --> 05:22:34,080
And we can close GitHub for now. And
6301
05:22:30,958 --> 05:22:36,798
we'll open the e-commerce project.
6302
05:22:34,080 --> 05:22:39,440
And you'll see that we have the HTML and
6303
05:22:36,798 --> 05:22:40,958
the text for the e-commerce project
6304
05:22:39,440 --> 05:22:42,958
homepage.
6305
05:22:40,958 --> 05:22:48,000
Now, obviously, this doesn't look very
6306
05:22:42,958 --> 05:22:51,280
good. So, let's move over the CSS. Next,
6307
05:22:48,000 --> 05:22:53,840
to move the CSS into our React project,
6308
05:22:51,280 --> 05:22:55,840
we're going to go back to our code
6309
05:22:53,840 --> 05:22:58,958
and we're going to open the starting
6310
05:22:55,840 --> 05:23:02,320
code index.html.
6311
05:22:58,958 --> 05:23:05,840
And here we're going to scroll up and we
6312
05:23:02,320 --> 05:23:08,480
load three CSS files. So, we're going to
6313
05:23:05,840 --> 05:23:13,120
move these three files into our React
6314
05:23:08,480 --> 05:23:16,080
project. We'll start with general.css.
6315
05:23:13,120 --> 05:23:19,680
If we open styles/shared
6316
05:23:16,080 --> 05:23:22,680
which is here and shared and open
6317
05:23:19,680 --> 05:23:22,680
general.css
6318
05:23:23,600 --> 05:23:29,920
and this file contains CSS for the
6319
05:23:26,400 --> 05:23:34,160
overall website. CSS for the overall
6320
05:23:29,920 --> 05:23:35,840
website goes into the index.css file in
6321
05:23:34,160 --> 05:23:39,040
React.
6322
05:23:35,840 --> 05:23:41,680
So we'll click general.css CSS and we'll
6323
05:23:39,040 --> 05:23:45,840
press control A on Windows or command A
6324
05:23:41,680 --> 05:23:49,280
on Mac to select all of this code and
6325
05:23:45,840 --> 05:23:53,440
we'll copy
6326
05:23:49,280 --> 05:23:55,200
and index.css in the e-commerce project
6327
05:23:53,440 --> 05:23:57,520
we're going to remove all of this
6328
05:23:55,200 --> 05:24:00,320
default code
6329
05:23:57,520 --> 05:24:03,680
and paste
6330
05:24:00,320 --> 05:24:05,600
and save.
6331
05:24:03,680 --> 05:24:09,360
And now if we go back to the starting
6332
05:24:05,600 --> 05:24:13,280
code, we've moved over general.css and
6333
05:24:09,360 --> 05:24:14,958
we can delete this file.
6334
05:24:13,280 --> 05:24:18,520
And now we'll go back to the starting
6335
05:24:14,958 --> 05:24:18,520
code index.html.
6336
05:24:19,280 --> 05:24:26,638
And we can remove this line.
6337
05:24:22,560 --> 05:24:30,080
And we'll move header.css and index.css
6338
05:24:26,638 --> 05:24:32,638
into our React project. So both of these
6339
05:24:30,080 --> 05:24:34,958
style the homepage. So, we'll just move
6340
05:24:32,638 --> 05:24:36,560
these files beside our homepage
6341
05:24:34,958 --> 05:24:39,440
component.
6342
05:24:36,560 --> 05:24:41,120
First, we'll go into styles, shared, and
6343
05:24:39,440 --> 05:24:44,240
header.css,
6344
05:24:41,120 --> 05:24:49,080
and we're going to drag this file
6345
05:24:44,240 --> 05:24:49,080
into our pages folder.
6346
05:24:49,280 --> 05:24:54,320
So, we keep our CSS beside the component
6347
05:24:52,240 --> 05:24:57,200
that it styles.
6348
05:24:54,320 --> 05:24:59,600
And we'll scroll down and find
6349
05:24:57,200 --> 05:25:02,480
styles/pages.
6350
05:24:59,600 --> 05:25:06,200
And we'll move index.css CSS beside the
6351
05:25:02,480 --> 05:25:06,200
homepage as well.
6352
05:25:08,718 --> 05:25:15,040
So header.css styles the header at the
6353
05:25:11,680 --> 05:25:18,160
top of the page and index.css
6354
05:25:15,040 --> 05:25:20,480
styles the main part of the homepage.
6355
05:25:18,160 --> 05:25:23,520
Now a best practice is to use the same
6356
05:25:20,480 --> 05:25:26,480
name as the component here. So we'll
6357
05:25:23,520 --> 05:25:28,560
rightclick index.css CSS and we'll
6358
05:25:26,480 --> 05:25:34,760
rename it
6359
05:25:28,560 --> 05:25:34,760
capital H home capital P page.
6360
05:25:35,040 --> 05:25:42,878
Next, to load these styles, we need to
6361
05:25:38,240 --> 05:25:45,200
import them. We'll open homepage.jsx
6362
05:25:42,878 --> 05:25:47,840
and at the top,
6363
05:25:45,200 --> 05:25:52,718
we're going to import
6364
05:25:47,840 --> 05:25:55,840
a string and we'll import the file dot /
6365
05:25:52,718 --> 05:26:00,160
header.css. CSS
6366
05:25:55,840 --> 05:26:01,760
and we'll also import a string
6367
05:26:00,160 --> 05:26:04,760
slashhome
6368
05:26:01,760 --> 05:26:04,760
page.css
6369
05:26:04,798 --> 05:26:10,000
and save.
6370
05:26:07,680 --> 05:26:13,200
And now if we go back to the starting
6371
05:26:10,000 --> 05:26:16,560
code index.html,
6372
05:26:13,200 --> 05:26:19,360
we moved all of the HTML and CSS into
6373
05:26:16,560 --> 05:26:22,360
our project. So now we can delete this
6374
05:26:19,360 --> 05:26:22,360
file.
6375
05:26:23,280 --> 05:26:29,760
Finally, we'll open app.css.
6376
05:26:27,120 --> 05:26:32,320
And these styles were for the default
6377
05:26:29,760 --> 05:26:35,600
React website. So we don't need these
6378
05:26:32,320 --> 05:26:40,878
anymore. So we can select all of the CSS
6379
05:26:35,600 --> 05:26:42,958
and just remove it and save.
6380
05:26:40,878 --> 05:26:45,600
So make sure that we saved all of our
6381
05:26:42,958 --> 05:26:48,000
files. And now we're going to view the
6382
05:26:45,600 --> 05:26:50,480
website.
6383
05:26:48,000 --> 05:26:53,040
And now it's looking really good. We
6384
05:26:50,480 --> 05:26:55,600
have the structure and the styling that
6385
05:26:53,040 --> 05:26:59,040
we need. The only thing that's missing
6386
05:26:55,600 --> 05:27:01,280
are the images. So let's move the images
6387
05:26:59,040 --> 05:27:03,760
next.
6388
05:27:01,280 --> 05:27:08,240
To move the images, we'll go back to VS
6389
05:27:03,760 --> 05:27:11,680
Code and we'll go to the starting code
6390
05:27:08,240 --> 05:27:14,160
and find the images folder. So this
6391
05:27:11,680 --> 05:27:15,840
contains all the images that we need.
6392
05:27:14,160 --> 05:27:18,000
So, we're going to move this into the
6393
05:27:15,840 --> 05:27:21,120
React project.
6394
05:27:18,000 --> 05:27:24,638
Now, usually we put images in the assets
6395
05:27:21,120 --> 05:27:26,958
folder and then we import the images.
6396
05:27:24,638 --> 05:27:30,080
However, to save time, we're going to
6397
05:27:26,958 --> 05:27:33,040
move this folder into the public folder.
6398
05:27:30,080 --> 05:27:37,080
So, we're going to move it up here and
6399
05:27:33,040 --> 05:27:37,080
into the public folder.
6400
05:27:38,240 --> 05:27:42,638
The reason we move it into the public
6401
05:27:40,160 --> 05:27:45,840
folder is because if we look at
6402
05:27:42,638 --> 05:27:48,400
homepage.js, jsx and we look at an
6403
05:27:45,840 --> 05:27:51,760
image. Notice the source attribute
6404
05:27:48,400 --> 05:27:54,080
starts with images slash. When using
6405
05:27:51,760 --> 05:27:57,280
vit, if we start with a folder name like
6406
05:27:54,080 --> 05:28:00,480
this, vit will look for this inside the
6407
05:27:57,280 --> 05:28:02,718
public folder. So, it finds it here. So,
6408
05:28:00,480 --> 05:28:05,040
by moving our images into the public
6409
05:28:02,718 --> 05:28:07,520
folder, we don't have to change any of
6410
05:28:05,040 --> 05:28:10,958
this code and it just saves time in the
6411
05:28:07,520 --> 05:28:14,160
video. We can move these images into the
6412
05:28:10,958 --> 05:28:16,638
source/assets folder and import them
6413
05:28:14,160 --> 05:28:20,320
later as an exercise.
6414
05:28:16,638 --> 05:28:23,680
So now let's open our website
6415
05:28:20,320 --> 05:28:26,878
and all of the images are loaded. So
6416
05:28:23,680 --> 05:28:30,080
that's how we move an HTML website into
6417
05:28:26,878 --> 05:28:34,638
a React website. We just move over the
6418
05:28:30,080 --> 05:28:37,600
HTML, CSS, and images one by one.
6419
05:28:34,638 --> 05:28:40,878
Let's go back to VS Code and we'll save
6420
05:28:37,600 --> 05:28:44,798
our changes into Git. We'll open the Git
6421
05:28:40,878 --> 05:28:48,000
section. And here we moved all the HTML,
6422
05:28:44,798 --> 05:28:51,520
CSS, and images for the homepage. We're
6423
05:28:48,000 --> 05:28:56,718
going to type a message at the top. Move
6424
05:28:51,520 --> 05:28:59,718
the home page into React and press
6425
05:28:56,718 --> 05:28:59,718
commit.
6426
05:29:02,080 --> 05:29:07,280
Next, we're going to move the checkout
6427
05:29:04,080 --> 05:29:09,360
page to the React setup. We'll go to our
6428
05:29:07,280 --> 05:29:10,958
browser
6429
05:29:09,360 --> 05:29:14,558
and we're going to open the final
6430
05:29:10,958 --> 05:29:16,558
project at superimple.dev
6431
05:29:14,558 --> 05:29:19,200
and we're going to click the cart icon
6432
05:29:16,558 --> 05:29:21,360
in the top right.
6433
05:29:19,200 --> 05:29:24,320
This will open the checkout page where
6434
05:29:21,360 --> 05:29:27,520
we can see our cart and we can place an
6435
05:29:24,320 --> 05:29:30,480
order. So notice that this is a separate
6436
05:29:27,520 --> 05:29:34,600
page from the homepage and this page is
6437
05:29:30,480 --> 05:29:34,600
also on a different URL/checkout.
6438
05:29:34,958 --> 05:29:40,400
So how do we create multiple pages in
6439
05:29:37,760 --> 05:29:43,760
React? To do that, we're going to learn
6440
05:29:40,400 --> 05:29:48,718
a feature called routing. Routing lets
6441
05:29:43,760 --> 05:29:51,520
us create multiple pages in React.
6442
05:29:48,718 --> 05:29:54,878
Let's go back to our code
6443
05:29:51,520 --> 05:29:56,878
and we'll open our files here.
6444
05:29:54,878 --> 05:29:59,440
and we're going to scroll down first to
6445
05:29:56,878 --> 05:30:02,160
the starting code folder.
6446
05:29:59,440 --> 05:30:05,360
So, normally to create multiple pages,
6447
05:30:02,160 --> 05:30:09,200
we need to create multiple HTML files
6448
05:30:05,360 --> 05:30:12,080
like here. Each page has its own HTML
6449
05:30:09,200 --> 05:30:15,680
file like the checkout page and the
6450
05:30:12,080 --> 05:30:18,000
orders page. However, inside these
6451
05:30:15,680 --> 05:30:21,120
files, notice that there's a lot of
6452
05:30:18,000 --> 05:30:25,120
repeated code. So, each file has the doc
6453
05:30:21,120 --> 05:30:27,520
type. it has some meta and link elements
6454
05:30:25,120 --> 05:30:30,878
and in the orders.html
6455
05:30:27,520 --> 05:30:33,200
it has the same meta and link elements.
6456
05:30:30,878 --> 05:30:36,000
So this is not great because if we want
6457
05:30:33,200 --> 05:30:39,200
to change something on one page we have
6458
05:30:36,000 --> 05:30:42,558
to change all of the pages. On the other
6459
05:30:39,200 --> 05:30:46,798
hand, routing lets us create multiple
6460
05:30:42,558 --> 05:30:51,520
pages using one HTML file, and this lets
6461
05:30:46,798 --> 05:30:54,240
us reuse our HTML code. To use routing,
6462
05:30:51,520 --> 05:30:57,840
we're going to install an MPM package
6463
05:30:54,240 --> 05:31:01,120
called React Router. So, let's reopen
6464
05:30:57,840 --> 05:31:05,280
our terminal. You can click here and
6465
05:31:01,120 --> 05:31:10,760
drag up or press control J on Windows or
6466
05:31:05,280 --> 05:31:10,760
command J on Mac to open this terminal.
6467
05:31:10,798 --> 05:31:15,520
Now we currently have the Vit server
6468
05:31:12,958 --> 05:31:19,600
running. So we'll press control C on
6469
05:31:15,520 --> 05:31:22,000
Windows and Mac to stop the V server.
6470
05:31:19,600 --> 05:31:26,638
And to install React Router, we're going
6471
05:31:22,000 --> 05:31:29,520
to run the command npm install
6472
05:31:26,638 --> 05:31:32,958
react-rowout.
6473
05:31:29,520 --> 05:31:35,440
And we'll also type at. So for this
6474
05:31:32,958 --> 05:31:38,000
video, we'll use a specific version of
6475
05:31:35,440 --> 05:31:40,638
React Router just to make sure that it
6476
05:31:38,000 --> 05:31:43,440
works with the video. So click this link
6477
05:31:40,638 --> 05:31:45,200
in the description, go to lesson six,
6478
05:31:43,440 --> 05:31:47,440
and here you'll find the version of
6479
05:31:45,200 --> 05:31:50,638
React Router that we'll use, and you'll
6480
05:31:47,440 --> 05:31:52,400
also find the full command. So again,
6481
05:31:50,638 --> 05:31:55,120
you should always use the version that
6482
05:31:52,400 --> 05:31:58,160
you see on your page.
6483
05:31:55,120 --> 05:32:00,878
So select the version on your page and
6484
05:31:58,160 --> 05:32:03,920
copy it. And then in our command line,
6485
05:32:00,878 --> 05:32:08,080
we're going to paste that version
6486
05:32:03,920 --> 05:32:11,520
and press enter to install.
6487
05:32:08,080 --> 05:32:14,160
So now, how do we use React Router? Like
6488
05:32:11,520 --> 05:32:16,480
most packages, React Router has a
6489
05:32:14,160 --> 05:32:18,718
documentation page that tells you how to
6490
05:32:16,480 --> 05:32:21,280
use it. You can find the documentation
6491
05:32:18,718 --> 05:32:22,798
in the description, but for the video,
6492
05:32:21,280 --> 05:32:25,760
I'm going to show you the parts of the
6493
05:32:22,798 --> 05:32:28,958
documentation that we need. According to
6494
05:32:25,760 --> 05:32:31,440
the documentation, to use React Router,
6495
05:32:28,958 --> 05:32:33,680
we need to load it when we render the
6496
05:32:31,440 --> 05:32:35,680
app component.
6497
05:32:33,680 --> 05:32:41,240
So, in our project, we're going to
6498
05:32:35,680 --> 05:32:41,240
scroll up and open main.jsx,
6499
05:32:42,240 --> 05:32:47,680
and I'll close these other files so we
6500
05:32:44,638 --> 05:32:51,040
can see more of the code.
6501
05:32:47,680 --> 05:32:55,360
So, in this file, we set up React. We
6502
05:32:51,040 --> 05:32:57,600
render our app component on the page.
6503
05:32:55,360 --> 05:33:00,000
According to the documentation, we need
6504
05:32:57,600 --> 05:33:02,878
to wrap this in a component called
6505
05:33:00,000 --> 05:33:06,000
browser router.
6506
05:33:02,878 --> 05:33:09,200
So at the top first we're going to
6507
05:33:06,000 --> 05:33:11,680
import curly brackets
6508
05:33:09,200 --> 05:33:14,000
the browser
6509
05:33:11,680 --> 05:33:16,480
router
6510
05:33:14,000 --> 05:33:18,638
from a string
6511
05:33:16,480 --> 05:33:21,760
and we'll import it from the package
6512
05:33:18,638 --> 05:33:23,840
react router.
6513
05:33:21,760 --> 05:33:27,120
As a review, if we just start with a
6514
05:33:23,840 --> 05:33:29,120
name like this without dot slash, this
6515
05:33:27,120 --> 05:33:32,558
means it's going to load it from node
6516
05:33:29,120 --> 05:33:35,440
modules from the React router package.
6517
05:33:32,558 --> 05:33:39,200
And now around the app, we're going to
6518
05:33:35,440 --> 05:33:42,798
create less than browser
6519
05:33:39,200 --> 05:33:44,718
router greater than.
6520
05:33:42,798 --> 05:33:47,920
And we're going to put the app inside
6521
05:33:44,718 --> 05:33:51,920
here. So we'll cut this
6522
05:33:47,920 --> 05:33:55,040
and paste it in here.
6523
05:33:51,920 --> 05:33:58,000
Let's save.
6524
05:33:55,040 --> 05:34:00,718
Next, we need to do a bit more setup.
6525
05:33:58,000 --> 05:34:03,200
According to the documentation, inside
6526
05:34:00,718 --> 05:34:06,000
our app component, we need to add a
6527
05:34:03,200 --> 05:34:09,920
component called routes.
6528
05:34:06,000 --> 05:34:12,400
So, let's open app.jsx.
6529
05:34:09,920 --> 05:34:16,638
And at the top,
6530
05:34:12,400 --> 05:34:20,240
we're going to import curly brackets
6531
05:34:16,638 --> 05:34:23,440
the component routes
6532
05:34:20,240 --> 05:34:27,878
from a string. And we'll import this
6533
05:34:23,440 --> 05:34:27,878
from the package react-outer.
6534
05:34:28,240 --> 05:34:33,040
And inside this component, we're going
6535
05:34:30,080 --> 05:34:38,320
to insert the routes component using
6536
05:34:33,040 --> 05:34:41,200
less than routes greater than.
6537
05:34:38,320 --> 05:34:44,958
So the routes component tells React all
6538
05:34:41,200 --> 05:34:47,200
the pages that are in our website. So to
6539
05:34:44,958 --> 05:34:50,878
add a page to our website, we're going
6540
05:34:47,200 --> 05:34:54,400
to use another component called route. A
6541
05:34:50,878 --> 05:34:57,120
route is basically a page.
6542
05:34:54,400 --> 05:34:59,360
So first we need to import that at the
6543
05:34:57,120 --> 05:35:02,000
top. We're already importing from React
6544
05:34:59,360 --> 05:35:06,000
Router. So we're going to add a comma
6545
05:35:02,000 --> 05:35:08,958
and import the route component.
6546
05:35:06,000 --> 05:35:13,920
So inside routes, we're going to add
6547
05:35:08,958 --> 05:35:17,120
less than route greater than. So again,
6548
05:35:13,920 --> 05:35:18,638
a route basically adds a page to our
6549
05:35:17,120 --> 05:35:20,320
website.
6550
05:35:18,638 --> 05:35:24,320
And we're going to give this route two
6551
05:35:20,320 --> 05:35:27,600
props. The first prop is called path
6552
05:35:24,320 --> 05:35:30,878
equals double quotes. This tells React
6553
05:35:27,600 --> 05:35:34,480
the URL path of this page.
6554
05:35:30,878 --> 05:35:38,878
The URL path is the last part of the URL
6555
05:35:34,480 --> 05:35:41,520
like /checkout or slash orders. So first
6556
05:35:38,878 --> 05:35:45,360
let's add the homepage. For the
6557
05:35:41,520 --> 05:35:48,080
homepage, the URL path is empty.
6558
05:35:45,360 --> 05:35:51,120
To set the URL path as empty, we're
6559
05:35:48,080 --> 05:35:52,878
going to set it to slash.
6560
05:35:51,120 --> 05:35:57,440
And then we're going to give route a
6561
05:35:52,878 --> 05:36:00,798
second prop called element equals curly
6562
05:35:57,440 --> 05:36:04,160
brackets. So this tells React which
6563
05:36:00,798 --> 05:36:07,920
element or component to display when we
6564
05:36:04,160 --> 05:36:10,958
go to this URL path for the homepage. We
6565
05:36:07,920 --> 05:36:15,760
want to display this homepage component.
6566
05:36:10,958 --> 05:36:20,320
So let's select it and cut.
6567
05:36:15,760 --> 05:36:22,878
And inside here we're going to paste.
6568
05:36:20,320 --> 05:36:26,558
So this tells React that when we go to
6569
05:36:22,878 --> 05:36:30,000
the URL path slash or empty, it's going
6570
05:36:26,558 --> 05:36:33,040
to display our homepage. So let's try it
6571
05:36:30,000 --> 05:36:35,520
out. We'll save this file
6572
05:36:33,040 --> 05:36:39,360
and then inside the terminal, we're
6573
05:36:35,520 --> 05:36:42,360
going to start the VIT server using npm
6574
05:36:39,360 --> 05:36:42,360
rundev.
6575
05:36:42,480 --> 05:36:47,440
And we'll visit this URL.
6576
05:36:44,958 --> 05:36:49,200
I already have it open. So I'll open my
6577
05:36:47,440 --> 05:36:52,638
browser.
6578
05:36:49,200 --> 05:36:55,920
And now if the URL path ends with slash
6579
05:36:52,638 --> 05:36:58,240
or empty, it's going to display the
6580
05:36:55,920 --> 05:37:01,120
homepage.
6581
05:36:58,240 --> 05:37:05,520
Next, let's use React Router to add a
6582
05:37:01,120 --> 05:37:08,160
checkout page. We'll go back to our code
6583
05:37:05,520 --> 05:37:10,080
and to add another page to our website,
6584
05:37:08,160 --> 05:37:14,320
we're going to use the route component
6585
05:37:10,080 --> 05:37:16,558
again. So, we'll create less than route
6586
05:37:14,320 --> 05:37:20,958
greater than and we'll give it two
6587
05:37:16,558 --> 05:37:23,280
props. the path equals double quotes.
6588
05:37:20,958 --> 05:37:26,798
And for the checkout page, we'll set the
6589
05:37:23,280 --> 05:37:28,718
URL path to checkout.
6590
05:37:26,798 --> 05:37:32,400
This means this page will be displayed
6591
05:37:28,718 --> 05:37:35,360
when we go to /checkout.
6592
05:37:32,400 --> 05:37:38,320
And we'll give it another prop element
6593
05:37:35,360 --> 05:37:40,958
equals curly brackets.
6594
05:37:38,320 --> 05:37:43,680
And right now, we didn't create a
6595
05:37:40,958 --> 05:37:45,920
component for the checkout page yet. So
6596
05:37:43,680 --> 05:37:48,320
let's just create a div here just to
6597
05:37:45,920 --> 05:37:51,840
make sure that this is working. So we'll
6598
05:37:48,320 --> 05:37:58,718
create a div element and inside let's
6599
05:37:51,840 --> 05:38:02,160
type test checkout page and save.
6600
05:37:58,718 --> 05:38:05,120
So now if we go to our project
6601
05:38:02,160 --> 05:38:07,920
and in the URL path we're going to go to
6602
05:38:05,120 --> 05:38:10,480
slash checkout
6603
05:38:07,920 --> 05:38:12,878
and press enter.
6604
05:38:10,480 --> 05:38:14,798
It displays the div with the text that
6605
05:38:12,878 --> 05:38:17,360
we created.
6606
05:38:14,798 --> 05:38:20,480
So, as you can see, routing is actually
6607
05:38:17,360 --> 05:38:23,120
pretty simple. We just tell React which
6608
05:38:20,480 --> 05:38:27,958
pages we want in our website and then
6609
05:38:23,120 --> 05:38:27,958
what to display for each page.
6610
05:38:28,000 --> 05:38:33,760
Now, let's learn some more details about
6611
05:38:30,400 --> 05:38:37,280
routing. If we look at our code, we now
6612
05:38:33,760 --> 05:38:41,120
have multiple pages in our website. And
6613
05:38:37,280 --> 05:38:44,240
all of these pages use one HTML file
6614
05:38:41,120 --> 05:38:47,920
which is index.html.
6615
05:38:44,240 --> 05:38:51,840
So this HTML is shared between all of
6616
05:38:47,920 --> 05:38:55,200
our pages and this reduces repetition.
6617
05:38:51,840 --> 05:38:59,360
By the way, this is called a single page
6618
05:38:55,200 --> 05:39:03,200
application or an SPA. This means we
6619
05:38:59,360 --> 05:39:07,360
only have one HTML file and instead we
6620
05:39:03,200 --> 05:39:09,600
use React to create multiple pages.
6621
05:39:07,360 --> 05:39:13,200
Next, we'll use some shortcuts with
6622
05:39:09,600 --> 05:39:16,240
React Router. The first shortcut is
6623
05:39:13,200 --> 05:39:21,040
instead of path equals slash, React
6624
05:39:16,240 --> 05:39:23,200
Router has a prop for this called index.
6625
05:39:21,040 --> 05:39:26,958
So, this does the same thing as path
6626
05:39:23,200 --> 05:39:29,680
equals slash. The second shortcut is
6627
05:39:26,958 --> 05:39:32,240
currently we don't have anything inside
6628
05:39:29,680 --> 05:39:35,680
these route components. So, we can use
6629
05:39:32,240 --> 05:39:38,400
the self-closing element shortcut. We'll
6630
05:39:35,680 --> 05:39:40,400
remove the closing tag.
6631
05:39:38,400 --> 05:39:43,920
And at the end here, we'll add a space
6632
05:39:40,400 --> 05:39:46,320
and a slash. And same thing here, we'll
6633
05:39:43,920 --> 05:39:51,840
remove the closing tag.
6634
05:39:46,320 --> 05:39:54,080
And add a space and slash and save.
6635
05:39:51,840 --> 05:39:58,480
And now we'll save these changes into
6636
05:39:54,080 --> 05:40:01,120
Git. We'll open the git section.
6637
05:39:58,480 --> 05:40:02,798
And here it tells us we installed React
6638
05:40:01,120 --> 05:40:06,240
Router.
6639
05:40:02,798 --> 05:40:09,280
And in app.jsx jsx. We added some routes
6640
05:40:06,240 --> 05:40:11,040
or some pages to our website. In
6641
05:40:09,280 --> 05:40:14,160
main.jsx,
6642
05:40:11,040 --> 05:40:15,840
we loaded the React router. So, let's
6643
05:40:14,160 --> 05:40:20,160
create a message to describe these
6644
05:40:15,840 --> 05:40:25,360
changes. Create multiple
6645
05:40:20,160 --> 05:40:28,840
pages using React-Router
6646
05:40:25,360 --> 05:40:28,840
and commit.
6647
05:40:33,360 --> 05:40:39,600
Now, let's move the checkout page into
6648
05:40:35,760 --> 05:40:44,120
our app. We'll go back to our files
6649
05:40:39,600 --> 05:40:44,120
and first we'll open app.jsx.
6650
05:40:44,160 --> 05:40:50,798
And instead of displaying a div when we
6651
05:40:46,718 --> 05:40:52,958
go to / checkout, let's remove it.
6652
05:40:50,798 --> 05:40:56,160
And let's create a component for the
6653
05:40:52,958 --> 05:40:59,280
checkout page. Just like the homepage in
6654
05:40:56,160 --> 05:41:02,320
the pages folder, we'll click this and
6655
05:40:59,280 --> 05:41:06,558
we'll create a new file and we'll name
6656
05:41:02,320 --> 05:41:09,558
this capital C checkout capital P
6657
05:41:06,558 --> 05:41:09,558
page.jsx.
6658
05:41:11,040 --> 05:41:16,480
And inside here, we'll create a new
6659
05:41:13,200 --> 05:41:18,000
component using function
6660
05:41:16,480 --> 05:41:22,240
checkout
6661
05:41:18,000 --> 05:41:26,000
page brackets and curly brackets. and
6662
05:41:22,240 --> 05:41:28,400
we'll return brackets
6663
05:41:26,000 --> 05:41:31,520
and we'll move the HTML for the checkout
6664
05:41:28,400 --> 05:41:34,320
page in here. So, we'll go to the
6665
05:41:31,520 --> 05:41:38,000
starting code folder
6666
05:41:34,320 --> 05:41:40,160
and we'll open checkout.html.
6667
05:41:38,000 --> 05:41:42,240
So, this is the HTML file for the
6668
05:41:40,160 --> 05:41:44,718
checkout page.
6669
05:41:42,240 --> 05:41:46,558
We're going to scroll down to the body
6670
05:41:44,718 --> 05:41:49,200
element first and we're going to move
6671
05:41:46,558 --> 05:41:51,200
over this HTML.
6672
05:41:49,200 --> 05:41:53,920
So we'll click here to collapse this
6673
05:41:51,200 --> 05:41:56,878
code and click here to collapse this
6674
05:41:53,920 --> 05:42:01,200
code as well and we'll select all this
6675
05:41:56,878 --> 05:42:04,958
HTML and we'll cut
6676
05:42:01,200 --> 05:42:08,878
and then inside checkout page inside the
6677
05:42:04,958 --> 05:42:11,680
return we're going to paste
6678
05:42:08,878 --> 05:42:14,080
and let's scroll up and we'll fix these
6679
05:42:11,680 --> 05:42:16,240
errors.
6680
05:42:14,080 --> 05:42:18,878
So first if we collapse these two
6681
05:42:16,240 --> 05:42:20,718
elements remember we're returning two
6682
05:42:18,878 --> 05:42:23,520
elements from this component which
6683
05:42:20,718 --> 05:42:27,160
causes an error. To fix this we'll wrap
6684
05:42:23,520 --> 05:42:27,160
this in a fragment
6685
05:42:30,080 --> 05:42:35,520
and we'll fix the indenting. Now you can
6686
05:42:32,958 --> 05:42:38,320
fix it manually like we did before but
6687
05:42:35,520 --> 05:42:41,680
you can also use this shortcut. We can
6688
05:42:38,320 --> 05:42:44,878
press controlshiftp on Windows or
6689
05:42:41,680 --> 05:42:47,440
commandshiftp on Mac and this will open
6690
05:42:44,878 --> 05:42:49,760
up a list of commands that we can use in
6691
05:42:47,440 --> 05:42:54,000
VS Code. So we're going to search for
6692
05:42:49,760 --> 05:42:57,280
the command format document
6693
05:42:54,000 --> 05:42:59,440
and we can click it or press enter
6694
05:42:57,280 --> 05:43:01,440
and it will automatically format the
6695
05:42:59,440 --> 05:43:03,360
code in this document and fix the
6696
05:43:01,440 --> 05:43:06,718
indenting.
6697
05:43:03,360 --> 05:43:09,280
Next, if we expand these elements,
6698
05:43:06,718 --> 05:43:12,480
remember in React, we have to use class
6699
05:43:09,280 --> 05:43:15,600
name instead of class. To replace these,
6700
05:43:12,480 --> 05:43:19,200
we'll press control F or command F to
6701
05:43:15,600 --> 05:43:22,400
find code. And we'll find the code class
6702
05:43:19,200 --> 05:43:25,520
equals and click this arrow. And we're
6703
05:43:22,400 --> 05:43:30,080
going to replace it with class name
6704
05:43:25,520 --> 05:43:33,280
equals and click replace all.
6705
05:43:30,080 --> 05:43:35,520
And finally the last eslint error here
6706
05:43:33,280 --> 05:43:39,120
is this component is not used in this
6707
05:43:35,520 --> 05:43:41,680
file. So we need to export it
6708
05:43:39,120 --> 05:43:44,000
and save.
6709
05:43:41,680 --> 05:43:48,240
And now let's use this component in our
6710
05:43:44,000 --> 05:43:50,878
route. We'll open app.jsx JSX
6711
05:43:48,240 --> 05:43:56,558
and at the top we'll import the page
6712
05:43:50,878 --> 05:43:58,240
using import curly brackets the checkout
6713
05:43:56,558 --> 05:44:02,878
page
6714
05:43:58,240 --> 05:44:07,600
from a string and we'll import dot slash
6715
05:44:02,878 --> 05:44:11,200
pages slashcheckout page
6716
05:44:07,600 --> 05:44:16,558
and we'll use it inside this route using
6717
05:44:11,200 --> 05:44:19,520
less than checkout page slashgreater
6718
05:44:16,558 --> 05:44:22,718
and save.
6719
05:44:19,520 --> 05:44:25,760
And now let's check our website.
6720
05:44:22,718 --> 05:44:28,160
And if we go to the path /checkout, you
6721
05:44:25,760 --> 05:44:31,120
can see we have the HTML for the
6722
05:44:28,160 --> 05:44:33,360
checkout page.
6723
05:44:31,120 --> 05:44:36,000
Now, let's add the rest of the code for
6724
05:44:33,360 --> 05:44:37,680
the checkout page. We'll go back to VS
6725
05:44:36,000 --> 05:44:39,600
Code
6726
05:44:37,680 --> 05:44:44,040
and we're going to go to the starting
6727
05:44:39,600 --> 05:44:44,040
code and open checkout.html.
6728
05:44:44,080 --> 05:44:49,200
and we're going to move this over one by
6729
05:44:46,160 --> 05:44:52,718
one. So, first, what do we do for this
6730
05:44:49,200 --> 05:44:56,638
title? Right now, all of our pages are
6731
05:44:52,718 --> 05:44:58,638
sharing one HTML file here. So, all of
6732
05:44:56,638 --> 05:45:01,680
the pages have the same title,
6733
05:44:58,638 --> 05:45:04,718
e-commerce project. However, we want
6734
05:45:01,680 --> 05:45:07,440
each page to have a different title. So,
6735
05:45:04,718 --> 05:45:09,520
how do we do that? Well, in React, it's
6736
05:45:07,440 --> 05:45:11,840
actually pretty simple. We just have to
6737
05:45:09,520 --> 05:45:15,280
take this title and move it into our
6738
05:45:11,840 --> 05:45:17,440
page component. So, we'll select this
6739
05:45:15,280 --> 05:45:19,600
and cut.
6740
05:45:17,440 --> 05:45:22,638
And then we'll scroll up and open
6741
05:45:19,600 --> 05:45:24,638
checkout page.jsx.
6742
05:45:22,638 --> 05:45:27,920
And if we want this page to have a
6743
05:45:24,638 --> 05:45:31,200
different title, all we have to do is to
6744
05:45:27,920 --> 05:45:33,760
paste the title at the top
6745
05:45:31,200 --> 05:45:36,240
and save.
6746
05:45:33,760 --> 05:45:38,480
So, if we look at our website
6747
05:45:36,240 --> 05:45:41,440
now, the checkout page has the title
6748
05:45:38,480 --> 05:45:43,040
checkout in the tabs. So, we'll go back
6749
05:45:41,440 --> 05:45:45,040
to our code.
6750
05:45:43,040 --> 05:45:47,360
And we're also going to set the title in
6751
05:45:45,040 --> 05:45:49,520
the homepage. So we'll open the
6752
05:45:47,360 --> 05:45:52,878
homepage.
6753
05:45:49,520 --> 05:45:55,920
And in the homepage,
6754
05:45:52,878 --> 05:45:58,000
let's create a title.
6755
05:45:55,920 --> 05:46:00,320
And we're going to set the title to
6756
05:45:58,000 --> 05:46:04,558
ecommerce
6757
05:46:00,320 --> 05:46:07,600
project and save.
6758
05:46:04,558 --> 05:46:10,558
And now we can scroll down
6759
05:46:07,600 --> 05:46:12,878
to the index.html HTML
6760
05:46:10,558 --> 05:46:16,160
and we can remove this title because
6761
05:46:12,878 --> 05:46:19,120
we're going to set it using React
6762
05:46:16,160 --> 05:46:21,840
and save.
6763
05:46:19,120 --> 05:46:24,558
And now let's move the rest of the HTML
6764
05:46:21,840 --> 05:46:27,920
in the checkout page.
6765
05:46:24,558 --> 05:46:30,958
If we scroll down here, these meta and
6766
05:46:27,920 --> 05:46:33,120
link elements have already been moved to
6767
05:46:30,958 --> 05:46:35,680
index.html.
6768
05:46:33,120 --> 05:46:38,798
So this is being shared across all of
6769
05:46:35,680 --> 05:46:41,040
our pages. So, we actually don't need to
6770
05:46:38,798 --> 05:46:43,680
move this over anymore because it's
6771
05:46:41,040 --> 05:46:46,160
shared. So, we can remove all of this
6772
05:46:43,680 --> 05:46:47,920
code.
6773
05:46:46,160 --> 05:46:51,040
And finally, we're going to move over
6774
05:46:47,920 --> 05:46:53,360
the CSS for this page
6775
05:46:51,040 --> 05:46:55,440
for general.css.
6776
05:46:53,360 --> 05:46:58,638
Remember that we already copied these
6777
05:46:55,440 --> 05:47:03,000
styles into index.css,
6778
05:46:58,638 --> 05:47:03,000
so we don't need to move this anymore.
6779
05:47:03,280 --> 05:47:08,558
And now we'll just move checkout
6780
05:47:04,878 --> 05:47:11,680
header.css CSS and checkout.css into our
6781
05:47:08,558 --> 05:47:15,480
React project.
6782
05:47:11,680 --> 05:47:15,480
So, we'll go to styles/pages/checkout
6783
05:47:16,240 --> 05:47:24,760
and we'll move checkout header.css
6784
05:47:19,120 --> 05:47:24,760
into the React project into here
6785
05:47:24,878 --> 05:47:30,798
and scroll down. And we'll also move
6786
05:47:27,440 --> 05:47:34,520
checkout.css CSS into our React project
6787
05:47:30,798 --> 05:47:34,520
beside the component.
6788
05:47:35,360 --> 05:47:40,798
And now that we moved everything over,
6789
05:47:37,360 --> 05:47:45,080
we can delete the checkout.html file.
6790
05:47:40,798 --> 05:47:45,080
So, we can delete this file.
6791
05:47:46,000 --> 05:47:51,520
Finally, let's scroll up
6792
05:47:48,798 --> 05:47:54,320
and we're going to rename the CSS file
6793
05:47:51,520 --> 05:47:58,000
to match the component name. So, we'll
6794
05:47:54,320 --> 05:48:02,280
rightclick and rename.
6795
05:47:58,000 --> 05:48:02,280
checkout page.css.
6796
05:48:03,600 --> 05:48:10,480
And now we'll load these two CSS files
6797
05:48:06,240 --> 05:48:15,760
using import. So in checkout page.jsx
6798
05:48:10,480 --> 05:48:19,680
at the top, we'll import a string and
6799
05:48:15,760 --> 05:48:21,440
we'll import the CSS file slash
6800
05:48:19,680 --> 05:48:24,440
checkout-
6801
05:48:21,440 --> 05:48:24,440
header.css.
6802
05:48:25,040 --> 05:48:30,718
And we'll also import
6803
05:48:28,240 --> 05:48:33,440
dot slashcheckout
6804
05:48:30,718 --> 05:48:36,558
page.css
6805
05:48:33,440 --> 05:48:39,520
and save.
6806
05:48:36,558 --> 05:48:41,920
We'll go to our website.
6807
05:48:39,520 --> 05:48:45,200
And now everything looks really good. So
6808
05:48:41,920 --> 05:48:47,680
we finished moving all the HTML and CSS
6809
05:48:45,200 --> 05:48:50,000
for the checkout page into our React
6810
05:48:47,680 --> 05:48:52,958
project.
6811
05:48:50,000 --> 05:48:55,840
Let's go back to our code and we'll save
6812
05:48:52,958 --> 05:48:58,000
these changes into git. We'll open the
6813
05:48:55,840 --> 05:49:00,160
git section
6814
05:48:58,000 --> 05:49:02,638
and in app.jsx
6815
05:49:00,160 --> 05:49:04,320
we use the checkout page component for
6816
05:49:02,638 --> 05:49:07,520
/checkout
6817
05:49:04,320 --> 05:49:11,280
and then we moved over the CSS files and
6818
05:49:07,520 --> 05:49:14,080
in here we moved over the HTML code as
6819
05:49:11,280 --> 05:49:16,400
well as the title. Let's type a message
6820
05:49:14,080 --> 05:49:22,160
to describe these changes.
6821
05:49:16,400 --> 05:49:25,160
move checkout page into React and
6822
05:49:22,160 --> 05:49:25,160
commit.
6823
05:49:28,480 --> 05:49:34,080
Before we continue moving pages, there's
6824
05:49:30,958 --> 05:49:39,600
an issue that we need to fix. If we open
6825
05:49:34,080 --> 05:49:41,840
the website and click the top left icon,
6826
05:49:39,600 --> 05:49:44,958
normally this should take us to the
6827
05:49:41,840 --> 05:49:48,558
homepage, but now it's blank. So why is
6828
05:49:44,958 --> 05:49:52,360
this not working? Notice in the URL the
6829
05:49:48,558 --> 05:49:52,360
path is /index.html.
6830
05:49:52,400 --> 05:49:59,360
Before this website was split up into
6831
05:49:54,958 --> 05:50:01,760
multiple HTML files and index.html
6832
05:49:59,360 --> 05:50:04,480
created the homepage.
6833
05:50:01,760 --> 05:50:08,160
But now we're using routing. So we set
6834
05:50:04,480 --> 05:50:10,320
the URL paths ourselves. In our project
6835
05:50:08,160 --> 05:50:14,240
we set the path slash to be the
6836
05:50:10,320 --> 05:50:16,798
homepage. and we set the path /checkout
6837
05:50:14,240 --> 05:50:19,120
to be the checkout page. So, we need to
6838
05:50:16,798 --> 05:50:22,878
go into our code and update all of the
6839
05:50:19,120 --> 05:50:25,760
links to go to the correct URL path.
6840
05:50:22,878 --> 05:50:28,638
We'll go back to VS Code.
6841
05:50:25,760 --> 05:50:31,120
And to update all of our links, we're
6842
05:50:28,638 --> 05:50:35,040
going to go to the search section of VS
6843
05:50:31,120 --> 05:50:39,360
Code. We can press control shift F on
6844
05:50:35,040 --> 05:50:41,920
Windows or commandshift F on Mac.
6845
05:50:39,360 --> 05:50:45,200
And here we can search inside all of our
6846
05:50:41,920 --> 05:50:47,920
files. First, let's search for the code
6847
05:50:45,200 --> 05:50:50,638
index.html.
6848
05:50:47,920 --> 05:50:53,120
This will give us a list of a elements
6849
05:50:50,638 --> 05:50:55,920
or link elements.
6850
05:50:53,120 --> 05:51:00,160
And href means where these links should
6851
05:50:55,920 --> 05:51:03,680
go. So now instead of index.html,
6852
05:51:00,160 --> 05:51:06,400
these links should go to slash. To
6853
05:51:03,680 --> 05:51:08,958
replace index.html HTML in all these
6854
05:51:06,400 --> 05:51:12,878
places. We can click this arrow on the
6855
05:51:08,958 --> 05:51:16,240
left to open the replace menu. And we'll
6856
05:51:12,878 --> 05:51:21,840
replace this with slash. And then click
6857
05:51:16,240 --> 05:51:24,000
here to replace all. And confirm.
6858
05:51:21,840 --> 05:51:27,520
And let's replace the links for the
6859
05:51:24,000 --> 05:51:31,680
other pages as well. So up here, we'll
6860
05:51:27,520 --> 05:51:34,000
also search for checkout.html.
6861
05:51:31,680 --> 05:51:35,680
And in this search result, we have a
6862
05:51:34,000 --> 05:51:37,760
link element with the href
6863
05:51:35,680 --> 05:51:40,320
checkout.html.
6864
05:51:37,760 --> 05:51:43,760
And in our router, we set the URL path
6865
05:51:40,320 --> 05:51:46,080
for the checkout page to /checkout.
6866
05:51:43,760 --> 05:51:48,480
So we'll replace all of these with
6867
05:51:46,080 --> 05:51:52,360
slashcheckout
6868
05:51:48,480 --> 05:51:52,360
and replace all.
6869
05:51:52,638 --> 05:51:59,120
And we'll do the same thing for the
6870
05:51:54,160 --> 05:52:03,520
orders page, which is orders.html.
6871
05:51:59,120 --> 05:52:06,920
And here we'll change it to slash orders
6872
05:52:03,520 --> 05:52:06,920
and replace.
6873
05:52:08,320 --> 05:52:15,040
And finally, we also have a tracking
6874
05:52:10,798 --> 05:52:17,440
page. So here we're going to search for
6875
05:52:15,040 --> 05:52:21,520
tracking.html
6876
05:52:17,440 --> 05:52:25,000
and replace it with slash tracking and
6877
05:52:21,520 --> 05:52:25,000
replace all.
6878
05:52:25,840 --> 05:52:31,040
And now all of our links should work
6879
05:52:27,760 --> 05:52:33,440
correctly because they match our router.
6880
05:52:31,040 --> 05:52:37,280
If we open the website
6881
05:52:33,440 --> 05:52:40,280
and at the top we change the URL path to
6882
05:52:37,280 --> 05:52:40,280
/checkout
6883
05:52:40,958 --> 05:52:46,480
it opens the checkout page. If we click
6884
05:52:43,920 --> 05:52:48,798
the top left icon
6885
05:52:46,480 --> 05:52:52,798
this should now go to the URL path slash
6886
05:52:48,798 --> 05:52:55,760
or empty and it will open the homepage.
6887
05:52:52,798 --> 05:52:58,400
So let's save this into Git. We'll open
6888
05:52:55,760 --> 05:53:00,638
the git section
6889
05:52:58,400 --> 05:53:04,080
and we just updated all the links in
6890
05:53:00,638 --> 05:53:08,878
these files to match the router.
6891
05:53:04,080 --> 05:53:15,638
Let's type the message update links to
6892
05:53:08,878 --> 05:53:15,638
match the router and commit.
6893
05:53:18,480 --> 05:53:23,920
Next, let's move the orders page into
6894
05:53:21,120 --> 05:53:25,920
our React project. Feel free to pause
6895
05:53:23,920 --> 05:53:30,080
the video if you want to try it yourself
6896
05:53:25,920 --> 05:53:32,638
first. So, we'll go back to our files
6897
05:53:30,080 --> 05:53:35,600
and first let's create a component for
6898
05:53:32,638 --> 05:53:38,638
the orders page. Inside the pages
6899
05:53:35,600 --> 05:53:43,840
folder, we'll create a new file and
6900
05:53:38,638 --> 05:53:46,400
we'll name it orders page.jsx
6901
05:53:43,840 --> 05:53:49,680
and press enter.
6902
05:53:46,400 --> 05:53:51,200
And inside here we'll create a component
6903
05:53:49,680 --> 05:53:54,958
function
6904
05:53:51,200 --> 05:53:58,638
orders page brackets and curly brackets.
6905
05:53:54,958 --> 05:54:01,840
And we'll return brackets
6906
05:53:58,638 --> 05:54:04,718
and we'll move over the HTML. So we'll
6907
05:54:01,840 --> 05:54:08,200
go back to the starting code folder and
6908
05:54:04,718 --> 05:54:08,200
open orders.html.
6909
05:54:08,480 --> 05:54:13,200
We'll start with the body. So we'll
6910
05:54:10,240 --> 05:54:17,040
scroll down and we can collapse these
6911
05:54:13,200 --> 05:54:19,440
two elements and we'll select the HTML
6912
05:54:17,040 --> 05:54:22,718
and cut
6913
05:54:19,440 --> 05:54:25,840
and inside the orders page inside the
6914
05:54:22,718 --> 05:54:29,480
return we'll paste.
6915
05:54:25,840 --> 05:54:29,480
We'll scroll up
6916
05:54:30,000 --> 05:54:34,400
and collapse these elements
6917
05:54:32,958 --> 05:54:37,760
and we're going to wrap these in a
6918
05:54:34,400 --> 05:54:41,120
fragment less than greater than and
6919
05:54:37,760 --> 05:54:43,600
remove this. and at the end less than
6920
05:54:41,120 --> 05:54:45,680
slashgreater than.
6921
05:54:43,600 --> 05:54:50,080
And to fix the indenting, we can use the
6922
05:54:45,680 --> 05:54:52,958
shortcut control shiftp or commandshiftp
6923
05:54:50,080 --> 05:54:56,958
and search for the command format
6924
05:54:52,958 --> 05:54:59,520
document and press enter.
6925
05:54:56,958 --> 05:55:02,000
Again, we need to rename class to class
6926
05:54:59,520 --> 05:55:04,638
name. So, we'll press control F or
6927
05:55:02,000 --> 05:55:07,760
command F to find code. We'll search for
6928
05:55:04,638 --> 05:55:12,638
class equals. Click here and replace it
6929
05:55:07,760 --> 05:55:15,440
with class name and replace all.
6930
05:55:12,638 --> 05:55:17,680
And finally, at the top, we'll export
6931
05:55:15,440 --> 05:55:20,638
this component.
6932
05:55:17,680 --> 05:55:25,480
Let's save this file.
6933
05:55:20,638 --> 05:55:25,480
Let's go back to orders.html.
6934
05:55:25,680 --> 05:55:30,718
Scroll up and we'll move over this code.
6935
05:55:29,120 --> 05:55:34,160
So, for the title, we're going to move
6936
05:55:30,718 --> 05:55:39,760
it into the component. We'll select it,
6937
05:55:34,160 --> 05:55:43,760
cut, and at the top in orders page,
6938
05:55:39,760 --> 05:55:47,558
we're going to paste it at the top.
6939
05:55:43,760 --> 05:55:47,558
Let's go back to orders.html.
6940
05:55:48,320 --> 05:55:53,760
And these meta and link elements are
6941
05:55:50,718 --> 05:55:57,280
already inside index.html.
6942
05:55:53,760 --> 05:55:59,760
So, we can remove this.
6943
05:55:57,280 --> 05:56:02,958
And finally, we'll move the CSS files
6944
05:55:59,760 --> 05:56:05,760
over. So, general.css CSS was already
6945
05:56:02,958 --> 05:56:09,040
moved into index.css.
6946
05:56:05,760 --> 05:56:10,958
We already moved over header.css up
6947
05:56:09,040 --> 05:56:12,958
here. So, we just need to move the
6948
05:56:10,958 --> 05:56:15,840
orders.css.
6949
05:56:12,958 --> 05:56:17,920
So, inside starting code, we'll go into
6950
05:56:15,840 --> 05:56:23,240
styles/pages
6951
05:56:17,920 --> 05:56:23,240
and we'll move this orders.css file.
6952
05:56:23,600 --> 05:56:30,958
So, we'll move it up here
6953
05:56:27,440 --> 05:56:33,520
beside the orders page component.
6954
05:56:30,958 --> 05:56:35,440
And now we moved over everything inside
6955
05:56:33,520 --> 05:56:38,320
orders.html.
6956
05:56:35,440 --> 05:56:41,320
So we can scroll down and delete this
6957
05:56:38,320 --> 05:56:41,320
file.
6958
05:56:42,400 --> 05:56:48,638
Next, we should rename orders.css
6959
05:56:45,680 --> 05:56:52,878
to match the component name. So we'll
6960
05:56:48,638 --> 05:56:57,320
rightclick and rename
6961
05:56:52,878 --> 05:56:57,320
orders page.css.
6962
05:56:57,600 --> 05:57:03,600
And finally, we'll load the CSS files.
6963
05:57:00,798 --> 05:57:07,440
So, in orders page.jsx
6964
05:57:03,600 --> 05:57:10,400
at the top, we're going to import a
6965
05:57:07,440 --> 05:57:14,000
string. And remember, this requires the
6966
05:57:10,400 --> 05:57:16,240
header and the orders page CSS.
6967
05:57:14,000 --> 05:57:19,240
So, first we'll import dot slash
6968
05:57:16,240 --> 05:57:19,240
header.css.
6969
05:57:19,360 --> 05:57:30,480
And we'll also import
6970
05:57:22,638 --> 05:57:33,760
dot slash orders page.css. CSS and save.
6971
05:57:30,480 --> 05:57:36,798
And now we moved over all the HTML and
6972
05:57:33,760 --> 05:57:38,878
CSS for the orders page.
6973
05:57:36,798 --> 05:57:43,718
So now let's create a route for this
6974
05:57:38,878 --> 05:57:43,718
page. We'll go to app.jsx.
6975
05:57:43,920 --> 05:57:53,040
And first at the top we're going to
6976
05:57:46,558 --> 05:57:56,400
import curly brackets the orders page
6977
05:57:53,040 --> 05:58:01,360
from a string. and we'll locate dot
6978
05:57:56,400 --> 05:58:03,520
slashpages slash orders page and then
6979
05:58:01,360 --> 05:58:07,840
down here we're going to add another
6980
05:58:03,520 --> 05:58:11,440
route. So at the end we'll add less than
6981
05:58:07,840 --> 05:58:12,958
route greater than and we can remove
6982
05:58:11,440 --> 05:58:15,200
this
6983
05:58:12,958 --> 05:58:17,440
and use the self-closing element
6984
05:58:15,200 --> 05:58:19,840
shortcut
6985
05:58:17,440 --> 05:58:23,520
and we'll give it two props. We'll set
6986
05:58:19,840 --> 05:58:26,558
the path equals double quotes and we'll
6987
05:58:23,520 --> 05:58:28,320
put the orders page on the path slash
6988
05:58:26,558 --> 05:58:31,600
orders
6989
05:58:28,320 --> 05:58:34,240
and we'll give it an element to display
6990
05:58:31,600 --> 05:58:36,878
equals curly brackets. And for the
6991
05:58:34,240 --> 05:58:42,798
orders page, we'll display the element
6992
05:58:36,878 --> 05:58:45,920
less than orders page slashgrater than
6993
05:58:42,798 --> 05:58:48,798
and save.
6994
05:58:45,920 --> 05:58:51,600
And now let's view the website. We'll
6995
05:58:48,798 --> 05:58:53,680
open the website
6996
05:58:51,600 --> 05:58:58,558
and on the homepage we're going to click
6997
05:58:53,680 --> 05:59:01,440
this orders link to go to slash orders
6998
05:58:58,558 --> 05:59:03,520
and it displays the orders page. So that
6999
05:59:01,440 --> 05:59:07,440
looks pretty good. And now we finished
7000
05:59:03,520 --> 05:59:10,958
moving the orders page into React.
7001
05:59:07,440 --> 05:59:14,080
So let's go back to VS Code.
7002
05:59:10,958 --> 05:59:16,480
And in the starting code, there's one
7003
05:59:14,080 --> 05:59:19,120
more page to move over, which is the
7004
05:59:16,480 --> 05:59:20,638
tracking page. But I'll leave this as an
7005
05:59:19,120 --> 05:59:22,480
exercise.
7006
05:59:20,638 --> 05:59:26,240
So now let's save these changes into
7007
05:59:22,480 --> 05:59:29,520
Git. Open the Git section.
7008
05:59:26,240 --> 05:59:32,878
In app.jsx, we added a new route for the
7009
05:59:29,520 --> 05:59:35,040
orders page and we moved over the CSS
7010
05:59:32,878 --> 05:59:39,360
and the HTML.
7011
05:59:35,040 --> 05:59:42,958
So we'll type the message move orders
7012
05:59:39,360 --> 05:59:46,520
page to React
7013
05:59:42,958 --> 05:59:46,520
and commit.
7014
05:59:50,000 --> 05:59:54,480
Next, we're going to separate our page
7015
05:59:52,080 --> 05:59:58,320
into smaller components.
7016
05:59:54,480 --> 06:00:00,798
For example, if we look at our website,
7017
05:59:58,320 --> 06:00:03,680
notice that on the orders page, there's
7018
06:00:00,798 --> 06:00:06,958
this header at the top. And if we click
7019
06:00:03,680 --> 06:00:09,200
the top left and go to the homepage, we
7020
06:00:06,958 --> 06:00:12,080
have the same header at the top of the
7021
06:00:09,200 --> 06:00:14,958
page. So, it's a good idea to separate
7022
06:00:12,080 --> 06:00:18,320
the header into a component so we can
7023
06:00:14,958 --> 06:00:21,040
reuse it between these pages. We'll go
7024
06:00:18,320 --> 06:00:24,080
back to VS Code
7025
06:00:21,040 --> 06:00:27,878
and we'll open our files and we're going
7026
06:00:24,080 --> 06:00:27,878
to open homepage.jsx.
7027
06:00:28,160 --> 06:00:34,160
And at the top here, we have the HTML
7028
06:00:30,958 --> 06:00:37,520
for the header. If we collapse it, this
7029
06:00:34,160 --> 06:00:39,760
is all the HTML for the header.
7030
06:00:37,520 --> 06:00:41,360
Let's separate this into a shared
7031
06:00:39,760 --> 06:00:43,520
component.
7032
06:00:41,360 --> 06:00:46,480
Now, because this component is shared
7033
06:00:43,520 --> 06:00:48,958
between multiple pages, we usually put
7034
06:00:46,480 --> 06:00:51,520
shared components in a folder called
7035
06:00:48,958 --> 06:00:53,760
components. So, inside our source
7036
06:00:51,520 --> 06:00:56,000
folder, we're going to click it and
7037
06:00:53,760 --> 06:01:00,600
create a new folder
7038
06:00:56,000 --> 06:01:00,600
and we'll call it components.
7039
06:01:01,280 --> 06:01:06,958
So the components folder is for shared
7040
06:01:03,760 --> 06:01:10,718
components and the pages folder is for
7041
06:01:06,958 --> 06:01:12,798
components that are specific to a page.
7042
06:01:10,718 --> 06:01:15,360
Inside the components folder, we'll
7043
06:01:12,798 --> 06:01:19,638
create a new file.
7044
06:01:15,360 --> 06:01:19,638
And let's name it header.jsx.
7045
06:01:21,120 --> 06:01:26,878
And in here we'll create a component for
7046
06:01:23,440 --> 06:01:30,718
the header using function
7047
06:01:26,878 --> 06:01:34,320
header brackets and curly brackets. And
7048
06:01:30,718 --> 06:01:37,040
we'll return brackets.
7049
06:01:34,320 --> 06:01:41,040
And we'll move over the HTML.
7050
06:01:37,040 --> 06:01:43,440
So we'll open homepage.jsx JSX
7051
06:01:41,040 --> 06:01:48,280
and we're going to select the HTML for
7052
06:01:43,440 --> 06:01:48,280
the header and cut
7053
06:01:50,160 --> 06:01:56,638
inside header.jsx.
7054
06:01:52,798 --> 06:01:58,718
Inside here, we're going to paste.
7055
06:01:56,638 --> 06:02:02,480
And we'll fix the indenting by pressing
7056
06:01:58,718 --> 06:02:05,840
control shiftp or command shiftp and
7057
06:02:02,480 --> 06:02:08,638
search for format document and press
7058
06:02:05,840 --> 06:02:12,160
enter.
7059
06:02:08,638 --> 06:02:14,638
And we'll scroll to the top. And lastly,
7060
06:02:12,160 --> 06:02:18,080
we're going to export this component to
7061
06:02:14,638 --> 06:02:20,878
remove this error.
7062
06:02:18,080 --> 06:02:24,000
Next, we should also move over the CSS
7063
06:02:20,878 --> 06:02:26,400
for the header. Now, luckily the CSS for
7064
06:02:24,000 --> 06:02:28,558
the header has already been separated
7065
06:02:26,400 --> 06:02:31,280
into header.css.
7066
06:02:28,558 --> 06:02:34,520
So, we can just move this over to beside
7067
06:02:31,280 --> 06:02:34,520
the component.
7068
06:02:36,480 --> 06:02:42,000
So, we want to keep the CSS close to the
7069
06:02:39,040 --> 06:02:45,440
component so it's easy to find. So at
7070
06:02:42,000 --> 06:02:49,600
the top of this we can import the CSS
7071
06:02:45,440 --> 06:02:53,360
using import a string and we'll import
7072
06:02:49,600 --> 06:02:56,320
dot slash header.css
7073
06:02:53,360 --> 06:02:58,320
and save.
7074
06:02:56,320 --> 06:03:00,878
So now that we created this header
7075
06:02:58,320 --> 06:03:04,400
component, we can reuse it in the
7076
06:03:00,878 --> 06:03:06,638
homepage and the orders page. We'll open
7077
06:03:04,400 --> 06:03:09,280
homepage.jsx.
7078
06:03:06,638 --> 06:03:11,280
And at the top first, we don't need to
7079
06:03:09,280 --> 06:03:13,920
import the header anymore. It's already
7080
06:03:11,280 --> 06:03:17,920
done by this component. So we can remove
7081
06:03:13,920 --> 06:03:23,280
it. And let's import the component using
7082
06:03:17,920 --> 06:03:25,680
import curly brackets. The header
7083
06:03:23,280 --> 06:03:28,718
from a string.
7084
06:03:25,680 --> 06:03:31,040
And now we need to locate this file. So
7085
06:03:28,718 --> 06:03:33,040
currently we're inside the pages folder.
7086
06:03:31,040 --> 06:03:36,080
So we need to get out of this folder
7087
06:03:33,040 --> 06:03:39,840
using dot dot slash and then go into the
7088
06:03:36,080 --> 06:03:41,520
components folder. components slash
7089
06:03:39,840 --> 06:03:43,840
header.
7090
06:03:41,520 --> 06:03:47,120
And now we'll use this component in the
7091
06:03:43,840 --> 06:03:50,400
HTML. So down here
7092
06:03:47,120 --> 06:03:55,840
we'll insert less than header
7093
06:03:50,400 --> 06:03:58,558
slashgreater than and save.
7094
06:03:55,840 --> 06:04:01,200
So now this HTML is a bit easier to work
7095
06:03:58,558 --> 06:04:03,920
with because we split it up into smaller
7096
06:04:01,200 --> 06:04:07,040
pieces. Let's do the same thing with the
7097
06:04:03,920 --> 06:04:08,798
orders page. We'll open the orders page.
7098
06:04:07,040 --> 06:04:10,878
jsx
7099
06:04:08,798 --> 06:04:14,718
and at the top we don't need to import
7100
06:04:10,878 --> 06:04:19,360
header.css CSS and instead we'll import
7101
06:04:14,718 --> 06:04:23,680
curly brackets the header component from
7102
06:04:19,360 --> 06:04:25,200
a string and we'll locate dot dot slash
7103
06:04:23,680 --> 06:04:27,840
components
7104
06:04:25,200 --> 06:04:30,958
slash header
7105
06:04:27,840 --> 06:04:33,760
and down here instead of this HTML we
7106
06:04:30,958 --> 06:04:38,320
can remove this and just use the
7107
06:04:33,760 --> 06:04:41,680
component less than header slashgrater
7108
06:04:38,320 --> 06:04:45,360
than this helps us reuse and save a lot
7109
06:04:41,680 --> 06:04:48,558
of code. Let's save
7110
06:04:45,360 --> 06:04:50,958
and then view the website.
7111
06:04:48,558 --> 06:04:53,200
The header looks good in the homepage.
7112
06:04:50,958 --> 06:04:55,840
And if we click here to go to the orders
7113
06:04:53,200 --> 06:04:58,958
page, the header also looks good in the
7114
06:04:55,840 --> 06:05:02,080
orders page. So, we're able to reuse the
7115
06:04:58,958 --> 06:05:04,558
header component.
7116
06:05:02,080 --> 06:05:08,080
Next, there's a small issue with the
7117
06:05:04,558 --> 06:05:11,120
header. when we click a link like this
7118
06:05:08,080 --> 06:05:13,520
link in the top left, it actually
7119
06:05:11,120 --> 06:05:17,600
reloads the page when we go to another
7120
06:05:13,520 --> 06:05:21,120
page. So by default, link elements or a
7121
06:05:17,600 --> 06:05:23,520
elements reload the page. Now this made
7122
06:05:21,120 --> 06:05:27,040
sense before when our website had
7123
06:05:23,520 --> 06:05:29,840
multiple HTML files. To go from one file
7124
06:05:27,040 --> 06:05:32,798
to another file, we have to reload the
7125
06:05:29,840 --> 06:05:35,680
page. However, now that we're using
7126
06:05:32,798 --> 06:05:38,638
routing and a single page application,
7127
06:05:35,680 --> 06:05:40,878
we don't have to reload every time, we
7128
06:05:38,638 --> 06:05:42,878
can just use JavaScript to switch
7129
06:05:40,878 --> 06:05:45,440
between pages.
7130
06:05:42,878 --> 06:05:48,718
To do this, React Router provides a
7131
06:05:45,440 --> 06:05:51,520
component called link. So, we'll go back
7132
06:05:48,718 --> 06:05:55,040
to our code
7133
06:05:51,520 --> 06:05:57,440
and we'll open the header component
7134
06:05:55,040 --> 06:06:00,160
and at the top
7135
06:05:57,440 --> 06:06:02,320
we're going to import
7136
06:06:00,160 --> 06:06:06,240
curly brackets
7137
06:06:02,320 --> 06:06:10,520
the link component from
7138
06:06:06,240 --> 06:06:10,520
the package react-rowout.
7139
06:06:10,878 --> 06:06:17,440
So, the link component lets us go to
7140
06:06:13,680 --> 06:06:20,080
another page without reloading.
7141
06:06:17,440 --> 06:06:23,120
To use it, we just have to replace our
7142
06:06:20,080 --> 06:06:26,958
link elements or a elements with this
7143
06:06:23,120 --> 06:06:32,160
link component. So here, let's replace
7144
06:06:26,958 --> 06:06:34,160
this with link and at the end slash
7145
06:06:32,160 --> 06:06:37,280
link.
7146
06:06:34,160 --> 06:06:40,558
And also instead of the href attribute,
7147
06:06:37,280 --> 06:06:46,240
the link component uses a prop called
7148
06:06:40,558 --> 06:06:49,280
to. So this will link to this page.
7149
06:06:46,240 --> 06:06:51,280
Let's scroll down
7150
06:06:49,280 --> 06:06:54,080
and in the header we have two other
7151
06:06:51,280 --> 06:06:56,798
links, the orders link and the cart
7152
06:06:54,080 --> 06:07:01,840
link. So we'll switch both of these as
7153
06:06:56,798 --> 06:07:04,160
well. So here we'll switch it to link
7154
06:07:01,840 --> 06:07:08,718
and slashlink
7155
06:07:04,160 --> 06:07:12,558
and we'll switch the href to two.
7156
06:07:08,718 --> 06:07:14,958
And finally, switch this to link
7157
06:07:12,558 --> 06:07:20,798
and slashlink
7158
06:07:14,958 --> 06:07:23,040
and switch href to two and save.
7159
06:07:20,798 --> 06:07:25,360
And now let's open our website to try it
7160
06:07:23,040 --> 06:07:27,520
out.
7161
06:07:25,360 --> 06:07:30,638
If we click this link to go to the
7162
06:07:27,520 --> 06:07:32,718
orders page, you'll notice it's a little
7163
06:07:30,638 --> 06:07:35,600
bit faster because now it's using
7164
06:07:32,718 --> 06:07:38,958
JavaScript to essentially display a
7165
06:07:35,600 --> 06:07:41,280
different page without reloading.
7166
06:07:38,958 --> 06:07:44,400
So when using React Router, we should
7167
06:07:41,280 --> 06:07:47,440
use the link component instead of the A
7168
06:07:44,400 --> 06:07:51,680
element because the link component lets
7169
06:07:47,440 --> 06:07:55,200
us go to another page without reloading.
7170
06:07:51,680 --> 06:07:58,400
Now let's save these changes into git.
7171
06:07:55,200 --> 06:08:01,600
We'll go to the git section
7172
06:07:58,400 --> 06:08:04,240
and here we split up the header into a
7173
06:08:01,600 --> 06:08:07,120
separate component. And then inside the
7174
06:08:04,240 --> 06:08:10,480
header, we switch to using the link
7175
06:08:07,120 --> 06:08:12,798
component to switch between pages.
7176
06:08:10,480 --> 06:08:17,840
Let's type the message
7177
06:08:12,798 --> 06:08:24,040
split header into a component
7178
06:08:17,840 --> 06:08:24,040
and use link and commit.
7179
06:08:27,760 --> 06:08:33,200
In the next lesson, we'll add a backend
7180
06:08:30,320 --> 06:08:35,680
to this project and make it interactive.
7181
06:08:33,200 --> 06:08:38,718
So, we'll be able to display a list of
7182
06:08:35,680 --> 06:08:41,520
products and add products to a cart and
7183
06:08:38,718 --> 06:08:44,240
create an order. And that's the end of
7184
06:08:41,520 --> 06:08:47,760
this lesson. In this lesson, we started
7185
06:08:44,240 --> 06:08:51,120
the ecommerce project using React and
7186
06:08:47,760 --> 06:08:54,400
Vit. We reviewed Git, which helps us
7187
06:08:51,120 --> 06:08:57,120
track changes in our code. We learned
7188
06:08:54,400 --> 06:09:01,120
routing, which lets us create multiple
7189
06:08:57,120 --> 06:09:03,600
pages in React. We created the homepage,
7190
06:09:01,120 --> 06:09:06,878
the checkout page, and the orders page
7191
06:09:03,600 --> 06:09:10,400
in React. We updated all the links to
7192
06:09:06,878 --> 06:09:13,600
work with routing, and we separated the
7193
06:09:10,400 --> 06:09:15,520
header into a component.
7194
06:09:13,600 --> 06:09:20,360
Here are some exercises to help you
7195
06:09:15,520 --> 06:09:20,360
practice what we learned in this lesson.
7196
06:10:04,000 --> 06:10:07,840
In this lesson, we're going to start
7197
06:10:05,920 --> 06:10:10,480
making our e-commerce project
7198
06:10:07,840 --> 06:10:13,680
interactive and then we'll learn how to
7199
06:10:10,480 --> 06:10:15,840
use a backend with this project.
7200
06:10:13,680 --> 06:10:18,080
By the way, if something in this lesson
7201
06:10:15,840 --> 06:10:19,600
does not work for you, you can check the
7202
06:10:18,080 --> 06:10:21,760
troubleshooting guide in the
7203
06:10:19,600 --> 06:10:25,600
description.
7204
06:10:21,760 --> 06:10:28,638
First, if we look at our website,
7205
06:10:25,600 --> 06:10:32,240
currently we only have three products on
7206
06:10:28,638 --> 06:10:35,120
this website, but in the final project,
7207
06:10:32,240 --> 06:10:37,360
we have a lot more products. So, we're
7208
06:10:35,120 --> 06:10:40,320
going to add all of these products to
7209
06:10:37,360 --> 06:10:42,480
our website. So, let's go to the code
7210
06:10:40,320 --> 06:10:47,520
for the homepage.
7211
06:10:42,480 --> 06:10:50,400
We'll open our code and go to the files
7212
06:10:47,520 --> 06:10:54,280
and we're going to find the pages folder
7213
06:10:50,400 --> 06:10:54,280
and open homepage.jsx.
7214
06:10:54,400 --> 06:10:59,760
So this component represents the
7215
06:10:56,400 --> 06:11:02,000
homepage. And we're going to scroll down
7216
06:10:59,760 --> 06:11:04,160
and find the element with a class
7217
06:11:02,000 --> 06:11:05,600
product-container.
7218
06:11:04,160 --> 06:11:07,920
And we're going to click here to
7219
06:11:05,600 --> 06:11:09,360
collapse this element. And we're going
7220
06:11:07,920 --> 06:11:12,000
to collapse this other product
7221
06:11:09,360 --> 06:11:14,878
container. and this other product
7222
06:11:12,000 --> 06:11:17,760
container as well.
7223
06:11:14,878 --> 06:11:20,798
Each product container represents one
7224
06:11:17,760 --> 06:11:23,600
product on the page. To add more
7225
06:11:20,798 --> 06:11:26,798
products, we could make a copy of this
7226
06:11:23,600 --> 06:11:29,600
code and add each product manually.
7227
06:11:26,798 --> 06:11:33,280
However, that would be very repetitive.
7228
06:11:29,600 --> 06:11:36,080
Instead, we can generate this HTML using
7229
06:11:33,280 --> 06:11:38,400
React. We already learned this technique
7230
06:11:36,080 --> 06:11:41,200
in the chatbot project where we
7231
06:11:38,400 --> 06:11:43,600
generated a list of chat messages on the
7232
06:11:41,200 --> 06:11:45,920
page. So, we're going to review how to
7233
06:11:43,600 --> 06:11:48,240
generate the HTML.
7234
06:11:45,920 --> 06:11:51,040
First, we need to save the data for
7235
06:11:48,240 --> 06:11:53,760
these products in our JavaScript. To
7236
06:11:51,040 --> 06:11:56,638
save time, I already did this. So, if we
7237
06:11:53,760 --> 06:11:58,958
go to our files and we go to the
7238
06:11:56,638 --> 06:12:02,320
starting code folder
7239
06:11:58,958 --> 06:12:06,000
and we open this data folder inside
7240
06:12:02,320 --> 06:12:09,280
products.js, JS I already saved the data
7241
06:12:06,000 --> 06:12:12,558
for the products in this array. So we
7242
06:12:09,280 --> 06:12:15,120
can just use this as our data.
7243
06:12:12,558 --> 06:12:18,480
Each object in the array represents one
7244
06:12:15,120 --> 06:12:21,840
product. So it has a name, an image, and
7245
06:12:18,480 --> 06:12:25,520
a price and some other details. The next
7246
06:12:21,840 --> 06:12:30,558
step is to use this data to generate the
7247
06:12:25,520 --> 06:12:32,798
HTML. We'll go back to the homepage
7248
06:12:30,558 --> 06:12:36,320
and first we're going to import that
7249
06:12:32,798 --> 06:12:39,440
products array. So at the top we're
7250
06:12:36,320 --> 06:12:43,360
going to import curly brackets the
7251
06:12:39,440 --> 06:12:46,000
products array from a string and we're
7252
06:12:43,360 --> 06:12:47,920
going to locate that file. So first we
7253
06:12:46,000 --> 06:12:51,840
need to get out of the pages and source
7254
06:12:47,920 --> 06:12:54,000
folder using dot dot dot slash and then
7255
06:12:51,840 --> 06:12:58,638
we'll find the starting code folder and
7256
06:12:54,000 --> 06:13:03,200
the data folder. starting dashcode slash
7257
06:12:58,638 --> 06:13:05,760
data slash and the products file.
7258
06:13:03,200 --> 06:13:09,280
Next, we're going to loop through this
7259
06:13:05,760 --> 06:13:12,000
products array. So, at the bottom here
7260
06:13:09,280 --> 06:13:15,440
above these product containers, we're
7261
06:13:12,000 --> 06:13:19,760
going to insert using curly brackets the
7262
06:13:15,440 --> 06:13:21,920
products array dot map and brackets. And
7263
06:13:19,760 --> 06:13:25,520
we're going to give map a function
7264
06:13:21,920 --> 06:13:29,120
brackets arrow and curly brackets.
7265
06:13:25,520 --> 06:13:32,878
As a review, do map takes each value in
7266
06:13:29,120 --> 06:13:35,360
an array and transforms it. In this
7267
06:13:32,878 --> 06:13:39,040
parameter, we get each value in this
7268
06:13:35,360 --> 06:13:42,000
array, which is a product.
7269
06:13:39,040 --> 06:13:43,840
So again, a product is just this object
7270
06:13:42,000 --> 06:13:45,920
and it has some details about the
7271
06:13:43,840 --> 06:13:48,400
product inside.
7272
06:13:45,920 --> 06:13:50,240
So, we'll go back
7273
06:13:48,400 --> 06:13:54,160
and we're going to transform this
7274
06:13:50,240 --> 06:13:56,240
product object into this HTML.
7275
06:13:54,160 --> 06:13:58,798
To do that, we're just going to copy
7276
06:13:56,240 --> 06:14:02,638
this HTML
7277
06:13:58,798 --> 06:14:04,480
and inside map, we're going to return
7278
06:14:02,638 --> 06:14:07,840
brackets
7279
06:14:04,480 --> 06:14:09,840
and paste that HTML.
7280
06:14:07,840 --> 06:14:13,760
And to fix the formatting, we're going
7281
06:14:09,840 --> 06:14:16,958
to press control or commandshiftp
7282
06:14:13,760 --> 06:14:19,520
and then search for format document
7283
06:14:16,958 --> 06:14:22,400
and press enter.
7284
06:14:19,520 --> 06:14:24,718
And let's scroll up.
7285
06:14:22,400 --> 06:14:26,958
And remember, whenever we loop through
7286
06:14:24,718 --> 06:14:30,320
an array like this, we need to give each
7287
06:14:26,958 --> 06:14:33,440
element a key prop equals curly
7288
06:14:30,320 --> 06:14:36,638
brackets. So the key helps React keep
7289
06:14:33,440 --> 06:14:39,520
track of changes in this array. Now the
7290
06:14:36,638 --> 06:14:43,280
key has to be unique. So looking at the
7291
06:14:39,520 --> 06:14:45,120
data, each product has a unique ID
7292
06:14:43,280 --> 06:14:47,920
property.
7293
06:14:45,120 --> 06:14:53,040
So we'll go back and we're going to use
7294
06:14:47,920 --> 06:14:56,000
the product. ID as the key. So this
7295
06:14:53,040 --> 06:14:59,520
basically takes each product object and
7296
06:14:56,000 --> 06:15:02,240
transforms it into this HTML.
7297
06:14:59,520 --> 06:15:05,520
So now let's save
7298
06:15:02,240 --> 06:15:08,558
and we'll check the website.
7299
06:15:05,520 --> 06:15:12,080
And as you can see, we took each product
7300
06:15:08,558 --> 06:15:13,760
and transformed it into some HTML.
7301
06:15:12,080 --> 06:15:16,558
Now, obviously, we don't want the
7302
06:15:13,760 --> 06:15:18,718
product to be socks every time. So,
7303
06:15:16,558 --> 06:15:23,200
let's insert the details of each
7304
06:15:18,718 --> 06:15:25,360
product. We'll go back to our code, and
7305
06:15:23,200 --> 06:15:28,878
instead of displaying the socks every
7306
06:15:25,360 --> 06:15:30,638
time, we're going to remove this. and
7307
06:15:28,878 --> 06:15:32,878
insert.
7308
06:15:30,638 --> 06:15:35,120
And each product object has a name
7309
06:15:32,878 --> 06:15:37,840
property. So we'll insert the
7310
06:15:35,120 --> 06:15:40,000
product.name.
7311
06:15:37,840 --> 06:15:43,040
And let's also replace the product
7312
06:15:40,000 --> 06:15:46,638
image. So instead of showing the socks
7313
06:15:43,040 --> 06:15:49,920
image every time, we'll remove this and
7314
06:15:46,638 --> 06:15:51,840
we'll insert the product.
7315
06:15:49,920 --> 06:15:55,440
Property.
7316
06:15:51,840 --> 06:15:57,840
Next, we'll insert the ratings. The way
7317
06:15:55,440 --> 06:16:00,160
this works is that each rating has an
7318
06:15:57,840 --> 06:16:03,920
image. And we just have to multiply the
7319
06:16:00,160 --> 06:16:06,320
rating by 10 to get the image name. So
7320
06:16:03,920 --> 06:16:09,440
for this one, we're going to put open
7321
06:16:06,320 --> 06:16:11,920
curly bracket and close curly bracket
7322
06:16:09,440 --> 06:16:14,718
around this value. And we're going to
7323
06:16:11,920 --> 06:16:18,160
change this to a template string using
7324
06:16:14,718 --> 06:16:21,040
back ticks and back ticks. And then
7325
06:16:18,160 --> 06:16:23,440
instead of 45 every time, we're going to
7326
06:16:21,040 --> 06:16:26,000
insert. And remember for template
7327
06:16:23,440 --> 06:16:28,400
strings to insert we use dollar curly
7328
06:16:26,000 --> 06:16:34,240
brackets. So in here we're going to
7329
06:16:28,400 --> 06:16:37,600
insert the product dot rating dot stars
7330
06:16:34,240 --> 06:16:39,680
and then multiply it by 10. And this
7331
06:16:37,600 --> 06:16:42,958
should give us the ratings image that we
7332
06:16:39,680 --> 06:16:45,920
need. Next we're going to replace the
7333
06:16:42,958 --> 06:16:50,160
rating count. So curly brackets and
7334
06:16:45,920 --> 06:16:51,920
we'll insert the product.
7335
06:16:50,160 --> 06:16:54,878
count.
7336
06:16:51,920 --> 06:16:57,280
Next, we'll insert the price. So,
7337
06:16:54,878 --> 06:17:03,280
instead of this price every time, we'll
7338
06:16:57,280 --> 06:17:06,080
insert the product dot price sets. And
7339
06:17:03,280 --> 06:17:08,160
we usually save money in cents because
7340
06:17:06,080 --> 06:17:11,200
many programming languages have trouble
7341
06:17:08,160 --> 06:17:13,520
calculating decimal numbers. Now, when
7342
06:17:11,200 --> 06:17:16,080
we display it on the website, we want to
7343
06:17:13,520 --> 06:17:18,320
display it as dollars. So to convert
7344
06:17:16,080 --> 06:17:24,160
this back to dollars, we're going to
7345
06:17:18,320 --> 06:17:27,040
divide by 100 since $1 is equal to 100.
7346
06:17:24,160 --> 06:17:30,558
And that's it. We inserted the details
7347
06:17:27,040 --> 06:17:32,320
of each product into this HTML.
7348
06:17:30,558 --> 06:17:35,120
This is the same technique that we used
7349
06:17:32,320 --> 06:17:38,160
in the chatbot project, except our data
7350
06:17:35,120 --> 06:17:40,400
is just a little more complicated. Now,
7351
06:17:38,160 --> 06:17:43,200
let's save
7352
06:17:40,400 --> 06:17:45,760
and check the website.
7353
06:17:43,200 --> 06:17:48,798
And now we have a list of different
7354
06:17:45,760 --> 06:17:52,080
products on the page and we generated
7355
06:17:48,798 --> 06:17:54,400
the HTML for these products. Now there
7356
06:17:52,080 --> 06:17:58,080
is a small problem here which is for
7357
06:17:54,400 --> 06:18:00,558
these socks it's showing the price 10.9
7358
06:17:58,080 --> 06:18:02,798
but usually for price we want to show
7359
06:18:00,558 --> 06:18:06,638
10.90.
7360
06:18:02,798 --> 06:18:08,718
To fix this, we'll go back to our code
7361
06:18:06,638 --> 06:18:12,080
and we're going to scroll down to where
7362
06:18:08,718 --> 06:18:14,240
we insert the price. And to force this
7363
06:18:12,080 --> 06:18:18,080
to show two decimals, we're going to
7364
06:18:14,240 --> 06:18:20,480
wrap this in a open bracket and a closed
7365
06:18:18,080 --> 06:18:23,840
bracket. And then outside of this
7366
06:18:20,480 --> 06:18:25,840
bracket, we'll use the method dot two
7367
06:18:23,840 --> 06:18:27,760
capital Fixed
7368
06:18:25,840 --> 06:18:29,840
and brackets.
7369
06:18:27,760 --> 06:18:32,798
So this converts the number into a
7370
06:18:29,840 --> 06:18:35,520
string and inside these brackets we can
7371
06:18:32,798 --> 06:18:38,000
tell it how many decimals to show. So
7372
06:18:35,520 --> 06:18:40,638
we'll put two
7373
06:18:38,000 --> 06:18:43,360
and save.
7374
06:18:40,638 --> 06:18:46,320
Now if we check the website
7375
06:18:43,360 --> 06:18:48,878
all the prices show two decimals. So
7376
06:18:46,320 --> 06:18:52,718
that's a review of how to generate the
7377
06:18:48,878 --> 06:18:54,958
HTML using some data. Let's go back to
7378
06:18:52,718 --> 06:18:57,280
our code.
7379
06:18:54,958 --> 06:19:00,878
And now that we're generating all this
7380
06:18:57,280 --> 06:19:03,920
HTML, we can scroll to the bottom and
7381
06:19:00,878 --> 06:19:07,600
remove this default HTML that we had
7382
06:19:03,920 --> 06:19:09,600
before and save.
7383
06:19:07,600 --> 06:19:13,360
And now let's save these changes into
7384
06:19:09,600 --> 06:19:17,520
git. We'll go to the git section and at
7385
06:19:13,360 --> 06:19:25,080
the top we'll use a message generate the
7386
06:19:17,520 --> 06:19:25,080
HTML for the home page and commit.
7387
06:19:27,680 --> 06:19:32,480
Now, we're going to take this project to
7388
06:19:29,680 --> 06:19:35,040
the next level and go beyond what we did
7389
06:19:32,480 --> 06:19:38,798
in the chatbot project. We're going to
7390
06:19:35,040 --> 06:19:41,680
add a backend to this project. So, first
7391
06:19:38,798 --> 06:19:45,440
we'll learn what a backend is and why we
7392
06:19:41,680 --> 06:19:47,440
use a backend. So, right now, if we look
7393
06:19:45,440 --> 06:19:49,520
at our files,
7394
06:19:47,440 --> 06:19:52,240
notice that all the products are saved
7395
06:19:49,520 --> 06:19:54,558
in a file on our computer called
7396
06:19:52,240 --> 06:19:57,520
products.js. JS.
7397
06:19:54,558 --> 06:20:00,160
The problem with this is an e-commerce
7398
06:19:57,520 --> 06:20:02,798
website or an online shopping website
7399
06:20:00,160 --> 06:20:05,280
might have thousands of products and it
7400
06:20:02,798 --> 06:20:08,400
does not make sense to save everything
7401
06:20:05,280 --> 06:20:11,040
on our computer or on the customer's
7402
06:20:08,400 --> 06:20:14,320
computer. We might not have enough space
7403
06:20:11,040 --> 06:20:17,520
on our computer. So, what the company or
7404
06:20:14,320 --> 06:20:20,160
the store does is they set up another
7405
06:20:17,520 --> 06:20:23,360
computer in their office or in what we
7406
06:20:20,160 --> 06:20:25,680
call a data center. And this computer
7407
06:20:23,360 --> 06:20:28,240
saves all the thousands of products that
7408
06:20:25,680 --> 06:20:31,280
they offer. And then when we visit the
7409
06:20:28,240 --> 06:20:34,320
website, this computer sends us only the
7410
06:20:31,280 --> 06:20:37,120
products that we need to display. So
7411
06:20:34,320 --> 06:20:39,520
this computer is called the backend.
7412
06:20:37,120 --> 06:20:42,320
It's just another computer that's owned
7413
06:20:39,520 --> 06:20:43,840
by the company and it manages all the
7414
06:20:42,320 --> 06:20:46,160
data.
7415
06:20:43,840 --> 06:20:48,798
Here's another example of why we use a
7416
06:20:46,160 --> 06:20:51,360
backend. Later in this course, we're
7417
06:20:48,798 --> 06:20:54,080
going to add products to a cart. So,
7418
06:20:51,360 --> 06:20:56,320
where do we save this cart? It does not
7419
06:20:54,080 --> 06:20:58,638
make sense to save the cart on the
7420
06:20:56,320 --> 06:21:00,958
customer's computer because if the
7421
06:20:58,638 --> 06:21:03,040
customer uses a different computer to
7422
06:21:00,958 --> 06:21:06,400
visit the website, they would no longer
7423
06:21:03,040 --> 06:21:09,440
have access to their cart. Instead, we
7424
06:21:06,400 --> 06:21:11,520
save the cart in the back end as well.
7425
06:21:09,440 --> 06:21:14,480
When they access the website, the
7426
06:21:11,520 --> 06:21:17,680
backend sends the cart to the website.
7427
06:21:14,480 --> 06:21:20,000
So, the back end also lets us share data
7428
06:21:17,680 --> 06:21:22,558
between computers.
7429
06:21:20,000 --> 06:21:26,400
So again, the backend is another
7430
06:21:22,558 --> 06:21:28,878
computer that manages the data. And this
7431
06:21:26,400 --> 06:21:32,080
is how a React website works in the real
7432
06:21:28,878 --> 06:21:34,240
world. We usually contact a backend to
7433
06:21:32,080 --> 06:21:36,080
get the data. So we're going to practice
7434
06:21:34,240 --> 06:21:38,320
that in this course. We're going to
7435
06:21:36,080 --> 06:21:39,840
learn how to add a backend and then
7436
06:21:38,320 --> 06:21:42,878
we're going to learn how to use the
7437
06:21:39,840 --> 06:21:47,040
backend in React. So you can see how we
7438
06:21:42,878 --> 06:21:49,040
use React in the real world.
7439
06:21:47,040 --> 06:21:52,320
Now that we learned what a backend is
7440
06:21:49,040 --> 06:21:55,200
and why we use a backend, let's add a
7441
06:21:52,320 --> 06:21:57,760
backend to our project. We're going to
7442
06:21:55,200 --> 06:22:00,638
click this link in the description. Go
7443
06:21:57,760 --> 06:22:03,280
to lesson 7 and click this link to open
7444
06:22:00,638 --> 06:22:06,000
the backend code.
7445
06:22:03,280 --> 06:22:08,480
And this is a backend that we can use
7446
06:22:06,000 --> 06:22:11,120
with our project. I called it the
7447
06:22:08,480 --> 06:22:13,440
e-commerce backend AI because this was
7448
06:22:11,120 --> 06:22:16,000
built with AI. And I show you how to do
7449
06:22:13,440 --> 06:22:18,558
that in another video. But for this
7450
06:22:16,000 --> 06:22:20,798
video, I recommend downloading this
7451
06:22:18,558 --> 06:22:23,040
version to make sure it works with a
7452
06:22:20,798 --> 06:22:25,600
video. You can try to recreate the back
7453
06:22:23,040 --> 06:22:27,920
end as an exercise. So, we're going to
7454
06:22:25,600 --> 06:22:31,280
click this code button here and we'll
7455
06:22:27,920 --> 06:22:33,200
click download zip.
7456
06:22:31,280 --> 06:22:35,600
Once that finishes downloading, we're
7457
06:22:33,200 --> 06:22:37,920
going to open the downloads folder. So,
7458
06:22:35,600 --> 06:22:40,558
for me, it's right here. And then we're
7459
06:22:37,920 --> 06:22:42,558
going to unzip this. On Windows, we're
7460
06:22:40,558 --> 06:22:46,240
going to rightclick the file and then
7461
06:22:42,558 --> 06:22:46,240
click extract all.
7462
06:22:49,520 --> 06:22:54,240
And on Mac, you can just doubleclick the
7463
06:22:51,840 --> 06:22:56,638
zip file.
7464
06:22:54,240 --> 06:22:59,360
Then we're going to move this back end
7465
06:22:56,638 --> 06:23:00,958
into our project. So we'll open our
7466
06:22:59,360 --> 06:23:02,638
code.
7467
06:23:00,958 --> 06:23:05,520
And then we're just going to drag this
7468
06:23:02,638 --> 06:23:08,320
folder into our project. But first, I'm
7469
06:23:05,520 --> 06:23:10,558
going to collapse these folders for now.
7470
06:23:08,320 --> 06:23:12,958
And we're going to drag this folder to
7471
06:23:10,558 --> 06:23:16,320
the outside
7472
06:23:12,958 --> 06:23:18,958
and click copy folder
7473
06:23:16,320 --> 06:23:22,080
so that the backend code is right beside
7474
06:23:18,958 --> 06:23:24,480
our website. By the way, we call the
7475
06:23:22,080 --> 06:23:27,440
website or what the user interacts with
7476
06:23:24,480 --> 06:23:29,600
the front end.
7477
06:23:27,440 --> 06:23:33,280
We're also going to rename this folder.
7478
06:23:29,600 --> 06:23:35,520
So, right click and rename.
7479
06:23:33,280 --> 06:23:39,920
and we'll just rename it the e-commerce
7480
06:23:35,520 --> 06:23:43,280
backend to make it easier to understand.
7481
06:23:39,920 --> 06:23:45,440
Next, we're going to start this backend.
7482
06:23:43,280 --> 06:23:48,638
So, we're going to go to our terminal or
7483
06:23:45,440 --> 06:23:52,240
our command line and we'll click here to
7484
06:23:48,638 --> 06:23:54,400
create a new tab. And then in here,
7485
06:23:52,240 --> 06:23:57,040
first we're going to change directory
7486
06:23:54,400 --> 06:24:00,080
into the e-commerce backend folder. So,
7487
06:23:57,040 --> 06:24:02,798
we'll cd ecommerce
7488
06:24:00,080 --> 06:24:04,718
dashbackend.
7489
06:24:02,798 --> 06:24:07,760
So now the command line is running
7490
06:24:04,718 --> 06:24:10,320
inside this folder and the process for
7491
06:24:07,760 --> 06:24:13,520
starting the back end is very similar to
7492
06:24:10,320 --> 06:24:17,360
the website or the front end. So first
7493
06:24:13,520 --> 06:24:22,440
we're going to npm install to install
7494
06:24:17,360 --> 06:24:22,440
all the npm packages that we need
7495
06:24:23,920 --> 06:24:30,958
and then we'll run npm rundev to start
7496
06:24:28,000 --> 06:24:32,718
the back end.
7497
06:24:30,958 --> 06:24:36,000
So, this tells us the back end is
7498
06:24:32,718 --> 06:24:39,360
running on port 3000, which is localhost
7499
06:24:36,000 --> 06:24:41,440
col 3000. To test it, we're going to go
7500
06:24:39,360 --> 06:24:43,280
to our browser,
7501
06:24:41,440 --> 06:24:46,718
and at the top, we're going to create a
7502
06:24:43,280 --> 06:24:48,320
new tab, and we're going to go to
7503
06:24:46,718 --> 06:24:51,120
localhost
7504
06:24:48,320 --> 06:24:54,120
col 3000
7505
06:24:51,120 --> 06:24:54,120
/s.
7506
06:24:56,798 --> 06:25:00,718
And the back end should send back some
7507
06:24:59,120 --> 06:25:03,040
data.
7508
06:25:00,718 --> 06:25:05,120
And this is actually the data for the
7509
06:25:03,040 --> 06:25:08,000
products.
7510
06:25:05,120 --> 06:25:10,558
I can also click this checkbox here to
7511
06:25:08,000 --> 06:25:12,958
show it in an easier to read format. And
7512
06:25:10,558 --> 06:25:15,680
you can see this is the same products
7513
06:25:12,958 --> 06:25:18,878
array that we were using before except
7514
06:25:15,680 --> 06:25:21,600
this is coming from the back end. So the
7515
06:25:18,878 --> 06:25:24,638
way a backend works is when we access
7516
06:25:21,600 --> 06:25:28,000
the backend using a URL, it will send
7517
06:25:24,638 --> 06:25:30,878
back some data. So this is working.
7518
06:25:28,000 --> 06:25:33,920
So now let's go back to our code and
7519
06:25:30,878 --> 06:25:36,000
we'll save these changes into git. Open
7520
06:25:33,920 --> 06:25:40,240
the git section and we'll use the
7521
06:25:36,000 --> 06:25:44,920
message add ecommerce
7522
06:25:40,240 --> 06:25:44,920
backend and commit.
7523
06:25:47,200 --> 06:25:52,718
Next we're going to learn data fetching.
7524
06:25:49,920 --> 06:25:56,320
Data fetching means to get data from the
7525
06:25:52,718 --> 06:25:59,440
back end using our code. So, first we're
7526
06:25:56,320 --> 06:26:02,080
going to go to our files and let's go
7527
06:25:59,440 --> 06:26:04,718
back to the e-commerce project. And
7528
06:26:02,080 --> 06:26:07,440
we're going to open our homepage,
7529
06:26:04,718 --> 06:26:10,798
which is right here.
7530
06:26:07,440 --> 06:26:13,680
And we'll scroll to the top. And one way
7531
06:26:10,798 --> 06:26:17,600
to fetch data or to get data from the
7532
06:26:13,680 --> 06:26:19,680
back end is to use the function fetch
7533
06:26:17,600 --> 06:26:22,638
and brackets.
7534
06:26:19,680 --> 06:26:24,958
So fetch is a built-in function. It's
7535
06:26:22,638 --> 06:26:26,638
provided by JavaScript.
7536
06:26:24,958 --> 06:26:28,718
between the brackets, we're going to
7537
06:26:26,638 --> 06:26:31,360
create a string
7538
06:26:28,718 --> 06:26:34,400
and we're going to put the URL that we
7539
06:26:31,360 --> 06:26:37,040
want to get data from. So, if we go back
7540
06:26:34,400 --> 06:26:39,840
to our browser,
7541
06:26:37,040 --> 06:26:42,718
remember that this URL gave us the
7542
06:26:39,840 --> 06:26:46,400
products data. So, we can just select
7543
06:26:42,718 --> 06:26:49,520
this URL and copy.
7544
06:26:46,400 --> 06:26:52,160
And in our code, we're going to paste it
7545
06:26:49,520 --> 06:26:54,638
into fetch.
7546
06:26:52,160 --> 06:26:56,878
And make sure you have http col double
7547
06:26:54,638 --> 06:26:58,958
slash in front. It doesn't show in the
7548
06:26:56,878 --> 06:27:02,798
browser, but we need this in order for
7549
06:26:58,958 --> 06:27:06,080
our code to work. So if we fetch data
7550
06:27:02,798 --> 06:27:09,200
from this URL, this should give us the
7551
06:27:06,080 --> 06:27:12,160
products data that we saw earlier.
7552
06:27:09,200 --> 06:27:14,958
However, we cannot save this data in a
7553
06:27:12,160 --> 06:27:18,000
variable like this. And that's because
7554
06:27:14,958 --> 06:27:20,718
when we contact a backend, it takes time
7555
06:27:18,000 --> 06:27:24,798
for this code to reach the back end and
7556
06:27:20,718 --> 06:27:27,600
to get a response. So this code does not
7557
06:27:24,798 --> 06:27:30,798
finish right away. It finishes at some
7558
06:27:27,600 --> 06:27:33,280
point in the future. As a review, we
7559
06:27:30,798 --> 06:27:35,600
call this asynchronous code.
7560
06:27:33,280 --> 06:27:38,558
Asynchronous code means code that does
7561
06:27:35,600 --> 06:27:40,798
not finish right away. So how do we
7562
06:27:38,558 --> 06:27:44,478
handle this situation?
7563
06:27:40,798 --> 06:27:47,440
fetch returns a promise and a promise
7564
06:27:44,478 --> 06:27:48,958
lets us wait for asynchronous code to
7565
06:27:47,440 --> 06:27:52,798
finish.
7566
06:27:48,958 --> 06:27:56,558
So a promise has a method called dot
7567
06:27:52,798 --> 06:27:59,200
then and brackets and we give dot then a
7568
06:27:56,558 --> 06:28:00,958
function brackets arrow and curly
7569
06:27:59,200 --> 06:28:03,760
brackets.
7570
06:28:00,958 --> 06:28:06,558
The way this works is that this code
7571
06:28:03,760 --> 06:28:08,558
contacts the backend to get some data
7572
06:28:06,558 --> 06:28:11,520
but it takes some time for the backend
7573
06:28:08,558 --> 06:28:15,360
to respond. So the code is going to keep
7574
06:28:11,520 --> 06:28:18,000
going line by line. Then at some point
7575
06:28:15,360 --> 06:28:19,680
in the future, this is going to finish.
7576
06:28:18,000 --> 06:28:22,878
We're going to get the response from the
7577
06:28:19,680 --> 06:28:26,958
back end. When it finishes in the
7578
06:28:22,878 --> 06:28:29,200
future, it will run the function inside.
7579
06:28:26,958 --> 06:28:32,638
So that's a review of how a promise
7580
06:28:29,200 --> 06:28:35,200
works. So next when fetch gets a
7581
06:28:32,638 --> 06:28:38,080
response from the back end it will save
7582
06:28:35,200 --> 06:28:40,798
it as a parameter in this inner function
7583
06:28:38,080 --> 06:28:43,440
called response.
7584
06:28:40,798 --> 06:28:47,280
By the way when we contact the back end
7585
06:28:43,440 --> 06:28:49,360
using fetch this is called a request.
7586
06:28:47,280 --> 06:28:53,280
When the back end responds with some
7587
06:28:49,360 --> 06:28:56,080
data this is called a response. So this
7588
06:28:53,280 --> 06:28:59,440
parameter contains the response from the
7589
06:28:56,080 --> 06:29:01,040
back end. And inside here, let's just
7590
06:28:59,440 --> 06:29:04,798
console.log
7591
06:29:01,040 --> 06:29:07,360
the response to see what it looks like.
7592
06:29:04,798 --> 06:29:10,400
We'll save
7593
06:29:07,360 --> 06:29:13,200
and then check our website
7594
06:29:10,400 --> 06:29:15,280
and we'll open the tab for our website
7595
06:29:13,200 --> 06:29:18,000
and we're going to open the console.
7596
06:29:15,280 --> 06:29:20,240
Rightclick, inspect,
7597
06:29:18,000 --> 06:29:22,558
and the console.
7598
06:29:20,240 --> 06:29:25,520
And you can see that we got the response
7599
06:29:22,558 --> 06:29:27,920
from the back end. So inside here we
7600
06:29:25,520 --> 06:29:31,680
have some information like the URL that
7601
06:29:27,920 --> 06:29:33,840
we accessed and a status code. 200 means
7602
06:29:31,680 --> 06:29:36,240
it was successful.
7603
06:29:33,840 --> 06:29:39,600
However, notice that we don't have the
7604
06:29:36,240 --> 06:29:41,840
products data in this response. To get
7605
06:29:39,600 --> 06:29:44,240
the products data, we're going to go
7606
06:29:41,840 --> 06:29:47,760
back to our code
7607
06:29:44,240 --> 06:29:50,558
and this response object has a method
7608
06:29:47,760 --> 06:29:53,920
called response.json
7609
06:29:50,558 --> 06:29:57,200
JSON and brackets.
7610
06:29:53,920 --> 06:29:59,840
So,JSON gives us the data that is
7611
06:29:57,200 --> 06:30:03,120
attached to the response. In this case,
7612
06:29:59,840 --> 06:30:06,478
it gives us the products data. Now, the
7613
06:30:03,120 --> 06:30:09,840
tricky part about this is response.json
7614
06:30:06,478 --> 06:30:13,440
is also asynchronous. So, again, we
7615
06:30:09,840 --> 06:30:16,400
can't save this in a variable. Instead,
7616
06:30:13,440 --> 06:30:20,080
this is also a promise. So we have to
7617
06:30:16,400 --> 06:30:22,958
use dot then again and give this another
7618
06:30:20,080 --> 06:30:25,280
function brackets arrow and curly
7619
06:30:22,958 --> 06:30:28,400
brackets
7620
06:30:25,280 --> 06:30:31,440
and then inside this inner function we
7621
06:30:28,400 --> 06:30:34,000
have access to the data that we get from
7622
06:30:31,440 --> 06:30:36,400
the back end. So in this case this is
7623
06:30:34,000 --> 06:30:41,760
the products data.
7624
06:30:36,400 --> 06:30:43,600
So if we console.log log the data and
7625
06:30:41,760 --> 06:30:46,400
save
7626
06:30:43,600 --> 06:30:48,478
and then check the console.
7627
06:30:46,400 --> 06:30:51,440
You'll see that the backend gave us an
7628
06:30:48,478 --> 06:30:54,878
array of products to display on the
7629
06:30:51,440 --> 06:30:56,878
page. So, this is the same thing as the
7630
06:30:54,878 --> 06:30:59,280
products array that we saved in our
7631
06:30:56,878 --> 06:31:01,600
file, but this data is coming from the
7632
06:30:59,280 --> 06:31:03,600
back end.
7633
06:31:01,600 --> 06:31:07,040
Let's go back.
7634
06:31:03,600 --> 06:31:09,680
And that's how we fetch data or get data
7635
06:31:07,040 --> 06:31:12,718
from the back end. We use the built-in
7636
06:31:09,680 --> 06:31:16,320
function fetch and give it the URL of
7637
06:31:12,718 --> 06:31:18,718
the back end and then we use dot then to
7638
06:31:16,320 --> 06:31:21,840
wait for a response.
7639
06:31:18,718 --> 06:31:24,798
Now one shortcut we can use is instead
7640
06:31:21,840 --> 06:31:28,080
of doing dot then inside another dot
7641
06:31:24,798 --> 06:31:30,958
then we can also return the
7642
06:31:28,080 --> 06:31:34,558
response.json JSON and then move this
7643
06:31:30,958 --> 06:31:36,558
dot then to the outside.
7644
06:31:34,558 --> 06:31:40,958
So this code does the same thing as
7645
06:31:36,558 --> 06:31:44,160
before. This waits for this to finish
7646
06:31:40,958 --> 06:31:46,240
and then runs this function. So this is
7647
06:31:44,160 --> 06:31:48,160
just a shortcut that we can use with
7648
06:31:46,240 --> 06:31:50,638
promises.
7649
06:31:48,160 --> 06:31:53,360
Another thing to note is the back end
7650
06:31:50,638 --> 06:31:56,558
can also run on the same computer as the
7651
06:31:53,360 --> 06:31:58,958
website or the front end. So right now
7652
06:31:56,558 --> 06:32:02,080
this back end is actually running on our
7653
06:31:58,958 --> 06:32:04,558
own computer. Locost means our own
7654
06:32:02,080 --> 06:32:06,798
computer. So when we contact the back
7655
06:32:04,558 --> 06:32:09,120
end, we're actually just contacting our
7656
06:32:06,798 --> 06:32:11,600
own computer. We do this during
7657
06:32:09,120 --> 06:32:13,920
development because it's easier. But
7658
06:32:11,600 --> 06:32:17,600
once this website is on the internet,
7659
06:32:13,920 --> 06:32:20,478
the back end will be another computer.
7660
06:32:17,600 --> 06:32:23,520
So now let's save this code
7661
06:32:20,478 --> 06:32:27,040
and we'll save this into git. In the get
7662
06:32:23,520 --> 06:32:33,878
section, we'll use the message fetch
7663
06:32:27,040 --> 06:32:33,878
data from the back end and commit.
7664
06:32:35,520 --> 06:32:42,558
Next, notice that using fetch is still a
7665
06:32:38,638 --> 06:32:46,958
little hard to read. We use a and then
7666
06:32:42,558 --> 06:32:48,798
we use response.json and then another.
7667
06:32:46,958 --> 06:32:52,638
To make this code cleaner, we're going
7668
06:32:48,798 --> 06:32:55,440
to use an npm package called Axios.
7669
06:32:52,638 --> 06:32:58,240
Axios is a cleaner way to make requests
7670
06:32:55,440 --> 06:33:00,478
to the back end. First, we're going to
7671
06:32:58,240 --> 06:33:03,600
go to our command line and we'll open
7672
06:33:00,478 --> 06:33:07,680
the tab for the front end. And here
7673
06:33:03,600 --> 06:33:12,400
we'll press Ctrl C to stop the V server.
7674
06:33:07,680 --> 06:33:14,958
And we're going to npm install Axios and
7675
06:33:12,400 --> 06:33:17,280
type at and we're going to give it a
7676
06:33:14,958 --> 06:33:20,400
version. So, we can click this link in
7677
06:33:17,280 --> 06:33:22,638
the description and then go to lesson 7.
7678
06:33:20,400 --> 06:33:25,360
And this is a version of Axios we're
7679
06:33:22,638 --> 06:33:27,360
going to use. So, again, you should
7680
06:33:25,360 --> 06:33:29,120
always use the version that you see on
7681
06:33:27,360 --> 06:33:32,240
your page.
7682
06:33:29,120 --> 06:33:34,558
So, select the version on your page and
7683
06:33:32,240 --> 06:33:37,520
copy it
7684
06:33:34,558 --> 06:33:41,200
and then paste it in our code and press
7685
06:33:37,520 --> 06:33:42,958
enter to install Axios.
7686
06:33:41,200 --> 06:33:46,718
Once that's installed, we're going to
7687
06:33:42,958 --> 06:33:48,400
run mpm rundev to restart the vit
7688
06:33:46,718 --> 06:33:51,360
server.
7689
06:33:48,400 --> 06:33:53,760
And then to use axios in our code, we're
7690
06:33:51,360 --> 06:33:58,160
going to go to the homepage. And at the
7691
06:33:53,760 --> 06:34:03,360
top, we're going to import axios without
7692
06:33:58,160 --> 06:34:05,920
curly brackets from a string axios.
7693
06:34:03,360 --> 06:34:09,920
And now instead of using fetch, we're
7694
06:34:05,920 --> 06:34:12,718
going to use axios.get.
7695
06:34:09,920 --> 06:34:14,958
So this does the same thing as before
7696
06:34:12,718 --> 06:34:18,320
except the data from the back end will
7697
06:34:14,958 --> 06:34:21,440
be saved directly inside the response.
7698
06:34:18,320 --> 06:34:26,840
So instead of doing all these steps,
7699
06:34:21,440 --> 06:34:26,840
we can just get the data using response.
7700
06:34:27,040 --> 06:34:34,400
So let's console.log this to see if it
7701
06:34:29,760 --> 06:34:37,520
works. So console.log and save.
7702
06:34:34,400 --> 06:34:39,680
And then if we open the console,
7703
06:34:37,520 --> 06:34:43,040
you'll notice it gets the same products
7704
06:34:39,680 --> 06:34:45,120
data as before.
7705
06:34:43,040 --> 06:34:47,600
But if we look at our code, it's a lot
7706
06:34:45,120 --> 06:34:50,558
cleaner because we can just get the data
7707
06:34:47,600 --> 06:34:53,520
directly from the response. So as you
7708
06:34:50,558 --> 06:34:56,400
can see, Axios is a cleaner way to make
7709
06:34:53,520 --> 06:34:59,520
requests to the back end. So now let's
7710
06:34:56,400 --> 06:35:04,160
save this into git. In the get section,
7711
06:34:59,520 --> 06:35:09,320
we'll use a message use Axios to fetch
7712
06:35:04,160 --> 06:35:09,320
the data and commit.
7713
06:35:11,520 --> 06:35:17,360
Now, let's use this data from the back
7714
06:35:14,320 --> 06:35:19,520
end to generate the HTML instead of
7715
06:35:17,360 --> 06:35:23,360
using the products file that we had
7716
06:35:19,520 --> 06:35:26,400
before. But first, we have a problem.
7717
06:35:23,360 --> 06:35:29,280
This code is directly inside the
7718
06:35:26,400 --> 06:35:32,638
homepage component. That means whenever
7719
06:35:29,280 --> 06:35:34,798
the homepage changes or rerenders, it
7720
06:35:32,638 --> 06:35:37,360
will run this code again and send
7721
06:35:34,798 --> 06:35:39,200
another request to the back end.
7722
06:35:37,360 --> 06:35:41,920
However, we just need to load the
7723
06:35:39,200 --> 06:35:44,080
products once on this page. We don't
7724
06:35:41,920 --> 06:35:47,680
need to load the products over and over
7725
06:35:44,080 --> 06:35:51,440
again. To make this code run once, we
7726
06:35:47,680 --> 06:35:54,798
can put it inside a use effect. At the
7727
06:35:51,440 --> 06:35:58,400
top, we're going to import curly
7728
06:35:54,798 --> 06:36:01,360
brackets use effect
7729
06:35:58,400 --> 06:36:04,320
from React.
7730
06:36:01,360 --> 06:36:08,558
And above this code, we're going to do
7731
06:36:04,320 --> 06:36:10,798
use effect brackets and give this a
7732
06:36:08,558 --> 06:36:13,798
function brackets arrow and curly
7733
06:36:10,798 --> 06:36:13,798
brackets.
7734
06:36:13,840 --> 06:36:20,160
Use effect lets us control when some
7735
06:36:16,878 --> 06:36:24,160
code runs. We're going to move this code
7736
06:36:20,160 --> 06:36:27,200
inside the use effect. So we'll cut and
7737
06:36:24,160 --> 06:36:29,040
in here we'll paste.
7738
06:36:27,200 --> 06:36:32,160
And I'll select these lines and press
7739
06:36:29,040 --> 06:36:35,360
tab to reformat.
7740
06:36:32,160 --> 06:36:38,080
By default, use effect runs this code
7741
06:36:35,360 --> 06:36:41,840
every time the component is created or
7742
06:36:38,080 --> 06:36:44,080
updated. But we can control this. At the
7743
06:36:41,840 --> 06:36:48,878
end of this function, we're going to add
7744
06:36:44,080 --> 06:36:52,160
a comma and an empty array. As a review,
7745
06:36:48,878 --> 06:36:54,958
this is called a dependency array. The
7746
06:36:52,160 --> 06:36:58,718
dependency array lets us control when
7747
06:36:54,958 --> 06:37:01,920
use effect runs. If we use an empty
7748
06:36:58,718 --> 06:37:05,520
array, that means this code will only
7749
06:37:01,920 --> 06:37:08,160
run once after the component is created.
7750
06:37:05,520 --> 06:37:10,478
And that's exactly what we need here. We
7751
06:37:08,160 --> 06:37:13,440
just need to load the products once on
7752
06:37:10,478 --> 06:37:15,520
this page. By the way, you might notice
7753
06:37:13,440 --> 06:37:18,000
in the console that use effect runs
7754
06:37:15,520 --> 06:37:20,240
twice instead of once. So, this is
7755
06:37:18,000 --> 06:37:23,680
totally normal and it's caused by using
7756
06:37:20,240 --> 06:37:26,478
strict mode in our app. Strict mode runs
7757
06:37:23,680 --> 06:37:28,160
use effect twice to help us catch bugs
7758
06:37:26,478 --> 06:37:30,718
and it only does this during
7759
06:37:28,160 --> 06:37:33,440
development.
7760
06:37:30,718 --> 06:37:36,718
So now that we have the data, how do we
7761
06:37:33,440 --> 06:37:39,040
use it in this loop down here to
7762
06:37:36,718 --> 06:37:41,200
generate the HTML?
7763
06:37:39,040 --> 06:37:45,280
To do that, we need to save the products
7764
06:37:41,200 --> 06:37:49,040
data inside the state. So at the top,
7765
06:37:45,280 --> 06:37:51,360
we're also going to import use state
7766
06:37:49,040 --> 06:37:53,520
from React.
7767
06:37:51,360 --> 06:37:57,040
And at the top of our component, we're
7768
06:37:53,520 --> 06:38:01,120
going to do const square bracket equals
7769
06:37:57,040 --> 06:38:04,400
use state and brackets.
7770
06:38:01,120 --> 06:38:07,760
As a review, use state gives us an array
7771
06:38:04,400 --> 06:38:10,320
with two values. The first value is the
7772
06:38:07,760 --> 06:38:13,360
name of the data. In this case, we'll
7773
06:38:10,320 --> 06:38:16,320
name it the products. And then we'll
7774
06:38:13,360 --> 06:38:19,760
type a comma. And the second value of
7775
06:38:16,320 --> 06:38:22,320
use state is the updater function. The
7776
06:38:19,760 --> 06:38:24,958
updater function lets us update this
7777
06:38:22,320 --> 06:38:27,920
value and then it will regenerate the
7778
06:38:24,958 --> 06:38:30,878
HTML. We usually name the updater
7779
06:38:27,920 --> 06:38:34,958
function set and then the name of the
7780
06:38:30,878 --> 06:38:37,600
data capital P products.
7781
06:38:34,958 --> 06:38:39,520
And finally between these brackets we're
7782
06:38:37,600 --> 06:38:43,440
going to put the starting value of the
7783
06:38:39,520 --> 06:38:46,160
products. Let's just use an empty array.
7784
06:38:43,440 --> 06:38:48,638
And now after we get the products from
7785
06:38:46,160 --> 06:38:50,798
the back end, we're going to save it
7786
06:38:48,638 --> 06:38:53,520
into the state.
7787
06:38:50,798 --> 06:38:58,000
So instead of console.log, we'll use the
7788
06:38:53,520 --> 06:39:01,120
updater function set products. This will
7789
06:38:58,000 --> 06:39:04,080
save the products data into here. And
7790
06:39:01,120 --> 06:39:07,600
then it's going to use this to generate
7791
06:39:04,080 --> 06:39:11,040
the HTML. So now we're using data from
7792
06:39:07,600 --> 06:39:13,360
the back end to generate the HTML. And
7793
06:39:11,040 --> 06:39:15,440
we don't need to import this file here
7794
06:39:13,360 --> 06:39:17,680
anymore.
7795
06:39:15,440 --> 06:39:20,478
So the only thing to note is that the
7796
06:39:17,680 --> 06:39:22,638
name of the data has to match the name
7797
06:39:20,478 --> 06:39:26,000
that we use down here. So that this code
7798
06:39:22,638 --> 06:39:28,320
is actually using the state.
7799
06:39:26,000 --> 06:39:31,200
Now let's save
7800
06:39:28,320 --> 06:39:34,240
and we'll check the website.
7801
06:39:31,200 --> 06:39:36,878
And everything still works except now
7802
06:39:34,240 --> 06:39:39,920
we're using data from the back end to
7803
06:39:36,878 --> 06:39:43,200
generate this HTML.
7804
06:39:39,920 --> 06:39:46,478
We'll go back to our code
7805
06:39:43,200 --> 06:39:49,760
and we'll save this into git. In the git
7806
06:39:46,478 --> 06:39:54,558
section, we'll create the message use
7807
06:39:49,760 --> 06:39:59,400
data from the back end to generate the
7808
06:39:54,558 --> 06:39:59,400
HTML and commit.
7809
06:40:02,000 --> 06:40:07,680
Next, if we look at the website
7810
06:40:05,200 --> 06:40:09,920
in the homepage, we also need to get the
7811
06:40:07,680 --> 06:40:13,600
cart from the back end because we need
7812
06:40:09,920 --> 06:40:17,360
to display this quantity number up here.
7813
06:40:13,600 --> 06:40:19,440
To do that, we'll go back to our code
7814
06:40:17,360 --> 06:40:22,478
and we're going to make another request
7815
06:40:19,440 --> 06:40:25,360
to the back end to get the cart. So,
7816
06:40:22,478 --> 06:40:30,878
below this request, we're going to use
7817
06:40:25,360 --> 06:40:34,878
axios again. Axios.get get brackets a
7818
06:40:30,878 --> 06:40:37,878
string and we'll give it the URL http
7819
06:40:34,878 --> 06:40:37,878
colhost
7820
06:40:38,080 --> 06:40:43,840
col 3000
7821
06:40:40,160 --> 06:40:45,840
slash API/cart-
7822
06:40:43,840 --> 06:40:48,718
items.
7823
06:40:45,840 --> 06:40:53,440
The way this works is the back end is
7824
06:40:48,718 --> 06:40:57,840
running on http localhost col 3000 and
7825
06:40:53,440 --> 06:41:00,638
after this we add a URL path. Each URL
7826
06:40:57,840 --> 06:41:04,638
path gives us different data. For
7827
06:41:00,638 --> 06:41:07,440
example, the URL path/ API/proucts
7828
06:41:04,638 --> 06:41:11,440
gives us the products data. The URL
7829
06:41:07,440 --> 06:41:14,558
path/ API/cart items gives us the cart
7830
06:41:11,440 --> 06:41:17,120
data. So the URL path can actually be
7831
06:41:14,558 --> 06:41:20,080
anything we want. Whoever creates the
7832
06:41:17,120 --> 06:41:23,680
backend decides what the URL paths will
7833
06:41:20,080 --> 06:41:27,520
be. In the description, you can find a
7834
06:41:23,680 --> 06:41:30,080
documentation page for my backend.
7835
06:41:27,520 --> 06:41:33,360
And this lists all the URL paths that we
7836
06:41:30,080 --> 06:41:37,360
can use. A common naming convention is
7837
06:41:33,360 --> 06:41:40,080
to start the URL path with slash API.
7838
06:41:37,360 --> 06:41:43,760
API means application programming
7839
06:41:40,080 --> 06:41:46,478
interface. Starting with / API makes it
7840
06:41:43,760 --> 06:41:48,958
clear that these URL paths are
7841
06:41:46,478 --> 06:41:51,520
specifically for interacting with the
7842
06:41:48,958 --> 06:41:53,600
backend.
7843
06:41:51,520 --> 06:41:55,280
So we already learned how to use /
7844
06:41:53,600 --> 06:41:57,360
API/roucts.
7845
06:41:55,280 --> 06:42:01,360
Now we're going to learn how to use /
7846
06:41:57,360 --> 06:42:03,680
API/cart items.
7847
06:42:01,360 --> 06:42:05,760
And remember whenever we make a request
7848
06:42:03,680 --> 06:42:08,718
to the back end it's going to take some
7849
06:42:05,760 --> 06:42:13,200
time to get a response. So we need to
7850
06:42:08,718 --> 06:42:15,600
wait for this code to finish using then
7851
06:42:13,200 --> 06:42:17,760
we'll give this a function
7852
06:42:15,600 --> 06:42:20,080
and the response is going to be saved in
7853
06:42:17,760 --> 06:42:23,440
here.
7854
06:42:20,080 --> 06:42:25,840
So when we make a request to /appi/cart
7855
06:42:23,440 --> 06:42:28,718
items, we're going to get the cart in
7856
06:42:25,840 --> 06:42:30,718
the response. So in here, let's
7857
06:42:28,718 --> 06:42:34,478
console.log
7858
06:42:30,718 --> 06:42:37,680
the response data to see what the cart
7859
06:42:34,478 --> 06:42:40,718
looks like. We'll save
7860
06:42:37,680 --> 06:42:43,440
and we'll check the console.
7861
06:42:40,718 --> 06:42:46,718
And you might need to refresh. And the
7862
06:42:43,440 --> 06:42:49,920
back end responded with an array. And
7863
06:42:46,718 --> 06:42:52,798
this array represents the cart that we
7864
06:42:49,920 --> 06:42:55,360
have on the website. So each item in the
7865
06:42:52,798 --> 06:42:58,000
cart contains the product ID that we
7866
06:42:55,360 --> 06:43:00,798
want to buy and the quantity of that
7867
06:42:58,000 --> 06:43:02,958
product. So next we're going to use this
7868
06:43:00,798 --> 06:43:05,360
to calculate the quantity and display it
7869
06:43:02,958 --> 06:43:09,360
on the page. So we'll go back to our
7870
06:43:05,360 --> 06:43:11,840
code and first we need to save this cart
7871
06:43:09,360 --> 06:43:13,600
into state so we can use it to create
7872
06:43:11,840 --> 06:43:16,400
the HTML.
7873
06:43:13,600 --> 06:43:20,638
At the top, we'll create another state
7874
06:43:16,400 --> 06:43:22,320
const brackets equals use state
7875
06:43:20,638 --> 06:43:25,520
brackets.
7876
06:43:22,320 --> 06:43:28,958
And this state will save the cart,
7877
06:43:25,520 --> 06:43:31,760
comma, and the updater function set
7878
06:43:28,958 --> 06:43:33,760
capital C cart.
7879
06:43:31,760 --> 06:43:37,520
And let's set the starting value of the
7880
06:43:33,760 --> 06:43:40,718
cart to an empty array. And we'll scroll
7881
06:43:37,520 --> 06:43:43,120
down. And now once we get the cart from
7882
06:43:40,718 --> 06:43:46,160
the back end, we're going to save it in
7883
06:43:43,120 --> 06:43:48,478
the state. So instead of console.log,
7884
06:43:46,160 --> 06:43:52,160
we're going to use the updater function
7885
06:43:48,478 --> 06:43:55,680
set cart. And now the cart from the back
7886
06:43:52,160 --> 06:43:57,920
end will be saved inside this variable.
7887
06:43:55,680 --> 06:44:00,798
And remember that the quantity for the
7888
06:43:57,920 --> 06:44:03,440
cart is inside the header. So we need to
7889
06:44:00,798 --> 06:44:07,440
pass the cart into the header using a
7890
06:44:03,440 --> 06:44:10,400
prop. So here we'll add a prop. cart
7891
06:44:07,440 --> 06:44:12,558
equals curly brackets and we'll give it
7892
06:44:10,400 --> 06:44:14,958
the cart.
7893
06:44:12,558 --> 06:44:18,000
Let's save this file
7894
06:44:14,958 --> 06:44:20,958
and we'll go into our code and go to
7895
06:44:18,000 --> 06:44:22,478
components header.jsx
7896
06:44:20,958 --> 06:44:25,360
and then inside the header we're going
7897
06:44:22,478 --> 06:44:27,280
to get the cart from the props. To do
7898
06:44:25,360 --> 06:44:31,120
that at the top, we'll type curly
7899
06:44:27,280 --> 06:44:34,638
brackets and the cart. As a reminder,
7900
06:44:31,120 --> 06:44:36,478
this is a shortcut for this code.
7901
06:44:34,638 --> 06:44:39,680
And now we're going to calculate the
7902
06:44:36,478 --> 06:44:43,760
total quantity in this cart. So at the
7903
06:44:39,680 --> 06:44:47,280
top, let's create a variable. Let total
7904
06:44:43,760 --> 06:44:50,718
quantity equals zero to start. And then
7905
06:44:47,280 --> 06:44:54,478
we'll loop through the cart using cart
7906
06:44:50,718 --> 06:44:57,478
for each brackets. And we'll give it a
7907
06:44:54,478 --> 06:44:57,478
function.
7908
06:44:58,000 --> 06:45:02,400
And the inner function will get one
7909
06:45:00,000 --> 06:45:06,798
parameter which is each item in the
7910
06:45:02,400 --> 06:45:09,520
cart. So, we'll call this the cart item.
7911
06:45:06,798 --> 06:45:11,040
And then each cart item has a quantity.
7912
06:45:09,520 --> 06:45:15,120
So, we're going to add that to this
7913
06:45:11,040 --> 06:45:21,718
total quantity up here. We'll do total
7914
06:45:15,120 --> 06:45:21,718
quantity plus equal the cart item.
7915
06:45:22,160 --> 06:45:28,798
And now we can scroll down
7916
06:45:25,680 --> 06:45:30,718
till we see the cart quantity down here.
7917
06:45:28,798 --> 06:45:35,520
And instead of three every time, we're
7918
06:45:30,718 --> 06:45:37,760
going to insert the total quantity
7919
06:45:35,520 --> 06:45:40,878
and save.
7920
06:45:37,760 --> 06:45:43,120
And now if we look at our project
7921
06:45:40,878 --> 06:45:46,400
now, it will display the total quantity
7922
06:45:43,120 --> 06:45:48,878
using data from the back end. So now
7923
06:45:46,400 --> 06:45:51,840
let's go back to our code.
7924
06:45:48,878 --> 06:45:55,200
And if we scroll up, you might see an
7925
06:45:51,840 --> 06:45:57,440
error here for props validation. Again,
7926
06:45:55,200 --> 06:46:00,000
this is an older feature of React. So,
7927
06:45:57,440 --> 06:46:04,718
we can disable this check by going into
7928
06:46:00,000 --> 06:46:07,680
our files and opening eslint.config.js.
7929
06:46:04,718 --> 06:46:10,400
And then we'll scroll down inside this
7930
06:46:07,680 --> 06:46:14,160
rules object. We're going to add a
7931
06:46:10,400 --> 06:46:18,718
string react/prop-
7932
06:46:14,160 --> 06:46:22,080
types colon the string off. So, make
7933
06:46:18,718 --> 06:46:24,000
sure it's inside this rules object. We
7934
06:46:22,080 --> 06:46:26,558
can save.
7935
06:46:24,000 --> 06:46:29,360
And now if we go back to the header, the
7936
06:46:26,558 --> 06:46:32,160
props validation error should be gone.
7937
06:46:29,360 --> 06:46:34,878
So now let's save this code into git.
7938
06:46:32,160 --> 06:46:38,958
We'll open the git section and we'll
7939
06:46:34,878 --> 06:46:46,600
create a message get cart from the back
7940
06:46:38,958 --> 06:46:46,600
end and display quantity and commit.
7941
06:46:49,280 --> 06:46:57,920
Next, if we go back to the homepage, so
7942
06:46:52,320 --> 06:47:00,798
in our files, go to pages and homepage
7943
06:46:57,920 --> 06:47:03,200
and scroll up. Notice that for each
7944
06:47:00,798 --> 06:47:05,760
request, we're sending it to localhost
7945
06:47:03,200 --> 06:47:08,240
3000. So, we can actually set up a
7946
06:47:05,760 --> 06:47:11,760
shortcut in Vit. So, we don't have to
7947
06:47:08,240 --> 06:47:15,718
type localhost 3000 every time. So, we
7948
06:47:11,760 --> 06:47:15,718
can actually remove these
7949
06:47:16,878 --> 06:47:21,760
and save.
7950
06:47:19,440 --> 06:47:26,360
And then we're going to go to our files
7951
06:47:21,760 --> 06:47:26,360
and we're going to open vit.config.js.
7952
06:47:27,040 --> 06:47:33,840
And inside here we're going to add the
7953
06:47:29,360 --> 06:47:37,200
property server colon and an object. And
7954
06:47:33,840 --> 06:47:39,920
inside we'll add the property proxy
7955
06:47:37,200 --> 06:47:44,718
colon another object.
7956
06:47:39,920 --> 06:47:47,120
And in here we'll add a string slash API
7957
06:47:44,718 --> 06:47:50,160
colon. And we're going to give this an
7958
06:47:47,120 --> 06:47:55,120
object. And inside we'll add a target
7959
06:47:50,160 --> 06:48:01,200
property colon and the string http colon
7960
06:47:55,120 --> 06:48:03,920
double slash localhost colon 3000.
7961
06:48:01,200 --> 06:48:06,558
So this is called a server proxy
7962
06:48:03,920 --> 06:48:10,240
configuration. This just means that if
7963
06:48:06,558 --> 06:48:12,638
the URL path starts with / API like we
7964
06:48:10,240 --> 06:48:15,760
had it before, the request will
7965
06:48:12,638 --> 06:48:18,478
automatically go to localhost 3000. So
7966
06:48:15,760 --> 06:48:20,558
we don't have to type it every time.
7967
06:48:18,478 --> 06:48:23,040
Another thing we can set up is when we
7968
06:48:20,558 --> 06:48:25,920
load images, we also want to load them
7969
06:48:23,040 --> 06:48:28,160
from the back end. Remember the back end
7970
06:48:25,920 --> 06:48:31,280
should manage the data including the
7971
06:48:28,160 --> 06:48:35,440
product images. So in here we'll also
7972
06:48:31,280 --> 06:48:39,760
add a comma and add a string slash
7973
06:48:35,440 --> 06:48:41,680
images colon and an object
7974
06:48:39,760 --> 06:48:45,280
and we'll give it the same target. So
7975
06:48:41,680 --> 06:48:47,200
we'll select this and copy and in here
7976
06:48:45,280 --> 06:48:50,000
we'll paste
7977
06:48:47,200 --> 06:48:52,320
and save.
7978
06:48:50,000 --> 06:48:55,760
And we need to do one more thing. We're
7979
06:48:52,320 --> 06:48:59,440
going to go inside index.html HTML and
7980
06:48:55,760 --> 06:49:02,160
in the head we're going to add less than
7981
06:48:59,440 --> 06:49:05,520
base slashgreater than and we're going
7982
06:49:02,160 --> 06:49:08,558
to give this an attribute href equals
7983
06:49:05,520 --> 06:49:10,958
double quotes and a slash.
7984
06:49:08,558 --> 06:49:14,638
So this adds slash in front of any
7985
06:49:10,958 --> 06:49:16,958
relative urls. All the image URLs start
7986
06:49:14,638 --> 06:49:20,000
with images and this will convert them
7987
06:49:16,958 --> 06:49:23,760
into slash images so that our redirect
7988
06:49:20,000 --> 06:49:27,280
will work. Let's save
7989
06:49:23,760 --> 06:49:29,200
and we'll go back to the vit config.
7990
06:49:27,280 --> 06:49:32,000
So now when we get images, we're going
7991
06:49:29,200 --> 06:49:33,760
to get them from the back end. And now
7992
06:49:32,000 --> 06:49:36,160
because we changed the vit
7993
06:49:33,760 --> 06:49:38,320
configuration, it's a good idea to stop
7994
06:49:36,160 --> 06:49:43,478
the server with control c and then
7995
06:49:38,320 --> 06:49:43,478
restart the server using mpm rundev.
7996
06:49:43,920 --> 06:49:48,400
And now let's check the website to make
7997
06:49:46,558 --> 06:49:50,958
sure everything is working. So I'll just
7998
06:49:48,400 --> 06:49:53,360
refresh this to make sure. And
7999
06:49:50,958 --> 06:49:55,600
everything still looks good. So now
8000
06:49:53,360 --> 06:49:59,360
we'll go back to our code and we'll save
8001
06:49:55,600 --> 06:50:02,638
these changes into git. Inside git,
8002
06:49:59,360 --> 06:50:08,558
we're going to type the message add
8003
06:50:02,638 --> 06:50:11,120
server proxy config for slash API and
8004
06:50:08,558 --> 06:50:14,600
slash images
8005
06:50:11,120 --> 06:50:14,600
and commit.
8006
06:50:17,760 --> 06:50:22,000
Next, we're going to move on to the
8007
06:50:19,520 --> 06:50:25,280
checkout page. So, let's open the
8008
06:50:22,000 --> 06:50:27,200
website and we'll click the cart icon in
8009
06:50:25,280 --> 06:50:29,440
the top right
8010
06:50:27,200 --> 06:50:32,160
and we're going to work on this page.
8011
06:50:29,440 --> 06:50:34,958
So, on this page, we also need the cart
8012
06:50:32,160 --> 06:50:38,080
to generate this HTML.
8013
06:50:34,958 --> 06:50:41,120
So, we'll go back to our code
8014
06:50:38,080 --> 06:50:44,558
and we'll open our files and we're going
8015
06:50:41,120 --> 06:50:47,760
to open checkout page.jsx.
8016
06:50:44,558 --> 06:50:50,400
And now inside here we could do another
8017
06:50:47,760 --> 06:50:53,760
axios.get/appi/cart
8018
06:50:50,400 --> 06:50:56,558
again. But remember we already loaded
8019
06:50:53,760 --> 06:50:59,600
the cart on the homepage. It does not
8020
06:50:56,558 --> 06:51:02,240
make sense to reload the cart whenever
8021
06:50:59,600 --> 06:51:05,840
we switch pages. We just need to load
8022
06:51:02,240 --> 06:51:07,680
the cart once for the entire website. To
8023
06:51:05,840 --> 06:51:09,200
do that, we're going to go to the
8024
06:51:07,680 --> 06:51:11,120
homepage
8025
06:51:09,200 --> 06:51:13,440
and we're going to share this cart
8026
06:51:11,120 --> 06:51:16,718
between the two pages using the
8027
06:51:13,440 --> 06:51:19,120
technique lifting the state up. As a
8028
06:51:16,718 --> 06:51:22,878
review, this means we move the cart
8029
06:51:19,120 --> 06:51:26,080
state up to an outer component. In this
8030
06:51:22,878 --> 06:51:28,558
case, the outer component is the app
8031
06:51:26,080 --> 06:51:31,360
component here, which contains a
8032
06:51:28,558 --> 06:51:35,440
homepage. So, we're going to lift the
8033
06:51:31,360 --> 06:51:37,920
cart up into the app component. And from
8034
06:51:35,440 --> 06:51:40,798
here, we can share the cart between the
8035
06:51:37,920 --> 06:51:43,920
homepage and the checkout page. So,
8036
06:51:40,798 --> 06:51:48,718
first, let's go into the homepage again.
8037
06:51:43,920 --> 06:51:51,840
We're going to select this cart and cut
8038
06:51:48,718 --> 06:51:55,840
and go back to the app. And we're going
8039
06:51:51,840 --> 06:51:57,760
to lift it up in here by pasting it. And
8040
06:51:55,840 --> 06:52:00,718
we also need the code that loads the
8041
06:51:57,760 --> 06:52:06,160
cart. So in the homepage,
8042
06:52:00,718 --> 06:52:08,638
we're going to select this code and cut
8043
06:52:06,160 --> 06:52:11,200
and save
8044
06:52:08,638 --> 06:52:14,160
inside app.jsx.
8045
06:52:11,200 --> 06:52:16,000
Below this, we're going to paste.
8046
06:52:14,160 --> 06:52:19,120
And remember, we need to put this inside
8047
06:52:16,000 --> 06:52:22,718
a use effect so that it only runs once.
8048
06:52:19,120 --> 06:52:24,878
So use effect brackets, give it a
8049
06:52:22,718 --> 06:52:28,558
function,
8050
06:52:24,878 --> 06:52:31,280
and a empty array at the end. And we're
8051
06:52:28,558 --> 06:52:34,920
going to select it and cut. And inside
8052
06:52:31,280 --> 06:52:34,920
here, paste.
8053
06:52:34,958 --> 06:52:40,478
And I'll select these lines and press
8054
06:52:36,878 --> 06:52:43,680
tab to reformat them. And next, we'll
8055
06:52:40,478 --> 06:52:47,200
need to import this code. So at the top,
8056
06:52:43,680 --> 06:52:52,478
we're going to import curly brackets,
8057
06:52:47,200 --> 06:52:55,760
use state, and use effect
8058
06:52:52,478 --> 06:52:58,478
from React.
8059
06:52:55,760 --> 06:53:03,040
And we'll also import axios. So at the
8060
06:52:58,478 --> 06:53:06,798
top again, we're going to import axios
8061
06:53:03,040 --> 06:53:08,958
from the string axios.
8062
06:53:06,798 --> 06:53:10,798
So now that we have the cart, we're
8063
06:53:08,958 --> 06:53:13,840
going to share it between these two
8064
06:53:10,798 --> 06:53:16,798
pages using a prop. So for the homepage,
8065
06:53:13,840 --> 06:53:20,080
we'll add a prop. Cart equals curly
8066
06:53:16,798 --> 06:53:22,558
brackets and we'll give it the cart. And
8067
06:53:20,080 --> 06:53:24,878
for the checkout page as well, cart
8068
06:53:22,558 --> 06:53:27,200
equals curly brackets. and we'll save
8069
06:53:24,878 --> 06:53:29,760
the cart.
8070
06:53:27,200 --> 06:53:32,240
Let's save this file.
8071
06:53:29,760 --> 06:53:34,240
And we'll go into the homepage.
8072
06:53:32,240 --> 06:53:36,240
And at the top, we need to access the
8073
06:53:34,240 --> 06:53:41,200
cart from the props using curly
8074
06:53:36,240 --> 06:53:42,878
brackets, the cart, and save.
8075
06:53:41,200 --> 06:53:44,798
So, the code should work the same as
8076
06:53:42,878 --> 06:53:47,760
before, except we're just getting the
8077
06:53:44,798 --> 06:53:49,920
cart from the props. Now, let's open our
8078
06:53:47,760 --> 06:53:53,120
website to make sure it works. We're
8079
06:53:49,920 --> 06:53:55,760
going to go to the homepage and refresh.
8080
06:53:53,120 --> 06:53:59,120
And that looks good. We'll go back to
8081
06:53:55,760 --> 06:54:02,080
our code. So now that we lifted the
8082
06:53:59,120 --> 06:54:04,400
state up, we can just load the card once
8083
06:54:02,080 --> 06:54:07,440
for the entire website instead of
8084
06:54:04,400 --> 06:54:10,320
reloading it on every page. We'll open
8085
06:54:07,440 --> 06:54:16,558
the get section and we'll give it the
8086
06:54:10,320 --> 06:54:22,600
message lift cart up and share it
8087
06:54:16,558 --> 06:54:22,600
between pages and commit.
8088
06:54:25,840 --> 06:54:31,520
Next, we're going to use the cart to
8089
06:54:28,240 --> 06:54:33,440
generate the HTML for the checkout page.
8090
06:54:31,520 --> 06:54:35,440
So, first make sure that we pass the
8091
06:54:33,440 --> 06:54:38,240
cart to the checkout page using this
8092
06:54:35,440 --> 06:54:42,080
prop. And now we're going to open the
8093
06:54:38,240 --> 06:54:44,160
checkout page, which is here. And we're
8094
06:54:42,080 --> 06:54:48,160
going to access the cart from the props
8095
06:54:44,160 --> 06:54:50,718
using curly brackets and the cart.
8096
06:54:48,160 --> 06:54:54,320
And I will use this cart to generate the
8097
06:54:50,718 --> 06:54:57,200
HTML down here. Now inside this code,
8098
06:54:54,320 --> 06:54:59,120
we're going to press Ctrl+F to find some
8099
06:54:57,200 --> 06:55:03,120
code. And we're going to search for
8100
06:54:59,120 --> 06:55:05,440
cart- item-container.
8101
06:55:03,120 --> 06:55:08,240
And here we're going to collapse this
8102
06:55:05,440 --> 06:55:10,160
element. And we'll collapse this element
8103
06:55:08,240 --> 06:55:13,360
as well.
8104
06:55:10,160 --> 06:55:16,400
So each of these represents a cart item
8105
06:55:13,360 --> 06:55:19,920
on the page. So let's generate this
8106
06:55:16,400 --> 06:55:24,798
using the cart. Above this we're going
8107
06:55:19,920 --> 06:55:27,440
to insert cart map brackets and we'll
8108
06:55:24,798 --> 06:55:30,400
give it a function brackets arrow and
8109
06:55:27,440 --> 06:55:33,200
curly brackets. And this inner function
8110
06:55:30,400 --> 06:55:36,240
gets one parameter which is the cart
8111
06:55:33,200 --> 06:55:40,638
item. And for each item we're going to
8112
06:55:36,240 --> 06:55:42,798
return brackets some HTML.
8113
06:55:40,638 --> 06:55:45,840
So for each cart item, we want to
8114
06:55:42,798 --> 06:55:49,680
generate this HTML. So let's just select
8115
06:55:45,840 --> 06:55:52,160
it and copy. And inside here, we're
8116
06:55:49,680 --> 06:55:53,920
going to paste.
8117
06:55:52,160 --> 06:55:56,958
And to fix the formatting, we're going
8118
06:55:53,920 --> 06:56:00,638
to press control or commandshiftp
8119
06:55:56,958 --> 06:56:03,760
and run format document.
8120
06:56:00,638 --> 06:56:05,920
Now let's scroll up.
8121
06:56:03,760 --> 06:56:08,798
And remember, when we loop through an
8122
06:56:05,920 --> 06:56:11,600
array, each element needs a key
8123
06:56:08,798 --> 06:56:14,240
property.
8124
06:56:11,600 --> 06:56:16,400
Each cart item has a product ID which is
8125
06:56:14,240 --> 06:56:20,000
unique. So we're going to save the key
8126
06:56:16,400 --> 06:56:21,760
as cart item.
8127
06:56:20,000 --> 06:56:24,320
ID.
8128
06:56:21,760 --> 06:56:26,240
And now instead of showing the same
8129
06:56:24,320 --> 06:56:29,440
product every time, we're going to
8130
06:56:26,240 --> 06:56:31,920
insert the details from each cart item.
8131
06:56:29,440 --> 06:56:34,878
However, there is a problem here.
8132
06:56:31,920 --> 06:56:38,240
Remember that the cart looks like this.
8133
06:56:34,878 --> 06:56:40,478
It doesn't have any product data on it.
8134
06:56:38,240 --> 06:56:44,080
To fix this, we're going to go to the
8135
06:56:40,478 --> 06:56:47,120
app component where we load the cart.
8136
06:56:44,080 --> 06:56:52,240
And at the end of this URL, we can add
8137
06:56:47,120 --> 06:56:55,760
question mark expand equals product.
8138
06:56:52,240 --> 06:56:58,160
So this is called a query parameter. A
8139
06:56:55,760 --> 06:57:00,958
query parameter lets us add additional
8140
06:56:58,160 --> 06:57:03,440
information to our request. When the
8141
06:57:00,958 --> 06:57:06,080
backend receives this query parameter,
8142
06:57:03,440 --> 06:57:09,520
it's going to add product details to the
8143
06:57:06,080 --> 06:57:12,718
cart. So, it's going to expand the cart
8144
06:57:09,520 --> 06:57:14,958
with product details. So, now let's save
8145
06:57:12,718 --> 06:57:17,440
this file
8146
06:57:14,958 --> 06:57:20,160
and we're going to open the website and
8147
06:57:17,440 --> 06:57:22,400
we're going to go to the checkout page
8148
06:57:20,160 --> 06:57:25,760
and we're going to open the console. So,
8149
06:57:22,400 --> 06:57:27,840
we'll rightclick inspect
8150
06:57:25,760 --> 06:57:30,718
and inside the console we're going to
8151
06:57:27,840 --> 06:57:32,638
open the network tab.
8152
06:57:30,718 --> 06:57:36,160
So, I'm going to remove any search that
8153
06:57:32,638 --> 06:57:38,638
I have. And in here, we can see all the
8154
06:57:36,160 --> 06:57:41,440
requests that we make to the back end
8155
06:57:38,638 --> 06:57:43,120
and the responses. So, by the way,
8156
06:57:41,440 --> 06:57:45,920
requests only show up here if the
8157
06:57:43,120 --> 06:57:48,400
console was open before the request. So,
8158
06:57:45,920 --> 06:57:50,400
we may need to refresh
8159
06:57:48,400 --> 06:57:52,638
to get the request to show up in the
8160
06:57:50,400 --> 06:57:56,000
network tab. And now we can see our
8161
06:57:52,638 --> 06:57:58,000
request to cart- items. If you don't see
8162
06:57:56,000 --> 06:58:01,120
it, you may need to search for it up
8163
06:57:58,000 --> 06:58:04,798
here by typing cart. And we have it
8164
06:58:01,120 --> 06:58:08,240
here. So we can open this by clicking it
8165
06:58:04,798 --> 06:58:10,558
and then go into the response tab. And
8166
06:58:08,240 --> 06:58:12,878
here you can see the response from the
8167
06:58:10,558 --> 06:58:16,478
back end. So now you can see that each
8168
06:58:12,878 --> 06:58:18,160
cart item has a property called product.
8169
06:58:16,478 --> 06:58:22,400
And this contains all the product
8170
06:58:18,160 --> 06:58:26,240
details that we need for this page.
8171
06:58:22,400 --> 06:58:27,920
So now let's go back to our code.
8172
06:58:26,240 --> 06:58:29,680
And we're going to go to the checkout
8173
06:58:27,920 --> 06:58:32,558
page.
8174
06:58:29,680 --> 06:58:35,280
And now instead of the socks every time,
8175
06:58:32,558 --> 06:58:40,840
we're going to remove this
8176
06:58:35,280 --> 06:58:40,840
and insert the cart item.
8177
06:58:42,638 --> 06:58:48,478
And we'll also replace the image. So
8178
06:58:45,040 --> 06:58:51,120
we'll remove this and insert the cart
8179
06:58:48,478 --> 06:58:54,160
item.
8180
06:58:51,120 --> 06:58:57,440
image and down here we're going to
8181
06:58:54,160 --> 06:58:59,200
insert the price. Now, if we go to the
8182
06:58:57,440 --> 06:59:02,000
homepage,
8183
06:58:59,200 --> 06:59:05,200
remember that in the homepage, we wrote
8184
06:59:02,000 --> 06:59:07,120
some code here to display the price. So,
8185
06:59:05,200 --> 06:59:10,798
let's actually move this code into a
8186
06:59:07,120 --> 06:59:13,120
function so we can reuse it. Inside our
8187
06:59:10,798 --> 06:59:16,160
files, we're going to click the source
8188
06:59:13,120 --> 06:59:21,360
folder and we'll create a new folder and
8189
06:59:16,160 --> 06:59:23,200
let's call it utils for utilities.
8190
06:59:21,360 --> 06:59:26,000
And inside the utmost folder, we'll
8191
06:59:23,200 --> 06:59:29,000
create a new file and we'll name it
8192
06:59:26,000 --> 06:59:29,000
money.js.
8193
06:59:29,120 --> 06:59:34,400
So this file contains utilities or code
8194
06:59:31,680 --> 06:59:37,120
that helps us work with money. So in
8195
06:59:34,400 --> 06:59:41,520
here, let's create a function and we'll
8196
06:59:37,120 --> 06:59:43,280
name it format money brackets and curly
8197
06:59:41,520 --> 06:59:45,200
brackets.
8198
06:59:43,280 --> 06:59:48,160
And we're going to copy the code from
8199
06:59:45,200 --> 06:59:50,958
the homepage. So, inside the homepage,
8200
06:59:48,160 --> 06:59:53,040
we're going to take all of this code and
8201
06:59:50,958 --> 06:59:56,558
copy it.
8202
06:59:53,040 --> 06:59:58,478
And inside money.js, inside here, we'll
8203
06:59:56,558 --> 07:00:00,638
paste it.
8204
06:59:58,478 --> 07:00:03,920
And now we're going to turn this into a
8205
07:00:00,638 --> 07:00:06,240
string. At the front, we'll type backick
8206
07:00:03,920 --> 07:00:08,160
and at the end a back tick to create a
8207
07:00:06,240 --> 07:00:10,558
template string. And remember in
8208
07:00:08,160 --> 07:00:14,000
template strings to insert something, we
8209
07:00:10,558 --> 07:00:15,760
need dollar curly brackets. And now,
8210
07:00:14,000 --> 07:00:18,080
instead of getting the product.pricent
8211
07:00:15,760 --> 07:00:20,000
price cents. We're going to pass it into
8212
07:00:18,080 --> 07:00:24,160
this function. So, let's name the
8213
07:00:20,000 --> 07:00:27,360
parameter amount cents. And we're going
8214
07:00:24,160 --> 07:00:31,040
to remove this and replace it with
8215
07:00:27,360 --> 07:00:33,680
amount cents. So, this function lets us
8216
07:00:31,040 --> 07:00:37,680
convert any amount cents into this
8217
07:00:33,680 --> 07:00:40,320
format. At the end, let's return this
8218
07:00:37,680 --> 07:00:42,878
value from the function. and we'll
8219
07:00:40,320 --> 07:00:46,478
export it so we can use it in other
8220
07:00:42,878 --> 07:00:49,520
files and save.
8221
07:00:46,478 --> 07:00:53,920
So now we can go back to the homepage
8222
07:00:49,520 --> 07:00:58,080
and at the top we're going to import
8223
07:00:53,920 --> 07:01:01,840
curly brackets the format money function
8224
07:00:58,080 --> 07:01:04,320
that we just created from a string and
8225
07:01:01,840 --> 07:01:06,240
we're going to locate the utils folder.
8226
07:01:04,320 --> 07:01:10,320
So, we'll go out of this folder with dot
8227
07:01:06,240 --> 07:01:14,400
dot slash utils slashmoney.
8228
07:01:10,320 --> 07:01:17,040
And now we'll scroll back down
8229
07:01:14,400 --> 07:01:21,280
and instead of this code, we're going to
8230
07:01:17,040 --> 07:01:25,280
remove it and insert format money
8231
07:01:21,280 --> 07:01:28,718
brackets and we'll give it product.pric
8232
07:01:25,280 --> 07:01:30,798
sets and save.
8233
07:01:28,718 --> 07:01:33,040
And now let's reuse this function in the
8234
07:01:30,798 --> 07:01:37,200
checkout page. So, we'll open the
8235
07:01:33,040 --> 07:01:41,760
checkout page, and at the top, we're
8236
07:01:37,200 --> 07:01:46,638
going to import curly brackets, the
8237
07:01:41,760 --> 07:01:49,280
format money function from a string, and
8238
07:01:46,638 --> 07:01:51,600
we're going to locate this file. And for
8239
07:01:49,280 --> 07:01:55,600
me, I'll go out of this folder using dot
8240
07:01:51,600 --> 07:01:57,520
dot slash utils/money.
8241
07:01:55,600 --> 07:01:59,600
Now, if you did the exercises from the
8242
07:01:57,520 --> 07:02:02,400
previous lesson, this file might be
8243
07:01:59,600 --> 07:02:04,638
inside a folder called checkout. In that
8244
07:02:02,400 --> 07:02:07,680
case, make sure you have two dot dot
8245
07:02:04,638 --> 07:02:11,040
slash in front. And now we're going to
8246
07:02:07,680 --> 07:02:13,760
scroll back down
8247
07:02:11,040 --> 07:02:16,478
and we're going to replace this money
8248
07:02:13,760 --> 07:02:20,000
with curly brackets.
8249
07:02:16,478 --> 07:02:24,000
Format money brackets. And we're going
8250
07:02:20,000 --> 07:02:27,360
to give it the cart item.
8251
07:02:24,000 --> 07:02:30,878
price sets and we'll replace the
8252
07:02:27,360 --> 07:02:37,120
quantity. So, insert
8253
07:02:30,878 --> 07:02:40,478
cart item dot quantity and save.
8254
07:02:37,120 --> 07:02:43,440
And now if we go back to the website,
8255
07:02:40,478 --> 07:02:46,160
now we are generating these cart items
8256
07:02:43,440 --> 07:02:49,520
using data from the back end. And this
8257
07:02:46,160 --> 07:02:52,558
was the default HTML we had before. So
8258
07:02:49,520 --> 07:02:55,040
now we can go back to our code and we
8259
07:02:52,558 --> 07:02:59,680
can scroll down
8260
07:02:55,040 --> 07:03:02,400
and we can remove the default HTML
8261
07:02:59,680 --> 07:03:04,638
and save.
8262
07:03:02,400 --> 07:03:08,558
So now let's save these changes into
8263
07:03:04,638 --> 07:03:11,360
git. We'll open the get section
8264
07:03:08,558 --> 07:03:12,878
and at the top we'll type the message
8265
07:03:11,360 --> 07:03:21,080
generate
8266
07:03:12,878 --> 07:03:21,080
HTML for the cart items and commit.
8267
07:03:23,680 --> 07:03:28,798
Next, we're going to generate the HTML
8268
07:03:25,920 --> 07:03:31,600
for the delivery options. So, we'll go
8269
07:03:28,798 --> 07:03:34,478
back to the checkout page and we'll
8270
07:03:31,600 --> 07:03:36,878
scroll to the top of this component. And
8271
07:03:34,478 --> 07:03:40,878
here we'll load the delivery options
8272
07:03:36,878 --> 07:03:45,840
from the back end. So first let's import
8273
07:03:40,878 --> 07:03:50,240
Axios. At the top we'll import Axios
8274
07:03:45,840 --> 07:03:54,320
from the Axios npm package. And we'll
8275
07:03:50,240 --> 07:03:58,080
also need import curly brackets. Use
8276
07:03:54,320 --> 07:04:00,718
state and use effect to save the
8277
07:03:58,080 --> 07:04:02,558
delivery options. So import it from
8278
07:04:00,718 --> 07:04:05,200
React.
8279
07:04:02,558 --> 07:04:07,040
And now inside the component
8280
07:04:05,200 --> 07:04:10,000
we'll create some state to save the
8281
07:04:07,040 --> 07:04:15,200
delivery options const
8282
07:04:10,000 --> 07:04:19,280
bracket equals use state and brackets
8283
07:04:15,200 --> 07:04:22,478
and we'll name this delivery options
8284
07:04:19,280 --> 07:04:25,120
colon and the updater function set
8285
07:04:22,478 --> 07:04:27,840
delivery options
8286
07:04:25,120 --> 07:04:30,400
and we'll set the starting value to an
8287
07:04:27,840 --> 07:04:32,958
empty array. Next, we'll get the
8288
07:04:30,400 --> 07:04:37,200
delivery options from the back end. So
8289
07:04:32,958 --> 07:04:39,520
here we'll do use effect brackets and
8290
07:04:37,200 --> 07:04:42,080
we'll give it a function brackets arrow
8291
07:04:39,520 --> 07:04:44,558
and curly brackets. And at the end we'll
8292
07:04:42,080 --> 07:04:47,280
give it a comma and an empty dependency
8293
07:04:44,558 --> 07:04:50,160
array so that this code only runs once
8294
07:04:47,280 --> 07:04:52,400
on this page. And inside here we're
8295
07:04:50,160 --> 07:04:56,000
going to call axials.get
8296
07:04:52,400 --> 07:05:00,080
get brackets and the URL to get the
8297
07:04:56,000 --> 07:05:03,080
delivery options is slash API slashd
8298
07:05:00,080 --> 07:05:03,080
delivery-options
8299
07:05:03,440 --> 07:05:08,240
and then we're going to wait for the
8300
07:05:04,878 --> 07:05:11,200
response to come back using then
8301
07:05:08,240 --> 07:05:14,478
brackets and give it a function brackets
8302
07:05:11,200 --> 07:05:16,400
arrow and curly brackets and inside the
8303
07:05:14,478 --> 07:05:20,638
function the response from the back end
8304
07:05:16,400 --> 07:05:22,638
will be saved as the response parameter.
8305
07:05:20,638 --> 07:05:24,478
So once we get this response, we're
8306
07:05:22,638 --> 07:05:30,000
going to save it in the delivery options
8307
07:05:24,478 --> 07:05:31,840
up here using set delivery options
8308
07:05:30,000 --> 07:05:34,718
brackets
8309
07:05:31,840 --> 07:05:38,080
response.
8310
07:05:34,718 --> 07:05:40,160
Now let's save this code and we're going
8311
07:05:38,080 --> 07:05:43,040
to check the network tab to see if it
8312
07:05:40,160 --> 07:05:45,760
works. So, we'll open the console and
8313
07:05:43,040 --> 07:05:48,400
we'll open the network tab. And we might
8314
07:05:45,760 --> 07:05:51,440
need to refresh to make sure we get the
8315
07:05:48,400 --> 07:05:54,958
request and we'll remove this and search
8316
07:05:51,440 --> 07:05:57,200
for delivery options. And we'll click
8317
07:05:54,958 --> 07:06:00,638
one of these and then look at the
8318
07:05:57,200 --> 07:06:03,280
response tab. So, the backend responded
8319
07:06:00,638 --> 07:06:05,280
with three delivery options. Each
8320
07:06:03,280 --> 07:06:08,080
delivery option has the number of days
8321
07:06:05,280 --> 07:06:12,240
it takes to deliver and the price in
8322
07:06:08,080 --> 07:06:14,400
cents. However, if we open our website,
8323
07:06:12,240 --> 07:06:17,920
notice that each delivery option also
8324
07:06:14,400 --> 07:06:20,638
has a estimated delivery time. So, we
8325
07:06:17,920 --> 07:06:23,360
could use this delivery days to
8326
07:06:20,638 --> 07:06:25,040
calculate this date on the front end.
8327
07:06:23,360 --> 07:06:28,080
But remember, it's generally the
8328
07:06:25,040 --> 07:06:29,920
backend's job to manage the data and do
8329
07:06:28,080 --> 07:06:33,440
these calculations.
8330
07:06:29,920 --> 07:06:36,240
So, if we go back to our code,
8331
07:06:33,440 --> 07:06:39,360
this URL path also supports a query
8332
07:06:36,240 --> 07:06:43,600
parameter called expand. So at the end
8333
07:06:39,360 --> 07:06:47,200
we'll add question mark expand equals
8334
07:06:43,600 --> 07:06:50,000
and I called it estimated
8335
07:06:47,200 --> 07:06:52,478
delivery time.
8336
07:06:50,000 --> 07:06:54,400
So when the back end sees this expand
8337
07:06:52,478 --> 07:06:56,878
parameter it will calculate the
8338
07:06:54,400 --> 07:07:01,440
estimated delivery time for us and then
8339
07:06:56,878 --> 07:07:04,400
give it back in our response. If we save
8340
07:07:01,440 --> 07:07:07,200
and check the console
8341
07:07:04,400 --> 07:07:10,558
and refresh
8342
07:07:07,200 --> 07:07:12,958
and open the response and now you'll see
8343
07:07:10,558 --> 07:07:16,718
that each delivery option now has an
8344
07:07:12,958 --> 07:07:19,280
estimated delivery time in milliseconds.
8345
07:07:16,718 --> 07:07:22,320
So now we'll go back to our code and we
8346
07:07:19,280 --> 07:07:24,080
have what we need to display the date.
8347
07:07:22,320 --> 07:07:26,718
As you can see it's generally the
8348
07:07:24,080 --> 07:07:29,280
backend's job to manage the data and to
8349
07:07:26,718 --> 07:07:32,400
do the calculations. And now the front
8350
07:07:29,280 --> 07:07:35,680
end's job is to present that data in an
8351
07:07:32,400 --> 07:07:38,160
easy to read way on the website.
8352
07:07:35,680 --> 07:07:41,200
So now let's use these delivery options
8353
07:07:38,160 --> 07:07:43,600
to generate the HTML. We're going to
8354
07:07:41,200 --> 07:07:45,760
click in this file and press Ctrl+F to
8355
07:07:43,600 --> 07:07:49,280
search for code. And we'll search for
8356
07:07:45,760 --> 07:07:51,920
delivery dash options.
8357
07:07:49,280 --> 07:07:54,718
And inside here we should see an element
8358
07:07:51,920 --> 07:07:57,760
with a class name delivery option. So
8359
07:07:54,718 --> 07:08:00,478
we're going to collapse this element and
8360
07:07:57,760 --> 07:08:02,638
also collapse the other delivery options
8361
07:08:00,478 --> 07:08:05,680
and each of these represents one
8362
07:08:02,638 --> 07:08:08,958
delivery option on the page. So let's
8363
07:08:05,680 --> 07:08:12,320
generate this HTML using React. Above
8364
07:08:08,958 --> 07:08:17,040
this we'll insert and we'll run the code
8365
07:08:12,320 --> 07:08:19,360
delivery options dot map brackets and
8366
07:08:17,040 --> 07:08:22,798
give it a function brackets arrow and
8367
07:08:19,360 --> 07:08:26,400
curly brackets and the parameter will be
8368
07:08:22,798 --> 07:08:30,240
each delivery option
8369
07:08:26,400 --> 07:08:33,360
and here we're going to return brackets
8370
07:08:30,240 --> 07:08:36,320
some HTML for each delivery option. So
8371
07:08:33,360 --> 07:08:39,520
we want to return this HTML. So we can
8372
07:08:36,320 --> 07:08:42,160
select it and copy. And inside here
8373
07:08:39,520 --> 07:08:44,080
we'll paste.
8374
07:08:42,160 --> 07:08:46,478
And we can fix the formatting using
8375
07:08:44,080 --> 07:08:50,558
control or commandshiftp
8376
07:08:46,478 --> 07:08:53,200
and press format document.
8377
07:08:50,558 --> 07:08:55,440
And let's scroll up. And as always, we
8378
07:08:53,200 --> 07:08:58,400
need a key prop whenever we're looping
8379
07:08:55,440 --> 07:09:01,600
through an array. So we'll add key
8380
07:08:58,400 --> 07:09:03,920
equals curly brackets. And each delivery
8381
07:09:01,600 --> 07:09:07,120
option has a unique ID. So we'll just
8382
07:09:03,920 --> 07:09:08,958
set it to the delivery option
8383
07:09:07,120 --> 07:09:10,958
ID.
8384
07:09:08,958 --> 07:09:13,680
Next, we're going to replace these
8385
07:09:10,958 --> 07:09:16,558
values here. So first we'll do the
8386
07:09:13,680 --> 07:09:19,360
delivery date. Currently, we only have
8387
07:09:16,558 --> 07:09:21,760
the delivery time in milliseconds. So
8388
07:09:19,360 --> 07:09:25,440
how do we display it as a date like
8389
07:09:21,760 --> 07:09:28,080
this? To do that, we can use another npm
8390
07:09:25,440 --> 07:09:29,920
package called Dayjs.
8391
07:09:28,080 --> 07:09:31,840
So, we're going to go to the terminal
8392
07:09:29,920 --> 07:09:34,958
and go to the tab that's running our
8393
07:09:31,840 --> 07:09:38,240
front end. And we can press Ctrl C to
8394
07:09:34,958 --> 07:09:42,558
stop the server. And we're going to npm
8395
07:09:38,240 --> 07:09:44,478
install dayjs. And we'll type at to give
8396
07:09:42,558 --> 07:09:46,478
it a version. And we're going to click
8397
07:09:44,478 --> 07:09:49,040
this link in the description to get the
8398
07:09:46,478 --> 07:09:51,760
version of day.js we're going to use.
8399
07:09:49,040 --> 07:09:55,520
And we'll copy that version and paste it
8400
07:09:51,760 --> 07:09:57,200
in here and press enter to install
8401
07:09:55,520 --> 07:10:00,798
day.js.
8402
07:09:57,200 --> 07:10:04,400
And after that we're going to npm rundev
8403
07:10:00,798 --> 07:10:07,280
to restart the server.
8404
07:10:04,400 --> 07:10:10,160
So the dayjs package helps us work with
8405
07:10:07,280 --> 07:10:13,440
dates and it helps us display a date in
8406
07:10:10,160 --> 07:10:15,760
a particular format. To use it, we're
8407
07:10:13,440 --> 07:10:20,160
going to scroll to the top and at the
8408
07:10:15,760 --> 07:10:24,718
top we're going to import dayjs
8409
07:10:20,160 --> 07:10:27,040
from the package day js.
8410
07:10:24,718 --> 07:10:29,040
And now we'll scroll back down or we can
8411
07:10:27,040 --> 07:10:32,040
control F and search for delivery
8412
07:10:29,040 --> 07:10:32,040
options.
8413
07:10:32,160 --> 07:10:38,958
And we'll scroll down here. And now
8414
07:10:35,280 --> 07:10:42,160
above this date, we're going to insert.
8415
07:10:38,958 --> 07:10:44,400
And we're going to run day JS and
8416
07:10:42,160 --> 07:10:47,360
brackets. And we're going to give Day.js
8417
07:10:44,400 --> 07:10:52,240
DJs a time in milliseconds which we
8418
07:10:47,360 --> 07:10:54,000
actually have inside delivery option dot
8419
07:10:52,240 --> 07:10:59,440
estimated
8420
07:10:54,000 --> 07:11:02,478
delivery time ms or milliseconds.
8421
07:10:59,440 --> 07:11:06,320
And now that we wrapped this in dayjs we
8422
07:11:02,478 --> 07:11:08,558
can format it into this format. So after
8423
07:11:06,320 --> 07:11:12,718
this bracket here, we're going to use
8424
07:11:08,558 --> 07:11:15,920
the method dot format and brackets and
8425
07:11:12,718 --> 07:11:18,958
give it a string. To show the time in
8426
07:11:15,920 --> 07:11:20,798
this format, we're going to type D four
8427
07:11:18,958 --> 07:11:22,638
times,
8428
07:11:20,798 --> 07:11:25,360
which is the day of the week. And then
8429
07:11:22,638 --> 07:11:28,320
we'll type a comma to show this comma
8430
07:11:25,360 --> 07:11:30,160
and a space. And we'll type capital M
8431
07:11:28,320 --> 07:11:33,360
four times
8432
07:11:30,160 --> 07:11:36,080
to show the month and a space. and we'll
8433
07:11:33,360 --> 07:11:41,760
type capital D which will show this
8434
07:11:36,080 --> 07:11:44,478
date. And now we can remove this code.
8435
07:11:41,760 --> 07:11:46,558
Next, we're going to replace this price.
8436
07:11:44,478 --> 07:11:48,558
If the price is free, we'll show free
8437
07:11:46,558 --> 07:11:51,760
shipping. Otherwise, we're going to show
8438
07:11:48,558 --> 07:11:54,638
the price dash shipping. So, we're going
8439
07:11:51,760 --> 07:11:56,400
to scroll up to the inside of this loop
8440
07:11:54,638 --> 07:11:58,638
here,
8441
07:11:56,400 --> 07:12:03,840
and we're going to create a variable for
8442
07:11:58,638 --> 07:12:07,680
the price. So, we'll do let price string
8443
07:12:03,840 --> 07:12:10,558
equals a string. And to make it easier,
8444
07:12:07,680 --> 07:12:13,440
we're going to set this to a default of
8445
07:12:10,558 --> 07:12:15,360
free shipping.
8446
07:12:13,440 --> 07:12:19,120
And then we're going to overwrite this
8447
07:12:15,360 --> 07:12:22,718
if we have a price. So, below this,
8448
07:12:19,120 --> 07:12:26,240
we're going to check if brackets the
8449
07:12:22,718 --> 07:12:29,520
delivery option.
8450
07:12:26,240 --> 07:12:32,718
Sense is greater than zero.
8451
07:12:29,520 --> 07:12:36,080
curly brackets. So if it is not free,
8452
07:12:32,718 --> 07:12:39,840
we're going to overwrite this price
8453
07:12:36,080 --> 07:12:42,878
string equals a template string
8454
07:12:39,840 --> 07:12:44,878
and inside we're going to insert and
8455
07:12:42,878 --> 07:12:47,600
remember we have the format money
8456
07:12:44,878 --> 07:12:51,520
function to display the price. So as I
8457
07:12:47,600 --> 07:12:54,638
here we'll call format money and
8458
07:12:51,520 --> 07:12:59,440
brackets and we're going to give it the
8459
07:12:54,638 --> 07:13:02,718
delivery option price sets.
8460
07:12:59,440 --> 07:13:05,040
And at the end we're going to add a dash
8461
07:13:02,718 --> 07:13:08,240
and shipping.
8462
07:13:05,040 --> 07:13:10,558
And now we can scroll back down
8463
07:13:08,240 --> 07:13:15,840
and instead of free shipping every time
8464
07:13:10,558 --> 07:13:18,558
we're going to insert the price string.
8465
07:13:15,840 --> 07:13:19,920
Next, we're going to scroll up a bit and
8466
07:13:18,558 --> 07:13:22,638
there's a few things that we need to
8467
07:13:19,920 --> 07:13:25,120
replace for this input type radio
8468
07:13:22,638 --> 07:13:27,280
element. So, this represents the
8469
07:13:25,120 --> 07:13:29,760
selector and there's a few special
8470
07:13:27,280 --> 07:13:32,798
attributes that we need to replace. The
8471
07:13:29,760 --> 07:13:36,000
first one is the name attribute. So, the
8472
07:13:32,798 --> 07:13:38,400
name groups the selectors together. So,
8473
07:13:36,000 --> 07:13:41,120
each set of selectors should have a
8474
07:13:38,400 --> 07:13:43,200
unique name. This makes sure that for
8475
07:13:41,120 --> 07:13:46,080
each product we can only select one of
8476
07:13:43,200 --> 07:13:48,718
them. So here instead of doing delivery
8477
07:13:46,080 --> 07:13:51,440
option one every time we're going to
8478
07:13:48,718 --> 07:13:54,240
replace this with a product ID because
8479
07:13:51,440 --> 07:13:56,240
each set of selectors should belong to
8480
07:13:54,240 --> 07:13:58,320
one product.
8481
07:13:56,240 --> 07:14:01,520
So in this code we're going to replace
8482
07:13:58,320 --> 07:14:03,440
it using curly brackets
8483
07:14:01,520 --> 07:14:06,400
and change this to a template string
8484
07:14:03,440 --> 07:14:08,320
using a back tick and a back tick and
8485
07:14:06,400 --> 07:14:12,000
instead of one every time we're going to
8486
07:14:08,320 --> 07:14:14,240
insert and we'll insert the product ID.
8487
07:14:12,000 --> 07:14:17,360
Now in this code we actually have access
8488
07:14:14,240 --> 07:14:22,400
to the cart item from above. So inside
8489
07:14:17,360 --> 07:14:24,240
here we can just insert the cart item.
8490
07:14:22,400 --> 07:14:26,638
ID.
8491
07:14:24,240 --> 07:14:29,280
Next we also need to update this checked
8492
07:14:26,638 --> 07:14:32,240
property. So this determines if the
8493
07:14:29,280 --> 07:14:35,120
selector is checked. So we don't want
8494
07:14:32,240 --> 07:14:38,478
the selector to be checked every time.
8495
07:14:35,120 --> 07:14:40,000
Each cart item has a delivery option ID
8496
07:14:38,478 --> 07:14:43,440
and this is the one that should be
8497
07:14:40,000 --> 07:14:45,600
checked. So, inside here, we're going to
8498
07:14:43,440 --> 07:14:49,280
add a new line. And we're going to make
8499
07:14:45,600 --> 07:14:52,638
check equal and insert some code. And
8500
07:14:49,280 --> 07:15:00,638
we're going to compare if this delivery
8501
07:14:52,638 --> 07:15:02,958
option do ID is equal to the cart items.
8502
07:15:00,638 --> 07:15:05,600
Option ID.
8503
07:15:02,958 --> 07:15:08,320
So only the delivery option that matches
8504
07:15:05,600 --> 07:15:10,798
the delivery option ID in the cart will
8505
07:15:08,320 --> 07:15:12,958
be checked. So that's all the things
8506
07:15:10,798 --> 07:15:16,160
that we have to replace in the delivery
8507
07:15:12,958 --> 07:15:20,080
options. So now let's scroll down and we
8508
07:15:16,160 --> 07:15:22,478
can remove this default code
8509
07:15:20,080 --> 07:15:25,920
and save
8510
07:15:22,478 --> 07:15:28,320
and let's check the website.
8511
07:15:25,920 --> 07:15:31,200
And now we're generating these delivery
8512
07:15:28,320 --> 07:15:33,520
options and the correct ones are checked
8513
07:15:31,200 --> 07:15:36,000
and we have the estimated delivery dates
8514
07:15:33,520 --> 07:15:37,840
for each displayed in an easyto read
8515
07:15:36,000 --> 07:15:39,680
format.
8516
07:15:37,840 --> 07:15:42,558
The last thing we need to generate is
8517
07:15:39,680 --> 07:15:44,798
this delivery date up here. So, we'll go
8518
07:15:42,558 --> 07:15:47,520
back to the code
8519
07:15:44,798 --> 07:15:50,240
and we're going to scroll up
8520
07:15:47,520 --> 07:15:52,400
um to the cart item and we're going to
8521
07:15:50,240 --> 07:15:55,440
find the cart item container and this
8522
07:15:52,400 --> 07:15:57,600
delivery date here. So, this date is
8523
07:15:55,440 --> 07:16:00,320
determined by whichever delivery option
8524
07:15:57,600 --> 07:16:03,520
is selected. So, first we need to find
8525
07:16:00,320 --> 07:16:06,558
the delivery option that is selected.
8526
07:16:03,520 --> 07:16:10,478
So inside this loop, we'll create a
8527
07:16:06,558 --> 07:16:14,638
variable const for the selected
8528
07:16:10,478 --> 07:16:17,440
delivery option and equals.
8529
07:16:14,638 --> 07:16:21,360
And to find this, we're going to get the
8530
07:16:17,440 --> 07:16:24,718
delivery options and then use the method
8531
07:16:21,360 --> 07:16:27,760
dot find and brackets.
8532
07:16:24,718 --> 07:16:30,400
And find gets a function. So brackets,
8533
07:16:27,760 --> 07:16:33,040
arrow, and curly brackets.
8534
07:16:30,400 --> 07:16:36,000
And in this inner function, it receives
8535
07:16:33,040 --> 07:16:38,798
each delivery option. So we'll call it
8536
07:16:36,000 --> 07:16:41,760
delivery option.
8537
07:16:38,798 --> 07:16:44,320
And I'll just add a new line here and
8538
07:16:41,760 --> 07:16:46,718
select this code and press tab to format
8539
07:16:44,320 --> 07:16:49,200
it a little bit.
8540
07:16:46,718 --> 07:16:51,600
So the way that find works is it's going
8541
07:16:49,200 --> 07:16:53,840
to loop through each delivery option.
8542
07:16:51,600 --> 07:16:56,000
And the first function here that returns
8543
07:16:53,840 --> 07:16:57,920
true is going to be the result. So
8544
07:16:56,000 --> 07:17:00,320
that's how we find something. And then
8545
07:16:57,920 --> 07:17:04,400
it's going to save it in this variable.
8546
07:17:00,320 --> 07:17:09,760
So inside here we want to return the
8547
07:17:04,400 --> 07:17:12,080
delivery option id that is equal to the
8548
07:17:09,760 --> 07:17:16,558
cart items
8549
07:17:12,080 --> 07:17:18,798
dot delivery option id. So the delivery
8550
07:17:16,558 --> 07:17:21,520
option that matches what is inside the
8551
07:17:18,798 --> 07:17:24,478
cart item will return true and that's
8552
07:17:21,520 --> 07:17:27,280
going to be saved in here. And now we
8553
07:17:24,478 --> 07:17:30,798
can replace this date. So we'll type
8554
07:17:27,280 --> 07:17:34,160
curly brackets and we'll use day js
8555
07:17:30,798 --> 07:17:40,400
brackets and the selected
8556
07:17:34,160 --> 07:17:43,520
delivery option dot estimated delivery
8557
07:17:40,400 --> 07:17:45,440
time ms
8558
07:17:43,520 --> 07:17:49,680
and outside this bracket we'll use a
8559
07:17:45,440 --> 07:17:52,878
method dot format brackets and give it a
8560
07:17:49,680 --> 07:17:58,320
string and to get this date format we'll
8561
07:17:52,878 --> 07:18:03,440
give it d four times a comma and capital
8562
07:17:58,320 --> 07:18:07,360
M four times space and capital D.
8563
07:18:03,440 --> 07:18:09,360
And now we can remove this date here.
8564
07:18:07,360 --> 07:18:11,680
And before we run this code, there is
8565
07:18:09,360 --> 07:18:14,878
one small problem here because remember
8566
07:18:11,680 --> 07:18:17,040
the delivery options start out as empty.
8567
07:18:14,878 --> 07:18:20,000
So if they are empty, we might not find
8568
07:18:17,040 --> 07:18:22,718
the selected delivery option. To fix
8569
07:18:20,000 --> 07:18:27,760
this in front of this code, we're going
8570
07:18:22,718 --> 07:18:32,320
to check if delivery options.length
8571
07:18:27,760 --> 07:18:34,400
is greater than zero and type and and
8572
07:18:32,320 --> 07:18:36,958
as a review. This works like an if
8573
07:18:34,400 --> 07:18:39,840
statement. It will check if delivery
8574
07:18:36,958 --> 07:18:42,160
options.length is greater than zero and
8575
07:18:39,840 --> 07:18:46,320
then only if it's true, it will run the
8576
07:18:42,160 --> 07:18:48,878
rest of the code. So now we can save
8577
07:18:46,320 --> 07:18:51,280
and check the website.
8578
07:18:48,878 --> 07:18:54,558
And now we're generating everything here
8579
07:18:51,280 --> 07:18:56,718
using data from the back end. So let's
8580
07:18:54,558 --> 07:18:59,840
go back to our code
8581
07:18:56,718 --> 07:19:03,120
and we'll save this into git. Inside the
8582
07:18:59,840 --> 07:19:09,200
git section, we'll type the message
8583
07:19:03,120 --> 07:19:10,718
generate the HTML for the delivery
8584
07:19:09,200 --> 07:19:14,280
options
8585
07:19:10,718 --> 07:19:14,280
and commit.
8586
07:19:16,798 --> 07:19:21,760
Next, if we go back to the website,
8587
07:19:19,520 --> 07:19:24,558
we're going to generate the HTML for the
8588
07:19:21,760 --> 07:19:26,558
payment summary here. Now, we could
8589
07:19:24,558 --> 07:19:29,440
calculate all these numbers inside
8590
07:19:26,558 --> 07:19:32,160
React. However, we're going to use the
8591
07:19:29,440 --> 07:19:34,718
back end to calculate these numbers.
8592
07:19:32,160 --> 07:19:37,680
Remember, the backend's job is to manage
8593
07:19:34,718 --> 07:19:40,240
the data and to do the calculations. The
8594
07:19:37,680 --> 07:19:43,280
front end's job is to present the data
8595
07:19:40,240 --> 07:19:45,920
in an easy to read way. So let's go back
8596
07:19:43,280 --> 07:19:48,400
to our code and we'll go back to the
8597
07:19:45,920 --> 07:19:51,200
checkout page and we'll scroll to the
8598
07:19:48,400 --> 07:19:54,320
top and we're going to create a state
8599
07:19:51,200 --> 07:19:58,558
for the payment summary. So here we'll
8600
07:19:54,320 --> 07:20:01,840
type const square brackets equals use
8601
07:19:58,558 --> 07:20:06,400
state and brackets
8602
07:20:01,840 --> 07:20:09,840
and we'll name this payment summary,
8603
07:20:06,400 --> 07:20:11,680
and the updater function set payment
8604
07:20:09,840 --> 07:20:14,000
summary.
8605
07:20:11,680 --> 07:20:17,280
And as a starting value, we're going to
8606
07:20:14,000 --> 07:20:19,440
use null because the payment summary is
8607
07:20:17,280 --> 07:20:21,200
going to be an object. And it's a little
8608
07:20:19,440 --> 07:20:23,280
easier to check if the object is not
8609
07:20:21,200 --> 07:20:24,798
loaded if we set it to null at the
8610
07:20:23,280 --> 07:20:27,200
start.
8611
07:20:24,798 --> 07:20:29,840
Next, we're going to use Axios to get
8612
07:20:27,200 --> 07:20:35,040
the payment summary from the back end.
8613
07:20:29,840 --> 07:20:38,160
So down here, we'll use Axios.get get
8614
07:20:35,040 --> 07:20:42,080
brackets and we'll give it the URL path
8615
07:20:38,160 --> 07:20:43,760
slash API slash payment-
8616
07:20:42,080 --> 07:20:46,718
summary
8617
07:20:43,760 --> 07:20:50,160
and we'll use dot then to wait for a
8618
07:20:46,718 --> 07:20:54,320
response and give it a function
8619
07:20:50,160 --> 07:20:56,558
and inside here we can get the response.
8620
07:20:54,320 --> 07:21:02,000
Once we get the response, we're going to
8621
07:20:56,558 --> 07:21:06,160
use set payment summary brackets
8622
07:21:02,000 --> 07:21:09,680
response data to save the data inside
8623
07:21:06,160 --> 07:21:11,840
here. So now let's save and check the
8624
07:21:09,680 --> 07:21:15,120
response from the back end. So we'll go
8625
07:21:11,840 --> 07:21:17,680
to our website and open the console and
8626
07:21:15,120 --> 07:21:20,400
open the network tab and we'll refresh
8627
07:21:17,680 --> 07:21:24,160
just in case. We can click this to
8628
07:21:20,400 --> 07:21:26,638
remove the search and type payment
8629
07:21:24,160 --> 07:21:29,120
and we get the payment summary. So we
8630
07:21:26,638 --> 07:21:31,600
can click this and you can see that in
8631
07:21:29,120 --> 07:21:33,920
the response the back end calculated all
8632
07:21:31,600 --> 07:21:36,878
the numbers that we need. So now we just
8633
07:21:33,920 --> 07:21:40,718
need to present this on the page.
8634
07:21:36,878 --> 07:21:44,080
Let's go back and we'll press CtrlF in
8635
07:21:40,718 --> 07:21:48,080
this file and we'll search for payment
8636
07:21:44,080 --> 07:21:51,760
dash summary and press enter to find the
8637
07:21:48,080 --> 07:21:55,040
next result. And this section represents
8638
07:21:51,760 --> 07:21:56,798
the payment summary on the page. And now
8639
07:21:55,040 --> 07:21:59,280
we'll replace these numbers with the
8640
07:21:56,798 --> 07:22:01,280
numbers we got from the back end. So
8641
07:21:59,280 --> 07:22:04,000
first we'll replace the total quantity
8642
07:22:01,280 --> 07:22:07,840
here using curly brackets and we'll
8643
07:22:04,000 --> 07:22:10,718
replace it with payment summary dot
8644
07:22:07,840 --> 07:22:14,558
total items.
8645
07:22:10,718 --> 07:22:16,320
And here we'll replace the product cost.
8646
07:22:14,558 --> 07:22:19,520
I'll type a new line to make it a little
8647
07:22:16,320 --> 07:22:21,680
easier to read. And we'll insert.
8648
07:22:19,520 --> 07:22:25,840
And remember for money we need to format
8649
07:22:21,680 --> 07:22:29,360
it using the function format money
8650
07:22:25,840 --> 07:22:35,040
brackets. and we'll get the payment
8651
07:22:29,360 --> 07:22:37,440
summary dot product cost set.
8652
07:22:35,040 --> 07:22:40,160
We'll scroll down and we'll replace this
8653
07:22:37,440 --> 07:22:43,760
next number. So we'll remove it and new
8654
07:22:40,160 --> 07:22:47,360
line and curly brackets and we'll run
8655
07:22:43,760 --> 07:22:50,240
format money again brackets and this one
8656
07:22:47,360 --> 07:22:55,920
is a shipping cost which is payment
8657
07:22:50,240 --> 07:22:58,400
summary dot shipping cost sense.
8658
07:22:55,920 --> 07:23:02,080
Let's keep going. And this is the total
8659
07:22:58,400 --> 07:23:06,958
before tax. So we'll replace this and
8660
07:23:02,080 --> 07:23:12,400
insert format money brackets
8661
07:23:06,958 --> 07:23:14,798
payment summary dot total cost before
8662
07:23:12,400 --> 07:23:17,840
tax cents.
8663
07:23:14,798 --> 07:23:23,200
And we'll scroll down here and replace
8664
07:23:17,840 --> 07:23:27,920
this. insert format money brackets the
8665
07:23:23,200 --> 07:23:29,680
payment summary dot tax sets
8666
07:23:27,920 --> 07:23:35,040
and then down here we're going to
8667
07:23:29,680 --> 07:23:39,840
replace this one and insert format money
8668
07:23:35,040 --> 07:23:42,240
brackets the payment summary dot total
8669
07:23:39,840 --> 07:23:45,840
cost sense
8670
07:23:42,240 --> 07:23:48,400
and finally we only want to display this
8671
07:23:45,840 --> 07:23:49,760
if the payment summary is loaded
8672
07:23:48,400 --> 07:23:53,520
otherwise we don't have this
8673
07:23:49,760 --> 07:23:56,000
information. So above this code,
8674
07:23:53,520 --> 07:24:00,718
we're going to type curly brackets and
8675
07:23:56,000 --> 07:24:02,400
we'll check if payment summary and and
8676
07:24:00,718 --> 07:24:04,240
brackets.
8677
07:24:02,400 --> 07:24:06,958
So this checks if the payment summary
8678
07:24:04,240 --> 07:24:08,958
object exists. If it's null at the
8679
07:24:06,958 --> 07:24:11,440
start, it's not going to run the rest of
8680
07:24:08,958 --> 07:24:13,920
the code. And now we're going to put all
8681
07:24:11,440 --> 07:24:16,638
of this code inside here. So we'll
8682
07:24:13,920 --> 07:24:19,760
select all this code
8683
07:24:16,638 --> 07:24:22,718
up to the place order button and we'll
8684
07:24:19,760 --> 07:24:27,120
cut
8685
07:24:22,718 --> 07:24:28,878
and inside here we're going to paste.
8686
07:24:27,120 --> 07:24:31,200
Now a problem here is that we're
8687
07:24:28,878 --> 07:24:33,920
returning multiple elements inside this
8688
07:24:31,200 --> 07:24:36,558
brackets. So to fix that we need to wrap
8689
07:24:33,920 --> 07:24:40,000
this in a fragment using less than
8690
07:24:36,558 --> 07:24:42,400
greater than and remove this for now.
8691
07:24:40,000 --> 07:24:44,958
And at the end
8692
07:24:42,400 --> 07:24:47,520
after the button we'll do less than
8693
07:24:44,958 --> 07:24:50,478
slashgreater than. So we're returning
8694
07:24:47,520 --> 07:24:52,718
one element inside this brackets. And
8695
07:24:50,478 --> 07:24:54,958
now we'll format our code using control
8696
07:24:52,718 --> 07:24:58,478
or commandshiftp
8697
07:24:54,958 --> 07:25:00,878
and run format document
8698
07:24:58,478 --> 07:25:03,040
and save.
8699
07:25:00,878 --> 07:25:05,280
And that's it. Now we're generating the
8700
07:25:03,040 --> 07:25:08,958
payment summary using data from the back
8701
07:25:05,280 --> 07:25:11,920
end. We'll go to our website. And that
8702
07:25:08,958 --> 07:25:15,680
looks good. So now let's go back to our
8703
07:25:11,920 --> 07:25:17,840
code and we'll save this into git. In
8704
07:25:15,680 --> 07:25:24,400
the git section, we're going to type the
8705
07:25:17,840 --> 07:25:29,160
message generate HTML for the payment
8706
07:25:24,400 --> 07:25:29,160
summary and commit.
8707
07:25:31,520 --> 07:25:37,200
Next, we're going to work on the orders
8708
07:25:33,600 --> 07:25:41,280
page. So, we'll go back to our files
8709
07:25:37,200 --> 07:25:44,080
and we're going to open the orders page.
8710
07:25:41,280 --> 07:25:46,558
So, first of all, the orders page has a
8711
07:25:44,080 --> 07:25:49,200
header component. And remember, the
8712
07:25:46,558 --> 07:25:51,520
header now needs the cart in order to
8713
07:25:49,200 --> 07:25:53,440
display the quantity. So, we need to
8714
07:25:51,520 --> 07:25:57,360
share the cart with the orders page
8715
07:25:53,440 --> 07:26:00,000
first. We'll go to our files and open
8716
07:25:57,360 --> 07:26:02,558
app.jsx.
8717
07:26:00,000 --> 07:26:04,400
And we'll scroll down. And here we'll
8718
07:26:02,558 --> 07:26:08,080
share the cart with the orders page
8719
07:26:04,400 --> 07:26:12,478
using a prop cart equals curly brackets.
8720
07:26:08,080 --> 07:26:14,320
And we'll save the cart and save.
8721
07:26:12,478 --> 07:26:16,958
And now we'll open the orders page
8722
07:26:14,320 --> 07:26:19,920
again. And at the top we're going to
8723
07:26:16,958 --> 07:26:22,638
access the prop using curly brackets and
8724
07:26:19,920 --> 07:26:26,478
cart. And we're going to pass this cart
8725
07:26:22,638 --> 07:26:29,600
into the header using cart equals curly
8726
07:26:26,478 --> 07:26:31,840
brackets and the cart.
8727
07:26:29,600 --> 07:26:34,558
Let's save.
8728
07:26:31,840 --> 07:26:36,478
and we'll open the website and we're
8729
07:26:34,558 --> 07:26:38,240
going to go to the orders page. So,
8730
07:26:36,478 --> 07:26:41,040
first we'll click here to go to the
8731
07:26:38,240 --> 07:26:43,520
homepage and then we'll click here to
8732
07:26:41,040 --> 07:26:46,160
open the orders page and it should be
8733
07:26:43,520 --> 07:26:49,840
working. So, now we're going to generate
8734
07:26:46,160 --> 07:26:53,200
this HTML using data from the back end.
8735
07:26:49,840 --> 07:26:56,240
So, we'll go back to the orders page and
8736
07:26:53,200 --> 07:27:01,280
at the top we're going to import what we
8737
07:26:56,240 --> 07:27:04,878
need. So first we'll import axios from
8738
07:27:01,280 --> 07:27:10,638
Axios and we'll also import curly
8739
07:27:04,878 --> 07:27:12,798
brackets use state and use effect from
8740
07:27:10,638 --> 07:27:15,360
react.
8741
07:27:12,798 --> 07:27:18,398
And now down here first we'll create
8742
07:27:15,360 --> 07:27:22,638
some state to save the orders using
8743
07:27:18,398 --> 07:27:24,398
const square brackets equals use state
8744
07:27:22,638 --> 07:27:27,600
and brackets.
8745
07:27:24,398 --> 07:27:31,120
And we'll call this data the orders,
8746
07:27:27,600 --> 07:27:33,120
comma, and the updater function set
8747
07:27:31,120 --> 07:27:35,120
orders.
8748
07:27:33,120 --> 07:27:38,798
And at the start, we'll set the orders
8749
07:27:35,120 --> 07:27:41,120
to an empty array. And below this, we're
8750
07:27:38,798 --> 07:27:46,160
going to make a request to the back end
8751
07:27:41,120 --> 07:27:48,958
using use effect brackets and a function
8752
07:27:46,160 --> 07:27:51,840
brackets arrow and curly brackets. And
8753
07:27:48,958 --> 07:27:55,200
down here, we'll give it a empty array
8754
07:27:51,840 --> 07:27:58,160
so that it only runs once. And inside
8755
07:27:55,200 --> 07:28:01,920
we're going to use axios.get
8756
07:27:58,160 --> 07:28:05,280
again. So brackets and the URL path for
8757
07:28:01,920 --> 07:28:06,958
the orders is slappi/ord
8758
07:28:05,280 --> 07:28:09,920
orders
8759
07:28:06,958 --> 07:28:12,080
and then we'll do dot then to wait for
8760
07:28:09,920 --> 07:28:14,798
the response from the back end. We'll
8761
07:28:12,080 --> 07:28:17,840
give this a function brackets arrow and
8762
07:28:14,798 --> 07:28:22,320
curly brackets. Inside this parameter is
8763
07:28:17,840 --> 07:28:24,240
the response from the back end. And once
8764
07:28:22,320 --> 07:28:28,638
we get the response, we're going to save
8765
07:28:24,240 --> 07:28:30,398
it inside orders using set orders
8766
07:28:28,638 --> 07:28:31,920
brackets
8767
07:28:30,398 --> 07:28:35,520
response
8768
07:28:31,920 --> 07:28:38,080
data. Let's save.
8769
07:28:35,520 --> 07:28:40,798
And now we can check the network tab to
8770
07:28:38,080 --> 07:28:42,558
see if we got the orders. We'll open the
8771
07:28:40,798 --> 07:28:46,478
console.
8772
07:28:42,558 --> 07:28:50,558
Inside the network tab, we can refresh
8773
07:28:46,478 --> 07:28:53,600
and remove this and search for orders.
8774
07:28:50,558 --> 07:28:56,878
And down here, we'll click this request
8775
07:28:53,600 --> 07:28:59,040
and go to the response tab. And the back
8776
07:28:56,878 --> 07:29:02,320
end gave us the orders that we need to
8777
07:28:59,040 --> 07:29:05,600
display. Now, a problem here is that we
8778
07:29:02,320 --> 07:29:07,600
only have the product ID in the order,
8779
07:29:05,600 --> 07:29:10,718
but we also need to display some product
8780
07:29:07,600 --> 07:29:13,520
details on the website. And again, the
8781
07:29:10,718 --> 07:29:16,958
backend supports an expand parameter for
8782
07:29:13,520 --> 07:29:21,120
this URL path. So at the end here, we'll
8783
07:29:16,958 --> 07:29:23,840
add question mark expand equals
8784
07:29:21,120 --> 07:29:28,160
products. This time, make sure you have
8785
07:29:23,840 --> 07:29:31,600
an s at the end. So now let's save
8786
07:29:28,160 --> 07:29:34,080
and check the network tab again. And
8787
07:29:31,600 --> 07:29:36,958
we'll refresh.
8788
07:29:34,080 --> 07:29:38,878
And now inside the orders request,
8789
07:29:36,958 --> 07:29:41,520
you'll see that each order has the
8790
07:29:38,878 --> 07:29:44,000
product ID, but it also has the other
8791
07:29:41,520 --> 07:29:47,200
data for the product. And this is what
8792
07:29:44,000 --> 07:29:49,600
we need to display on the website.
8793
07:29:47,200 --> 07:29:52,398
So now we'll go into our code and we're
8794
07:29:49,600 --> 07:29:55,280
going to generate the HTML. So first
8795
07:29:52,398 --> 07:29:58,240
we'll press control or command F and
8796
07:29:55,280 --> 07:30:00,558
we're going to search for order dash
8797
07:29:58,240 --> 07:30:03,760
container
8798
07:30:00,558 --> 07:30:06,798
which is down here. And we can collapse
8799
07:30:03,760 --> 07:30:09,680
this code and collapse the other order
8800
07:30:06,798 --> 07:30:12,160
container. Each of these represents an
8801
07:30:09,680 --> 07:30:14,478
order on the website. So, we're going to
8802
07:30:12,160 --> 07:30:17,600
generate this HTML.
8803
07:30:14,478 --> 07:30:21,360
At the top, we'll insert and we'll run
8804
07:30:17,600 --> 07:30:23,440
orders.m map brackets. And we'll give it
8805
07:30:21,360 --> 07:30:25,840
a function.
8806
07:30:23,440 --> 07:30:28,240
And this parameter is going to be each
8807
07:30:25,840 --> 07:30:30,478
order.
8808
07:30:28,240 --> 07:30:33,760
And for each order, we're going to
8809
07:30:30,478 --> 07:30:35,840
return some HTML.
8810
07:30:33,760 --> 07:30:38,878
So, we want each order to be converted
8811
07:30:35,840 --> 07:30:43,520
into this HTML. We're going to select it
8812
07:30:38,878 --> 07:30:46,718
and copy it inside here. We'll paste.
8813
07:30:43,520 --> 07:30:49,200
Press control or command shiftp and run
8814
07:30:46,718 --> 07:30:52,798
format document.
8815
07:30:49,200 --> 07:30:54,958
And we'll scroll back up.
8816
07:30:52,798 --> 07:30:57,200
As always, we need a key prop when we're
8817
07:30:54,958 --> 07:31:01,440
looping through an array. We'll set the
8818
07:30:57,200 --> 07:31:03,840
key equals. And each order has an order
8819
07:31:01,440 --> 07:31:05,520
ID property.
8820
07:31:03,840 --> 07:31:07,840
As usual, we're going to replace the
8821
07:31:05,520 --> 07:31:10,558
values in here. Feel free to pause if
8822
07:31:07,840 --> 07:31:13,040
you want to try it yourself first. So
8823
07:31:10,558 --> 07:31:17,040
for the delivery date, we're going to
8824
07:31:13,040 --> 07:31:21,360
insert and we're going to use dayjs
8825
07:31:17,040 --> 07:31:24,398
brackets and each order has an order dot
8826
07:31:21,360 --> 07:31:26,718
order time ms
8827
07:31:24,398 --> 07:31:30,718
and outside this bracket we use dot
8828
07:31:26,718 --> 07:31:32,320
format brackets and a string. And to
8829
07:31:30,718 --> 07:31:35,600
display the date, we're going to use
8830
07:31:32,320 --> 07:31:38,798
four capital M's for the month and a
8831
07:31:35,600 --> 07:31:41,440
capital D for the date. So now we also
8832
07:31:38,798 --> 07:31:45,280
need to import day.js. So we'll scroll
8833
07:31:41,440 --> 07:31:50,398
to the top and at the top we'll import
8834
07:31:45,280 --> 07:31:52,638
dayjs from a string dayjs.
8835
07:31:50,398 --> 07:31:54,718
And let's also import format money
8836
07:31:52,638 --> 07:31:59,600
because we're going to use it later. So,
8837
07:31:54,718 --> 07:32:03,680
import curly brackets format money from
8838
07:31:59,600 --> 07:32:05,360
a string dot dot slashyouutills/
8839
07:32:03,680 --> 07:32:08,160
money.
8840
07:32:05,360 --> 07:32:10,000
Now, we're going to scroll back down
8841
07:32:08,160 --> 07:32:12,558
and we're going to replace the total
8842
07:32:10,000 --> 07:32:15,920
order cost. So here we're going to
8843
07:32:12,558 --> 07:32:20,000
remove this and curly brackets we're
8844
07:32:15,920 --> 07:32:24,398
going to insert format money brackets
8845
07:32:20,000 --> 07:32:26,000
and give it the order dot total cost
8846
07:32:24,398 --> 07:32:28,558
sense.
8847
07:32:26,000 --> 07:32:31,520
We'll scroll down and here we're going
8848
07:32:28,558 --> 07:32:36,160
to replace this with the order ID. So
8849
07:32:31,520 --> 07:32:39,680
curly brackets order ID.
8850
07:32:36,160 --> 07:32:42,638
Next, inside this element order details
8851
07:32:39,680 --> 07:32:44,878
grid. This displays the list of products
8852
07:32:42,638 --> 07:32:49,120
in the order and we're going to generate
8853
07:32:44,878 --> 07:32:52,320
this using React as well. So above this
8854
07:32:49,120 --> 07:32:55,200
we're going to use curly brackets and
8855
07:32:52,320 --> 07:32:57,360
we're going to get the order products
8856
07:32:55,200 --> 07:33:00,718
array and we're going to loop through it
8857
07:32:57,360 --> 07:33:02,638
using map brackets and give it a
8858
07:33:00,718 --> 07:33:05,360
function
8859
07:33:02,638 --> 07:33:07,360
and save each product
8860
07:33:05,360 --> 07:33:11,520
and for each product we're going to
8861
07:33:07,360 --> 07:33:14,718
return brackets some HTML.
8862
07:33:11,520 --> 07:33:17,920
So if we collapse this code and collapse
8863
07:33:14,718 --> 07:33:21,478
this element and this element and we
8864
07:33:17,920 --> 07:33:21,478
also collapse these.
8865
07:33:21,600 --> 07:33:26,000
So three of these elements represents
8866
07:33:23,680 --> 07:33:28,000
one product. It's a little different
8867
07:33:26,000 --> 07:33:30,320
because we don't have an element that
8868
07:33:28,000 --> 07:33:33,040
contains all of these because we're
8869
07:33:30,320 --> 07:33:36,080
using a grid here, but it works in a
8870
07:33:33,040 --> 07:33:39,680
similar way. We're going to copy this
8871
07:33:36,080 --> 07:33:41,440
code and inside return we're going to
8872
07:33:39,680 --> 07:33:43,600
paste
8873
07:33:41,440 --> 07:33:45,840
and the one difference here is that if
8874
07:33:43,600 --> 07:33:47,760
we scroll up
8875
07:33:45,840 --> 07:33:49,920
we're returning multiple elements from
8876
07:33:47,760 --> 07:33:53,040
this function. So we need to wrap these
8877
07:33:49,920 --> 07:33:55,920
in a fragment. So at the top we'll type
8878
07:33:53,040 --> 07:33:58,398
less than greater than. But then we have
8879
07:33:55,920 --> 07:34:00,798
another problem here which is remember
8880
07:33:58,398 --> 07:34:02,878
when we loop through an array each
8881
07:34:00,798 --> 07:34:05,440
element that we return should have a key
8882
07:34:02,878 --> 07:34:08,478
property and we can't add props to
8883
07:34:05,440 --> 07:34:11,600
fragments like this. To solve this we
8884
07:34:08,478 --> 07:34:14,958
need to use the full fragment syntax. So
8885
07:34:11,600 --> 07:34:18,558
we're going to scroll to the top and for
8886
07:34:14,958 --> 07:34:21,280
React we're going to import using comma
8887
07:34:18,558 --> 07:34:24,080
the fragment component.
8888
07:34:21,280 --> 07:34:26,798
So we'll scroll back down and in here
8889
07:34:24,080 --> 07:34:28,398
we're going to use a full fragment
8890
07:34:26,798 --> 07:34:31,440
syntax
8891
07:34:28,398 --> 07:34:34,320
and remove this. So this does the same
8892
07:34:31,440 --> 07:34:37,840
thing except now we can add a key prop
8893
07:34:34,320 --> 07:34:42,080
to it equals curly brackets and let's
8894
07:34:37,840 --> 07:34:45,280
set the prop to the product ID.
8895
07:34:42,080 --> 07:34:47,920
and we'll scroll to the bottom.
8896
07:34:45,280 --> 07:34:52,478
And before these brackets, remember to
8897
07:34:47,920 --> 07:34:54,398
add the closing fragment tag.
8898
07:34:52,478 --> 07:34:56,080
And finally, we'll press control or
8899
07:34:54,398 --> 07:34:58,958
commandshiftp
8900
07:34:56,080 --> 07:35:01,040
and format document.
8901
07:34:58,958 --> 07:35:04,920
And now we can replace this default code
8902
07:35:01,040 --> 07:35:04,920
that we have down here.
8903
07:35:06,638 --> 07:35:12,000
Finally, we're going to replace each
8904
07:35:08,478 --> 07:35:14,798
product's details. So here we're going
8905
07:35:12,000 --> 07:35:18,718
to replace the name and insert the
8906
07:35:14,798 --> 07:35:20,878
product name. And here let's also
8907
07:35:18,718 --> 07:35:25,600
replace the image
8908
07:35:20,878 --> 07:35:27,760
curly brackets. Replace it with product.
8909
07:35:25,600 --> 07:35:29,920
And actually I made a mistake. I forgot
8910
07:35:27,760 --> 07:35:32,878
that all the product details are saved
8911
07:35:29,920 --> 07:35:37,360
in a property called product. So let's
8912
07:35:32,878 --> 07:35:39,280
actually rename this to order product.
8913
07:35:37,360 --> 07:35:44,240
And then here we're going to do the
8914
07:35:39,280 --> 07:35:46,798
order product dotproduct ID and the
8915
07:35:44,240 --> 07:35:49,440
order product.product.im
8916
07:35:46,798 --> 07:35:52,440
image and the order
8917
07:35:49,440 --> 07:35:52,440
product.product.name.
8918
07:35:52,798 --> 07:35:57,280
And then down here to replace the
8919
07:35:54,718 --> 07:36:01,040
delivery date. This is directly on the
8920
07:35:57,280 --> 07:36:05,040
order product. So we can remove this and
8921
07:36:01,040 --> 07:36:08,958
insert and we'll use dayjs and bracket
8922
07:36:05,040 --> 07:36:13,840
and give it the order product dot
8923
07:36:08,958 --> 07:36:17,520
estimated delivery time ms and outside
8924
07:36:13,840 --> 07:36:20,718
these brackets we'll do dot format
8925
07:36:17,520 --> 07:36:24,000
brackets and a string and we'll use the
8926
07:36:20,718 --> 07:36:27,840
format capital M four times for the
8927
07:36:24,000 --> 07:36:30,398
month and capital D for the date. And
8928
07:36:27,840 --> 07:36:33,120
then down here for the quantity, we're
8929
07:36:30,398 --> 07:36:36,718
going to insert and the quantity is
8930
07:36:33,120 --> 07:36:38,240
directly on the order product dot
8931
07:36:36,718 --> 07:36:40,878
quantity
8932
07:36:38,240 --> 07:36:44,080
and save.
8933
07:36:40,878 --> 07:36:48,718
And now if we scroll down, we can also
8934
07:36:44,080 --> 07:36:52,000
remove the rest of this default HTML
8935
07:36:48,718 --> 07:36:54,000
and save this file. And now let's check
8936
07:36:52,000 --> 07:36:56,080
our website.
8937
07:36:54,000 --> 07:36:58,240
And everything should be working except
8938
07:36:56,080 --> 07:37:01,760
we're generating the HTML for these
8939
07:36:58,240 --> 07:37:04,320
orders using data from the back end. So
8940
07:37:01,760 --> 07:37:08,320
now let's go back and we'll save this
8941
07:37:04,320 --> 07:37:11,600
into git. We'll open the git section
8942
07:37:08,320 --> 07:37:20,600
and we'll give it the message generate
8943
07:37:11,600 --> 07:37:20,600
the HTML for the orders page and commit.
8944
07:37:22,478 --> 07:37:27,840
Next, if we look at our code right now,
8945
07:37:25,680 --> 07:37:30,798
you'll notice we have a lot of code in
8946
07:37:27,840 --> 07:37:33,200
one file. To make our code cleaner, we
8947
07:37:30,798 --> 07:37:36,558
should separate our code into smaller
8948
07:37:33,200 --> 07:37:38,080
components. So, let's go to our files
8949
07:37:36,558 --> 07:37:40,558
and we're going to start with the
8950
07:37:38,080 --> 07:37:43,200
homepage.
8951
07:37:40,558 --> 07:37:46,320
So, inside the homepage, we're showing a
8952
07:37:43,200 --> 07:37:48,718
grid of products. To make the code
8953
07:37:46,320 --> 07:37:51,840
cleaner, we can separate this into its
8954
07:37:48,718 --> 07:37:54,638
own component. So, we'll go to the pages
8955
07:37:51,840 --> 07:37:57,360
folder and we'll click it and we're
8956
07:37:54,638 --> 07:38:03,240
going to create a new file and let's
8957
07:37:57,360 --> 07:38:03,240
call it the products grid.jsx.
8958
07:38:04,240 --> 07:38:10,320
And now we have multiple components that
8959
07:38:07,280 --> 07:38:13,440
are related to the homepage. So, usually
8960
07:38:10,320 --> 07:38:15,440
we organize this into a folder. So,
8961
07:38:13,440 --> 07:38:19,040
inside here, we're going to create a new
8962
07:38:15,440 --> 07:38:21,120
folder and we're going to call it home
8963
07:38:19,040 --> 07:38:23,200
and press enter.
8964
07:38:21,120 --> 07:38:26,080
And we group all the components related
8965
07:38:23,200 --> 07:38:29,520
to the homepage inside here. So, we'll
8966
07:38:26,080 --> 07:38:32,798
move the homepage.jsx.
8967
07:38:29,520 --> 07:38:36,398
And let's update the imports
8968
07:38:32,798 --> 07:38:40,760
and also homepage.css
8969
07:38:36,398 --> 07:38:40,760
and also the products grid.
8970
07:38:41,520 --> 07:38:45,840
And whenever we move files, we need to
8971
07:38:43,440 --> 07:38:48,958
make sure the imports are still correct.
8972
07:38:45,840 --> 07:38:51,120
So in homepage, I'm going to scroll up
8973
07:38:48,958 --> 07:38:53,440
and we should make sure that we have two
8974
07:38:51,120 --> 07:38:55,920
dot dot slashes in front because now
8975
07:38:53,440 --> 07:38:58,398
we're inside an additional folder. So
8976
07:38:55,920 --> 07:39:01,520
that looks good to me. And we're also
8977
07:38:58,398 --> 07:39:04,080
going to go to app.jsx
8978
07:39:01,520 --> 07:39:07,360
and scroll up and make sure that the
8979
07:39:04,080 --> 07:39:11,680
homepage is now inside the home folder.
8980
07:39:07,360 --> 07:39:14,638
So now let's save and check our website
8981
07:39:11,680 --> 07:39:17,440
and go to the homepage to make sure it
8982
07:39:14,638 --> 07:39:19,680
still works. So now let's go back to our
8983
07:39:17,440 --> 07:39:22,958
code and we're going to separate the
8984
07:39:19,680 --> 07:39:25,440
products grid into its own file. So in
8985
07:39:22,958 --> 07:39:28,798
here we'll create the component using
8986
07:39:25,440 --> 07:39:32,000
export function
8987
07:39:28,798 --> 07:39:36,478
products grid brackets and curly
8988
07:39:32,000 --> 07:39:38,798
brackets and we'll return some HTML
8989
07:39:36,478 --> 07:39:41,920
and we're just going to copy the HTML to
8990
07:39:38,798 --> 07:39:44,958
here. So, inside the homepage, we're
8991
07:39:41,920 --> 07:39:48,478
going to scroll down to the products
8992
07:39:44,958 --> 07:39:52,798
grid and we can collapse this element
8993
07:39:48,478 --> 07:39:55,440
and select all this code and copy
8994
07:39:52,798 --> 07:39:58,160
inside the products grid. Inside here,
8995
07:39:55,440 --> 07:40:01,200
we can paste
8996
07:39:58,160 --> 07:40:03,760
and press control or commandshiftp and
8997
07:40:01,200 --> 07:40:05,840
format document.
8998
07:40:03,760 --> 07:40:08,478
Now, we'll scroll up and we're going to
8999
07:40:05,840 --> 07:40:10,798
fix these errors. So first we need
9000
07:40:08,478 --> 07:40:14,000
access to the products. So we can pass
9001
07:40:10,798 --> 07:40:15,840
those in as a prop curly brackets
9002
07:40:14,000 --> 07:40:18,558
products.
9003
07:40:15,840 --> 07:40:22,160
And down here we also need the format
9004
07:40:18,558 --> 07:40:27,280
money function. So at the top we're
9005
07:40:22,160 --> 07:40:28,798
going to import curly brackets format
9006
07:40:27,280 --> 07:40:31,920
money
9007
07:40:28,798 --> 07:40:33,920
from a string and we'll find that utils
9008
07:40:31,920 --> 07:40:36,638
file. So we need to get out of two
9009
07:40:33,920 --> 07:40:40,160
folders this time using dot dot slash
9010
07:40:36,638 --> 07:40:42,398
dot dot slash utils/money
9011
07:40:40,160 --> 07:40:46,240
and save.
9012
07:40:42,398 --> 07:40:48,718
And now inside the homepage at the top
9013
07:40:46,240 --> 07:40:51,840
we're going to import our products grid.
9014
07:40:48,718 --> 07:40:55,040
So import curly brackets
9015
07:40:51,840 --> 07:40:58,320
the products grid
9016
07:40:55,040 --> 07:41:01,680
from a string and it's right beside the
9017
07:40:58,320 --> 07:41:04,320
homepage. So we can just do dot slash
9018
07:41:01,680 --> 07:41:06,958
products grid.
9019
07:41:04,320 --> 07:41:09,040
And then down here we can remove this
9020
07:41:06,958 --> 07:41:12,320
HTML
9021
07:41:09,040 --> 07:41:16,320
and just use our component less than
9022
07:41:12,320 --> 07:41:18,638
products grid slashgreater than and we
9023
07:41:16,320 --> 07:41:22,000
need to give it the products. So we'll
9024
07:41:18,638 --> 07:41:26,080
create a prop products equals curly
9025
07:41:22,000 --> 07:41:28,160
brackets and we'll give it the products.
9026
07:41:26,080 --> 07:41:30,558
So, as you can see, this makes the code
9027
07:41:28,160 --> 07:41:33,680
much cleaner when we separate it into
9028
07:41:30,558 --> 07:41:36,798
smaller components. The last step is at
9029
07:41:33,680 --> 07:41:38,478
the top, we can remove this format money
9030
07:41:36,798 --> 07:41:41,840
function since we're not using it
9031
07:41:38,478 --> 07:41:44,798
anymore. Let's save
9032
07:41:41,840 --> 07:41:47,280
and let's open the website and make sure
9033
07:41:44,798 --> 07:41:48,958
everything still works. So, that looks
9034
07:41:47,280 --> 07:41:50,638
good.
9035
07:41:48,958 --> 07:41:54,638
Next, we're going to do the checkout
9036
07:41:50,638 --> 07:41:56,718
page. So, we'll go back to our code
9037
07:41:54,638 --> 07:41:59,440
and we're going to open the checkout
9038
07:41:56,718 --> 07:42:02,440
page and we're going to scroll to the
9039
07:41:59,440 --> 07:42:02,440
top.
9040
07:42:02,958 --> 07:42:07,520
And I'll leave separating the checkout
9041
07:42:04,958 --> 07:42:10,478
header as an exercise. But we're going
9042
07:42:07,520 --> 07:42:12,638
to scroll down here and we can move the
9043
07:42:10,478 --> 07:42:16,000
order summary section into its own
9044
07:42:12,638 --> 07:42:18,000
component. So, inside the pages folder,
9045
07:42:16,000 --> 07:42:21,280
I'm going to click it and create a new
9046
07:42:18,000 --> 07:42:23,120
folder called checkout.
9047
07:42:21,280 --> 07:42:25,120
Now, you might have already done this if
9048
07:42:23,120 --> 07:42:27,280
you did the exercises from the previous
9049
07:42:25,120 --> 07:42:29,760
lesson, but we're going to use this
9050
07:42:27,280 --> 07:42:33,200
folder checkout. And I'm going to move
9051
07:42:29,760 --> 07:42:36,398
the checkout page inside here.
9052
07:42:33,200 --> 07:42:39,958
And update the imports and also move the
9053
07:42:36,398 --> 07:42:39,958
CSS files.
9054
07:42:40,478 --> 07:42:46,000
And I'll make sure that the imports look
9055
07:42:42,320 --> 07:42:49,120
good. So, in here at the top, make sure
9056
07:42:46,000 --> 07:42:52,080
there's two dot dot slashes in front.
9057
07:42:49,120 --> 07:42:53,680
And inside app.jsx,
9058
07:42:52,080 --> 07:42:56,718
make sure that there is a checkout
9059
07:42:53,680 --> 07:43:00,080
folder in front of this component. So
9060
07:42:56,718 --> 07:43:03,040
I'll go back to the checkout page and
9061
07:43:00,080 --> 07:43:07,878
I'll create a new file. Let's call it
9062
07:43:03,040 --> 07:43:07,878
the order summary.jsx.
9063
07:43:09,200 --> 07:43:16,398
And we're going to create export
9064
07:43:12,080 --> 07:43:18,798
function order summary brackets and
9065
07:43:16,398 --> 07:43:23,120
curly brackets. and we're going to
9066
07:43:18,798 --> 07:43:26,798
return brackets the HTML for the order
9067
07:43:23,120 --> 07:43:29,520
summary. Inside the checkout page, we're
9068
07:43:26,798 --> 07:43:31,760
going to scroll down
9069
07:43:29,520 --> 07:43:35,040
and find the order summary. We can
9070
07:43:31,760 --> 07:43:36,718
collapse it and select this code and
9071
07:43:35,040 --> 07:43:40,478
copy.
9072
07:43:36,718 --> 07:43:43,440
Inside the order summary, we'll paste
9073
07:43:40,478 --> 07:43:46,080
and press control or command shift P and
9074
07:43:43,440 --> 07:43:48,798
format document.
9075
07:43:46,080 --> 07:43:50,878
and we'll scroll to the top and we're
9076
07:43:48,798 --> 07:43:53,120
going to need the delivery options and
9077
07:43:50,878 --> 07:43:55,040
the cart inside here. So, we're going to
9078
07:43:53,120 --> 07:43:58,320
get this from the props using curly
9079
07:43:55,040 --> 07:44:01,280
brackets, the cart, comma, and the
9080
07:43:58,320 --> 07:44:04,240
delivery options.
9081
07:44:01,280 --> 07:44:07,360
And we also need dayjs and the format
9082
07:44:04,240 --> 07:44:09,920
money function. So at the top we'll also
9083
07:44:07,360 --> 07:44:15,760
import dayjs
9084
07:44:09,920 --> 07:44:18,798
from day js and import curly brackets
9085
07:44:15,760 --> 07:44:21,680
format money
9086
07:44:18,798 --> 07:44:24,958
from a string
9087
07:44:21,680 --> 07:44:28,080
and we'll find the utils file which is
9088
07:44:24,958 --> 07:44:30,558
dot dot slash dot dot slash utils
9089
07:44:28,080 --> 07:44:33,440
slashmoney
9090
07:44:30,558 --> 07:44:35,520
and all the errors should now be gone.
9091
07:44:33,440 --> 07:44:38,798
We'll save.
9092
07:44:35,520 --> 07:44:40,558
And inside checkout page, we can remove
9093
07:44:38,798 --> 07:44:42,878
this code.
9094
07:44:40,558 --> 07:44:48,398
And we can use our new component less
9095
07:44:42,878 --> 07:44:50,798
than order summary /grater than. And we
9096
07:44:48,398 --> 07:44:53,840
need to give it the cart equals curly
9097
07:44:50,798 --> 07:44:57,440
brackets and save the cart. And we need
9098
07:44:53,840 --> 07:45:01,120
to give it the delivery options equals
9099
07:44:57,440 --> 07:45:04,080
curly brackets. And give it the delivery
9100
07:45:01,120 --> 07:45:08,240
options. And we need to import this at
9101
07:45:04,080 --> 07:45:12,558
the top. So we'll scroll up and here we
9102
07:45:08,240 --> 07:45:14,398
can import curly brackets the order
9103
07:45:12,558 --> 07:45:21,040
summary
9104
07:45:14,398 --> 07:45:24,240
from a string dot slash order summary.
9105
07:45:21,040 --> 07:45:26,958
And we don't need dayjs anymore.
9106
07:45:24,240 --> 07:45:29,920
So let's save
9107
07:45:26,958 --> 07:45:32,000
and check the website. And everything
9108
07:45:29,920 --> 07:45:33,920
still works.
9109
07:45:32,000 --> 07:45:36,240
We'll go back
9110
07:45:33,920 --> 07:45:38,320
and scroll down.
9111
07:45:36,240 --> 07:45:40,878
And next, we can also move the payment
9112
07:45:38,320 --> 07:45:42,558
summary into its own component. Feel
9113
07:45:40,878 --> 07:45:45,280
free to pause the video if you want to
9114
07:45:42,558 --> 07:45:47,440
try it yourself first. So, inside the
9115
07:45:45,280 --> 07:45:52,798
checkout folder, we'll click here and
9116
07:45:47,440 --> 07:45:55,600
create a new file, payment summary.jsx,
9117
07:45:52,798 --> 07:45:59,040
and we'll create the component export
9118
07:45:55,600 --> 07:46:02,160
function payment summary brackets and
9119
07:45:59,040 --> 07:46:03,840
curly brackets. and we'll return the
9120
07:46:02,160 --> 07:46:06,478
HTML.
9121
07:46:03,840 --> 07:46:08,878
Inside the checkout page, we're going to
9122
07:46:06,478 --> 07:46:11,920
collapse this element and select the
9123
07:46:08,878 --> 07:46:14,080
HTML and copy
9124
07:46:11,920 --> 07:46:15,840
inside the payment summary. We're going
9125
07:46:14,080 --> 07:46:18,320
to paste
9126
07:46:15,840 --> 07:46:19,920
control or command shiftp format
9127
07:46:18,320 --> 07:46:22,798
document
9128
07:46:19,920 --> 07:46:24,240
and we're going to scroll back up and
9129
07:46:22,798 --> 07:46:27,360
we're going to get the payment summary
9130
07:46:24,240 --> 07:46:30,718
from the props. So curly brackets
9131
07:46:27,360 --> 07:46:34,320
payment summary and we need the format
9132
07:46:30,718 --> 07:46:39,200
money function. So up here import curly
9133
07:46:34,320 --> 07:46:43,000
brackets format money from a string dot
9134
07:46:39,200 --> 07:46:43,000
dot slash dot do/youutils/money
9135
07:46:43,600 --> 07:46:49,120
and save.
9136
07:46:46,398 --> 07:46:52,638
In the checkout page, we can remove this
9137
07:46:49,120 --> 07:46:56,398
code and replace it with less than
9138
07:46:52,638 --> 07:47:00,320
payment summary slashgreater than. And
9139
07:46:56,398 --> 07:47:02,718
we need to give this the payment summary
9140
07:47:00,320 --> 07:47:06,000
equals curly brackets and save the
9141
07:47:02,718 --> 07:47:08,320
payment summary.
9142
07:47:06,000 --> 07:47:10,798
And as you can see, our code is much
9143
07:47:08,320 --> 07:47:13,600
cleaner now because we separated it into
9144
07:47:10,798 --> 07:47:15,840
smaller components. So now we need to
9145
07:47:13,600 --> 07:47:19,600
import this. So, we're going to scroll
9146
07:47:15,840 --> 07:47:23,280
to the top and here we'll import curly
9147
07:47:19,600 --> 07:47:26,958
brackets the payment summary
9148
07:47:23,280 --> 07:47:28,718
from a string dot slashpayment
9149
07:47:26,958 --> 07:47:32,160
summary.
9150
07:47:28,718 --> 07:47:35,120
And we don't need format money anymore
9151
07:47:32,160 --> 07:47:37,840
and save.
9152
07:47:35,120 --> 07:47:40,798
Let's go back to the website and
9153
07:47:37,840 --> 07:47:43,440
everything is still working.
9154
07:47:40,798 --> 07:47:46,000
We'll go back to our code
9155
07:47:43,440 --> 07:47:48,240
and we're going to go inside the order
9156
07:47:46,000 --> 07:47:50,958
summary component.
9157
07:47:48,240 --> 07:47:53,600
So in here the code is still a little
9158
07:47:50,958 --> 07:47:56,000
bit too complicated. So what we can do
9159
07:47:53,600 --> 07:47:58,080
is we can find this element with the
9160
07:47:56,000 --> 07:48:00,718
class delivery options and we can
9161
07:47:58,080 --> 07:48:02,878
separate this into its own component as
9162
07:48:00,718 --> 07:48:05,360
well. Feel free to pause the video if
9163
07:48:02,878 --> 07:48:07,360
you want to try this yourself first. So,
9164
07:48:05,360 --> 07:48:10,878
I'm going to go to the checkout folder
9165
07:48:07,360 --> 07:48:15,160
and create a new file and name it
9166
07:48:10,878 --> 07:48:15,160
delivery options.jsx.
9167
07:48:15,600 --> 07:48:22,160
And we'll create a component export
9168
07:48:18,000 --> 07:48:26,878
function delivery options brackets and
9169
07:48:22,160 --> 07:48:29,600
curly brackets and return some HTML. And
9170
07:48:26,878 --> 07:48:33,040
back to the order summary. And here we
9171
07:48:29,600 --> 07:48:34,958
can collapse this element and select it
9172
07:48:33,040 --> 07:48:38,160
and copy.
9173
07:48:34,958 --> 07:48:42,160
Inside delivery options inside here we
9174
07:48:38,160 --> 07:48:44,398
can paste. Control or command shiftp and
9175
07:48:42,160 --> 07:48:47,200
format document.
9176
07:48:44,398 --> 07:48:49,200
And we'll scroll to the top. Here we
9177
07:48:47,200 --> 07:48:52,558
need to get the delivery options using
9178
07:48:49,200 --> 07:48:54,240
the props. So curly brackets delivery
9179
07:48:52,558 --> 07:48:57,120
options.
9180
07:48:54,240 --> 07:49:00,718
And we're going to import format money.
9181
07:48:57,120 --> 07:49:05,920
So up here we're going to import curly
9182
07:49:00,718 --> 07:49:09,840
brackets format money from a string dot
9183
07:49:05,920 --> 07:49:13,840
dot slash dot dot slash utils/money
9184
07:49:09,840 --> 07:49:19,680
and we also need dayjs. So up here we'll
9185
07:49:13,840 --> 07:49:22,000
import dayjs from a string dayjs
9186
07:49:19,680 --> 07:49:24,478
and I think we also need another thing
9187
07:49:22,000 --> 07:49:26,000
here which is the cart item. So we're
9188
07:49:24,478 --> 07:49:26,958
going to get this from the props as
9189
07:49:26,000 --> 07:49:30,398
well.
9190
07:49:26,958 --> 07:49:34,638
So up here we're also going to get the
9191
07:49:30,398 --> 07:49:37,200
cart item and save.
9192
07:49:34,638 --> 07:49:40,320
And now in the order summary we can
9193
07:49:37,200 --> 07:49:45,040
replace all this code with our component
9194
07:49:40,320 --> 07:49:48,000
less than delivery options /grater than.
9195
07:49:45,040 --> 07:49:50,718
And we're going to give it the cart item
9196
07:49:48,000 --> 07:49:55,360
equals curly brackets. save the cart
9197
07:49:50,718 --> 07:49:58,798
item and give it the delivery options
9198
07:49:55,360 --> 07:50:00,718
equals curly brackets save the delivery
9199
07:49:58,798 --> 07:50:03,440
options
9200
07:50:00,718 --> 07:50:05,600
and we also need to import this. So at
9201
07:50:03,440 --> 07:50:09,440
the top
9202
07:50:05,600 --> 07:50:12,160
we can import curly brackets the
9203
07:50:09,440 --> 07:50:14,398
delivery options
9204
07:50:12,160 --> 07:50:18,240
from a string
9205
07:50:14,398 --> 07:50:20,398
dot slash delivery options
9206
07:50:18,240 --> 07:50:22,798
and save
9207
07:50:20,398 --> 07:50:25,280
and let's check our website and make
9208
07:50:22,798 --> 07:50:29,200
sure it still works. So that looks good
9209
07:50:25,280 --> 07:50:31,040
to me. So if we go back to our code, you
9210
07:50:29,200 --> 07:50:33,200
can see that our code is cleaner now
9211
07:50:31,040 --> 07:50:36,000
after we separate this into another
9212
07:50:33,200 --> 07:50:38,478
component. And remember, it's up to us
9213
07:50:36,000 --> 07:50:40,398
how we want to separate our components.
9214
07:50:38,478 --> 07:50:43,440
We can actually keep separating this
9215
07:50:40,398 --> 07:50:46,000
into smaller components if we want. For
9216
07:50:43,440 --> 07:50:48,718
example, we can also have a cart item
9217
07:50:46,000 --> 07:50:50,878
details component or a delivery date
9218
07:50:48,718 --> 07:50:53,600
component, but I'll leave these as
9219
07:50:50,878 --> 07:50:56,320
exercises. I'm also going to leave the
9220
07:50:53,600 --> 07:50:59,760
orders page as an exercise, but we can
9221
07:50:56,320 --> 07:51:02,320
just get started on it. So, here inside
9222
07:50:59,760 --> 07:51:05,280
the pages folder, we're going to create
9223
07:51:02,320 --> 07:51:07,280
a new folder for the orders page. So,
9224
07:51:05,280 --> 07:51:09,200
orders
9225
07:51:07,280 --> 07:51:12,000
and we're going to move this component
9226
07:51:09,200 --> 07:51:14,558
inside here
9227
07:51:12,000 --> 07:51:16,000
and update the imports.
9228
07:51:14,558 --> 07:51:19,200
And we're going to move this inside here
9229
07:51:16,000 --> 07:51:22,718
as well. And just make sure the imports
9230
07:51:19,200 --> 07:51:25,120
look okay. So in here, we'll scroll up.
9231
07:51:22,718 --> 07:51:28,718
Make sure that these two have two dot
9232
07:51:25,120 --> 07:51:30,798
dot slashes in front. That looks good.
9233
07:51:28,718 --> 07:51:32,718
And in app.jsx,
9234
07:51:30,798 --> 07:51:36,000
make sure that the orders page is now
9235
07:51:32,718 --> 07:51:39,920
inside an orders folder. So we'll save
9236
07:51:36,000 --> 07:51:41,840
this and open the website.
9237
07:51:39,920 --> 07:51:44,718
And we're going to click here to go to
9238
07:51:41,840 --> 07:51:47,280
home and click here to go to orders. And
9239
07:51:44,718 --> 07:51:49,520
the orders page should still be working.
9240
07:51:47,280 --> 07:51:52,958
So I'll leave separating this as an
9241
07:51:49,520 --> 07:51:56,080
exercise. We'll go back to our code and
9242
07:51:52,958 --> 07:51:59,360
we'll save this into git. Inside the git
9243
07:51:56,080 --> 07:52:04,798
section, we'll create the message
9244
07:51:59,360 --> 07:52:07,798
separate into smaller components and
9245
07:52:04,798 --> 07:52:07,798
commit.
9246
07:52:10,638 --> 07:52:15,520
Next, we're going to go back to our
9247
07:52:12,320 --> 07:52:17,920
files and we're going to go to our
9248
07:52:15,520 --> 07:52:21,360
homepage.
9249
07:52:17,920 --> 07:52:24,160
and we're going to review async await.
9250
07:52:21,360 --> 07:52:26,478
Async await is a feature of JavaScript
9251
07:52:24,160 --> 07:52:30,160
and it lets us write asynchronous code
9252
07:52:26,478 --> 07:52:33,840
like normal code. Right now we're using
9253
07:52:30,160 --> 07:52:36,160
promises which means we need to use to
9254
07:52:33,840 --> 07:52:39,680
wait for this code to finish and then
9255
07:52:36,160 --> 07:52:42,080
run this function with async await. We
9256
07:52:39,680 --> 07:52:45,680
can actually write this like normal
9257
07:52:42,080 --> 07:52:50,798
code. To do that in front of axios we
9258
07:52:45,680 --> 07:52:53,840
can type await and space. So this is
9259
07:52:50,798 --> 07:52:55,760
going to wait for this code to finish
9260
07:52:53,840 --> 07:52:58,798
and then it's going to move on to the
9261
07:52:55,760 --> 07:53:01,040
next line. And now because we're waiting
9262
07:52:58,798 --> 07:53:03,840
for this code to finish, we can actually
9263
07:53:01,040 --> 07:53:07,360
save this result in a variable. So at
9264
07:53:03,840 --> 07:53:12,398
the front we can save it in const
9265
07:53:07,360 --> 07:53:14,878
response equals await this code.
9266
07:53:12,398 --> 07:53:16,798
And now we don't need to use dot then
9267
07:53:14,878 --> 07:53:19,360
anymore
9268
07:53:16,798 --> 07:53:22,240
and we can remove it.
9269
07:53:19,360 --> 07:53:25,680
However, in order to use a weight, we
9270
07:53:22,240 --> 07:53:27,760
need to be inside an async function. So
9271
07:53:25,680 --> 07:53:32,878
in front of this function, we need to
9272
07:53:27,760 --> 07:53:36,080
add async and space. As you can see,
9273
07:53:32,878 --> 07:53:39,600
async8 lets us write asynchronous code
9274
07:53:36,080 --> 07:53:42,878
like normal code or code that runs line
9275
07:53:39,600 --> 07:53:46,320
by line. And async await makes the code
9276
07:53:42,878 --> 07:53:48,798
easier to understand. So in react it's
9277
07:53:46,320 --> 07:53:52,478
recommended to use async await instead
9278
07:53:48,798 --> 07:53:54,958
of promises when we can. However, in
9279
07:53:52,478 --> 07:53:58,478
React there is a small problem with
9280
07:53:54,958 --> 07:54:01,280
using async await and use effect. As a
9281
07:53:58,478 --> 07:54:04,478
review, when we put async in front of a
9282
07:54:01,280 --> 07:54:07,680
function, this function will now return
9283
07:54:04,478 --> 07:54:10,478
a promise. That's because this function
9284
07:54:07,680 --> 07:54:12,958
has some asynchronous code inside or
9285
07:54:10,478 --> 07:54:15,680
code that takes some time to finish. So
9286
07:54:12,958 --> 07:54:17,520
that means that this entire function is
9287
07:54:15,680 --> 07:54:20,080
going to take some time to finish.
9288
07:54:17,520 --> 07:54:22,958
That's why it has to return a promise.
9289
07:54:20,080 --> 07:54:26,000
Now the problem is the inner function in
9290
07:54:22,958 --> 07:54:28,320
use effect should not return a promise.
9291
07:54:26,000 --> 07:54:31,520
It should only return nothing or a
9292
07:54:28,320 --> 07:54:33,360
cleanup function like this. A cleanup
9293
07:54:31,520 --> 07:54:36,638
function is useful if we want to do some
9294
07:54:33,360 --> 07:54:38,638
cleanup when this component is removed.
9295
07:54:36,638 --> 07:54:40,878
So because this inner function is now
9296
07:54:38,638 --> 07:54:44,000
returning a promise, it's sort of
9297
07:54:40,878 --> 07:54:46,558
breaking the rules of use effect. To
9298
07:54:44,000 --> 07:54:50,478
solve this, we usually create a new
9299
07:54:46,558 --> 07:54:54,798
function inside use effect like const
9300
07:54:50,478 --> 07:54:57,200
get home data equals and create a new
9301
07:54:54,798 --> 07:54:59,840
function brackets arrow and curly
9302
07:54:57,200 --> 07:55:03,360
brackets. And we're going to make this
9303
07:54:59,840 --> 07:55:06,080
function async. And we'll leave this as
9304
07:55:03,360 --> 07:55:08,000
an ordinary function.
9305
07:55:06,080 --> 07:55:11,200
And now we're going to move our code
9306
07:55:08,000 --> 07:55:13,920
inside here. So we'll cut it and paste
9307
07:55:11,200 --> 07:55:16,478
it. And now because we're inside an
9308
07:55:13,920 --> 07:55:19,920
async function, we can now use a weight
9309
07:55:16,478 --> 07:55:21,920
and it will also not mess up use effect.
9310
07:55:19,920 --> 07:55:26,718
At the end, we just need to run this
9311
07:55:21,920 --> 07:55:28,398
function using get home data and
9312
07:55:26,718 --> 07:55:31,120
brackets.
9313
07:55:28,398 --> 07:55:34,320
So this gets the same data as before but
9314
07:55:31,120 --> 07:55:37,600
it uses async await. So remember when
9315
07:55:34,320 --> 07:55:40,240
using async await inside use effect we
9316
07:55:37,600 --> 07:55:42,958
need to create a new function and then
9317
07:55:40,240 --> 07:55:47,360
run the function otherwise it will break
9318
07:55:42,958 --> 07:55:50,000
the rules of use effect. So let's save
9319
07:55:47,360 --> 07:55:52,878
and check the homepage
9320
07:55:50,000 --> 07:55:56,478
and go to the homepage and everything
9321
07:55:52,878 --> 07:55:59,360
still works. So let's go back
9322
07:55:56,478 --> 07:56:01,200
and we'll use async await in the app
9323
07:55:59,360 --> 07:56:05,440
component
9324
07:56:01,200 --> 07:56:07,280
where we get the cart. So here feel free
9325
07:56:05,440 --> 07:56:10,478
to pause if you want to try yourself
9326
07:56:07,280 --> 07:56:15,440
first. We'll create a new function using
9327
07:56:10,478 --> 07:56:18,478
const. Let's call it fetch app data
9328
07:56:15,440 --> 07:56:20,558
equals a function brackets arrow and
9329
07:56:18,478 --> 07:56:22,398
curly brackets.
9330
07:56:20,558 --> 07:56:26,398
And we'll move this code into the
9331
07:56:22,398 --> 07:56:29,520
function. So we'll cut and paste and
9332
07:56:26,398 --> 07:56:32,000
we'll select this and press tab.
9333
07:56:29,520 --> 07:56:36,160
In front of the function we'll use
9334
07:56:32,000 --> 07:56:39,680
async. And now we can use await inside.
9335
07:56:36,160 --> 07:56:42,080
So in front of axios we'll type await to
9336
07:56:39,680 --> 07:56:44,558
wait for this to finish. And then we're
9337
07:56:42,080 --> 07:56:49,280
going to save the result in a variable
9338
07:56:44,558 --> 07:56:51,840
const response equals this result.
9339
07:56:49,280 --> 07:56:55,638
And now we can remove
9340
07:56:51,840 --> 07:56:55,638
and these brackets
9341
07:56:56,080 --> 07:57:01,520
and the code is much cleaner. Finally,
9342
07:56:58,798 --> 07:57:06,638
we're going to run this function fetch
9343
07:57:01,520 --> 07:57:08,398
app data and brackets and save.
9344
07:57:06,638 --> 07:57:10,478
And let's do the same thing in the
9345
07:57:08,398 --> 07:57:12,958
checkout page. So we'll go to the
9346
07:57:10,478 --> 07:57:14,638
checkout page here.
9347
07:57:12,958 --> 07:57:19,760
Feel free to pause if you want to try it
9348
07:57:14,638 --> 07:57:23,840
yourself. const fetch checkout data
9349
07:57:19,760 --> 07:57:28,398
equals brackets arrow and curly brackets
9350
07:57:23,840 --> 07:57:32,320
and we'll move this code inside cut
9351
07:57:28,398 --> 07:57:35,520
and in here paste and select these lines
9352
07:57:32,320 --> 07:57:38,320
and press tab and we're going to turn
9353
07:57:35,520 --> 07:57:41,440
this into an async function so we can
9354
07:57:38,320 --> 07:57:44,080
use await inside. So in front of this
9355
07:57:41,440 --> 07:57:46,718
request, we're going to await.
9356
07:57:44,080 --> 07:57:49,360
And we're going to save it in a variable
9357
07:57:46,718 --> 07:57:51,680
let response
9358
07:57:49,360 --> 07:57:53,840
equals. So we're going to use let
9359
07:57:51,680 --> 07:57:56,558
because we have two requests here and we
9360
07:57:53,840 --> 07:57:58,798
might want to reuse this variable. And
9361
07:57:56,558 --> 07:58:04,280
I'll add a new line to make it a little
9362
07:57:58,798 --> 07:58:04,280
easier to read. And now we can remove.
9363
07:58:05,760 --> 07:58:11,440
And then down here, we'll also use await
9364
07:58:08,798 --> 07:58:14,798
to wait for this to finish. And we're
9365
07:58:11,440 --> 07:58:16,398
going to save the result inside response
9366
07:58:14,798 --> 07:58:20,920
equals.
9367
07:58:16,398 --> 07:58:20,920
And now we can remove then.
9368
07:58:21,360 --> 07:58:28,240
And the last step is to run this
9369
07:58:23,040 --> 07:58:31,680
function. So fetch checkout data and
9370
07:58:28,240 --> 07:58:34,240
brackets and save.
9371
07:58:31,680 --> 07:58:36,958
We'll open the website. Make sure the
9372
07:58:34,240 --> 07:58:38,958
homepage is working and then go to the
9373
07:58:36,958 --> 07:58:42,160
checkout page and make sure this is
9374
07:58:38,958 --> 07:58:44,638
still working. So that looks good to me.
9375
07:58:42,160 --> 07:58:47,680
We'll go back to our code
9376
07:58:44,638 --> 07:58:51,200
and that's async await. It lets us write
9377
07:58:47,680 --> 07:58:53,200
asynchronous code like normal code. I'll
9378
07:58:51,200 --> 07:58:56,878
leave converting the orders page to
9379
07:58:53,200 --> 07:58:59,440
async aait as an exercise. So now we can
9380
07:58:56,878 --> 07:59:03,600
save this into git.
9381
07:58:59,440 --> 07:59:05,680
We'll give it the message convert home
9382
07:59:03,600 --> 07:59:12,600
and checkout
9383
07:59:05,680 --> 07:59:12,600
to async await and commit.
9384
07:59:14,798 --> 07:59:19,680
In the next lesson, we're going to learn
9385
07:59:16,878 --> 07:59:21,840
data mutation, which means instead of
9386
07:59:19,680 --> 07:59:24,478
just getting data from the back end,
9387
07:59:21,840 --> 07:59:26,320
we'll update data in the back end, and
9388
07:59:24,478 --> 07:59:29,600
we'll be able to add products to our
9389
07:59:26,320 --> 07:59:32,240
cart and create orders. And that's the
9390
07:59:29,600 --> 07:59:35,280
end of this lesson. In this lesson, we
9391
07:59:32,240 --> 07:59:38,478
reviewed backend, which manages the data
9392
07:59:35,280 --> 07:59:41,040
for a website. We set up a backend for
9393
07:59:38,478 --> 07:59:44,558
our project. We learned how to fetch
9394
07:59:41,040 --> 07:59:47,360
data or get data from the back end. We
9395
07:59:44,558 --> 07:59:49,760
learned Axios, which is an easier way to
9396
07:59:47,360 --> 07:59:52,638
make requests to the back end. We
9397
07:59:49,760 --> 07:59:55,760
learned how to generate the HTML using
9398
07:59:52,638 --> 07:59:59,120
the data from the back end. We separated
9399
07:59:55,760 --> 08:00:01,360
our pages into smaller components and we
9400
07:59:59,120 --> 08:00:04,000
learned async a weight which lets us
9401
08:00:01,360 --> 08:00:07,120
write asynchronous code like normal
9402
08:00:04,000 --> 08:00:09,200
code. Here are some exercises to help
9403
08:00:07,120 --> 08:00:12,200
you practice what we learned in this
9404
08:00:09,200 --> 08:00:12,200
lesson.
9405
08:01:03,360 --> 08:01:07,200
In this lesson, we're going to learn
9406
08:01:05,040 --> 08:01:10,558
data mutation and we're going to make
9407
08:01:07,200 --> 08:01:14,478
the website interactive. Data mutation
9408
08:01:10,558 --> 08:01:16,878
means we update data in the back end. If
9409
08:01:14,478 --> 08:01:20,638
we open the website
9410
08:01:16,878 --> 08:01:23,760
and I'll close some of these other tabs.
9411
08:01:20,638 --> 08:01:26,000
If we go to the homepage,
9412
08:01:23,760 --> 08:01:29,520
notice that right now we can only view
9413
08:01:26,000 --> 08:01:32,398
the data. If we try to update this data,
9414
08:01:29,520 --> 08:01:34,240
like we try to add to cart, it doesn't
9415
08:01:32,398 --> 08:01:37,280
do anything. So, we're going to make
9416
08:01:34,240 --> 08:01:39,280
this add to cart button work. We'll go
9417
08:01:37,280 --> 08:01:41,440
to our code
9418
08:01:39,280 --> 08:01:45,280
and we're going to find the add to cart
9419
08:01:41,440 --> 08:01:48,080
button. So, I'll open my files and I'm
9420
08:01:45,280 --> 08:01:51,440
going to go to the homepage and we're
9421
08:01:48,080 --> 08:01:52,958
going to open the products grid.
9422
08:01:51,440 --> 08:01:57,440
And we're going to scroll down in this
9423
08:01:52,958 --> 08:01:59,120
file to this add to cart button. The
9424
08:01:57,440 --> 08:02:02,320
first step is to make this button
9425
08:01:59,120 --> 08:02:05,600
interactive. So at the end we're going
9426
08:02:02,320 --> 08:02:08,798
to add an on click attribute equals
9427
08:02:05,600 --> 08:02:12,080
curly brackets and give this a function
9428
08:02:08,798 --> 08:02:15,040
brackets arrow and curly brackets.
9429
08:02:12,080 --> 08:02:17,840
As a review on click is called an event
9430
08:02:15,040 --> 08:02:20,080
handler. When we click this button it
9431
08:02:17,840 --> 08:02:22,718
will run this function.
9432
08:02:20,080 --> 08:02:25,600
Now, inside here, we're going to add the
9433
08:02:22,718 --> 08:02:28,878
product to the cart. So, we could try to
9434
08:02:25,600 --> 08:02:30,878
update the cart here in React, but then
9435
08:02:28,878 --> 08:02:34,558
we have to figure out where to save the
9436
08:02:30,878 --> 08:02:37,040
cart. Instead, in the real world, we use
9437
08:02:34,558 --> 08:02:39,360
the backend to update the cart and to
9438
08:02:37,040 --> 08:02:42,080
save the cart. The advantage of using
9439
08:02:39,360 --> 08:02:45,440
the backend is it will simplify our
9440
08:02:42,080 --> 08:02:47,520
React code and we can share the data in
9441
08:02:45,440 --> 08:02:49,760
the back end. So if we access this
9442
08:02:47,520 --> 08:02:52,000
website from another computer, we can
9443
08:02:49,760 --> 08:02:53,840
access the updated cart using the back
9444
08:02:52,000 --> 08:02:56,638
end.
9445
08:02:53,840 --> 08:02:58,878
So here to use a backend to add a
9446
08:02:56,638 --> 08:03:02,000
product to the cart. We're just going to
9447
08:02:58,878 --> 08:03:04,398
make another request to the back end.
9448
08:03:02,000 --> 08:03:07,360
We're going to go to the top
9449
08:03:04,398 --> 08:03:09,840
and at the top we're going to import
9450
08:03:07,360 --> 08:03:12,080
axios so that we can make another
9451
08:03:09,840 --> 08:03:15,680
request.
9452
08:03:12,080 --> 08:03:17,520
and we'll scroll back down.
9453
08:03:15,680 --> 08:03:19,440
And when we click this button, we're
9454
08:03:17,520 --> 08:03:22,320
going to make a request to the back end
9455
08:03:19,440 --> 08:03:26,958
using axios. And this time we're going
9456
08:03:22,320 --> 08:03:30,240
to use post and brackets.
9457
08:03:26,958 --> 08:03:34,478
So previously we used axios.get
9458
08:03:30,240 --> 08:03:37,440
to get data from the backend. Post is a
9459
08:03:34,478 --> 08:03:40,478
different type of request.
9460
08:03:37,440 --> 08:03:43,040
Is used to create data in the back end.
9461
08:03:40,478 --> 08:03:45,440
In this case, we are adding a product to
9462
08:03:43,040 --> 08:03:49,360
the back end. So, we're creating a new
9463
08:03:45,440 --> 08:03:51,760
cart item. That's why we use post.
9464
08:03:49,360 --> 08:03:56,840
Inside here, we're going to give it a
9465
08:03:51,760 --> 08:03:56,840
URL path slash API/cart-
9466
08:03:56,878 --> 08:04:03,200
items. So, notice this is the same URL
9467
08:04:00,398 --> 08:04:05,600
path that we used to get the cart. But
9468
08:04:03,200 --> 08:04:08,398
when we use post, the backend will
9469
08:04:05,600 --> 08:04:10,558
behave differently. This will create a
9470
08:04:08,398 --> 08:04:12,160
cart item instead of getting the cart
9471
08:04:10,558 --> 08:04:14,478
items.
9472
08:04:12,160 --> 08:04:17,840
Next, we need to tell the backend which
9473
08:04:14,478 --> 08:04:21,440
product to add to the cart. To do that,
9474
08:04:17,840 --> 08:04:24,240
we can give axios.post another value
9475
08:04:21,440 --> 08:04:27,360
which is an object.
9476
08:04:24,240 --> 08:04:30,240
When we use post, we can also send
9477
08:04:27,360 --> 08:04:32,878
information to the back end using an
9478
08:04:30,240 --> 08:04:36,160
object. And this is called the request
9479
08:04:32,878 --> 08:04:38,398
body. Now the back end needs to know two
9480
08:04:36,160 --> 08:04:41,520
things in order to add a product to the
9481
08:04:38,398 --> 08:04:46,240
cart. First, it needs to know the
9482
08:04:41,520 --> 08:04:49,840
product ID that we want to add. In this
9483
08:04:46,240 --> 08:04:53,360
case, this button is for if we scroll
9484
08:04:49,840 --> 08:04:57,040
up, the button is for this product right
9485
08:04:53,360 --> 08:04:58,798
here. So, at the bottom,
9486
08:04:57,040 --> 08:05:01,520
we're going to tell the back end we want
9487
08:04:58,798 --> 08:05:05,120
to add this product
9488
08:05:01,520 --> 08:05:07,520
ID to the cart. And we also need another
9489
08:05:05,120 --> 08:05:10,080
information which is the quantity that
9490
08:05:07,520 --> 08:05:13,920
we want to add. For now, let's just use
9491
08:05:10,080 --> 08:05:16,638
the quantity one. So this code will send
9492
08:05:13,920 --> 08:05:19,120
a request to the back end telling it to
9493
08:05:16,638 --> 08:05:21,840
add this product to the cart with a
9494
08:05:19,120 --> 08:05:24,878
quantity of one.
9495
08:05:21,840 --> 08:05:28,080
So now let's save this file
9496
08:05:24,878 --> 08:05:30,160
and then we'll go to the website
9497
08:05:28,080 --> 08:05:32,478
and we're going to go to this t-shirt
9498
08:05:30,160 --> 08:05:34,558
product and we're going to click add to
9499
08:05:32,478 --> 08:05:36,240
cart.
9500
08:05:34,558 --> 08:05:38,320
And then we're going to refresh the
9501
08:05:36,240 --> 08:05:40,718
page.
9502
08:05:38,320 --> 08:05:43,040
And now if we look at the cart here, we
9503
08:05:40,718 --> 08:05:46,478
have one more product. The quantity is
9504
08:05:43,040 --> 08:05:49,040
four. If we open the cart and we scroll
9505
08:05:46,478 --> 08:05:51,760
to the bottom, notice that it added the
9506
08:05:49,040 --> 08:05:54,878
t-shirt to the cart. So that's how we do
9507
08:05:51,760 --> 08:05:57,760
data mutation or update data in the back
9508
08:05:54,878 --> 08:06:00,000
end. We just send a request to the back
9509
08:05:57,760 --> 08:06:03,200
end and the backend takes care of
9510
08:06:00,000 --> 08:06:05,760
updating and saving the data. So, in the
9511
08:06:03,200 --> 08:06:09,200
real world, we use the back end to
9512
08:06:05,760 --> 08:06:11,600
manage and update the data.
9513
08:06:09,200 --> 08:06:14,160
Next, notice that when we added this
9514
08:06:11,600 --> 08:06:17,120
product to the cart, we had to refresh
9515
08:06:14,160 --> 08:06:19,440
the page to see the new cart. It would
9516
08:06:17,120 --> 08:06:22,080
be better if we could just reload the
9517
08:06:19,440 --> 08:06:24,160
cart and update the page without needing
9518
08:06:22,080 --> 08:06:26,478
to refresh.
9519
08:06:24,160 --> 08:06:29,600
We'll go to our code
9520
08:06:26,478 --> 08:06:32,600
and at the bottom, we're going to go to
9521
08:06:29,600 --> 08:06:32,600
app.jsx.
9522
08:06:32,638 --> 08:06:40,160
So here this code reloads the cart from
9523
08:06:36,878 --> 08:06:42,558
the back end and it updates the state.
9524
08:06:40,160 --> 08:06:45,680
After it updates the state, React will
9525
08:06:42,558 --> 08:06:48,000
update the page. So when we add a
9526
08:06:45,680 --> 08:06:50,798
product to the cart, we just need to
9527
08:06:48,000 --> 08:06:53,120
rerun this code and it will update the
9528
08:06:50,798 --> 08:06:55,360
page and then we don't have to refresh
9529
08:06:53,120 --> 08:06:57,200
the page every time.
9530
08:06:55,360 --> 08:07:00,320
We're going to select this entire
9531
08:06:57,200 --> 08:07:02,958
function and we'll cut.
9532
08:07:00,320 --> 08:07:05,520
And outside the use effect, we're going
9533
08:07:02,958 --> 08:07:07,360
to paste it.
9534
08:07:05,520 --> 08:07:09,920
And now that it's outside the use
9535
08:07:07,360 --> 08:07:11,920
effect, we can share this function with
9536
08:07:09,920 --> 08:07:14,558
our components.
9537
08:07:11,920 --> 08:07:17,520
Now, let's actually rename this function
9538
08:07:14,558 --> 08:07:22,638
to something more accurate like load
9539
08:07:17,520 --> 08:07:24,798
cart. And in here, load cart.
9540
08:07:22,638 --> 08:07:27,680
And now we can share this with our
9541
08:07:24,798 --> 08:07:31,040
components using a prop. So we're going
9542
08:07:27,680 --> 08:07:33,840
to scroll down here and in the homepage
9543
08:07:31,040 --> 08:07:37,040
we're going to give it the prop load
9544
08:07:33,840 --> 08:07:42,478
cart equals curly brackets and we'll
9545
08:07:37,040 --> 08:07:45,840
give it the function load cart and save.
9546
08:07:42,478 --> 08:07:49,920
And now if we go to the homepage
9547
08:07:45,840 --> 08:07:52,878
at the top, we'll get load cart out of
9548
08:07:49,920 --> 08:07:55,760
the props and then give it to the
9549
08:07:52,878 --> 08:07:58,320
products grid component. So remember
9550
08:07:55,760 --> 08:08:01,440
this component contains the add to cart
9551
08:07:58,320 --> 08:08:05,280
button. So in here we'll give it another
9552
08:08:01,440 --> 08:08:10,878
prop load cart equals curly brackets and
9553
08:08:05,280 --> 08:08:14,478
give it the load cart function and save.
9554
08:08:10,878 --> 08:08:19,360
And now inside the products grid
9555
08:08:14,478 --> 08:08:22,320
at the top, we're going to get load cart
9556
08:08:19,360 --> 08:08:24,558
out of the props. And then if we scroll
9557
08:08:22,320 --> 08:08:27,680
back down,
9558
08:08:24,558 --> 08:08:31,760
after we update the cart, we're going to
9559
08:08:27,680 --> 08:08:33,920
call the function load cart.
9560
08:08:31,760 --> 08:08:36,240
So this means we will update the cart in
9561
08:08:33,920 --> 08:08:39,280
the back end and then we'll get the new
9562
08:08:36,240 --> 08:08:41,040
cart and update the page without needing
9563
08:08:39,280 --> 08:08:43,760
to refresh.
9564
08:08:41,040 --> 08:08:46,080
One other thing we have to do here is
9565
08:08:43,760 --> 08:08:49,120
remember axios.post
9566
08:08:46,080 --> 08:08:51,600
contacts the back end. So this is
9567
08:08:49,120 --> 08:08:54,638
asynchronous code. It does not finish
9568
08:08:51,600 --> 08:08:57,600
right away. That means we need to wait
9569
08:08:54,638 --> 08:09:00,240
for this code to finish before going on
9570
08:08:57,600 --> 08:09:03,520
to the next line. To wait for this to
9571
08:09:00,240 --> 08:09:07,920
finish at the front of this code we can
9572
08:09:03,520 --> 08:09:10,558
type await and space. And remember in
9573
08:09:07,920 --> 08:09:15,360
order to use await we need to be inside
9574
08:09:10,558 --> 08:09:17,920
an async function. So we'll add async.
9575
08:09:15,360 --> 08:09:20,080
And also this code is not inside a use
9576
08:09:17,920 --> 08:09:22,798
effect. So we can just use async
9577
08:09:20,080 --> 08:09:25,600
directly here like this.
9578
08:09:22,798 --> 08:09:28,558
After that load cart is also
9579
08:09:25,600 --> 08:09:31,280
asynchronous. So it's a good idea to
9580
08:09:28,558 --> 08:09:35,280
await this code as well to be
9581
08:09:31,280 --> 08:09:38,558
consistent. So now let's save
9582
08:09:35,280 --> 08:09:42,160
and we'll check the website.
9583
08:09:38,558 --> 08:09:44,878
And first we'll go back to the homepage.
9584
08:09:42,160 --> 08:09:47,440
And this time let's click add to cart on
9585
08:09:44,878 --> 08:09:49,120
this toaster. So we'll click add to
9586
08:09:47,440 --> 08:09:51,600
cart.
9587
08:09:49,120 --> 08:09:54,080
And now notice that the quantity here
9588
08:09:51,600 --> 08:09:57,760
updated to five without needing to
9589
08:09:54,080 --> 08:09:59,360
refresh. If we go to the cart and scroll
9590
08:09:57,760 --> 08:10:02,000
down,
9591
08:09:59,360 --> 08:10:05,280
it also updated the cart on the page
9592
08:10:02,000 --> 08:10:08,240
without needing to refresh.
9593
08:10:05,280 --> 08:10:10,320
We'll go back to the homepage
9594
08:10:08,240 --> 08:10:13,280
and we're going to make these quantity
9595
08:10:10,320 --> 08:10:15,600
selectors work. So, this lets us add
9596
08:10:13,280 --> 08:10:19,600
more than one product to the cart at a
9597
08:10:15,600 --> 08:10:22,398
time. We're going to go to our code,
9598
08:10:19,600 --> 08:10:25,360
and first we need to get the quantity in
9599
08:10:22,398 --> 08:10:28,320
the quantity selector. To do that, we're
9600
08:10:25,360 --> 08:10:30,160
going to use a controlled input. We
9601
08:10:28,320 --> 08:10:33,360
learned controlled inputs when creating
9602
08:10:30,160 --> 08:10:36,638
the text box in the chatbot project. So,
9603
08:10:33,360 --> 08:10:38,958
as a review, we'll scroll up
9604
08:10:36,638 --> 08:10:42,160
and we're going to create a state to
9605
08:10:38,958 --> 08:10:47,200
save the quantity. At the top, we're
9606
08:10:42,160 --> 08:10:49,600
going to import curly brackets use state
9607
08:10:47,200 --> 08:10:51,920
from React.
9608
08:10:49,600 --> 08:10:54,878
And in this component,
9609
08:10:51,920 --> 08:11:00,240
we're going to create a state const
9610
08:10:54,878 --> 08:11:02,558
bracket equals use state and brackets.
9611
08:11:00,240 --> 08:11:04,240
And we're going to name the state the
9612
08:11:02,558 --> 08:11:07,520
quantity,
9613
08:11:04,240 --> 08:11:09,200
comma, and the updater function set
9614
08:11:07,520 --> 08:11:11,120
quantity.
9615
08:11:09,200 --> 08:11:13,760
And we'll set the starting value of the
9616
08:11:11,120 --> 08:11:16,478
quantity to one.
9617
08:11:13,760 --> 08:11:19,920
Then we're going to scroll down and
9618
08:11:16,478 --> 08:11:22,558
we're going to find this select element.
9619
08:11:19,920 --> 08:11:24,878
So this element represents the selector
9620
08:11:22,558 --> 08:11:28,558
on the page and we're going to give this
9621
08:11:24,878 --> 08:11:31,680
two props. The first prop is the value
9622
08:11:28,558 --> 08:11:33,920
equals curly brackets and the value is
9623
08:11:31,680 --> 08:11:37,840
the quantity.
9624
08:11:33,920 --> 08:11:39,920
The second prop is on change equals
9625
08:11:37,840 --> 08:11:42,398
curly brackets. And we're going to give
9626
08:11:39,920 --> 08:11:44,558
this a function. So brackets, arrow and
9627
08:11:42,398 --> 08:11:46,878
curly brackets.
9628
08:11:44,558 --> 08:11:50,080
And whenever we use an event handler
9629
08:11:46,878 --> 08:11:53,760
like on click or on change, this
9630
08:11:50,080 --> 08:11:56,160
function gets a parameter called event.
9631
08:11:53,760 --> 08:11:58,398
So event contains details about the
9632
08:11:56,160 --> 08:12:00,798
event. In this case, the event will
9633
08:11:58,398 --> 08:12:04,000
contain the quantity that we selected,
9634
08:12:00,798 --> 08:12:07,120
which is one of these values. To get the
9635
08:12:04,000 --> 08:12:10,120
quantity that we selected, we can use
9636
08:12:07,120 --> 08:12:10,120
event.target.
9637
08:12:10,160 --> 08:12:14,320
value
9638
08:12:11,760 --> 08:12:17,680
and let's save the quantity was selected
9639
08:12:14,320 --> 08:12:22,398
in a variable at the front. We'll create
9640
08:12:17,680 --> 08:12:24,878
const quantity selected equals this
9641
08:12:22,398 --> 08:12:27,760
value.
9642
08:12:24,878 --> 08:12:30,000
Now one thing to be careful of is this
9643
08:12:27,760 --> 08:12:32,718
value is going to be one of these values
9644
08:12:30,000 --> 08:12:35,600
here and these are going to be a string.
9645
08:12:32,718 --> 08:12:38,240
So we need to convert it into a number.
9646
08:12:35,600 --> 08:12:41,840
To do that around this value, we're
9647
08:12:38,240 --> 08:12:45,920
going to use the code capital n number
9648
08:12:41,840 --> 08:12:48,320
open bracket and close bracket.
9649
08:12:45,920 --> 08:12:51,840
So this will convert the string here
9650
08:12:48,320 --> 08:12:54,718
into a number. So now that we have the
9651
08:12:51,840 --> 08:12:59,200
quantity that we selected, let's save it
9652
08:12:54,718 --> 08:13:01,920
into the state using set quantity
9653
08:12:59,200 --> 08:13:04,718
brackets
9654
08:13:01,920 --> 08:13:07,120
quantity selected.
9655
08:13:04,718 --> 08:13:09,520
So this is called a controlled input.
9656
08:13:07,120 --> 08:13:12,398
When we have a value and an onchange
9657
08:13:09,520 --> 08:13:14,240
property when we change the selector,
9658
08:13:12,398 --> 08:13:16,638
it's going to set the quantity in the
9659
08:13:14,240 --> 08:13:19,120
state and then the new state is going to
9660
08:13:16,638 --> 08:13:21,440
be set as the value and then it updates
9661
08:13:19,120 --> 08:13:23,920
the page. So we're basically putting
9662
08:13:21,440 --> 08:13:26,000
react in the middle of this process so
9663
08:13:23,920 --> 08:13:28,878
that we have access to the quantity that
9664
08:13:26,000 --> 08:13:32,798
we selected. And that's a review of how
9665
08:13:28,878 --> 08:13:38,320
controlled inputs work. And let's also
9666
08:13:32,798 --> 08:13:42,160
console.log log the quantity selected to
9667
08:13:38,320 --> 08:13:44,878
see if it works. So, we'll save
9668
08:13:42,160 --> 08:13:46,958
and we'll go to the website
9669
08:13:44,878 --> 08:13:51,200
and we're going to open the console with
9670
08:13:46,958 --> 08:13:53,680
rightclick inspect and the console. And
9671
08:13:51,200 --> 08:13:56,080
now on the website, we're going to click
9672
08:13:53,680 --> 08:13:58,240
the quantity selector here and we'll
9673
08:13:56,080 --> 08:14:01,200
select three.
9674
08:13:58,240 --> 08:14:04,478
And now inside the console, it displays
9675
08:14:01,200 --> 08:14:07,520
the quantity that we selected.
9676
08:14:04,478 --> 08:14:10,080
However, there is a problem here. Notice
9677
08:14:07,520 --> 08:14:12,478
on this website when we change one
9678
08:14:10,080 --> 08:14:14,638
quantity selector, it changes all of
9679
08:14:12,478 --> 08:14:18,398
them. So if I change this one here to
9680
08:14:14,638 --> 08:14:20,638
five, it changes all the quantities. So
9681
08:14:18,398 --> 08:14:22,718
let's fix this. We'll go back to the
9682
08:14:20,638 --> 08:14:25,680
code.
9683
08:14:22,718 --> 08:14:29,120
And if we scroll to the top,
9684
08:14:25,680 --> 08:14:32,718
this is happening because the state here
9685
08:14:29,120 --> 08:14:35,760
is outside of this loop. So that means
9686
08:14:32,718 --> 08:14:38,160
the state is shared across all the
9687
08:14:35,760 --> 08:14:41,200
products. That's why when we change one
9688
08:14:38,160 --> 08:14:43,680
selector, it changes all of them. To
9689
08:14:41,200 --> 08:14:46,958
solve this problem, we need to move this
9690
08:14:43,680 --> 08:14:50,160
state inside this loop so that each
9691
08:14:46,958 --> 08:14:54,638
product has its own quantity. So, we're
9692
08:14:50,160 --> 08:14:57,600
going to select this code and cut.
9693
08:14:54,638 --> 08:15:00,600
And inside this loop, we're going to
9694
08:14:57,600 --> 08:15:00,600
paste.
9695
08:15:00,798 --> 08:15:06,398
However, this code actually breaks the
9696
08:15:03,200 --> 08:15:09,600
rules of React hooks. We can only put
9697
08:15:06,398 --> 08:15:12,240
hooks at the top level of a component.
9698
08:15:09,600 --> 08:15:15,920
We should not put hooks inside a loop
9699
08:15:12,240 --> 08:15:18,160
like this. To fix this problem, we can
9700
08:15:15,920 --> 08:15:19,600
separate each product into its own
9701
08:15:18,160 --> 08:15:22,398
component.
9702
08:15:19,600 --> 08:15:25,920
So, inside the home folder, we're going
9703
08:15:22,398 --> 08:15:28,920
to create a new file and we'll name it
9704
08:15:25,920 --> 08:15:28,920
product.jsx.
9705
08:15:29,600 --> 08:15:35,840
And we'll create a component for each
9706
08:15:31,680 --> 08:15:39,760
product, which is export function
9707
08:15:35,840 --> 08:15:43,280
product brackets and curly brackets. And
9708
08:15:39,760 --> 08:15:47,200
we're going to return the HTML for each
9709
08:15:43,280 --> 08:15:50,718
product. Inside the products grid, we're
9710
08:15:47,200 --> 08:15:53,040
going to collapse this code. And this is
9711
08:15:50,718 --> 08:15:56,798
the code for each product. So, we'll
9712
08:15:53,040 --> 08:15:59,760
select it and copy.
9713
08:15:56,798 --> 08:16:02,000
And inside the product component, we're
9714
08:15:59,760 --> 08:16:04,798
going to paste
9715
08:16:02,000 --> 08:16:07,840
and then control or command shiftp
9716
08:16:04,798 --> 08:16:10,320
format document.
9717
08:16:07,840 --> 08:16:13,840
We'll scroll up
9718
08:16:10,320 --> 08:16:16,798
and we'll go back to the products grid.
9719
08:16:13,840 --> 08:16:20,558
And we also need to copy over the state.
9720
08:16:16,798 --> 08:16:24,320
So we're going to select this copy and
9721
08:16:20,558 --> 08:16:26,398
inside product at the top we're going to
9722
08:16:24,320 --> 08:16:29,120
paste.
9723
08:16:26,398 --> 08:16:33,040
And we'll fix these errors. So first we
9724
08:16:29,120 --> 08:16:36,878
need to import use state at the top. So
9725
08:16:33,040 --> 08:16:38,478
import curly brackets use state from
9726
08:16:36,878 --> 08:16:40,558
react
9727
08:16:38,478 --> 08:16:44,958
and the product we'll get it from the
9728
08:16:40,558 --> 08:16:46,798
props using curly brackets product.
9729
08:16:44,958 --> 08:16:49,840
Scroll down
9730
08:16:46,798 --> 08:16:54,160
we'll need the format money function as
9731
08:16:49,840 --> 08:16:59,520
well as axios and load cart. So first
9732
08:16:54,160 --> 08:17:03,280
we'll import curly brackets format money
9733
08:16:59,520 --> 08:17:06,600
from a string and we'll find dot dot
9734
08:17:03,280 --> 08:17:06,600
slash do/youutils/money.
9735
08:17:07,280 --> 08:17:14,878
At the top we're going to import axios
9736
08:17:12,240 --> 08:17:16,638
from axios
9737
08:17:14,878 --> 08:17:20,958
and we'll get the load cart function
9738
08:17:16,638 --> 08:17:22,958
from the props. So, comma load cart and
9739
08:17:20,958 --> 08:17:25,040
save.
9740
08:17:22,958 --> 08:17:28,398
So, now each product has a separate
9741
08:17:25,040 --> 08:17:31,360
state. It has its own quantity. And this
9742
08:17:28,398 --> 08:17:33,680
is no longer breaking the rules of hooks
9743
08:17:31,360 --> 08:17:35,760
because this state is at the top level
9744
08:17:33,680 --> 08:17:38,718
of this component.
9745
08:17:35,760 --> 08:17:42,478
We'll go back to the products grid.
9746
08:17:38,718 --> 08:17:46,160
And here we can remove this HTML and
9747
08:17:42,478 --> 08:17:49,520
just use our component less than product
9748
08:17:46,160 --> 08:17:51,360
/greater than and we need to give it the
9749
08:17:49,520 --> 08:17:54,798
product
9750
08:17:51,360 --> 08:17:58,558
equals product and also the load cart
9751
08:17:54,798 --> 08:18:01,760
function equals load cart
9752
08:17:58,558 --> 08:18:04,320
and we can remove this state and
9753
08:18:01,760 --> 08:18:07,120
remember when we loop through an array
9754
08:18:04,320 --> 08:18:09,760
we need to give it a key prop. So at the
9755
08:18:07,120 --> 08:18:13,840
front here we'll set a key equals curly
9756
08:18:09,760 --> 08:18:15,360
brackets and we'll set it to the product
9757
08:18:13,840 --> 08:18:17,280
id.
9758
08:18:15,360 --> 08:18:20,398
And finally we need to import this
9759
08:18:17,280 --> 08:18:24,160
component. So we'll scroll up and at the
9760
08:18:20,398 --> 08:18:28,320
top we'll import curly brackets the
9761
08:18:24,160 --> 08:18:30,798
product component from a string and it's
9762
08:18:28,320 --> 08:18:34,240
right beside this file. So, we'll do dot
9763
08:18:30,798 --> 08:18:37,120
slash product and we can remove these
9764
08:18:34,240 --> 08:18:38,878
because they're no longer being used and
9765
08:18:37,120 --> 08:18:41,280
save.
9766
08:18:38,878 --> 08:18:44,240
And since we have the key in this loop
9767
08:18:41,280 --> 08:18:50,398
here, we can also go into the product
9768
08:18:44,240 --> 08:18:54,000
and remove the key in here and save.
9769
08:18:50,398 --> 08:18:57,360
Now, let's open the website.
9770
08:18:54,000 --> 08:18:59,360
And if we change one of these selectors,
9771
08:18:57,360 --> 08:19:02,320
notice that each product can now have
9772
08:18:59,360 --> 08:19:04,638
its own quantity.
9773
08:19:02,320 --> 08:19:07,440
Now that we have this quantity, when we
9774
08:19:04,638 --> 08:19:11,920
add a product to the cart, let's use
9775
08:19:07,440 --> 08:19:13,840
this quantity. We'll go to our code
9776
08:19:11,920 --> 08:19:18,000
and we're going to go inside each
9777
08:19:13,840 --> 08:19:21,360
product and we're going to scroll down
9778
08:19:18,000 --> 08:19:23,600
to the add to cart button here. And
9779
08:19:21,360 --> 08:19:26,240
instead of adding a quantity of one
9780
08:19:23,600 --> 08:19:28,958
every time, we're going to add the
9781
08:19:26,240 --> 08:19:32,798
quantity that is saved in the state
9782
08:19:28,958 --> 08:19:35,600
above. So now let's save
9783
08:19:32,798 --> 08:19:39,040
and we'll try out this code. We'll open
9784
08:19:35,600 --> 08:19:41,120
the website and let's go to this plate
9785
08:19:39,040 --> 08:19:44,718
product and we're going to select a
9786
08:19:41,120 --> 08:19:47,520
quantity of three this time and we'll
9787
08:19:44,718 --> 08:19:50,000
click add to cart.
9788
08:19:47,520 --> 08:19:52,558
And notice that the cart updated and it
9789
08:19:50,000 --> 08:19:55,440
now has a quantity of eight, which is
9790
08:19:52,558 --> 08:19:56,958
three more than before. If we open the
9791
08:19:55,440 --> 08:20:00,398
cart
9792
08:19:56,958 --> 08:20:03,280
and we scroll to the bottom, we now have
9793
08:20:00,398 --> 08:20:06,478
three of these plates in the cart. So
9794
08:20:03,280 --> 08:20:09,440
now the quantity selector is working.
9795
08:20:06,478 --> 08:20:11,360
Let's go back to the homepage.
9796
08:20:09,440 --> 08:20:14,478
And this time, let's try adding a
9797
08:20:11,360 --> 08:20:16,958
product that already exists in the cart.
9798
08:20:14,478 --> 08:20:20,160
So, for the plate product, we're going
9799
08:20:16,958 --> 08:20:23,760
to select the quantity of three again,
9800
08:20:20,160 --> 08:20:25,840
and we're going to click add to cart.
9801
08:20:23,760 --> 08:20:29,200
Notice the cart quantity increased by
9802
08:20:25,840 --> 08:20:32,558
three. And if we open the cart and
9803
08:20:29,200 --> 08:20:34,798
scroll down, instead of adding a new
9804
08:20:32,558 --> 08:20:38,080
plate product, it just increased the
9805
08:20:34,798 --> 08:20:40,240
quantity by three for a total of six. So
9806
08:20:38,080 --> 08:20:42,958
if the product already exists in the
9807
08:20:40,240 --> 08:20:46,398
cart, the backend handles this situation
9808
08:20:42,958 --> 08:20:48,320
for us and just increases the quantity.
9809
08:20:46,398 --> 08:20:51,040
Remember the purpose of the backend is
9810
08:20:48,320 --> 08:20:53,760
to manage the data. By letting the
9811
08:20:51,040 --> 08:20:57,200
backend manage and update the data, we
9812
08:20:53,760 --> 08:20:59,920
can simplify our React code.
9813
08:20:57,200 --> 08:21:01,760
Let's go back to our code
9814
08:20:59,920 --> 08:21:03,680
and we're going to do some cleanup in
9815
08:21:01,760 --> 08:21:05,920
this component.
9816
08:21:03,680 --> 08:21:08,320
First, instead of doing quantity colon
9817
08:21:05,920 --> 08:21:11,120
quantity, we can use the shorthand
9818
08:21:08,320 --> 08:21:13,040
property syntax and just use the
9819
08:21:11,120 --> 08:21:14,558
quantity. It does the same thing as
9820
08:21:13,040 --> 08:21:16,878
before.
9821
08:21:14,558 --> 08:21:20,240
Next, instead of putting our event
9822
08:21:16,878 --> 08:21:22,240
handlers directly in the HTML like this,
9823
08:21:20,240 --> 08:21:26,558
we usually save this in a separate
9824
08:21:22,240 --> 08:21:28,958
function. So, let's select all this code
9825
08:21:26,558 --> 08:21:32,240
and copy.
9826
08:21:28,958 --> 08:21:34,718
And we're going to scroll up.
9827
08:21:32,240 --> 08:21:40,638
And at the top we're going to create a
9828
08:21:34,718 --> 08:21:43,440
function const add to cart equals and
9829
08:21:40,638 --> 08:21:45,200
we'll paste that code.
9830
08:21:43,440 --> 08:21:47,440
So we usually save the function
9831
08:21:45,200 --> 08:21:49,920
separately like this. And then we can
9832
08:21:47,440 --> 08:21:52,478
scroll down.
9833
08:21:49,920 --> 08:21:56,958
We can remove this code and just use the
9834
08:21:52,478 --> 08:21:59,920
function name add to cart. And this
9835
08:21:56,958 --> 08:22:02,638
makes the code easier to read. Next,
9836
08:21:59,920 --> 08:22:07,280
we'll scroll up to the quantity
9837
08:22:02,638 --> 08:22:09,200
selector. We can remove the console.log
9838
08:22:07,280 --> 08:22:12,718
and we'll move this into a separate
9839
08:22:09,200 --> 08:22:14,958
function as well. We'll select this code
9840
08:22:12,718 --> 08:22:17,360
and copy
9841
08:22:14,958 --> 08:22:20,160
and we'll scroll up
9842
08:22:17,360 --> 08:22:24,478
and here we're going to create another
9843
08:22:20,160 --> 08:22:28,558
function const select quantity and make
9844
08:22:24,478 --> 08:22:31,600
it equal and we'll paste that code and
9845
08:22:28,558 --> 08:22:33,200
we'll use this function below. So down
9846
08:22:31,600 --> 08:22:34,638
here,
9847
08:22:33,200 --> 08:22:37,440
instead of putting this function
9848
08:22:34,638 --> 08:22:43,600
directly in the HTML, we use the
9849
08:22:37,440 --> 08:22:46,160
function name select quantity and save.
9850
08:22:43,600 --> 08:22:48,638
And that's it. We created the add to
9851
08:22:46,160 --> 08:22:50,718
cart feature. In the final project,
9852
08:22:48,638 --> 08:22:52,958
there's a small message that pops up
9853
08:22:50,718 --> 08:22:55,840
when we click add to cart, but I'll
9854
08:22:52,958 --> 08:22:58,558
leave this as an exercise. Now, let's
9855
08:22:55,840 --> 08:23:02,958
save this into Git. We'll open the git
9856
08:22:58,558 --> 08:23:09,638
section and let's use the message create
9857
08:23:02,958 --> 08:23:09,638
add to cart feature and commit.
9858
08:23:11,200 --> 08:23:15,440
Before we continue, I want to go back to
9859
08:23:13,360 --> 08:23:18,958
the code that we wrote. So, we're going
9860
08:23:15,440 --> 08:23:21,360
to scroll up in this component and I
9861
08:23:18,958 --> 08:23:24,878
want to explain axios.post
9862
08:23:21,360 --> 08:23:27,840
a little more. As a review, when we send
9863
08:23:24,878 --> 08:23:31,840
a request to the backend, we can set a
9864
08:23:27,840 --> 08:23:34,558
URL path. Based on the URL path, the
9865
08:23:31,840 --> 08:23:38,080
backend can do different things. For
9866
08:23:34,558 --> 08:23:40,878
example, if we use / API/roucts,
9867
08:23:38,080 --> 08:23:44,398
the backend gives us the products. If we
9868
08:23:40,878 --> 08:23:46,798
use / API/cart items, the backend gives
9869
08:23:44,398 --> 08:23:49,520
us the cart.
9870
08:23:46,798 --> 08:23:53,280
In addition to the URL path, every
9871
08:23:49,520 --> 08:23:57,558
request also has a type. In the previous
9872
08:23:53,280 --> 08:23:57,558
lesson when we did axios.get/api/cart
9873
08:23:57,920 --> 08:24:05,600
items, this actually sends the type get
9874
08:24:01,440 --> 08:24:08,718
and the URL path/ API/cart items to the
9875
08:24:05,600 --> 08:24:11,600
back end. So it sends two pieces of
9876
08:24:08,718 --> 08:24:14,958
information to the back end. The type
9877
08:24:11,600 --> 08:24:17,120
and the URL path.
9878
08:24:14,958 --> 08:24:19,520
When we did axios.post/appi/cart
9879
08:24:17,120 --> 08:24:23,360
post/api/cart
9880
08:24:19,520 --> 08:24:26,000
items. This sends the type post and the
9881
08:24:23,360 --> 08:24:28,958
URL path /api/cart
9882
08:24:26,000 --> 08:24:31,440
items to the back end.
9883
08:24:28,958 --> 08:24:35,040
Now when the backend receives our
9884
08:24:31,440 --> 08:24:39,760
request, it looks at both the type and
9885
08:24:35,040 --> 08:24:43,200
the URL path to decide what to do.
9886
08:24:39,760 --> 08:24:45,120
For example, if the backend receives get
9887
08:24:43,200 --> 08:24:48,240
and/api/cart
9888
08:24:45,120 --> 08:24:52,000
items, it will get the cart items and
9889
08:24:48,240 --> 08:24:54,000
return it. If the backend receives post
9890
08:24:52,000 --> 08:24:58,320
and/appi/cart
9891
08:24:54,000 --> 08:25:01,760
items, it will create a new cart item.
9892
08:24:58,320 --> 08:25:04,478
So both the type and the URL path
9893
08:25:01,760 --> 08:25:06,398
together determine what the backend
9894
08:25:04,478 --> 08:25:08,160
does.
9895
08:25:06,398 --> 08:25:12,240
In the description, I have a
9896
08:25:08,160 --> 08:25:14,558
documentation page for my backend,
9897
08:25:12,240 --> 08:25:17,600
and it lists all the different requests
9898
08:25:14,558 --> 08:25:22,240
that my backend supports. Notice that
9899
08:25:17,600 --> 08:25:24,878
each request has a type and a URL path.
9900
08:25:22,240 --> 08:25:27,840
By the way, the type is also called a
9901
08:25:24,878 --> 08:25:30,000
HTTP method.
9902
08:25:27,840 --> 08:25:32,638
When working with a backend, there are
9903
08:25:30,000 --> 08:25:38,000
four common types of requests that we
9904
08:25:32,638 --> 08:25:41,360
use. Get, post, put, and delete. Get
9905
08:25:38,000 --> 08:25:44,718
means we want to get some data. Post
9906
08:25:41,360 --> 08:25:47,680
means we want to create some data. Put
9907
08:25:44,718 --> 08:25:50,000
means we want to update some data and
9908
08:25:47,680 --> 08:25:51,760
delete means we want to delete some
9909
08:25:50,000 --> 08:25:55,360
data.
9910
08:25:51,760 --> 08:25:58,160
So, Axios has a method for each type.
9911
08:25:55,360 --> 08:26:01,440
Axios.get Get sets the type of request
9912
08:25:58,160 --> 08:26:05,280
to get. Axios.post
9913
08:26:01,440 --> 08:26:08,718
sets the type to post. Axios.put sets
9914
08:26:05,280 --> 08:26:11,520
the type to put. And axios.delete
9915
08:26:08,718 --> 08:26:13,760
sets the type to delete. And then
9916
08:26:11,520 --> 08:26:16,080
between the brackets, we put the URL
9917
08:26:13,760 --> 08:26:19,200
path.
9918
08:26:16,080 --> 08:26:22,318
So in our code, this sends a request to
9919
08:26:19,200 --> 08:26:27,120
the back end with the type post and the
9920
08:26:22,318 --> 08:26:29,440
URL path/ API/cart items. So the most
9921
08:26:27,120 --> 08:26:33,760
important thing to remember here is that
9922
08:26:29,440 --> 08:26:35,920
each request has a type and a URL path
9923
08:26:33,760 --> 08:26:38,318
and both of these determine what the
9924
08:26:35,920 --> 08:26:40,398
backend will do.
9925
08:26:38,318 --> 08:26:42,638
In the rest of this lesson, we'll get
9926
08:26:40,398 --> 08:26:45,520
more practice sending different types of
9927
08:26:42,638 --> 08:26:48,958
requests to the backend.
9928
08:26:45,520 --> 08:26:51,760
Next, let's go back to the website.
9929
08:26:48,958 --> 08:26:54,958
And inside the checkout page for each
9930
08:26:51,760 --> 08:26:57,040
product, we have these delivery options.
9931
08:26:54,958 --> 08:27:00,000
So, we're going to add the ability to
9932
08:26:57,040 --> 08:27:02,160
update the delivery option. Because
9933
08:27:00,000 --> 08:27:04,558
we're updating something in the cart,
9934
08:27:02,160 --> 08:27:06,318
we're going to practice using a put
9935
08:27:04,558 --> 08:27:08,398
request.
9936
08:27:06,318 --> 08:27:10,878
First, let's find the code for these
9937
08:27:08,398 --> 08:27:12,798
delivery options. We'll go back to our
9938
08:27:10,878 --> 08:27:15,600
code
9939
08:27:12,798 --> 08:27:18,240
and open our files.
9940
08:27:15,600 --> 08:27:20,718
and we're going to find the pages and
9941
08:27:18,240 --> 08:27:24,200
checkout folder and we're going to open
9942
08:27:20,718 --> 08:27:24,200
delivery options.jsx
9943
08:27:25,200 --> 08:27:30,478
and we'll scroll down
9944
08:27:27,760 --> 08:27:33,360
and here each of these represents one
9945
08:27:30,478 --> 08:27:35,440
delivery option on the page. So we're
9946
08:27:33,360 --> 08:27:38,558
going to put an on click property on
9947
08:27:35,440 --> 08:27:41,360
this element. Now, usually we put the on
9948
08:27:38,558 --> 08:27:43,600
click on the input, but in this case,
9949
08:27:41,360 --> 08:27:46,798
this will make only the radial selector
9950
08:27:43,600 --> 08:27:49,040
clickable. Instead, we want the entire
9951
08:27:46,798 --> 08:27:51,280
delivery option, including the text, to
9952
08:27:49,040 --> 08:27:53,520
be clickable. So, that's why we're going
9953
08:27:51,280 --> 08:27:55,200
to put the on click on the outside
9954
08:27:53,520 --> 08:27:57,920
component.
9955
08:27:55,200 --> 08:28:01,520
So, we're going to add the on click
9956
08:27:57,920 --> 08:28:04,558
attribute equals curly brackets. And
9957
08:28:01,520 --> 08:28:08,000
let's create a function for this. Above
9958
08:28:04,558 --> 08:28:12,878
this, we'll create a function with const
9959
08:28:08,000 --> 08:28:15,920
and let's name it update delivery option
9960
08:28:12,878 --> 08:28:18,080
equals. And let's make it an async
9961
08:28:15,920 --> 08:28:20,478
function brackets arrow and curly
9962
08:28:18,080 --> 08:28:22,878
brackets because we know we're going to
9963
08:28:20,478 --> 08:28:26,000
make a request to the back end to update
9964
08:28:22,878 --> 08:28:29,200
the delivery option.
9965
08:28:26,000 --> 08:28:31,680
Also, we create the function here inside
9966
08:28:29,200 --> 08:28:34,160
of this loop because we need access to
9967
08:28:31,680 --> 08:28:37,280
each delivery option.
9968
08:28:34,160 --> 08:28:39,280
So next to update the delivery option
9969
08:28:37,280 --> 08:28:42,000
for a product, we're going to make a
9970
08:28:39,280 --> 08:28:45,360
request to the back end. We're going to
9971
08:28:42,000 --> 08:28:51,280
scroll to the top and at the top we'll
9972
08:28:45,360 --> 08:28:54,240
import axials from Axios
9973
08:28:51,280 --> 08:28:56,478
and we'll scroll back down
9974
08:28:54,240 --> 08:28:59,360
and inside here according to my
9975
08:28:56,478 --> 08:29:04,318
documentation my backend supports the
9976
08:28:59,360 --> 08:29:07,120
request put/ API/cart items. This lets
9977
08:29:04,318 --> 08:29:09,120
us update the cart. For example, we can
9978
08:29:07,120 --> 08:29:11,200
update the delivery option for a
9979
08:29:09,120 --> 08:29:15,360
product.
9980
08:29:11,200 --> 08:29:17,120
So, we're going to do axios.put
9981
08:29:15,360 --> 08:29:19,600
brackets.
9982
08:29:17,120 --> 08:29:22,798
This sends a request with the type put
9983
08:29:19,600 --> 08:29:26,958
to the back end. And we'll set the URL
9984
08:29:22,798 --> 08:29:30,958
path to / API/cart-ite
9985
08:29:26,958 --> 08:29:33,200
items. And we'll add another slash. And
9986
08:29:30,958 --> 08:29:35,600
we're going to insert the product ID
9987
08:29:33,200 --> 08:29:39,200
that we want to update here at the end
9988
08:29:35,600 --> 08:29:42,000
of the URL path. So in my documentation,
9989
08:29:39,200 --> 08:29:44,958
colon product ID is known as a URL
9990
08:29:42,000 --> 08:29:47,760
parameter. This means my backend wants
9991
08:29:44,958 --> 08:29:50,478
the product ID we want to update inside
9992
08:29:47,760 --> 08:29:52,080
the URL. This is just a naming
9993
08:29:50,478 --> 08:29:54,798
convention that is popular with
9994
08:29:52,080 --> 08:29:57,040
backends. When we update something, we
9995
08:29:54,798 --> 08:30:01,200
usually put the ID of what we want to
9996
08:29:57,040 --> 08:30:02,718
update inside the URL. So here first
9997
08:30:01,200 --> 08:30:06,000
we're going to change this to a template
9998
08:30:02,718 --> 08:30:08,558
string using back ticks
9999
08:30:06,000 --> 08:30:10,958
and then at the end we'll insert dollar
10000
08:30:08,558 --> 08:30:14,000
curly brackets and we're going to insert
10001
08:30:10,958 --> 08:30:16,958
the product ID that we want to update.
10002
08:30:14,000 --> 08:30:20,558
So the product ID is saved inside the
10003
08:30:16,958 --> 08:30:24,160
cart item. So inside here we're going to
10004
08:30:20,558 --> 08:30:26,958
insert the cart item
10005
08:30:24,160 --> 08:30:29,280
product ID.
10006
08:30:26,958 --> 08:30:32,878
Next, we need to tell the backend which
10007
08:30:29,280 --> 08:30:34,718
delivery option we want to update to. To
10008
08:30:32,878 --> 08:30:37,760
do that, we're going to give the backend
10009
08:30:34,718 --> 08:30:41,280
some data. At the end of this, we'll add
10010
08:30:37,760 --> 08:30:44,318
a comma and an object.
10011
08:30:41,280 --> 08:30:46,958
And in my documentation, this section
10012
08:30:44,318 --> 08:30:50,318
tells us what data we can send to the
10013
08:30:46,958 --> 08:30:54,718
back end. Here, we can send the delivery
10014
08:30:50,318 --> 08:30:57,760
option ID that we want to update to.
10015
08:30:54,718 --> 08:31:01,360
So, we're going to give the back end the
10016
08:30:57,760 --> 08:31:05,760
delivery option ID that we want to
10017
08:31:01,360 --> 08:31:09,920
update to. So, if we scroll up here, we
10018
08:31:05,760 --> 08:31:12,160
want to update to this delivery option.
10019
08:31:09,920 --> 08:31:17,760
So, we'll tell the backend to update to
10020
08:31:12,160 --> 08:31:20,638
the delivery option ID.
10021
08:31:17,760 --> 08:31:22,958
And after we update the delivery option,
10022
08:31:20,638 --> 08:31:24,718
we're going to reload the cart and
10023
08:31:22,958 --> 08:31:27,040
update the page.
10024
08:31:24,718 --> 08:31:30,240
So in front of this, let's wait for this
10025
08:31:27,040 --> 08:31:32,638
to finish using await.
10026
08:31:30,240 --> 08:31:36,478
And remember, we created a function for
10027
08:31:32,638 --> 08:31:40,160
reloading the cart called load cart. So
10028
08:31:36,478 --> 08:31:43,040
inside app.jsx,
10029
08:31:40,160 --> 08:31:46,160
we're going to share load cart with the
10030
08:31:43,040 --> 08:31:51,360
checkout page. So down here we'll add a
10031
08:31:46,160 --> 08:31:55,760
prop load cart equals curly brackets
10032
08:31:51,360 --> 08:31:59,040
load cart and save.
10033
08:31:55,760 --> 08:32:04,080
And then inside the checkout page
10034
08:31:59,040 --> 08:32:06,080
at the top we're going to get load cart.
10035
08:32:04,080 --> 08:32:08,398
And now we need to share this with the
10036
08:32:06,080 --> 08:32:10,478
delivery options. Now the delivery
10037
08:32:08,398 --> 08:32:12,240
options are actually inside the order
10038
08:32:10,478 --> 08:32:14,240
summary. So we're going to share it with
10039
08:32:12,240 --> 08:32:17,680
the order summary first.
10040
08:32:14,240 --> 08:32:20,638
We'll scroll down to the order summary
10041
08:32:17,680 --> 08:32:25,520
and here we're going to give it the load
10042
08:32:20,638 --> 08:32:27,840
cart equals curly brackets load cart and
10043
08:32:25,520 --> 08:32:30,398
save
10044
08:32:27,840 --> 08:32:33,200
inside the order summary
10045
08:32:30,398 --> 08:32:36,798
at the top we're going to get from the
10046
08:32:33,200 --> 08:32:38,958
props load cart and then we're going to
10047
08:32:36,798 --> 08:32:41,360
share it with the delivery options. So,
10048
08:32:38,958 --> 08:32:45,040
we'll scroll down to these delivery
10049
08:32:41,360 --> 08:32:49,600
options and we'll give it the prop load
10050
08:32:45,040 --> 08:32:51,440
cart equals curly brackets load cart and
10051
08:32:49,600 --> 08:32:54,398
save.
10052
08:32:51,440 --> 08:32:57,600
And finally, in the delivery options, we
10053
08:32:54,398 --> 08:33:02,558
can get load cart from the props. So,
10054
08:32:57,600 --> 08:33:05,280
comma load cart and scroll down.
10055
08:33:02,558 --> 08:33:09,280
After we update the delivery option,
10056
08:33:05,280 --> 08:33:12,958
we're going to await load cart and
10057
08:33:09,280 --> 08:33:15,200
brackets so that we reload the cart and
10058
08:33:12,958 --> 08:33:18,638
update the page and we don't have to
10059
08:33:15,200 --> 08:33:20,160
refresh to see the updates. Finally,
10060
08:33:18,638 --> 08:33:23,600
we're going to take this function and
10061
08:33:20,160 --> 08:33:26,318
put it in this on click. So, update
10062
08:33:23,600 --> 08:33:29,200
delivery option
10063
08:33:26,318 --> 08:33:31,920
and save.
10064
08:33:29,200 --> 08:33:33,840
Now let's go to the website
10065
08:33:31,920 --> 08:33:35,440
and we'll try changing a delivery
10066
08:33:33,840 --> 08:33:37,920
option.
10067
08:33:35,440 --> 08:33:40,878
And now you can see that it updates when
10068
08:33:37,920 --> 08:33:43,520
we change the delivery option.
10069
08:33:40,878 --> 08:33:45,840
Now one last thing here is if we open
10070
08:33:43,520 --> 08:33:47,600
the console,
10071
08:33:45,840 --> 08:33:49,760
we're going to get this warning message
10072
08:33:47,600 --> 08:33:52,958
saying we provided a check property
10073
08:33:49,760 --> 08:33:55,280
without an onchange handler. So let's
10074
08:33:52,958 --> 08:33:57,920
fix this warning. We're going to go back
10075
08:33:55,280 --> 08:34:00,398
to the code
10076
08:33:57,920 --> 08:34:03,600
and the warning means that for an input
10077
08:34:00,398 --> 08:34:07,040
element, if we have a checked prop, we
10078
08:34:03,600 --> 08:34:09,520
should also have an onchange prop. On
10079
08:34:07,040 --> 08:34:11,200
change lets us change the value of this
10080
08:34:09,520 --> 08:34:14,000
input.
10081
08:34:11,200 --> 08:34:17,200
However, in our situation, we already
10082
08:34:14,000 --> 08:34:19,520
use this on click up here to change the
10083
08:34:17,200 --> 08:34:23,200
value of the input. So, we don't really
10084
08:34:19,520 --> 08:34:25,520
need an onchange property. So in here to
10085
08:34:23,200 --> 08:34:28,318
get rid of that warning, we can just add
10086
08:34:25,520 --> 08:34:31,920
an on change property equals curly
10087
08:34:28,318 --> 08:34:33,760
brackets and give it an empty function.
10088
08:34:31,920 --> 08:34:37,120
And this will just make the warning go
10089
08:34:33,760 --> 08:34:39,840
away. We'll save
10090
08:34:37,120 --> 08:34:42,240
and go back to our website.
10091
08:34:39,840 --> 08:34:45,360
And let's refresh.
10092
08:34:42,240 --> 08:34:48,318
And now if we change the delivery option
10093
08:34:45,360 --> 08:34:51,280
and check the console, we no longer get
10094
08:34:48,318 --> 08:34:53,680
that warning.
10095
08:34:51,280 --> 08:34:56,718
Next, we have another problem on this
10096
08:34:53,680 --> 08:34:59,360
page, which is when we update the
10097
08:34:56,718 --> 08:35:02,000
delivery option. The payment summary
10098
08:34:59,360 --> 08:35:04,080
here is not updating to show the new
10099
08:35:02,000 --> 08:35:06,798
delivery price.
10100
08:35:04,080 --> 08:35:09,680
To fix this, after we update a delivery
10101
08:35:06,798 --> 08:35:12,398
option, we also want to reload the
10102
08:35:09,680 --> 08:35:15,760
payment summary. So, let's find the code
10103
08:35:12,398 --> 08:35:18,080
for reloading the payment summary. We'll
10104
08:35:15,760 --> 08:35:20,160
go to our code
10105
08:35:18,080 --> 08:35:22,240
and we're going to open the checkout
10106
08:35:20,160 --> 08:35:25,200
page
10107
08:35:22,240 --> 08:35:29,040
and we're going to scroll up
10108
08:35:25,200 --> 08:35:32,000
and inside this use effect we have this
10109
08:35:29,040 --> 08:35:33,680
code here that reloads the payment
10110
08:35:32,000 --> 08:35:36,318
summary.
10111
08:35:33,680 --> 08:35:39,360
So we want to rerun this code after
10112
08:35:36,318 --> 08:35:41,840
updating the delivery option. Now we
10113
08:35:39,360 --> 08:35:44,240
could separate this into a function and
10114
08:35:41,840 --> 08:35:47,440
then share it like we did with load
10115
08:35:44,240 --> 08:35:51,440
cart. However, we have an easier option
10116
08:35:47,440 --> 08:35:55,200
here. Remember that in use effect at the
10117
08:35:51,440 --> 08:35:57,840
end we have this dependency array. So
10118
08:35:55,200 --> 08:36:00,878
this array determines when use effect
10119
08:35:57,840 --> 08:36:04,638
runs. Right now it's empty which means
10120
08:36:00,878 --> 08:36:08,478
use effect will only run once. However,
10121
08:36:04,638 --> 08:36:11,680
we can put values inside this array. And
10122
08:36:08,478 --> 08:36:15,280
whenever a value changes, it will rerun
10123
08:36:11,680 --> 08:36:18,240
the use effect. In our situation, we can
10124
08:36:15,280 --> 08:36:20,718
put the value cart inside this
10125
08:36:18,240 --> 08:36:24,080
dependency array.
10126
08:36:20,718 --> 08:36:27,040
And now whenever the cart changes, it
10127
08:36:24,080 --> 08:36:30,000
will rerun the use effect and update the
10128
08:36:27,040 --> 08:36:32,160
payment summary. And that makes sense.
10129
08:36:30,000 --> 08:36:34,558
Every time the cart changes, we do want
10130
08:36:32,160 --> 08:36:37,600
to update the payment summary in case
10131
08:36:34,558 --> 08:36:40,240
the cost of the order changes.
10132
08:36:37,600 --> 08:36:42,718
So in React there are often multiple
10133
08:36:40,240 --> 08:36:45,840
solutions to do something and we should
10134
08:36:42,718 --> 08:36:47,760
try to pick the cleanest solution.
10135
08:36:45,840 --> 08:36:50,398
Also note that this will cause the
10136
08:36:47,760 --> 08:36:53,040
delivery options to be reloaded which is
10137
08:36:50,398 --> 08:36:55,280
not really necessary. So we could
10138
08:36:53,040 --> 08:36:57,840
separate reloading the payment summary
10139
08:36:55,280 --> 08:37:00,958
into another use effect but I'll leave
10140
08:36:57,840 --> 08:37:03,680
this as an exercise.
10141
08:37:00,958 --> 08:37:06,398
So now let's save
10142
08:37:03,680 --> 08:37:08,318
and we'll go to our website.
10143
08:37:06,398 --> 08:37:10,958
And now when we update the delivery
10144
08:37:08,318 --> 08:37:13,760
option, notice that the payment summary
10145
08:37:10,958 --> 08:37:17,440
also updates to show the new delivery
10146
08:37:13,760 --> 08:37:20,240
price. And now this feature is working.
10147
08:37:17,440 --> 08:37:23,440
So now let's save this into git. We'll
10148
08:37:20,240 --> 08:37:25,200
go back to our code and open the git
10149
08:37:23,440 --> 08:37:28,718
section.
10150
08:37:25,200 --> 08:37:33,760
And here we'll use a message create
10151
08:37:28,718 --> 08:37:36,760
update delivery option feature and
10152
08:37:33,760 --> 08:37:36,760
commit.
10153
08:37:39,280 --> 08:37:44,958
Next, we're going to go to our website
10154
08:37:42,638 --> 08:37:47,200
and we're going to work on deleting an
10155
08:37:44,958 --> 08:37:49,760
item from the cart. So, we're going to
10156
08:37:47,200 --> 08:37:52,478
practice making a delete request to the
10157
08:37:49,760 --> 08:37:55,440
back end. First, we're going to find the
10158
08:37:52,478 --> 08:37:57,200
code for this delete link. We'll go to
10159
08:37:55,440 --> 08:38:00,638
our code
10160
08:37:57,200 --> 08:38:03,680
and in our files we're going to find the
10161
08:38:00,638 --> 08:38:05,440
order summary component
10162
08:38:03,680 --> 08:38:09,120
and we're going to scroll down in this
10163
08:38:05,440 --> 08:38:11,280
component and find the delete link. If
10164
08:38:09,120 --> 08:38:15,040
you did the exercises, this might be
10165
08:38:11,280 --> 08:38:17,200
inside a cart item details component.
10166
08:38:15,040 --> 08:38:20,878
Now, for this delete link, we're going
10167
08:38:17,200 --> 08:38:23,520
to add an on click attribute equals
10168
08:38:20,878 --> 08:38:27,200
curly brackets. And we'll give this a
10169
08:38:23,520 --> 08:38:30,318
function to run. Let's call it delete
10170
08:38:27,200 --> 08:38:32,558
cart item. And let's create this
10171
08:38:30,318 --> 08:38:34,160
function above. So, I'm going to scroll
10172
08:38:32,558 --> 08:38:36,240
up
10173
08:38:34,160 --> 08:38:38,718
and I'm going to create the function
10174
08:38:36,240 --> 08:38:42,798
inside this loop because I need access
10175
08:38:38,718 --> 08:38:46,478
to this cart item to delete. So in here
10176
08:38:42,798 --> 08:38:51,760
we'll create the function const delete
10177
08:38:46,478 --> 08:38:54,558
cart item equals a function.
10178
08:38:51,760 --> 08:38:56,398
And let's make this an async function
10179
08:38:54,558 --> 08:38:59,520
because we know we're going to make a
10180
08:38:56,398 --> 08:39:02,798
request to the back end. And first let's
10181
08:38:59,520 --> 08:39:07,760
import axios. So we'll scroll up and at
10182
08:39:02,798 --> 08:39:09,840
the top we'll import axios from a string
10183
08:39:07,760 --> 08:39:12,478
axios.
10184
08:39:09,840 --> 08:39:15,360
And then inside this function,
10185
08:39:12,478 --> 08:39:17,520
my backend can receive a request called
10186
08:39:15,360 --> 08:39:20,000
delete/appi/cart
10187
08:39:17,520 --> 08:39:23,600
items. And this will delete a product
10188
08:39:20,000 --> 08:39:25,200
from the cart. So we'll do axials
10189
08:39:23,600 --> 08:39:27,520
dodelete
10190
08:39:25,200 --> 08:39:29,520
and brackets.
10191
08:39:27,520 --> 08:39:32,558
This sends a request with the type
10192
08:39:29,520 --> 08:39:34,718
delete to the back end. For the URL
10193
08:39:32,558 --> 08:39:39,360
path, we're going to use a template
10194
08:39:34,718 --> 08:39:42,718
string and we'll do / API/cart-
10195
08:39:39,360 --> 08:39:46,000
items slash and we're going to insert
10196
08:39:42,718 --> 08:39:47,840
the product ID in the URL. Again, this
10197
08:39:46,000 --> 08:39:51,440
is just a naming convention that my
10198
08:39:47,840 --> 08:39:54,638
backend uses. So, we're going to insert
10199
08:39:51,440 --> 08:39:56,398
the ID of what we want to delete. In
10200
08:39:54,638 --> 08:39:59,760
this case, we need to give the backend
10201
08:39:56,398 --> 08:40:02,240
the product ID we want to delete. So the
10202
08:39:59,760 --> 08:40:04,638
product ID is saved inside the cart
10203
08:40:02,240 --> 08:40:10,798
item. So we're going to give the backend
10204
08:40:04,638 --> 08:40:13,600
the cart item dot product ID. And that's
10205
08:40:10,798 --> 08:40:17,040
it. When my backend receives a request
10206
08:40:13,600 --> 08:40:18,958
with the type delete and this URL path,
10207
08:40:17,040 --> 08:40:22,478
it's going to delete this product from
10208
08:40:18,958 --> 08:40:25,840
the cart. So after deleting the product,
10209
08:40:22,478 --> 08:40:28,878
let's reload the cart. First, at the
10210
08:40:25,840 --> 08:40:31,600
front of this, we'll type await to wait
10211
08:40:28,878 --> 08:40:35,920
for this code to finish. And then we're
10212
08:40:31,600 --> 08:40:38,000
going to await load the cart and
10213
08:40:35,920 --> 08:40:40,798
brackets.
10214
08:40:38,000 --> 08:40:43,440
So, I have load cart already in the
10215
08:40:40,798 --> 08:40:45,520
order summary. If you did the exercises
10216
08:40:43,440 --> 08:40:47,680
from the previous lesson, you may need
10217
08:40:45,520 --> 08:40:50,878
to pass this function into your
10218
08:40:47,680 --> 08:40:52,958
component using a prop.
10219
08:40:50,878 --> 08:40:55,280
So, now let's scroll down. And that's
10220
08:40:52,958 --> 08:40:57,840
all we need to delete a product from the
10221
08:40:55,280 --> 08:41:01,520
cart and then reload and update the
10222
08:40:57,840 --> 08:41:03,840
page. Let's save.
10223
08:41:01,520 --> 08:41:05,520
We'll go to the website
10224
08:41:03,840 --> 08:41:07,280
and we're going to delete this first
10225
08:41:05,520 --> 08:41:09,760
product.
10226
08:41:07,280 --> 08:41:12,638
And notice it updates the cart and it
10227
08:41:09,760 --> 08:41:15,120
also updates the payment summary. If we
10228
08:41:12,638 --> 08:41:17,360
delete another product, you'll notice it
10229
08:41:15,120 --> 08:41:19,760
updates the cart and updates the payment
10230
08:41:17,360 --> 08:41:23,200
summary. So that's how we make the
10231
08:41:19,760 --> 08:41:26,318
delete link work. I'll leave updating
10232
08:41:23,200 --> 08:41:29,520
the quantity in the cart as an exercise.
10233
08:41:26,318 --> 08:41:32,638
So now let's save this into git. We'll
10234
08:41:29,520 --> 08:41:38,398
open the git section and then here we'll
10235
08:41:32,638 --> 08:41:43,160
use the message create delete cart item
10236
08:41:38,398 --> 08:41:43,160
feature and commit.
10237
08:41:45,840 --> 08:41:51,440
Let's go back to the website.
10238
08:41:49,040 --> 08:41:55,200
And in the checkout page, we're going to
10239
08:41:51,440 --> 08:41:57,600
add the ability to create an order. So,
10240
08:41:55,200 --> 08:42:01,760
first let's find the code for this place
10241
08:41:57,600 --> 08:42:04,160
order button. We'll go back to our code
10242
08:42:01,760 --> 08:42:06,318
and in our files,
10243
08:42:04,160 --> 08:42:09,680
we're going to open the payment summary
10244
08:42:06,318 --> 08:42:12,798
component and we're going to scroll down
10245
08:42:09,680 --> 08:42:15,920
to the bottom to this place order
10246
08:42:12,798 --> 08:42:19,840
button. And to this button, we're going
10247
08:42:15,920 --> 08:42:22,958
to add a prop on click equals curly
10248
08:42:19,840 --> 08:42:27,360
brackets. And we'll give it a function.
10249
08:42:22,958 --> 08:42:29,600
Let's call it create order.
10250
08:42:27,360 --> 08:42:34,240
And now let's scroll up and create this
10251
08:42:29,600 --> 08:42:37,120
function. So up here
10252
08:42:34,240 --> 08:42:41,360
we're going to create const and we'll
10253
08:42:37,120 --> 08:42:44,000
name it create order equals an async
10254
08:42:41,360 --> 08:42:46,398
function brackets arrow and curly
10255
08:42:44,000 --> 08:42:48,240
brackets.
10256
08:42:46,398 --> 08:42:51,120
Again we're going to make a request to
10257
08:42:48,240 --> 08:42:55,680
the back end to create the order. So at
10258
08:42:51,120 --> 08:42:59,040
the top we're going to import axios from
10259
08:42:55,680 --> 08:43:02,558
a string axios
10260
08:42:59,040 --> 08:43:06,000
and in here for my back end we can send
10261
08:43:02,558 --> 08:43:08,638
a request with the type post and the URL
10262
08:43:06,000 --> 08:43:11,680
path /api/orders
10263
08:43:08,638 --> 08:43:15,440
to create an order. So we'll use the
10264
08:43:11,680 --> 08:43:18,638
code axios.post post
10265
08:43:15,440 --> 08:43:23,120
to make a request with the type post and
10266
08:43:18,638 --> 08:43:24,718
then give it the URL path slash API/
10267
08:43:23,120 --> 08:43:27,120
orders.
10268
08:43:24,718 --> 08:43:29,600
And that's all we need. Remember the
10269
08:43:27,120 --> 08:43:31,600
cart is already saved in the back end.
10270
08:43:29,600 --> 08:43:34,638
So the back end can take that cart and
10271
08:43:31,600 --> 08:43:37,120
create an order for us.
10272
08:43:34,638 --> 08:43:40,718
So let's wait for this code to finish
10273
08:43:37,120 --> 08:43:43,040
using a wait.
10274
08:43:40,718 --> 08:43:45,120
And after we create an order, the back
10275
08:43:43,040 --> 08:43:48,080
end will remove everything from our
10276
08:43:45,120 --> 08:43:51,680
cart. So we also need to reload our cart
10277
08:43:48,080 --> 08:43:54,798
here. First we need to get the load cart
10278
08:43:51,680 --> 08:43:57,040
function and that can be found in the
10279
08:43:54,798 --> 08:43:59,920
checkout page.
10280
08:43:57,040 --> 08:44:02,398
So here we get load cart from the props
10281
08:43:59,920 --> 08:44:04,718
and we're going to share this with the
10282
08:44:02,398 --> 08:44:07,440
payment summary. So, we'll scroll down
10283
08:44:04,718 --> 08:44:11,440
and in the payment summary, we'll share
10284
08:44:07,440 --> 08:44:17,200
load cart using a prop load cart equals
10285
08:44:11,440 --> 08:44:20,718
curly brackets load cart and save.
10286
08:44:17,200 --> 08:44:24,878
And then inside the payment summary
10287
08:44:20,718 --> 08:44:27,040
at the top in the props, we'll get comma
10288
08:44:24,878 --> 08:44:30,080
load cart.
10289
08:44:27,040 --> 08:44:35,840
And now after we create an order, we're
10290
08:44:30,080 --> 08:44:40,000
going to await load cart and brackets to
10291
08:44:35,840 --> 08:44:42,718
reload the cart and update the page.
10292
08:44:40,000 --> 08:44:45,520
Finally, after we create an order, let's
10293
08:44:42,718 --> 08:44:49,360
also redirect to the orders page so we
10294
08:44:45,520 --> 08:44:51,680
can see our new order. To do that, React
10295
08:44:49,360 --> 08:44:54,638
Router provides a hook that lets us
10296
08:44:51,680 --> 08:44:58,240
navigate to another page or go to
10297
08:44:54,638 --> 08:45:03,760
another page. At the top, we're going to
10298
08:44:58,240 --> 08:45:07,840
import curly brackets use navigate
10299
08:45:03,760 --> 08:45:11,040
from React-Router.
10300
08:45:07,840 --> 08:45:14,398
And at the top of our component, we're
10301
08:45:11,040 --> 08:45:19,680
going to use const
10302
08:45:14,398 --> 08:45:23,600
equals use navigate and brackets.
10303
08:45:19,680 --> 08:45:26,638
So use navigate gives us a function and
10304
08:45:23,600 --> 08:45:30,398
this function lets us navigate to or go
10305
08:45:26,638 --> 08:45:33,680
to another page in our app. So after we
10306
08:45:30,398 --> 08:45:37,440
create an order and we reload the cart,
10307
08:45:33,680 --> 08:45:41,520
let's navigate to the orders page using
10308
08:45:37,440 --> 08:45:46,558
navigate brackets and a string. And
10309
08:45:41,520 --> 08:45:50,478
we're going to navigate to slash orders.
10310
08:45:46,558 --> 08:45:52,878
Now, navigate is not asynchronous, so it
10311
08:45:50,478 --> 08:45:57,760
finishes immediately and we don't need
10312
08:45:52,878 --> 08:46:00,798
to await this code. Let's save.
10313
08:45:57,760 --> 08:46:03,600
And we'll go to the website and test out
10314
08:46:00,798 --> 08:46:05,680
the place order button.
10315
08:46:03,600 --> 08:46:08,638
So, we could update the delivery option
10316
08:46:05,680 --> 08:46:10,958
to get a shipping cost. And then click
10317
08:46:08,638 --> 08:46:13,440
place order.
10318
08:46:10,958 --> 08:46:15,440
And now you can see that it created a
10319
08:46:13,440 --> 08:46:17,440
new order.
10320
08:46:15,440 --> 08:46:20,398
You'll also see that it updated the
10321
08:46:17,440 --> 08:46:24,160
cart. So, the quantity is now zero. And
10322
08:46:20,398 --> 08:46:26,718
it navigated us to the orders page.
10323
08:46:24,160 --> 08:46:29,360
Once we are on the orders page, there's
10324
08:46:26,718 --> 08:46:31,840
some code at the top of the orders page
10325
08:46:29,360 --> 08:46:34,478
that loads the orders from the back end
10326
08:46:31,840 --> 08:46:39,040
and updates the page. And that's how
10327
08:46:34,478 --> 08:46:42,240
we're able to see our newest order here.
10328
08:46:39,040 --> 08:46:44,718
So, that's how we create an order. I'll
10329
08:46:42,240 --> 08:46:47,680
leave making these add to cart buttons
10330
08:46:44,718 --> 08:46:49,440
work as an exercise.
10331
08:46:47,680 --> 08:46:51,760
So now we finished all the major
10332
08:46:49,440 --> 08:46:54,240
features of our project. We can add
10333
08:46:51,760 --> 08:46:57,360
products to our cart. We can update the
10334
08:46:54,240 --> 08:46:59,440
cart and we can create an order. And
10335
08:46:57,360 --> 08:47:02,398
you'll see that in the real world, we
10336
08:46:59,440 --> 08:47:04,398
use a backend to manage and update all
10337
08:47:02,398 --> 08:47:07,200
the data.
10338
08:47:04,398 --> 08:47:09,760
And in our React code, we just make
10339
08:47:07,200 --> 08:47:12,638
requests to the back end and then update
10340
08:47:09,760 --> 08:47:15,440
the page. The advantage of letting the
10341
08:47:12,638 --> 08:47:18,718
backend manage the data is that it will
10342
08:47:15,440 --> 08:47:21,520
simplify our code a lot. So now let's
10343
08:47:18,718 --> 08:47:26,080
save this into git. Go to the git
10344
08:47:21,520 --> 08:47:33,558
section and at the top we'll type make
10345
08:47:26,080 --> 08:47:33,558
create order button work and commit.
10346
08:47:35,680 --> 08:47:40,478
And now we finished all the major
10347
08:47:37,920 --> 08:47:42,318
features of our project. In the next
10348
08:47:40,478 --> 08:47:45,040
lesson, we'll learn how to create
10349
08:47:42,318 --> 08:47:47,360
automated tests in React to make sure
10350
08:47:45,040 --> 08:47:50,000
our code still works whenever we make
10351
08:47:47,360 --> 08:47:52,958
changes. And that's the end of this
10352
08:47:50,000 --> 08:47:55,920
lesson. In this lesson, we learned data
10353
08:47:52,958 --> 08:47:58,638
mutation, which means to update data in
10354
08:47:55,920 --> 08:48:02,878
the back end. We learned different types
10355
08:47:58,638 --> 08:48:06,638
of requests like get, post, put, and
10356
08:48:02,878 --> 08:48:09,120
delete. We sent a post request to add
10357
08:48:06,638 --> 08:48:12,558
products to the cart and to create an
10358
08:48:09,120 --> 08:48:15,920
order. We sent a put request to update
10359
08:48:12,558 --> 08:48:18,878
the cart. We sent a delete request to
10360
08:48:15,920 --> 08:48:21,680
delete a product from the cart. We used
10361
08:48:18,878 --> 08:48:24,000
the dependency array of use effect to
10362
08:48:21,680 --> 08:48:27,040
update the payment summary whenever the
10363
08:48:24,000 --> 08:48:29,680
cart changes. We learned the use
10364
08:48:27,040 --> 08:48:33,360
navigate hook which lets us navigate to
10365
08:48:29,680 --> 08:48:35,520
another page using our code.
10366
08:48:33,360 --> 08:48:37,280
Thanks for watching so far. If you find
10367
08:48:35,520 --> 08:48:39,520
this course valuable, you can support
10368
08:48:37,280 --> 08:48:42,478
the channel by liking this video,
10369
08:48:39,520 --> 08:48:44,160
leaving a comment, and subscribing.
10370
08:48:42,478 --> 08:48:46,240
You can also check out the premium
10371
08:48:44,160 --> 08:48:48,798
version of this course where you'll get
10372
08:48:46,240 --> 08:48:51,600
all the lessons ads free broken down
10373
08:48:48,798 --> 08:48:53,680
into small videos, as well as additional
10374
08:48:51,600 --> 08:48:56,478
lessons and more projects that are not
10375
08:48:53,680 --> 08:48:58,958
in the public React course. Currently,
10376
08:48:56,478 --> 08:49:00,878
the premium course contains 11 lessons,
10377
08:48:58,958 --> 08:49:03,680
and I'll be adding more lessons in the
10378
08:49:00,878 --> 08:49:06,160
future. You can also get a certificate
10379
08:49:03,680 --> 08:49:08,638
of completion, create comments or
10380
08:49:06,160 --> 08:49:10,958
questions on specific topics, get
10381
08:49:08,638 --> 08:49:13,440
progress tracking, search and course
10382
08:49:10,958 --> 08:49:15,520
notes. You can also check out the
10383
08:49:13,440 --> 08:49:18,958
membership where you can get this course
10384
08:49:15,520 --> 08:49:21,920
ads free separated by each lesson, AI
10385
08:49:18,958 --> 08:49:23,840
videos, backend tutorials, and more.
10386
08:49:21,920 --> 08:49:26,718
Thanks again, and let's continue with
10387
08:49:23,840 --> 08:49:28,558
the course. Here are some exercises to
10388
08:49:26,718 --> 08:49:31,798
help you practice what we learned in
10389
08:49:28,558 --> 08:49:31,798
this lesson.
10390
08:50:15,200 --> 08:50:20,878
In this lesson, we're going to add
10391
08:50:17,280 --> 08:50:24,718
automated tests to our app. Automated
10392
08:50:20,878 --> 08:50:26,878
tests means using code to test our app.
10393
08:50:24,718 --> 08:50:29,200
This is more efficient than manually
10394
08:50:26,878 --> 08:50:31,040
opening the app and clicking around to
10395
08:50:29,200 --> 08:50:33,760
test it.
10396
08:50:31,040 --> 08:50:37,040
To create automated tests in React,
10397
08:50:33,760 --> 08:50:39,360
we're going to use an npm package. The
10398
08:50:37,040 --> 08:50:43,280
two most popular packages for automated
10399
08:50:39,360 --> 08:50:45,760
testing are justest and vest.
10400
08:50:43,280 --> 08:50:48,478
We're going to use VEST because it
10401
08:50:45,760 --> 08:50:51,200
integrates well with Vit, but just and
10402
08:50:48,478 --> 08:50:54,240
Vest are very similar. If you learn one,
10403
08:50:51,200 --> 08:50:56,638
you can easily learn the other. First,
10404
08:50:54,240 --> 08:50:58,798
we're going to install Vest to our
10405
08:50:56,638 --> 08:51:01,600
project. So, we're going to go to our
10406
08:50:58,798 --> 08:51:04,318
command line and open the tab that's
10407
08:51:01,600 --> 08:51:07,680
running the front end. And we'll stop
10408
08:51:04,318 --> 08:51:11,920
the VIT server using C.
10409
08:51:07,680 --> 08:51:16,040
And we're going to npm install
10410
08:51:11,920 --> 08:51:16,040
dash save-dev
10411
08:51:16,160 --> 08:51:21,360
test.
10412
08:51:17,760 --> 08:51:23,840
So, save dev means that vest is only for
10413
08:51:21,360 --> 08:51:27,840
development. This package is not needed
10414
08:51:23,840 --> 08:51:31,040
to run our app or to run our website.
10415
08:51:27,840 --> 08:51:33,920
And we're also going to add at to give
10416
08:51:31,040 --> 08:51:36,240
vest a specific version. So you can
10417
08:51:33,920 --> 08:51:39,280
click this link in the description, go
10418
08:51:36,240 --> 08:51:42,478
to lesson 9, and here you'll find the
10419
08:51:39,280 --> 08:51:44,878
version of Vest that we'll install. So
10420
08:51:42,478 --> 08:51:46,798
we'll copy that version and then paste
10421
08:51:44,878 --> 08:51:52,360
it in here
10422
08:51:46,798 --> 08:51:52,360
and press enter to install VEST.
10423
08:51:53,120 --> 08:51:58,958
Now that we installed Vest, we're ready
10424
08:51:55,600 --> 08:52:00,958
to create our first automated test. So
10425
08:51:58,958 --> 08:52:03,840
there are different types of automated
10426
08:52:00,958 --> 08:52:07,760
tests that we can create. The first type
10427
08:52:03,840 --> 08:52:10,798
is called unit tests. A unit test means
10428
08:52:07,760 --> 08:52:13,920
we test one unit or one piece of the
10429
08:52:10,798 --> 08:52:15,680
code. For example, if we look at our
10430
08:52:13,920 --> 08:52:18,878
files
10431
08:52:15,680 --> 08:52:21,520
and we open our utils folder and open
10432
08:52:18,878 --> 08:52:24,558
money.js, JS.
10433
08:52:21,520 --> 08:52:27,200
If we test a function like format money,
10434
08:52:24,558 --> 08:52:30,558
this would be considered a unit test
10435
08:52:27,200 --> 08:52:33,200
because it's just one piece of the code.
10436
08:52:30,558 --> 08:52:35,920
However, if we test something like the
10437
08:52:33,200 --> 08:52:39,280
product component here, testing
10438
08:52:35,920 --> 08:52:41,440
components like this are not unit tests
10439
08:52:39,280 --> 08:52:44,478
because they use many different pieces
10440
08:52:41,440 --> 08:52:46,878
of code together.
10441
08:52:44,478 --> 08:52:49,200
So, we'll start by creating unit tests
10442
08:52:46,878 --> 08:52:52,000
for format money.
10443
08:52:49,200 --> 08:52:55,040
So, we're going to go to this money.js
10444
08:52:52,000 --> 08:52:58,318
file, and beside this file, we're going
10445
08:52:55,040 --> 08:53:01,600
to create a new file for the tests. And
10446
08:52:58,318 --> 08:53:06,440
we'll name it the same money, but this
10447
08:53:01,600 --> 08:53:06,440
time we'll end with test.js.
10448
08:53:07,440 --> 08:53:13,200
So, the naming convention is to put it
10449
08:53:09,680 --> 08:53:15,520
beside the original file and end it with
10450
08:53:13,200 --> 08:53:18,558
test.js.
10451
08:53:15,520 --> 08:53:22,000
Now inside this test file at the top
10452
08:53:18,558 --> 08:53:23,520
we'll import curly brackets
10453
08:53:22,000 --> 08:53:28,080
it
10454
08:53:23,520 --> 08:53:32,558
from a string v test.
10455
08:53:28,080 --> 08:53:34,478
So it lets us create a test and we'll
10456
08:53:32,558 --> 08:53:36,638
also import
10457
08:53:34,478 --> 08:53:40,958
the format
10458
08:53:36,638 --> 08:53:46,440
money function from a string and import
10459
08:53:40,958 --> 08:53:46,440
it from this file dot / money.
10460
08:53:46,558 --> 08:53:52,718
And now to create our first test, we're
10461
08:53:49,280 --> 08:53:55,840
going to use it and brackets and we'll
10462
08:53:52,718 --> 08:53:58,718
give this a string.
10463
08:53:55,840 --> 08:54:01,280
So this creates a test and the string
10464
08:53:58,718 --> 08:54:04,080
here is the name of the test. So this
10465
08:54:01,280 --> 08:54:08,958
describes what we are testing. So let's
10466
08:54:04,080 --> 08:54:10,558
give it a name like formats1
10467
08:54:08,958 --> 08:54:15,318
1999
10468
08:54:10,558 --> 08:54:15,318
cents as $119.99.
10469
08:54:16,000 --> 08:54:22,478
If we run format money with 19.99, it
10470
08:54:19,920 --> 08:54:25,440
should give us $119.99
10471
08:54:22,478 --> 08:54:28,000
as the result. So this name is
10472
08:54:25,440 --> 08:54:30,160
describing what we want to test and a
10473
08:54:28,000 --> 08:54:34,240
naming convention is to make this read
10474
08:54:30,160 --> 08:54:37,680
like English. For example, it formats 1
10475
08:54:34,240 --> 08:54:39,360
1999 as 19.99.
10476
08:54:37,680 --> 08:54:42,080
And then we're going to give this a
10477
08:54:39,360 --> 08:54:44,718
function brackets arrow and curly
10478
08:54:42,080 --> 08:54:46,958
brackets.
10479
08:54:44,718 --> 08:54:49,760
And in here we're going to test if
10480
08:54:46,958 --> 08:54:53,280
format money works correctly.
10481
08:54:49,760 --> 08:54:58,000
To do that, we're also going to import
10482
08:54:53,280 --> 08:55:01,280
expect from vest. So expect lets us
10483
08:54:58,000 --> 08:55:04,878
check if the result is correct. So in
10484
08:55:01,280 --> 08:55:06,718
here we're going to run expect and
10485
08:55:04,878 --> 08:55:09,360
brackets.
10486
08:55:06,718 --> 08:55:12,958
And in here we put the code that we want
10487
08:55:09,360 --> 08:55:19,440
to test. For example, let's put the code
10488
08:55:12,958 --> 08:55:21,840
format money brackets and 1 n99.
10489
08:55:19,440 --> 08:55:24,958
And then expect has different methods
10490
08:55:21,840 --> 08:55:29,840
that we can use to check this result.
10491
08:55:24,958 --> 08:55:32,638
One method is called dot to b and
10492
08:55:29,840 --> 08:55:35,440
brackets.
10493
08:55:32,638 --> 08:55:39,040
So in this second bracket we can put the
10494
08:55:35,440 --> 08:55:41,600
expected result of this code. So we said
10495
08:55:39,040 --> 08:55:44,240
that the expected result should be this
10496
08:55:41,600 --> 08:55:47,240
string here. So we'll give it
10497
08:55:44,240 --> 08:55:47,240
dollar19.99.
10498
08:55:48,160 --> 08:55:55,760
So this code tests if this value is
10499
08:55:51,760 --> 08:55:58,398
equal to this value. If they are equal,
10500
08:55:55,760 --> 08:56:01,840
this check has passed. If they are not
10501
08:55:58,398 --> 08:56:04,798
equal, this check has failed.
10502
08:56:01,840 --> 08:56:08,318
And then expect is going to send this
10503
08:56:04,798 --> 08:56:10,718
result to vest. and V test will display
10504
08:56:08,318 --> 08:56:12,478
a summary of what passed and what
10505
08:56:10,718 --> 08:56:15,200
failed.
10506
08:56:12,478 --> 08:56:17,920
So now let's run this test. We're going
10507
08:56:15,200 --> 08:56:20,398
to save this file
10508
08:56:17,920 --> 08:56:22,558
and then in our command line, we're
10509
08:56:20,398 --> 08:56:24,318
going to create a new tab for running
10510
08:56:22,558 --> 08:56:26,558
the tests.
10511
08:56:24,318 --> 08:56:30,080
And first we have to be inside the
10512
08:56:26,558 --> 08:56:32,160
e-commerce project folder. So we'll cd
10513
08:56:30,080 --> 08:56:35,360
ecommerce
10514
08:56:32,160 --> 08:56:39,040
dash project
10515
08:56:35,360 --> 08:56:42,040
and we're going to run npx
10516
08:56:39,040 --> 08:56:42,040
vest.
10517
08:56:43,920 --> 08:56:50,638
So v test is going to search our code
10518
08:56:46,878 --> 08:56:53,760
for all the files that end with test.js
10519
08:56:50,638 --> 08:56:56,558
and runs the tests inside and then it
10520
08:56:53,760 --> 08:56:59,040
displays the result in the command line.
10521
08:56:56,558 --> 08:57:03,558
So this tells us that our format money
10522
08:56:59,040 --> 08:57:03,558
test here has passed.
10523
08:57:03,760 --> 08:57:09,920
Now let's create another test for format
10524
08:57:06,318 --> 08:57:12,318
money. We'll go back to this test file
10525
08:57:09,920 --> 08:57:15,520
and to create another test, we're just
10526
08:57:12,318 --> 08:57:19,200
going to use it again
10527
08:57:15,520 --> 08:57:21,920
and a string. And remember that we had
10528
08:57:19,200 --> 08:57:23,440
some problems with money not showing two
10529
08:57:21,920 --> 08:57:28,160
decimals.
10530
08:57:23,440 --> 08:57:31,120
So let's test if it displays two
10531
08:57:28,160 --> 08:57:34,958
decimals
10532
08:57:31,120 --> 08:57:38,958
and give it a function.
10533
08:57:34,958 --> 08:57:41,040
And now inside here we can run expect
10534
08:57:38,958 --> 08:57:45,760
and brackets
10535
08:57:41,040 --> 08:57:49,040
format money brackets. And let's give it
10536
08:57:45,760 --> 08:57:53,120
a value that might not show two decimals
10537
08:57:49,040 --> 08:57:55,760
like 1 090.
10538
08:57:53,120 --> 08:57:57,440
And then we'll check if this is correct
10539
08:57:55,760 --> 08:58:03,040
using
10540
08:57:57,440 --> 08:58:05,920
2 tob and brackets and a string. And the
10541
08:58:03,040 --> 08:58:09,600
expected result of this code should be
10542
08:58:05,920 --> 08:58:11,760
dollar 10.90.
10543
08:58:09,600 --> 08:58:17,440
So we're going to check if this result
10544
08:58:11,760 --> 08:58:20,080
is equal to this result. If we save,
10545
08:58:17,440 --> 08:58:22,798
vest is actually watching our files for
10546
08:58:20,080 --> 08:58:25,760
changes. So when we save it will
10547
08:58:22,798 --> 08:58:27,840
automatically rerun our tests. And now
10548
08:58:25,760 --> 08:58:30,240
you can see we have two tests that
10549
08:58:27,840 --> 08:58:33,200
passed.
10550
08:58:30,240 --> 08:58:36,638
Next inside each test we can have
10551
08:58:33,200 --> 08:58:41,520
multiple checks. So in here let's add
10552
08:58:36,638 --> 08:58:46,478
another check using expect brackets.
10553
08:58:41,520 --> 08:58:51,120
And let's try format money brackets with
10554
08:58:46,478 --> 08:58:54,478
a different value like 100. So we expect
10555
08:58:51,120 --> 08:59:02,920
this to be dollar1.00.
10556
08:58:54,478 --> 08:59:02,920
So we'll do 2v brackets dollar 1.0
10557
08:59:03,280 --> 08:59:08,878
and save.
10558
08:59:05,920 --> 08:59:10,798
Now v test will rerun all of our tests.
10559
08:59:08,878 --> 08:59:13,760
And now you can see that both tests
10560
08:59:10,798 --> 08:59:16,798
still passed. So when we have multiple
10561
08:59:13,760 --> 08:59:20,080
checks like this, all of the checks have
10562
08:59:16,798 --> 08:59:22,558
to pass in order for this test to pass.
10563
08:59:20,080 --> 08:59:25,600
If one of these checks fails, the entire
10564
08:59:22,558 --> 08:59:28,080
test will fail and it will show up here.
10565
08:59:25,600 --> 08:59:30,080
So as a quick example, if I change this
10566
08:59:28,080 --> 08:59:31,920
and save,
10567
08:59:30,080 --> 08:59:35,840
one of these checks failed. So we can
10568
08:59:31,920 --> 08:59:40,200
see that this test has failed. So I'm
10569
08:59:35,840 --> 08:59:40,200
going to add it back and save.
10570
08:59:42,318 --> 08:59:48,478
Next, if we look at our file here,
10571
08:59:45,840 --> 08:59:51,360
notice that these tests are related to
10572
08:59:48,478 --> 08:59:53,440
the format money function. A best
10573
08:59:51,360 --> 08:59:57,440
practice is to group these tests
10574
08:59:53,440 --> 09:00:01,600
together. To do that, at the top, we're
10575
08:59:57,440 --> 09:00:05,680
also going to import describe
10576
09:00:01,600 --> 09:00:09,040
from vest. So, describe groups test
10577
09:00:05,680 --> 09:00:11,200
together. we call a group of tests a
10578
09:00:09,040 --> 09:00:14,080
test suite.
10579
09:00:11,200 --> 09:00:18,878
So above these tests, we're going to use
10580
09:00:14,080 --> 09:00:21,120
the code describe and brackets and we'll
10581
09:00:18,878 --> 09:00:23,920
give this a string
10582
09:00:21,120 --> 09:00:26,878
and this is the name of the test suite
10583
09:00:23,920 --> 09:00:31,440
or the group of tests. So these tests
10584
09:00:26,878 --> 09:00:34,318
are for the format money function and
10585
09:00:31,440 --> 09:00:36,558
we'll give it a function brackets arrow
10586
09:00:34,318 --> 09:00:39,200
and curly brackets
10587
09:00:36,558 --> 09:00:44,478
and we're going to move our test inside.
10588
09:00:39,200 --> 09:00:47,760
So we'll select it and cut and paste
10589
09:00:44,478 --> 09:00:50,638
and save
10590
09:00:47,760 --> 09:00:53,600
and everything still passes
10591
09:00:50,638 --> 09:00:56,878
and we can see the test suite name over
10592
09:00:53,600 --> 09:00:59,440
here. So test suites basically help us
10593
09:00:56,878 --> 09:01:01,600
keep our tests more organized. If we
10594
09:00:59,440 --> 09:01:04,638
want to test a different function inside
10595
09:01:01,600 --> 09:01:07,760
this file, we would create another test
10596
09:01:04,638 --> 09:01:10,398
suite and group our tests in there. So a
10597
09:01:07,760 --> 09:01:12,958
best practice is to always put our tests
10598
09:01:10,398 --> 09:01:16,398
inside a test suite, even if we only
10599
09:01:12,958 --> 09:01:19,120
have one test suite. So now let's save
10600
09:01:16,398 --> 09:01:21,120
this into git. We're going to go to the
10601
09:01:19,120 --> 09:01:25,600
get section
10602
09:01:21,120 --> 09:01:29,680
and let's use the message create unit
10603
09:01:25,600 --> 09:01:34,280
test for format
10604
09:01:29,680 --> 09:01:34,280
money and commit.
10605
09:01:36,558 --> 09:01:43,440
Next, we'll create another type of test
10606
09:01:40,080 --> 09:01:46,638
called an integration test. Integration
10607
09:01:43,440 --> 09:01:50,000
test means we test multiple units or
10608
09:01:46,638 --> 09:01:53,360
pieces of code working together.
10609
09:01:50,000 --> 09:01:57,120
For example, if we look at our files and
10610
09:01:53,360 --> 09:01:59,680
we open the product component,
10611
09:01:57,120 --> 09:02:03,280
testing a component would be considered
10612
09:01:59,680 --> 09:02:06,240
an integration test. Notice this uses
10613
09:02:03,280 --> 09:02:08,798
multiple pieces of code like the format
10614
09:02:06,240 --> 09:02:10,318
money function and the load cart
10615
09:02:08,798 --> 09:02:13,440
function.
10616
09:02:10,318 --> 09:02:15,920
So now let's create tests for this file.
10617
09:02:13,440 --> 09:02:18,798
Beside product.js,
10618
09:02:15,920 --> 09:02:23,478
we're going to create a new file
10619
09:02:18,798 --> 09:02:23,478
and we'll name it product.est.jsx.
10620
09:02:28,000 --> 09:02:34,000
So we end with JSX this time because
10621
09:02:31,360 --> 09:02:36,638
this will let us use component syntax in
10622
09:02:34,000 --> 09:02:38,718
this file like this.
10623
09:02:36,638 --> 09:02:42,478
So, first let's actually copy the
10624
09:02:38,718 --> 09:02:44,878
imports from our money test file because
10625
09:02:42,478 --> 09:02:46,878
we're going to be using these again. So,
10626
09:02:44,878 --> 09:02:49,040
we'll copy
10627
09:02:46,878 --> 09:02:51,600
and in here
10628
09:02:49,040 --> 09:02:55,120
we'll paste,
10629
09:02:51,600 --> 09:03:00,558
but this time we're going to import the
10630
09:02:55,120 --> 09:03:03,558
product component from the dot /rouct
10631
09:03:00,558 --> 09:03:03,558
file.
10632
09:03:04,000 --> 09:03:11,040
And first we're going to create a test
10633
09:03:06,398 --> 09:03:13,280
suite. So we use describe brackets and a
10634
09:03:11,040 --> 09:03:16,958
string. And we're going to be testing
10635
09:03:13,280 --> 09:03:20,318
the product component.
10636
09:03:16,958 --> 09:03:22,240
And give this a function.
10637
09:03:20,318 --> 09:03:26,398
And inside we'll create our first
10638
09:03:22,240 --> 09:03:28,240
integration test using it brackets and a
10639
09:03:26,398 --> 09:03:30,558
string.
10640
09:03:28,240 --> 09:03:33,120
And for components, the first thing we
10641
09:03:30,558 --> 09:03:36,160
can check is if the component is
10642
09:03:33,120 --> 09:03:38,160
displayed correctly. So for the product
10643
09:03:36,160 --> 09:03:41,760
component, we can check if the name is
10644
09:03:38,160 --> 09:03:45,600
correct, the image is correct and so on.
10645
09:03:41,760 --> 09:03:52,318
So we'll name this test it
10646
09:03:45,600 --> 09:03:56,280
displays the product details correctly.
10647
09:03:52,318 --> 09:03:56,280
And we'll give it a function.
10648
09:03:56,478 --> 09:04:02,000
And now inside here, how do we check if
10649
09:03:59,440 --> 09:04:05,200
this component displayed the details
10650
09:04:02,000 --> 09:04:07,520
correctly? So when testing a function,
10651
09:04:05,200 --> 09:04:10,718
we run the function and check the
10652
09:04:07,520 --> 09:04:13,040
result. When testing a component, we
10653
09:04:10,718 --> 09:04:16,240
render the component and check the
10654
09:04:13,040 --> 09:04:19,760
result. So render means to display the
10655
09:04:16,240 --> 09:04:22,478
component on the page. To render the
10656
09:04:19,760 --> 09:04:25,840
component in our test, we need to add
10657
09:04:22,478 --> 09:04:29,120
some more npm packages. So, we're going
10658
09:04:25,840 --> 09:04:31,680
to go to our command line and right now
10659
09:04:29,120 --> 09:04:34,318
vest is watching all of our files for
10660
09:04:31,680 --> 09:04:39,440
changes. So, we're going to stop vest
10661
09:04:34,318 --> 09:04:41,360
using C or you can press Q to quit.
10662
09:04:39,440 --> 09:04:44,958
And now we're going to click this link
10663
09:04:41,360 --> 09:04:47,520
in the description, go to lesson 9, and
10664
09:04:44,958 --> 09:04:50,080
we're going to install all of these npm
10665
09:04:47,520 --> 09:04:54,160
packages that we need. So, we're going
10666
09:04:50,080 --> 09:04:56,558
to select this and copy. And then in our
10667
09:04:54,160 --> 09:04:58,318
command line, we're going to paste this
10668
09:04:56,558 --> 09:05:01,360
command
10669
09:04:58,318 --> 09:05:04,360
and press enter to install all these
10670
09:05:01,360 --> 09:05:04,360
packages.
10671
09:05:04,958 --> 09:05:09,760
After installing, we also need to create
10672
09:05:07,360 --> 09:05:13,120
some config files to test our
10673
09:05:09,760 --> 09:05:16,398
components. So, we'll go to our files
10674
09:05:13,120 --> 09:05:18,398
and we'll scroll down and find
10675
09:05:16,398 --> 09:05:20,558
vit.config.js
10676
09:05:18,398 --> 09:05:23,840
and click it. And we're going to create
10677
09:05:20,558 --> 09:05:26,840
a new file and name this
10678
09:05:23,840 --> 09:05:26,840
vtest.config.js.
10679
09:05:30,318 --> 09:05:35,520
And we'll click this link in the
10680
09:05:31,920 --> 09:05:38,398
description and go to lesson 9. We're
10681
09:05:35,520 --> 09:05:42,318
going to copy this setup code. So we're
10682
09:05:38,398 --> 09:05:47,200
going to select this and copy.
10683
09:05:42,318 --> 09:05:50,160
And then in here we'll paste that code
10684
09:05:47,200 --> 09:05:52,240
and save.
10685
09:05:50,160 --> 09:05:54,958
And we're also going to create another
10686
09:05:52,240 --> 09:05:58,878
file beside this.
10687
09:05:54,958 --> 09:06:02,920
So here create a new file called setup
10688
09:05:58,878 --> 09:06:02,920
capital T tests.js.
10689
09:06:04,798 --> 09:06:09,680
And we'll click the link in the
10690
09:06:06,318 --> 09:06:13,520
description again and go to lesson 9.
10691
09:06:09,680 --> 09:06:16,558
And we're going to copy this code
10692
09:06:13,520 --> 09:06:19,278
and paste it in here
10693
09:06:16,558 --> 09:06:21,040
and save.
10694
09:06:19,278 --> 09:06:25,200
So that's just some setup that we need
10695
09:06:21,040 --> 09:06:26,878
to do. If we look at this config file,
10696
09:06:25,200 --> 09:06:28,878
you don't have to worry too much about
10697
09:06:26,878 --> 09:06:32,000
how this works. This is just some
10698
09:06:28,878 --> 09:06:34,718
standard setup. These two are required
10699
09:06:32,000 --> 09:06:38,800
for component tests or integration tests
10700
09:06:34,718 --> 09:06:41,278
to work. And this basically runs the
10701
09:06:38,800 --> 09:06:45,840
code inside this file before all of our
10702
09:06:41,278 --> 09:06:49,122
tests. So in this file we import just
10703
09:06:45,840 --> 09:06:52,000
DOM and this basically adds a bunch of
10704
09:06:49,122 --> 09:06:55,000
methods to expect that help us test
10705
09:06:52,000 --> 09:06:55,000
components.
10706
09:06:55,840 --> 09:07:01,520
Now that we have the setup, we're going
10707
09:06:58,078 --> 09:07:05,918
to go back to our test
10708
09:07:01,520 --> 09:07:08,960
and at the top we're going to import
10709
09:07:05,918 --> 09:07:12,480
curly brackets render
10710
09:07:08,960 --> 09:07:15,480
from the package at testing-
10711
09:07:12,480 --> 09:07:15,480
library/react.
10712
09:07:16,800 --> 09:07:23,520
So this renders or displays a component
10713
09:07:20,558 --> 09:07:25,200
in a fake web page specifically for
10714
09:07:23,520 --> 09:07:28,320
testing.
10715
09:07:25,200 --> 09:07:32,238
Inside our test, we're going to use the
10716
09:07:28,320 --> 09:07:34,558
render function and brackets
10717
09:07:32,238 --> 09:07:38,800
and we'll tell it to render the product
10718
09:07:34,558 --> 09:07:41,360
component using less than product
10719
09:07:38,800 --> 09:07:44,160
slashgreater than.
10720
09:07:41,360 --> 09:07:47,040
So next the product component has some
10721
09:07:44,160 --> 09:07:50,878
props. So we need to provide these props
10722
09:07:47,040 --> 09:07:54,398
in our test. So in here we're going to
10723
09:07:50,878 --> 09:07:55,918
add the prop product equals curly
10724
09:07:54,398 --> 09:07:57,840
brackets
10725
09:07:55,918 --> 09:08:01,278
and we're going to provide a sample
10726
09:07:57,840 --> 09:08:03,840
product to test with. To do that in our
10727
09:08:01,278 --> 09:08:07,200
files we're going to go to the starting
10728
09:08:03,840 --> 09:08:10,480
code folder and open the data folder and
10729
09:08:07,200 --> 09:08:12,558
open products.js.
10730
09:08:10,480 --> 09:08:15,278
And here we have a bunch of sample
10731
09:08:12,558 --> 09:08:20,480
products that we can use. So, let's just
10732
09:08:15,278 --> 09:08:23,680
select the first product and copy it.
10733
09:08:20,480 --> 09:08:26,078
And then in our test,
10734
09:08:23,680 --> 09:08:31,360
we're going to save the sample product
10735
09:08:26,078 --> 09:08:34,320
in a variable const product equals and
10736
09:08:31,360 --> 09:08:37,200
paste.
10737
09:08:34,320 --> 09:08:41,918
And we're going to take this product and
10738
09:08:37,200 --> 09:08:44,640
save it as the product prop.
10739
09:08:41,918 --> 09:08:47,758
Next, we also need to provide a load
10740
09:08:44,640 --> 09:08:51,122
cart function to this component. So in
10741
09:08:47,758 --> 09:08:54,480
our app, load cart actually contacts the
10742
09:08:51,122 --> 09:08:56,960
backend. However, in our tests, we
10743
09:08:54,480 --> 09:08:58,960
should not contact a real backend
10744
09:08:56,960 --> 09:09:01,758
because the backend might not be
10745
09:08:58,960 --> 09:09:05,438
available during tests. In our tests
10746
09:09:01,758 --> 09:09:08,718
might accidentally update real data. So
10747
09:09:05,438 --> 09:09:12,078
instead, a best practice is to mock this
10748
09:09:08,718 --> 09:09:15,758
function in our tests. Mock means to
10749
09:09:12,078 --> 09:09:19,360
create a fake version of this function.
10750
09:09:15,758 --> 09:09:24,398
To create a mock at the top, we're going
10751
09:09:19,360 --> 09:09:27,360
to import vi from vest.
10752
09:09:24,398 --> 09:09:34,960
And we'll scroll back down and we'll
10753
09:09:27,360 --> 09:09:37,520
create const load cart equals vi.fn. fn
10754
09:09:34,960 --> 09:09:39,758
and brackets.
10755
09:09:37,520 --> 09:09:42,878
So, vi.fn
10756
09:09:39,758 --> 09:09:45,758
basically creates a fake function that
10757
09:09:42,878 --> 09:09:48,320
doesn't do anything. So, this fake
10758
09:09:45,758 --> 09:09:50,078
function is called a mock and we're
10759
09:09:48,320 --> 09:09:53,758
going to give this mock to the
10760
09:09:50,078 --> 09:09:57,758
component. So, here we'll create a prop
10761
09:09:53,758 --> 09:10:02,160
load cart equals curly bracket and our
10762
09:09:57,758 --> 09:10:04,398
mock load cart. And this will prevent
10763
09:10:02,160 --> 09:10:07,758
our component from accidentally
10764
09:10:04,398 --> 09:10:10,398
contacting the back end in our tests.
10765
09:10:07,758 --> 09:10:14,320
And that's it. This will render the
10766
09:10:10,398 --> 09:10:16,878
product component on a fake web page. So
10767
09:10:14,320 --> 09:10:20,078
now, how do we check if this component
10768
09:10:16,878 --> 09:10:22,480
was displayed correctly? We can't see
10769
09:10:20,078 --> 09:10:25,278
the fake web page, but we can check it
10770
09:10:22,480 --> 09:10:27,520
using our code. To do that, we're going
10771
09:10:25,278 --> 09:10:31,840
to scroll up
10772
09:10:27,520 --> 09:10:34,640
and we'll also import the screen from
10773
09:10:31,840 --> 09:10:38,000
testing library/react.
10774
09:10:34,640 --> 09:10:40,480
So screen lets us check the screen or
10775
09:10:38,000 --> 09:10:43,438
the fake web page to see if everything
10776
09:10:40,480 --> 09:10:46,640
was rendered correctly
10777
09:10:43,438 --> 09:10:50,960
in our test. After we render the
10778
09:10:46,640 --> 09:10:52,718
component, we're going to use screen and
10779
09:10:50,960 --> 09:10:56,960
the method
10780
09:10:52,718 --> 09:11:00,480
get by text and brackets.
10781
09:10:56,960 --> 09:11:04,000
So this searches the screen or the fake
10782
09:11:00,480 --> 09:11:07,680
web page for an element with a specific
10783
09:11:04,000 --> 09:11:09,918
text. So we'll give it a string
10784
09:11:07,680 --> 09:11:13,278
and first let's check if the product
10785
09:11:09,918 --> 09:11:16,480
name is displayed correctly. So, we'll
10786
09:11:13,278 --> 09:11:19,122
scroll up and we're going to copy this
10787
09:11:16,480 --> 09:11:23,758
product name.
10788
09:11:19,122 --> 09:11:26,238
And then in here, we're going to paste.
10789
09:11:23,758 --> 09:11:29,122
And this will search the screen or the
10790
09:11:26,238 --> 09:11:31,040
fake web page for an element with this
10791
09:11:29,122 --> 09:11:32,800
text inside.
10792
09:11:31,040 --> 09:11:35,598
So, now we're going to check if this
10793
09:11:32,800 --> 09:11:37,758
element exists. That will tell us if we
10794
09:11:35,598 --> 09:11:41,758
displayed the correct product on the
10795
09:11:37,758 --> 09:11:46,160
page. To do that before this code, we'll
10796
09:11:41,758 --> 09:11:50,078
use expect and open bracket
10797
09:11:46,160 --> 09:11:57,040
and at the end a close bracket and we'll
10798
09:11:50,078 --> 09:11:59,360
use the method dot to be in the document
10799
09:11:57,040 --> 09:12:02,160
and brackets.
10800
09:11:59,360 --> 09:12:04,878
So this method is added by testing
10801
09:12:02,160 --> 09:12:08,398
library/jestdom
10802
09:12:04,878 --> 09:12:12,078
and this checks if this element exists
10803
09:12:08,398 --> 09:12:14,960
on the screen or on the fake web page.
10804
09:12:12,078 --> 09:12:18,558
So now let's save
10805
09:12:14,960 --> 09:12:25,398
and we'll run this test using vest in
10806
09:12:18,558 --> 09:12:25,398
the command line. We'll run npx v test.
10807
09:12:27,122 --> 09:12:34,238
And all of our tests passed.
10808
09:12:31,438 --> 09:12:37,360
Next, let's add some more checks to this
10809
09:12:34,238 --> 09:12:40,960
test. So, let's also check if the price
10810
09:12:37,360 --> 09:12:45,122
was displayed correctly. So, here we'll
10811
09:12:40,960 --> 09:12:49,840
add another expect brackets and we'll
10812
09:12:45,122 --> 09:12:52,238
use screen.get get by text
10813
09:12:49,840 --> 09:12:55,598
and a string and we're going to search
10814
09:12:52,238 --> 09:12:58,800
for the price on the screen. So for the
10815
09:12:55,598 --> 09:13:01,360
sample product, the price should be
10816
09:12:58,800 --> 09:13:04,160
dollar10.90.
10817
09:13:01,360 --> 09:13:08,160
So, we'll scroll down and search for
10818
09:13:04,160 --> 09:13:14,960
dollar 10.90 on the screen. And we
10819
09:13:08,160 --> 09:13:17,680
expect this dot to be in the document
10820
09:13:14,960 --> 09:13:20,640
and brackets
10821
09:13:17,680 --> 09:13:23,200
and save
10822
09:13:20,640 --> 09:13:25,598
and feed will rerun the test and tell us
10823
09:13:23,200 --> 09:13:28,078
that this passed.
10824
09:13:25,598 --> 09:13:29,918
Next, let's check if the product image
10825
09:13:28,078 --> 09:13:33,840
is correct.
10826
09:13:29,918 --> 09:13:35,360
So down here we'll use another expect
10827
09:13:33,840 --> 09:13:39,840
brackets
10828
09:13:35,360 --> 09:13:42,160
screen dot but this time we can't use
10829
09:13:39,840 --> 09:13:45,598
get by text because we're trying to find
10830
09:13:42,160 --> 09:13:49,438
an image. Instead we're going to add a
10831
09:13:45,598 --> 09:13:53,438
test ID to the image element. So in our
10832
09:13:49,438 --> 09:13:56,160
files we'll open the product component
10833
09:13:53,438 --> 09:14:00,078
and we're going to scroll down until we
10834
09:13:56,160 --> 09:14:02,078
find the product image here.
10835
09:14:00,078 --> 09:14:05,122
And for this element, we're going to add
10836
09:14:02,078 --> 09:14:11,360
an attribute data-
10837
09:14:05,122 --> 09:14:15,680
test id equals a string product dash
10838
09:14:11,360 --> 09:14:18,640
image. So this ID lets us find the image
10839
09:14:15,680 --> 09:14:21,122
element in our tests. We could also use
10840
09:14:18,640 --> 09:14:23,200
the class name to find this element, but
10841
09:14:21,122 --> 09:14:25,840
if we change our class name, it will
10842
09:14:23,200 --> 09:14:28,078
break the test. To avoid this, we
10843
09:14:25,840 --> 09:14:33,200
usually use an ID specifically for
10844
09:14:28,078 --> 09:14:38,160
testing like this. So now let's save.
10845
09:14:33,200 --> 09:14:44,558
And in our test, we'll use screen dot
10846
09:14:38,160 --> 09:14:49,438
get by test ID and brackets and a string
10847
09:14:44,558 --> 09:14:51,758
and the test ID product dash image. So
10848
09:14:49,438 --> 09:14:54,238
this will give us the image element in
10849
09:14:51,758 --> 09:14:56,718
the product component.
10850
09:14:54,238 --> 09:15:00,398
Now let's check if this image element
10851
09:14:56,718 --> 09:15:04,960
source attribute is equal to the
10852
09:15:00,398 --> 09:15:09,438
products image up here. So we'll select
10853
09:15:04,960 --> 09:15:12,960
this image and copy
10854
09:15:09,438 --> 09:15:18,078
and then down here
10855
09:15:12,960 --> 09:15:21,278
dot to have attribute
10856
09:15:18,078 --> 09:15:24,558
brackets and we'll check the source
10857
09:15:21,278 --> 09:15:26,718
attribute if it's equal to the string
10858
09:15:24,558 --> 09:15:30,558
and paste
10859
09:15:26,718 --> 09:15:33,520
the image from our sample product
10860
09:15:30,558 --> 09:15:35,840
and Save
10861
09:15:33,520 --> 09:15:37,680
V test will rerun our tests and
10862
09:15:35,840 --> 09:15:40,320
everything passed. So we know we're
10863
09:15:37,680 --> 09:15:42,960
displaying the correct image.
10864
09:15:40,320 --> 09:15:45,680
Next, let's check the ratings image and
10865
09:15:42,960 --> 09:15:48,718
the rating count. Feel free to pause if
10866
09:15:45,680 --> 09:15:52,078
you want to try it yourself first. So
10867
09:15:48,718 --> 09:15:56,960
first we'll do expect
10868
09:15:52,078 --> 09:15:59,758
open bracket the screen dot and we'll
10869
09:15:56,960 --> 09:16:02,480
check the ratings image first.
10870
09:15:59,758 --> 09:16:04,878
So, we'll go to the product component
10871
09:16:02,480 --> 09:16:08,398
and we're going to scroll down until we
10872
09:16:04,878 --> 09:16:12,718
find the image product rating stars and
10873
09:16:08,398 --> 09:16:14,320
we'll give this a data dash test id
10874
09:16:12,718 --> 09:16:21,680
equals
10875
09:16:14,320 --> 09:16:24,800
product rating stars image and save.
10876
09:16:21,680 --> 09:16:26,718
And let's just select this test ID and
10877
09:16:24,800 --> 09:16:28,960
copy.
10878
09:16:26,718 --> 09:16:35,040
And in our test,
10879
09:16:28,960 --> 09:16:38,480
we'll get by test ID brackets a string
10880
09:16:35,040 --> 09:16:43,520
and paste.
10881
09:16:38,480 --> 09:16:47,360
And we expect this dot to have attribute
10882
09:16:43,520 --> 09:16:48,960
brackets the source attribute equals a
10883
09:16:47,360 --> 09:16:50,800
string.
10884
09:16:48,960 --> 09:16:52,480
And we'll go back to the product
10885
09:16:50,800 --> 09:16:54,640
component.
10886
09:16:52,480 --> 09:16:59,680
And let's just copy the source attribute
10887
09:16:54,640 --> 09:17:02,640
here. So we'll select it and copy.
10888
09:16:59,680 --> 09:17:05,122
And inside our test we're going to
10889
09:17:02,640 --> 09:17:08,078
paste.
10890
09:17:05,122 --> 09:17:10,640
Now for our sample product we have a
10891
09:17:08,078 --> 09:17:12,480
rating of
10892
09:17:10,640 --> 09:17:15,840
4.5.
10893
09:17:12,480 --> 09:17:19,758
So that means we take 4.5 and multiply
10894
09:17:15,840 --> 09:17:21,360
it by 10. We're going to get the value
10895
09:17:19,758 --> 09:17:23,598
45.
10896
09:17:21,360 --> 09:17:29,438
And this is the rating image that should
10897
09:17:23,598 --> 09:17:32,800
be displayed on the page. So we'll save.
10898
09:17:29,438 --> 09:17:36,320
And v test tells us everything passed.
10899
09:17:32,800 --> 09:17:41,598
Finally, let's check the rating count.
10900
09:17:36,320 --> 09:17:43,840
So we'll expect brackets screen dot. And
10901
09:17:41,598 --> 09:17:49,122
this time we're looking for text. So we
10902
09:17:43,840 --> 09:17:52,000
can just use get by text brackets. And
10903
09:17:49,122 --> 09:17:54,000
for this sample product, the count is
10904
09:17:52,000 --> 09:17:58,398
87.
10905
09:17:54,000 --> 09:18:06,000
So we'll look for the string 87.
10906
09:17:58,398 --> 09:18:09,040
And we expect this to be in the document
10907
09:18:06,000 --> 09:18:12,878
and save.
10908
09:18:09,040 --> 09:18:15,200
Vest will rerun and everything passed.
10909
09:18:12,878 --> 09:18:17,758
So now we tested if the product
10910
09:18:15,200 --> 09:18:21,598
component displayed all the details
10911
09:18:17,758 --> 09:18:24,558
correctly. So let's save this into git.
10912
09:18:21,598 --> 09:18:27,758
We'll open the git section
10913
09:18:24,558 --> 09:18:32,960
and we'll use the message create
10914
09:18:27,758 --> 09:18:36,438
integration test for product component
10915
09:18:32,960 --> 09:18:36,438
and commit.
10916
09:18:39,122 --> 09:18:42,878
Next we're going to test user
10917
09:18:41,200 --> 09:18:45,520
interactions.
10918
09:18:42,878 --> 09:18:48,000
In the product component, we have an add
10919
09:18:45,520 --> 09:18:50,718
to cart button. We can also test
10920
09:18:48,000 --> 09:18:53,360
clicking this add to cart button to see
10921
09:18:50,718 --> 09:18:56,800
if it behaves correctly.
10922
09:18:53,360 --> 09:19:02,320
So in our test file, let's create a new
10923
09:18:56,800 --> 09:19:08,000
test using it brackets and a string. And
10924
09:19:02,320 --> 09:19:11,960
let's name it adds a product to the cart
10925
09:19:08,000 --> 09:19:11,960
and give it a function.
10926
09:19:13,758 --> 09:19:20,320
So first let's render the product
10927
09:19:16,080 --> 09:19:23,680
component again. So we'll scroll up
10928
09:19:20,320 --> 09:19:26,240
and let's just copy all of this code. So
10929
09:19:23,680 --> 09:19:29,916
we'll select it
10930
09:19:26,240 --> 09:19:32,880
to here and copy.
10931
09:19:29,916 --> 09:19:35,520
And then in our second test,
10932
09:19:32,880 --> 09:19:38,320
we're going to paste.
10933
09:19:35,520 --> 09:19:40,800
So this sets up the sample product and
10934
09:19:38,320 --> 09:19:43,840
the mock and then renders the product
10935
09:19:40,800 --> 09:19:46,640
component again. So now that we rendered
10936
09:19:43,840 --> 09:19:48,960
this, how do we click the add to cart
10937
09:19:46,640 --> 09:19:51,436
button in our test?
10938
09:19:48,960 --> 09:19:55,120
Earlier we installed a package called
10939
09:19:51,436 --> 09:19:58,160
testing library/ user event. This
10940
09:19:55,120 --> 09:20:01,596
package lets us simulate events like
10941
09:19:58,160 --> 09:20:05,596
clicking a button. So at the top of our
10942
09:20:01,596 --> 09:20:11,120
test, we're going to scroll up and we'll
10943
09:20:05,596 --> 09:20:14,080
import user event from the package at
10944
09:20:11,120 --> 09:20:17,080
testing- library/
10945
09:20:14,080 --> 09:20:17,080
user-event.
10946
09:20:18,000 --> 09:20:22,320
So this lets us simulate events like a
10947
09:20:20,800 --> 09:20:24,240
click.
10948
09:20:22,320 --> 09:20:26,640
So now we'll scroll back down to our
10949
09:20:24,240 --> 09:20:30,000
test
10950
09:20:26,640 --> 09:20:35,916
and first we need to set up user event
10951
09:20:30,000 --> 09:20:39,680
using const user equals user event dot
10952
09:20:35,916 --> 09:20:43,276
setup and brackets.
10953
09:20:39,680 --> 09:20:46,276
So now we can simulate events using
10954
09:20:43,276 --> 09:20:46,276
user.click.
10955
09:20:47,120 --> 09:20:52,080
So this simulates a click event and
10956
09:20:50,160 --> 09:20:55,520
inside the brackets we're going to tell
10957
09:20:52,080 --> 09:20:58,560
it which element to click. So we want it
10958
09:20:55,520 --> 09:21:00,960
to click the add to cart button. So
10959
09:20:58,560 --> 09:21:04,480
first we need to get the add to cart
10960
09:21:00,960 --> 09:21:06,320
button inside our test. We'll go to our
10961
09:21:04,480 --> 09:21:09,520
files
10962
09:21:06,320 --> 09:21:12,880
and in the product component,
10963
09:21:09,520 --> 09:21:15,916
let's scroll down to the add to cart
10964
09:21:12,880 --> 09:21:22,320
button and we're going to give this a
10965
09:21:15,916 --> 09:21:25,360
data dash test ID equals a string add to
10966
09:21:22,320 --> 09:21:29,596
cart button.
10967
09:21:25,360 --> 09:21:32,400
And let's select this test ID and copy
10968
09:21:29,596 --> 09:21:35,916
and save.
10969
09:21:32,400 --> 09:21:40,480
And then in the product test, we're
10970
09:21:35,916 --> 09:21:45,360
going to get the button using screen dot
10971
09:21:40,480 --> 09:21:48,400
get by test ID brackets
10972
09:21:45,360 --> 09:21:50,080
and a string and we'll paste the test
10973
09:21:48,400 --> 09:21:52,640
ID.
10974
09:21:50,080 --> 09:21:54,640
So this will get the add to cart button
10975
09:21:52,640 --> 09:21:58,960
from the screen.
10976
09:21:54,640 --> 09:22:01,916
So let's save it in a variable const add
10977
09:21:58,960 --> 09:22:03,436
to cart button
10978
09:22:01,916 --> 09:22:04,960
equals
10979
09:22:03,436 --> 09:22:06,640
and we're going to give this to
10980
09:22:04,960 --> 09:22:11,596
user.click
10981
09:22:06,640 --> 09:22:14,800
add to cart button.
10982
09:22:11,596 --> 09:22:17,436
Next when we click this button it takes
10983
09:22:14,800 --> 09:22:21,040
some time for the page to process the
10984
09:22:17,436 --> 09:22:24,160
click. So user.click takes some time to
10985
09:22:21,040 --> 09:22:28,160
finish. That means this is asynchronous
10986
09:22:24,160 --> 09:22:31,120
code and it returns a promise. To wait
10987
09:22:28,160 --> 09:22:33,756
for this to finish at the front, we're
10988
09:22:31,120 --> 09:22:36,480
going to use await.
10989
09:22:33,756 --> 09:22:40,800
And remember to use await we need to be
10990
09:22:36,480 --> 09:22:43,120
inside an async function. So above in
10991
09:22:40,800 --> 09:22:45,840
this function, we're going to make it
10992
09:22:43,120 --> 09:22:49,276
async.
10993
09:22:45,840 --> 09:22:52,240
And we'll scroll back down.
10994
09:22:49,276 --> 09:22:54,880
And now once we click the add to cart
10995
09:22:52,240 --> 09:22:58,720
button, this is going to make a request
10996
09:22:54,880 --> 09:23:01,276
to the back end using axios.post.
10997
09:22:58,720 --> 09:23:03,916
But remember in our tests we should not
10998
09:23:01,276 --> 09:23:07,120
contact a real backend because we might
10999
09:23:03,916 --> 09:23:10,160
accidentally update real data.
11000
09:23:07,120 --> 09:23:13,680
So earlier we mocked load cart and
11001
09:23:10,160 --> 09:23:17,040
replaced it with a fake function.
11002
09:23:13,680 --> 09:23:19,680
Now we're also going to mock axios. So
11003
09:23:17,040 --> 09:23:25,120
we can actually mock an entire npm
11004
09:23:19,680 --> 09:23:28,400
package. To do that at the top,
11005
09:23:25,120 --> 09:23:31,200
we're going to use the code vi
11006
09:23:28,400 --> 09:23:34,560
mock brackets
11007
09:23:31,200 --> 09:23:38,480
and the string axios.
11008
09:23:34,560 --> 09:23:40,640
So this mocks the entire axios package.
11009
09:23:38,480 --> 09:23:43,680
This means that when we are testing,
11010
09:23:40,640 --> 09:23:46,800
when we import from axios, we will get a
11011
09:23:43,680 --> 09:23:49,436
fake version of axios instead.
11012
09:23:46,800 --> 09:23:52,240
So when we click add to cart, it will
11013
09:23:49,436 --> 09:23:54,800
use the fake version of Axios and it
11014
09:23:52,240 --> 09:23:59,436
will do nothing. It will not contact the
11015
09:23:54,800 --> 09:24:02,000
back end. So now what should we test? We
11016
09:23:59,436 --> 09:24:03,756
can still test that our code ran
11017
09:24:02,000 --> 09:24:06,560
axios.post
11018
09:24:03,756 --> 09:24:09,360
and we can test if we gave axios.post
11019
09:24:06,560 --> 09:24:12,560
the correct values.
11020
09:24:09,360 --> 09:24:19,200
To do that at the top, we're also going
11021
09:24:12,560 --> 09:24:22,000
to import Axios from the package Axios.
11022
09:24:19,200 --> 09:24:25,916
And because of vi.mmock,
11023
09:24:22,000 --> 09:24:30,480
this is a fake version of Axios.
11024
09:24:25,916 --> 09:24:34,560
So now we can scroll back down
11025
09:24:30,480 --> 09:24:40,960
and after we click add to cart, we can
11026
09:24:34,560 --> 09:24:44,480
expect brackets axios.post. post.
11027
09:24:40,960 --> 09:24:47,840
If we give a mock to expect, we can
11028
09:24:44,480 --> 09:24:51,276
check if our code ran axios.post
11029
09:24:47,840 --> 09:24:57,756
and if we gave it the correct values
11030
09:24:51,276 --> 09:25:01,276
using the method dot to have been called
11031
09:24:57,756 --> 09:25:04,720
with and brackets.
11032
09:25:01,276 --> 09:25:09,276
So we expect our code to call axios.post
11033
09:25:04,720 --> 09:25:12,160
with /api/cart items. So in here we'll
11034
09:25:09,276 --> 09:25:15,160
check if the first value we gave it was
11035
09:25:12,160 --> 09:25:15,160
slappi/cart-
11036
09:25:15,436 --> 09:25:20,160
items
11037
09:25:17,596 --> 09:25:24,000
and we also expect our code to give
11038
09:25:20,160 --> 09:25:26,160
axios.post an object with a product ID
11039
09:25:24,000 --> 09:25:28,320
and a quantity.
11040
09:25:26,160 --> 09:25:34,720
So in here we can also check if we give
11041
09:25:28,320 --> 09:25:37,120
it an object with a product ID colon and
11042
09:25:34,720 --> 09:25:40,800
we're going to get the product ID of our
11043
09:25:37,120 --> 09:25:45,040
sample product. So here we'll select
11044
09:25:40,800 --> 09:25:47,756
this ID and copy
11045
09:25:45,040 --> 09:25:52,880
and scroll back down
11046
09:25:47,756 --> 09:25:56,400
and create a string and paste the ID
11047
09:25:52,880 --> 09:25:59,040
and we expect the quantity colon to be
11048
09:25:56,400 --> 09:26:01,200
one because we did not change the
11049
09:25:59,040 --> 09:26:03,756
quantity selector.
11050
09:26:01,200 --> 09:26:06,960
So this basically checks that when we
11051
09:26:03,756 --> 09:26:09,436
click add to cart does our code run
11052
09:26:06,960 --> 09:26:12,640
axios.post post and give it these
11053
09:26:09,436 --> 09:26:15,040
values. So even though mocks don't do
11054
09:26:12,640 --> 09:26:17,276
anything, we can still check if we
11055
09:26:15,040 --> 09:26:19,200
called them and we gave them the correct
11056
09:26:17,276 --> 09:26:22,240
values.
11057
09:26:19,200 --> 09:26:26,080
Next, we can also check if our code ran
11058
09:26:22,240 --> 09:26:29,276
load cart. So remember that load cart is
11059
09:26:26,080 --> 09:26:34,880
also a mock. So we can see if our code
11060
09:26:29,276 --> 09:26:36,480
ran load cart. So down here we can also
11061
09:26:34,880 --> 09:26:40,400
expect
11062
09:26:36,480 --> 09:26:45,040
load cart and we don't give load cart
11063
09:26:40,400 --> 09:26:49,680
any values. So we can just use dot2
11064
09:26:45,040 --> 09:26:52,080
have been called and brackets.
11065
09:26:49,680 --> 09:26:54,480
So this just checks if our code called
11066
09:26:52,080 --> 09:26:59,840
load cart and does not check what values
11067
09:26:54,480 --> 09:27:02,880
we gave it. So now let's save
11068
09:26:59,840 --> 09:27:05,436
and v test will run our code and
11069
09:27:02,880 --> 09:27:08,560
everything still passed. So that's how
11070
09:27:05,436 --> 09:27:11,276
we test user interactions. We create
11071
09:27:08,560 --> 09:27:13,916
some mocks and then we check if our code
11072
09:27:11,276 --> 09:27:18,240
ran the mock and gave it the correct
11073
09:27:13,916 --> 09:27:21,360
values. So now let's save this into git.
11074
09:27:18,240 --> 09:27:27,596
In the get section, we'll use a message
11075
09:27:21,360 --> 09:27:31,080
test clicking add to cart button
11076
09:27:27,596 --> 09:27:31,080
and commit.
11077
09:27:32,960 --> 09:27:38,000
Before we move on, we're going to do
11078
09:27:34,720 --> 09:27:41,276
some code cleanup. Notice that in each
11079
09:27:38,000 --> 09:27:44,480
of our tests, we have some setup code at
11080
09:27:41,276 --> 09:27:47,680
the top where we create some sample data
11081
09:27:44,480 --> 09:27:51,276
and mock load cart. So we can actually
11082
09:27:47,680 --> 09:27:53,840
share this setup code between our tests.
11083
09:27:51,276 --> 09:27:57,840
To do that, we can move these two
11084
09:27:53,840 --> 09:28:02,000
variables outside of this it function.
11085
09:27:57,840 --> 09:28:05,680
So we'll select this code
11086
09:28:02,000 --> 09:28:08,560
and cut.
11087
09:28:05,680 --> 09:28:11,560
And outside of this function, we'll
11088
09:28:08,560 --> 09:28:11,560
paste.
11089
09:28:11,680 --> 09:28:17,596
Now that these two variables are outside
11090
09:28:14,080 --> 09:28:20,400
of this function because of scope these
11091
09:28:17,596 --> 09:28:23,200
can be accessed anywhere inside this
11092
09:28:20,400 --> 09:28:26,720
outer describe function. So that means
11093
09:28:23,200 --> 09:28:29,040
it can be accessed in both tests. So in
11094
09:28:26,720 --> 09:28:32,320
the second test
11095
09:28:29,040 --> 09:28:35,436
we can just remove this setup code
11096
09:28:32,320 --> 09:28:38,160
because now it's shared between the two
11097
09:28:35,436 --> 09:28:40,800
tests up here.
11098
09:28:38,160 --> 09:28:43,916
Another recommendation is to recreate
11099
09:28:40,800 --> 09:28:47,276
shared variables before each test
11100
09:28:43,916 --> 09:28:49,520
because if one test modifies a variable,
11101
09:28:47,276 --> 09:28:53,200
that change is going to carry over into
11102
09:28:49,520 --> 09:28:55,916
the next test which can cause bugs.
11103
09:28:53,200 --> 09:28:58,640
So before each test, we're going to
11104
09:28:55,916 --> 09:29:03,276
recreate these variables.
11105
09:28:58,640 --> 09:29:06,720
To do that, we can use before capital E
11106
09:29:03,276 --> 09:29:09,520
each brackets. And we'll give this a
11107
09:29:06,720 --> 09:29:11,276
function brackets arrow and curly
11108
09:29:09,520 --> 09:29:14,640
brackets.
11109
09:29:11,276 --> 09:29:16,640
So before each runs some code before
11110
09:29:14,640 --> 09:29:19,360
each test
11111
09:29:16,640 --> 09:29:21,596
and also we need to import before each.
11112
09:29:19,360 --> 09:29:28,916
So at the top
11113
09:29:21,596 --> 09:29:28,916
we'll import before each from vest.
11114
09:29:29,040 --> 09:29:34,240
So we want to recreate these shared
11115
09:29:31,840 --> 09:29:37,120
variables before each test. So we're
11116
09:29:34,240 --> 09:29:40,720
going to do that in here. Inside we'll
11117
09:29:37,120 --> 09:29:44,080
use product equals
11118
09:29:40,720 --> 09:29:46,640
and let's just select this value and
11119
09:29:44,080 --> 09:29:51,360
cut.
11120
09:29:46,640 --> 09:29:53,360
And in here we'll paste.
11121
09:29:51,360 --> 09:29:55,756
And because we're modifying this
11122
09:29:53,360 --> 09:29:58,000
variable, we need to change from const
11123
09:29:55,756 --> 09:30:00,480
to let.
11124
09:29:58,000 --> 09:30:04,880
And we'll also recreate load cart in
11125
09:30:00,480 --> 09:30:07,756
before each. So here we'll do load cart
11126
09:30:04,880 --> 09:30:10,080
equals vi.fn
11127
09:30:07,756 --> 09:30:13,436
and brackets
11128
09:30:10,080 --> 09:30:17,840
and above we can remove this
11129
09:30:13,436 --> 09:30:21,200
and change this to a let.
11130
09:30:17,840 --> 09:30:24,240
So now before each test we're going to
11131
09:30:21,200 --> 09:30:26,640
recreate the setup code and we'll have a
11132
09:30:24,240 --> 09:30:29,520
fresh set of variables and this will
11133
09:30:26,640 --> 09:30:32,560
make our tests more consistent. So now
11134
09:30:29,520 --> 09:30:35,276
let's save.
11135
09:30:32,560 --> 09:30:37,756
V test will rerun our test and
11136
09:30:35,276 --> 09:30:41,200
everything still passes.
11137
09:30:37,756 --> 09:30:42,960
So before each is known as a test hook.
11138
09:30:41,200 --> 09:30:45,360
There are other test hooks that we can
11139
09:30:42,960 --> 09:30:48,080
use to help us do some setup or some
11140
09:30:45,360 --> 09:30:51,360
cleanup in our tests.
11141
09:30:48,080 --> 09:30:55,040
So now let's save this into git. In the
11142
09:30:51,360 --> 09:31:01,360
get section, we'll use the message use
11143
09:30:55,040 --> 09:31:04,840
before each to remove duplication
11144
09:31:01,360 --> 09:31:04,840
and commit.
11145
09:31:07,916 --> 09:31:13,120
So, we just learned how to create mocks
11146
09:31:10,000 --> 09:31:16,400
that don't do anything. Now, we're going
11147
09:31:13,120 --> 09:31:19,040
to learn how to mock the implementation.
11148
09:31:16,400 --> 09:31:22,240
Mock the implementation means we make
11149
09:31:19,040 --> 09:31:24,080
the mock do whatever we want. To do
11150
09:31:22,240 --> 09:31:27,520
this, we're going to test another
11151
09:31:24,080 --> 09:31:29,596
component. So in our file,
11152
09:31:27,520 --> 09:31:31,756
we're going to go to the homepage
11153
09:31:29,596 --> 09:31:33,596
component.
11154
09:31:31,756 --> 09:31:35,360
And here we're going to create a new
11155
09:31:33,596 --> 09:31:39,640
file
11156
09:31:35,360 --> 09:31:39,640
and name it homepage.est.jsx.
11157
09:31:44,000 --> 09:31:49,756
And let's just copy all the imports from
11158
09:31:46,720 --> 09:31:52,800
the product component test because we'll
11159
09:31:49,756 --> 09:31:55,200
use most of them. So we'll select this
11160
09:31:52,800 --> 09:31:57,436
and copy.
11161
09:31:55,200 --> 09:32:00,240
And in homepage.test
11162
09:31:57,436 --> 09:32:04,160
we're going to paste.
11163
09:32:00,240 --> 09:32:09,200
And this time we'll import homepage
11164
09:32:04,160 --> 09:32:11,840
from dot /home page.
11165
09:32:09,200 --> 09:32:15,360
And as always, we'll start by creating a
11166
09:32:11,840 --> 09:32:18,560
test suite with describe.
11167
09:32:15,360 --> 09:32:20,240
And we'll name this homepage
11168
09:32:18,560 --> 09:32:23,200
component
11169
09:32:20,240 --> 09:32:26,240
and a function.
11170
09:32:23,200 --> 09:32:31,360
And we'll create a test using it
11171
09:32:26,240 --> 09:32:35,040
brackets. And let's call it displays the
11172
09:32:31,360 --> 09:32:39,120
products correctly.
11173
09:32:35,040 --> 09:32:42,960
Brackets, arrow, and curly brackets.
11174
09:32:39,120 --> 09:32:47,756
And now let's render the homepage using
11175
09:32:42,960 --> 09:32:50,720
render bracket less than homepage
11176
09:32:47,756 --> 09:32:54,320
slashgreater than.
11177
09:32:50,720 --> 09:32:58,560
And the homepage requires two props cart
11178
09:32:54,320 --> 09:33:00,800
and load cart. So for the cart equals
11179
09:32:58,560 --> 09:33:03,276
curly brackets. We're not going to test
11180
09:33:00,800 --> 09:33:05,596
the cart right now. So we'll just set it
11181
09:33:03,276 --> 09:33:10,000
as an empty array.
11182
09:33:05,596 --> 09:33:13,200
And for load cart equals curly brackets
11183
09:33:10,000 --> 09:33:15,520
again we'll mock load cart. So above
11184
09:33:13,200 --> 09:33:20,480
this test
11185
09:33:15,520 --> 09:33:24,160
we'll create let node cart and we'll use
11186
09:33:20,480 --> 09:33:27,680
before each brackets
11187
09:33:24,160 --> 09:33:29,276
and a function brackets arrow and curly
11188
09:33:27,680 --> 09:33:33,520
brackets
11189
09:33:29,276 --> 09:33:38,800
and we'll set load cart equals a mock
11190
09:33:33,520 --> 09:33:41,360
function vi.fn Fn and brackets
11191
09:33:38,800 --> 09:33:44,840
and we're going to give this mock to the
11192
09:33:41,360 --> 09:33:44,840
homepage component.
11193
09:33:45,040 --> 09:33:52,960
Next, the homepage component is special.
11194
09:33:49,680 --> 09:33:56,080
If we open the homepage,
11195
09:33:52,960 --> 09:33:58,000
notice at the start, we load products
11196
09:33:56,080 --> 09:34:01,000
from the back end using
11197
09:33:58,000 --> 09:34:01,000
axios.get/appi/roucts.
11198
09:34:01,840 --> 09:34:06,640
Again, in our test, we should not
11199
09:34:03,916 --> 09:34:09,120
contact a real backend. So, we're going
11200
09:34:06,640 --> 09:34:11,276
to mock Axios.
11201
09:34:09,120 --> 09:34:14,880
So, here
11202
09:34:11,276 --> 09:34:16,960
at the top, we'll mock Axios using
11203
09:34:14,880 --> 09:34:20,880
vi.mmock
11204
09:34:16,960 --> 09:34:23,680
brackets and the package axios.
11205
09:34:20,880 --> 09:34:27,436
Now, this just makes axios.get do
11206
09:34:23,680 --> 09:34:30,240
nothing. However, in the homepage, we
11207
09:34:27,436 --> 09:34:32,880
actually need axios.get get to return
11208
09:34:30,240 --> 09:34:35,276
something because we're going to return
11209
09:34:32,880 --> 09:34:38,000
a response and the homepage is going to
11210
09:34:35,276 --> 09:34:42,080
use this response to display the
11211
09:34:38,000 --> 09:34:44,880
products. So here axios.get cannot just
11212
09:34:42,080 --> 09:34:47,200
do nothing. We need to mock the
11213
09:34:44,880 --> 09:34:51,680
implementation which means we're going
11214
09:34:47,200 --> 09:34:56,800
to make axio.get do whatever we want.
11215
09:34:51,680 --> 09:35:00,800
So in our test inside before each
11216
09:34:56,800 --> 09:35:05,436
we're going to do axials.get
11217
09:35:00,800 --> 09:35:07,840
dot mock implementation
11218
09:35:05,436 --> 09:35:09,840
and brackets
11219
09:35:07,840 --> 09:35:13,680
and we're going to give this a function
11220
09:35:09,840 --> 09:35:16,800
brackets arrow and curly brackets.
11221
09:35:13,680 --> 09:35:19,756
So this means that in our code whenever
11222
09:35:16,800 --> 09:35:22,960
we use axios.get get it's going to run
11223
09:35:19,756 --> 09:35:25,916
this fake function instead. So basically
11224
09:35:22,960 --> 09:35:28,720
we can make axios.get do whatever we
11225
09:35:25,916 --> 09:35:30,640
want. That's what mock implementation
11226
09:35:28,720 --> 09:35:34,160
means.
11227
09:35:30,640 --> 09:35:36,960
So remember we can give axios.get some
11228
09:35:34,160 --> 09:35:40,240
values. Those values are going to be
11229
09:35:36,960 --> 09:35:43,120
saved as parameters in here. So the
11230
09:35:40,240 --> 09:35:46,960
first value we give to axios.get is the
11231
09:35:43,120 --> 09:35:51,436
URL path. So in here our fake function
11232
09:35:46,960 --> 09:35:54,800
will also get the URL path.
11233
09:35:51,436 --> 09:36:00,400
Next inside our fake function let's
11234
09:35:54,800 --> 09:36:03,276
check if the URL path is equal to the
11235
09:36:00,400 --> 09:36:06,276
string /
11236
09:36:03,276 --> 09:36:06,276
products.
11237
09:36:06,960 --> 09:36:13,840
So for the URL path /api/proucts,
11238
09:36:11,120 --> 09:36:17,200
usually the backend returns all the
11239
09:36:13,840 --> 09:36:19,360
products in an array. But in our test,
11240
09:36:17,200 --> 09:36:21,916
we don't really need all the products.
11241
09:36:19,360 --> 09:36:27,120
So let's just return two products to
11242
09:36:21,916 --> 09:36:30,560
test with. So in here, we'll just return
11243
09:36:27,120 --> 09:36:33,360
two products. Now, whatever we return
11244
09:36:30,560 --> 09:36:37,120
from this fake function should match
11245
09:36:33,360 --> 09:36:38,800
what axios.get normally returns. So
11246
09:36:37,120 --> 09:36:42,160
axios.get
11247
09:36:38,800 --> 09:36:46,880
normally returns a response with a
11248
09:36:42,160 --> 09:36:49,680
property data. So in our test
11249
09:36:46,880 --> 09:36:56,400
this mock implementation should also
11250
09:36:49,680 --> 09:36:58,320
return an object with a property data.
11251
09:36:56,400 --> 09:37:02,480
And normally
11252
09:36:58,320 --> 09:37:06,240
response data is an array of products.
11253
09:37:02,480 --> 09:37:09,436
So in our test, our data property should
11254
09:37:06,240 --> 09:37:12,320
also be an array.
11255
09:37:09,436 --> 09:37:15,120
So basically in this fake function, we
11256
09:37:12,320 --> 09:37:17,916
should match what axios.get normally
11257
09:37:15,120 --> 09:37:20,320
returns. And this makes sure that the
11258
09:37:17,916 --> 09:37:21,840
rest of the code still works with our
11259
09:37:20,320 --> 09:37:24,240
mock.
11260
09:37:21,840 --> 09:37:26,560
So now in our test,
11261
09:37:24,240 --> 09:37:30,080
let's just return two products to test
11262
09:37:26,560 --> 09:37:33,520
with. to find two sample products. We
11263
09:37:30,080 --> 09:37:37,276
can go back to the starting code folder
11264
09:37:33,520 --> 09:37:39,916
and the data and the products.js.
11265
09:37:37,276 --> 09:37:43,840
And here, let's just collapse the first
11266
09:37:39,916 --> 09:37:45,756
two products and we'll select them and
11267
09:37:43,840 --> 09:37:48,880
copy.
11268
09:37:45,756 --> 09:37:51,916
And inside the homepage test,
11269
09:37:48,880 --> 09:37:55,360
we're going to paste.
11270
09:37:51,916 --> 09:37:58,800
So now in our test, axio.get get will
11271
09:37:55,360 --> 09:38:01,040
return these two products.
11272
09:37:58,800 --> 09:38:04,480
Now there's one more thing we need to do
11273
09:38:01,040 --> 09:38:07,200
which is remember that axios.get
11274
09:38:04,480 --> 09:38:11,436
normally returns a promise that we have
11275
09:38:07,200 --> 09:38:14,800
to await. So in our test we should also
11276
09:38:11,436 --> 09:38:17,596
return a promise. To make this return a
11277
09:38:14,800 --> 09:38:21,680
promise we can wrap this value in
11278
09:38:17,596 --> 09:38:24,960
promise.resolve like this. Or an easier
11279
09:38:21,680 --> 09:38:26,960
way to do this is to make this function
11280
09:38:24,960 --> 09:38:29,680
async.
11281
09:38:26,960 --> 09:38:32,160
So remember that async makes a function
11282
09:38:29,680 --> 09:38:35,200
return a promise.
11283
09:38:32,160 --> 09:38:37,756
Now axios.get will return exactly what
11284
09:38:35,200 --> 09:38:40,400
we want. And that's how we mock the
11285
09:38:37,756 --> 09:38:44,560
implementation which means we make the
11286
09:38:40,400 --> 09:38:46,800
mock do whatever we want.
11287
09:38:44,560 --> 09:38:50,560
Now let's run our tests to make sure
11288
09:38:46,800 --> 09:38:53,916
that this works. We'll save
11289
09:38:50,560 --> 09:38:57,276
and v test will rerun and we actually
11290
09:38:53,916 --> 09:39:01,756
got an error. So if we look at this
11291
09:38:57,276 --> 09:39:04,560
error and we scroll up
11292
09:39:01,756 --> 09:39:07,276
to the details of the error, you'll see
11293
09:39:04,560 --> 09:39:10,960
that we got this error here and it came
11294
09:39:07,276 --> 09:39:14,080
from React Router. And this happens
11295
09:39:10,960 --> 09:39:15,840
because if we look at our homepage
11296
09:39:14,080 --> 09:39:18,640
component,
11297
09:39:15,840 --> 09:39:22,240
remember that the homepage also contains
11298
09:39:18,640 --> 09:39:24,400
a header component. And if we open the
11299
09:39:22,240 --> 09:39:26,400
components folder and the header
11300
09:39:24,400 --> 09:39:29,040
component,
11301
09:39:26,400 --> 09:39:31,596
inside the header component, we have
11302
09:39:29,040 --> 09:39:34,000
these link components.
11303
09:39:31,596 --> 09:39:37,360
In order for a link components to work,
11304
09:39:34,000 --> 09:39:41,520
they need to be inside a router. In
11305
09:39:37,360 --> 09:39:44,000
main.jsx, JSX the entire app is inside a
11306
09:39:41,520 --> 09:39:47,596
router. So that's why link components
11307
09:39:44,000 --> 09:39:50,480
work normally but in our test our
11308
09:39:47,596 --> 09:39:53,120
homepage component is not inside any
11309
09:39:50,480 --> 09:39:56,400
router. So the link component doesn't
11310
09:39:53,120 --> 09:39:59,040
work and it creates an error. To fix
11311
09:39:56,400 --> 09:40:02,320
this we just need to put a router around
11312
09:39:59,040 --> 09:40:05,596
the homepage in our test. So, we'll go
11313
09:40:02,320 --> 09:40:08,560
back to our test
11314
09:40:05,596 --> 09:40:10,960
and first we'll scroll to the top and
11315
09:40:08,560 --> 09:40:14,720
we're going to import a router. So,
11316
09:40:10,960 --> 09:40:19,040
we'll import curly brackets and we're
11317
09:40:14,720 --> 09:40:23,560
going to import the memory router
11318
09:40:19,040 --> 09:40:23,560
from the package react-outer.
11319
09:40:23,680 --> 09:40:28,640
So, the memory router is specifically
11320
09:40:26,400 --> 09:40:31,916
for testing.
11321
09:40:28,640 --> 09:40:34,480
So we'll scroll down
11322
09:40:31,916 --> 09:40:37,360
and when we render the homepage, we're
11323
09:40:34,480 --> 09:40:42,560
going to put it inside a memory router.
11324
09:40:37,360 --> 09:40:46,720
So less than memory router
11325
09:40:42,560 --> 09:40:49,596
and we'll select the homepage and cut.
11326
09:40:46,720 --> 09:40:53,560
And in here we'll paste
11327
09:40:49,596 --> 09:40:53,560
and we'll fix the indenting.
11328
09:40:54,400 --> 09:41:00,640
And now if we save
11329
09:40:57,680 --> 09:41:04,480
the link components are inside a router.
11330
09:41:00,640 --> 09:41:06,800
And now our tests should pass.
11331
09:41:04,480 --> 09:41:09,916
Now let's check if the homepage
11332
09:41:06,800 --> 09:41:13,436
displayed the products correctly. So
11333
09:41:09,916 --> 09:41:16,320
after we render this homepage because we
11334
09:41:13,436 --> 09:41:19,120
mocked Axios to return these two sample
11335
09:41:16,320 --> 09:41:22,960
products, there should be two products
11336
09:41:19,120 --> 09:41:25,040
on the page. Let's add a test ID to
11337
09:41:22,960 --> 09:41:29,596
these elements so we can get them in our
11338
09:41:25,040 --> 09:41:33,120
test. In the product component,
11339
09:41:29,596 --> 09:41:36,000
we're going to scroll up to the top of
11340
09:41:33,120 --> 09:41:38,000
the HTML and we're going to find the
11341
09:41:36,000 --> 09:41:40,240
product container
11342
09:41:38,000 --> 09:41:45,680
and we're going to add the attribute
11343
09:41:40,240 --> 09:41:48,720
data- test id equals a string the
11344
09:41:45,680 --> 09:41:51,596
product dash container
11345
09:41:48,720 --> 09:41:54,400
and save.
11346
09:41:51,596 --> 09:41:57,436
And in our test,
11347
09:41:54,400 --> 09:42:01,040
we're going to look for the test ID
11348
09:41:57,436 --> 09:42:06,560
using screen.get
11349
09:42:01,040 --> 09:42:10,160
all by test ID and brackets and the test
11350
09:42:06,560 --> 09:42:12,880
ID product dash container.
11351
09:42:10,160 --> 09:42:15,360
So we use all here because we're going
11352
09:42:12,880 --> 09:42:17,436
to have multiple products on the page.
11353
09:42:15,360 --> 09:42:19,680
So we're going to get multiple product
11354
09:42:17,436 --> 09:42:21,756
container elements.
11355
09:42:19,680 --> 09:42:23,276
However, there's actually a problem
11356
09:42:21,756 --> 09:42:26,640
here.
11357
09:42:23,276 --> 09:42:29,200
When the homepage is first created, the
11358
09:42:26,640 --> 09:42:32,480
products are empty. So, there will not
11359
09:42:29,200 --> 09:42:34,960
be any products on the page. And we need
11360
09:42:32,480 --> 09:42:37,840
to wait for the homepage to run use
11361
09:42:34,960 --> 09:42:40,720
effect and load the products and then
11362
09:42:37,840 --> 09:42:45,436
update the state. So to wait for
11363
09:42:40,720 --> 09:42:47,436
homepage to finish loading in our test,
11364
09:42:45,436 --> 09:42:49,120
we're going to use a different method
11365
09:42:47,436 --> 09:42:52,640
called
11366
09:42:49,120 --> 09:42:56,160
find all by test ID.
11367
09:42:52,640 --> 09:42:58,880
So find does the same thing as get, but
11368
09:42:56,160 --> 09:43:00,640
it will wait until it finds this
11369
09:42:58,880 --> 09:43:02,960
element.
11370
09:43:00,640 --> 09:43:05,040
So this is useful when our component
11371
09:43:02,960 --> 09:43:08,240
needs to load something and we need to
11372
09:43:05,040 --> 09:43:10,880
wait for it to load. Now, because this
11373
09:43:08,240 --> 09:43:14,080
code is waiting until it finds these
11374
09:43:10,880 --> 09:43:16,880
elements, this code is asynchronous and
11375
09:43:14,080 --> 09:43:19,596
it returns a promise. So, we're going to
11376
09:43:16,880 --> 09:43:23,276
wait for this code to finish using a
11377
09:43:19,596 --> 09:43:27,596
wait. And again, to use a wait, we need
11378
09:43:23,276 --> 09:43:30,560
to be inside an async function.
11379
09:43:27,596 --> 09:43:33,840
So now this will wait until it finds
11380
09:43:30,560 --> 09:43:37,596
these product containers. And then let's
11381
09:43:33,840 --> 09:43:43,360
save these in a variable using const
11382
09:43:37,596 --> 09:43:46,080
product containers equals this result.
11383
09:43:43,360 --> 09:43:49,200
And now we can check this result to see
11384
09:43:46,080 --> 09:43:50,880
if it was displayed correctly. So down
11385
09:43:49,200 --> 09:43:53,200
here
11386
09:43:50,880 --> 09:43:57,360
let's check if we have two product
11387
09:43:53,200 --> 09:44:03,480
containers on the page. So we can expect
11388
09:43:57,360 --> 09:44:03,480
brackets the product containers.length
11389
09:44:04,000 --> 09:44:08,240
dot 2b
11390
09:44:06,320 --> 09:44:11,840
2.
11391
09:44:08,240 --> 09:44:13,916
So this gives us an array of elements
11392
09:44:11,840 --> 09:44:18,240
and we're just checking if the length is
11393
09:44:13,916 --> 09:44:21,360
equal to two. If we save,
11394
09:44:18,240 --> 09:44:24,160
v test will rerun. And now this test
11395
09:44:21,360 --> 09:44:26,400
passed. So we know we're displaying the
11396
09:44:24,160 --> 09:44:28,320
correct number of elements on the
11397
09:44:26,400 --> 09:44:30,960
homepage.
11398
09:44:28,320 --> 09:44:33,040
Next, we'll check inside each product
11399
09:44:30,960 --> 09:44:36,640
container to see if they have the
11400
09:44:33,040 --> 09:44:39,840
correct product name. Now, before we
11401
09:44:36,640 --> 09:44:43,436
used screen get by text to search for
11402
09:44:39,840 --> 09:44:47,120
some text on the page. However, this
11403
09:44:43,436 --> 09:44:49,840
searches the entire page. Here we want
11404
09:44:47,120 --> 09:44:52,320
to check specifically the first product
11405
09:44:49,840 --> 09:44:55,360
container to see if it has the correct
11406
09:44:52,320 --> 09:45:00,160
name. To do that we can use a feature
11407
09:44:55,360 --> 09:45:03,680
called within. So we'll scroll up
11408
09:45:00,160 --> 09:45:05,276
and here we're also going to import
11409
09:45:03,680 --> 09:45:08,480
within
11410
09:45:05,276 --> 09:45:12,080
from testing library/react.
11411
09:45:08,480 --> 09:45:16,000
And we'll scroll back down.
11412
09:45:12,080 --> 09:45:19,520
And within lets us find things within or
11413
09:45:16,000 --> 09:45:21,756
inside a specific element. So to find
11414
09:45:19,520 --> 09:45:25,596
things inside the first product
11415
09:45:21,756 --> 09:45:30,480
container, we're going to use within
11416
09:45:25,596 --> 09:45:32,560
brackets the product containers square
11417
09:45:30,480 --> 09:45:34,160
bracket zero.
11418
09:45:32,560 --> 09:45:35,916
This gives us the first product
11419
09:45:34,160 --> 09:45:39,120
container and we're going to search
11420
09:45:35,916 --> 09:45:43,916
inside this container. And then here
11421
09:45:39,120 --> 09:45:45,436
we're going to do dot get by text and
11422
09:45:43,916 --> 09:45:47,596
brackets.
11423
09:45:45,436 --> 09:45:50,560
And for the first container, we're going
11424
09:45:47,596 --> 09:45:52,960
to search for the first product name. If
11425
09:45:50,560 --> 09:45:55,120
we scroll up,
11426
09:45:52,960 --> 09:45:58,240
this is the first product name. So we'll
11427
09:45:55,120 --> 09:46:00,800
select it and copy.
11428
09:45:58,240 --> 09:46:03,756
And down here,
11429
09:46:00,800 --> 09:46:06,320
we're going to paste.
11430
09:46:03,756 --> 09:46:09,436
So again, this searches inside the first
11431
09:46:06,320 --> 09:46:11,360
product container for this text. And
11432
09:46:09,436 --> 09:46:14,320
I'll add a new line to make it a little
11433
09:46:11,360 --> 09:46:19,200
easier to read. And now we expect this
11434
09:46:14,320 --> 09:46:21,120
to exist. So here we'll type expect open
11435
09:46:19,200 --> 09:46:24,320
bracket
11436
09:46:21,120 --> 09:46:27,436
and just going to reformat this code a
11437
09:46:24,320 --> 09:46:29,276
little bit and at the end close bracket
11438
09:46:27,436 --> 09:46:35,756
and use
11439
09:46:29,276 --> 09:46:38,800
2B in the document and brackets.
11440
09:46:35,756 --> 09:46:40,720
Now let's save
11441
09:46:38,800 --> 09:46:43,040
and v test will tell us that this
11442
09:46:40,720 --> 09:46:45,520
passed. So, we know we're displaying the
11443
09:46:43,040 --> 09:46:48,240
correct product in the first product
11444
09:46:45,520 --> 09:46:50,480
container. Now, let's check the second
11445
09:46:48,240 --> 09:46:53,360
product container. We'll select this
11446
09:46:50,480 --> 09:46:57,120
code and copy.
11447
09:46:53,360 --> 09:47:00,640
And down here, paste.
11448
09:46:57,120 --> 09:47:03,200
And we'll change the zero to a one. This
11449
09:47:00,640 --> 09:47:04,720
gets us the second product container.
11450
09:47:03,200 --> 09:47:08,480
And we're going to get the second
11451
09:47:04,720 --> 09:47:12,640
product name. So up here, we're going to
11452
09:47:08,480 --> 09:47:16,160
select this name and copy
11453
09:47:12,640 --> 09:47:19,276
and scroll back down.
11454
09:47:16,160 --> 09:47:22,916
And here we're going to paste
11455
09:47:19,276 --> 09:47:22,916
and save
11456
09:47:23,840 --> 09:47:28,720
and the test passed. So now we know that
11457
09:47:26,720 --> 09:47:31,916
the second product container is
11458
09:47:28,720 --> 09:47:33,916
displaying the correct product. Now we
11459
09:47:31,916 --> 09:47:36,720
could check all the other details of the
11460
09:47:33,916 --> 09:47:39,276
product as an exercise. However, we
11461
09:47:36,720 --> 09:47:41,680
don't have to do it because remember
11462
09:47:39,276 --> 09:47:44,240
that all the product details have
11463
09:47:41,680 --> 09:47:47,120
already been tested in the product
11464
09:47:44,240 --> 09:47:50,240
component test.
11465
09:47:47,120 --> 09:47:52,640
So instead for the homepage component,
11466
09:47:50,240 --> 09:47:55,200
we focus on testing things that are
11467
09:47:52,640 --> 09:47:57,756
specific to the homepage such as
11468
09:47:55,200 --> 09:48:00,320
rendering multiple products and loading
11469
09:47:57,756 --> 09:48:02,560
products using Axios.
11470
09:48:00,320 --> 09:48:05,680
So that's how we test a more complicated
11471
09:48:02,560 --> 09:48:09,360
component like the homepage. We need to
11472
09:48:05,680 --> 09:48:11,916
mock the implementation of axios.get
11473
09:48:09,360 --> 09:48:15,040
and we may need to wrap the component
11474
09:48:11,916 --> 09:48:18,400
inside a router. I'll leave adding more
11475
09:48:15,040 --> 09:48:21,276
tests for the homepage as an exercise.
11476
09:48:18,400 --> 09:48:24,560
So now let's save to git. We'll open the
11477
09:48:21,276 --> 09:48:31,040
git section and we'll use the message
11478
09:48:24,560 --> 09:48:34,520
create integration test for home page
11479
09:48:31,040 --> 09:48:34,520
and commit.
11480
09:48:36,400 --> 09:48:42,640
So now whenever we make changes to our
11481
09:48:39,200 --> 09:48:45,360
code we can run these automated tests to
11482
09:48:42,640 --> 09:48:47,756
make sure that format money the product
11483
09:48:45,360 --> 09:48:50,560
component and the homepage are still
11484
09:48:47,756 --> 09:48:53,040
correct. Now, usually we create tests
11485
09:48:50,560 --> 09:48:56,000
for all of our components, but I'll
11486
09:48:53,040 --> 09:48:57,840
leave that as exercises.
11487
09:48:56,000 --> 09:49:00,560
In the next lesson, we'll learn
11488
09:48:57,840 --> 09:49:04,000
deploying, which means how to put our
11489
09:49:00,560 --> 09:49:06,240
React app on the internet.
11490
09:49:04,000 --> 09:49:08,320
And that's the end of this lesson. In
11491
09:49:06,240 --> 09:49:11,916
this lesson, we learned how to create
11492
09:49:08,320 --> 09:49:14,960
automated tests with VEST. We learned
11493
09:49:11,916 --> 09:49:18,160
how to create unit tests, which test one
11494
09:49:14,960 --> 09:49:20,960
piece of the code. We learned it,
11495
09:49:18,160 --> 09:49:23,596
expect, and describe, which lets us
11496
09:49:20,960 --> 09:49:26,080
create a test, check the results, and
11497
09:49:23,596 --> 09:49:28,560
group the tests together into a test
11498
09:49:26,080 --> 09:49:31,276
suite. We learned how to create
11499
09:49:28,560 --> 09:49:34,000
integration tests, which test multiple
11500
09:49:31,276 --> 09:49:36,480
pieces of code working together. We
11501
09:49:34,000 --> 09:49:39,276
learned how to test a component using
11502
09:49:36,480 --> 09:49:42,080
render and screen. We learned how to
11503
09:49:39,276 --> 09:49:45,040
create a mock, which is a fake version
11504
09:49:42,080 --> 09:49:47,680
of a function or a package. And we learn
11505
09:49:45,040 --> 09:49:50,400
how to mock the implementation, which
11506
09:49:47,680 --> 09:49:52,960
means we make a mock do whatever we
11507
09:49:50,400 --> 09:49:54,880
want.
11508
09:49:52,960 --> 09:49:59,640
Here are some exercises to help you
11509
09:49:54,880 --> 09:49:59,640
practice what we learned in this lesson.
11510
09:50:35,840 --> 09:50:40,160
In this lesson, we're going to learn
11511
09:50:37,596 --> 09:50:43,276
deploying, which means how to put a
11512
09:50:40,160 --> 09:50:46,320
React app on the internet. So, there are
11513
09:50:43,276 --> 09:50:49,520
many services we can use to deploy. In
11514
09:50:46,320 --> 09:50:53,360
this lesson, we're going to use AWS or
11515
09:50:49,520 --> 09:50:55,276
Amazon Web Services. AWS is the biggest
11516
09:50:53,360 --> 09:50:58,160
service for putting websites on the
11517
09:50:55,276 --> 09:51:00,800
internet. Most big tech companies use
11518
09:50:58,160 --> 09:51:03,436
AWS, so it's the most useful to learn
11519
09:51:00,800 --> 09:51:07,520
for your career.
11520
09:51:03,436 --> 09:51:10,320
So AWS stands for Amazon Web Services.
11521
09:51:07,520 --> 09:51:13,840
It's a collection of services that help
11522
09:51:10,320 --> 09:51:16,480
us put stuff on the internet. And AWS
11523
09:51:13,840 --> 09:51:18,640
has a free tier where we can use many of
11524
09:51:16,480 --> 09:51:20,560
their services for free using some
11525
09:51:18,640 --> 09:51:22,240
credits.
11526
09:51:20,560 --> 09:51:26,560
So, let's click this link in the
11527
09:51:22,240 --> 09:51:30,000
description. Go to lesson 10.
11528
09:51:26,560 --> 09:51:33,040
And here we have a link to AWS.
11529
09:51:30,000 --> 09:51:35,436
Now, before we begin, AWS might change
11530
09:51:33,040 --> 09:51:37,756
their website design in the future. If
11531
09:51:35,436 --> 09:51:42,160
you have any issues, please check this
11532
09:51:37,756 --> 09:51:45,200
AWS troubleshooting guide here.
11533
09:51:42,160 --> 09:51:49,276
Now, let's get started with AWS.
11534
09:51:45,200 --> 09:51:51,916
We'll click this link to open AWS.
11535
09:51:49,276 --> 09:51:54,240
And the free tier is free for new
11536
09:51:51,916 --> 09:51:57,276
accounts. So, we're going to create a
11537
09:51:54,240 --> 09:52:00,960
new AWS account. If you don't see this,
11538
09:51:57,276 --> 09:52:03,360
you can also click sign in
11539
09:52:00,960 --> 09:52:07,320
and then scroll down and click create a
11540
09:52:03,360 --> 09:52:07,320
new AWS account.
11541
09:52:07,596 --> 09:52:12,640
Before we continue, because AWS is the
11542
09:52:10,480 --> 09:52:14,320
biggest web service, it has to follow a
11543
09:52:12,640 --> 09:52:16,800
lot of security and compliance
11544
09:52:14,320 --> 09:52:19,200
regulations. So, even though it has a
11545
09:52:16,800 --> 09:52:21,436
free tier, it will ask you for a credit
11546
09:52:19,200 --> 09:52:23,840
card, billing address, and phone number
11547
09:52:21,436 --> 09:52:25,916
when signing up. If you're unable to
11548
09:52:23,840 --> 09:52:28,320
provide this information, please check
11549
09:52:25,916 --> 09:52:30,320
the AWS troubleshooting guide in the
11550
09:52:28,320 --> 09:52:32,400
description.
11551
09:52:30,320 --> 09:52:35,276
However, I still recommend at least
11552
09:52:32,400 --> 09:52:37,596
watching this tutorial to see how AWS
11553
09:52:35,276 --> 09:52:41,360
works since it's the most useful for
11554
09:52:37,596 --> 09:52:44,160
your career. So, here we'll enter our
11555
09:52:41,360 --> 09:52:48,200
email address for logging in. So, for
11556
09:52:44,160 --> 09:52:48,200
me, I'll use this email
11557
09:52:50,320 --> 09:52:55,800
and then we'll go through the rest of
11558
09:52:52,000 --> 09:52:55,800
the sign up process.
11559
09:53:05,436 --> 09:53:10,240
After signing up, it might take a few
11560
09:53:07,520 --> 09:53:11,840
minutes for the account to be activated.
11561
09:53:10,240 --> 09:53:14,800
So, we just need to give it a bit of
11562
09:53:11,840 --> 09:53:17,520
time.
11563
09:53:14,800 --> 09:53:20,400
Now that we created a new AWS account,
11564
09:53:17,520 --> 09:53:22,240
one thing to note is when signing into
11565
09:53:20,400 --> 09:53:26,240
AWS,
11566
09:53:22,240 --> 09:53:28,240
if you see an account ID and I am here,
11567
09:53:26,240 --> 09:53:31,120
we're going to click this sign in with
11568
09:53:28,240 --> 09:53:33,120
root email button.
11569
09:53:31,120 --> 09:53:37,360
And then here we can sign in with the
11570
09:53:33,120 --> 09:53:39,520
email and password we created earlier.
11571
09:53:37,360 --> 09:53:42,640
After signing in, you can see the
11572
09:53:39,520 --> 09:53:45,916
details of the free tier here or by
11573
09:53:42,640 --> 09:53:45,916
clicking here.
11574
09:53:53,200 --> 09:53:59,520
After we sign in, we're going to land on
11575
09:53:55,436 --> 09:54:02,240
this AWS dashboard. So, as I said, AWS
11576
09:53:59,520 --> 09:54:06,000
is a collection of services that help us
11577
09:54:02,240 --> 09:54:08,000
put stuff on the internet. So, here we
11578
09:54:06,000 --> 09:54:12,080
can see all the services that are part
11579
09:54:08,000 --> 09:54:15,276
of AWS. And there's a lot of services.
11580
09:54:12,080 --> 09:54:17,520
At the top in the search bar, we can
11581
09:54:15,276 --> 09:54:20,400
search for a service. So, we're going to
11582
09:54:17,520 --> 09:54:22,800
start with the most basic service called
11583
09:54:20,400 --> 09:54:27,080
EC2.
11584
09:54:22,800 --> 09:54:27,080
And click here to open it.
11585
09:54:27,756 --> 09:54:34,400
EC2 stands for Elastic Compute Cloud. It
11586
09:54:31,916 --> 09:54:35,916
basically lets us rent a computer from
11587
09:54:34,400 --> 09:54:38,000
AWS.
11588
09:54:35,916 --> 09:54:40,400
Earlier in the course, I mentioned that
11589
09:54:38,000 --> 09:54:43,200
many websites use a backend to manage
11590
09:54:40,400 --> 09:54:46,720
the data and the backend is another
11591
09:54:43,200 --> 09:54:49,520
computer on the internet. Now, we could
11592
09:54:46,720 --> 09:54:52,480
buy a physical computer ourselves and
11593
09:54:49,520 --> 09:54:54,640
use that as the back end, but then we
11594
09:54:52,480 --> 09:54:56,800
have to keep this computer running 24
11595
09:54:54,640 --> 09:55:00,160
hours a day and put it in our office
11596
09:54:56,800 --> 09:55:02,960
somewhere. So this is a lot of work and
11597
09:55:00,160 --> 09:55:07,360
what most companies do is they just rent
11598
09:55:02,960 --> 09:55:09,756
a computer from AWS and AWS will manage
11599
09:55:07,360 --> 09:55:12,240
the computer and keep it running 24
11600
09:55:09,756 --> 09:55:16,560
hours a day and this saves a lot of
11601
09:55:12,240 --> 09:55:19,840
work. So EC2 basically lets us rent a
11602
09:55:16,560 --> 09:55:23,520
computer from AWS. This is called
11603
09:55:19,840 --> 09:55:25,680
infrastructure as a service or IAS. This
11604
09:55:23,520 --> 09:55:30,080
means that AWS manages the
11605
09:55:25,680 --> 09:55:32,800
infrastructure or the computers for us.
11606
09:55:30,080 --> 09:55:36,000
So to rent a computer in here, we can
11607
09:55:32,800 --> 09:55:39,520
click the instances section and then
11608
09:55:36,000 --> 09:55:43,120
click launch instance. By the way, one
11609
09:55:39,520 --> 09:55:45,436
instance is one EC2 computer.
11610
09:55:43,120 --> 09:55:47,520
However, we're not going to use this
11611
09:55:45,436 --> 09:55:51,120
because there's still some problems with
11612
09:55:47,520 --> 09:55:53,520
this process. EC2 just gives us a
11613
09:55:51,120 --> 09:55:56,240
computer. We still need to install
11614
09:55:53,520 --> 09:55:57,916
NodeJS on this computer. We still need
11615
09:55:56,240 --> 09:56:00,640
to connect this computer to the
11616
09:55:57,916 --> 09:56:04,000
internet. And we need to upload our code
11617
09:56:00,640 --> 09:56:06,400
to this computer. So even though AWS
11618
09:56:04,000 --> 09:56:09,360
manages the computer, there's still a
11619
09:56:06,400 --> 09:56:12,560
lot of manual work we have to do. To
11620
09:56:09,360 --> 09:56:14,480
solve this problem, AWS provides another
11621
09:56:12,560 --> 09:56:17,596
service.
11622
09:56:14,480 --> 09:56:19,276
At the top, we're going to search for
11623
09:56:17,596 --> 09:56:21,040
elastic
11624
09:56:19,276 --> 09:56:25,680
beantock
11625
09:56:21,040 --> 09:56:28,160
and click here to open this service.
11626
09:56:25,680 --> 09:56:32,720
You can think of elastic beantock as a
11627
09:56:28,160 --> 09:56:35,680
layer on top of EC2. It uses EC2 to rent
11628
09:56:32,720 --> 09:56:38,480
a computer, but it also installs all the
11629
09:56:35,680 --> 09:56:40,880
software we need on this computer and it
11630
09:56:38,480 --> 09:56:43,916
connects the computer to the internet.
11631
09:56:40,880 --> 09:56:47,436
So, Elastic Beantock also does all the
11632
09:56:43,916 --> 09:56:49,840
software setup for us. All we have to do
11633
09:56:47,436 --> 09:56:53,360
is just upload our code to elastic
11634
09:56:49,840 --> 09:56:56,000
beantock and it will just work. This is
11635
09:56:53,360 --> 09:56:59,360
called a platform as a service or a
11636
09:56:56,000 --> 09:57:02,720
path. This manages the infrastructure
11637
09:56:59,360 --> 09:57:04,800
and the software for us. If you want to
11638
09:57:02,720 --> 09:57:07,120
learn more about these concepts, you can
11639
09:57:04,800 --> 09:57:10,320
check out my backend overview video in
11640
09:57:07,120 --> 09:57:13,200
the description.
11641
09:57:10,320 --> 09:57:15,680
Now that we have an overview of AWS and
11642
09:57:13,200 --> 09:57:18,960
some of the services we can use, let's
11643
09:57:15,680 --> 09:57:21,040
learn how to use Elastic Beantock. Now,
11644
09:57:18,960 --> 09:57:23,840
Amazon might change the design of these
11645
09:57:21,040 --> 09:57:26,240
pages in the future. If you run into any
11646
09:57:23,840 --> 09:57:27,756
issues, make sure to check the AWS
11647
09:57:26,240 --> 09:57:29,840
troubleshooting guide in the
11648
09:57:27,756 --> 09:57:31,840
description.
11649
09:57:29,840 --> 09:57:35,596
We're going to start by creating an
11650
09:57:31,840 --> 09:57:37,200
application. Or we can click here, click
11651
09:57:35,596 --> 09:57:40,800
environments,
11652
09:57:37,200 --> 09:57:43,120
and create environment.
11653
09:57:40,800 --> 09:57:46,160
And on this page, we're going to select
11654
09:57:43,120 --> 09:57:48,960
a web server environment. And we're
11655
09:57:46,160 --> 09:57:52,640
going to give a name to our application.
11656
09:57:48,960 --> 09:57:54,240
So let's just use e-commerce-
11657
09:57:52,640 --> 09:57:56,160
project-
11658
09:57:54,240 --> 09:57:58,560
react.
11659
09:57:56,160 --> 09:58:02,160
And we can use the default values for
11660
09:57:58,560 --> 09:58:04,320
this section. And we'll scroll down. And
11661
09:58:02,160 --> 09:58:06,320
for the platform section, we're going to
11662
09:58:04,320 --> 09:58:11,880
choose the technology we want to use
11663
09:58:06,320 --> 09:58:11,880
here. For us, we're going to use NodeJS.
11664
09:58:12,480 --> 09:58:18,240
And down here, we'll just use the sample
11665
09:58:15,360 --> 09:58:19,756
application. We can upload our own code
11666
09:58:18,240 --> 09:58:22,720
later.
11667
09:58:19,756 --> 09:58:25,916
And we'll scroll down. And here we'll
11668
09:58:22,720 --> 09:58:27,916
choose single instance. If you see free
11669
09:58:25,916 --> 09:58:29,680
tier allegible, pick that one.
11670
09:58:27,916 --> 09:58:31,360
Otherwise, just pick the single
11671
09:58:29,680 --> 09:58:35,720
instance.
11672
09:58:31,360 --> 09:58:35,720
And then here we'll click next.
11673
09:58:35,840 --> 09:58:41,520
And here we need to create a role. A
11674
09:58:38,720 --> 09:58:45,436
role gives elastic beantock permission
11675
09:58:41,520 --> 09:58:48,320
to use other services in AWS.
11676
09:58:45,436 --> 09:58:51,120
So in here, if you already have a role
11677
09:58:48,320 --> 09:58:55,720
for elastic beantock, you can use that.
11678
09:58:51,120 --> 09:58:55,720
Otherwise, we'll click create ro.
11679
09:58:57,200 --> 09:59:02,080
And we should be able to use the default
11680
09:58:59,436 --> 09:59:04,880
values that they set here. And we'll
11681
09:59:02,080 --> 09:59:07,756
click next.
11682
09:59:04,880 --> 09:59:10,240
And then click next.
11683
09:59:07,756 --> 09:59:12,960
And at the bottom, we'll click create
11684
09:59:10,240 --> 09:59:14,800
roll.
11685
09:59:12,960 --> 09:59:17,840
And then we'll go back to Elastic
11686
09:59:14,800 --> 09:59:19,436
Beantock. And we'll click this button to
11687
09:59:17,840 --> 09:59:21,200
refresh.
11688
09:59:19,436 --> 09:59:23,040
And we'll select the RO that we just
11689
09:59:21,200 --> 09:59:24,880
created.
11690
09:59:23,040 --> 09:59:28,320
For this one, we also need to create a
11691
09:59:24,880 --> 09:59:30,240
role. So, we'll click here.
11692
09:59:28,320 --> 09:59:33,756
And then we can also use the default
11693
09:59:30,240 --> 09:59:35,680
values that it gives. Click next
11694
09:59:33,756 --> 09:59:40,596
and next.
11695
09:59:35,680 --> 09:59:40,596
And at the bottom, create RO.
11696
09:59:41,040 --> 09:59:48,560
And we'll go back and refresh
11697
09:59:45,040 --> 09:59:51,276
and select the RO that we just created.
11698
09:59:48,560 --> 09:59:54,160
And we can keep this as blank. And we'll
11699
09:59:51,276 --> 09:59:54,160
click next.
11700
09:59:54,800 --> 09:59:58,560
For the networking and database
11701
09:59:56,560 --> 10:00:01,360
settings, we can just use the default
11702
09:59:58,560 --> 10:00:04,560
values here. So, we'll scroll down and
11703
10:00:01,360 --> 10:00:07,360
click next.
11704
10:00:04,560 --> 10:00:09,436
And for the instance settings, we're
11705
10:00:07,360 --> 10:00:11,916
going to scroll down and find the
11706
10:00:09,436 --> 10:00:13,596
section called capacity.
11707
10:00:11,916 --> 10:00:16,596
And down here, we're going to scroll
11708
10:00:13,596 --> 10:00:16,596
down
11709
10:00:18,720 --> 10:00:23,840
and we're going to find a section like
11710
10:00:20,640 --> 10:00:26,640
this called instance types. And it has a
11711
10:00:23,840 --> 10:00:28,960
drop down here. So, this lets us choose
11712
10:00:26,640 --> 10:00:31,436
the size of the computer that we want to
11713
10:00:28,960 --> 10:00:33,520
rent from EC2.
11714
10:00:31,436 --> 10:00:37,120
So, in the description, we're going to
11715
10:00:33,520 --> 10:00:39,360
click this link and go to lesson 10 and
11716
10:00:37,120 --> 10:00:43,596
then follow the instructions here to
11717
10:00:39,360 --> 10:00:49,756
select the correct instance types.
11718
10:00:43,596 --> 10:00:51,840
So, now we'll scroll down and click next
11719
10:00:49,756 --> 10:00:54,400
and we can leave monitoring and logging
11720
10:00:51,840 --> 10:00:57,640
settings as the default. So, we'll
11721
10:00:54,400 --> 10:00:57,640
scroll down
11722
10:00:58,400 --> 10:01:02,040
and click next.
11723
10:01:02,160 --> 10:01:09,560
And finally, we'll scroll to the bottom
11724
10:01:05,756 --> 10:01:09,560
and click create.
11725
10:01:10,720 --> 10:01:17,680
So now, Elastic Beantock is going to
11726
10:01:13,596 --> 10:01:19,840
rent a computer for us using EC2. It
11727
10:01:17,680 --> 10:01:22,960
will also install all the software that
11728
10:01:19,840 --> 10:01:25,436
we need on this computer like NodeJS and
11729
10:01:22,960 --> 10:01:27,916
npm. And it will do all the
11730
10:01:25,436 --> 10:01:30,240
configuration to connect this computer
11731
10:01:27,916 --> 10:01:31,916
to the internet.
11732
10:01:30,240 --> 10:01:34,000
So, this will take a few minutes to
11733
10:01:31,916 --> 10:01:35,840
complete. So, we need to wait for this
11734
10:01:34,000 --> 10:01:38,400
to finish.
11735
10:01:35,840 --> 10:01:42,840
And you might need to refresh the page
11736
10:01:38,400 --> 10:01:42,840
to check the progress.
11737
10:01:43,120 --> 10:01:47,680
Once that's done, it should look
11738
10:01:45,200 --> 10:01:52,160
something like this. And we can click
11739
10:01:47,680 --> 10:01:55,160
this URL here or click here to open this
11740
10:01:52,160 --> 10:01:55,160
website.
11741
10:01:55,276 --> 10:02:01,120
And now we should see a sample NodeJS
11742
10:01:58,320 --> 10:02:03,520
application on this page. If something
11743
10:02:01,120 --> 10:02:05,840
goes wrong and this doesn't work, please
11744
10:02:03,520 --> 10:02:09,040
check the AWS troubleshooting guide in
11745
10:02:05,840 --> 10:02:11,360
the description.
11746
10:02:09,040 --> 10:02:13,680
Now that we set up Elastic Beantock,
11747
10:02:11,360 --> 10:02:16,560
we're ready to put our code on the
11748
10:02:13,680 --> 10:02:18,320
internet. We'll go back to Elastic
11749
10:02:16,560 --> 10:02:21,840
Beantock.
11750
10:02:18,320 --> 10:02:24,720
And our code has two parts, the back end
11751
10:02:21,840 --> 10:02:27,360
and the front end. We put the back end
11752
10:02:24,720 --> 10:02:30,320
on the internet first and then we put
11753
10:02:27,360 --> 10:02:33,120
the front end. To put the back end on
11754
10:02:30,320 --> 10:02:36,480
the internet, we just need to upload our
11755
10:02:33,120 --> 10:02:40,916
backend code to elastic beantock. So
11756
10:02:36,480 --> 10:02:40,916
we'll click upload and deploy.
11757
10:02:40,960 --> 10:02:46,160
And here we're going to upload our
11758
10:02:43,120 --> 10:02:49,840
backend code. However, we need to upload
11759
10:02:46,160 --> 10:02:52,960
the code as a zip file. So we'll go back
11760
10:02:49,840 --> 10:02:56,880
to VS Code.
11761
10:02:52,960 --> 10:02:59,200
And I'll collapse this for now. And I
11762
10:02:56,880 --> 10:03:02,800
actually set up the e-commerce backend
11763
10:02:59,200 --> 10:03:06,320
so that it works with elastic beantock.
11764
10:03:02,800 --> 10:03:09,680
So this backend has a command that will
11765
10:03:06,320 --> 10:03:12,000
save all the code in a zip file. So
11766
10:03:09,680 --> 10:03:15,680
we're going to go to the command line
11767
10:03:12,000 --> 10:03:19,200
and we're going to create a new tab.
11768
10:03:15,680 --> 10:03:22,800
And in here we're going to cd into
11769
10:03:19,200 --> 10:03:22,800
e-commerce backend.
11770
10:03:23,916 --> 10:03:32,400
and we'll run the command npm run zip.
11771
10:03:29,680 --> 10:03:36,240
So this will save all the backend code
11772
10:03:32,400 --> 10:03:38,800
into a zip file which is right here. If
11773
10:03:36,240 --> 10:03:40,720
I open this zip file,
11774
10:03:38,800 --> 10:03:43,360
you'll see that it just contains all of
11775
10:03:40,720 --> 10:03:46,320
the backend code.
11776
10:03:43,360 --> 10:03:49,520
So now we just need to upload this zip
11777
10:03:46,320 --> 10:03:51,840
file to Elastic Beantock. We'll go back
11778
10:03:49,520 --> 10:03:55,596
to the browser
11779
10:03:51,840 --> 10:03:59,200
and we'll click this to choose a file.
11780
10:03:55,596 --> 10:04:02,080
And for me, my code is on the desktop
11781
10:03:59,200 --> 10:04:04,400
inside the React course folder inside
11782
10:04:02,080 --> 10:04:06,880
e-commerce backend.
11783
10:04:04,400 --> 10:04:09,436
And if I scroll down, I'll see this
11784
10:04:06,880 --> 10:04:14,480
e-commerce backend one, which is a zip
11785
10:04:09,436 --> 10:04:16,880
file. So, I'll click this and open.
11786
10:04:14,480 --> 10:04:19,756
Wait for it to upload and then click
11787
10:04:16,880 --> 10:04:19,756
deploy.
11788
10:04:21,680 --> 10:04:27,916
So now elastic beantock will take the
11789
10:04:24,400 --> 10:04:29,916
backend code and put it on the internet.
11790
10:04:27,916 --> 10:04:33,120
So we'll wait for a few minutes for this
11791
10:04:29,916 --> 10:04:35,436
to complete.
11792
10:04:33,120 --> 10:04:38,320
Once that's done, we're going to click
11793
10:04:35,436 --> 10:04:40,240
this URL again.
11794
10:04:38,320 --> 10:04:43,040
And now instead of the sample
11795
10:04:40,240 --> 10:04:45,520
application, it should say index not
11796
10:04:43,040 --> 10:04:47,596
found, which is normal. This is how I
11797
10:04:45,520 --> 10:04:50,080
designed my back end.
11798
10:04:47,596 --> 10:04:53,120
Now in the URL, we're going to click up
11799
10:04:50,080 --> 10:04:57,680
here and go to the end. We're going to
11800
10:04:53,120 --> 10:05:01,916
enter the URL path slash API slash
11801
10:04:57,680 --> 10:05:04,800
products and press enter.
11802
10:05:01,916 --> 10:05:07,276
So this will give us the products data
11803
10:05:04,800 --> 10:05:10,160
that we were using in our project on our
11804
10:05:07,276 --> 10:05:13,200
computer. So this is the same data that
11805
10:05:10,160 --> 10:05:16,840
we saw before except now our backend is
11806
10:05:13,200 --> 10:05:16,840
on the internet.
11807
10:05:17,360 --> 10:05:21,916
Now that our back end is on the
11808
10:05:19,120 --> 10:05:24,960
internet, let's put our front end on the
11809
10:05:21,916 --> 10:05:28,000
internet as well. For the front end, we
11810
10:05:24,960 --> 10:05:31,120
also need to prepare our code. So, we'll
11811
10:05:28,000 --> 10:05:34,720
go back to VS Code.
11812
10:05:31,120 --> 10:05:37,756
And I'll collapse this folder for now.
11813
10:05:34,720 --> 10:05:42,160
And if we look at the front-end code,
11814
10:05:37,756 --> 10:05:44,400
notice that in here we use JSX code.
11815
10:05:42,160 --> 10:05:47,756
However, remember that the browser does
11816
10:05:44,400 --> 10:05:50,400
not understand JSX. It only understands
11817
10:05:47,756 --> 10:05:54,080
normal JavaScript. So, we need to
11818
10:05:50,400 --> 10:05:57,596
convert this code into normal JavaScript
11819
10:05:54,080 --> 10:06:00,880
first. To do that, VIT has a command for
11820
10:05:57,596 --> 10:06:03,360
this. Inside the command line, we'll
11821
10:06:00,880 --> 10:06:06,720
create a new tab.
11822
10:06:03,360 --> 10:06:08,560
And this time we'll cd into
11823
10:06:06,720 --> 10:06:12,240
ecommerce
11824
10:06:08,560 --> 10:06:16,800
dash project.
11825
10:06:12,240 --> 10:06:18,560
And vit has a command called npm run
11826
10:06:16,800 --> 10:06:22,160
build.
11827
10:06:18,560 --> 10:06:25,960
This is a shortcut for vit build.
11828
10:06:22,160 --> 10:06:25,960
So we'll press enter.
11829
10:06:27,040 --> 10:06:33,596
And this basically converts all of our
11830
10:06:29,596 --> 10:06:36,560
JSX code into normal JavaScript and
11831
10:06:33,596 --> 10:06:40,320
saves the code in a folder called dist.
11832
10:06:36,560 --> 10:06:43,840
So inside here we have an index.html
11833
10:06:40,320 --> 10:06:47,520
file and this is the HTML for our React
11834
10:06:43,840 --> 10:06:51,120
website. And inside the assets folder we
11835
10:06:47,520 --> 10:06:54,320
have this JavaScript file. So this is
11836
10:06:51,120 --> 10:06:57,120
all of our React code but converted into
11837
10:06:54,320 --> 10:06:59,916
normal JavaScript. You'll also notice
11838
10:06:57,120 --> 10:07:02,880
that Vit combined all of our code into
11839
10:06:59,916 --> 10:07:05,360
one file and it also minified or
11840
10:07:02,880 --> 10:07:07,916
compressed the code. So, it shortened
11841
10:07:05,360 --> 10:07:11,040
the variable names and removed the extra
11842
10:07:07,916 --> 10:07:13,916
spaces. This is because when we open a
11843
10:07:11,040 --> 10:07:17,200
website, our browser needs to download
11844
10:07:13,916 --> 10:07:19,520
all the front-end code and run it. By
11845
10:07:17,200 --> 10:07:22,640
combining and compressing the code, it
11846
10:07:19,520 --> 10:07:25,360
makes our website load faster. And Vit
11847
10:07:22,640 --> 10:07:27,276
did the same thing with the CSS.
11848
10:07:25,360 --> 10:07:30,800
You can see that it combined all of our
11849
10:07:27,276 --> 10:07:33,840
CSS files together and compressed it. So
11850
10:07:30,800 --> 10:07:36,960
now our front end is optimized and it's
11851
10:07:33,840 --> 10:07:39,916
ready to be put on the internet.
11852
10:07:36,960 --> 10:07:42,320
So now when we open a website, we need
11853
10:07:39,916 --> 10:07:44,320
to download this React code from
11854
10:07:42,320 --> 10:07:47,200
somewhere. So where do we put this
11855
10:07:44,320 --> 10:07:50,240
front-end code? So we could put this in
11856
10:07:47,200 --> 10:07:52,240
another service like GitHub pages and
11857
10:07:50,240 --> 10:07:54,240
then when we visit the website our
11858
10:07:52,240 --> 10:07:56,640
browser will download this front-end
11859
10:07:54,240 --> 10:07:59,680
code from there.
11860
10:07:56,640 --> 10:08:01,840
However, notice in our current setup
11861
10:07:59,680 --> 10:08:04,160
that we already have this backend
11862
10:08:01,840 --> 10:08:05,680
computer that's running in elastic
11863
10:08:04,160 --> 10:08:07,916
beantock.
11864
10:08:05,680 --> 10:08:11,040
So we can just reuse this backend
11865
10:08:07,916 --> 10:08:13,360
computer. we can put our React code into
11866
10:08:11,040 --> 10:08:15,916
the backend computer and our browser
11867
10:08:13,360 --> 10:08:18,880
will download the React code from the
11868
10:08:15,916 --> 10:08:21,756
backend computer. So, this is a common
11869
10:08:18,880 --> 10:08:24,240
setup and it simplifies our setup
11870
10:08:21,756 --> 10:08:27,680
because now we only need to set up one
11871
10:08:24,240 --> 10:08:30,560
computer. So, to do this step, all we
11872
10:08:27,680 --> 10:08:32,960
need to do is to take this disc folder
11873
10:08:30,560 --> 10:08:36,880
and then drag it into the e-commerce
11874
10:08:32,960 --> 10:08:39,120
back end like this.
11875
10:08:36,880 --> 10:08:41,596
And I added some code in the e-commerce
11876
10:08:39,120 --> 10:08:45,120
back end so that it works with this disc
11877
10:08:41,596 --> 10:08:47,596
folder. So now in the command line,
11878
10:08:45,120 --> 10:08:49,840
we'll go back to the tab that's running
11879
10:08:47,596 --> 10:08:52,880
in the back end and we're going to
11880
10:08:49,840 --> 10:08:56,916
create the zip file again. So we'll do
11881
10:08:52,880 --> 10:08:56,916
npm runzip.
11882
10:08:59,040 --> 10:09:05,596
So this will create another zip file but
11883
10:09:02,320 --> 10:09:08,240
with our front-end code inside.
11884
10:09:05,596 --> 10:09:11,200
And now we just need to upload this to
11885
10:09:08,240 --> 10:09:13,120
Elastic Beantock. We'll go back to the
11886
10:09:11,200 --> 10:09:16,400
browser
11887
10:09:13,120 --> 10:09:19,756
and open Elastic Beantock and we'll
11888
10:09:16,400 --> 10:09:22,720
click upload and deploy.
11889
10:09:19,756 --> 10:09:25,840
And we'll choose a file.
11890
10:09:22,720 --> 10:09:29,240
And this time we'll choose the second
11891
10:09:25,840 --> 10:09:29,240
file here
11892
10:09:29,436 --> 10:09:33,240
and click deploy.
11893
10:09:35,360 --> 10:09:40,400
And we'll wait a few minutes for this to
11894
10:09:37,680 --> 10:09:42,320
complete.
11895
10:09:40,400 --> 10:09:45,520
Once that's finished, we're going to
11896
10:09:42,320 --> 10:09:48,560
click this URL again.
11897
10:09:45,520 --> 10:09:51,360
And now we should have our React app on
11898
10:09:48,560 --> 10:09:53,916
the internet. So, let's try adding some
11899
10:09:51,360 --> 10:09:58,160
stuff to the cart to make sure it works.
11900
10:09:53,916 --> 10:10:00,480
I'll add a toaster and two plates.
11901
10:09:58,160 --> 10:10:03,840
And now, if I look at the cart, it
11902
10:10:00,480 --> 10:10:06,720
updated. If I open it, we have our new
11903
10:10:03,840 --> 10:10:08,960
products in the cart and we can update
11904
10:10:06,720 --> 10:10:12,480
them like usual.
11905
10:10:08,960 --> 10:10:15,200
And if we click create order,
11906
10:10:12,480 --> 10:10:18,160
it works. So everything is working the
11907
10:10:15,200 --> 10:10:21,520
same as on our computer, but this React
11908
10:10:18,160 --> 10:10:23,840
app is now on the internet. If we go
11909
10:10:21,520 --> 10:10:26,000
back to the homepage,
11910
10:10:23,840 --> 10:10:28,800
I'm going to give you a overview of how
11911
10:10:26,000 --> 10:10:31,040
this works. So visually, this is what's
11912
10:10:28,800 --> 10:10:32,960
happening. First we put our back end on
11913
10:10:31,040 --> 10:10:35,360
the internet. So the back end is running
11914
10:10:32,960 --> 10:10:37,200
on a separate computer and then we put
11915
10:10:35,360 --> 10:10:39,840
our front end code in the backend
11916
10:10:37,200 --> 10:10:41,840
computer and I set up some code in the
11917
10:10:39,840 --> 10:10:45,040
back end so that when we visit the
11918
10:10:41,840 --> 10:10:48,000
website using the URL path slash the
11919
10:10:45,040 --> 10:10:50,960
backend sends back the react code to our
11920
10:10:48,000 --> 10:10:53,276
computer. So now we have the react code
11921
10:10:50,960 --> 10:10:55,680
running on our computer and it
11922
10:10:53,276 --> 10:10:58,720
communicates with the backend running on
11923
10:10:55,680 --> 10:11:01,756
another computer. So, this is how a
11924
10:10:58,720 --> 10:11:04,080
realworld React app works. When we're
11925
10:11:01,756 --> 10:11:06,640
developing, the front end and the back
11926
10:11:04,080 --> 10:11:09,040
end run on the same computer because
11927
10:11:06,640 --> 10:11:11,276
it's easier for development.
11928
10:11:09,040 --> 10:11:14,080
But when we put the React app on the
11929
10:11:11,276 --> 10:11:16,720
internet, the front-end code runs on our
11930
10:11:14,080 --> 10:11:20,200
computer and the backend code runs on
11931
10:11:16,720 --> 10:11:20,200
another computer.
11932
10:11:20,720 --> 10:11:26,400
Next, we can add a shortcut to Vit to
11933
10:11:23,756 --> 10:11:29,680
make this process a little easier. We'll
11934
10:11:26,400 --> 10:11:32,560
go back to VS Code
11935
10:11:29,680 --> 10:11:36,000
and we'll go to the e-commerce project
11936
10:11:32,560 --> 10:11:38,880
folder which is here. So remember that
11937
10:11:36,000 --> 10:11:41,276
we created a disc folder and then
11938
10:11:38,880 --> 10:11:45,276
manually dragged it into e-commerce
11939
10:11:41,276 --> 10:11:47,520
backend. We can configure VIT to put the
11940
10:11:45,276 --> 10:11:49,200
disc folder in e-commerce backend
11941
10:11:47,520 --> 10:11:51,756
directly.
11942
10:11:49,200 --> 10:11:55,480
We're going to scroll down
11943
10:11:51,756 --> 10:11:55,480
to vit.config.js.
11944
10:11:56,480 --> 10:12:02,800
And in this configuration at the bottom
11945
10:12:00,320 --> 10:12:07,596
we're going to add a property called
11946
10:12:02,800 --> 10:12:12,080
build colon curly brackets. And in here
11947
10:12:07,596 --> 10:12:14,800
we'll use a property out capital D dur
11948
10:12:12,080 --> 10:12:17,436
colon and a string.
11949
10:12:14,800 --> 10:12:18,960
So this tells vit where to put the dist
11950
10:12:17,436 --> 10:12:20,640
folder.
11951
10:12:18,960 --> 10:12:23,200
So we want to put it inside the
11952
10:12:20,640 --> 10:12:26,320
e-commerce backend folder. So we'll give
11953
10:12:23,200 --> 10:12:30,720
it dot dot slash to go out of e-commerce
11954
10:12:26,320 --> 10:12:32,560
project and then into e-commerce
11955
10:12:30,720 --> 10:12:34,880
dashbackend
11956
10:12:32,560 --> 10:12:37,680
slashdist
11957
10:12:34,880 --> 10:12:40,960
and save.
11958
10:12:37,680 --> 10:12:46,276
So now if we go into the back end and we
11959
10:12:40,960 --> 10:12:46,276
rightclick and delete this disc folder
11960
10:12:46,560 --> 10:12:52,240
and then run npm run build. So in the
11961
10:12:49,360 --> 10:12:54,960
command line we'll open the tab that's
11962
10:12:52,240 --> 10:13:02,596
running in the e-commerce project and
11963
10:12:54,960 --> 10:13:02,596
then run npm run build and press enter.
11964
10:13:02,720 --> 10:13:07,596
It will create the disc folder directly
11965
10:13:04,800 --> 10:13:10,480
in here. So directly in the e-commerce
11966
10:13:07,596 --> 10:13:13,436
back end. So this just saves a step in
11967
10:13:10,480 --> 10:13:15,520
our process. We can also automate this
11968
10:13:13,436 --> 10:13:17,596
process further but I'll leave that as
11969
10:13:15,520 --> 10:13:20,720
an exercise.
11970
10:13:17,596 --> 10:13:24,000
So now we'll save this change into git.
11971
10:13:20,720 --> 10:13:28,400
We'll go to the git section
11972
10:13:24,000 --> 10:13:33,120
and we'll use the message save build
11973
10:13:28,400 --> 10:13:36,596
result in ecommerce dashbackend
11974
10:13:33,120 --> 10:13:36,596
and commit.
11975
10:13:38,800 --> 10:13:44,080
Before we continue, I want to discuss
11976
10:13:41,200 --> 10:13:46,000
how to deploy a react app without a
11977
10:13:44,080 --> 10:13:49,040
backend.
11978
10:13:46,000 --> 10:13:52,800
So notice that so far to put a React app
11979
10:13:49,040 --> 10:13:54,800
on the internet, we needed a backend.
11980
10:13:52,800 --> 10:13:57,756
But what if we don't know how to write
11981
10:13:54,800 --> 10:14:01,596
backend code yet? What if we just know
11982
10:13:57,756 --> 10:14:03,916
React? So if a React app does not have a
11983
10:14:01,596 --> 10:14:07,040
backend, we can actually just put the
11984
10:14:03,916 --> 10:14:10,480
code on the internet directly.
11985
10:14:07,040 --> 10:14:13,276
For example, in our code here, we're
11986
10:14:10,480 --> 10:14:16,080
going to go back to our chatbot project
11987
10:14:13,276 --> 10:14:19,596
from earlier. And notice that this
11988
10:14:16,080 --> 10:14:23,120
project does not use a backend. It only
11989
10:14:19,596 --> 10:14:25,680
has React code. So to deploy this
11990
10:14:23,120 --> 10:14:28,320
project, we're going to use npm run
11991
10:14:25,680 --> 10:14:30,720
build again. So in our command line,
11992
10:14:28,320 --> 10:14:34,320
we'll create a new tab
11993
10:14:30,720 --> 10:14:37,436
and we'll cd into the chatbot-
11994
10:14:34,320 --> 10:14:43,000
project folder.
11995
10:14:37,436 --> 10:14:43,000
And if we need to, we can npm install
11996
10:14:44,080 --> 10:14:49,400
and then npm run build.
11997
10:14:49,680 --> 10:14:54,640
And this will convert all the React code
11998
10:14:51,916 --> 10:14:58,240
into normal JavaScript and save it in a
11999
10:14:54,640 --> 10:15:00,960
disc folder again. So notice that inside
12000
10:14:58,240 --> 10:15:04,880
the disc folder, we actually just have
12001
10:15:00,960 --> 10:15:08,160
normal HTML, CSS, and JavaScript code.
12002
10:15:04,880 --> 10:15:10,720
So this is actually just an HTML website
12003
10:15:08,160 --> 10:15:13,680
and we can actually upload this code to
12004
10:15:10,720 --> 10:15:17,120
any service that puts an HTML website on
12005
10:15:13,680 --> 10:15:19,680
the internet. For example, here I use a
12006
10:15:17,120 --> 10:15:22,160
service called GitHub pages and I can
12007
10:15:19,680 --> 10:15:24,800
just upload this code to GitHub pages
12008
10:15:22,160 --> 10:15:27,520
and it will put it on the internet. No
12009
10:15:24,800 --> 10:15:29,276
backend knowledge required.
12010
10:15:27,520 --> 10:15:31,916
To learn more, you can actually follow
12011
10:15:29,276 --> 10:15:35,596
the same steps as my video, how to put a
12012
10:15:31,916 --> 10:15:37,520
HTML website on the internet. However,
12013
10:15:35,596 --> 10:15:39,916
make sure you do the exercises in this
12014
10:15:37,520 --> 10:15:42,000
lesson first since there's still some
12015
10:15:39,916 --> 10:15:45,436
small configuration changes that we need
12016
10:15:42,000 --> 10:15:48,240
to make for this to work. So, basically,
12017
10:15:45,436 --> 10:15:50,320
if we just have a front end, we can just
12018
10:15:48,240 --> 10:15:52,560
upload this code directly to the
12019
10:15:50,320 --> 10:15:55,916
internet using something like GitHub
12020
10:15:52,560 --> 10:15:58,400
pages. But if our project has a backend
12021
10:15:55,916 --> 10:16:01,520
as well, then we need to use a more
12022
10:15:58,400 --> 10:16:04,080
complicated service like AWS to put the
12023
10:16:01,520 --> 10:16:06,880
back end and the front end on the
12024
10:16:04,080 --> 10:16:09,040
internet.
12025
10:16:06,880 --> 10:16:12,560
In the rest of this lesson, we're going
12026
10:16:09,040 --> 10:16:15,120
to improve our AWS setup to make it more
12027
10:16:12,560 --> 10:16:17,916
real world and professional.
12028
10:16:15,120 --> 10:16:21,916
So, we'll go back to the browser
12029
10:16:17,916 --> 10:16:24,080
and we'll open AWS again. And I'm
12030
10:16:21,916 --> 10:16:27,276
actually going to close some of these
12031
10:16:24,080 --> 10:16:30,160
other tabs because we don't need them.
12032
10:16:27,276 --> 10:16:32,800
And inside Elastic Beantock in this
12033
10:16:30,160 --> 10:16:36,640
project, we're going to go to the
12034
10:16:32,800 --> 10:16:38,640
configuration section.
12035
10:16:36,640 --> 10:16:41,756
And we're going to scroll down and find
12036
10:16:38,640 --> 10:16:43,680
the configuration for instance traffic.
12037
10:16:41,756 --> 10:16:45,840
If you don't see this, you can check the
12038
10:16:43,680 --> 10:16:47,360
AWS troubleshooting guide in the
12039
10:16:45,840 --> 10:16:51,120
description.
12040
10:16:47,360 --> 10:16:54,400
So here we'll click edit.
12041
10:16:51,120 --> 10:16:58,160
and we're going to scroll until we find
12042
10:16:54,400 --> 10:17:00,480
the capacity section. And here we'll
12043
10:16:58,160 --> 10:17:02,240
look for something like environment type
12044
10:17:00,480 --> 10:17:04,720
and a drop down that says single
12045
10:17:02,240 --> 10:17:08,400
instance. So we're going to change this
12046
10:17:04,720 --> 10:17:11,276
to load balanced.
12047
10:17:08,400 --> 10:17:13,680
So what this does is it adds another
12048
10:17:11,276 --> 10:17:15,596
computer in front of our backend
12049
10:17:13,680 --> 10:17:18,720
computer. This is called a load
12050
10:17:15,596 --> 10:17:21,916
balancer. So what a load balancer does
12051
10:17:18,720 --> 10:17:24,400
is if we have a really busy website, one
12052
10:17:21,916 --> 10:17:27,040
computer might not be enough to handle
12053
10:17:24,400 --> 10:17:29,680
all the requests. So we can actually run
12054
10:17:27,040 --> 10:17:32,000
the back end on multiple computers and
12055
10:17:29,680 --> 10:17:34,880
this load balancer will distribute the
12056
10:17:32,000 --> 10:17:37,200
requests across these computers. This
12057
10:17:34,880 --> 10:17:39,916
allows our website to handle much more
12058
10:17:37,200 --> 10:17:43,040
users.
12059
10:17:39,916 --> 10:17:45,276
So here we can decide how many back-end
12060
10:17:43,040 --> 10:17:47,520
computers to run. Since we're just
12061
10:17:45,276 --> 10:17:52,080
testing, we'll set this to a minimum of
12062
10:17:47,520 --> 10:17:56,276
one and a maximum of one.
12063
10:17:52,080 --> 10:17:56,276
And then we'll scroll down
12064
10:17:56,480 --> 10:18:00,520
and click apply.
12065
10:18:02,160 --> 10:18:07,200
And now Elastic Beantock will set up a
12066
10:18:04,720 --> 10:18:09,276
load balancer computer in front of our
12067
10:18:07,200 --> 10:18:12,720
back end. And this will take a few
12068
10:18:09,276 --> 10:18:14,800
minutes to complete.
12069
10:18:12,720 --> 10:18:19,240
After it finishes, we're going to go
12070
10:18:14,800 --> 10:18:19,240
back to the configuration again
12071
10:18:19,276 --> 10:18:24,320
and we'll scroll down to the instance
12072
10:18:21,916 --> 10:18:27,840
traffic settings and just make sure that
12073
10:18:24,320 --> 10:18:31,436
min is one and max is one. If it's not
12074
10:18:27,840 --> 10:18:34,320
one, we can click edit
12075
10:18:31,436 --> 10:18:38,480
and then scroll back down and then
12076
10:18:34,320 --> 10:18:42,480
change it to one. And we'll scroll down
12077
10:18:38,480 --> 10:18:42,480
and click apply again.
12078
10:18:47,040 --> 10:18:52,276
So when that finishes, I'll just check
12079
10:18:49,120 --> 10:18:52,276
it again.
12080
10:18:52,320 --> 10:18:56,756
And now it's one and one.
12081
10:18:57,916 --> 10:19:03,436
So now our website setup looks like
12082
10:19:00,400 --> 10:19:05,756
this. We have three computers, the front
12083
10:19:03,436 --> 10:19:08,800
end, the back end, and the load
12084
10:19:05,756 --> 10:19:12,160
balancer. This is closer to a real world
12085
10:19:08,800 --> 10:19:14,400
setup. Again, you can watch my video on
12086
10:19:12,160 --> 10:19:17,200
backend overview to learn more about
12087
10:19:14,400 --> 10:19:20,160
these concepts.
12088
10:19:17,200 --> 10:19:22,880
Next, currently all the data for our
12089
10:19:20,160 --> 10:19:26,400
React app like the products, the cart,
12090
10:19:22,880 --> 10:19:30,480
and the orders are saved in a database
12091
10:19:26,400 --> 10:19:32,960
and the database is saved in a file.
12092
10:19:30,480 --> 10:19:35,120
However, when we put our React app on
12093
10:19:32,960 --> 10:19:38,480
the internet, we usually put the
12094
10:19:35,120 --> 10:19:41,276
database on a separate computer and this
12095
10:19:38,480 --> 10:19:43,916
lets us share the data between backend
12096
10:19:41,276 --> 10:19:47,520
computers.
12097
10:19:43,916 --> 10:19:50,720
So, inside AWS, we're going to go to the
12098
10:19:47,520 --> 10:19:53,756
configuration again
12099
10:19:50,720 --> 10:19:58,800
and we'll scroll down to the database
12100
10:19:53,756 --> 10:20:01,520
configuration and we'll click edit.
12101
10:19:58,800 --> 10:20:04,320
And inside the database section, we're
12102
10:20:01,520 --> 10:20:07,756
going to scroll down
12103
10:20:04,320 --> 10:20:10,560
and find database and we'll click enable
12104
10:20:07,756 --> 10:20:12,880
database.
12105
10:20:10,560 --> 10:20:15,360
Elastic Beantock will automatically
12106
10:20:12,880 --> 10:20:17,596
create a new computer that's running a
12107
10:20:15,360 --> 10:20:20,960
database and it will connect this
12108
10:20:17,596 --> 10:20:23,040
database to our back end.
12109
10:20:20,960 --> 10:20:26,320
So, we'll scroll down and look at these
12110
10:20:23,040 --> 10:20:28,960
options. And here we can select the
12111
10:20:26,320 --> 10:20:30,960
database that we want to use. So the
12112
10:20:28,960 --> 10:20:33,596
e-commerce back end works with the
12113
10:20:30,960 --> 10:20:35,840
database MySQL. So we're going to use
12114
10:20:33,596 --> 10:20:38,160
that.
12115
10:20:35,840 --> 10:20:41,360
And then here we're going to look at the
12116
10:20:38,160 --> 10:20:43,596
instance class setting. So this is the
12117
10:20:41,360 --> 10:20:45,360
size of the computer running the
12118
10:20:43,596 --> 10:20:47,360
database.
12119
10:20:45,360 --> 10:20:51,756
In the description, we'll click this
12120
10:20:47,360 --> 10:20:53,680
link, go to lesson 10, and then here
12121
10:20:51,756 --> 10:20:55,840
we'll follow the instructions to select
12122
10:20:53,680 --> 10:20:58,840
the correct instance class for the
12123
10:20:55,840 --> 10:20:58,840
database.
12124
10:21:03,596 --> 10:21:08,520
And finally, we'll set a password.
12125
10:21:08,880 --> 10:21:15,000
And now we'll scroll down to the bottom
12126
10:21:11,200 --> 10:21:15,000
and click apply.
12127
10:21:16,320 --> 10:21:22,560
So AWS will set up a separate computer
12128
10:21:19,276 --> 10:21:24,960
to run the database. And I already added
12129
10:21:22,560 --> 10:21:27,596
some code in my e-commerce back end to
12130
10:21:24,960 --> 10:21:30,560
connect to this database. So it should
12131
10:21:27,596 --> 10:21:35,320
just work. If you have issues, please
12132
10:21:30,560 --> 10:21:35,320
check the AWS troubleshooting guide.
12133
10:21:35,916 --> 10:21:40,880
After that finishes, we can open the
12134
10:21:38,320 --> 10:21:43,680
website again
12135
10:21:40,880 --> 10:21:46,480
and it still works. If we add something
12136
10:21:43,680 --> 10:21:49,680
to the cart, notice that the database
12137
10:21:46,480 --> 10:21:51,360
still works. And if we open the orders
12138
10:21:49,680 --> 10:21:53,756
page,
12139
10:21:51,360 --> 10:21:55,436
remember that earlier I created a new
12140
10:21:53,756 --> 10:21:57,200
order.
12141
10:21:55,436 --> 10:21:59,360
And now the orders are back to the
12142
10:21:57,200 --> 10:22:01,520
default values.
12143
10:21:59,360 --> 10:22:05,040
So this means that we're using the new
12144
10:22:01,520 --> 10:22:08,320
MySQL database that we set up. And now
12145
10:22:05,040 --> 10:22:10,880
our AWS setup looks like this. We have a
12146
10:22:08,320 --> 10:22:13,360
load balancer, a backend, and it
12147
10:22:10,880 --> 10:22:15,520
connects to a database on another
12148
10:22:13,360 --> 10:22:20,120
computer. And this is getting even
12149
10:22:15,520 --> 10:22:20,120
closer to a real world setup.
12150
10:22:20,400 --> 10:22:27,520
Next, if we go back to AWS,
12151
10:22:24,800 --> 10:22:31,120
notice that to open our website, we use
12152
10:22:27,520 --> 10:22:32,960
this default URL, which is really long.
12153
10:22:31,120 --> 10:22:35,120
So, now we're going to set up a domain
12154
10:22:32,960 --> 10:22:39,680
name to make our website easier to
12155
10:22:35,120 --> 10:22:43,120
access. A domain name is like myite.com
12156
10:22:39,680 --> 10:22:46,560
or youtube.com. It's a short name that
12157
10:22:43,120 --> 10:22:48,960
makes it easy to access our website.
12158
10:22:46,560 --> 10:22:52,320
So instead of putting this long URL in
12159
10:22:48,960 --> 10:22:55,360
the browser, we can just type myite.com
12160
10:22:52,320 --> 10:22:57,436
and it will open this website. So I'll
12161
10:22:55,360 --> 10:22:59,436
show you how to set up a domain name
12162
10:22:57,436 --> 10:23:02,000
with AWS.
12163
10:22:59,436 --> 10:23:05,276
First, we need to purchase a domain
12164
10:23:02,000 --> 10:23:08,160
name. Most domain names cost around $10
12165
10:23:05,276 --> 10:23:11,040
or $20 per year, so this section is
12166
10:23:08,160 --> 10:23:12,880
optional. If you already have a domain
12167
10:23:11,040 --> 10:23:15,360
name that you purchased before and you
12168
10:23:12,880 --> 10:23:17,916
want to use, feel free to skip to the
12169
10:23:15,360 --> 10:23:20,000
next section. Otherwise, I'm going to
12170
10:23:17,916 --> 10:23:22,800
show you how to purchase your own domain
12171
10:23:20,000 --> 10:23:25,360
name. There are many services we can use
12172
10:23:22,800 --> 10:23:27,520
to purchase domain names. I use a
12173
10:23:25,360 --> 10:23:30,800
service called namecheep for all my
12174
10:23:27,520 --> 10:23:32,880
websites like superimple.dev.
12175
10:23:30,800 --> 10:23:36,320
In the description, we'll click this
12176
10:23:32,880 --> 10:23:38,720
link, go to lesson 10, and click here to
12177
10:23:36,320 --> 10:23:40,960
go to namecheep. So, this is an
12178
10:23:38,720 --> 10:23:43,520
affiliate link. If you decide to
12179
10:23:40,960 --> 10:23:45,360
purchase a domain name using this link,
12180
10:23:43,520 --> 10:23:50,040
part of your purchase will help support
12181
10:23:45,360 --> 10:23:50,040
this channel. Thank you so much.
12182
10:23:50,400 --> 10:23:54,960
Once we're on NameCheep, we're going to
12183
10:23:52,400 --> 10:23:57,040
search for a domain name that we want.
12184
10:23:54,960 --> 10:23:59,596
If you see a search box here, you can
12185
10:23:57,040 --> 10:24:03,360
search for it. Otherwise, we can go to
12186
10:23:59,596 --> 10:24:05,840
domains and domain name search.
12187
10:24:03,360 --> 10:24:08,960
and we'll scroll down here
12188
10:24:05,840 --> 10:24:11,596
and search for a domain name. So, domain
12189
10:24:08,960 --> 10:24:14,400
names are unique. Once they are taken,
12190
10:24:11,596 --> 10:24:17,200
like youtube.com, we can't purchase it
12191
10:24:14,400 --> 10:24:20,320
anymore. If you don't know what domain
12192
10:24:17,200 --> 10:24:22,320
name you want, my recommendation is to
12193
10:24:20,320 --> 10:24:24,480
try to register your first and last
12194
10:24:22,320 --> 10:24:28,960
name. This might be useful for a
12195
10:24:24,480 --> 10:24:32,480
personal website. So, for me, it's Simon
12196
10:24:28,960 --> 10:24:35,040
Bao. and we need to give it an ending
12197
10:24:32,480 --> 10:24:37,840
like
12198
10:24:35,040 --> 10:24:40,560
do stands for commercial. It's the most
12199
10:24:37,840 --> 10:24:42,160
popular ending for domain names. Now,
12200
10:24:40,560 --> 10:24:44,880
you could pick another ending that you
12201
10:24:42,160 --> 10:24:48,080
want, but be careful because some domain
12202
10:24:44,880 --> 10:24:49,680
endings don't work with normal browsers.
12203
10:24:48,080 --> 10:24:52,960
If you're not sure which ending you
12204
10:24:49,680 --> 10:24:54,800
want, I recommend using com or.net just
12205
10:24:52,960 --> 10:24:56,240
to be safe because they are the most
12206
10:24:54,800 --> 10:24:59,680
popular.
12207
10:24:56,240 --> 10:25:03,360
So now I'm going to click search to see
12208
10:24:59,680 --> 10:25:05,200
if this domain has already been taken.
12209
10:25:03,360 --> 10:25:07,436
So it looks like this one has already
12210
10:25:05,200 --> 10:25:12,640
been taken. So I'll just add something
12211
10:25:07,436 --> 10:25:15,360
to it like Simon Balenge or Engineer.
12212
10:25:12,640 --> 10:25:17,276
And this one is available. So you might
12213
10:25:15,360 --> 10:25:19,680
need to modify your domain name to find
12214
10:25:17,276 --> 10:25:24,120
one that works for you.
12215
10:25:19,680 --> 10:25:24,120
So, we'll add this to the cart
12216
10:25:24,240 --> 10:25:28,120
and then open the cart.
12217
10:25:29,520 --> 10:25:34,080
If you want to autorenew this domain
12218
10:25:31,436 --> 10:25:37,596
name, you can click here and then we'll
12219
10:25:34,080 --> 10:25:39,840
click confirm order.
12220
10:25:37,596 --> 10:25:44,276
And if you don't have a name account,
12221
10:25:39,840 --> 10:25:44,276
you need to create an account first.
12222
10:25:47,120 --> 10:25:53,560
And after filling out all the
12223
10:25:48,720 --> 10:25:53,560
information, we'll purchase the domain.
12224
10:25:55,680 --> 10:26:00,400
And that's it. Now we have a domain name
12225
10:25:58,400 --> 10:26:02,800
and we own a little piece of the
12226
10:26:00,400 --> 10:26:04,960
internet.
12227
10:26:02,800 --> 10:26:08,480
Once we have a domain name, we're going
12228
10:26:04,960 --> 10:26:11,840
to set up the domain name in AWS.
12229
10:26:08,480 --> 10:26:14,240
So, we'll go back to AWS
12230
10:26:11,840 --> 10:26:18,320
and at the top we're going to search for
12231
10:26:14,240 --> 10:26:21,436
the service route 53
12232
10:26:18,320 --> 10:26:25,400
and we're going to write click this and
12233
10:26:21,436 --> 10:26:25,400
open in a new tab.
12234
10:26:26,400 --> 10:26:34,320
So, route 53 lets us manage our domain
12235
10:26:29,276 --> 10:26:37,436
names in AWS. We can click get started
12236
10:26:34,320 --> 10:26:40,400
and click create hosted zones
12237
10:26:37,436 --> 10:26:43,276
and then click get started.
12238
10:26:40,400 --> 10:26:45,360
If you don't see that, we can also click
12239
10:26:43,276 --> 10:26:47,916
this menu here,
12240
10:26:45,360 --> 10:26:52,480
click hosted zones
12241
10:26:47,916 --> 10:26:55,360
and then click create hosted zone.
12242
10:26:52,480 --> 10:26:58,560
A hosted zone lets us connect a domain
12243
10:26:55,360 --> 10:27:01,840
name to our app. So currently a hosted
12244
10:26:58,560 --> 10:27:05,276
zone costs about 1.6 6 cents per day. So
12245
10:27:01,840 --> 10:27:07,596
you can use it for free for a few hours.
12246
10:27:05,276 --> 10:27:12,160
So here we're going to enter the domain
12247
10:27:07,596 --> 10:27:15,160
name that we have. So for me it's simon
12248
10:27:12,160 --> 10:27:15,160
balenge.com
12249
10:27:16,000 --> 10:27:21,840
and scroll down and we'll leave it as a
12250
10:27:18,720 --> 10:27:24,400
public and then click create hosted
12251
10:27:21,840 --> 10:27:27,276
zone.
12252
10:27:24,400 --> 10:27:31,596
And now inside here, we're going to
12253
10:27:27,276 --> 10:27:34,160
scroll down and click create record.
12254
10:27:31,596 --> 10:27:36,720
So this sets where the domain name will
12255
10:27:34,160 --> 10:27:40,000
go. We're going to leave the subdomain
12256
10:27:36,720 --> 10:27:43,680
as blank and we'll create an A record
12257
10:27:40,000 --> 10:27:47,200
and we're going to check alias.
12258
10:27:43,680 --> 10:27:49,680
And here we can select which AWS service
12259
10:27:47,200 --> 10:27:55,360
we want to connect to.
12260
10:27:49,680 --> 10:27:57,840
So for us, we're using elastic beantock
12261
10:27:55,360 --> 10:27:59,756
and we're going to choose the region. If
12262
10:27:57,840 --> 10:28:03,200
we go back to the tab that's running
12263
10:27:59,756 --> 10:28:05,680
elastic beantock, we can see the region
12264
10:28:03,200 --> 10:28:07,436
up here. So for me, I'm using the region
12265
10:28:05,680 --> 10:28:10,080
Ohio.
12266
10:28:07,436 --> 10:28:14,800
So go back to route 53 and I'll search
12267
10:28:10,080 --> 10:28:17,596
for the region Ohio and select it. And
12268
10:28:14,800 --> 10:28:20,160
we should see our app here. So I'll
12269
10:28:17,596 --> 10:28:22,960
select that.
12270
10:28:20,160 --> 10:28:25,960
and then scroll down and click create
12271
10:28:22,960 --> 10:28:25,960
records.
12272
10:28:26,000 --> 10:28:30,880
And finally, we need to tell NameCheep
12273
10:28:28,800 --> 10:28:35,680
or the company that you use for your
12274
10:28:30,880 --> 10:28:38,720
domain name to redirect to AWS.
12275
10:28:35,680 --> 10:28:41,120
So inside NameCheep or your domain
12276
10:28:38,720 --> 10:28:44,120
company, we're going to go to the
12277
10:28:41,120 --> 10:28:44,120
settings
12278
10:28:44,160 --> 10:28:50,960
and we're going to find the domain. So
12279
10:28:46,560 --> 10:28:53,200
in namecheep it's in domain list
12280
10:28:50,960 --> 10:28:55,596
and here you may need to verify your
12281
10:28:53,200 --> 10:28:58,400
email. So we'll click this and check
12282
10:28:55,596 --> 10:29:00,160
your email for the verification. So this
12283
10:28:58,400 --> 10:29:04,800
might take a while. So we just have to
12284
10:29:00,160 --> 10:29:07,520
wait until the verification is complete.
12285
10:29:04,800 --> 10:29:09,680
And we may need to log out and log back
12286
10:29:07,520 --> 10:29:13,640
in to our NameCheep account for the
12287
10:29:09,680 --> 10:29:13,640
verification to update.
12288
10:29:14,400 --> 10:29:19,436
Once verification is complete, we should
12289
10:29:16,960 --> 10:29:22,080
be able to go back to the domain list,
12290
10:29:19,436 --> 10:29:25,560
find our domain, and open the settings
12291
10:29:22,080 --> 10:29:25,560
for the domain.
12292
10:29:25,840 --> 10:29:30,640
And in the settings, we're going to look
12293
10:29:28,080 --> 10:29:32,880
for a section that's called name
12294
10:29:30,640 --> 10:29:35,276
servers. If you're having trouble
12295
10:29:32,880 --> 10:29:37,596
finding this in namecheep or your domain
12296
10:29:35,276 --> 10:29:39,040
company, please check the AWS
12297
10:29:37,596 --> 10:29:40,720
troubleshooting guide in the
12298
10:29:39,040 --> 10:29:42,720
description.
12299
10:29:40,720 --> 10:29:44,400
And then in here, we're going to click
12300
10:29:42,720 --> 10:29:47,916
this dropown
12301
10:29:44,400 --> 10:29:51,120
and we're going to set custom DNS
12302
10:29:47,916 --> 10:29:53,520
because we're going to use AWS for this.
12303
10:29:51,120 --> 10:29:56,160
So now we need to enter some name
12304
10:29:53,520 --> 10:30:00,640
servers. To find these, we're going to
12305
10:29:56,160 --> 10:30:03,120
go back to AWS route 53.
12306
10:30:00,640 --> 10:30:07,200
And I need to zoom out a little bit to
12307
10:30:03,120 --> 10:30:09,436
see this. Inside the hosted zone, we
12308
10:30:07,200 --> 10:30:12,080
have some records here. and we're going
12309
10:30:09,436 --> 10:30:13,756
to look for the record NS or name
12310
10:30:12,080 --> 10:30:17,360
server.
12311
10:30:13,756 --> 10:30:20,400
So in the NS record, we should see these
12312
10:30:17,360 --> 10:30:23,120
four URLs here. So we just need to copy
12313
10:30:20,400 --> 10:30:27,840
these over to namecheep or your domain
12314
10:30:23,120 --> 10:30:30,320
company. So we'll select this and copy.
12315
10:30:27,840 --> 10:30:33,680
And then inside NameCheep
12316
10:30:30,320 --> 10:30:37,436
and here we'll paste.
12317
10:30:33,680 --> 10:30:38,880
And we'll go back and select the second
12318
10:30:37,436 --> 10:30:40,800
name server.
12319
10:30:38,880 --> 10:30:42,800
copy
12320
10:30:40,800 --> 10:30:45,596
and paste.
12321
10:30:42,800 --> 10:30:47,520
If we need to, we can add two more name
12322
10:30:45,596 --> 10:30:52,240
servers.
12323
10:30:47,520 --> 10:30:55,680
And here we'll select this and copy.
12324
10:30:52,240 --> 10:31:00,480
And here we'll paste.
12325
10:30:55,680 --> 10:31:06,120
And we'll select the last one. Copy.
12326
10:31:00,480 --> 10:31:06,120
And here paste. And then save.
12327
10:31:07,200 --> 10:31:12,640
So what this does is when we open our
12328
10:31:09,756 --> 10:31:16,480
domain name in the browser, NameCheep is
12329
10:31:12,640 --> 10:31:19,360
going to redirect to AWS and then AWS
12330
10:31:16,480 --> 10:31:21,680
will redirect to Elastic Beantock or to
12331
10:31:19,360 --> 10:31:23,680
our app.
12332
10:31:21,680 --> 10:31:26,480
So this setting will take a while to
12333
10:31:23,680 --> 10:31:29,120
update. Sometimes it finishes in just 10
12334
10:31:26,480 --> 10:31:33,520
minutes, but it could take up to 24 to
12335
10:31:29,120 --> 10:31:36,240
48 hours. So we just have to be patient.
12336
10:31:33,520 --> 10:31:39,276
So once we waited for a while, we can
12337
10:31:36,240 --> 10:31:43,040
try opening a new tab.
12338
10:31:39,276 --> 10:31:46,640
And in here we'll type http
12339
10:31:43,040 --> 10:31:51,596
colon double slash and our domain name.
12340
10:31:46,640 --> 10:31:55,120
So for me it's simon baenge.com
12341
10:31:51,596 --> 10:31:58,080
and press enter.
12342
10:31:55,120 --> 10:32:00,800
And this will redirect to our app. So
12343
10:31:58,080 --> 10:32:03,596
now we're able to access our React app
12344
10:32:00,800 --> 10:32:06,596
using a short domain name that's easy to
12345
10:32:03,596 --> 10:32:06,596
remember.
12346
10:32:06,720 --> 10:32:12,960
Next, I'm going to explain why we enter
12347
10:32:09,436 --> 10:32:16,000
HTTP in front and why we see this not
12348
10:32:12,960 --> 10:32:19,040
secure warning here. So this means that
12349
10:32:16,000 --> 10:32:21,360
our website is not using encryption.
12350
10:32:19,040 --> 10:32:25,200
This means that any data we send to this
12351
10:32:21,360 --> 10:32:28,000
website can be intercepted by a hacker.
12352
10:32:25,200 --> 10:32:31,360
To enable encryption on this website and
12353
10:32:28,000 --> 10:32:33,840
make it secure, we need to set up an SSL
12354
10:32:31,360 --> 10:32:36,560
certificate, which is a special file
12355
10:32:33,840 --> 10:32:39,596
that enables encryption.
12356
10:32:36,560 --> 10:32:41,360
To get an SSL certificate, we can use
12357
10:32:39,596 --> 10:32:45,840
AWS.
12358
10:32:41,360 --> 10:32:48,400
So, we'll go back to AWS Route 53 and
12359
10:32:45,840 --> 10:32:51,916
then at the top we'll search for another
12360
10:32:48,400 --> 10:32:53,756
service called certificate
12361
10:32:51,916 --> 10:32:57,400
manager
12362
10:32:53,756 --> 10:32:57,400
and click here
12363
10:32:58,160 --> 10:33:03,436
and we can request a certificate. If you
12364
10:33:01,120 --> 10:33:07,520
don't see it, you can also go in here
12365
10:33:03,436 --> 10:33:10,000
and click request certificate.
12366
10:33:07,520 --> 10:33:12,320
We'll use the default value and click
12367
10:33:10,000 --> 10:33:14,400
next.
12368
10:33:12,320 --> 10:33:16,880
And here we're going to enter our domain
12369
10:33:14,400 --> 10:33:18,400
name. Make sure to enter your domain
12370
10:33:16,880 --> 10:33:21,680
name.
12371
10:33:18,400 --> 10:33:24,080
And we'll scroll down and make sure DNS
12372
10:33:21,680 --> 10:33:28,880
validation is selected.
12373
10:33:24,080 --> 10:33:30,880
And scroll down and click request.
12374
10:33:28,880 --> 10:33:33,880
And then we'll wait for this to finish
12375
10:33:30,880 --> 10:33:33,880
loading.
12376
10:33:34,720 --> 10:33:40,160
And now AWS needs to confirm that we own
12377
10:33:38,160 --> 10:33:42,320
the domain name. That's why it says
12378
10:33:40,160 --> 10:33:44,560
pending validation.
12379
10:33:42,320 --> 10:33:47,596
To do that, we can just click this
12380
10:33:44,560 --> 10:33:50,000
create records in Route 53 button. If
12381
10:33:47,596 --> 10:33:53,640
you don't see this, please check the AWS
12382
10:33:50,000 --> 10:33:53,640
troubleshooting guide.
12383
10:33:53,680 --> 10:33:59,596
And it should show up here. And we'll
12384
10:33:56,000 --> 10:34:02,240
click create records.
12385
10:33:59,596 --> 10:34:06,080
And we'll wait for this to finish. We
12386
10:34:02,240 --> 10:34:08,960
may need to refresh a few times until it
12387
10:34:06,080 --> 10:34:12,000
says it was successful.
12388
10:34:08,960 --> 10:34:15,520
And now that we have an SSL certificate,
12389
10:34:12,000 --> 10:34:18,880
we can use this in elastic beantock. So
12390
10:34:15,520 --> 10:34:23,800
we'll go back to elastic beantock
12391
10:34:18,880 --> 10:34:23,800
and then in the configurations here,
12392
10:34:23,916 --> 10:34:29,120
we're going to scroll down to the
12393
10:34:26,240 --> 10:34:31,916
instance traffic settings and click
12394
10:34:29,120 --> 10:34:35,880
edit.
12395
10:34:31,916 --> 10:34:35,880
And we're going to scroll down
12396
10:34:38,240 --> 10:34:45,916
until we find this listeners section.
12397
10:34:42,080 --> 10:34:49,840
And here we'll click add listener.
12398
10:34:45,916 --> 10:34:52,240
For the port we're going to use 443.
12399
10:34:49,840 --> 10:34:55,040
And for the protocol we're going to use
12400
10:34:52,240 --> 10:34:57,916
HTTPS.
12401
10:34:55,040 --> 10:35:00,080
HTTPS means we're going to have a secure
12402
10:34:57,916 --> 10:35:03,200
connection to our website. And this
12403
10:35:00,080 --> 10:35:05,756
usually uses port 443.
12404
10:35:03,200 --> 10:35:09,436
And here we're going to choose the SSL
12405
10:35:05,756 --> 10:35:11,840
certificate that we just created.
12406
10:35:09,436 --> 10:35:16,080
And we should be able to leave the SSL
12407
10:35:11,840 --> 10:35:18,160
policy blank and click save.
12408
10:35:16,080 --> 10:35:21,520
If that didn't work, please check the
12409
10:35:18,160 --> 10:35:24,480
AWS troubleshooting guide. So this
12410
10:35:21,520 --> 10:35:27,436
basically adds an SSL certificate to the
12411
10:35:24,480 --> 10:35:30,160
load balancer. And now any data sent to
12412
10:35:27,436 --> 10:35:32,880
our website will be encrypted.
12413
10:35:30,160 --> 10:35:35,880
So, we're going to scroll down and click
12414
10:35:32,880 --> 10:35:35,880
apply.
12415
10:35:38,400 --> 10:35:42,640
And again, this will take a few minutes
12416
10:35:40,560 --> 10:35:45,800
to complete. So, we just have to wait
12417
10:35:42,640 --> 10:35:45,800
for it.
12418
10:35:46,240 --> 10:35:52,400
Once that's complete, I'll close some of
12419
10:35:49,040 --> 10:35:55,756
these other tabs
12420
10:35:52,400 --> 10:35:58,480
and I'll create a new tab.
12421
10:35:55,756 --> 10:36:00,800
And in the URL, we're going to type
12422
10:35:58,480 --> 10:36:03,520
https
12423
10:36:00,800 --> 10:36:05,916
colon double slash. So this tells the
12424
10:36:03,520 --> 10:36:07,680
browser to use the secure connection to
12425
10:36:05,916 --> 10:36:10,080
our website
12426
10:36:07,680 --> 10:36:14,276
and then we'll type our domain name. For
12427
10:36:10,080 --> 10:36:14,276
me, it's simonbowenge.com
12428
10:36:14,320 --> 10:36:19,840
and press enter.
12429
10:36:16,880 --> 10:36:22,000
So now the not secure warning is gone
12430
10:36:19,840 --> 10:36:25,200
and we're using a secure connection to
12431
10:36:22,000 --> 10:36:28,560
our website that uses encryption.
12432
10:36:25,200 --> 10:36:32,720
Also now if we just type our domain name
12433
10:36:28,560 --> 10:36:35,040
in the URL without HTTPS in front and
12434
10:36:32,720 --> 10:36:37,360
press enter
12435
10:36:35,040 --> 10:36:40,480
it will default to using the secure
12436
10:36:37,360 --> 10:36:44,800
version. So that's how we set up an SSL
12437
10:36:40,480 --> 10:36:47,040
certificate and HTTPS in AWS.
12438
10:36:44,800 --> 10:36:51,840
And now we have a professional and
12439
10:36:47,040 --> 10:36:53,840
realworld setup for our React app.
12440
10:36:51,840 --> 10:36:57,276
In this section, I'll show you how to
12441
10:36:53,840 --> 10:36:59,840
delete everything we created in AWS.
12442
10:36:57,276 --> 10:37:01,916
However, I recommend to do the exercises
12443
10:36:59,840 --> 10:37:04,560
of this lesson first to get more
12444
10:37:01,916 --> 10:37:06,560
practice with AWS and then do this
12445
10:37:04,560 --> 10:37:08,640
deletion.
12446
10:37:06,560 --> 10:37:11,120
Also, I try to keep everything we
12447
10:37:08,640 --> 10:37:13,360
created in the free tier. The only
12448
10:37:11,120 --> 10:37:16,160
exception is the hosted zone in Route
12449
10:37:13,360 --> 10:37:18,640
53. You can use it for free for a few
12450
10:37:16,160 --> 10:37:22,560
hours. So you could keep running these
12451
10:37:18,640 --> 10:37:25,520
services and practice AWS if you want to
12452
10:37:22,560 --> 10:37:28,080
check your AWS bill. We can go to the
12453
10:37:25,520 --> 10:37:29,840
profile and click billing and cost
12454
10:37:28,080 --> 10:37:32,320
management.
12455
10:37:29,840 --> 10:37:34,240
And now after you finish the exercises,
12456
10:37:32,320 --> 10:37:38,320
I'll show you how to delete everything
12457
10:37:34,240 --> 10:37:41,596
just in case. So in elastic beantock, we
12458
10:37:38,320 --> 10:37:44,960
can click this actions dropown and click
12459
10:37:41,596 --> 10:37:46,800
terminate environment.
12460
10:37:44,960 --> 10:37:51,320
And in here we need to type the name of
12461
10:37:46,800 --> 10:37:51,320
the environment which is here
12462
10:37:53,120 --> 10:37:56,840
and click terminate.
12463
10:37:59,596 --> 10:38:06,680
Once that's finished, we can go up here
12464
10:38:02,160 --> 10:38:06,680
and go to route 53.
12465
10:38:07,916 --> 10:38:12,400
And in this menu, we can go to hosted
12466
10:38:10,800 --> 10:38:15,840
zones.
12467
10:38:12,400 --> 10:38:18,480
and we'll open the hosted zone.
12468
10:38:15,840 --> 10:38:23,120
And inside here, we're going to select
12469
10:38:18,480 --> 10:38:26,840
the A record and the CNAME record and
12470
10:38:23,120 --> 10:38:26,840
delete these records.
12471
10:38:28,240 --> 10:38:33,840
And then at the top, we can delete the
12472
10:38:31,360 --> 10:38:37,596
hosted zone.
12473
10:38:33,840 --> 10:38:40,240
And in here, we'll type delete and click
12474
10:38:37,596 --> 10:38:42,880
delete.
12475
10:38:40,240 --> 10:38:47,720
Once that's finished, at the top, we'll
12476
10:38:42,880 --> 10:38:47,720
go to certificate manager.
12477
10:38:48,080 --> 10:38:53,040
And in this menu, we're going to click
12478
10:38:50,160 --> 10:38:55,436
list certificates.
12479
10:38:53,040 --> 10:38:58,800
And we'll select the certificate and
12480
10:38:55,436 --> 10:39:04,596
click delete.
12481
10:38:58,800 --> 10:39:04,596
Type delete and click delete.
12482
10:39:05,916 --> 10:39:11,756
You can also delete your entire account
12483
10:39:08,080 --> 10:39:15,320
if you want by clicking your profile and
12484
10:39:11,756 --> 10:39:15,320
clicking account
12485
10:39:16,480 --> 10:39:21,276
and then close account. Although you
12486
10:39:19,200 --> 10:39:23,840
lose access to the free tier if you do
12487
10:39:21,276 --> 10:39:28,480
this. I usually keep my account open for
12488
10:39:23,840 --> 10:39:31,916
a while to practice with the free tier.
12489
10:39:28,480 --> 10:39:34,560
So now we put our React app on the
12490
10:39:31,916 --> 10:39:37,276
internet and we created a professional
12491
10:39:34,560 --> 10:39:40,160
real world setup and that's the end of
12492
10:39:37,276 --> 10:39:44,000
this lesson. In this lesson we learned
12493
10:39:40,160 --> 10:39:46,160
AWS or Amazon Web Services which is a
12494
10:39:44,000 --> 10:39:49,680
collection of services that help us put
12495
10:39:46,160 --> 10:39:52,960
stuff on the internet. We learned EC2
12496
10:39:49,680 --> 10:39:55,276
which lets us rent a computer from AWS
12497
10:39:52,960 --> 10:39:59,120
and is known as infrastructure as a
12498
10:39:55,276 --> 10:40:02,720
service or IAS. We learned Elastic
12499
10:39:59,120 --> 10:40:05,200
Beantock which manages EC2 and sets up
12500
10:40:02,720 --> 10:40:08,400
all the software that we need. This is
12501
10:40:05,200 --> 10:40:11,680
also known as a platform as a service or
12502
10:40:08,400 --> 10:40:14,240
path. We put our backend on the
12503
10:40:11,680 --> 10:40:16,800
internet. We learned how to build and
12504
10:40:14,240 --> 10:40:19,756
optimize the front end and put it on the
12505
10:40:16,800 --> 10:40:22,800
internet. We learned how to deploy React
12506
10:40:19,756 --> 10:40:25,276
without a back end. We added a load
12507
10:40:22,800 --> 10:40:28,160
balancer and database to create a more
12508
10:40:25,276 --> 10:40:31,200
realworld setup. And we learned how to
12509
10:40:28,160 --> 10:40:36,160
set up a domain name, SSL certificate,
12510
10:40:31,200 --> 10:40:38,000
and HTTPS for our website.
12511
10:40:36,160 --> 10:40:42,756
Here are some exercises to help you
12512
10:40:38,000 --> 10:40:42,756
practice what we learned in this lesson.
12513
10:41:11,200 --> 10:41:15,200
In this lesson, we're going to learn
12514
10:41:13,120 --> 10:41:17,596
some of the updates in the new version
12515
10:41:15,200 --> 10:41:20,320
of React, and we'll learn how to use
12516
10:41:17,596 --> 10:41:22,720
TypeScript with React.
12517
10:41:20,320 --> 10:41:25,040
On the React website, there's a page
12518
10:41:22,720 --> 10:41:28,000
that lists the updates in the new
12519
10:41:25,040 --> 10:41:30,640
version of React. You can find a link to
12520
10:41:28,000 --> 10:41:33,520
this page in the description.
12521
10:41:30,640 --> 10:41:35,520
So there's a lot of changes here and
12522
10:41:33,520 --> 10:41:38,000
many of these changes are related to
12523
10:41:35,520 --> 10:41:41,596
using React in a framework which is
12524
10:41:38,000 --> 10:41:43,596
basically a layer on top of React.
12525
10:41:41,596 --> 10:41:48,000
For now I'm just going to go over the
12526
10:41:43,596 --> 10:41:50,400
changes that are related to this course.
12527
10:41:48,000 --> 10:41:53,276
The first change is called support for
12528
10:41:50,400 --> 10:41:55,596
document metadata. So this means that we
12529
10:41:53,276 --> 10:41:58,560
can put elements that normally go inside
12530
10:41:55,596 --> 10:42:01,680
the head section like the title, link
12531
10:41:58,560 --> 10:42:04,800
and meta elements directly inside our
12532
10:42:01,680 --> 10:42:06,880
component like this. So this lets us
12533
10:42:04,800 --> 10:42:09,276
change the head section when we render
12534
10:42:06,880 --> 10:42:12,160
this component. For example, in our
12535
10:42:09,276 --> 10:42:14,320
project, we used a title element to
12536
10:42:12,160 --> 10:42:17,840
change the text in the tab when we
12537
10:42:14,320 --> 10:42:20,800
switch pages. The way this works is when
12538
10:42:17,840 --> 10:42:23,756
this component renders, React will put
12539
10:42:20,800 --> 10:42:26,640
this title inside the head section and
12540
10:42:23,756 --> 10:42:29,596
this will change the text in the tab.
12541
10:42:26,640 --> 10:42:33,200
Before React 19, we had to use an npm
12542
10:42:29,596 --> 10:42:36,000
package like React helmet to do this.
12543
10:42:33,200 --> 10:42:38,320
After React 19, we can just put these
12544
10:42:36,000 --> 10:42:41,276
elements directly in our components. So,
12545
10:42:38,320 --> 10:42:44,640
it's easier.
12546
10:42:41,276 --> 10:42:46,800
Another change is called ref as a prop.
12547
10:42:44,640 --> 10:42:49,680
Earlier in this course, we learned about
12548
10:42:46,800 --> 10:42:53,680
refs and we can use a ref to get access
12549
10:42:49,680 --> 10:42:56,480
to an HTML element directly.
12550
10:42:53,680 --> 10:42:59,040
We can also give a ref to a component.
12551
10:42:56,480 --> 10:43:02,720
For example, if we want to get an HTML
12552
10:42:59,040 --> 10:43:06,720
element inside this component.
12553
10:43:02,720 --> 10:43:08,720
Before React 19, ref was a special prop.
12554
10:43:06,720 --> 10:43:10,880
We can't access it inside this
12555
10:43:08,720 --> 10:43:13,680
component. We had to use a feature
12556
10:43:10,880 --> 10:43:16,960
called forward ref.
12557
10:43:13,680 --> 10:43:19,436
After React 19, ref is now a normal
12558
10:43:16,960 --> 10:43:21,520
prop. We can access it like any other
12559
10:43:19,436 --> 10:43:25,756
prop and we don't need to know about
12560
10:43:21,520 --> 10:43:27,840
forward ref anymore, so it's easier.
12561
10:43:25,756 --> 10:43:31,520
The next change is the introduction of
12562
10:43:27,840 --> 10:43:35,120
the React compiler. The React compiler
12563
10:43:31,520 --> 10:43:37,520
optimizes our React code. For example,
12564
10:43:35,120 --> 10:43:39,596
let's say we had a component like this.
12565
10:43:37,520 --> 10:43:42,400
We have a counter and two other
12566
10:43:39,596 --> 10:43:46,480
components inside. When we click the
12567
10:43:42,400 --> 10:43:49,040
counter, this element will update, but
12568
10:43:46,480 --> 10:43:51,360
it will also recreate these other two
12569
10:43:49,040 --> 10:43:53,680
components. Even though clicking the
12570
10:43:51,360 --> 10:43:55,276
counter does not affect the other two
12571
10:43:53,680 --> 10:43:57,200
components.
12572
10:43:55,276 --> 10:44:00,080
That's because by default, when a
12573
10:43:57,200 --> 10:44:03,916
component updates, it will also update
12574
10:44:00,080 --> 10:44:07,916
or recreate all the components inside.
12575
10:44:03,916 --> 10:44:10,480
And this may cause performance issues.
12576
10:44:07,916 --> 10:44:13,680
To avoid this issue before React
12577
10:44:10,480 --> 10:44:17,276
compiler, we had to use three features.
12578
10:44:13,680 --> 10:44:19,520
Use memo, use callback, and memo. And
12579
10:44:17,276 --> 10:44:22,080
this prevents a component from being
12580
10:44:19,520 --> 10:44:24,000
recreated unless one of the props
12581
10:44:22,080 --> 10:44:25,840
changed.
12582
10:44:24,000 --> 10:44:28,480
So don't worry if you don't understand
12583
10:44:25,840 --> 10:44:30,880
this code because after the React
12584
10:44:28,480 --> 10:44:34,560
compiler, the compiler will just add
12585
10:44:30,880 --> 10:44:36,960
these optimizations automatically.
12586
10:44:34,560 --> 10:44:39,360
And now we can just write our code as
12587
10:44:36,960 --> 10:44:42,800
normal and we don't need to worry about
12588
10:44:39,360 --> 10:44:45,360
use memo, use callback or memo anymore
12589
10:44:42,800 --> 10:44:48,800
in most cases.
12590
10:44:45,360 --> 10:44:52,240
So again, the React compiler optimizes
12591
10:44:48,800 --> 10:44:54,880
our React code. All we have to do is to
12592
10:44:52,240 --> 10:44:57,520
set up the React compiler and we will
12593
10:44:54,880 --> 10:45:01,720
get these optimizations. We don't have
12594
10:44:57,520 --> 10:45:01,720
to change any of our code.
12595
10:45:02,240 --> 10:45:07,120
Now, let's learn how to set up the React
12596
10:45:04,640 --> 10:45:09,596
compiler in our project. In the
12597
10:45:07,120 --> 10:45:12,800
description, we'll click this link, go
12598
10:45:09,596 --> 10:45:14,960
to lesson 11, and here we have the
12599
10:45:12,800 --> 10:45:17,756
instructions for setting up the React
12600
10:45:14,960 --> 10:45:20,080
compiler. I put the setup instructions
12601
10:45:17,756 --> 10:45:22,240
here because this process might change
12602
10:45:20,080 --> 10:45:24,720
in the future, and it's not easy to
12603
10:45:22,240 --> 10:45:27,756
update the video. So, follow the
12604
10:45:24,720 --> 10:45:30,480
instructions that you see on your page.
12605
10:45:27,756 --> 10:45:33,520
For me, the first step is to install the
12606
10:45:30,480 --> 10:45:36,240
React compiler npm package. So, I'm
12607
10:45:33,520 --> 10:45:40,160
going to select this command and copy
12608
10:45:36,240 --> 10:45:42,080
it. And then in my project,
12609
10:45:40,160 --> 10:45:45,120
I'm going to go to the command line and
12610
10:45:42,080 --> 10:45:49,040
create a new tab. And I'll close these
12611
10:45:45,120 --> 10:45:50,880
other tabs so that I can start fresh.
12612
10:45:49,040 --> 10:45:53,680
And first, I need to make sure I'm
12613
10:45:50,880 --> 10:45:57,276
running inside e-commerce-pro.
12614
10:45:53,680 --> 10:45:59,680
So, cd ecommerce-
12615
10:45:57,276 --> 10:46:02,720
project
12616
10:45:59,680 --> 10:46:05,756
and then I'll paste the command
12617
10:46:02,720 --> 10:46:09,120
and press enter to install the react
12618
10:46:05,756 --> 10:46:11,840
compiler npm package.
12619
10:46:09,120 --> 10:46:14,160
And then I'll go back and the next
12620
10:46:11,840 --> 10:46:17,680
instruction is to copy this react
12621
10:46:14,160 --> 10:46:19,756
config. So, I'll select this config and
12622
10:46:17,680 --> 10:46:22,640
copy it.
12623
10:46:19,756 --> 10:46:26,560
And then I need to update vit.config.js.
12624
10:46:22,640 --> 10:46:28,800
js and put that react config in here. So
12625
10:46:26,560 --> 10:46:32,400
I'll go to the project
12626
10:46:28,800 --> 10:46:35,840
and open vit. doconfig.js.
12627
10:46:32,400 --> 10:46:39,120
And at the top I see this react config.
12628
10:46:35,840 --> 10:46:41,120
So I'm just going to replace it and
12629
10:46:39,120 --> 10:46:43,360
paste
12630
10:46:41,120 --> 10:46:45,840
and save.
12631
10:46:43,360 --> 10:46:47,596
And that's the setup. Again, the
12632
10:46:45,840 --> 10:46:49,596
instructions might be different when you
12633
10:46:47,596 --> 10:46:53,120
watch this video. So follow the
12634
10:46:49,596 --> 10:46:55,680
instructions on your page.
12635
10:46:53,120 --> 10:46:58,240
Next, let's check if the React compiler
12636
10:46:55,680 --> 10:47:00,080
is working. So, we'll go back to the
12637
10:46:58,240 --> 10:47:02,800
instructions
12638
10:47:00,080 --> 10:47:05,040
and down here we have some instructions
12639
10:47:02,800 --> 10:47:07,596
for checking if React compiler is
12640
10:47:05,040 --> 10:47:09,596
working. Again, follow the instructions
12641
10:47:07,596 --> 10:47:12,400
on your page because they might have
12642
10:47:09,596 --> 10:47:16,000
changed. So, first I'll start the back
12643
10:47:12,400 --> 10:47:18,720
end and the front end using mpm rundev.
12644
10:47:16,000 --> 10:47:21,200
I'll go back to VS Code
12645
10:47:18,720 --> 10:47:23,360
and in the command line I'll create a
12646
10:47:21,200 --> 10:47:25,200
new tab
12647
10:47:23,360 --> 10:47:30,680
and I'm going to go into the backend
12648
10:47:25,200 --> 10:47:30,680
folder using cd ecommerce-backend
12649
10:47:31,040 --> 10:47:35,880
and run mpm rundev.
12650
10:47:36,320 --> 10:47:44,840
And I'll go to the front end tab
12651
10:47:39,436 --> 10:47:44,840
and I'll also run mpm rundev.
12652
10:47:45,200 --> 10:47:50,800
So now both of them are running. I'll go
12653
10:47:48,320 --> 10:47:53,200
back to the instructions
12654
10:47:50,800 --> 10:47:56,480
and the next step is to install the
12655
10:47:53,200 --> 10:47:58,480
React DevTools Chrome extension. So
12656
10:47:56,480 --> 10:48:00,240
we'll click here.
12657
10:47:58,480 --> 10:48:02,880
So this is an extension made
12658
10:48:00,240 --> 10:48:04,880
specifically for React. It has a bunch
12659
10:48:02,880 --> 10:48:06,480
of features that help us with React
12660
10:48:04,880 --> 10:48:08,400
development.
12661
10:48:06,480 --> 10:48:12,916
So here we're going to add this
12662
10:48:08,400 --> 10:48:12,916
extension to our browser.
12663
10:48:13,680 --> 10:48:19,200
And once that's finished, I'll go back
12664
10:48:15,916 --> 10:48:21,756
to the instructions.
12665
10:48:19,200 --> 10:48:23,840
And the next step is in your project,
12666
10:48:21,756 --> 10:48:28,000
open the console and open the components
12667
10:48:23,840 --> 10:48:31,960
tab. So, in VS Code, we're going to open
12668
10:48:28,000 --> 10:48:31,960
this project in the browser
12669
10:48:33,040 --> 10:48:38,560
and I'm going to rightclick
12670
10:48:35,436 --> 10:48:40,720
and inspect.
12671
10:48:38,560 --> 10:48:43,200
And in the console, we're going to click
12672
10:48:40,720 --> 10:48:46,320
here. And we should have a new
12673
10:48:43,200 --> 10:48:48,640
components tab.
12674
10:48:46,320 --> 10:48:53,080
So, we'll click this.
12675
10:48:48,640 --> 10:48:53,080
If it gets stuck, you can refresh.
12676
10:48:53,916 --> 10:48:58,800
And this shows us all the React
12677
10:48:55,840 --> 10:49:01,596
components that are on our page. If we
12678
10:48:58,800 --> 10:49:03,680
scroll down and if we look at our
12679
10:49:01,596 --> 10:49:06,640
components, they should have a badge
12680
10:49:03,680 --> 10:49:09,360
beside them called memo. This means that
12681
10:49:06,640 --> 10:49:13,040
the React compiler is working and it's
12682
10:49:09,360 --> 10:49:15,120
optimizing our React components.
12683
10:49:13,040 --> 10:49:19,120
Now, let's go back and look at some more
12684
10:49:15,120 --> 10:49:22,000
changes in the new version of React.
12685
10:49:19,120 --> 10:49:25,596
The next change is removed prop types
12686
10:49:22,000 --> 10:49:28,400
and default props. So prop types were an
12687
10:49:25,596 --> 10:49:32,480
older feature of React. It basically
12688
10:49:28,400 --> 10:49:35,276
lets us check the type of a prop.
12689
10:49:32,480 --> 10:49:38,960
For example, here we can check if the
12690
10:49:35,276 --> 10:49:41,916
prop text is a string. If we run this
12691
10:49:38,960 --> 10:49:45,200
app and text is not a string, it would
12692
10:49:41,916 --> 10:49:47,596
show a warning in the console.
12693
10:49:45,200 --> 10:49:50,160
So now we use a different tool called
12694
10:49:47,596 --> 10:49:53,360
TypeScript which is better at this type
12695
10:49:50,160 --> 10:49:55,276
of type checking. So React removed the
12696
10:49:53,360 --> 10:49:57,276
prop types feature in favor of
12697
10:49:55,276 --> 10:50:00,720
TypeScript.
12698
10:49:57,276 --> 10:50:03,596
React also removed default props. If a
12699
10:50:00,720 --> 10:50:07,276
prop is undefined, this feature lets us
12700
10:50:03,596 --> 10:50:09,916
set a default value. So now we can do
12701
10:50:07,276 --> 10:50:14,400
this with normal JavaScript. So we don't
12702
10:50:09,916 --> 10:50:16,560
need this React feature anymore.
12703
10:50:14,400 --> 10:50:19,276
Now that we learned that propt types was
12704
10:50:16,560 --> 10:50:22,720
replaced by TypeScript, let's learn how
12705
10:50:19,276 --> 10:50:25,916
to set up TypeScript with React, we'll
12706
10:50:22,720 --> 10:50:28,960
go back to VS Code.
12707
10:50:25,916 --> 10:50:32,480
And to set up TypeScript with React, we
12708
10:50:28,960 --> 10:50:36,640
can use the create vit command. So in
12709
10:50:32,480 --> 10:50:38,480
the command line, we'll create a new tab
12710
10:50:36,640 --> 10:50:41,360
and make sure this is running in the
12711
10:50:38,480 --> 10:50:44,320
React course folder. And we're going to
12712
10:50:41,360 --> 10:50:48,400
run the command npx
12713
10:50:44,320 --> 10:50:51,120
create-ash vit. And we'll type at to
12714
10:50:48,400 --> 10:50:53,276
give it a specific version. And we'll
12715
10:50:51,120 --> 10:50:55,520
click this link in the description. Go
12716
10:50:53,276 --> 10:50:58,160
to lesson 11.
12717
10:50:55,520 --> 10:51:00,000
And we'll use this version of vit. So
12718
10:50:58,160 --> 10:51:01,520
copy the version that you see on your
12719
10:51:00,000 --> 10:51:04,560
page.
12720
10:51:01,520 --> 10:51:08,160
And here we'll paste
12721
10:51:04,560 --> 10:51:10,960
and press enter.
12722
10:51:08,160 --> 10:51:14,720
If it asks to install the package, press
12723
10:51:10,960 --> 10:51:17,520
Y and press enter.
12724
10:51:14,720 --> 10:51:20,800
And as a review, create VIT creates a
12725
10:51:17,520 --> 10:51:23,680
new project. So let's name this project
12726
10:51:20,800 --> 10:51:26,240
ecommerce-
12727
10:51:23,680 --> 10:51:30,400
project-ts
12728
10:51:26,240 --> 10:51:32,480
or TypeScript and press enter.
12729
10:51:30,400 --> 10:51:34,880
For the framework,
12730
10:51:32,480 --> 10:51:37,276
we're going to press down and choose
12731
10:51:34,880 --> 10:51:39,436
React.
12732
10:51:37,276 --> 10:51:42,840
And for the variant, we're going to
12733
10:51:39,436 --> 10:51:42,840
choose TypeScript.
12734
10:51:43,436 --> 10:51:48,960
So this will create a new React project
12735
10:51:46,640 --> 10:51:52,080
that uses TypeScript.
12736
10:51:48,960 --> 10:51:55,360
If we look at the code inside source,
12737
10:51:52,080 --> 10:51:56,960
notice that it uses tsx files instead of
12738
10:51:55,360 --> 10:52:00,160
JSX.
12739
10:51:56,960 --> 10:52:04,080
TSX means this code uses TypeScript
12740
10:52:00,160 --> 10:52:06,320
while JSX means it uses JavaScript.
12741
10:52:04,080 --> 10:52:08,800
So now to run this project, we're going
12742
10:52:06,320 --> 10:52:13,436
to follow the instructions that we see.
12743
10:52:08,800 --> 10:52:16,916
So for me, I'll cd into e-commerce
12744
10:52:13,436 --> 10:52:16,916
dash project-ts
12745
10:52:17,520 --> 10:52:23,756
and run npm install
12746
10:52:21,680 --> 10:52:26,080
to install all the packages that we
12747
10:52:23,756 --> 10:52:27,840
need.
12748
10:52:26,080 --> 10:52:30,320
And then I'm going to open the tab
12749
10:52:27,840 --> 10:52:34,080
that's running the front end. And I'll
12750
10:52:30,320 --> 10:52:36,640
press Ctrl C to stop the front end so we
12751
10:52:34,080 --> 10:52:40,160
don't have any issues. And then in the
12752
10:52:36,640 --> 10:52:45,360
TypeScript tab, we're going to run mpm
12753
10:52:40,160 --> 10:52:48,160
rundev to start this project.
12754
10:52:45,360 --> 10:52:50,560
And now we'll open this in the browser.
12755
10:52:48,160 --> 10:52:53,596
So I already have it open. So I'm going
12756
10:52:50,560 --> 10:52:56,080
to go in here and open this tab and
12757
10:52:53,596 --> 10:52:59,200
refresh.
12758
10:52:56,080 --> 10:53:01,756
Now we have a new React project. But
12759
10:52:59,200 --> 10:53:04,640
this project uses TypeScript instead of
12760
10:53:01,756 --> 10:53:06,960
JavaScript.
12761
10:53:04,640 --> 10:53:08,480
And now I'll give you an intro to
12762
10:53:06,960 --> 10:53:11,436
TypeScript.
12763
10:53:08,480 --> 10:53:14,560
Let's go back to our code.
12764
10:53:11,436 --> 10:53:18,360
And inside the TypeScript project, we're
12765
10:53:14,560 --> 10:53:18,360
going to open app.tsx.
12766
10:53:18,800 --> 10:53:24,720
So first of all, what is Typescript?
12767
10:53:21,840 --> 10:53:27,120
Typescript is just JavaScript with extra
12768
10:53:24,720 --> 10:53:30,080
features.
12769
10:53:27,120 --> 10:53:33,436
So notice that this TypeScript code is
12770
10:53:30,080 --> 10:53:36,320
the same as JavaScript code. The syntax
12771
10:53:33,436 --> 10:53:40,400
is the same and the features are the
12772
10:53:36,320 --> 10:53:43,040
same. For example, in here
12773
10:53:40,400 --> 10:53:47,040
in Typescript, we can create a variable
12774
10:53:43,040 --> 10:53:51,360
the same way as JavaScript using const
12775
10:53:47,040 --> 10:53:56,800
message equals the string hello.
12776
10:53:51,360 --> 10:53:59,276
And then we can console.log log message.
12777
10:53:56,800 --> 10:54:00,800
So as you can see, TypeScript is just
12778
10:53:59,276 --> 10:54:03,360
JavaScript.
12779
10:54:00,800 --> 10:54:06,800
However, TypeScript gives us some extra
12780
10:54:03,360 --> 10:54:09,596
features on top of JavaScript. The main
12781
10:54:06,800 --> 10:54:11,360
feature is we can add types to our
12782
10:54:09,596 --> 10:54:13,916
variables.
12783
10:54:11,360 --> 10:54:16,880
So what are types? Remember that
12784
10:54:13,916 --> 10:54:20,320
JavaScript has different types of values
12785
10:54:16,880 --> 10:54:23,360
like numbers, strings, and objects.
12786
10:54:20,320 --> 10:54:27,120
In Typescript, we can set what type of
12787
10:54:23,360 --> 10:54:30,240
value is inside a variable.
12788
10:54:27,120 --> 10:54:34,160
For example, in this variable, we have a
12789
10:54:30,240 --> 10:54:39,520
string. So, after the variable name, we
12790
10:54:34,160 --> 10:54:41,916
can type colon space and string.
12791
10:54:39,520 --> 10:54:45,200
So, this tells TypeScript that this
12792
10:54:41,916 --> 10:54:50,000
variable contains a string.
12793
10:54:45,200 --> 10:54:54,080
So now below this if I type message and
12794
10:54:50,000 --> 10:54:57,116
then dot TypeScript knows that this is a
12795
10:54:54,080 --> 10:55:01,360
string. So now it's able to suggest all
12796
10:54:57,116 --> 10:55:03,756
the features of a string like two
12797
10:55:01,360 --> 10:55:06,480
lowercase
12798
10:55:03,756 --> 10:55:08,720
and brackets.
12799
10:55:06,480 --> 10:55:12,160
So using TypeScript our computer can
12800
10:55:08,720 --> 10:55:16,320
give us better autocomplete suggestions.
12801
10:55:12,160 --> 10:55:20,880
Next, if I type message
12802
10:55:16,320 --> 10:55:24,480
to fixed and brackets,
12803
10:55:20,880 --> 10:55:26,720
two fixed only works with numbers.
12804
10:55:24,480 --> 10:55:29,276
Because TypeScript knows that this is a
12805
10:55:26,720 --> 10:55:31,840
string, it's going to give us an error
12806
10:55:29,276 --> 10:55:34,160
saying that too does not work on
12807
10:55:31,840 --> 10:55:36,320
strings.
12808
10:55:34,160 --> 10:55:40,080
So, this feature is called type
12809
10:55:36,320 --> 10:55:43,116
checking. TypeScript checks if this code
12810
10:55:40,080 --> 10:55:46,640
works with this type of value and this
12811
10:55:43,116 --> 10:55:49,040
helps us avoid errors. In JavaScript,
12812
10:55:46,640 --> 10:55:51,200
there is no type checking. So if I want
12813
10:55:49,040 --> 10:55:53,520
to put this code on my website,
12814
10:55:51,200 --> 10:55:56,240
JavaScript will just let me do it and it
12815
10:55:53,520 --> 10:55:58,480
will crash my website. But in
12816
10:55:56,240 --> 10:56:02,320
Typescript, if I want to put this code
12817
10:55:58,480 --> 10:56:04,480
on my website, I would save this code
12818
10:56:02,320 --> 10:56:08,560
and then in my command line, press
12819
10:56:04,480 --> 10:56:12,116
control C to stop the server and run npm
12820
10:56:08,560 --> 10:56:12,116
run build.
12821
10:56:12,720 --> 10:56:17,116
Notice that TypeScript actually blocks
12822
10:56:14,960 --> 10:56:19,680
us from building this code and putting
12823
10:56:17,116 --> 10:56:21,916
it on our website because there's an
12824
10:56:19,680 --> 10:56:24,720
error with the types.
12825
10:56:21,916 --> 10:56:27,116
So, as you can see, TypeScript gives us
12826
10:56:24,720 --> 10:56:29,200
type checking, which helps us avoid a
12827
10:56:27,116 --> 10:56:32,800
lot of these coding errors and helps us
12828
10:56:29,200 --> 10:56:35,360
keep our code safer.
12829
10:56:32,800 --> 10:56:38,240
Another feature of TypeScript is type
12830
10:56:35,360 --> 10:56:40,320
inference. Type inference means
12831
10:56:38,240 --> 10:56:42,080
TypeScript can figure out the type
12832
10:56:40,320 --> 10:56:46,800
automatically.
12833
10:56:42,080 --> 10:56:49,596
For example, in this code, this variable
12834
10:56:46,800 --> 10:56:51,596
obviously contains a string. So we
12835
10:56:49,596 --> 10:56:56,080
actually don't need to set the type
12836
10:56:51,596 --> 10:56:59,040
manually here. If we delete this type,
12837
10:56:56,080 --> 10:57:02,560
TypeScript will infer or figure out that
12838
10:56:59,040 --> 10:57:04,320
this is a string. So I can remove it and
12839
10:57:02,560 --> 10:57:06,720
save
12840
10:57:04,320 --> 10:57:09,840
and the error check still works because
12841
10:57:06,720 --> 10:57:12,640
TypeScript knows that this is a string.
12842
10:57:09,840 --> 10:57:16,000
Another example of type inference is up
12843
10:57:12,640 --> 10:57:19,040
here in use state. So this count
12844
10:57:16,000 --> 10:57:22,080
variable contains the number zero.
12845
10:57:19,040 --> 10:57:25,200
TypeScript will infer or figure out that
12846
10:57:22,080 --> 10:57:30,320
count is a number.
12847
10:57:25,200 --> 10:57:33,200
So if we do count to two lowercase and
12848
10:57:30,320 --> 10:57:35,680
brackets, it's going to give us an error
12849
10:57:33,200 --> 10:57:38,240
because two lowercase only works with
12850
10:57:35,680 --> 10:57:41,756
strings and TypeScript figured out that
12851
10:57:38,240 --> 10:57:44,400
this is a number. So, as you can see,
12852
10:57:41,756 --> 10:57:47,116
TypeScript is just JavaScript, but it
12853
10:57:44,400 --> 10:57:50,240
adds extra features like type checking
12854
10:57:47,116 --> 10:57:53,436
and type inference to our code, and this
12855
10:57:50,240 --> 10:57:56,400
helps us find and avoid errors. So, now
12856
10:57:53,436 --> 10:57:59,520
let's save this code. And we'll save
12857
10:57:56,400 --> 10:58:01,116
these changes into git. In the git
12858
10:57:59,520 --> 10:58:03,520
section
12859
10:58:01,116 --> 10:58:09,200
at the top, we're going to use the
12860
10:58:03,520 --> 10:58:10,720
message create new React TypeScript
12861
10:58:09,200 --> 10:58:14,360
project
12862
10:58:10,720 --> 10:58:14,360
and press commit.
12863
10:58:16,800 --> 10:58:22,240
So, we just learned how to create a new
12864
10:58:19,116 --> 10:58:24,320
React project that uses TypeScript.
12865
10:58:22,240 --> 10:58:27,276
Now, let's learn how to convert an
12866
10:58:24,320 --> 10:58:30,960
existing project, like our e-commerce
12867
10:58:27,276 --> 10:58:32,560
project, to Typescript. To do that,
12868
10:58:30,960 --> 10:58:35,276
we're just going to move all of our
12869
10:58:32,560 --> 10:58:36,880
changes over to the new TypeScript
12870
10:58:35,276 --> 10:58:39,360
project.
12871
10:58:36,880 --> 10:58:41,680
Before we begin, if you have any issues
12872
10:58:39,360 --> 10:58:44,240
during this process, please check the
12873
10:58:41,680 --> 10:58:46,240
troubleshooting guide in the description
12874
10:58:44,240 --> 10:58:50,000
since things might change depending on
12875
10:58:46,240 --> 10:58:52,560
the version of VIT.
12876
10:58:50,000 --> 10:58:54,880
To make this easier in the e-commerce
12877
10:58:52,560 --> 10:58:59,000
project, we're going to delete the node
12878
10:58:54,880 --> 10:58:59,000
modules folder for now.
12879
10:59:01,840 --> 10:59:06,720
And then we'll make a copy of this
12880
10:59:03,916 --> 10:59:08,880
folder. So we'll rightclick,
12881
10:59:06,720 --> 10:59:11,840
copy,
12882
10:59:08,880 --> 10:59:15,680
and we'll collapse these folders and
12883
10:59:11,840 --> 10:59:17,756
click the outside and rightclick and
12884
10:59:15,680 --> 10:59:19,520
paste.
12885
10:59:17,756 --> 10:59:21,596
So, this just helps us keep track of
12886
10:59:19,520 --> 10:59:24,320
progress when we're moving the changes
12887
10:59:21,596 --> 10:59:26,960
over. So, we're going to start from top
12888
10:59:24,320 --> 10:59:30,320
to bottom. First, we'll look at the
12889
10:59:26,960 --> 10:59:33,916
public folder. So, in here, the change
12890
10:59:30,320 --> 10:59:35,116
we made is we added this images folder.
12891
10:59:33,916 --> 10:59:38,160
So, we're going to take the public
12892
10:59:35,116 --> 10:59:40,800
folder and we're going to drag it to the
12893
10:59:38,160 --> 10:59:42,880
TypeScript project
12894
10:59:40,800 --> 10:59:46,320
and click move
12895
10:59:42,880 --> 10:59:50,160
and click replace.
12896
10:59:46,320 --> 10:59:52,080
Next, let's go into the source folder
12897
10:59:50,160 --> 10:59:55,200
and we're going to move our changes over
12898
10:59:52,080 --> 10:59:56,960
one by one. So, first in the assets
12899
10:59:55,200 --> 10:59:58,800
folder,
12900
10:59:56,960 --> 11:00:00,800
we didn't make any changes to this
12901
10:59:58,800 --> 11:00:06,116
folder, so we're not going to move it
12902
11:00:00,800 --> 11:00:06,116
over and we can just delete this folder.
12903
11:00:06,640 --> 11:00:12,080
As a reminder, we're not trying to move
12904
11:00:09,116 --> 11:00:14,960
all of our code over. We want to start
12905
11:00:12,080 --> 11:00:17,916
from a default TypeScript project and
12906
11:00:14,960 --> 11:00:19,596
then only move our changes over to the
12907
11:00:17,916 --> 11:00:21,436
new project.
12908
11:00:19,596 --> 11:00:24,320
So let's continue and look at the
12909
11:00:21,436 --> 11:00:26,320
components folder. So this folder is
12910
11:00:24,320 --> 11:00:28,560
completely new. So we're going to move
12911
11:00:26,320 --> 11:00:31,680
this entire folder over to the
12912
11:00:28,560 --> 11:00:34,240
TypeScript project inside the source
12913
11:00:31,680 --> 11:00:37,040
folder
12914
11:00:34,240 --> 11:00:39,436
and click move.
12915
11:00:37,040 --> 11:00:41,916
And if it asks to update the imports,
12916
11:00:39,436 --> 11:00:44,640
we're going to click no because we're
12917
11:00:41,916 --> 11:00:48,000
going to move everything over.
12918
11:00:44,640 --> 11:00:50,720
Next, we're going to go back up and the
12919
11:00:48,000 --> 11:00:53,596
pages folder is new. So, we're going to
12920
11:00:50,720 --> 11:00:56,640
drag this folder into the TypeScript
12921
11:00:53,596 --> 11:00:58,880
project inside source
12922
11:00:56,640 --> 11:01:03,116
and click move
12923
11:00:58,880 --> 11:01:06,240
and update the imports. Click no.
12924
11:01:03,116 --> 11:01:08,480
Next, we'll scroll up and the utils
12925
11:01:06,240 --> 11:01:10,960
folder is new. So, we're going to select
12926
11:01:08,480 --> 11:01:14,320
this folder and drag it into the
12927
11:01:10,960 --> 11:01:18,400
TypeScript folder inside source. So,
12928
11:01:14,320 --> 11:01:22,080
move and click no.
12929
11:01:18,400 --> 11:01:24,160
And next, we'll keep going one by one.
12930
11:01:22,080 --> 11:01:26,240
For app.css,
12931
11:01:24,160 --> 11:01:31,596
we made it empty. So, we're going to
12932
11:01:26,240 --> 11:01:35,916
move this over. So, drag it into source.
12933
11:01:31,596 --> 11:01:40,000
Click move and click replace
12934
11:01:35,916 --> 11:01:42,640
and scroll back up for app.jsx.
12935
11:01:40,000 --> 11:01:45,116
We made a bunch of changes in here. So,
12936
11:01:42,640 --> 11:01:48,080
we're going to move this over. So, drag
12937
11:01:45,116 --> 11:01:50,960
it into source
12938
11:01:48,080 --> 11:01:53,116
and click move.
12939
11:01:50,960 --> 11:01:57,116
However, notice in the TypeScript
12940
11:01:53,116 --> 11:01:58,800
project, the file is called app.tsx.
12941
11:01:57,116 --> 11:02:01,680
So, we're going to replace this code
12942
11:01:58,800 --> 11:02:06,756
with our file. So, first we're going to
12943
11:02:01,680 --> 11:02:06,756
select this and delete app.tsx.
12944
11:02:07,040 --> 11:02:12,400
And then we'll rightclick app.jsx
12945
11:02:10,320 --> 11:02:16,840
and rename
12946
11:02:12,400 --> 11:02:16,840
and rename it to tsx.
12947
11:02:17,360 --> 11:02:22,400
Now, we'll scroll back up and continue
12948
11:02:20,320 --> 11:02:24,480
moving our changes.
12949
11:02:22,400 --> 11:02:27,360
So, index.css,
12950
11:02:24,480 --> 11:02:29,436
we completely changed this file. So,
12951
11:02:27,360 --> 11:02:33,040
we're going to move it into the
12952
11:02:29,436 --> 11:02:37,436
TypeScript project into source.
12953
11:02:33,040 --> 11:02:41,040
Click move and click replace
12954
11:02:37,436 --> 11:02:42,640
and scroll back up. And finally, in
12955
11:02:41,040 --> 11:02:45,200
main.jsx,
12956
11:02:42,640 --> 11:02:48,320
we just added the router. So, let's move
12957
11:02:45,200 --> 11:02:51,276
this file as well. We'll select it and
12958
11:02:48,320 --> 11:02:53,436
drag it into source
12959
11:02:51,276 --> 11:02:55,680
and move.
12960
11:02:53,436 --> 11:02:58,240
And again in Typescript the file is
12961
11:02:55,680 --> 11:03:02,520
called main.tsx.
12962
11:02:58,240 --> 11:03:02,520
We'll select it and delete.
12963
11:03:02,880 --> 11:03:08,720
And we're going to rename our file.
12964
11:03:06,000 --> 11:03:11,116
Right click, rename
12965
11:03:08,720 --> 11:03:14,160
main.tsx,
12966
11:03:11,116 --> 11:03:18,160
and press enter.
12967
11:03:14,160 --> 11:03:21,680
Update the imports. Select no.
12968
11:03:18,160 --> 11:03:24,000
We'll scroll back up and we moved all of
12969
11:03:21,680 --> 11:03:27,560
the code from the source folder so we
12970
11:03:24,000 --> 11:03:27,560
can delete it.
12971
11:03:27,916 --> 11:03:32,800
Next, we don't need to move the starting
12972
11:03:30,400 --> 11:03:34,800
code folder because this was just for
12973
11:03:32,800 --> 11:03:37,200
practice when we were setting up this
12974
11:03:34,800 --> 11:03:40,200
project. So, we can just delete this
12975
11:03:37,200 --> 11:03:40,200
folder.
12976
11:03:41,916 --> 11:03:47,840
Now, let's move all the files over to
12977
11:03:44,800 --> 11:03:50,400
the TypeScript project. So in git
12978
11:03:47,840 --> 11:03:52,480
ignore, we didn't make any changes to
12979
11:03:50,400 --> 11:03:57,116
this file, so we don't need to move it
12980
11:03:52,480 --> 11:04:00,320
over and we can delete it.
12981
11:03:57,116 --> 11:04:02,880
In eslint.config.js,
12982
11:04:00,320 --> 11:04:06,000
we added one change to this file at the
12983
11:04:02,880 --> 11:04:08,800
bottom, which is we added this rule to
12984
11:04:06,000 --> 11:04:12,640
disable prop types. So we'll select this
12985
11:04:08,800 --> 11:04:15,116
code and copy.
12986
11:04:12,640 --> 11:04:18,240
And in the TypeScript project, we're
12987
11:04:15,116 --> 11:04:22,480
going to open eslint.config.
12988
11:04:18,240 --> 11:04:24,640
And at the bottom in this rules object,
12989
11:04:22,480 --> 11:04:28,320
we're going to paste and move that
12990
11:04:24,640 --> 11:04:30,400
change over and save.
12991
11:04:28,320 --> 11:04:32,720
So again, we don't want to move all of
12992
11:04:30,400 --> 11:04:35,520
our code over. We just want to move the
12993
11:04:32,720 --> 11:04:38,640
changes over because a lot of this code
12994
11:04:35,520 --> 11:04:41,596
is specific to TypeScript.
12995
11:04:38,640 --> 11:04:44,160
So, we'll scroll back up here and we can
12996
11:04:41,596 --> 11:04:46,800
delete this file because we moved it
12997
11:04:44,160 --> 11:04:50,640
over.
12998
11:04:46,800 --> 11:04:52,480
Next, let's keep going in here. For
12999
11:04:50,640 --> 11:04:55,680
index.html,
13000
11:04:52,480 --> 11:04:58,480
we changed a few parts of this code. So,
13001
11:04:55,680 --> 11:05:00,720
we're going to move these changes over
13002
11:04:58,480 --> 11:05:03,040
to help us compare this code with the
13003
11:05:00,720 --> 11:05:04,560
TypeScript version. I'm going to scroll
13004
11:05:03,040 --> 11:05:07,436
down
13005
11:05:04,560 --> 11:05:10,400
and take this index.html HTML and then
13006
11:05:07,436 --> 11:05:13,596
move it at the bottom here so that I can
13007
11:05:10,400 --> 11:05:16,480
compare these side by side.
13008
11:05:13,596 --> 11:05:19,040
So first the first three lines here we
13009
11:05:16,480 --> 11:05:20,880
did not make any changes so we can skip
13010
11:05:19,040 --> 11:05:24,320
them.
13011
11:05:20,880 --> 11:05:26,480
And then here the next three lines we
13012
11:05:24,320 --> 11:05:30,080
also did not make any changes so we can
13013
11:05:26,480 --> 11:05:32,240
skip them. And here we added a base
13014
11:05:30,080 --> 11:05:36,000
element. So we're going to select this
13015
11:05:32,240 --> 11:05:39,360
and copy. And in the TypeScript version,
13016
11:05:36,000 --> 11:05:42,720
we're going to paste that change.
13017
11:05:39,360 --> 11:05:44,640
And here we also deleted the title. So
13018
11:05:42,720 --> 11:05:47,916
in the TypeScript version, we're also
13019
11:05:44,640 --> 11:05:51,596
going to delete the title.
13020
11:05:47,916 --> 11:05:53,916
And here we added a font called Roboto.
13021
11:05:51,596 --> 11:05:56,880
So we're going to move this change by
13022
11:05:53,916 --> 11:06:01,596
selecting it and copy.
13023
11:05:56,880 --> 11:06:03,360
And in here we'll paste the font. And if
13024
11:06:01,596 --> 11:06:06,800
we need to fix the formatting, we can
13025
11:06:03,360 --> 11:06:10,436
select this and press shift tab to
13026
11:06:06,800 --> 11:06:10,436
remove some indents
13027
11:06:10,640 --> 11:06:16,800
and I'll scroll down.
13028
11:06:14,400 --> 11:06:19,276
And for the rest of the code, we did not
13029
11:06:16,800 --> 11:06:22,400
make any changes. So now we moved all
13030
11:06:19,276 --> 11:06:26,400
the changes over. I'm going to save this
13031
11:06:22,400 --> 11:06:28,800
file and close this for now.
13032
11:06:26,400 --> 11:06:31,800
And then in the copy, we can delete
13033
11:06:28,800 --> 11:06:31,800
index.html.
13034
11:06:32,560 --> 11:06:38,480
And next, we don't need to move packaged
13035
11:06:35,756 --> 11:06:41,840
lock.json over because this will be
13036
11:06:38,480 --> 11:06:46,116
autogenerated when we npm install. So,
13037
11:06:41,840 --> 11:06:46,116
we can just delete this file
13038
11:06:46,960 --> 11:06:51,400
and I'll scroll back up
13039
11:06:51,916 --> 11:06:57,596
and we'll move over package.json.
13040
11:06:55,360 --> 11:07:00,000
In this file, we didn't change anything
13041
11:06:57,596 --> 11:07:02,720
at the top, but we did add some
13042
11:07:00,000 --> 11:07:05,276
dependencies and dev dependencies. So,
13043
11:07:02,720 --> 11:07:09,520
we're going to move these over. We'll
13044
11:07:05,276 --> 11:07:12,560
select the dependencies and copy.
13045
11:07:09,520 --> 11:07:15,560
And then in the TypeScript project, open
13046
11:07:12,560 --> 11:07:15,560
package.json.
13047
11:07:15,840 --> 11:07:21,916
And in these dependencies,
13048
11:07:18,160 --> 11:07:24,720
we'll type comma and paste.
13049
11:07:21,916 --> 11:07:30,240
And then we'll remove any duplicates.
13050
11:07:24,720 --> 11:07:32,480
So, we'll remove React and React DOM and
13051
11:07:30,240 --> 11:07:35,116
save.
13052
11:07:32,480 --> 11:07:37,840
And we'll scroll back up
13053
11:07:35,116 --> 11:07:39,680
and open package.json.
13054
11:07:37,840 --> 11:07:42,720
And we'll do the same thing with the dev
13055
11:07:39,680 --> 11:07:44,400
dependencies. So, we'll select all these
13056
11:07:42,720 --> 11:07:46,640
dependencies
13057
11:07:44,400 --> 11:07:49,360
and copy.
13058
11:07:46,640 --> 11:07:51,840
And in the TypeScript project
13059
11:07:49,360 --> 11:07:54,560
package.json JSON in the dev
13060
11:07:51,840 --> 11:07:58,000
dependencies at the bottom we'll type
13061
11:07:54,560 --> 11:08:01,520
comma and paste
13062
11:07:58,000 --> 11:08:05,116
and we'll remove all these duplicates.
13063
11:08:01,520 --> 11:08:08,320
So I'll remove eslint
13064
11:08:05,116 --> 11:08:11,756
and I'll remove these duplicates
13065
11:08:08,320 --> 11:08:15,400
and these duplicates
13066
11:08:11,756 --> 11:08:15,400
and these duplicates
13067
11:08:15,596 --> 11:08:20,400
and remove this
13068
11:08:18,240 --> 11:08:22,000
until there are no more duplicates
13069
11:08:20,400 --> 11:08:24,560
remaining.
13070
11:08:22,000 --> 11:08:26,960
And now we'll save.
13071
11:08:24,560 --> 11:08:29,360
And then we're going to run npm install
13072
11:08:26,960 --> 11:08:32,400
to make sure we successfully install all
13073
11:08:29,360 --> 11:08:34,560
these packages. In the command line,
13074
11:08:32,400 --> 11:08:40,680
we'll go to the tab for the TypeScript
13075
11:08:34,560 --> 11:08:40,680
project and we'll run npm install.
13076
11:08:41,360 --> 11:08:47,640
And now we can scroll up and delete
13077
11:08:44,640 --> 11:08:47,640
package.json.
13078
11:08:48,320 --> 11:08:54,480
And we'll keep going for readme.md. We
13079
11:08:52,480 --> 11:08:57,880
didn't change this file. So we can
13080
11:08:54,480 --> 11:08:57,880
delete it.
13081
11:08:59,276 --> 11:09:05,116
And this file setup test.js is new. So
13082
11:09:03,116 --> 11:09:07,360
we'll move this over to the TypeScript
13083
11:09:05,116 --> 11:09:10,160
project.
13084
11:09:07,360 --> 11:09:12,960
And we'll scroll up and we're going to
13085
11:09:10,160 --> 11:09:14,880
move over vit.config.js.
13086
11:09:12,960 --> 11:09:16,400
So in here we added a bunch of
13087
11:09:14,880 --> 11:09:19,200
configurations.
13088
11:09:16,400 --> 11:09:23,276
So in the TypeScript project
13089
11:09:19,200 --> 11:09:25,520
we're going to drag vit.config.ts ts at
13090
11:09:23,276 --> 11:09:27,200
the bottom
13091
11:09:25,520 --> 11:09:32,480
and we're going to move our changes
13092
11:09:27,200 --> 11:09:34,640
over. So, inside the config,
13093
11:09:32,480 --> 11:09:37,520
we added some stuff for the React
13094
11:09:34,640 --> 11:09:40,800
config. So, we're going to select this
13095
11:09:37,520 --> 11:09:44,000
React config and copy
13096
11:09:40,800 --> 11:09:46,960
and then select this code, remove it,
13097
11:09:44,000 --> 11:09:49,680
and paste.
13098
11:09:46,960 --> 11:09:52,320
And we'll keep going. So, all of these
13099
11:09:49,680 --> 11:09:54,960
configs are new. So, we can just select
13100
11:09:52,320 --> 11:09:57,680
this code
13101
11:09:54,960 --> 11:10:00,640
and then copy.
13102
11:09:57,680 --> 11:10:03,040
And inside the TypeScript version, we're
13103
11:10:00,640 --> 11:10:06,000
going to paste
13104
11:10:03,040 --> 11:10:08,480
and save.
13105
11:10:06,000 --> 11:10:11,596
So, now we moved all of our changes over
13106
11:10:08,480 --> 11:10:14,916
to the new project. And we can delete
13107
11:10:11,596 --> 11:10:14,916
this file.
13108
11:10:15,040 --> 11:10:20,480
And I'll close this for now.
13109
11:10:17,840 --> 11:10:23,840
And finally, vtest.config.js.
13110
11:10:20,480 --> 11:10:26,800
JS is new. So, we can drag it into the
13111
11:10:23,840 --> 11:10:29,756
project.
13112
11:10:26,800 --> 11:10:32,480
And now we moved all of our changes over
13113
11:10:29,756 --> 11:10:36,200
into the TypeScript project. And I can
13114
11:10:32,480 --> 11:10:36,200
delete this copy.
13115
11:10:38,080 --> 11:10:42,160
Next, we're going to go into the
13116
11:10:39,756 --> 11:10:45,200
TypeScript project and we're going to
13117
11:10:42,160 --> 11:10:47,436
fix the errors. First, we'll open
13118
11:10:45,200 --> 11:10:50,000
main.tsx.
13119
11:10:47,436 --> 11:10:51,916
And in here, instead of importing from
13120
11:10:50,000 --> 11:10:55,200
app.jsx,
13121
11:10:51,916 --> 11:10:58,240
the file is now app.tsx.
13122
11:10:55,200 --> 11:11:01,360
So we're going to update that.
13123
11:10:58,240 --> 11:11:03,596
Also, we have an error here. So this
13124
11:11:01,360 --> 11:11:07,840
code tries to get an element from the
13125
11:11:03,596 --> 11:11:10,880
page using the ID root. However, this
13126
11:11:07,840 --> 11:11:13,520
element might not exist. So TypeScript
13127
11:11:10,880 --> 11:11:17,360
gives us an error telling us this value
13128
11:11:13,520 --> 11:11:20,080
might be null. However, if we check our
13129
11:11:17,360 --> 11:11:23,756
index.html
13130
11:11:20,080 --> 11:11:26,560
and scroll down, we have an element here
13131
11:11:23,756 --> 11:11:29,840
with the ID of root. So, we know that
13132
11:11:26,560 --> 11:11:31,840
this element definitely exists.
13133
11:11:29,840 --> 11:11:34,240
In main.tsx,
13134
11:11:31,840 --> 11:11:37,200
to tell Typescript that this value
13135
11:11:34,240 --> 11:11:42,480
definitely exists. At the end, we're
13136
11:11:37,200 --> 11:11:45,520
going to add exclamation and save.
13137
11:11:42,480 --> 11:11:48,160
So this tells Typescript that this value
13138
11:11:45,520 --> 11:11:52,880
definitely exists and it will not be
13139
11:11:48,160 --> 11:11:57,080
null. And now the error is gone.
13140
11:11:52,880 --> 11:11:57,080
Next we'll open app.tsx.
13141
11:11:57,200 --> 11:12:02,800
And up here we get some errors when we
13142
11:11:59,916 --> 11:12:04,800
are trying to import these files. The
13143
11:12:02,800 --> 11:12:08,240
reason we get these errors is because
13144
11:12:04,800 --> 11:12:11,756
these files are still.jsx
13145
11:12:08,240 --> 11:12:15,200
files or JavaScript files. So, we could
13146
11:12:11,756 --> 11:12:16,960
rename all of our files totsx,
13147
11:12:15,200 --> 11:12:19,520
but then Typescript will start doing
13148
11:12:16,960 --> 11:12:22,080
type checking in these files and we may
13149
11:12:19,520 --> 11:12:23,916
get a bunch of errors.
13150
11:12:22,080 --> 11:12:27,436
Instead,
13151
11:12:23,916 --> 11:12:30,436
in our files, we can scroll down to
13152
11:12:27,436 --> 11:12:30,436
tsconfig.app.json.
13153
11:12:31,680 --> 11:12:36,000
And inside here,
13154
11:12:34,160 --> 11:12:40,880
we're going to add a setting double
13155
11:12:36,000 --> 11:12:44,480
quotes allow capital J S
13156
11:12:40,880 --> 11:12:47,276
colon true and comma
13157
11:12:44,480 --> 11:12:50,320
and save.
13158
11:12:47,276 --> 11:12:53,680
So this setting lets us mix TypeScript
13159
11:12:50,320 --> 11:12:56,880
and JavaScript files in our project. So
13160
11:12:53,680 --> 11:12:59,680
now in app.tsx, tsx.
13161
11:12:56,880 --> 11:13:02,560
Our TypeScript code can also import from
13162
11:12:59,680 --> 11:13:06,080
JavaScript files and it won't cause an
13163
11:13:02,560 --> 11:13:08,560
error. So the allowJS setting lets us
13164
11:13:06,080 --> 11:13:12,800
gradually convert a JavaScript project
13165
11:13:08,560 --> 11:13:15,116
to TypeScript, one file at a time.
13166
11:13:12,800 --> 11:13:17,200
Now that we fixed the errors, let's run
13167
11:13:15,116 --> 11:13:18,960
this project again to see if our
13168
11:13:17,200 --> 11:13:22,320
e-commerce project works with
13169
11:13:18,960 --> 11:13:25,436
TypeScript. in the command line. Make
13170
11:13:22,320 --> 11:13:28,960
sure you're in e-commerce project ts and
13171
11:13:25,436 --> 11:13:32,160
we're going to npm rundev
13172
11:13:28,960 --> 11:13:34,800
and press enter.
13173
11:13:32,160 --> 11:13:36,480
And now if we open this URL in the
13174
11:13:34,800 --> 11:13:39,916
browser
13175
11:13:36,480 --> 11:13:42,160
and refresh
13176
11:13:39,916 --> 11:13:45,200
and now you can see that our e-commerce
13177
11:13:42,160 --> 11:13:47,596
project is working with TypeScript. If
13178
11:13:45,200 --> 11:13:50,000
we try some of the features like add to
13179
11:13:47,596 --> 11:13:53,040
cart, you'll see that everything still
13180
11:13:50,000 --> 11:13:54,800
works. If this does not work for you,
13181
11:13:53,040 --> 11:13:57,840
you can check the troubleshooting guide
13182
11:13:54,800 --> 11:14:00,800
in the description.
13183
11:13:57,840 --> 11:14:03,360
Now, let's go back to our code
13184
11:14:00,800 --> 11:14:06,080
and let's rename our folders so that we
13185
11:14:03,360 --> 11:14:08,880
use TypeScript by default. We're going
13186
11:14:06,080 --> 11:14:13,200
to scroll up and we're going to rename
13187
11:14:08,880 --> 11:14:16,960
e-commerce project. Right click, rename
13188
11:14:13,200 --> 11:14:18,880
to e-commerce project dashjs
13189
11:14:16,960 --> 11:14:20,400
for JavaScript.
13190
11:14:18,880 --> 11:14:23,916
And then we're going to rename
13191
11:14:20,400 --> 11:14:28,040
e-commerce project-ts
13192
11:14:23,916 --> 11:14:28,040
to e-commerce project.
13193
11:14:28,720 --> 11:14:33,200
So now by default our project will be
13194
11:14:31,200 --> 11:14:34,880
using TypeScript.
13195
11:14:33,200 --> 11:14:37,680
And just to make sure, we're going to go
13196
11:14:34,880 --> 11:14:39,756
to the command line and stop the V
13197
11:14:37,680 --> 11:14:42,720
server. And we're going to delete all
13198
11:14:39,756 --> 11:14:45,596
our tabs. So here we'll also control C
13199
11:14:42,720 --> 11:14:48,640
to stop the back end and delete all the
13200
11:14:45,596 --> 11:14:51,360
tabs. And then we'll reopen the command
13201
11:14:48,640 --> 11:14:54,240
line using the terminal menu and new
13202
11:14:51,360 --> 11:14:56,400
terminal.
13203
11:14:54,240 --> 11:15:01,080
And we're going to restart the back end
13204
11:14:56,400 --> 11:15:01,080
using cd ecommerce-backend
13205
11:15:01,596 --> 11:15:05,400
mpm rundev
13206
11:15:05,916 --> 11:15:12,720
and create a new tab
13207
11:15:10,000 --> 11:15:15,840
and we'll run the front end cd
13208
11:15:12,720 --> 11:15:20,276
ecommerce- project
13209
11:15:15,840 --> 11:15:20,276
and npm rundev.
13210
11:15:20,400 --> 11:15:28,720
And in the browser, we'll refresh
13211
11:15:25,360 --> 11:15:32,240
and our TypeScript project still works.
13212
11:15:28,720 --> 11:15:34,400
We'll go to VS Code. And finally, we're
13213
11:15:32,240 --> 11:15:36,960
going to organize these folders a little
13214
11:15:34,400 --> 11:15:39,276
bit. So, right now, we're no longer
13215
11:15:36,960 --> 11:15:42,160
using the e-commerce project JavaScript
13216
11:15:39,276 --> 11:15:44,960
version or the React basics folder. So,
13217
11:15:42,160 --> 11:15:47,360
let's click outside and we'll click here
13218
11:15:44,960 --> 11:15:51,756
to create a new folder. and let's call
13219
11:15:47,360 --> 11:15:53,840
it old dash projects.
13220
11:15:51,756 --> 11:15:57,560
And then we'll drag the JavaScript
13221
11:15:53,840 --> 11:15:57,560
version into here.
13222
11:15:57,756 --> 11:16:03,560
And we'll move React Basics into here.
13223
11:16:04,160 --> 11:16:09,200
So now our folders are a little bit more
13224
11:16:06,640 --> 11:16:11,916
organized.
13225
11:16:09,200 --> 11:16:15,360
So now let's save this into Git. We'll
13226
11:16:11,916 --> 11:16:20,480
open the git section and at the top
13227
11:16:15,360 --> 11:16:27,320
we'll use the message convert e-commerce
13228
11:16:20,480 --> 11:16:27,320
project to type script and commit.
13229
11:16:30,080 --> 11:16:36,480
Next we're going to learn an intro to
13230
11:16:32,880 --> 11:16:38,640
TypeScript with React. First, we'll go
13231
11:16:36,480 --> 11:16:41,276
into our files
13232
11:16:38,640 --> 11:16:45,360
and in the project, we're going to go to
13233
11:16:41,276 --> 11:16:48,880
source the utils folder and we're going
13234
11:16:45,360 --> 11:16:51,916
to look at money.js.
13235
11:16:48,880 --> 11:16:54,400
Currently, this is a JavaScript file.
13236
11:16:51,916 --> 11:16:56,560
That means there is no type checking in
13237
11:16:54,400 --> 11:17:00,640
this code. So, we're going to convert
13238
11:16:56,560 --> 11:17:02,880
this to TypeScript by right-clicking
13239
11:17:00,640 --> 11:17:06,320
and rename.
13240
11:17:02,880 --> 11:17:09,040
And we'll rename it to money.t. ts or
13241
11:17:06,320 --> 11:17:11,200
typescript.
13242
11:17:09,040 --> 11:17:14,000
So this converts the file into
13243
11:17:11,200 --> 11:17:16,640
typescript and now it will enable type
13244
11:17:14,000 --> 11:17:18,800
checking and now we will get an error
13245
11:17:16,640 --> 11:17:21,436
here.
13246
11:17:18,800 --> 11:17:25,040
So this error says amount sent
13247
11:17:21,436 --> 11:17:27,680
implicitly has an any type. So what this
13248
11:17:25,040 --> 11:17:29,916
means is that earlier we learned the
13249
11:17:27,680 --> 11:17:32,240
feature type inference which means
13250
11:17:29,916 --> 11:17:34,480
typescript can figure out the type
13251
11:17:32,240 --> 11:17:37,520
automatically.
13252
11:17:34,480 --> 11:17:40,000
However, in this situation, TypeScript
13253
11:17:37,520 --> 11:17:43,276
does not have enough information to
13254
11:17:40,000 --> 11:17:46,960
figure out the type of amount sets. So,
13255
11:17:43,276 --> 11:17:50,000
we have to set the type manually.
13256
11:17:46,960 --> 11:17:52,400
To set the type of a parameter after
13257
11:17:50,000 --> 11:17:56,000
this parameter, we're going to type
13258
11:17:52,400 --> 11:17:59,200
colon space and then the type of this
13259
11:17:56,000 --> 11:18:01,360
value. So, in this case, amount sent
13260
11:17:59,200 --> 11:18:06,400
should be a number. So, we're going to
13261
11:18:01,360 --> 11:18:09,116
type number and save.
13262
11:18:06,400 --> 11:18:11,680
And now that TypeScript knows what type
13263
11:18:09,116 --> 11:18:14,160
of value this is, the error will go
13264
11:18:11,680 --> 11:18:17,200
away. And now we successfully converted
13265
11:18:14,160 --> 11:18:19,756
this file to TypeScript.
13266
11:18:17,200 --> 11:18:22,960
Let's do another example. We're going to
13267
11:18:19,756 --> 11:18:25,520
go to the components folder and we're
13268
11:18:22,960 --> 11:18:27,840
going to convert header.jsx into
13269
11:18:25,520 --> 11:18:29,596
TypeScript. So we're going to rename
13270
11:18:27,840 --> 11:18:33,000
this file
13271
11:18:29,596 --> 11:18:33,000
to header.tsx.
13272
11:18:33,840 --> 11:18:40,720
So tsx is just a TypeScript file, but
13273
11:18:37,596 --> 11:18:44,640
the X means we can write HTML inside
13274
11:18:40,720 --> 11:18:47,040
this file. So it's just like JSX except
13275
11:18:44,640 --> 11:18:48,960
it uses TypeScript.
13276
11:18:47,040 --> 11:18:51,916
So now this converts the file to
13277
11:18:48,960 --> 11:18:54,640
TypeScript and it enables type checking
13278
11:18:51,916 --> 11:18:57,360
and we get some errors here.
13279
11:18:54,640 --> 11:19:00,560
If we look at this error,
13280
11:18:57,360 --> 11:19:03,756
we get a similar error. Cart implicitly
13281
11:19:00,560 --> 11:19:06,000
has an any type. So again, this means
13282
11:19:03,756 --> 11:19:08,480
that TypeScript does not have enough
13283
11:19:06,000 --> 11:19:11,276
information to figure out the type of
13284
11:19:08,480 --> 11:19:14,080
cart automatically. So we have to set
13285
11:19:11,276 --> 11:19:16,560
the type manually.
13286
11:19:14,080 --> 11:19:19,916
We might try to add the type after cart
13287
11:19:16,560 --> 11:19:23,596
like this. However, this is not how we
13288
11:19:19,916 --> 11:19:26,720
add a type to this code because remember
13289
11:19:23,596 --> 11:19:28,560
this code is actually a shortcut for
13290
11:19:26,720 --> 11:19:31,276
this code. This is called the
13291
11:19:28,560 --> 11:19:35,756
dstructuring shortcut.
13292
11:19:31,276 --> 11:19:39,596
So this whole thing is the parameter.
13293
11:19:35,756 --> 11:19:42,240
So we need to add the type here
13294
11:19:39,596 --> 11:19:45,436
after the parameter.
13295
11:19:42,240 --> 11:19:48,480
So this code is a shortcut for props
13296
11:19:45,436 --> 11:19:51,840
which is an object. To tell Typescript
13297
11:19:48,480 --> 11:19:54,800
that this is an object here we're going
13298
11:19:51,840 --> 11:19:57,360
to type curly brackets
13299
11:19:54,800 --> 11:20:00,560
and then inside here we can also tell
13300
11:19:57,360 --> 11:20:04,080
TypeScript what properties are in this
13301
11:20:00,560 --> 11:20:06,960
object. So in this object we have a cart
13302
11:20:04,080 --> 11:20:10,240
property. So in the type we're also
13303
11:20:06,960 --> 11:20:13,200
going to add a cart property. And
13304
11:20:10,240 --> 11:20:15,276
remember that the cart is an array like
13305
11:20:13,200 --> 11:20:17,116
this.
13306
11:20:15,276 --> 11:20:20,960
So we're also going to tell Typescript
13307
11:20:17,116 --> 11:20:24,320
that this cart is an array using colon
13308
11:20:20,960 --> 11:20:27,040
and square bracket semicolon.
13309
11:20:24,320 --> 11:20:30,320
So again, this code tells Typescript
13310
11:20:27,040 --> 11:20:33,276
that this value is an object with a
13311
11:20:30,320 --> 11:20:35,840
property called cart and the cart is an
13312
11:20:33,276 --> 11:20:38,160
array.
13313
11:20:35,840 --> 11:20:41,360
Next, we can also tell Typescript what
13314
11:20:38,160 --> 11:20:44,960
is inside this array. In front of the
13315
11:20:41,360 --> 11:20:47,360
array, we can type curly brackets to
13316
11:20:44,960 --> 11:20:50,160
tell Typescript that this is an array of
13317
11:20:47,360 --> 11:20:52,960
objects.
13318
11:20:50,160 --> 11:20:55,596
And inside this object, we can also tell
13319
11:20:52,960 --> 11:20:59,040
TypeScript what properties each object
13320
11:20:55,596 --> 11:21:03,680
has. So inside our cart array, each
13321
11:20:59,040 --> 11:21:06,320
object has a product ID and this product
13322
11:21:03,680 --> 11:21:12,240
ID is a string. So we can tell that to
13323
11:21:06,320 --> 11:21:15,840
TypeScript using colon string semicolon
13324
11:21:12,240 --> 11:21:18,000
and each object also has a quantity and
13325
11:21:15,840 --> 11:21:23,276
this is a number. So we can tell that to
13326
11:21:18,000 --> 11:21:28,080
Typescript using colon number semicolon
13327
11:21:23,276 --> 11:21:31,916
and each object has a delivery option ID
13328
11:21:28,080 --> 11:21:34,000
which is a string. So colon string
13329
11:21:31,916 --> 11:21:36,880
semicolon.
13330
11:21:34,000 --> 11:21:39,916
So using this code we can tell
13331
11:21:36,880 --> 11:21:42,960
Typescript exactly what type of value
13332
11:21:39,916 --> 11:21:46,640
this is. It's an object with a property
13333
11:21:42,960 --> 11:21:49,276
called cart. And the cart is an array.
13334
11:21:46,640 --> 11:21:51,200
And each value in the array has these
13335
11:21:49,276 --> 11:21:53,520
three properties.
13336
11:21:51,200 --> 11:21:56,480
So now TypeScript can actually look at
13337
11:21:53,520 --> 11:21:59,840
the rest of our code and check if each
13338
11:21:56,480 --> 11:22:02,480
value in the array has a quantity
13339
11:21:59,840 --> 11:22:06,080
and if we accidentally misspell quantity
13340
11:22:02,480 --> 11:22:09,276
here like this. TypeScript can use this
13341
11:22:06,080 --> 11:22:11,520
type here to check if we made a mistake.
13342
11:22:09,276 --> 11:22:14,240
So this helps us avoid a lot of mistakes
13343
11:22:11,520 --> 11:22:18,436
when we're coding. So, let's add that
13344
11:22:14,240 --> 11:22:18,436
back and save.
13345
11:22:20,560 --> 11:22:26,400
Next, we're going to organize this code
13346
11:22:22,720 --> 11:22:29,116
a little bit. So, putting a complex type
13347
11:22:26,400 --> 11:22:32,160
directly in the code like this is a
13348
11:22:29,116 --> 11:22:34,560
little hard to read. So, we can actually
13349
11:22:32,160 --> 11:22:36,160
take this type and then save it
13350
11:22:34,560 --> 11:22:40,480
separately.
13351
11:22:36,160 --> 11:22:44,480
above this component we can use type
13352
11:22:40,480 --> 11:22:47,840
space and then give this a name.
13353
11:22:44,480 --> 11:22:50,800
So here this value is the props for the
13354
11:22:47,840 --> 11:22:55,680
header component. So we'll name this
13355
11:22:50,800 --> 11:22:57,680
header props and we'll type equal and
13356
11:22:55,680 --> 11:23:01,756
we're going to move this type up here.
13357
11:22:57,680 --> 11:23:06,520
So we'll select this type and cut.
13358
11:23:01,756 --> 11:23:06,520
And then in here we're going to paste.
13359
11:23:07,200 --> 11:23:12,720
So this is called a type alias and it
13360
11:23:10,480 --> 11:23:17,040
works just like a variable but for
13361
11:23:12,720 --> 11:23:19,756
types. So we can save a type in here and
13362
11:23:17,040 --> 11:23:23,436
then we can use this type later in our
13363
11:23:19,756 --> 11:23:28,800
code. For example, down here, we can
13364
11:23:23,436 --> 11:23:31,840
just use this type header props
13365
11:23:28,800 --> 11:23:35,116
and it will substitute this type into
13366
11:23:31,840 --> 11:23:38,320
our code just like a variable.
13367
11:23:35,116 --> 11:23:40,800
And now our code is much cleaner. So
13368
11:23:38,320 --> 11:23:44,400
that's an intro to how to use TypeScript
13369
11:23:40,800 --> 11:23:48,160
with React. We basically add types to
13370
11:23:44,400 --> 11:23:50,800
React features like props.
13371
11:23:48,160 --> 11:23:53,840
So now let's save
13372
11:23:50,800 --> 11:23:56,400
and we'll save these changes into git.
13373
11:23:53,840 --> 11:24:00,800
In the git section
13374
11:23:56,400 --> 11:24:07,276
at the top we'll use the message convert
13375
11:24:00,800 --> 11:24:10,680
money utils and header to type script
13376
11:24:07,276 --> 11:24:10,680
and commit.
13377
11:24:12,800 --> 11:24:18,080
Finally, we're going to learn how to use
13378
11:24:14,960 --> 11:24:21,840
AI or artificial intelligence with
13379
11:24:18,080 --> 11:24:25,680
React. After we learn React, we can use
13380
11:24:21,840 --> 11:24:28,160
AI to help us write React code faster.
13381
11:24:25,680 --> 11:24:31,360
The easiest way to get started with AI
13382
11:24:28,160 --> 11:24:35,916
is to use GitHub Copilot, which is built
13383
11:24:31,360 --> 11:24:38,160
into VS Code. Before we begin, AI tools
13384
11:24:35,916 --> 11:24:40,480
are changing very quickly. If something
13385
11:24:38,160 --> 11:24:42,640
in this section is not working, you can
13386
11:24:40,480 --> 11:24:44,240
check the troubleshooting guide in the
13387
11:24:42,640 --> 11:24:47,200
description.
13388
11:24:44,240 --> 11:24:50,640
To set up GitHub Copilot, we're going to
13389
11:24:47,200 --> 11:24:53,916
install the GitHub Copilot extension.
13390
11:24:50,640 --> 11:24:56,160
So, in the extension section, we need to
13391
11:24:53,916 --> 11:24:58,880
make sure we have the GitHub Copilot
13392
11:24:56,160 --> 11:25:02,240
extension installed. If it's not
13393
11:24:58,880 --> 11:25:06,080
installed, we can search up here for
13394
11:25:02,240 --> 11:25:10,520
GitHub Copilot
13395
11:25:06,080 --> 11:25:10,520
and install this extension.
13396
11:25:12,160 --> 11:25:17,040
Once we have that installed,
13397
11:25:14,640 --> 11:25:20,480
we're going to click this C-pilot icon
13398
11:25:17,040 --> 11:25:25,276
up here and we'll need to log in to our
13399
11:25:20,480 --> 11:25:28,320
GitHub account. So, we'll click sign in
13400
11:25:25,276 --> 11:25:30,320
and then we need to log in to GitHub. If
13401
11:25:28,320 --> 11:25:33,040
you don't have a GitHub account, you can
13402
11:25:30,320 --> 11:25:34,960
create one by clicking here or going to
13403
11:25:33,040 --> 11:25:37,040
github.com.
13404
11:25:34,960 --> 11:25:39,276
So, I'm going to log into my GitHub
13405
11:25:37,040 --> 11:25:43,400
account
13406
11:25:39,276 --> 11:25:43,400
and then I'll click continue
13407
11:25:43,756 --> 11:25:48,240
and I'll click here to go back to VS
13408
11:25:46,720 --> 11:25:50,800
Code.
13409
11:25:48,240 --> 11:25:54,400
Once we finish the setup, we can start
13410
11:25:50,800 --> 11:25:57,436
using some features of AI. The first
13411
11:25:54,400 --> 11:26:00,480
feature is autocomplete.
13412
11:25:57,436 --> 11:26:04,480
So, let's say that we're in the header
13413
11:26:00,480 --> 11:26:08,400
and we'll close this window for now.
13414
11:26:04,480 --> 11:26:10,000
And let's scroll down to the HTML
13415
11:26:08,400 --> 11:26:11,520
and we're going to go to the right
13416
11:26:10,000 --> 11:26:13,916
section.
13417
11:26:11,520 --> 11:26:17,520
Let's say that inside the header, we
13418
11:26:13,916 --> 11:26:20,320
want to add a link to a login page. So
13419
11:26:17,520 --> 11:26:22,720
here we can just start typing in this
13420
11:26:20,320 --> 11:26:24,880
code
13421
11:26:22,720 --> 11:26:28,000
and the AI will give us some
13422
11:26:24,880 --> 11:26:31,596
autocomplete suggestions. To accept
13423
11:26:28,000 --> 11:26:33,596
these suggestions, we can press tab.
13424
11:26:31,596 --> 11:26:37,116
So this creates a new link and some
13425
11:26:33,596 --> 11:26:39,276
text. And I'll press enter.
13426
11:26:37,116 --> 11:26:41,040
And it gives another suggestion. So I
13427
11:26:39,276 --> 11:26:43,360
press tab.
13428
11:26:41,040 --> 11:26:45,520
And this creates the closing tag for the
13429
11:26:43,360 --> 11:26:47,756
link.
13430
11:26:45,520 --> 11:26:51,116
So you notice that the autocomplete is
13431
11:26:47,756 --> 11:26:54,080
very smart because the AI can read and
13432
11:26:51,116 --> 11:26:56,240
understand the rest of our code and it
13433
11:26:54,080 --> 11:26:58,960
can suggest code that follows the same
13434
11:26:56,240 --> 11:27:02,000
patterns as the rest of our code. For
13435
11:26:58,960 --> 11:27:04,240
example, here it creates a link element
13436
11:27:02,000 --> 11:27:07,116
and it gave it a class name that is
13437
11:27:04,240 --> 11:27:09,916
similar to the other class names.
13438
11:27:07,116 --> 11:27:12,560
However, when using AI, we may need to
13439
11:27:09,916 --> 11:27:16,000
go in and modify things to make sure we
13440
11:27:12,560 --> 11:27:19,276
get exactly what we want. So, here the
13441
11:27:16,000 --> 11:27:21,520
AI created a link to the homepage, but
13442
11:27:19,276 --> 11:27:24,800
what we actually want is a link to a
13443
11:27:21,520 --> 11:27:29,916
login page. So, I can click here and
13444
11:27:24,800 --> 11:27:34,080
modify the URL path to slash login.
13445
11:27:29,916 --> 11:27:37,596
And I can also change the text to login.
13446
11:27:34,080 --> 11:27:39,596
And I'll change this to login. And I
13447
11:27:37,596 --> 11:27:42,640
don't really have an icon for the login
13448
11:27:39,596 --> 11:27:45,916
page. So we can delete it.
13449
11:27:42,640 --> 11:27:48,480
And I'll change this to login.
13450
11:27:45,916 --> 11:27:50,480
So using AI, I'm able to write this code
13451
11:27:48,480 --> 11:27:53,520
a lot faster.
13452
11:27:50,480 --> 11:27:55,756
Another feature of AI is we can give the
13453
11:27:53,520 --> 11:27:58,240
AI instructions.
13454
11:27:55,756 --> 11:28:00,560
For example, let's remove this code for
13455
11:27:58,240 --> 11:28:04,240
now.
13456
11:28:00,560 --> 11:28:07,756
And we can click this copilot icon.
13457
11:28:04,240 --> 11:28:11,040
and it will open this chat window. And
13458
11:28:07,756 --> 11:28:14,400
inside here we can just tell the AI what
13459
11:28:11,040 --> 11:28:18,720
we want to do. For example, in here we
13460
11:28:14,400 --> 11:28:24,960
can say in the header
13461
11:28:18,720 --> 11:28:28,080
add a link to the login page and make
13462
11:28:24,960 --> 11:28:30,320
sure in this dropdown you have agent or
13463
11:28:28,080 --> 11:28:33,276
edit selected
13464
11:28:30,320 --> 11:28:38,320
and we'll press enter.
13465
11:28:33,276 --> 11:28:40,640
and the AI will make edits to our code.
13466
11:28:38,320 --> 11:28:44,000
So here you can see that it added a
13467
11:28:40,640 --> 11:28:47,040
login link like before. So we can press
13468
11:28:44,000 --> 11:28:49,520
keep to keep these changes or undo if we
13469
11:28:47,040 --> 11:28:54,240
don't like these changes. So this looks
13470
11:28:49,520 --> 11:28:56,640
good to me. So I'm going to press keep.
13471
11:28:54,240 --> 11:29:00,240
And again I'll go in here and modify it
13472
11:28:56,640 --> 11:29:02,880
a bit. So here it added the login link
13473
11:29:00,240 --> 11:29:05,680
after the orders link but I want to put
13474
11:29:02,880 --> 11:29:09,756
this before the orders. So I can just
13475
11:29:05,680 --> 11:29:12,080
select this code and cut
13476
11:29:09,756 --> 11:29:15,880
and at the top
13477
11:29:12,080 --> 11:29:15,880
paste that code.
13478
11:29:16,400 --> 11:29:22,640
So, as you can see, by using AI, we can
13479
11:29:19,596 --> 11:29:24,800
write React code faster. But you'll
13480
11:29:22,640 --> 11:29:27,680
notice that we still need to understand
13481
11:29:24,800 --> 11:29:30,800
the code to check if it's what we want
13482
11:29:27,680 --> 11:29:33,200
and to modify the code if we need to.
13483
11:29:30,800 --> 11:29:36,640
So, it's important to learn how to code
13484
11:29:33,200 --> 11:29:40,400
first and then we can use AI to help us
13485
11:29:36,640 --> 11:29:43,756
code faster. So now I'll go into the git
13486
11:29:40,400 --> 11:29:46,560
section and I'm actually going to undo
13487
11:29:43,756 --> 11:29:49,560
these changes because this was just an
13488
11:29:46,560 --> 11:29:49,560
example.
13489
11:29:50,240 --> 11:29:54,800
And that's the end of this lesson. In
13490
11:29:52,720 --> 11:29:58,160
this lesson, we learned some of the
13491
11:29:54,800 --> 11:30:00,800
updates in the new version of React. We
13492
11:29:58,160 --> 11:30:03,520
learned how to set up the React compiler
13493
11:30:00,800 --> 11:30:07,040
to automatically optimize our React
13494
11:30:03,520 --> 11:30:10,800
code. We set up a new Typescript React
13495
11:30:07,040 --> 11:30:13,360
project using create vit. We did an
13496
11:30:10,800 --> 11:30:15,596
intro to TypeScript and learned basic
13497
11:30:13,360 --> 11:30:18,720
features like type checking and type
13498
11:30:15,596 --> 11:30:21,116
inference. We converted our e-commerce
13499
11:30:18,720 --> 11:30:23,436
project to TypeScript.
13500
11:30:21,116 --> 11:30:27,276
We learned how to use TypeScript with
13501
11:30:23,436 --> 11:30:32,080
React. And we learned how to use AI with
13502
11:30:27,276 --> 11:30:34,320
React to help us write code faster. Here
13503
11:30:32,080 --> 11:30:38,596
are some exercises to help you practice
13504
11:30:34,320 --> 11:30:38,596
what we learned in this lesson.
13505
11:31:09,596 --> 11:31:13,596
Good job on finishing this React
13506
11:31:11,596 --> 11:31:15,680
tutorial. If you want to continue
13507
11:31:13,596 --> 11:31:17,916
learning with me, you can check out the
13508
11:31:15,680 --> 11:31:19,680
premium version of this course where I
13509
11:31:17,916 --> 11:31:22,320
will be adding extra lessons and
13510
11:31:19,680 --> 11:31:24,800
projects in the future. Currently, the
13511
11:31:22,320 --> 11:31:27,276
premium version has 11 lessons, but I'll
13512
11:31:24,800 --> 11:31:29,200
be adding more lessons in the future.
13513
11:31:27,276 --> 11:31:33,200
You can also check out the membership
13514
11:31:29,200 --> 11:31:36,080
for extra content on AI and backend.
13515
11:31:33,200 --> 11:31:38,240
As the next step after learning React, I
13516
11:31:36,080 --> 11:31:41,520
recommend moving to the back end and
13517
11:31:38,240 --> 11:31:43,360
learning the command line and NodeJS.
13518
11:31:41,520 --> 11:31:45,680
I'll be putting my work in progress on
13519
11:31:43,360 --> 11:31:49,040
these technologies inside the membership
13520
11:31:45,680 --> 11:31:50,800
first, so be on the lookout for that.
13521
11:31:49,040 --> 11:31:52,880
Thanks so much for watching and taking
13522
11:31:50,800 --> 11:31:56,276
this course with me and I'll see you in
13523
11:31:52,880 --> 11:31:56,276
the next one.
943726
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.