YouTube2Text

YouTube transcript (TtPXvEcE11E) — Transcript

85,918 words · 13,523 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Welcome to the React full course. In
  2. 0:03this course, we'll learn how to build
  3. 0:04complex websites with React. And by the
  4. 0:07end of this course, we'll build two
  5. 0:09projects, a chatbot and an e-commerce
  6. 0:12project.
  7. 0:14We'll start from the very basics of
  8. 0:16React like how to set up React with
  9. 0:19JavaScript and we'll learn each feature
  10. 0:21in detail step by step. We'll learn the
  11. 0:24major features of React as well as
  12. 0:27advanced features like how to use React
  13. 0:30with a backend, deploy React to the
  14. 0:33internet with AWS,
  15. 0:35React with TypeScript, and React with
  16. 0:38AI.
  17. 0:40You can find the sections of this course
  18. 0:43below the video here and here.
  19. 0:47You can also change the speed of the
  20. 0:49video here.
  21. 0:53And you can find subtitles for different
  22. 0:55languages here.
  23. 0:59And after each lesson, I'll give you a
  24. 1:01bunch of exercises that you can do on
  25. 1:03your own to practice the skills that we
  26. 1:06learned. In total, this course has over
  27. 1:10125 exercises.
  28. 1:12So, with that being said, let's get
  29. 1:14started.
  30. 1:25I'm going to be doing this course on
  31. 1:26Windows, but the steps are the same if
  32. 1:28you're using a Mac. Before we begin,
  33. 1:31we're going to need two pieces of
  34. 1:33software. Number one is a web browser,
  35. 1:36and number two is a code editor. The
  36. 1:40most popular web browser for web
  37. 1:41development is Google Chrome and the
  38. 1:44most popular code editor for web
  39. 1:45development is VS Code. So I recommend
  40. 1:49using these. If you need to install
  41. 1:51these, you can search for them on
  42. 1:53Google.
  43. 1:55Click the link and follow the
  44. 1:57instructions to download and install.
  45. 2:02Now you'll also need to know the basics
  46. 2:05of JavaScript.
  47. 2:07React is built on JavaScript and we're
  48. 2:09going to be writing a lot of JavaScript
  49. 2:11when using React. Don't worry, you don't
  50. 2:14need to be an expert in JavaScript. You
  51. 2:16just need to know some basic features
  52. 2:18like the ones listed here. And I'll also
  53. 2:21try to review JavaScript features as we
  54. 2:23go through this course. If you want to
  55. 2:26learn JavaScript in more detail, you can
  56. 2:28check out my JavaScript full course in
  57. 2:31the video description.
  58. 2:34Finally, you'll need to know the basics
  59. 2:36of HTML.
  60. 2:38When using React, we're going to be
  61. 2:40writing a lot of HTML.
  62. 2:43Again, don't worry. You don't need to be
  63. 2:45an expert in HTML. You just need to know
  64. 2:48some basic features like the ones listed
  65. 2:50here. And I'll try my best to review
  66. 2:53HTML features as we go through this
  67. 2:56course.
  68. 2:57If you want to learn HTML in more
  69. 2:59detail, you can check out my HTML and
  70. 3:02CSS full course in the video
  71. 3:04description.
  72. 3:06If you have any issues in this course,
  73. 3:08you can check the troubleshooting guide
  74. 3:10in the description.
  75. 3:13Now that we have all the requirements
  76. 3:15that we need, we're ready to learn
  77. 3:17React.
  78. 3:21On our computer, we're going to go to
  79. 3:22our desktop and we're going to create a
  80. 3:25new folder. So here I'm going to
  81. 3:28rightclick and create a new folder.
  82. 3:33And this folder is going to contain all
  83. 3:35the code that we write in this course.
  84. 3:38So let's name it react dashc course and
  85. 3:42press enter.
  86. 3:45Next we're going to open this folder in
  87. 3:47our code editor VS Code. So, we'll open
  88. 3:51VS Code
  89. 3:53and then click File,
  90. 3:56Open Folder
  91. 3:58and select the folder we just created.
  92. 4:01So, I created my folder on the desktop
  93. 4:04and it's this folder right here. So,
  94. 4:06we'll select this folder and then click
  95. 4:09select folder
  96. 4:12and we can close this welcome page.
  97. 4:15So, now let's create a new file to learn
  98. 4:18React. So up here, we're going to create
  99. 4:20a new file and we're going to name it
  100. 4:24react-basics.html
  101. 4:28and press enter.
  102. 4:32As a review, a file that ends inh
  103. 4:35contains HTML code and this creates a
  104. 4:39website.
  105. 4:42Next, we're going to create a website
  106. 4:44and we're going to set up React on this
  107. 4:46website. Now, to save time, we're
  108. 4:49actually going to download some starting
  109. 4:51code.
  110. 4:54So, we're going to open our web browser,
  111. 4:56Google Chrome.
  112. 4:58And at the top, we're going to type
  113. 5:01super simple.dev/react-basics
  114. 5:05[Music]
  115. 5:07and press enter.
  116. 5:10So, this page has some starting HTML
  117. 5:13code we can use to learn React. So now
  118. 5:16let's click on this page and we're going
  119. 5:18to select all this code by pressing
  120. 5:20control A on Windows or command A on Mac
  121. 5:24and we're going to copy paste this code
  122. 5:27into our file. So we'll right click
  123. 5:30copy. We can also press control C on
  124. 5:34Windows or command C on Mac
  125. 5:39and then open our code editor and then
  126. 5:42inside this HTML file we're going to
  127. 5:44rightclick and paste. We can also press
  128. 5:48control +VV on Windows or commandV on
  129. 5:51Mac.
  130. 5:54And finally let's save this file by
  131. 5:57pressing control S on Windows or command
  132. 6:00S on Mac.
  133. 6:03Next, we're going to open this file in
  134. 6:05our web browser to see what the website
  135. 6:08looks like. To make this easier, we're
  136. 6:11going to use a VS Code extension called
  137. 6:13Live Server. So, the benefit of live
  138. 6:16server is it will automatically refresh
  139. 6:19the web page whenever we change some
  140. 6:21code. So, it's going to make our coding
  141. 6:23easier.
  142. 6:24If you don't have live server installed,
  143. 6:27you can click the extension section and
  144. 6:30then search for live server at the top
  145. 6:35and then click install.
  146. 6:39Once liveer is installed, we're going to
  147. 6:42go back to our code and we're going to
  148. 6:44open this file in live server by
  149. 6:46rightclicking in the file and click open
  150. 6:50with live server.
  151. 6:55So live server will open the HTML file
  152. 6:58using our browser and it's going to
  153. 7:00create a website and we should see the
  154. 7:03message welcome to super simple dev
  155. 7:05react course on this website.
  156. 7:09So this message is displayed by React
  157. 7:12and this means that we successfully set
  158. 7:14up React on this website.
  159. 7:18Now, let's go back to VS Code.
  160. 7:22And this is the minimum amount of code
  161. 7:24that we need to set up React. We're
  162. 7:28going to go through this code step by
  163. 7:29step to make sure that you understand
  164. 7:32everything here.
  165. 7:35First of all, what is React? React is an
  166. 7:39external library that helps us create
  167. 7:41websites easier. Now, there are two
  168. 7:44parts to this statement. Number one,
  169. 7:46React is an external library. And number
  170. 7:50two, React helps us create websites
  171. 7:52easier. Let's go through these one by
  172. 7:55one. So number one, React is an external
  173. 7:58library. First, let's review what an
  174. 8:02external library is. So here we have a
  175. 8:05bunch of HTML code. An HTML code creates
  176. 8:09a website. And now, how do we load
  177. 8:12JavaScript onto our website? There are
  178. 8:15several ways to load JavaScript on a
  179. 8:17website. One way is to use an HTML
  180. 8:20element called a script element.
  181. 8:23For example, under this div element,
  182. 8:26let's create a script element using less
  183. 8:29than script greater than and less
  184. 8:33than/cript greater than. But our code
  185. 8:35editor might autocomplete this for us.
  186. 8:38Now, inside this script element, we can
  187. 8:41write some JavaScript code. For example,
  188. 8:44console.log
  189. 8:46the string hello.
  190. 8:49As a review, this code displays the text
  191. 8:52hello in the console. So now let's save
  192. 8:58and we're going to open the console in
  193. 8:59our browser. So we'll open Google Chrome
  194. 9:04and Liveer is going to automatically
  195. 9:06refresh the page and run that JavaScript
  196. 9:08code. And now we're going to check the
  197. 9:10console by right-clicking and then click
  198. 9:13inspect
  199. 9:15and open the console tab.
  200. 9:18And you'll see that inside the console
  201. 9:20we displayed the text hello. And that's
  202. 9:24exactly what this code does. So we
  203. 9:26successfully loaded this code onto our
  204. 9:29website.
  205. 9:31We'll go back to VS Code. So this is one
  206. 9:35way to run JavaScript code on a website.
  207. 9:38First, we create a script element and
  208. 9:40then we put our JavaScript code inside.
  209. 9:44Another way to run JavaScript is to save
  210. 9:46our JavaScript code in a file. So on the
  211. 9:49left, let's create a new file
  212. 9:53and we'll name it React-basics.js
  213. 9:57and press enter.
  214. 10:00So when we end a file with js, this file
  215. 10:03contains JavaScript code. So now let's
  216. 10:05move that code into this file. So we'll
  217. 10:08go to our HTML file. We're going to
  218. 10:11select this code and then rightclick
  219. 10:15cut. We can also press controlX on
  220. 10:18Windows or command X on Mac.
  221. 10:23And inside our JavaScript file, we're
  222. 10:26going to rightclick and paste
  223. 10:30and save this file.
  224. 10:33Now to run the code inside this file,
  225. 10:36we're going to go back to our HTML
  226. 10:40and we're going to add an HTML attribute
  227. 10:42to this script element. So after this
  228. 10:45script, we're going to type space and
  229. 10:48then type src equals double quotes. So
  230. 10:53the src attribute lets us load
  231. 10:56JavaScript from a file.
  232. 10:58Inside these double quotes, we're going
  233. 11:00to tell the computer which JavaScript
  234. 11:02file to load. So we'll type
  235. 11:05react-basics.js.
  236. 11:10So this will look for a file called
  237. 11:12react-basics.js
  238. 11:14beside the current HTML file. So it's
  239. 11:18going to find the file here, and it's
  240. 11:20going to run all the code inside this
  241. 11:22file. So now let's save
  242. 11:25and open our browser and check the
  243. 11:27result.
  244. 11:29Live server will automatically refresh
  245. 11:32the page. And inside the console you can
  246. 11:34see that we display the message hello.
  247. 11:37So we're successfully loading code from
  248. 11:40inside a file.
  249. 11:43We'll go back to VS Code.
  250. 11:46And this is a second way to load
  251. 11:48JavaScript. We save our JavaScript in a
  252. 11:50file and then we load it from the file.
  253. 11:55Next, the script element can actually
  254. 11:57load code from anywhere on the internet,
  255. 12:01not just on our computer. For example,
  256. 12:04we're going to open Google Chrome. And
  257. 12:06then we're going to create a new tab.
  258. 12:09And at the top we're going to type
  259. 12:12unpkg.com/sups
  260. 12:17simpledev/external
  261. 12:22dash library.js
  262. 12:26and press enter.
  263. 12:30So on this website we have some
  264. 12:32JavaScript code. So this is just normal
  265. 12:35JavaScript code. it displays a message.
  266. 12:38This is an external library inside the
  267. 12:40console. So using the script element, we
  268. 12:44can actually take this code from this
  269. 12:46website and run it on our own website.
  270. 12:50To do that, we're going to open VS Code
  271. 12:54and inside the src attribute, instead of
  272. 12:58loading the JavaScript code from a file,
  273. 13:00we're going to load the JavaScript code
  274. 13:02from a website. So, first we need the
  275. 13:05URL of the website. So, we'll go back to
  276. 13:08Google Chrome and at the top this is a
  277. 13:11URL and we're going to copy
  278. 13:15and VS Code we're going to remove this
  279. 13:19file and we're going to paste the URL.
  280. 13:24And now let's save.
  281. 13:27So now let's go back to our website and
  282. 13:30we're going to go to the tab for our
  283. 13:32website.
  284. 13:33And we're going to check the console.
  285. 13:36And inside the console, you can see we
  286. 13:38displayed the message, this is an
  287. 13:40external library.
  288. 13:42So we successfully loaded JavaScript
  289. 13:44code from another website.
  290. 13:47Let's go back to VS Code.
  291. 13:50So using the script element, we can load
  292. 13:53JavaScript code from anywhere on the
  293. 13:55internet. So this code is called an
  294. 13:59external library. An external library is
  295. 14:03just code that is outside of our
  296. 14:05computer. It's code that someone else
  297. 14:08wrote and we can load that code into our
  298. 14:10website using the script element.
  299. 14:15Now, React is an external library. So,
  300. 14:19React is just a bunch of code that is
  301. 14:21outside our computer that someone else
  302. 14:23wrote and we can load this code on our
  303. 14:26website and use it.
  304. 14:29So inside our starting file at the
  305. 14:32bottom here, these two script elements
  306. 14:35load the React external library. So
  307. 14:38loads React.js
  308. 14:40and React-dom.js.
  309. 14:44So let's open these URLs in our browser
  310. 14:46and take a look. So we're going to
  311. 14:49select this URL and we're going to copy.
  312. 14:54And inside Google Chrome, we're going to
  313. 14:56create a new tab. And at the top, we're
  314. 14:59going to paste the URL and press enter.
  315. 15:04And on this website, you can see that we
  316. 15:06have a bunch of JavaScript code. So,
  317. 15:09this is the code for React. We're just
  318. 15:13taking all of this code and then loading
  319. 15:15it onto our website.
  320. 15:18Let's go back to VS Code. And the same
  321. 15:21thing applies to this second URL. If we
  322. 15:25select it and then copy
  323. 15:29and in our browser create a new tab and
  324. 15:33then at the top paste
  325. 15:36and press enter.
  326. 15:39You can see that this website also has a
  327. 15:41bunch of JavaScript code
  328. 15:44and this is also the code for React. So,
  329. 15:48as you can see, React is an external
  330. 15:51library. It's just a bunch of code that
  331. 15:54is outside of our computer that someone
  332. 15:56else wrote. And we can load this code on
  333. 15:59our website and use it.
  334. 16:03Now, before we move on, there's two
  335. 16:05things you might be wondering about.
  336. 16:08Number one, why are we loading React
  337. 16:10from Super Simple Dev instead of some
  338. 16:13official React URL? So, this just lets
  339. 16:17me control the version of React. If we
  340. 16:20loaded React from the official URL, you
  341. 16:22might get a different version of React
  342. 16:24than the one I'm using in this video,
  343. 16:26which might cause issues. So, for the
  344. 16:29video, we're going to use a super simple
  345. 16:32dev URL to make sure that we have the
  346. 16:34same version of React and you're able to
  347. 16:37follow along.
  348. 16:39The second thing you might be wondering
  349. 16:41is why are there two external libraries
  350. 16:44for React React and React DOM. So React
  351. 16:49is designed to be used in different
  352. 16:50places like websites and also mobile
  353. 16:53apps. So the creators of React decided
  354. 16:56to divide it into two external
  355. 16:59libraries. React which contains all the
  356. 17:02shared features that are used in both
  357. 17:04websites and mobile apps and React DOM
  358. 17:08which contain features that are specific
  359. 17:10to websites. So if we're using React to
  360. 17:13create websites, we need to load the
  361. 17:16React external library and the React DOM
  362. 17:20external library. If we were using React
  363. 17:23to create mobile apps, we would load the
  364. 17:25React external library and a different
  365. 17:28library called React Native.
  366. 17:32In this course, we're going to use React
  367. 17:34to create websites. So, we're going to
  368. 17:36load React and React DOM.
  369. 17:41Okay, so we learned that React is an
  370. 17:44external library. It's just a bunch of
  371. 17:46code that is outside of our computer and
  372. 17:48we're going to load it onto our website.
  373. 17:51The next part is React helps us create
  374. 17:54websites easier. So now let's learn how
  375. 17:57to use React.
  376. 18:00We'll go back to VS Code
  377. 18:04and we're going to scroll down and these
  378. 18:07two lines of JavaScript at the bottom
  379. 18:10sets up React. So let's go through this
  380. 18:13step by step. In the first line, we're
  381. 18:16using document.query query selector to
  382. 18:19get an HTML element from the website and
  383. 18:22put it into our JavaScript. As a review,
  384. 18:26this is a feature of JavaScript called
  385. 18:27the DOM or the document object model.
  386. 18:31Basically, JavaScript gives us this
  387. 18:33document object which sort of combines
  388. 18:36JavaScript and HTML together. It lets us
  389. 18:39get HTML elements from the page and put
  390. 18:42them into our JavaScript and it also
  391. 18:45lets our JavaScript modify HTML elements
  392. 18:48on the page. So the DOM or the document
  393. 18:52object sort of combines HTML and
  394. 18:55JavaScript together and it lets the two
  395. 18:57languages interact with each other.
  396. 19:00Don't worry, you don't need to know a
  397. 19:02lot about the DOM to learn React. I'm
  398. 19:05just explaining what this code does so
  399. 19:07you understand what's going on here.
  400. 19:10So the code document.query selector lets
  401. 19:14us get an HTML element from the page and
  402. 19:17put it into our JavaScript.
  403. 19:20Inside these brackets, we tell the
  404. 19:22computer which HTML element we want to
  405. 19:25get from the page. So if we start with a
  406. 19:28dot, this means we're going to look for
  407. 19:29a class. And here we're looking for the
  408. 19:32class js-container.
  409. 19:35Up here, this div element has the class
  410. 19:39js-container.
  411. 19:40So, it's going to take this element and
  412. 19:42put it into our JavaScript. And finally,
  413. 19:45we're going to save it inside a variable
  414. 19:48so we can use it later.
  415. 19:51Next, let's go to the second line of
  416. 19:53code and react.create
  417. 19:57routt basically sets up React
  418. 20:01To set up React, we have to give it an
  419. 20:04HTML element. So, we're going to give it
  420. 20:06the element that we saved up here, which
  421. 20:08is this div element. So, basically the
  422. 20:11way this works is that this element is
  423. 20:14going to act as a container.
  424. 20:17Everything that we create with React is
  425. 20:19going to be displayed inside this
  426. 20:21element or inside this container. The
  427. 20:25reason React uses a container is because
  428. 20:28it keeps things organized.
  429. 20:30Everything inside this element will be
  430. 20:32controlled by React and React will not
  431. 20:35affect anything outside of this element.
  432. 20:38So it keeps things isolated and
  433. 20:40organized.
  434. 20:43So after we set up React with a
  435. 20:45container, we can start using the
  436. 20:48features of React.
  437. 20:52One feature of React is render. Render
  438. 20:56means to display something. So render
  439. 21:00lets us display something inside this
  440. 21:02container or inside this element. Now
  441. 21:06between the brackets of render, we're
  442. 21:09going to tell React what to display
  443. 21:11inside the container. So right now we
  444. 21:14told it to display the text welcome to
  445. 21:17super simple dev react course. And if we
  446. 21:21go back to our website and open the tab
  447. 21:24for our website
  448. 21:26and after we open the console, this time
  449. 21:28we're going to go to the elements tab.
  450. 21:32So this lets us see the HTML that's
  451. 21:34currently on the page. We're going to
  452. 21:36open this body element and inside we can
  453. 21:40see our container and you can see that
  454. 21:42React rendered or displayed the message
  455. 21:46welcome to super simple dev react course
  456. 21:49inside our container. So we'll go back
  457. 21:52to our code
  458. 21:54and that's how we set up and use React.
  459. 21:57First we give it a container and then we
  460. 22:00can display things like a message inside
  461. 22:03this container.
  462. 22:06Before we continue, let's review the
  463. 22:09rest of the code in this file to make
  464. 22:11sure that we understand every line of
  465. 22:13code. So, the HTML at the top here
  466. 22:18as well as the bottom here.
  467. 22:25This HTML is called the HTML structure.
  468. 22:30As a review, this is a standard
  469. 22:32structure that we follow when writing
  470. 22:34HTML.
  471. 22:36At the top, dock type HTML tells a
  472. 22:39computer to use a modern version of
  473. 22:42HTML.
  474. 22:44The HTML element represents a website.
  475. 22:47So all the code for our website should
  476. 22:50go inside the HTML element. Now every
  477. 22:53website is split into two sections. the
  478. 22:56head section and the body section. The
  479. 23:00head section generally contains things
  480. 23:02that are not visible on the page. For
  481. 23:05example, the title element sets the text
  482. 23:08at the top of the tab. So, this is not
  483. 23:11visible on the page. So, we put this
  484. 23:13inside the head.
  485. 23:16The body section generally contains
  486. 23:18things that are visible on the page. For
  487. 23:21example, the div element will be visible
  488. 23:24on the page. So we put it inside the
  489. 23:27body.
  490. 23:29Finally, we usually put our script
  491. 23:31elements at the bottom of the body
  492. 23:33because we want our HTML to be created
  493. 23:36first and then our JavaScript can modify
  494. 23:39the HTML.
  495. 23:41So that's a review of the HTML
  496. 23:43structure. This is a standard structure
  497. 23:46that we follow when writing HTML.
  498. 23:51So now we understand almost every line
  499. 23:54of code in this file. This is just the
  500. 23:57HTML structure that we have to follow.
  501. 24:00This is a container that we give to
  502. 24:02React to keep things isolated and
  503. 24:05organized.
  504. 24:06This is an example of an external
  505. 24:08library and this loads the React
  506. 24:12external library or loads the code for
  507. 24:14React.
  508. 24:17And this sets up and uses React.
  509. 24:21The last two lines that we haven't
  510. 24:23learned yet are these two lines right
  511. 24:26here. So here we're loading another
  512. 24:29external library. And this external
  513. 24:32library is called Babel.
  514. 24:34So again if I open this URL in my
  515. 24:37browser copy
  516. 24:41and in my browser going to create a new
  517. 24:44tab and at the top paste and press
  518. 24:48enter.
  519. 24:50You'll see that this website contains a
  520. 24:52bunch of JavaScript code and this is the
  521. 24:55code for another external library called
  522. 24:58Babel.
  523. 25:00So we'll go back to VS Code and let's
  524. 25:03learn what is the Babel external
  525. 25:05library.
  526. 25:07Babel is known as a JavaScript compiler.
  527. 25:10So it basically translates other
  528. 25:12languages into JavaScript.
  529. 25:17So why do we need Babel? When using
  530. 25:20React, we actually don't use normal
  531. 25:22JavaScript. We use an enhanced version
  532. 25:26of JavaScript called JSX.
  533. 25:29JSX stands for JavaScript XML. It's the
  534. 25:33same as JavaScript, but we can also
  535. 25:35write HTML directly in our JavaScript
  536. 25:38code. So, it's just JavaScript with
  537. 25:41extra features. So, let's do an example
  538. 25:44of JSX to learn how to use it. Inside
  539. 25:48the script element at the bottom, we're
  540. 25:51going to create a new variable using
  541. 25:54const and let's call it button
  542. 25:58equals. And now let's create a button
  543. 26:01HTML element. So here we're going to
  544. 26:05type HTML directly less than button
  545. 26:09greater than and then less than /button
  546. 26:12greater than.
  547. 26:14So this is actually HTML code and we're
  548. 26:18writing it directly inside our
  549. 26:20JavaScript. So this HTML code creates a
  550. 26:24button and then inside our button we can
  551. 26:27put the text hello.
  552. 26:30So this code here is actually using JSX.
  553. 26:34So JSX works the same way as JavaScript
  554. 26:37except we can also write HTML directly
  555. 26:40in our JavaScript code.
  556. 26:43So, what does this code do? It's
  557. 26:46actually pretty straightforward. This
  558. 26:48code creates a new button element inside
  559. 26:52our JavaScript and then we save it in a
  560. 26:55variable.
  561. 26:57We can actually do this with normal
  562. 26:59JavaScript as well. We can use the DOM
  563. 27:02and we can create a button element in
  564. 27:04our JavaScript like this. So you can
  565. 27:08think of this JSX code as a shortcut for
  566. 27:11this JavaScript code.
  567. 27:14So that's JSX. JSX is an enhanced
  568. 27:18version of JavaScript. So it's the same
  569. 27:20as JavaScript, but we can also write
  570. 27:23HTML directly in our JavaScript code. So
  571. 27:27when using React, we usually use JSX
  572. 27:30instead of normal JavaScript because, as
  573. 27:33you can see, JSX is simpler.
  574. 27:38Now there is one problem with JSX which
  575. 27:41is our web browser doesn't understand
  576. 27:44JSX. It only understands normal
  577. 27:47JavaScript.
  578. 27:49In order to use JSX we need to translate
  579. 27:52it into normal JavaScript first and then
  580. 27:56our web browser can understand this
  581. 27:58code.
  582. 28:00To do that we use the Babel external
  583. 28:03library. Babel translates JSX into
  584. 28:07normal JavaScript so our web browser can
  585. 28:10understand it. To use Babel, we need to
  586. 28:13do two steps. First, we load the Babel
  587. 28:17external library, which is what we do
  588. 28:19here. And then when we want to translate
  589. 28:23something into JavaScript, we have to
  590. 28:25give the script element an attribute
  591. 28:28type equals text/babel.
  592. 28:31So this attribute tells Babel to
  593. 28:34translate all the code inside here into
  594. 28:37normal JavaScript.
  595. 28:39So that's what these two lines of code
  596. 28:41do. First, we load the Babel external
  597. 28:44library and then we tell Babel to
  598. 28:47translate the code from JSX into normal
  599. 28:50JavaScript so our web browser can
  600. 28:52understand it. So now we understand all
  601. 28:56of the code in our starting file. And
  602. 28:59again, this is the minimum amount of
  603. 29:02code that we need to set up React.
  604. 29:06Next, let's learn some more features of
  605. 29:08React. So, we'll scroll back down. And
  606. 29:12currently, we're using React to display
  607. 29:15some text inside this container. Now, in
  608. 29:19addition to text, we can also display
  609. 29:21HTML elements inside this container. So
  610. 29:25to do that, we're going to remove this
  611. 29:27string
  612. 29:29and instead we're going to give render
  613. 29:31an HTML element.
  614. 29:34So up here, we actually created an HTML
  615. 29:37element or a button. So we're going to
  616. 29:39give this to the render function. So
  617. 29:42here we're going to give it the button.
  618. 29:46And now it's going to display this
  619. 29:47button inside our container or inside
  620. 29:50this div here. Let's save and we'll open
  621. 29:55the browser
  622. 29:57and open the tab for our website. And
  623. 30:00you'll see it displayed the button with
  624. 30:02the text hello on the website.
  625. 30:05And if we check the elements tab, you
  626. 30:08can see that React displayed the button
  627. 30:10inside our container.
  628. 30:13We'll go back to our code.
  629. 30:15So that's what this code does. It
  630. 30:18displays an element inside this
  631. 30:20container.
  632. 30:23Now let's do another example. Let's
  633. 30:25display a different HTML element inside
  634. 30:28this container. So below this button,
  635. 30:32let's use JSX to create a paragraph
  636. 30:35element. So first we'll create a new
  637. 30:37variable using const and we'll name the
  638. 30:40variable paragraph
  639. 30:43equals and we'll create a paragraph
  640. 30:46element using the code less than p
  641. 30:50greater than less than /p greater than.
  642. 30:54So this html code creates a paragraph or
  643. 30:58a paragraph of text. Inside our
  644. 31:01paragraph, let's just put paragraph of
  645. 31:05text.
  646. 31:06And now let's display this paragraph on
  647. 31:09our website. So inside render, instead
  648. 31:12of the button, we're going to render the
  649. 31:15paragraph
  650. 31:17and save.
  651. 31:20If we open our website,
  652. 31:22you can see it displayed a paragraph of
  653. 31:25text. If we go into the elements tab and
  654. 31:28we open this div container,
  655. 31:31we can see that React displayed this
  656. 31:33paragraph element inside this container.
  657. 31:37So let's go back to our code. And that's
  658. 31:40basically how we use React. We give
  659. 31:43React a container and then we can
  660. 31:45display an HTML element inside this
  661. 31:48container.
  662. 31:51Next, we're currently using React to
  663. 31:53display one element at a time on our
  664. 31:56website, which is not that useful. So
  665. 31:58now we're going to learn how to use
  666. 32:00React to display multiple elements on
  667. 32:02our website. So one thing we can't do is
  668. 32:06give render multiple elements. So this
  669. 32:09is not supported. We also can't save
  670. 32:12multiple elements inside a variable.
  671. 32:15This is also not supported. Instead, we
  672. 32:18need to use another HTML element called
  673. 32:21a div element. So, under our paragraph,
  674. 32:24let's create a new variable using const
  675. 32:28and let's name it div and make it equal.
  676. 32:32And we'll create an HTML div element
  677. 32:35using the code less than div greater
  678. 32:38than less than /d greater than.
  679. 32:42So this is the HTML code to create a div
  680. 32:46element. As a review, the div element
  681. 32:49represents a container. The purpose of a
  682. 32:52div element is to contain other elements
  683. 32:55like buttons and paragraphs. So that's
  684. 32:58actually why we used a div element up
  685. 33:01here. This element is a container. It's
  686. 33:04going to contain everything that we
  687. 33:05create using React. So let's go back
  688. 33:08down.
  689. 33:10Inside our div element, we can put other
  690. 33:13HTML elements like a button. So, let's
  691. 33:17actually select this code and we'll copy
  692. 33:22and then inside this div, paste.
  693. 33:27And let's also put a paragraph element
  694. 33:29inside our div. So we'll select this
  695. 33:32code for the paragraph element and we'll
  696. 33:35copy
  697. 33:37and our div paste.
  698. 33:42So this code works because a div is
  699. 33:45considered as one element. So we're just
  700. 33:47taking one element and saving it in one
  701. 33:50variable which is supported in
  702. 33:52JavaScript. The only thing special about
  703. 33:54this is that this one element can
  704. 33:57contain other elements inside.
  705. 34:00Next, we're going to give this div to
  706. 34:02the render function here. So, instead of
  707. 34:05the paragraph, we're going to give
  708. 34:07render our div and save.
  709. 34:12And if we check our website,
  710. 34:15you can see that React displayed
  711. 34:17multiple elements on the website, a
  712. 34:20button and a paragraph. If we look at
  713. 34:23the elements tab and open this div
  714. 34:25container,
  715. 34:28you can see that React displayed our div
  716. 34:30inside this container.
  717. 34:33Let's go back to our code. And that's
  718. 34:36how we display multiple elements on a
  719. 34:38website using React.
  720. 34:41First, we group the elements inside a
  721. 34:43div element. And then we render the div
  722. 34:46element.
  723. 34:49Before we continue, we're going to do
  724. 34:51some code cleanup. When we have multiple
  725. 34:54lines of HTML like this, a best practice
  726. 34:57is to add brackets around it. So, at the
  727. 35:00front, we're going to add an open
  728. 35:02bracket and at the end, a closed
  729. 35:05bracket.
  730. 35:06And we're going to put the HTML on its
  731. 35:08own line. So, here we have JavaScript
  732. 35:11and HTML together. We're going to add a
  733. 35:14new line here by pressing enter. and
  734. 35:17we're going to put the HTML on its own
  735. 35:19line. Same thing here. Click here and
  736. 35:22press enter to put the HTML on its own
  737. 35:26line.
  738. 35:28Next, we have two elements that are
  739. 35:30inside a div. So, we're also going to
  740. 35:32give this line an indent by pressing
  741. 35:35tab. And then, we're going to give this
  742. 35:37line an indent by pressing tab. So that
  743. 35:41it's easy to see that these two elements
  744. 35:43are inside this div element.
  745. 35:46So this is how we should format our code
  746. 35:48when we have multiple lines of HTML.
  747. 35:52Another way to think about this is
  748. 35:54inside here, you're actually just
  749. 35:56writing HTML normally.
  750. 35:59So don't worry, the more that you use
  751. 36:02React, the more you'll get used to this
  752. 36:04format.
  753. 36:05So let's save this file.
  754. 36:09Now that we learn the basics of React,
  755. 36:11let's talk about how React helps us
  756. 36:14create websites easier.
  757. 36:17If we look at this code, creating a
  758. 36:19website with React feels natural. It's a
  759. 36:23lot simpler than using the DOM feature
  760. 36:25in JavaScript. We can just create the
  761. 36:28HTML elements we want directly and then
  762. 36:31give it to React and React will display
  763. 36:34it on the website.
  764. 36:36Another benefit is JSX lets us find
  765. 36:40errors easier.
  766. 36:42For example, if I misspell button, it
  767. 36:45will actually tell me that these two
  768. 36:47don't match because this is an actual
  769. 36:51element rather than just text or a
  770. 36:53string. So, it makes it easier to find
  771. 36:56errors when we're coding. So, I'll add
  772. 36:59the O back.
  773. 37:02Another benefit is we can insert values
  774. 37:05inside our JSX elements. For example,
  775. 37:09inside this paragraph, we're going to
  776. 37:11type space and we're going to insert a
  777. 37:13value here by typing open curly bracket
  778. 37:16close curly bracket. And inside here, we
  779. 37:20can give it any JavaScript code. For
  780. 37:22example, 2 + 2. So this will calculate 2
  781. 37:27+ 2 and then insert it into this
  782. 37:30element. If we save
  783. 37:34and then check our website,
  784. 37:36we can see that it calculated 2 + 2
  785. 37:39which is four and inserted it into this
  786. 37:42paragraph element. So we'll go back to
  787. 37:45our code and that's another benefit of
  788. 37:48using React. We can insert JavaScript
  789. 37:50code directly into our JSX elements. So
  790. 37:54this is similar to string interpolation
  791. 37:57where we can insert values into a string
  792. 38:01except here we're inserting values into
  793. 38:04actual HTML elements. So those are three
  794. 38:07ways that React helps us create websites
  795. 38:10easier. Number one, it's more natural.
  796. 38:13We can create the elements that we want
  797. 38:15and then just give it to React and it
  798. 38:17will put it on the website. Number two,
  799. 38:20it helps us find errors easier because
  800. 38:22these are actual elements. And number
  801. 38:25three, we can insert our JavaScript code
  802. 38:27into our elements. So this helps us use
  803. 38:30JavaScript and HTML together more
  804. 38:33easily.
  805. 38:35Now, React has many other features that
  806. 38:37helps us create websites easier, but
  807. 38:40we're going to learn those features
  808. 38:41later on as we go through the course.
  809. 38:44The last thing we're going to do is
  810. 38:46we're going to scroll up and we're just
  811. 38:49going to remove this example external
  812. 38:51library
  813. 38:54and save
  814. 38:56so that we just have the code that we
  815. 38:58need for React.
  816. 39:02And that's the end of this lesson. In
  817. 39:04this lesson, we learned that React is an
  818. 39:07external library that helps us create
  819. 39:09websites easier. We learned how to load
  820. 39:12the React external library. We learned
  821. 39:15how to set up React and use render. We
  822. 39:19reviewed the basics of HTML and
  823. 39:22JavaScript.
  824. 39:23We learned JSX, an enhanced version of
  825. 39:27JavaScript.
  826. 39:28We created elements directly with JSX.
  827. 39:32And we learned how to insert values into
  828. 39:35our JSX elements. Here are some
  829. 39:38exercises to help you practice what we
  830. 39:40learned in this lesson. You can find the
  831. 39:43solutions to these exercises in the
  832. 39:45video description.
  833. 40:20In this lesson, we're going to learn two
  834. 40:22of the most important features of React,
  835. 40:25which are components and props. To learn
  836. 40:29this, I actually prepared a project that
  837. 40:31we can do for this lesson. We're going
  838. 40:34to go to our browser
  839. 40:36and we can delete these extra tabs for
  840. 40:39now.
  841. 40:42And we're going to create a new tab.
  842. 40:45And at the top, we're going to go to
  843. 40:48super simple.dev/projects/
  844. 40:54chatbot and press enter.
  845. 40:58This is a chatbot project. We can send a
  846. 41:02message to a robot up here and the robot
  847. 41:05will respond to our message. For
  848. 41:08example, we can click in here and type
  849. 41:11hello chatbot and then press send.
  850. 41:18And the robot will respond with hello,
  851. 41:20how can I help you? Now, if we scroll to
  852. 41:24the bottom of this page, we can also
  853. 41:26move the text box to the bottom by
  854. 41:28clicking this link.
  855. 41:31So, a lot of chatbot applications use
  856. 41:33this design where the text box is at the
  857. 41:36bottom. However, because we're watching
  858. 41:39a video, this text box might get cut off
  859. 41:42or it might be covered by the video
  860. 41:44progress bar. So, just for this video,
  861. 41:47we're going to click this again and
  862. 41:50we're going to start with a text box at
  863. 41:52the top.
  864. 41:53Next, this is a simple chatbot and
  865. 41:57currently it can only do three things.
  866. 41:59get today's date, flip a coin, and roll
  867. 42:02a dice. For example, up here, we can
  868. 42:05type, "Can you get me today's date?"
  869. 42:12and press send.
  870. 42:16And it will respond with today's date.
  871. 42:19And let's try typing, "How about flip a
  872. 42:25coin?" and press send.
  873. 42:30and it will respond with heads or tails.
  874. 42:34Finally, let's type okay, thank you and
  875. 42:39press send.
  876. 42:42And it responds with no problem.
  877. 42:46So, this is a chatbot project we're
  878. 42:49going to work on for the next few
  879. 42:50lessons. Now, this is a simple chatbot
  880. 42:52and it can only do these three things.
  881. 42:55But in the future, we can also switch
  882. 42:57this to a more powerful AI that can
  883. 42:59respond to any message we send.
  884. 43:03But for now, we'll just use this simple
  885. 43:05chatbot to learn React. Now, let's use
  886. 43:09React to build this chatbot project.
  887. 43:12We'll go back to VS Code
  888. 43:15and let's make a copy of React Basics.
  889. 43:19So, first we'll select this file and
  890. 43:21then rightclick,
  891. 43:24copy.
  892. 43:26And inside here, we're going to
  893. 43:28rightclick and paste.
  894. 43:32And let's rename this file. So, we'll
  895. 43:34rightclick and rename
  896. 43:38and rename it to chatbot.html
  897. 43:42and press enter.
  898. 43:46And let's close the React Basics files
  899. 43:48for now.
  900. 43:51So, we're going to start with the same
  901. 43:52code as the React Basics file, except
  902. 43:56we're just going to change the title up
  903. 43:58here. So, we'll remove React Basics and
  904. 44:01just change it to chatbot
  905. 44:05and save.
  906. 44:07And now, let's open this file in our
  907. 44:09browser. We'll rightclick,
  908. 44:12open with a live server.
  909. 44:16And it should say chatbot in the tab up
  910. 44:19here. So now we can close the React
  911. 44:22basics tabs which are these two here.
  912. 44:27And we'll go back to VS Code.
  913. 44:30And now we're ready to begin the chatbot
  914. 44:32project.
  915. 44:35First we're going to learn one of the
  916. 44:37most important features of React called
  917. 44:40components. A component is just a piece
  918. 44:44of the website.
  919. 44:47For example, let's go back to our
  920. 44:49browser and open the final chatbot
  921. 44:52project.
  922. 44:54So looking at this project, we can split
  923. 44:57it up into different pieces. One piece
  924. 45:00can be the text box and the button at
  925. 45:02the top and another piece are each of
  926. 45:05these chat messages.
  927. 45:07So these are called components. A
  928. 45:10component is just a piece of the
  929. 45:12website. Now, when building websites,
  930. 45:15it's better to split up the website into
  931. 45:18pieces or components so that we can work
  932. 45:21on a small piece of the website at a
  933. 45:23time.
  934. 45:26Now that we know what components are,
  935. 45:29let's create our first component in
  936. 45:31React. So, we mentioned that one way we
  937. 45:34can split up this website is to have one
  938. 45:37component at the top for this text box
  939. 45:40and the button. So, let's create a
  940. 45:42component for this in React. We'll go
  941. 45:45back to our code
  942. 45:48and we'll scroll down and first let's
  943. 45:51remove the example button and paragraph
  944. 45:54that we used. So, we'll select this and
  945. 45:57remove it.
  946. 46:00And to create a component in React,
  947. 46:02we're going to use a function. So we'll
  948. 46:05type function. And now we're going to
  949. 46:08name this component. Since this piece of
  950. 46:11the website is for inputting a message
  951. 46:13and sending it to the chatbot, let's
  952. 46:16name this component capital C chat
  953. 46:19capital I input brackets and curly
  954. 46:23brackets.
  955. 46:25Now in React the component name must
  956. 46:28start with a capital letter. So make
  957. 46:30sure that chat input starts with a
  958. 46:32capital C. This is also known as Pascal
  959. 46:36case where each word in the name starts
  960. 46:38with a capital letter.
  961. 46:43Next this function will return some
  962. 46:45HTML. So we're going to type return and
  963. 46:50we're going to return the HTML for this
  964. 46:52piece of the website. So this piece of
  965. 46:55the website has a text box and a button.
  966. 46:58So let's type brackets and press enter
  967. 47:01between these brackets.
  968. 47:04And to create a text box, we're going to
  969. 47:06use the HTML element less than input
  970. 47:10greater than. And we also need to type
  971. 47:12less than /input greater than. Now you
  972. 47:17may notice that in normal HTML input
  973. 47:20doesn't need this closing tag. However,
  974. 47:24JSX is more strict than normal HTML. So,
  975. 47:28all elements need a closing tag,
  976. 47:30including input. Now, there's actually a
  977. 47:33shortcut we can use here. If there's
  978. 47:36nothing inside an element, we can
  979. 47:38actually remove the closing tag.
  980. 47:42And at the end of this opening tag, we
  981. 47:45can just type space and then slash. So,
  982. 47:48this code is a shortcut for this code.
  983. 47:52And this is called a self-closing
  984. 47:54element. So in JSX we have to either
  985. 47:57have a closing tag or use this
  986. 48:00self-closing element shortcut.
  987. 48:03Next, this piece of the website also has
  988. 48:05a button. So let's also create a button
  989. 48:08element using less than button greater
  990. 48:11than less than /button greater than. And
  991. 48:16inside this button we have the text
  992. 48:18send.
  993. 48:20And now we're going to get an error here
  994. 48:22because we're trying to return two
  995. 48:24values or two elements from this
  996. 48:26function. But we can only return one
  997. 48:29value from a function. To solve this
  998. 48:31problem, we can use a div element like
  999. 48:34down here to group these two elements
  1000. 48:36together.
  1001. 48:38So above this, let's create a div
  1002. 48:40element.
  1003. 48:43And at the bottom, less than /d greater
  1004. 48:47than. And let's also add indents before
  1005. 48:50these two lines. So we'll click here and
  1006. 48:53press tab. And also click here and press
  1007. 48:56tab.
  1008. 48:57So this shows that these two elements
  1009. 48:59are inside this div element. And now
  1010. 49:02this code no longer has an error because
  1011. 49:05we're returning one value from this
  1012. 49:07function.
  1013. 49:09Now that we created this component,
  1014. 49:11let's learn how to display it on the
  1015. 49:13website. If we scroll down, right now
  1016. 49:18we're using React to display this div on
  1017. 49:21the website. So all we have to do is to
  1018. 49:24insert our component inside this div and
  1019. 49:27it will be displayed on the website. So
  1020. 49:30one way to do this is to use curly
  1021. 49:32brackets to insert something like we did
  1022. 49:35before. So here we'll type curly
  1023. 49:38brackets. And now we're just going to
  1024. 49:41run this function
  1025. 49:44capital C chat input and brackets.
  1026. 49:49So this will run the function and it
  1027. 49:52will return this HTML from the function
  1028. 49:55and then the curly brackets will insert
  1029. 49:57the HTML into the div and then display
  1030. 50:00it on the page.
  1031. 50:02So, as you can see, we can put any
  1032. 50:04JavaScript code between these curly
  1033. 50:06brackets. As long as this code results
  1034. 50:09in a value, we can insert it into this
  1035. 50:12HTML.
  1036. 50:14Let's remove the button and the
  1037. 50:16paragraph that we used as an example
  1038. 50:19and save.
  1039. 50:22And let's check our website. We'll open
  1040. 50:24the browser and go to our chatbot
  1041. 50:27project. And now we created the elements
  1042. 50:30for the top section of the chatbot
  1043. 50:32project, the text box and the send
  1044. 50:35button. So don't worry about how it
  1045. 50:37looks for now. Later in the course,
  1046. 50:40we're going to style this with CSS and
  1047. 50:42make it look like the final project.
  1048. 50:46Let's go back to our code.
  1049. 50:50And next, React has a special syntax we
  1050. 50:53can use to insert a component. Instead
  1051. 50:57of using curly brackets and running the
  1052. 50:59function, we can type this code less
  1053. 51:03than and then the name of the component
  1054. 51:06capital C chat input greater than and
  1055. 51:11then less than slash chat input greater
  1056. 51:16than.
  1057. 51:17So this is called the component syntax.
  1058. 51:21This does the same thing as the code
  1059. 51:23above. It runs the chat input function
  1060. 51:27and then inserts the result into the
  1061. 51:30HTML.
  1062. 51:31If we save
  1063. 51:34and we check our project, you'll notice
  1064. 51:36that that syntax does the same thing and
  1065. 51:39it creates another text box and button.
  1066. 51:43Let's go back to our code.
  1067. 51:46And again, this code does the same thing
  1068. 51:49as this code. Now this syntax has a big
  1069. 51:53advantage.
  1070. 51:55This actually looks like an HTML
  1071. 51:58element. It has an opening tag and a
  1072. 52:01closing tag. So you can think of this
  1073. 52:03like we're creating our own HTML element
  1074. 52:07called chat input.
  1075. 52:10HTML provides us with some default
  1076. 52:12elements like div, input and button. And
  1077. 52:16now using React, we can actually create
  1078. 52:19our own HTML element like chat input.
  1079. 52:23Then we can use our own HTML elements to
  1080. 52:26build the website instead of just the
  1081. 52:29default HTML elements. And this is
  1082. 52:32actually one of the main ideas of React.
  1083. 52:35We can create our own HTML elements. So
  1084. 52:39when using React, we should use this
  1085. 52:42component syntax instead of running the
  1086. 52:44component function directly because this
  1087. 52:47syntax lets us create our own HTML
  1088. 52:50element.
  1089. 52:52And again, we can use the self-closing
  1090. 52:55element shortcut like we did up here. If
  1091. 52:58there is nothing inside this element, we
  1092. 53:01can remove this closing tag.
  1093. 53:04And inside the opening tag at the end,
  1094. 53:07we can type space and slash.
  1095. 53:11And now we can remove this example code
  1096. 53:13above.
  1097. 53:16And let's save and check our project.
  1098. 53:21And that creates the top section of the
  1099. 53:23chatbot project.
  1100. 53:27So go back to VS Code.
  1101. 53:30And we mentioned in the previous lesson
  1102. 53:32that React helps us create websites
  1103. 53:34easier. And this is another way that
  1104. 53:37React helps us create websites easier.
  1105. 53:40It lets us split our website into
  1106. 53:42smaller pieces or components. And this
  1107. 53:45lets us work on a small piece of the
  1108. 53:47website at a time.
  1109. 53:51Before we create the rest of the
  1110. 53:53project, we're going to improve our code
  1111. 53:55a little bit. First, notice that in this
  1112. 53:59component, we're using a div to group
  1113. 54:02these two elements together. However,
  1114. 54:05this is also going to insert an extra
  1115. 54:08div element into our website. So, if we
  1116. 54:11go to our website
  1117. 54:15and rightclick and click inspect,
  1118. 54:19and we're going to go to the elements
  1119. 54:21tab, and we're going to go to our
  1120. 54:24container here, and we're going to
  1121. 54:25expand it.
  1122. 54:27So notice here that we have a bunch of
  1123. 54:30extra divs on this website. So what if
  1124. 54:33we don't want to insert these extra
  1125. 54:35divs? What if we just want to insert the
  1126. 54:38text box and the button on our website?
  1127. 54:41Let's go back to our code and learn how
  1128. 54:43to do that.
  1129. 54:46React has another way to group elements
  1130. 54:49together called a fragment. To use a
  1131. 54:52fragment, we can remove the word div.
  1132. 54:56and div from the closing tag and just
  1133. 55:00have a less than greater than and a less
  1134. 55:02than slashgreater than. So this is
  1135. 55:05called a fragment and it groups the two
  1136. 55:08elements together. So we can return it
  1137. 55:10from the function but it won't insert an
  1138. 55:13extra div. So, if we save this code and
  1139. 55:17then go to our website
  1140. 55:19and take a look at the HTML,
  1141. 55:22you'll notice now we have one less extra
  1142. 55:25div on the website.
  1143. 55:27Let's go back
  1144. 55:29and we can also use a fragment down
  1145. 55:31here. If we don't want to insert this
  1146. 55:34extra div, we can remove it and remove
  1147. 55:38it at the bottom and just use a
  1148. 55:40fragment. If we save and check the
  1149. 55:44website,
  1150. 55:47now we no longer have those extra divs.
  1151. 55:49We have our container and we just
  1152. 55:52inserted the text box and the button.
  1153. 55:56So that's fragments. It allows us to
  1154. 55:58group elements together without creating
  1155. 56:01an extra div.
  1156. 56:04Let's go back to our code.
  1157. 56:07And since this variable no longer
  1158. 56:10contains a div, let's rename it. So
  1159. 56:13right now we're building a chatbot
  1160. 56:15application or an app. So let's rename
  1161. 56:18this variable to app instead. And here
  1162. 56:22as well, we're going to render the app
  1163. 56:25and save.
  1164. 56:28So again in React, when returning
  1165. 56:30multiple elements or saving multiple
  1166. 56:33elements, we can use a fragment to group
  1167. 56:36them together. And this makes our HTML a
  1168. 56:39little bit cleaner because it doesn't
  1169. 56:41create an extra div element.
  1170. 56:45Next, another improvement we can make is
  1171. 56:47to add a placeholder to the text box at
  1172. 56:50the top. So the gray text inside the
  1173. 56:53text box is called a placeholder. It
  1174. 56:56tells us what to type inside the text
  1175. 56:59box. And when we start typing, it will
  1176. 57:02disappear.
  1177. 57:03To create a placeholder, we give this
  1178. 57:06input element an attribute. So at the
  1179. 57:09end, we're going to type the attribute
  1180. 57:12placeholder
  1181. 57:14equals double quotes.
  1182. 57:17And then inside these double quotes,
  1183. 57:19we're going to put the text that we
  1184. 57:21want, which is send a message to
  1185. 57:26chatbot.
  1186. 57:29If we save this code
  1187. 57:31and look at our website,
  1188. 57:34now we have a placeholder in the text
  1189. 57:36box. And when we start typing in it, it
  1190. 57:40will disappear. So the placeholder is
  1191. 57:42working. Now there is one problem here,
  1192. 57:45which is if we remove this text, you'll
  1193. 57:48notice that this placeholder is too long
  1194. 57:50and it's getting cut off. To fix this,
  1195. 57:53let's make this text box wider. We'll go
  1196. 57:56back to our code.
  1197. 57:58And to make this input wider, we're
  1198. 58:00going to give it another attribute. So
  1199. 58:03at the end, we'll type another attribute
  1200. 58:06called size equals double quotes. And
  1201. 58:10this sets how many characters can fit in
  1202. 58:12this input. By the way, a character is
  1203. 58:16just a letter, number, or symbol.
  1204. 58:19Let's set the size to 30 characters and
  1205. 58:23save and take a look.
  1206. 58:26Okay, so that looks pretty good for now.
  1207. 58:29Again, don't worry about how this looks.
  1208. 58:32We'll style it later in the course using
  1209. 58:34CSS.
  1210. 58:37So, we just created our first component
  1211. 58:40or our first piece of the website, which
  1212. 58:42is the section at the top. Now, let's
  1213. 58:45get some practice and create more
  1214. 58:47components.
  1215. 58:49Let's go back to the final chatbot
  1216. 58:51project
  1217. 58:53and let's ask ourselves how can we split
  1218. 58:56up the rest of this website into pieces.
  1219. 59:00Another piece of this website could be
  1220. 59:02one chat message here. So let's create a
  1221. 59:05component for this piece of the website.
  1222. 59:08We'll go back to VS Code
  1223. 59:12and to create a component we're going to
  1224. 59:14use a function. So down here, let's
  1225. 59:18create a function
  1226. 59:21and let's name it capital C chat capital
  1227. 59:25M message
  1228. 59:27brackets and curly brackets. So remember
  1229. 59:31a component name must start with a
  1230. 59:33capital letter and we use Pascal case.
  1231. 59:36So each word starts with a capital
  1232. 59:38letter.
  1233. 59:40Now inside this function, we're going to
  1234. 59:43return the HTML for this piece of the
  1235. 59:46website or this component.
  1236. 59:49So let's also type brackets and enter.
  1237. 59:52So we can write the HTML on multiple
  1238. 59:55lines. And now each chat message has two
  1239. 59:59things, some text and a profile image.
  1240. 1:00:03Since we have multiple things inside
  1241. 1:00:05this component, let's group them
  1242. 1:00:07together using a fragment. So we can
  1243. 1:00:10return one value from this function. So
  1244. 1:00:13we'll type less than greater than enter
  1245. 1:00:16and then less than /g greater than to
  1246. 1:00:19create a fragment. And now inside here
  1247. 1:00:24each message has some text. So let's
  1248. 1:00:26create the first chat message in our
  1249. 1:00:29project. We'll go back to the final
  1250. 1:00:31project.
  1251. 1:00:33And let's just copy paste the text from
  1252. 1:00:35the first message here which is hello
  1253. 1:00:38chatbot. So we'll select this and then
  1254. 1:00:41copy by pressing control C on Windows or
  1255. 1:00:44command C on Mac.
  1256. 1:00:47And then inside our code
  1257. 1:00:50in the chat message component we're
  1258. 1:00:52going to paste by pressing controlV on
  1259. 1:00:55Windows or commandV on Mac.
  1260. 1:00:59And let's fix the indent by pressing tab
  1261. 1:01:01at the start.
  1262. 1:01:03Next, each chat message has a profile
  1263. 1:01:06image. To create an image, we can use an
  1264. 1:01:10HTML element called img. So, let's
  1265. 1:01:13create a new line. And we're going to
  1266. 1:01:15type less than img greater than and then
  1267. 1:01:19less than / img greater than. So,
  1268. 1:01:23remember that in JSX, every element has
  1269. 1:01:26to have a closing tag. And again, we can
  1270. 1:01:30use the self-closing element shortcut
  1271. 1:01:32like we did down here. If there's
  1272. 1:01:34nothing inside this element, we can
  1273. 1:01:37remove the closing tag and then just add
  1274. 1:01:40a space and a slash at the end of the
  1275. 1:01:43opening tag. And now we have the two
  1276. 1:01:46things that we need for each chat
  1277. 1:01:48message, the text and an image.
  1278. 1:01:53Next, we need to tell the computer which
  1279. 1:01:56image to load. Right now, if we look at
  1280. 1:01:59our final chatbot project, we have two
  1281. 1:02:02images that we use, but we currently
  1282. 1:02:04don't have these in our project. So,
  1283. 1:02:06let's download them. We're going to
  1284. 1:02:09rightclick this first image and then
  1285. 1:02:12click save image.
  1286. 1:02:15And we're going to find the folder that
  1287. 1:02:17contains all of our code. So, for me,
  1288. 1:02:19it's on the desktop
  1289. 1:02:22and it's in the folder React-Course.
  1290. 1:02:25and we're going to save it inside of
  1291. 1:02:27this folder.
  1292. 1:02:30And same things for this robot image.
  1293. 1:02:32We're going to rightclick
  1294. 1:02:35and click save image.
  1295. 1:02:38Go to the React-Course folder. And we're
  1296. 1:02:41also going to save the robot image.
  1297. 1:02:46And now if we look at our code,
  1298. 1:02:48we should have two images inside our
  1299. 1:02:51project. User.png png and robot.png.
  1300. 1:02:57And now let's go back to our code.
  1301. 1:03:00To load an image on the website, we need
  1302. 1:03:03to tell the computer which image to
  1303. 1:03:05load. To do that, we're going to give
  1304. 1:03:08this image element an attribute called
  1305. 1:03:11src or source equals double quotes. And
  1306. 1:03:16inside these double quotes, we're going
  1307. 1:03:18to tell the computer which image to load
  1308. 1:03:21for the first chat message. The image is
  1309. 1:03:24the user image. So let's type this file
  1310. 1:03:27name. userpng.
  1311. 1:03:32So this will look for a file called
  1312. 1:03:34user.png
  1313. 1:03:36beside the current HTML file. This is a
  1314. 1:03:40current HTML file. So we'll look for
  1315. 1:03:42user.png beside it, which is right here.
  1316. 1:03:46and then it's going to load this image
  1317. 1:03:48on the website.
  1318. 1:03:50Okay, so we created a component for a
  1319. 1:03:53chat message. Now let's insert this
  1320. 1:03:56component on the website. We're going to
  1321. 1:03:58scroll down
  1322. 1:04:00to the app and let's insert this
  1323. 1:04:03component using the component syntax
  1324. 1:04:05like we did up here. So we'll type a new
  1325. 1:04:08line and type less than and then the
  1326. 1:04:11name of the component which is capital C
  1327. 1:04:14chat
  1328. 1:04:16capital M message.
  1329. 1:04:19And currently we don't have anything to
  1330. 1:04:21put inside this component. So we use the
  1331. 1:04:24self-closing element shortcut like we
  1332. 1:04:26did up here. So now let's save
  1333. 1:04:31and check our website
  1334. 1:04:33which is this tab. And let's close the
  1335. 1:04:36console so we can see it. And we created
  1336. 1:04:40a chat message.
  1337. 1:04:43However, we have a few problems here.
  1338. 1:04:45The first problem is that the profile
  1339. 1:04:47image is too big. To make it smaller,
  1340. 1:04:50we're going to go back to our code
  1341. 1:04:53and inside the img element, we're going
  1342. 1:04:56to give it another attribute called
  1343. 1:04:59width equals double quotes.
  1344. 1:05:03And let's try setting the width to 50
  1345. 1:05:06and save. And then check our website.
  1346. 1:05:10And that looks close enough.
  1347. 1:05:14Another problem is the chat message is
  1348. 1:05:17appearing on the same line as the chat
  1349. 1:05:19input, but in the final project, we want
  1350. 1:05:22it to appear below. To fix this, we'll
  1351. 1:05:25go back to our code
  1352. 1:05:28and inside the chat message component,
  1353. 1:05:31instead of using a fragment to group
  1354. 1:05:33these elements together, we're going to
  1355. 1:05:35change it back to a div. So, inside
  1356. 1:05:38we'll type div and inside here /d.
  1357. 1:05:44The reason we're using a div is because
  1358. 1:05:46in HTML, a div is known as a block
  1359. 1:05:49element. A block element will always
  1360. 1:05:53take up an entire line by itself. So if
  1361. 1:05:56we group this in a div element, instead
  1362. 1:05:59of appearing beside the chat input, the
  1363. 1:06:02div is going to take up an entire line
  1364. 1:06:04by itself. So it's going to take up the
  1365. 1:06:06entire line under the chat input. So
  1366. 1:06:10let's save this code and then check our
  1367. 1:06:13website.
  1368. 1:06:15So now this div is taking up this entire
  1369. 1:06:18line. If we rightclick and click inspect
  1370. 1:06:23and then click this arrow icon in the
  1371. 1:06:26top left and then click on this div,
  1372. 1:06:30you can see that this div now takes up
  1373. 1:06:32this entire line. So now we have the
  1374. 1:06:35layout that we want. So in React, we
  1375. 1:06:39still use divs for layout purposes.
  1376. 1:06:42If you want to learn divs, layout, and
  1377. 1:06:45HTML in more detail, you can check out
  1378. 1:06:48my HTML and CSS full course in the video
  1379. 1:06:52description.
  1380. 1:06:54So now we created our first chat
  1381. 1:06:56message.
  1382. 1:07:00Next, let's create the second chat
  1383. 1:07:03message. We'll go back to our code
  1384. 1:07:07and instead of creating a whole new
  1385. 1:07:09component, we can actually reuse our
  1386. 1:07:12chat message component. Components are
  1387. 1:07:14actually designed to be reusable.
  1388. 1:07:18To reuse this component, we don't want
  1389. 1:07:21the message to be hello chatbot every
  1390. 1:07:23time. We want to be able to display
  1391. 1:07:26different messages in this component. To
  1392. 1:07:29do that, we can give our component an
  1393. 1:07:32attribute. So down here, we're going to
  1394. 1:07:35go to the chat message component and
  1395. 1:07:37we're going to give it an attribute. So
  1396. 1:07:40this works just like HTML attributes.
  1397. 1:07:43We're going to give our attribute a
  1398. 1:07:45name. Let's call it message
  1399. 1:07:49equals double quotes. And inside these
  1400. 1:07:52double quotes, we're going to give the
  1401. 1:07:54attribute a value.
  1402. 1:07:57Let's put the text for the first message
  1403. 1:07:59which is hello chatbot. So let's select
  1404. 1:08:02this code and then copy
  1405. 1:08:06and we're going to paste it inside here.
  1406. 1:08:10So now we have an attribute called
  1407. 1:08:12message with a value hello chatbot. So
  1408. 1:08:15again you can see that this syntax looks
  1409. 1:08:18like HTML. We can even add our own
  1410. 1:08:21attributes.
  1411. 1:08:23Now that we added this message
  1412. 1:08:24attribute, how do we access it inside
  1413. 1:08:27this component and use it? To do that,
  1414. 1:08:31we're going to use a feature called
  1415. 1:08:33props.
  1416. 1:08:34Every component function gets one
  1417. 1:08:37parameter up here called props.
  1418. 1:08:42This props parameter is an object and it
  1419. 1:08:46contains all the attributes that we give
  1420. 1:08:49to this component. So up here, let's
  1421. 1:08:52actually console.log
  1422. 1:08:55props to see what it looks like. Let's
  1423. 1:08:59save and then open our website and check
  1424. 1:09:03the console.
  1425. 1:09:06And you'll see that props is an object
  1426. 1:09:08and it contains the attributes that we
  1427. 1:09:10gave to our component. So here we have
  1428. 1:09:13the message attribute and it has a value
  1429. 1:09:15hello chatbot. So the attribute name is
  1430. 1:09:18saved as a property and the attributes
  1431. 1:09:20value is saved as a value of this
  1432. 1:09:23object.
  1433. 1:09:24So let's go back to VS code
  1434. 1:09:27and using this props object we can
  1435. 1:09:30access our attributes. By the way props
  1436. 1:09:34is short for properties. We call this
  1437. 1:09:37props because the attributes are saved
  1438. 1:09:39as properties in this object.
  1439. 1:09:42So now to access the message attribute
  1440. 1:09:45we can remove this and type props
  1441. 1:09:50dot and the attribute name which is
  1442. 1:09:54message
  1443. 1:09:57and let's save this in a variable so we
  1444. 1:09:59can use it later. So at the front we'll
  1445. 1:10:02create a variable using const name name
  1446. 1:10:05it message
  1447. 1:10:07equals props dossage
  1448. 1:10:10and we'll add a new line to separate the
  1449. 1:10:12code a little bit.
  1450. 1:10:14Now instead of displaying hello chatbot
  1451. 1:10:17every time let's display whatever is in
  1452. 1:10:20the message attribute. So we'll remove
  1453. 1:10:23this
  1454. 1:10:24and let's insert this variable into this
  1455. 1:10:27element. To do that we can use curly
  1456. 1:10:30brackets and type the variable name
  1457. 1:10:34message.
  1458. 1:10:36So again we can put any JavaScript code
  1459. 1:10:39between these curly brackets and as long
  1460. 1:10:41as it results in a value it will insert
  1461. 1:10:44it into this element. So now let's save
  1462. 1:10:48and we'll check the website
  1463. 1:10:51and it displays a message hello chatbot.
  1464. 1:10:55That's the message attribute that we
  1465. 1:10:57gave it.
  1466. 1:11:00Let's go back to our code.
  1467. 1:11:02So that's props. Just like a normal HTML
  1468. 1:11:06element, we can give attributes to our
  1469. 1:11:09component and then access these
  1470. 1:11:11attributes using this props object.
  1471. 1:11:15So props are really useful because props
  1472. 1:11:18make our component reusable.
  1473. 1:11:20For example, let's try creating the
  1474. 1:11:23second chat message. To do that, we can
  1475. 1:11:26just create another chat message
  1476. 1:11:28component. So down here, let's type less
  1477. 1:11:32than chat message/grater
  1478. 1:11:37than.
  1479. 1:11:39And we just need to give it a different
  1480. 1:11:41message attribute. So here we'll give it
  1481. 1:11:44an attribute message equals double
  1482. 1:11:47quotes. And we'll copy paste the second
  1483. 1:11:51message in the chatbot project. We'll go
  1484. 1:11:53to our browser
  1485. 1:11:55and go to the chatbot project and let's
  1486. 1:11:58copy and paste this second message.
  1487. 1:12:01We'll select it and then copy.
  1488. 1:12:05And inside our code in the message
  1489. 1:12:08attribute, we're going to paste.
  1490. 1:12:12So this will save the message hello how
  1491. 1:12:15can I help you inside props and then we
  1492. 1:12:19get it out of props and insert it into
  1493. 1:12:21the component. Let's save and check the
  1494. 1:12:26website
  1495. 1:12:28and go to our tab.
  1496. 1:12:30And now we created the second chat
  1497. 1:12:33message. So let's go back to our code.
  1498. 1:12:37And you can see that props make our
  1499. 1:12:39component reusable. We can reuse this
  1500. 1:12:43component, but just give it a different
  1501. 1:12:45attribute or prop and it will display a
  1502. 1:12:48different message.
  1503. 1:12:50Let's get some practice and use props to
  1504. 1:12:53create two more chat messages. Feel free
  1505. 1:12:56to pause if you want to try it yourself
  1506. 1:12:58first.
  1507. 1:12:59So down here, let's create our third
  1508. 1:13:02message using less than chat
  1509. 1:13:05message/grater
  1510. 1:13:07than. and we'll give it a message
  1511. 1:13:11attribute equals double quotes and we'll
  1512. 1:13:14copy paste the third message. We'll go
  1513. 1:13:16to the chatbot project
  1514. 1:13:20and select this third message and then
  1515. 1:13:23copy.
  1516. 1:13:25And in our code inside the attribute,
  1517. 1:13:28we're going to paste.
  1518. 1:13:31And finally, let's create the fourth
  1519. 1:13:33message less than chat message
  1520. 1:13:37slashgreater than and then the message
  1521. 1:13:40attribute equals double quotes and we'll
  1522. 1:13:43copy paste the fourth message which is
  1523. 1:13:46this one. So don't worry if your
  1524. 1:13:48messages are different from mine. We're
  1525. 1:13:50just doing an example. So we'll select
  1526. 1:13:53this and copy.
  1527. 1:13:56And inside here, we're going to paste
  1528. 1:14:00and save.
  1529. 1:14:03And now, if we check our website
  1530. 1:14:06and go to our tab, you'll see that we
  1531. 1:14:09now have four chat messages in our
  1532. 1:14:12project. So, that's how we use props to
  1533. 1:14:15reuse the chat message component.
  1534. 1:14:19Next, in the final chatbot project,
  1535. 1:14:22notice that these two messages are
  1536. 1:14:24different. These two messages are from
  1537. 1:14:27the chatbot. So there are two
  1538. 1:14:30differences here. The profile image is a
  1539. 1:14:33robot and the image is on the left. To
  1540. 1:14:37create this, we can add another prop to
  1541. 1:14:40chat message. Let's go back to VS Code
  1542. 1:14:46and for each chat message, we're going
  1543. 1:14:48to add another attribute.
  1544. 1:14:51And let's call this attribute the sender
  1545. 1:14:55equals double quotes.
  1546. 1:14:58This attribute tells us who sent the
  1547. 1:15:00message. And we can use this to
  1548. 1:15:02determine which image to load and where
  1549. 1:15:05to place the image. So if we send the
  1550. 1:15:08message, let's make the sender the user.
  1551. 1:15:13And for the second chat message, we're
  1552. 1:15:16going to add another sender attribute
  1553. 1:15:19equals double quotes. And the second
  1554. 1:15:21message is from the chatbot. So let's
  1555. 1:15:24make the sender attribute the robot.
  1556. 1:15:28And we'll do the same thing for the
  1557. 1:15:30third and fourth message. We'll give it
  1558. 1:15:32a sender attribute.
  1559. 1:15:35The third message is from the user. So
  1560. 1:15:38let's make the sender user.
  1561. 1:15:41And the fourth message sender equals
  1562. 1:15:45double quotes. This message is from the
  1563. 1:15:47robot. So let's make the sender robot.
  1564. 1:15:53Next, let's access the sender attribute
  1565. 1:15:56inside our component. To do that, all
  1566. 1:15:59the attributes are saved inside this
  1567. 1:16:02props object. To access the sender
  1568. 1:16:05attribute, we can type props dot sender.
  1569. 1:16:11So this gives us the value of the sender
  1570. 1:16:13attribute which is either user or robot.
  1571. 1:16:17So let's save this in a variable so we
  1572. 1:16:19can use it later. Type const and let's
  1573. 1:16:23call it sender equals props.
  1574. 1:16:28So now that we have the sender
  1575. 1:16:30attribute, let's use it to decide which
  1576. 1:16:33profile image to load and where to place
  1577. 1:16:35this image. To do that, we can just use
  1578. 1:16:38an if statement.
  1579. 1:16:41So inside here, we can type if brackets
  1580. 1:16:45and curly brackets.
  1581. 1:16:47And inside these brackets, we're going
  1582. 1:16:49to check if the sender
  1583. 1:16:53is equal to the string robot.
  1584. 1:16:57By the way, by default, if the attribute
  1585. 1:17:01just has double quotes like this, the
  1586. 1:17:03value is a string. So here the value is
  1587. 1:17:06going to be the string user or the
  1588. 1:17:08string robot.
  1589. 1:17:10So now inside our component, if the
  1590. 1:17:12sender is the robot, we're going to
  1591. 1:17:15return some different HTML.
  1592. 1:17:17So let's type return brackets. So we can
  1593. 1:17:21write this HTML on multiple lines. And
  1594. 1:17:25let's just copy paste the HTML from down
  1595. 1:17:27here and modify it. So we'll select this
  1596. 1:17:31HTML and then copy
  1597. 1:17:34and then inside this return we're going
  1598. 1:17:37to paste.
  1599. 1:17:40And let's also select these lines and
  1600. 1:17:42press tab to fix the indenting.
  1601. 1:17:46So the first thing we need to change if
  1602. 1:17:48the sender is a robot is that we want to
  1603. 1:17:51load the robot image instead.
  1604. 1:17:54So, we already downloaded the robot
  1605. 1:17:56image here. To load it, we're just going
  1606. 1:17:59to change the src attribute or the
  1607. 1:18:02source attribute of this image. So,
  1608. 1:18:05we're going to change this from user to
  1609. 1:18:07robot.png.
  1610. 1:18:10And we also want this image to be on the
  1611. 1:18:13left. So, let's just move this image
  1612. 1:18:16before the message. So we'll select this
  1613. 1:18:18code and then cut by pressing control X
  1614. 1:18:22on Windows or command X on Mac.
  1615. 1:18:26And then before the message we're going
  1616. 1:18:28to paste by pressing controlV on Windows
  1617. 1:18:31or commandV on Mac.
  1618. 1:18:34And we can remove this extra new line.
  1619. 1:18:37So as you can see inside a component we
  1620. 1:18:40can use any JavaScript feature like if
  1621. 1:18:43statement and we can return a different
  1622. 1:18:46HTML and whatever we return will be
  1623. 1:18:50inserted inside the element here. So now
  1624. 1:18:54let's save this code
  1625. 1:18:56and then check our website
  1626. 1:18:59and then open our tab
  1627. 1:19:02and you can see that now we have two
  1628. 1:19:04messages from the chatbot. that loads
  1629. 1:19:07the robot image and it puts the image on
  1630. 1:19:09the left.
  1631. 1:19:12We'll go back to our code
  1632. 1:19:15and using this sender prop, we're able
  1633. 1:19:18to reuse this component to create a chat
  1634. 1:19:21message from the robot. So again, props
  1635. 1:19:25make our component reusable.
  1636. 1:19:28And that's it. If we scroll down,
  1637. 1:19:31using components, we split up the
  1638. 1:19:34website or the app into smaller pieces
  1639. 1:19:37like the chat input section at the top
  1640. 1:19:40and each chat message. And we created
  1641. 1:19:43all the HTML that we need for this
  1642. 1:19:45project.
  1643. 1:19:48The last thing we're going to learn in
  1644. 1:19:49this lesson are some shortcuts that are
  1645. 1:19:51commonly used in React, and we can use
  1646. 1:19:54these to clean up our code.
  1647. 1:19:57First, let's scroll up to the chat
  1648. 1:20:00message component.
  1649. 1:20:02So, instead of doing con message equals
  1650. 1:20:04props message, we actually have a
  1651. 1:20:07shortcut for this in JavaScript. So,
  1652. 1:20:10down here we can just type const curly
  1653. 1:20:14brackets and inside we can type message
  1654. 1:20:19and make it equal to props.
  1655. 1:20:24So this takes the message property out
  1656. 1:20:27of this object and saves it in a
  1657. 1:20:29variable called message. So this does
  1658. 1:20:32the same thing as this line above. This
  1659. 1:20:36code is a shortcut for this code. And
  1660. 1:20:39this shortcut is called dstructuring
  1661. 1:20:42because we're taking stuff out of this
  1662. 1:20:44object. We're deconstructing it. We can
  1663. 1:20:48also use this shortcut with the sender
  1664. 1:20:50property to add another property to take
  1665. 1:20:54out of this object. After the first
  1666. 1:20:56property, we can type comma and the
  1667. 1:20:59second property sender.
  1668. 1:21:03So this will get the message property
  1669. 1:21:05and the sender property out of this
  1670. 1:21:08object and it will save it in a message
  1671. 1:21:10variable and a sender variable. So this
  1672. 1:21:14line of code is a shortcut for these two
  1673. 1:21:17lines above.
  1674. 1:21:19So now let's actually turn these two
  1675. 1:21:21lines into comments or comment out these
  1676. 1:21:24lines. At the front we're going to type
  1677. 1:21:28double slash and at the front here we'll
  1678. 1:21:31type double slash.
  1679. 1:21:34By the way, if we start with double
  1680. 1:21:36slash, it will turn this code into
  1681. 1:21:38comments and it will be ignored by the
  1682. 1:21:40computer.
  1683. 1:21:42So, we'll just keep this here as a
  1684. 1:21:44reminder that this is a shortcut for
  1685. 1:21:47these two.
  1686. 1:21:49Next, JavaScript has an even shorter
  1687. 1:21:52shortcut for this. Since props is a
  1688. 1:21:55parameter up here, we can just
  1689. 1:21:58dstructure it directly up here. So here
  1690. 1:22:01instead of the props object we can just
  1691. 1:22:04type curly brackets directly
  1692. 1:22:07and then type message
  1693. 1:22:11and sender.
  1694. 1:22:14This takes the message and sender
  1695. 1:22:16properties directly out of the first
  1696. 1:22:18parameter and then saves them in a
  1697. 1:22:20variable called message and a variable
  1698. 1:22:23called sender. So it does the same thing
  1699. 1:22:25as this code down here, but it's an even
  1700. 1:22:28shorter shortcut. We're dstructuring the
  1701. 1:22:31parameter directly at the top. So this
  1702. 1:22:34shortcut is used a lot in React
  1703. 1:22:36tutorials and documentation. So it's
  1704. 1:22:39useful to know how this works.
  1705. 1:22:43So let's comment out this line here at
  1706. 1:22:46the front. We'll type double slash.
  1707. 1:22:49So we remember that this code is a
  1708. 1:22:52shortcut for this line of code and these
  1709. 1:22:55two lines of code.
  1710. 1:22:57Now, let's save and check our website to
  1711. 1:23:01make sure that everything is still
  1712. 1:23:02working. So, that looks good.
  1713. 1:23:06We'll go back. And that is a
  1714. 1:23:08dstructuring shortcut. It allows us to
  1715. 1:23:11take properties directly out of an
  1716. 1:23:13object.
  1717. 1:23:16Next, we're going to learn how to use if
  1718. 1:23:18statements directly inside JSX.
  1719. 1:23:21So currently we have some JSX here and
  1720. 1:23:25we put an if statement outside of this
  1721. 1:23:28JSX.
  1722. 1:23:29However, this means we're duplicating
  1723. 1:23:31some code like the div and the message.
  1724. 1:23:36To make this code more reusable, we can
  1725. 1:23:39actually move this if statement directly
  1726. 1:23:42inside the JSX down here. So what we
  1727. 1:23:46want to do is if the sender is a robot,
  1728. 1:23:50we want to put the image before the
  1729. 1:23:52message. So down here, let's go before
  1730. 1:23:55the message and press enter. And we're
  1731. 1:23:58going to type curly brackets and we're
  1732. 1:24:01going to insert the if statement
  1733. 1:24:03directly inside here. So here before the
  1734. 1:24:08message we want to do something like if
  1735. 1:24:11the sender is equal to the string robot
  1736. 1:24:16we're going to insert this image.
  1737. 1:24:20However we can't actually use the word
  1738. 1:24:23if inside of these curly brackets.
  1739. 1:24:26Instead we need to use another feature
  1740. 1:24:28of JavaScript called a guard operator or
  1741. 1:24:32and.
  1742. 1:24:34In JavaScript, you can write value one
  1743. 1:24:37and value two. If value one is true,
  1744. 1:24:40then the result will be value two. So
  1745. 1:24:43this works just like an if statement
  1746. 1:24:46except we can write it on one line and
  1747. 1:24:48it results in a value. So if you want to
  1748. 1:24:51learn more about this feature, I cover
  1749. 1:24:53it in the booleans lesson of my
  1750. 1:24:55JavaScript course and you can check out
  1751. 1:24:58that in the video description.
  1752. 1:25:00For now, the only thing we need to know
  1753. 1:25:03is we can use and as sort of an if
  1754. 1:25:06statement in our JSX.
  1755. 1:25:08So, first let's remove this
  1756. 1:25:12and then we're going to use and to
  1757. 1:25:14insert this if statement directly inside
  1758. 1:25:17the JSX.
  1759. 1:25:19So, the first value is going to be our
  1760. 1:25:21if condition, which is sender equals the
  1761. 1:25:25robot. So we'll check if the sender is
  1762. 1:25:29equal to the string robot
  1763. 1:25:33and then we're going to use and.
  1764. 1:25:36So if this is true, the result is going
  1765. 1:25:39to be the second value here. So here if
  1766. 1:25:43the sender is a robot, we want to insert
  1767. 1:25:46the robot image. So let's just copy and
  1768. 1:25:49paste this code. We're going to select
  1769. 1:25:52this
  1770. 1:25:53and then copy it.
  1771. 1:25:56And then down here after and we're going
  1772. 1:25:58to paste.
  1773. 1:26:01So again the way this works is if the
  1774. 1:26:04value on the left is true then the
  1775. 1:26:07result will be the value on the right.
  1776. 1:26:10So this and is sort of a shortcut for
  1777. 1:26:13this if statement and it lets us insert
  1778. 1:26:16if statements directly inside our JSX.
  1779. 1:26:20So let's actually comment out this if
  1780. 1:26:22statement out here. So we remember that
  1781. 1:26:25this code is a shortcut for this code.
  1782. 1:26:29So here before the if statement we'll
  1783. 1:26:32type slash and star and after the if
  1784. 1:26:35statement we'll type star and slash and
  1785. 1:26:39remove the indent. So this is known as a
  1786. 1:26:42multi-line comment. All the code between
  1787. 1:26:45these two stars will be ignored by the
  1788. 1:26:48computer. So now let's save
  1789. 1:26:52and check our website.
  1790. 1:26:55And as you can see, if the sender is a
  1791. 1:26:58robot, we're going to add an image on
  1792. 1:27:01the left before the message. So now, one
  1793. 1:27:04problem here is that we're also showing
  1794. 1:27:07the user's profile image at the back,
  1795. 1:27:10but we only want to show one profile
  1796. 1:27:12image. We'll go back to our code
  1797. 1:27:16and here we only want to show the user
  1798. 1:27:19profile image if the sender is the user.
  1799. 1:27:23So let's insert another if statement
  1800. 1:27:25down here. Feel free to pause this video
  1801. 1:27:28if you want to try it yourself first.
  1802. 1:27:32So at the front we'll type open curly
  1803. 1:27:34bracket. At the end a close curly
  1804. 1:27:37bracket.
  1805. 1:27:38And at the front we're going to type the
  1806. 1:27:41if condition. So we'll check if the
  1807. 1:27:45sender is equal to the string user and
  1808. 1:27:50then we'll type ant. So if this is true
  1809. 1:27:54then we will show this image. If this is
  1810. 1:27:57not true, we will not show this image.
  1811. 1:28:00So this code is a shortcut for this
  1812. 1:28:02code.
  1813. 1:28:05Let's save and check our website.
  1814. 1:28:10And it works. The robot image is on the
  1815. 1:28:13left and the user image is on the right.
  1816. 1:28:18So let's go back.
  1817. 1:28:20Using the guard operator or and we can
  1818. 1:28:24insert an if statement directly inside
  1819. 1:28:26our JSX.
  1820. 1:28:29Next, we're going to do some code
  1821. 1:28:31cleanup. So, let's scroll up and we're
  1822. 1:28:34going to go to the chat input component
  1823. 1:28:37first. So, you notice that in here, this
  1824. 1:28:40line is a little bit too long. To make
  1825. 1:28:43our code easier to read, we can actually
  1826. 1:28:46put each attribute on its own line. So,
  1827. 1:28:49here we'll type enter to put placeholder
  1828. 1:28:52on its own line. And here we'll type
  1829. 1:28:55enter to put size on its own line. And
  1830. 1:28:58let's also type enter at the back. So
  1831. 1:29:01the attributes are on their own lines.
  1832. 1:29:04And then let's add an indent in front of
  1833. 1:29:06these attributes by pressing tab and
  1834. 1:29:08pressing tab. This makes it clear that
  1835. 1:29:11these attributes are part of this
  1836. 1:29:13element. And we also want to make sure
  1837. 1:29:16that these two are vertically aligned.
  1838. 1:29:19This makes it easy to see where this
  1839. 1:29:21element starts and where it ends.
  1840. 1:29:24Next, let's scroll down to the chat
  1841. 1:29:27message component. And notice that these
  1842. 1:29:31two lines of code with the and are also
  1843. 1:29:34a bit too long. To improve this, we can
  1844. 1:29:37surround this value with brackets. So
  1845. 1:29:40we'll type open bracket and at the end
  1846. 1:29:43close bracket and then we can put this
  1847. 1:29:46code on its own line. So here we'll type
  1848. 1:29:49enter and at the end we'll type enter.
  1849. 1:29:53So this will still have the same value,
  1850. 1:29:55but the brackets allow us to put the
  1851. 1:29:57code on multiple lines. We'll do the
  1852. 1:30:00same thing for the second image.
  1853. 1:30:03We'll surround it in brackets, open
  1854. 1:30:05bracket, and close bracket. And we'll
  1855. 1:30:08put this value on a separate line
  1856. 1:30:11so that it's no longer too long.
  1857. 1:30:15Next, let's scroll down even more to the
  1858. 1:30:18app variable. And you'll notice that
  1859. 1:30:21these lines are also a bit too long
  1860. 1:30:24because of the attributes. Again, we can
  1861. 1:30:26put each attribute on its own line. So
  1862. 1:30:30here we'll put message on its own line
  1863. 1:30:32and sender on its own line. And we'll
  1864. 1:30:35put the end of the component on its own
  1865. 1:30:37line so that this is vertically aligned
  1866. 1:30:40with this. And let's also add indents
  1867. 1:30:42before the attributes. So we'll press
  1868. 1:30:44tab and tab. So it's easy to see that
  1869. 1:30:48these attributes are part of this
  1870. 1:30:50component. And we'll do the same thing
  1871. 1:30:52for the other three chat messages. So
  1872. 1:30:55we'll put this on its own line
  1873. 1:30:58and add indents.
  1874. 1:31:02And put this on its own line.
  1875. 1:31:05Add indents.
  1876. 1:31:08And put this on its own line.
  1877. 1:31:12And add indents.
  1878. 1:31:15and save
  1879. 1:31:19and check our website and everything
  1880. 1:31:21should still work because we just
  1881. 1:31:23changed the formatting of our code. So
  1882. 1:31:26let's go back and that's how we avoid
  1883. 1:31:28our lines being too long. We can put
  1884. 1:31:31each attribute on its own line
  1885. 1:31:34and we can also surround a value with
  1886. 1:31:36brackets and put it on its own line.
  1887. 1:31:41Finally, down here, instead of using a
  1888. 1:31:44variable to save our app, a best
  1889. 1:31:47practice is to use a component to create
  1890. 1:31:50this app.
  1891. 1:31:52So here, let's create a component using
  1892. 1:31:56function
  1893. 1:31:58and we'll name it capital A app
  1894. 1:32:02brackets and curly brackets.
  1895. 1:32:05And then this component will return all
  1896. 1:32:09of this HTML.
  1897. 1:32:11So we'll return brackets so we can write
  1898. 1:32:14it on multiple lines.
  1899. 1:32:17And we're going to move all this code up
  1900. 1:32:19here. So let's select this code. And
  1901. 1:32:23we'll cut
  1902. 1:32:26and inside the return
  1903. 1:32:28we're going to paste.
  1904. 1:32:32And we can select these lines
  1905. 1:32:36and press tab to fix the indenting.
  1906. 1:32:40So now we're going to use this component
  1907. 1:32:42instead of the variable to create our
  1908. 1:32:44app. So down here, let's remove this
  1909. 1:32:48variable.
  1910. 1:32:50And then inside render, we're going to
  1911. 1:32:53create the app. We can type less than
  1912. 1:32:57capital A app greater than and then less
  1913. 1:33:01than slash app greater than.
  1914. 1:33:05So this creates an entire chatbot
  1915. 1:33:07application. And then we give this to
  1916. 1:33:10React to display on the website.
  1917. 1:33:13And again since there's nothing inside
  1918. 1:33:16this component, we can use the
  1919. 1:33:18self-closing element shortcut. So we can
  1920. 1:33:21remove this closing tag. And then at the
  1921. 1:33:24end here we can type space and slash.
  1922. 1:33:29And now let's save
  1923. 1:33:31and check our website.
  1924. 1:33:34And it displays the app on the website.
  1925. 1:33:38Let's go back to our code.
  1926. 1:33:41So a best practice is to use a component
  1927. 1:33:44called app to create the website because
  1928. 1:33:47it gives us access to extra features of
  1929. 1:33:50components like props.
  1930. 1:33:53And also notice that we can have
  1931. 1:33:55components inside a component. So this
  1932. 1:33:59lets us split up a website into smaller
  1933. 1:34:01and smaller components or smaller and
  1934. 1:34:04smaller pieces.
  1935. 1:34:06So again, React helps us create websites
  1936. 1:34:09easier because it lets us work on a
  1937. 1:34:12small piece of the website at a time.
  1938. 1:34:17In the next lesson, we're going to learn
  1939. 1:34:19a very important feature of React called
  1940. 1:34:22state. State lets us make our website
  1941. 1:34:25interactive, and it will let us add new
  1942. 1:34:28chat messages to our website.
  1943. 1:34:32And that's the end of this lesson. In
  1944. 1:34:34this lesson, we learned that a component
  1945. 1:34:37is just a piece of the website. We also
  1946. 1:34:40learned that components let us create
  1947. 1:34:43our own HTML elements. We started the
  1948. 1:34:47chatbot project. We split the chatbot
  1949. 1:34:50into chat input and chat message
  1950. 1:34:53components. We learned props which let
  1951. 1:34:56us insert values into components and
  1952. 1:34:59make components reusable.
  1953. 1:35:02We learned the destructuring and guard
  1954. 1:35:04operator shortcuts. We did some code
  1955. 1:35:07cleanup and split longer lines into
  1956. 1:35:10multiple lines. And we created an app
  1957. 1:35:14component which represents the entire
  1958. 1:35:17application.
  1959. 1:35:19Thanks for watching so far. If you find
  1960. 1:35:22this course valuable, you can support
  1961. 1:35:23the channel by liking this video,
  1962. 1:35:26leaving a comment, and subscribing.
  1963. 1:35:28You can also check out the premium
  1964. 1:35:30version of this course where you'll get
  1965. 1:35:32all the lessons ads free broken down
  1966. 1:35:35into small videos as well as additional
  1967. 1:35:38lessons and more projects that are not
  1968. 1:35:40in the public React course. Currently,
  1969. 1:35:42the premium course contains 11 lessons
  1970. 1:35:45and I'll be adding more lessons in the
  1971. 1:35:47future. You can also get a certificate
  1972. 1:35:50of completion, create comments or
  1973. 1:35:52questions on specific topics, get
  1974. 1:35:55progress tracking, search and course
  1975. 1:35:57notes. You can also check out the
  1976. 1:35:59membership where you can get this course
  1977. 1:36:02ads free separated by each lesson, AI
  1978. 1:36:05videos, backend tutorials, and more.
  1979. 1:36:08Thanks again, and let's continue with
  1980. 1:36:10the course. Here are some exercises to
  1981. 1:36:13help you practice what we learned in
  1982. 1:36:15this lesson.
  1983. 1:36:54In this lesson, we're going to learn two
  1984. 1:36:56of the most important features of React
  1985. 1:36:59called state and event handlers. And
  1986. 1:37:02we're going to use these features to
  1987. 1:37:04create the functionality for the chatbot
  1988. 1:37:07project.
  1989. 1:37:09Before we learn state, we're going to
  1990. 1:37:11improve our code a bit. Currently to add
  1991. 1:37:14a new chat message we have to manually
  1992. 1:37:17type a new chat message component.
  1993. 1:37:20Instead of manually typing this code we
  1994. 1:37:22can actually use JavaScript to generate
  1995. 1:37:25these components.
  1996. 1:37:27To do that we're going to do two steps.
  1997. 1:37:30The first step is to save the data. Data
  1998. 1:37:34means information.
  1999. 1:37:36Here the data is the information about
  2000. 1:37:39our chat messages like the message and
  2001. 1:37:42the sender. So we need to save this data
  2002. 1:37:46or this information in our JavaScript.
  2003. 1:37:50Let's use a variable to save this data.
  2004. 1:37:53At the top of this component, we're
  2005. 1:37:56going to create a variable using const
  2006. 1:37:59and we'll name it chat messages
  2007. 1:38:03equals and we'll save it as an array by
  2008. 1:38:06typing square brackets.
  2009. 1:38:09As a review, an array is a list of
  2010. 1:38:12values. So here we want to save a list
  2011. 1:38:15of chat messages. So it makes sense to
  2012. 1:38:18save it as an array.
  2013. 1:38:21Next, each chat message has multiple
  2014. 1:38:23pieces of data, like the message and the
  2015. 1:38:27sender. So, inside this array, we're
  2016. 1:38:30going to save each chat message as an
  2017. 1:38:33object by typing curly brackets.
  2018. 1:38:37As a review, an object groups related
  2019. 1:38:40values together. So, it's going to help
  2020. 1:38:42us group the message and the sender
  2021. 1:38:45together.
  2022. 1:38:47And then inside this object, we're going
  2023. 1:38:49to create a property called message
  2024. 1:38:53colon and a string. And we're going to
  2025. 1:38:56use this to save the message. So let's
  2026. 1:38:59copy the first message. We'll select it
  2027. 1:39:02and copy. And we're going to paste it in
  2028. 1:39:05here.
  2029. 1:39:07And then we'll add a comma. And we're
  2030. 1:39:10going to add a sender property colon and
  2031. 1:39:14a string. and we're going to save the
  2032. 1:39:16first sender which is the user.
  2033. 1:39:21So that's how we save the data for the
  2034. 1:39:23first chat message in our JavaScript.
  2035. 1:39:27Now let's save the data for the second
  2036. 1:39:29chat message.
  2037. 1:39:31So after the first object, we're going
  2038. 1:39:33to type comma and create another object.
  2039. 1:39:38I will give this one a message property
  2040. 1:39:40as well colon a string. and we'll save
  2041. 1:39:44the second message.
  2042. 1:39:47We'll select this copy and then inside
  2043. 1:39:51here we're going to paste.
  2044. 1:39:54Add a comma and a sender property colon
  2045. 1:39:58and a string. And the second chat
  2046. 1:40:01message is from the robot. So inside the
  2047. 1:40:04sender, we'll save robot.
  2048. 1:40:08Let's do the same thing for the third
  2049. 1:40:10and fourth chat messages. we'll save
  2050. 1:40:12them in our JavaScript. Feel free to
  2051. 1:40:14pause the video if you want to try it
  2052. 1:40:16yourself.
  2053. 1:40:18So, for the third chat message, we're
  2054. 1:40:20going to add a comma at the end and
  2055. 1:40:22another object, a message property, a
  2056. 1:40:27string,
  2057. 1:40:29and we're going to copy the third
  2058. 1:40:31message,
  2059. 1:40:33and paste it in here.
  2060. 1:40:37and a sender property. The third message
  2061. 1:40:40is from the user.
  2062. 1:40:43And then we're going to type comma and
  2063. 1:40:46another object the message property
  2064. 1:40:49colon and a string. And we'll copy paste
  2065. 1:40:52the fourth message. So we'll select it,
  2066. 1:40:56copy, and then inside here we're going
  2067. 1:41:00to paste
  2068. 1:41:02and a sender property.
  2069. 1:41:05And the fourth message is from the
  2070. 1:41:08robot.
  2071. 1:41:09So that's how we save the data or the
  2072. 1:41:12information for the chat messages.
  2073. 1:41:17After we save the data, the second step
  2074. 1:41:20is to generate the HTML. So we're going
  2075. 1:41:23to use JavaScript to convert this data
  2076. 1:41:27into these components. So we don't have
  2077. 1:41:29to type these components manually.
  2078. 1:41:32To do that, after the data,
  2079. 1:41:36we're going to type the name of the
  2080. 1:41:38data, which is chat messages.
  2081. 1:41:43And this is an array. And arrays have a
  2082. 1:41:46feature called dot map and brackets.
  2083. 1:41:51As a review, do map lets us go through
  2084. 1:41:55each value of an array and convert it
  2085. 1:41:58into a new value.
  2086. 1:42:01To use dot map, we need to give it a
  2087. 1:42:04function. So between these brackets,
  2088. 1:42:07we're going to type a function using
  2089. 1:42:10brackets, arrow, and curly brackets.
  2090. 1:42:14As a review, this is an arrow function.
  2091. 1:42:18It mostly works the same way as a
  2092. 1:42:21function, but it's shorter. When we give
  2093. 1:42:25a function to another function like map,
  2094. 1:42:28we usually use an arrow function because
  2095. 1:42:31it makes the code a bit cleaner.
  2096. 1:42:34This is what React tutorials and
  2097. 1:42:35documentations use.
  2098. 1:42:39Next, this function gets a parameter. So
  2099. 1:42:42at the top, we'll create a new parameter
  2100. 1:42:45and name it chat message.
  2101. 1:42:49So now dom takes each value in this
  2102. 1:42:53array and converts it into a new value.
  2103. 1:42:58The way it works is it starts with the
  2104. 1:43:00first value and then it saves it in this
  2105. 1:43:03parameter and runs this function and
  2106. 1:43:06whatever we return from this function
  2107. 1:43:08will be the new first value. Then it
  2108. 1:43:11takes the second value. It saves it in
  2109. 1:43:14this parameter and runs the function.
  2110. 1:43:17And whatever we return from the function
  2111. 1:43:19will be the new second value. Then it
  2112. 1:43:22takes the third value, saves it, runs a
  2113. 1:43:26function and whatever we return will be
  2114. 1:43:28the new third value and so on. So map
  2115. 1:43:33basically goes through each value in
  2116. 1:43:35this array, runs a function and converts
  2117. 1:43:38it into a new value.
  2118. 1:43:44So here we want to convert each chat
  2119. 1:43:47message object into a chat message
  2120. 1:43:50component. So inside this inner
  2121. 1:43:53function, we're going to return a chat
  2122. 1:43:56message component by typing brackets
  2123. 1:44:00and then here less than chat
  2124. 1:44:04message/grater
  2125. 1:44:06than to return a chat message component.
  2126. 1:44:11Next, each chat message component has
  2127. 1:44:14two props, message and sender. So we're
  2128. 1:44:18going to add those props to this
  2129. 1:44:20component.
  2130. 1:44:21So here
  2131. 1:44:23we'll create a prop message
  2132. 1:44:26equals
  2133. 1:44:28and now what do we save in this prop?
  2134. 1:44:31Remember that map takes each object in
  2135. 1:44:35this array and saves it in this
  2136. 1:44:38parameter. So this parameter contains a
  2137. 1:44:41chat message object and each chat
  2138. 1:44:44message object has a message and a
  2139. 1:44:47sender. So we want to save the message
  2140. 1:44:50inside this prop.
  2141. 1:44:52To do that we're going to type curly
  2142. 1:44:55brackets and inside we'll type this
  2143. 1:44:59object chat message and get the message
  2144. 1:45:04property which is dot message.
  2145. 1:45:08This gets the message out of this object
  2146. 1:45:11and saves it in the prop.
  2147. 1:45:15So why do we use curly brackets here
  2148. 1:45:18instead of double quotes? If we use
  2149. 1:45:21double quotes, it will save a string
  2150. 1:45:24into this prop, which is not what we
  2151. 1:45:26want. But if we use curly brackets, it
  2152. 1:45:30will save the result of this code into
  2153. 1:45:33this prop.
  2154. 1:45:36So for the first chat message, the
  2155. 1:45:38result of chat message dossage is going
  2156. 1:45:42to be the string hello chatbot. So it's
  2157. 1:45:45going to take this and save it into the
  2158. 1:45:47message prop here. For the second chat
  2159. 1:45:51message, the result of this code is
  2160. 1:45:54hello, how can I help you? And it will
  2161. 1:45:56save this into the message prop and so
  2162. 1:45:59on. So using curly brackets we can put
  2163. 1:46:03any JavaScript code inside and it will
  2164. 1:46:06save the result of this code into the
  2165. 1:46:09prop not just a string.
  2166. 1:46:12So next each chat message component has
  2167. 1:46:15a sender prop. So here we're also going
  2168. 1:46:19to add a sender
  2169. 1:46:22equals and again we want to save the
  2170. 1:46:25sender property of this chat message
  2171. 1:46:28object. So we'll type curly brackets and
  2172. 1:46:32the object
  2173. 1:46:34chat message dot sender.
  2174. 1:46:38So this will get the sender property out
  2175. 1:46:41of each chat message object and save it
  2176. 1:46:44in here. And that's it. Now map will
  2177. 1:46:48take each value of this array and
  2178. 1:46:51convert it into a chat message
  2179. 1:46:53component.
  2180. 1:46:55And here's what the result looks like.
  2181. 1:47:02Next, we're going to save the result of
  2182. 1:47:04this code in a variable. So, up here,
  2183. 1:47:08let's type const and we'll name the
  2184. 1:47:11variable chat message components equals
  2185. 1:47:17this code.
  2186. 1:47:19And now we have an array of components
  2187. 1:47:23or a list of components.
  2188. 1:47:26Finally, we're going to insert this
  2189. 1:47:28array into the JSX.
  2190. 1:47:31So, down here, we can actually remove
  2191. 1:47:34all of these chat message components
  2192. 1:47:36because we're generating them now.
  2193. 1:47:40And we're going to type curly brackets,
  2194. 1:47:42and we're going to insert this array.
  2195. 1:47:46In here, we'll type chat message
  2196. 1:47:50components.
  2197. 1:47:52If we insert an array of components,
  2198. 1:47:56React will just display all of these
  2199. 1:47:58components on the page. If we save
  2200. 1:48:03and then go to our project,
  2201. 1:48:06it displays all the chat message
  2202. 1:48:08components that we generated with our
  2203. 1:48:10JavaScript.
  2204. 1:48:11So now we're generating this HTML with
  2205. 1:48:14JavaScript and we no longer have to type
  2206. 1:48:17out each component manually.
  2207. 1:48:20Now, there's one more thing that's
  2208. 1:48:22useful to know. Let's go back to our
  2209. 1:48:24code.
  2210. 1:48:27And there's actually a shortcut that we
  2211. 1:48:29can use here. Instead of saving this
  2212. 1:48:32result in a variable, we can actually
  2213. 1:48:35just insert this result directly in the
  2214. 1:48:37JSX.
  2215. 1:48:39So, we'll select this code
  2216. 1:48:42and then cut.
  2217. 1:48:45And between the curly brackets, we're
  2218. 1:48:47going to remove the variable and just
  2219. 1:48:49paste that code.
  2220. 1:48:52And we'll remove the semicolon at the
  2221. 1:48:54end.
  2222. 1:48:56And we'll select this code and press tab
  2223. 1:48:59a few times to fix the indenting.
  2224. 1:49:02And finally, we can remove this variable
  2225. 1:49:05up here.
  2226. 1:49:09Now we're just inserting the result of
  2227. 1:49:11this code directly in the JSX.
  2228. 1:49:14This shortcut is used a lot in React
  2229. 1:49:17tutorials and documentation. So it's
  2230. 1:49:20useful to know how it works.
  2231. 1:49:23Now let's save
  2232. 1:49:25and then check our website.
  2233. 1:49:28And everything still works. So that's
  2234. 1:49:30how we generate the HTML.
  2235. 1:49:35Before we move on, there's actually an
  2236. 1:49:37issue that we need to fix here. If we
  2237. 1:49:40look at our console and we scroll up,
  2238. 1:49:43we'll see an error saying each child in
  2239. 1:49:46a list should have a unique key prop. In
  2240. 1:49:50React, if we insert an array of
  2241. 1:49:53components, we need to give each
  2242. 1:49:55component a prop called key. The key
  2243. 1:49:59helps React track changes in the array.
  2244. 1:50:03So, we should give each component a key.
  2245. 1:50:06Let's go back to VS Code
  2246. 1:50:10and for each chat message, we're going
  2247. 1:50:12to add a prop called key equals.
  2248. 1:50:17And this key has to be unique. So, what
  2249. 1:50:21should we use as the key?
  2250. 1:50:23Usually when we have some data like this
  2251. 1:50:27in the real world, each piece of data
  2252. 1:50:30should have a unique ID that lets us
  2253. 1:50:33identify it. So let's add a unique ID to
  2254. 1:50:36each message to match what we would see
  2255. 1:50:39in the real world. So for the first
  2256. 1:50:41message after the sender, we'll add a
  2257. 1:50:44property called ID colon. And let's just
  2258. 1:50:49make it a string ID1 to keep it simple.
  2259. 1:50:53For the second message, we'll add an ID
  2260. 1:50:56with the string ID 2. For the third
  2261. 1:51:00message, we'll make the ID ID3. And for
  2262. 1:51:04the fourth message, we'll add an ID
  2263. 1:51:07string ID 4. So, we'll use these IDs for
  2264. 1:51:12practice. They just have to be unique.
  2265. 1:51:16And now we'll use this unique ID as the
  2266. 1:51:19key. So down here we'll set the key to
  2267. 1:51:22be equal to curly brackets and we'll get
  2268. 1:51:26the chat message do ID.
  2269. 1:51:30So chat message
  2270. 1:51:33id.
  2271. 1:51:35Again we use curly brackets here because
  2272. 1:51:38we want to save the result of this code
  2273. 1:51:41as the key. And now each component in
  2274. 1:51:44this array will have a unique key. Let's
  2275. 1:51:48save and check our website.
  2276. 1:51:52And now the error is gone from the
  2277. 1:51:54console.
  2278. 1:51:57Let's go back to our code.
  2279. 1:52:00And now we have a lot of code in our app
  2280. 1:52:04component.
  2281. 1:52:05To make our code easier to work with,
  2282. 1:52:08we're going to separate this code into
  2283. 1:52:10smaller pieces.
  2284. 1:52:12Notice that this code and this code are
  2285. 1:52:16related to chat messages.
  2286. 1:52:19So it makes sense to separate these two
  2287. 1:52:22pieces of code into a chat messages
  2288. 1:52:25component.
  2289. 1:52:26So we'll scroll up
  2290. 1:52:29above app. Let's create a new component
  2291. 1:52:32using function and we'll name it capital
  2292. 1:52:36C chat capital M messages
  2293. 1:52:41brackets and curly brackets.
  2294. 1:52:44And now we're going to move all the code
  2295. 1:52:46related to chat messages into this new
  2296. 1:52:49component. So we'll select this code
  2297. 1:52:54and cut.
  2298. 1:52:57And inside here, we're going to paste
  2299. 1:53:02and create some new lines. And we're
  2300. 1:53:04going to select this code
  2301. 1:53:08and cut.
  2302. 1:53:10And inside here, we're going to paste.
  2303. 1:53:15And at the end of this component, we
  2304. 1:53:18need to return the HTML.
  2305. 1:53:20So at the end we're going to return open
  2306. 1:53:24bracket and we'll remove the closed
  2307. 1:53:27bracket for now and we're going to use a
  2308. 1:53:29fragment to group all of these
  2309. 1:53:31components together. So we'll type less
  2310. 1:53:34than greater than and at the end of this
  2311. 1:53:38less than/greater than and we'll add the
  2312. 1:53:41close bracket at the end. And finally,
  2313. 1:53:45we can fix the indenting by pressing
  2314. 1:53:48tab. And we'll remove the indent here
  2315. 1:53:51and here as well, so that these are
  2316. 1:53:54vertically aligned.
  2317. 1:53:56Finally, now that we have this chat
  2318. 1:53:59messages component, we're going to
  2319. 1:54:01insert this into our app. So, we'll
  2320. 1:54:04scroll down into app and remove these
  2321. 1:54:07lines.
  2322. 1:54:10And to insert the chat messages
  2323. 1:54:12component, we'll use the component
  2324. 1:54:14syntax again. Less than capital C chat
  2325. 1:54:19capital M messages
  2326. 1:54:22slash greater than.
  2327. 1:54:25So now we just separated all that code
  2328. 1:54:27into its own component and it makes our
  2329. 1:54:30code much cleaner. Let's save
  2330. 1:54:34and check our website.
  2331. 1:54:37And everything still works. We just
  2332. 1:54:39reorganized our code into a chat
  2333. 1:54:41messages component.
  2334. 1:54:44Okay, so far we saved the data and we
  2335. 1:54:48generated the HTML. The last step to
  2336. 1:54:51building a website is to make it
  2337. 1:54:53interactive.
  2338. 1:54:55For example, when we click this send
  2339. 1:54:58button, we want to add a chat message to
  2340. 1:55:00the website. To make this website
  2341. 1:55:03interactive, we're going to use event
  2342. 1:55:06handlers.
  2343. 1:55:07An event handler lets us run a function
  2344. 1:55:11when we interact with a website.
  2345. 1:55:14Let's do an example. We'll go back to
  2346. 1:55:16our code
  2347. 1:55:19and we're going to scroll up to the chat
  2348. 1:55:21messages component
  2349. 1:55:23and then inside this fragment we're
  2350. 1:55:26going to create a new line and let's
  2351. 1:55:28create a new button using less than
  2352. 1:55:32button greater than and less than/button
  2353. 1:55:36greater than and inside this button
  2354. 1:55:39let's put the text send message.
  2355. 1:55:44So, we'll create a new button for
  2356. 1:55:45practice.
  2357. 1:55:47Next, we're going to give this button a
  2358. 1:55:49special prop. So, here we'll add a new
  2359. 1:55:53prop called on capital C click equals
  2360. 1:55:59curly brackets.
  2361. 1:56:01So, on click lets us run a function when
  2362. 1:56:05we click this button. So, above this,
  2363. 1:56:08let's create a function to run. So we'll
  2364. 1:56:11type function and let's name it send
  2365. 1:56:16message brackets and curly brackets
  2366. 1:56:21and inside this function let's just
  2367. 1:56:23console.log
  2368. 1:56:25the string send message.
  2369. 1:56:30Now that we have this function we're
  2370. 1:56:32going to save this function inside on
  2371. 1:56:35click. So between these curly brackets,
  2372. 1:56:38we're going to give the name of the
  2373. 1:56:39function send message.
  2374. 1:56:43So notice that again we used curly
  2375. 1:56:46brackets. Curly brackets lets us save
  2376. 1:56:49any type of value in a prop including a
  2377. 1:56:52function.
  2378. 1:56:54Now that we gave on click this function,
  2379. 1:56:57when we click this button, it's going to
  2380. 1:56:59run this function and console.log this
  2381. 1:57:02message. Let's save
  2382. 1:57:06and we'll go to our website
  2383. 1:57:09and try clicking this new send message
  2384. 1:57:11button
  2385. 1:57:13and this will run the function and
  2386. 1:57:15display the message send message in the
  2387. 1:57:18console.
  2388. 1:57:19Let's go back to our code
  2389. 1:57:23and we'll learn more details about event
  2390. 1:57:25handlers.
  2391. 1:57:27So on click is known as the event and
  2392. 1:57:31send message is known as the event
  2393. 1:57:33handler. It does something when this
  2394. 1:57:36event happens or it handles the event.
  2395. 1:57:41Now there's two other things to note.
  2396. 1:57:43The event prop always starts with on and
  2397. 1:57:47then the name of the event like click
  2398. 1:57:50and the event prop must use camelc case.
  2399. 1:57:54As a review, camelc case means the first
  2400. 1:57:57word is lowercase and all other words
  2401. 1:58:00start with a capital. So that's why on
  2402. 1:58:03is lowercase and click starts with a
  2403. 1:58:05capital C.
  2404. 1:58:08Another thing we should know is that
  2405. 1:58:10here we should not run the event handler
  2406. 1:58:13function. So we should not use brackets.
  2407. 1:58:17So this will run the function and then
  2408. 1:58:19the result of the function will be
  2409. 1:58:21undefined. and then it's going to save
  2410. 1:58:24undefined into on click which doesn't
  2411. 1:58:27work. Instead, we should just give the
  2412. 1:58:30function directly to on click without
  2413. 1:58:33running it.
  2414. 1:58:35So that's event handlers and event
  2415. 1:58:37handler lets us run a function when we
  2416. 1:58:41interact with a website and they make
  2417. 1:58:43the website interactive.
  2418. 1:58:47Next, when we click this button, instead
  2419. 1:58:50of just console.log, log. Let's add a
  2420. 1:58:54new chat message. To do that, we can add
  2421. 1:58:57a new object to the chat messages array
  2422. 1:59:00up here. So, inside this function, let's
  2423. 1:59:04remove console.log
  2424. 1:59:06and then type the name of the array chat
  2425. 1:59:10messages and then use the code.push
  2426. 1:59:15and brackets.
  2427. 1:59:17As a review, dot push adds a value to
  2428. 1:59:21the end of an array. So inside here,
  2429. 1:59:25we're going to add another object using
  2430. 1:59:27curly brackets.
  2431. 1:59:30And each object will have a message
  2432. 1:59:32property. So we'll type message colon
  2433. 1:59:37and then a string. And for our new
  2434. 1:59:39message, let's just make the message
  2435. 1:59:42test.
  2436. 1:59:43And we'll add a comma. And we'll add a
  2437. 1:59:46sender property sender colon a string
  2438. 1:59:51and this message will be from the user
  2439. 1:59:54comma and we'll give it an ID colon and
  2440. 1:59:58let's just use the string ID5.
  2441. 2:00:02Now there's one problem with using ID5
  2442. 2:00:05in this situation.
  2443. 2:00:07Every message that we create when we
  2444. 2:00:10click this button is going to have the
  2445. 2:00:12ID ID5. But the ID should be unique. To
  2446. 2:00:17create a unique ID, we can use some
  2447. 2:00:20JavaScript code. So instead of ID5,
  2448. 2:00:23we're going to use the code crypto dot
  2449. 2:00:28random capital U, capital U, capital I,
  2450. 2:00:32capital D, and brackets.
  2451. 2:00:36So this is some JavaScript code that
  2452. 2:00:38will generate a unique ID string every
  2453. 2:00:41time we run this code. So now every
  2454. 2:00:44message that we create using this button
  2455. 2:00:46will have a unique ID
  2456. 2:00:50at the end. Let's create some new lines
  2457. 2:00:53and let's console.log
  2458. 2:00:56the chat messages array to see if it has
  2459. 2:01:00a new message. Let's save
  2460. 2:01:04and we'll go to our website
  2461. 2:01:06and we'll click send message.
  2462. 2:01:10And now inside chat messages, we have
  2463. 2:01:13five messages. So we added this last one
  2464. 2:01:16here. It has the message test. And
  2465. 2:01:20notice it has a unique ID. So we
  2466. 2:01:23successfully added a chat message when
  2467. 2:01:26clicking this button.
  2468. 2:01:30Next, notice that even though we updated
  2469. 2:01:33the data, it did not update the website.
  2470. 2:01:36So, it did not display a new chat
  2471. 2:01:39message component on the website. To fix
  2472. 2:01:42this, we're going to learn one of the
  2473. 2:01:44most important features of React called
  2474. 2:01:47state. State is data that is connected
  2475. 2:01:52to the HTML. When we update this data,
  2476. 2:01:56it will update the HTML.
  2477. 2:02:00Let's go back to our code.
  2478. 2:02:03And if we scroll up,
  2479. 2:02:06currently chat messages is just a normal
  2480. 2:02:09variable. When we update chat messages,
  2481. 2:02:13it will not update the HTML.
  2482. 2:02:16However, if we convert chat messages
  2483. 2:02:19into state, when we update chat
  2484. 2:02:22messages, it will update the HTML. So
  2485. 2:02:25now let's learn how to convert chat
  2486. 2:02:27messages into state.
  2487. 2:02:31At the top of this component, we're
  2488. 2:02:33going to use this code capital R react
  2489. 2:02:37dot use capital S state and brackets.
  2490. 2:02:44And between the brackets, we're going to
  2491. 2:02:47give it the chat messages data. So,
  2492. 2:02:50we're going to select this data
  2493. 2:02:53and cut.
  2494. 2:02:56and between these brackets we're going
  2495. 2:02:58to paste.
  2496. 2:03:05So this will convert this data into
  2497. 2:03:08state. So now if we update this data, it
  2498. 2:03:12will also update the HTML.
  2499. 2:03:16So at the bottom here, we can remove the
  2500. 2:03:18chat messages variable because we
  2501. 2:03:21converted it into state.
  2502. 2:03:25Next, react doouse state returns an
  2503. 2:03:28array. So at the front, let's save the
  2504. 2:03:31result in a variable const array equals
  2505. 2:03:36react. State.
  2506. 2:03:40This array has two values. To get the
  2507. 2:03:43first value, we can click here to
  2508. 2:03:46collapse the code for now. And to get
  2509. 2:03:49the first value, we're going to type the
  2510. 2:03:51name of the array. Array and then square
  2511. 2:03:54brackets zero.
  2512. 2:03:57So remember, an array is a list of
  2513. 2:03:59values and square bracket zero gets us
  2514. 2:04:02the first value in the list or in the
  2515. 2:04:04array.
  2516. 2:04:06So the first value we get from use state
  2517. 2:04:09is the current data.
  2518. 2:04:12So here chat messages can change over
  2519. 2:04:16time. It's going to start off as this
  2520. 2:04:18value which is the initial value and
  2521. 2:04:22then we might add new chat messages in
  2522. 2:04:24the future.
  2523. 2:04:27So the first value in this array gives
  2524. 2:04:29us the current value of chat messages or
  2525. 2:04:32the current data.
  2526. 2:04:35Let's save this in a variable so we can
  2527. 2:04:37use it later. We'll do const and let's
  2528. 2:04:41name it chat messages equals array zero.
  2529. 2:04:47So now we have the most current chat
  2530. 2:04:49messages and we can use it down here to
  2531. 2:04:52generate the HTML.
  2532. 2:04:55Next use state gives us a second value
  2533. 2:04:58in this array. To get the second value
  2534. 2:05:02we're going to type array square bracket
  2535. 2:05:041.
  2536. 2:05:07And the second value of this array is a
  2537. 2:05:10function that updates this data.
  2538. 2:05:14So let's save this function in a
  2539. 2:05:16variable using const. And the naming
  2540. 2:05:20convention for this function is a use
  2541. 2:05:23set. And then the name of the data which
  2542. 2:05:26is capital C chat capital M messages
  2543. 2:05:32equals this value. And we should also
  2544. 2:05:35use camel case here. So the first word
  2545. 2:05:37starts with a lowercase and every other
  2546. 2:05:40word starts with a capital.
  2547. 2:05:43So this function lets us update the
  2548. 2:05:45data. In React, we should not update the
  2549. 2:05:49data directly like we did down here
  2550. 2:05:52using push. We should always use this
  2551. 2:05:55function to update the data. And that's
  2552. 2:05:58because this function tells React that
  2553. 2:06:01the data changed and to update the HTML.
  2554. 2:06:05If we update the data directly, React
  2555. 2:06:07will not update the HTML.
  2556. 2:06:10If we use this function to update the
  2557. 2:06:13data, React will update the HTML. So we
  2558. 2:06:17should always use this function to
  2559. 2:06:19update the data. This is also known as
  2560. 2:06:22the updater function.
  2561. 2:06:27So now let's give it a try. Instead of
  2562. 2:06:29updating this data directly, we're going
  2563. 2:06:32to use this updater function by typing
  2564. 2:06:36set chat messages and brackets.
  2565. 2:06:42And between the brackets, we're going to
  2566. 2:06:44put the new value of chat messages.
  2567. 2:06:48So the way that this updator function
  2568. 2:06:50works is it's going to replace the
  2569. 2:06:53entire chat messages array with a new
  2570. 2:06:56array. So let's give it a try. First, we
  2571. 2:07:00need to make a copy of this array.
  2572. 2:07:04In React, we should not modify the data
  2573. 2:07:06directly. We should always create a copy
  2574. 2:07:09of the data and then modify the copy.
  2575. 2:07:13This helps React be more efficient.
  2576. 2:07:16To create a copy, we're going to click
  2577. 2:07:19between these brackets and create a new
  2578. 2:07:22array using square brackets.
  2579. 2:07:26And to copy chat messages, we can use
  2580. 2:07:29this code dot dot dot and the array that
  2581. 2:07:33we want to copy, which is chat messages.
  2582. 2:07:38As a review, this is a JavaScript
  2583. 2:07:41feature called the spread operator. It
  2584. 2:07:44basically takes the values in an array
  2585. 2:07:47and then copies them into a new array.
  2586. 2:07:51You can think of it like we're taking
  2587. 2:07:53the values in this array and then
  2588. 2:07:55spreading them out into this new array.
  2589. 2:07:58That's why it's called the spread
  2590. 2:08:00operator.
  2591. 2:08:03The spread operator is used a lot in
  2592. 2:08:05React tutorials and documentation. So,
  2593. 2:08:08it's useful to know how it works.
  2594. 2:08:12So, we created a copy of chat messages.
  2595. 2:08:16And now we want to add a new chat
  2596. 2:08:19message at the end of this copy because
  2597. 2:08:22we're trying to send a new message. To
  2598. 2:08:24do that, we can just add a comma at the
  2599. 2:08:27end and then add a new chat message
  2600. 2:08:30directly using curly brackets.
  2601. 2:08:34So each chat message has a message
  2602. 2:08:36property colon and we'll give it the
  2603. 2:08:39string test for now. And each chat
  2604. 2:08:43message has a sender property colon. And
  2605. 2:08:46let's make the sender the user.
  2606. 2:08:49And we'll give each chat message an ID.
  2607. 2:08:53And to create a unique ID we can use
  2608. 2:08:56crypto
  2609. 2:08:58random capital U capital U capital I
  2610. 2:09:03capital D and brackets.
  2611. 2:09:07So using the spread operator we made a
  2612. 2:09:10copy of the chat messages array and then
  2613. 2:09:13we added a value at the end of this new
  2614. 2:09:16array.
  2615. 2:09:17And now this updater function will
  2616. 2:09:20replace chat messages with this new
  2617. 2:09:23array. And because we use this updater
  2618. 2:09:26function, React will regenerate the
  2619. 2:09:29HTML. So it will basically rerun all
  2620. 2:09:32this code and it will update the
  2621. 2:09:34website. So now let's save
  2622. 2:09:38and let's check our website
  2623. 2:09:41and we'll press send message.
  2624. 2:09:44it will create a new chat message in our
  2625. 2:09:47data and it will also update the HTML.
  2626. 2:09:51So it will create a new chat message
  2627. 2:09:53component and display it on the website.
  2628. 2:09:57Now let's go back
  2629. 2:10:01and that is how state works. Let's do a
  2630. 2:10:04short review. State is basically data
  2631. 2:10:07that is connected to the HTML. When we
  2632. 2:10:11update this data or we update the state,
  2633. 2:10:14it will update the HTML.
  2634. 2:10:17To convert some data into state, we use
  2635. 2:10:20react doouse state. So here it will
  2636. 2:10:23convert this data into state and use
  2637. 2:10:27state gives us two values.
  2638. 2:10:31The first value is the current data and
  2639. 2:10:35then the second value is a function to
  2640. 2:10:37update this data. If we use the updater
  2641. 2:10:41function, it will also update the HTML.
  2642. 2:10:45So don't worry if state is a little bit
  2643. 2:10:48confusing at first. The more you work
  2644. 2:10:50with state, the more you'll get used to
  2645. 2:10:52it. And we'll do more examples of state
  2646. 2:10:54in this course and in the exercises so
  2647. 2:10:57you can get more practice.
  2648. 2:11:01Next, we're going to learn a shortcut
  2649. 2:11:03called array destructuring.
  2650. 2:11:06Here we get the first value out of this
  2651. 2:11:09array and save it in a variable called
  2652. 2:11:12chat messages.
  2653. 2:11:14Now JavaScript actually has a shortcut
  2654. 2:11:17for this. Above this we can type const
  2655. 2:11:22and then square bracket equals this
  2656. 2:11:25array
  2657. 2:11:27and inside the square brackets we can
  2658. 2:11:29type chat messages.
  2659. 2:11:33So this is similar to object
  2660. 2:11:35dstructuring. This gets the first value
  2661. 2:11:39out of this array and saves it in a
  2662. 2:11:41variable called chat messages. So this
  2663. 2:11:44code is a shortcut for this code. We can
  2664. 2:11:48also get the second value out of this
  2665. 2:11:51array. At the end here we can type comma
  2666. 2:11:55and then another variable name. Let's
  2667. 2:11:57use set chat messages.
  2668. 2:12:02So this gets the second value out of
  2669. 2:12:05this array and saves it in a variable
  2670. 2:12:08called set chat messages. So this code
  2671. 2:12:12is a shortcut for these two lines of
  2672. 2:12:15code. And this shortcut is called array
  2673. 2:12:18dstructuring because we're taking stuff
  2674. 2:12:21out of this array. We're deconstructing
  2675. 2:12:24it. Now in array dstructuring the order
  2676. 2:12:28matters. So this will always get the
  2677. 2:12:31first value out of the array and this
  2678. 2:12:33will always get the second value out of
  2679. 2:12:36the array.
  2680. 2:12:38So now let's comment out these two
  2681. 2:12:40lines. So we remember that this is just
  2682. 2:12:42a shortcut. So we'll type double slash
  2683. 2:12:45at the front and here double slash at
  2684. 2:12:48the front.
  2685. 2:12:50Now another shortcut we can use is
  2686. 2:12:52instead of saving the result of use
  2687. 2:12:55state in this variable and then
  2688. 2:12:57dstructuring it we can just dstructure
  2689. 2:13:00this up here.
  2690. 2:13:02So instead of array we can just directly
  2691. 2:13:05dstructure using square brackets and
  2692. 2:13:08then the first value of u state is going
  2693. 2:13:11to be chat messages
  2694. 2:13:14and the second value from state is going
  2695. 2:13:16to be the updater function which is set
  2696. 2:13:20chat messages.
  2697. 2:13:23So this code is a shortcut for this
  2698. 2:13:26code. We just don't save it in a
  2699. 2:13:28variable in the middle.
  2700. 2:13:31So at the front of this code, let's also
  2701. 2:13:33comment it out using double slash.
  2702. 2:13:37So that is array dstructuring. It gives
  2703. 2:13:40us an easier way to take values out of
  2704. 2:13:43an array.
  2705. 2:13:45Now this shortcut is used a lot in React
  2706. 2:13:47tutorials and documentation. So it's
  2707. 2:13:50useful to know how it works.
  2708. 2:13:54Next, instead of adding the same message
  2709. 2:13:57test every time, we should add whatever
  2710. 2:14:00message is in the text box. So, we'll
  2711. 2:14:03learn how to get the text in a text box.
  2712. 2:14:07In normal JavaScript, we might use the
  2713. 2:14:09DOM to get the input element and then
  2714. 2:14:13use a property called dot value to get
  2715. 2:14:16the text inside the text box.
  2716. 2:14:21However, in React, a best practice is do
  2717. 2:14:24not use the DOM manually. That's because
  2718. 2:14:27React is managing the website. So, if we
  2719. 2:14:30use the DOM manually, it might interfere
  2720. 2:14:33with React.
  2721. 2:14:36Instead, we should use React features to
  2722. 2:14:39get the text from a text box. One way to
  2723. 2:14:42do this is we're going to scroll up to
  2724. 2:14:46the text box which is inside the chat
  2725. 2:14:50input component and it's the input
  2726. 2:14:52element here. So to get the text in this
  2727. 2:14:55text box we can use a prop called on
  2728. 2:15:00capital C change equals curly brackets.
  2729. 2:15:06Earlier we learned about the event on
  2730. 2:15:09click which runs a function when we
  2731. 2:15:11click an element. On change is another
  2732. 2:15:15event we can use in React. You can tell
  2733. 2:15:18it's an event because it starts with on.
  2734. 2:15:22So onchange runs a function when we
  2735. 2:15:24change the text inside an input.
  2736. 2:15:28So above this let's create a function to
  2737. 2:15:31run. We'll type function
  2738. 2:15:35and let's name it save input text
  2739. 2:15:40brackets and curly brackets because
  2740. 2:15:43we're going to save the text inside this
  2741. 2:15:46input.
  2742. 2:15:48And now let's save this function inside
  2743. 2:15:50on change. So between the curly brackets
  2744. 2:15:53we'll give it the function save input
  2745. 2:15:57text. So again we use curly brackets
  2746. 2:16:01here because we want to save a function
  2747. 2:16:04inside this prop rather than just a
  2748. 2:16:06string. So now every time we change the
  2749. 2:16:10text in this input it will run this
  2750. 2:16:13function.
  2751. 2:16:15Next when onchange runs this function
  2752. 2:16:19it's going to give this function one
  2753. 2:16:21parameter up here called event.
  2754. 2:16:26event is an object and it contains
  2755. 2:16:28details about the event or about the
  2756. 2:16:31change. So one detail is event dot
  2757. 2:16:37target
  2758. 2:16:39and this gives us the element that we're
  2759. 2:16:41typing in or that we changed. So in this
  2760. 2:16:45case it's going to give us this input
  2761. 2:16:47element. So React gives us access to
  2762. 2:16:50this input element automatically using
  2763. 2:16:53event.target. target.
  2764. 2:16:55And now to get the text inside this
  2765. 2:16:58input, we're going to use the property
  2766. 2:17:01value.
  2767. 2:17:03So now let's console.log this to see
  2768. 2:17:05what it looks like. console.log
  2769. 2:17:10and let's save our code
  2770. 2:17:14and we'll go back to our website
  2771. 2:17:17and we'll give it a try. So we'll click
  2772. 2:17:19this text box and we're going to type
  2773. 2:17:22hello.
  2774. 2:17:24And you'll see that every time we type
  2775. 2:17:26or change the text, it will console.log
  2776. 2:17:29the text in the text box. So that is how
  2777. 2:17:33we get the text inside the text box
  2778. 2:17:36using an onchange event.
  2779. 2:17:40Let's go back to our code.
  2780. 2:17:44And now we need to save this text so we
  2781. 2:17:48can use it later when creating a chat
  2782. 2:17:50message. To save this text, we're going
  2783. 2:17:53to use state. In React, we should use
  2784. 2:17:56state to save data that changes over
  2785. 2:17:59time, like the text in the text box. So,
  2786. 2:18:03at the top of this component,
  2787. 2:18:06let's use the code react dot use state
  2788. 2:18:11and brackets.
  2789. 2:18:14And between the brackets, we're going to
  2790. 2:18:16put the initial value. In this case, the
  2791. 2:18:20initial value in the text box.
  2792. 2:18:23The text box originally starts as empty.
  2793. 2:18:26So let's set the value to the empty
  2794. 2:18:28string.
  2795. 2:18:30Next, use state returns an array with
  2796. 2:18:34two values. So at the front, let's use
  2797. 2:18:37array dstructuring using const and we'll
  2798. 2:18:41type square brackets equals this result.
  2799. 2:18:46And now to get the first value out of
  2800. 2:18:48this array inside the square brackets
  2801. 2:18:51we're going to type input text.
  2802. 2:18:56So this is the current data or the
  2803. 2:18:59current text in the text box. So that's
  2804. 2:19:03why we name it input text.
  2805. 2:19:06To get the second value out of this
  2806. 2:19:08array after this we can type comma and
  2807. 2:19:12give a name for the second value. So the
  2808. 2:19:15naming convention is to use the word set
  2809. 2:19:18and then the name of the data which is
  2810. 2:19:22capital I input capital T text.
  2811. 2:19:27So this second value is an updator
  2812. 2:19:30function. It lets us update the data.
  2813. 2:19:34Now that we set up the state, every time
  2814. 2:19:37we change the text, we're going to save
  2815. 2:19:39it up here inside input text. To do
  2816. 2:19:43that, instead of console.log,
  2817. 2:19:46we're going to use the updater function
  2818. 2:19:49set input text.
  2819. 2:19:53And now, every time we change the text,
  2820. 2:19:56it's going to run this function. And
  2821. 2:19:58it's going to save the text inside input
  2822. 2:20:02text.
  2823. 2:20:05Next, when we click the send button, we
  2824. 2:20:08want to use this text to create a new
  2825. 2:20:11chat message. First, we need to make the
  2826. 2:20:14send button interactive.
  2827. 2:20:16To do that, inside this button element,
  2828. 2:20:20we're going to add the prop on click
  2829. 2:20:23equals curly brackets.
  2830. 2:20:27Again, on click lets us run a function
  2831. 2:20:30when we click this button.
  2832. 2:20:33And now above this, let's create a
  2833. 2:20:35function to run. We'll use function
  2834. 2:20:39and let's name it send message brackets
  2835. 2:20:44and curly brackets and we're going to
  2836. 2:20:46give this function to on click. So down
  2837. 2:20:49here we'll give the function name send
  2838. 2:20:52message.
  2839. 2:20:55Now when we click this button it will
  2840. 2:20:58run this function and let's just
  2841. 2:21:00console.log input text to see what the
  2842. 2:21:03value is. So in here we'll console.log
  2843. 2:21:09brackets the input text and save.
  2844. 2:21:15And we'll go back to our website
  2845. 2:21:18and in the text box we're going to type
  2846. 2:21:22hello and then press send.
  2847. 2:21:26And you'll see that it console.logs the
  2848. 2:21:29text inside the text box.
  2849. 2:21:34Next, we're going to create a new chat
  2850. 2:21:36message using this text. Let's go back
  2851. 2:21:40to our code.
  2852. 2:21:42And if we scroll down in our code
  2853. 2:21:47to the chat messages component, we
  2854. 2:21:51already have code here that adds a new
  2855. 2:21:54message to chat messages. So, let's just
  2856. 2:21:57select this code and we'll copy.
  2857. 2:22:01And at the top
  2858. 2:22:04inside send message, we're going to
  2859. 2:22:07paste. And we can remove this
  2860. 2:22:09console.log.
  2861. 2:22:12And we'll scroll back down.
  2862. 2:22:16And we created this function and this
  2863. 2:22:18button for practice. But now we don't
  2864. 2:22:21need it anymore. So we can actually
  2865. 2:22:23remove this code
  2866. 2:22:25and remove this practice button.
  2867. 2:22:30Now, let's scroll back up
  2868. 2:22:35and we're going to use this code to add
  2869. 2:22:37a new chat message.
  2870. 2:22:40However, we have a problem here. We
  2871. 2:22:43don't actually have access to set chat
  2872. 2:22:45messages inside this component because
  2873. 2:22:49this is inside a different component. To
  2874. 2:22:52solve this problem, we're going to learn
  2875. 2:22:54a technique called lifting the state up.
  2876. 2:22:59First we need to visualize the
  2877. 2:23:01components in our app. So we have an app
  2878. 2:23:05component which represents the entire
  2879. 2:23:07application and inside we have a chat
  2880. 2:23:10input component and a chat messages
  2881. 2:23:13component. Inside the chat messages
  2882. 2:23:16component we have some chat message
  2883. 2:23:18components.
  2884. 2:23:20This is called the component tree
  2885. 2:23:22because it looks like the branch of a
  2886. 2:23:25tree and this helps us visualize the
  2887. 2:23:28structure of the components in our app.
  2888. 2:23:32Currently, chat messages and set chat
  2889. 2:23:35messages are located inside the chat
  2890. 2:23:38messages component, but we want to use
  2891. 2:23:41it in the chat input component as well.
  2892. 2:23:45To solve this problem, we're going to
  2893. 2:23:47move this state up into the app
  2894. 2:23:50component and then app can share chat
  2895. 2:23:53messages and set chat messages between
  2896. 2:23:56these two components.
  2897. 2:23:58This technique is called lifting the
  2898. 2:24:00state up. By moving the state up to an
  2899. 2:24:04outer component, it lets us share state
  2900. 2:24:07between multiple components.
  2901. 2:24:11So, let's give this a try. First, we're
  2902. 2:24:14going to move the chat messages state up
  2903. 2:24:18into app. So, in our code, we're going
  2904. 2:24:22to scroll down
  2905. 2:24:24to the chat messages state, which is
  2906. 2:24:26right here, and we're going to move it
  2907. 2:24:29into the app component. So, we'll select
  2908. 2:24:32all this code,
  2909. 2:24:34and then cut
  2910. 2:24:37and remove these lines.
  2911. 2:24:40And then inside the app component,
  2912. 2:24:43we'll create some new lines and paste.
  2913. 2:24:48So now we lifted the state up into the
  2914. 2:24:52app component.
  2915. 2:24:54Next, we're going to share the state
  2916. 2:24:56between the two components using props.
  2917. 2:25:02So first, the chat input component needs
  2918. 2:25:05set chat messages and chat messages.
  2919. 2:25:10So down here, let's create two props for
  2920. 2:25:14this component.
  2921. 2:25:16We'll create a prop called chat messages
  2922. 2:25:21equals curly brackets. And we're going
  2923. 2:25:23to save the value of chat messages from
  2924. 2:25:26the state. So we'll save chat messages.
  2925. 2:25:31And we'll give it another prop called
  2926. 2:25:34set chat messages equals curly brackets.
  2927. 2:25:39And we'll give it the set chat messages
  2928. 2:25:41function from the state.
  2929. 2:25:45So set chat messages.
  2930. 2:25:50By the way, notice that chat messages is
  2931. 2:25:53an array. So using curly brackets, we
  2932. 2:25:56can save an array into this prop. So we
  2933. 2:26:00can save any type of value inside a
  2934. 2:26:02prop.
  2935. 2:26:05And then inside chat input, we can
  2936. 2:26:08access these two props by scrolling up.
  2937. 2:26:13And in the chat input component at the
  2938. 2:26:16top, we'll type curly brackets and we'll
  2939. 2:26:19get the chat messages prop,
  2940. 2:26:24and we'll also get the set chat messages
  2941. 2:26:28prop.
  2942. 2:26:31And now this code has access to set chat
  2943. 2:26:34messages and chat messages.
  2944. 2:26:39Next, let's scroll back down
  2945. 2:26:42to the chat messages component.
  2946. 2:26:45So here we just need the chat messages
  2947. 2:26:48array to generate the HTML. So let's
  2948. 2:26:52provide chat messages using a prop at
  2949. 2:26:55the bottom
  2950. 2:26:58at the chat messages component. We're
  2951. 2:27:00going to add a prop. chat messages
  2952. 2:27:04equals curly brackets and we'll give it
  2953. 2:27:07the chat messages array. So here chat
  2954. 2:27:12messages.
  2955. 2:27:15Now I want to talk about the naming
  2956. 2:27:17convention used here. You'll notice that
  2957. 2:27:19the component, the prop and the data all
  2958. 2:27:23use the same name. Instead of using
  2959. 2:27:26names like this,
  2960. 2:27:30this is the naming convention used in
  2961. 2:27:32the React documentation.
  2962. 2:27:35The documentation tries to use the same
  2963. 2:27:37name for many things like the component,
  2964. 2:27:40the props, and the data. This keeps
  2965. 2:27:43things simple and consistent. We don't
  2966. 2:27:45have to come up with different names
  2967. 2:27:47every time. And when we look at other
  2968. 2:27:49people's React code, the names will be
  2969. 2:27:52the same.
  2970. 2:27:54So, don't worry. It might be a bit
  2971. 2:27:56confusing at first because many things
  2972. 2:27:58use the same name. The more you work
  2973. 2:28:01with React, the more you'll get used to
  2974. 2:28:03this naming convention, and we'll get
  2975. 2:28:06lots of practice in this course.
  2976. 2:28:10Now to access this prop, we're going to
  2977. 2:28:13scroll up
  2978. 2:28:16and up here we'll type curly bracket
  2979. 2:28:20and we'll get the chat messages prop.
  2980. 2:28:24And now this code has access to
  2981. 2:28:27everything that it needs. Let's save our
  2982. 2:28:30code and then check our website.
  2983. 2:28:34And you'll notice that everything still
  2984. 2:28:36works. We just lifted the state up into
  2985. 2:28:40the app component and shared it between
  2986. 2:28:43chat input and chat messages.
  2987. 2:28:50Let's go back to our code
  2988. 2:28:53and we're going to scroll up to the chat
  2989. 2:28:56input component.
  2990. 2:28:59So now that chat input has access to set
  2991. 2:29:02chat messages, let's use it to create a
  2992. 2:29:06new chat message. So when we click send,
  2993. 2:29:10we're going to run set chat messages and
  2994. 2:29:12we're going to give it a copy of the
  2995. 2:29:15chat messages array with a new message
  2996. 2:29:18at the end. In this new message, instead
  2997. 2:29:22of test every time, we're going to use
  2998. 2:29:24the text inside the text box, which is
  2999. 2:29:27saved in input text. So, we'll remove
  3000. 2:29:30this and replace it with input text.
  3001. 2:29:36The sender will be the user because we
  3002. 2:29:39sent this message. And we'll give the
  3003. 2:29:41message a unique ID. And that's it. This
  3004. 2:29:45will update the chat messages array with
  3005. 2:29:48this new message. And because we use the
  3006. 2:29:51updater function, React will also update
  3007. 2:29:54the HTML.
  3008. 2:29:56So, let's save.
  3009. 2:29:59And we'll go to our project.
  3010. 2:30:02Inside this text box, let's type hello
  3011. 2:30:06and press send.
  3012. 2:30:09It will take the text in the text box,
  3013. 2:30:12create a new chat message, and it will
  3014. 2:30:14update the HTML.
  3015. 2:30:19Next, there is one improvement that we
  3016. 2:30:22can make at this point. If we scroll up
  3017. 2:30:25after we press send, we should also
  3018. 2:30:28remove the text in the text box. So, we
  3019. 2:30:31don't have to manually remove it if we
  3020. 2:30:33want to send another message.
  3021. 2:30:35To do that, we're going to learn a
  3022. 2:30:37feature called controlled inputs.
  3023. 2:30:42Let's go back to our code.
  3024. 2:30:45And after adding a new message, we're
  3025. 2:30:48going to remove the text in the text
  3026. 2:30:50box. So after this, let's use the
  3027. 2:30:54updater function. Set input text
  3028. 2:30:59brackets
  3029. 2:31:00will change the input text to the empty
  3030. 2:31:03string.
  3031. 2:31:05So this will update the input text up
  3032. 2:31:08here to be empty.
  3033. 2:31:11However, it will not update the HTML
  3034. 2:31:14yet.
  3035. 2:31:16To update the HTML, we need to use this
  3036. 2:31:19input text inside this HTML.
  3037. 2:31:24To do that, we're going to give the
  3038. 2:31:25input element a prop called value equals
  3039. 2:31:30curly brackets.
  3040. 2:31:34So value lets us change the value of
  3041. 2:31:37this input or the text inside this
  3042. 2:31:40input.
  3043. 2:31:42So we're going to set the value to
  3044. 2:31:44whatever input text is.
  3045. 2:31:48So now if we set the input text to be
  3046. 2:31:51empty, this will set the value to be
  3047. 2:31:54empty as well and it will make the text
  3048. 2:31:56box empty.
  3049. 2:31:59Let's save.
  3050. 2:32:01We'll go back to our website.
  3051. 2:32:04Inside the text box, we're going to type
  3052. 2:32:07hello and press send.
  3053. 2:32:10And you'll notice that it makes the text
  3054. 2:32:12box empty after we send. So let's go
  3055. 2:32:16back to our code.
  3056. 2:32:19And this is called a controlled input.
  3057. 2:32:22Using the value prop, we can control the
  3058. 2:32:26text inside this text box using our
  3059. 2:32:29code.
  3060. 2:32:32The last feature we're going to work on
  3061. 2:32:33in this lesson is if we scroll up after
  3062. 2:32:38we send a message, we're going to get a
  3063. 2:32:40response from the chatbot.
  3064. 2:32:45To get a response, we're going to use
  3065. 2:32:47another external library. So, we'll
  3066. 2:32:50scroll up to the script elements up
  3067. 2:32:53here. And to add another external
  3068. 2:32:56library, we're going to add another
  3069. 2:32:58script element.
  3070. 2:33:00And we're going to give this element the
  3071. 2:33:02source attribute equals double quotes.
  3072. 2:33:07And we're going to give it the URL of
  3073. 2:33:09the external library. Now, this is
  3074. 2:33:12another external library that I wrote.
  3075. 2:33:15So, let's just copy one of these URLs up
  3076. 2:33:17here with super simple dev. And we'll
  3077. 2:33:20copy.
  3078. 2:33:22And inside here, we're going to paste.
  3079. 2:33:25And then at the end, instead of
  3080. 2:33:27react.js,
  3081. 2:33:29we're going to load chatbot.js
  3082. 2:33:33and save.
  3083. 2:33:36So now, let's take a look at the code in
  3084. 2:33:38this URL.
  3085. 2:33:40On Windows, you can press control and
  3086. 2:33:42click this link. And on Mac, you can
  3087. 2:33:44press command and click this link.
  3088. 2:33:47If it doesn't open, you can also type
  3089. 2:33:49the URL in the browser.
  3090. 2:33:52So again, an external library is just
  3091. 2:33:55normal JavaScript code that someone else
  3092. 2:33:58wrote. In this case, it's some code that
  3093. 2:34:00I wrote. And this JavaScript code
  3094. 2:34:03creates a simple chatbot.
  3095. 2:34:06This is an object and it has a method
  3096. 2:34:09called get response.
  3097. 2:34:12We just have to give it our message and
  3098. 2:34:15it will return a response.
  3099. 2:34:19So now let's use this external library.
  3100. 2:34:23We'll go back to VS Code
  3101. 2:34:26and we'll scroll down to chat input. And
  3102. 2:34:30now after we send a message below this,
  3103. 2:34:35let's use the chatbot external library
  3104. 2:34:38to get a response. So the external
  3105. 2:34:40library creates a variable called
  3106. 2:34:43capital C chatbot.
  3107. 2:34:47This is an object and it has a method
  3108. 2:34:49called dot get response and brackets.
  3109. 2:34:56Between the brackets, we're just going
  3110. 2:34:58to put the message that we sent, which
  3111. 2:35:00is input text. So in here, we'll type
  3112. 2:35:04input text
  3113. 2:35:07and get response will return a response.
  3114. 2:35:10So let's save it in a variable using
  3115. 2:35:13const and we'll name it response.
  3116. 2:35:16equals this result. And next, let's just
  3117. 2:35:20console.log the response to see what it
  3118. 2:35:23looks like. console.log
  3119. 2:35:26the response
  3120. 2:35:29and save.
  3121. 2:35:32And now let's go to our website
  3122. 2:35:35and we're going to close the external
  3123. 2:35:37library for now. And then we're going to
  3124. 2:35:39click this text box and we'll type the
  3125. 2:35:42message hello and press send.
  3126. 2:35:47And now inside our console it will
  3127. 2:35:50display the response from the chatbot.
  3128. 2:35:52And the chatbot responded with hello how
  3129. 2:35:55can I help you? So using the chatbot
  3130. 2:35:58external library we're able to get a
  3131. 2:36:01response from the chatbot.
  3132. 2:36:05The last step is we're going to display
  3133. 2:36:08this chatbot response on the website.
  3134. 2:36:12Let's go back to VS Code.
  3135. 2:36:16And to display the response on the
  3136. 2:36:18website, we can just add another chat
  3137. 2:36:21message using set chat messages.
  3138. 2:36:25Because this is an updater function from
  3139. 2:36:27the state, React will automatically
  3140. 2:36:30update the website as well. So down here
  3141. 2:36:34we'll remove console.log
  3142. 2:36:38and let's just copy paste this code up
  3143. 2:36:40here to create a new message. So we'll
  3144. 2:36:43copy and then down here we'll paste
  3145. 2:36:49and here the message should be the
  3146. 2:36:52response from the chatbot. So we'll
  3147. 2:36:54replace this with response
  3148. 2:36:58and the sender is going to be the robot
  3149. 2:37:01this time. and we'll give it a unique
  3150. 2:37:04ID.
  3151. 2:37:06So now let's save this code and try it
  3152. 2:37:09out. We'll open the website and then in
  3153. 2:37:12the text box we'll type hello and press
  3154. 2:37:16send.
  3155. 2:37:19And if we look at the website, it sort
  3156. 2:37:22of worked. It displayed the response
  3157. 2:37:24from the chatbot on the website.
  3158. 2:37:28However, it also removed our hello
  3159. 2:37:30message. So why did this happen? Let's
  3160. 2:37:33go back to VS Code.
  3161. 2:37:36And if we look at this code
  3162. 2:37:39here, we updated chat messages by making
  3163. 2:37:43a copy and adding a new message at the
  3164. 2:37:46end. However, in React, state does not
  3165. 2:37:50update immediately. The state is updated
  3166. 2:37:54after all of the code is finished.
  3167. 2:37:59So here all of the code is not finished
  3168. 2:38:02yet. So chat messages is not updated to
  3169. 2:38:06this new array immediately.
  3170. 2:38:09So when we get to this point in the
  3171. 2:38:11code, this chat messages is still the
  3172. 2:38:15old array without the new message that
  3173. 2:38:17we added up here. And then we take the
  3174. 2:38:20old array and we add the chatbots
  3175. 2:38:22response. And we essentially lost our
  3176. 2:38:26message.
  3177. 2:38:28So one way to fix this issue is to save
  3178. 2:38:30this updated chat messages in a
  3179. 2:38:33variable. So above this we're going to
  3180. 2:38:37create a new variable using const and
  3181. 2:38:40let's name it new chat messages
  3182. 2:38:45equals
  3183. 2:38:47and we're going to save this value. So,
  3184. 2:38:49we'll select this code
  3185. 2:38:52and then cut.
  3186. 2:38:54And up here, we're going to paste.
  3187. 2:39:00And we'll set the chat messages to this
  3188. 2:39:02variable, new chat messages.
  3189. 2:39:07So, this does the same thing as before,
  3190. 2:39:10except we saved the new chat messages in
  3191. 2:39:13this variable.
  3192. 2:39:15And then down here, instead of making a
  3193. 2:39:18copy of chat messages, we're going to
  3194. 2:39:20make a copy of new chat messages, which
  3195. 2:39:25contains our new message that we just
  3196. 2:39:28added. So now it will have our message,
  3197. 2:39:31and we're going to add the chatbots
  3198. 2:39:33response. So now let's save this code
  3199. 2:39:38and go to our website.
  3200. 2:39:41In the text box, we're going to type
  3201. 2:39:44hello and press send.
  3202. 2:39:48And it displays our message, hello, and
  3203. 2:39:50the chatbot's response, hello, how can I
  3204. 2:39:53help you?
  3205. 2:39:56Let's try another example. In the text
  3206. 2:39:59box, we can type, "Can you flip a coin?"
  3207. 2:40:05and press send.
  3208. 2:40:08It will add and display our message and
  3209. 2:40:11it will add and display the chatbot's
  3210. 2:40:13response which is sure you got heads or
  3211. 2:40:16tails and everything is working
  3212. 2:40:19correctly.
  3213. 2:40:20So as you can see state is another way
  3214. 2:40:24that React helps us create websites
  3215. 2:40:26easier. All we have to do is update the
  3216. 2:40:30state and React will automatically
  3217. 2:40:32update the website. We don't have to
  3218. 2:40:35update the website manually ourselves.
  3219. 2:40:41And that's it. Using state, we created
  3220. 2:40:44the functionality that we need for this
  3221. 2:40:47project. In the next lesson, we'll style
  3222. 2:40:50this with CSS to make it look like the
  3223. 2:40:53final chatbot project.
  3224. 2:40:56And that's the end of this lesson. In
  3225. 2:40:59this lesson, we saved the data for our
  3226. 2:41:02website using arrays and objects. We
  3227. 2:41:06generated the HTML using map and the key
  3228. 2:41:11prop. We made the website interactive
  3229. 2:41:14using on click and onchange. We learned
  3230. 2:41:18state which is data that changes over
  3231. 2:41:21time and is connected to the HTML. We
  3232. 2:41:25used the updater function to update the
  3233. 2:41:28state and update the HTML at the same
  3234. 2:41:31time. We learned the array destructuring
  3235. 2:41:34shortcut. We learned a technique called
  3236. 2:41:37lifting the state up which lets us share
  3237. 2:41:40state between components. We made the
  3238. 2:41:44chat input component interactive and
  3239. 2:41:47created new chat messages. And we got
  3240. 2:41:50responses from the chatbot.
  3241. 2:41:53Here
  3242. 2:41:55are some exercises to help you practice
  3243. 2:41:57what we learned in this lesson.
  3244. 2:42:46In this lesson, we're going to learn how
  3245. 2:42:48to use CSS with React and we'll finish
  3246. 2:42:52the chatbot project. Let's go back to
  3247. 2:42:55our code
  3248. 2:42:57and we'll start by reviewing CSS.
  3249. 2:43:00CSS is another language and it lets us
  3250. 2:43:04change the appearance of the website. To
  3251. 2:43:07use CSS with React, we just use CSS
  3252. 2:43:11normally. So, one way to use CSS is to
  3253. 2:43:14create a style element.
  3254. 2:43:18So, we'll scroll to the top of our code
  3255. 2:43:21to the head element and then inside
  3256. 2:43:23here, we'll create a style element.
  3257. 2:43:29So, the style element doesn't appear on
  3258. 2:43:31the website. That's why we put it in the
  3259. 2:43:34head section.
  3260. 2:43:36And the special thing about the style
  3261. 2:43:38element is we can write CSS code inside.
  3262. 2:43:42In here we'll type the CSS code button
  3263. 2:43:46and curly brackets. And inside we'll
  3264. 2:43:50type background dash color
  3265. 2:43:55green and semicolon.
  3266. 2:43:57So this is some CSS code. As a review,
  3267. 2:44:01this code at the front is called the CSS
  3268. 2:44:04selector. This tells a computer which
  3269. 2:44:07elements we want to change the
  3270. 2:44:09appearance. And inside we have CSS
  3271. 2:44:13styles. This tells a computer how to
  3272. 2:44:16change the appearance of these elements.
  3273. 2:44:19The left side is called the CSS
  3274. 2:44:22property. This is what we're changing.
  3275. 2:44:25And the right side is called the CSS
  3276. 2:44:27value. This is what we're changing the
  3277. 2:44:29property to. So this code changes all
  3278. 2:44:34buttons on the website and changes their
  3279. 2:44:36background color to green. If we save
  3280. 2:44:41and then check our website,
  3281. 2:44:45you can see that it changed the
  3282. 2:44:46background color of the button to green.
  3283. 2:44:50Let's go back to our code.
  3284. 2:44:53And one problem here is button will
  3285. 2:44:56style all buttons on the website. But
  3286. 2:45:00usually we want to just style a specific
  3287. 2:45:02button. For example, let's scroll down
  3288. 2:45:06to the chat input component and we're
  3289. 2:45:09going to scroll down to this send button
  3290. 2:45:12here. So let's say we just want to style
  3291. 2:45:15this send button. As a review to style a
  3292. 2:45:19specific element, we can add an HTML
  3293. 2:45:22attribute called class. In React to set
  3294. 2:45:26the class attribute, we're going to give
  3295. 2:45:28this element a prop called class capital
  3296. 2:45:33N name equals double quotes.
  3297. 2:45:38And we'll give it the class send dash
  3298. 2:45:41button.
  3299. 2:45:43In React, we use class name to set the
  3300. 2:45:46class. That's because React is actually
  3301. 2:45:49just JavaScript code and JavaScript
  3302. 2:45:52already has a feature called class. So
  3303. 2:45:55class is a reserved word and we can't
  3304. 2:45:58use it. That's why React uses class name
  3305. 2:46:01to set the class.
  3306. 2:46:04Now that this button has the class
  3307. 2:46:06send-button,
  3308. 2:46:08we can style this specific element.
  3309. 2:46:11We'll go to our CSS
  3310. 2:46:15and in here we're going to remove button
  3311. 2:46:18and we're going to type dot and then the
  3312. 2:46:22class send dash button.
  3313. 2:46:26As a review, if we start the CSS
  3314. 2:46:28selector with a dot, it will only style
  3315. 2:46:31elements with this class. So this will
  3316. 2:46:35only style elements with the class
  3317. 2:46:37send-ash button. If we save
  3318. 2:46:42and check our website,
  3319. 2:46:44it styles our send button because we
  3320. 2:46:47gave it the send-button class.
  3321. 2:46:50If we open the console and go to the
  3322. 2:46:52elements tab and then we look at our
  3323. 2:46:55HTML here and we find the send button,
  3324. 2:46:59you'll notice that it has the class
  3325. 2:47:02attribute. So class name in React is
  3326. 2:47:05converted to class in HTML.
  3327. 2:47:10Now that we reviewed CSS, we're going to
  3328. 2:47:13style this button to look like the final
  3329. 2:47:16design. So, first we're going to put the
  3330. 2:47:19final design and our website side by
  3331. 2:47:21side so it's easier to compare. So, I'll
  3332. 2:47:24take the final design here and I'll put
  3333. 2:47:27it on the bottom left and I'll take our
  3334. 2:47:30website and put it in the top left. And
  3335. 2:47:33I'm going to resize our code editor
  3336. 2:47:37so we can see everything side by side.
  3337. 2:47:42And I'll hide the sidebar.
  3338. 2:47:45Next, to style our send button to look
  3339. 2:47:48like the final project, we just have to
  3340. 2:47:51style it one by one with CSS. So, we
  3341. 2:47:55already made the background color green.
  3342. 2:47:58Next, let's make the text color white.
  3343. 2:48:01To do that, we'll type the CSS property
  3344. 2:48:04color colon and set it to white and
  3345. 2:48:09save.
  3346. 2:48:12And that will make the text color white.
  3347. 2:48:16Next, let's make our button bigger. To
  3348. 2:48:19do that, we'll add space on the inside
  3349. 2:48:22of the button. As a review, space on the
  3350. 2:48:26inside of an element is called padding.
  3351. 2:48:29So in our CSS we're going to add padding
  3352. 2:48:34colon and we'll give the padding two
  3353. 2:48:36values 12 px and 20 px.
  3354. 2:48:42So the px here stands for pixels. A
  3355. 2:48:46pixel is a unit of measurement in
  3356. 2:48:48computers. For example, my computer
  3357. 2:48:51screen is about 3,800 pixels wide. So 12
  3358. 2:48:55pixels and 20 pixels looks like this.
  3359. 2:48:59The first value is the vertical space
  3360. 2:49:02inside this button. And the second value
  3361. 2:49:06is the horizontal space inside this
  3362. 2:49:08button. So we're going to add space all
  3363. 2:49:11around the inside of this button. So
  3364. 2:49:14let's save.
  3365. 2:49:16And it adds space on the inside.
  3366. 2:49:19And I think that looks close enough.
  3367. 2:49:22Next, let's add some space on the
  3368. 2:49:25outside of this button on the left. As a
  3369. 2:49:28review, space on the outside of an
  3370. 2:49:31element is called margin.
  3371. 2:49:33So, in our CSS, we'll add margin dash
  3372. 2:49:38and we'll add it on the left colon. And
  3373. 2:49:42let's add 10 pixels of margin. And save.
  3374. 2:49:48And that looks close enough.
  3375. 2:49:51Next, our button has a border around the
  3376. 2:49:54button, but in the final design, there
  3377. 2:49:56is no border. To remove the borders,
  3378. 2:49:59we'll type the property border colon and
  3379. 2:50:03set it to none and save.
  3380. 2:50:08And that will remove the borders.
  3381. 2:50:11Next, the corners of the button in the
  3382. 2:50:14final design are round. To make our
  3383. 2:50:18corners round, we're going to use the
  3384. 2:50:20CSS property border dash radius colon
  3385. 2:50:26and then how round we want the corners
  3386. 2:50:28to be. Let's try 10 pixels and save.
  3387. 2:50:35And I think that looks close enough.
  3388. 2:50:38Next, the text in the button is a little
  3389. 2:50:42bigger in the final design. To change
  3390. 2:50:44the size of the text, we'll use the CSS
  3391. 2:50:47property font dash size colon and let's
  3392. 2:50:52try 15 pixels and save.
  3393. 2:50:58And that looks pretty good. Next, if we
  3394. 2:51:01put our mouse over our button, we no
  3395. 2:51:04longer have the hand pointer icon that
  3396. 2:51:07we see here. to use this hand pointer
  3397. 2:51:10icon. We're going to set the property
  3398. 2:51:13cursor colon pointer
  3399. 2:51:18and save.
  3400. 2:51:21And now if we put our mouse over the
  3401. 2:51:23button, we have the hand pointer icon
  3402. 2:51:25again.
  3403. 2:51:27Finally, the background color of our
  3404. 2:51:29button is a little different from the
  3405. 2:51:32final design. So to get this exact green
  3406. 2:51:35color, we can go to the final project.
  3407. 2:51:38And then we're going to rightclick this
  3408. 2:51:40button and then click inspect.
  3409. 2:51:45And we can click these three dots here
  3410. 2:51:47and click this icon to open the console
  3411. 2:51:51in a different window.
  3412. 2:51:54And we're going to go to the elements
  3413. 2:51:55tab and we'll find the send button in
  3414. 2:51:58the HTML. And we're going to look at
  3415. 2:52:00these styles. So you can see that this
  3416. 2:52:03button here has a background color of
  3417. 2:52:06this RGB value. So we're going to select
  3418. 2:52:09this RGB value and we're going to copy
  3419. 2:52:12it.
  3420. 2:52:14And in our code, instead of the color
  3421. 2:52:17green, we're going to paste this RGB
  3422. 2:52:21color.
  3423. 2:52:22As a review, RGB stands for red, green,
  3424. 2:52:27blue, and it's a way to measure color in
  3425. 2:52:30computers. Computers can create any
  3426. 2:52:33color using a combination of red, green,
  3427. 2:52:36and blue. The first value here is how
  3428. 2:52:39much red we want in the color. The
  3429. 2:52:42second value is how much green we want
  3430. 2:52:44in the color. And the third value is how
  3431. 2:52:47much blue we want. And each value goes
  3432. 2:52:50from 0 to 255.
  3433. 2:52:52So this color combines a little bit of
  3434. 2:52:55red, a lot of green, and a bit of blue
  3435. 2:52:59to get this exact green color. So let's
  3436. 2:53:02save our code.
  3437. 2:53:04And now our button matches the final
  3438. 2:53:07project.
  3439. 2:53:09And that's how we style the send button.
  3440. 2:53:12We just style it one by one using CSS
  3441. 2:53:15until it looks like the final design. So
  3442. 2:53:18this is just a review of CSS. If you
  3443. 2:53:21want to learn CSS in more detail, you
  3444. 2:53:24can check out my HTML and CSS full
  3445. 2:53:26course in the video description.
  3446. 2:53:31Next, we're going to style this text box
  3447. 2:53:34to look like the final design. Again,
  3448. 2:53:37we're just going to style it one by one
  3449. 2:53:39with CSS.
  3450. 2:53:41First, we need to add a class to this
  3451. 2:53:44element so we can select it in our CSS.
  3452. 2:53:47We'll go to our code and we're going to
  3453. 2:53:49scroll down to this text box which is
  3454. 2:53:52inside the chat input component and it's
  3455. 2:53:56this input element here. So to add a
  3456. 2:53:59class we're going to use class name. So
  3457. 2:54:03inside the input element we'll add a
  3458. 2:54:06class name equals double quotes and
  3459. 2:54:10let's give it the class chat dash input.
  3460. 2:54:16Now, let's scroll back up to the CSS.
  3461. 2:54:20And below these styles,
  3462. 2:54:22we're going to type dot to select a
  3463. 2:54:25class. And we're going to select the
  3464. 2:54:27class we just added, which is chat dash
  3465. 2:54:31input and curly brackets. And now we're
  3466. 2:54:35ready to style the text box.
  3467. 2:54:38The first thing we'll change is there's
  3468. 2:54:40a lot of space on the inside of this
  3469. 2:54:42text box in the final design. So again,
  3470. 2:54:46space on the inside of an element is
  3471. 2:54:48called padding.
  3472. 2:54:51In our CSS, we'll add padding colon. And
  3473. 2:54:56let's do 12 pixels for the vertical
  3474. 2:54:59space and 15 pixels for the horizontal
  3475. 2:55:03space. Now let's save.
  3476. 2:55:08And that adds space on the inside of our
  3477. 2:55:10text box. And I think that looks close
  3478. 2:55:13enough. Next, we'll make the corners
  3479. 2:55:16round. To do that, we'll use border dash
  3480. 2:55:21radius colon. And let's try 10 pixels
  3481. 2:55:27and save.
  3482. 2:55:29And that looks pretty good. Next, our
  3483. 2:55:32border is too thick compared to the
  3484. 2:55:35final design. To change the thickness of
  3485. 2:55:38the border, we'll use border dash width
  3486. 2:55:44colon. And let's change it to 1 pixel
  3487. 2:55:48and save.
  3488. 2:55:50So that looks pretty good. Finally, the
  3489. 2:55:53text is bigger in the final design. To
  3490. 2:55:56change the size of the text, we can use
  3491. 2:56:00font dash size colon. And let's use 15
  3492. 2:56:05pixels again. So 15px
  3493. 2:56:08and save.
  3494. 2:56:11And I think that looks good enough for
  3495. 2:56:13now. So we styled the text box one by
  3496. 2:56:17one to look like the final design.
  3497. 2:56:21Next in the final design, the text box
  3498. 2:56:25is wider. And this text box is also
  3499. 2:56:28flexible. That means if we resize the
  3500. 2:56:31browser, the text box will also resize.
  3501. 2:56:36To make our text box flexible, we're
  3502. 2:56:39going to review a feature of CSS called
  3503. 2:56:42flexbox.
  3504. 2:56:43Flexbox lets us create a flexible
  3505. 2:56:46layout. Layout means how the elements
  3506. 2:56:50are positioned on the website.
  3507. 2:56:53Here we have a horizontal layout where
  3508. 2:56:57the elements are beside each other. And
  3509. 2:57:00also this element is flexible. So
  3510. 2:57:03flexbox helps us create this flexible
  3511. 2:57:06layout. To use flexbox we need to do
  3512. 2:57:10three steps. The first step is to create
  3513. 2:57:13a container around the elements that we
  3514. 2:57:16want to position. So let's go back to
  3515. 2:57:18our code and we're going to scroll down
  3516. 2:57:22to the chat input component and we'll
  3517. 2:57:25scroll down to the HTML.
  3518. 2:57:27So here we want to position the text box
  3519. 2:57:30and the button. So we need to create a
  3520. 2:57:34container around these two elements.
  3521. 2:57:37Right now we have a fragment around
  3522. 2:57:40these two elements. However, we cannot
  3523. 2:57:43style a fragment with CSS since
  3524. 2:57:46fragments don't actually appear on the
  3525. 2:57:48website.
  3526. 2:57:50Instead, we're going to switch this
  3527. 2:57:52container to a div element because we
  3528. 2:57:55can style a div element. So, inside
  3529. 2:57:58here, we'll type div and at the end,
  3530. 2:58:02we'll do slash div to convert this
  3531. 2:58:06container to a div element.
  3532. 2:58:09And now we have a container around the
  3533. 2:58:12elements.
  3534. 2:58:14The second step is to add the property
  3535. 2:58:16display flex to the container.
  3536. 2:58:20First, let's add a class name to this
  3537. 2:58:22container so we can select it in our
  3538. 2:58:24CSS. In here, we'll type class name
  3539. 2:58:30equals double quotes and we'll give it
  3540. 2:58:33the class chat-input
  3541. 2:58:36dash container.
  3542. 2:58:39And then we'll scroll up to our CSS.
  3543. 2:58:45And at the end we'll type dot to select
  3544. 2:58:49a class and we'll select the class
  3545. 2:58:52chat-input
  3546. 2:58:54dash container and curly brackets.
  3547. 2:58:59And finally we'll add the property
  3548. 2:59:02display colon flex and that will turn
  3549. 2:59:06this container into a flexbox.
  3550. 2:59:10The third step is to use flexbox
  3551. 2:59:13features. Flexbox has many features we
  3552. 2:59:16can use to control the layout or to
  3553. 2:59:19control how these elements are
  3554. 2:59:21positioned. One feature is called flex
  3555. 2:59:24grow. Flex grow makes an element grow
  3556. 2:59:29and take up the remaining space. So here
  3557. 2:59:32we want this text box to grow and take
  3558. 2:59:36up this remaining space.
  3559. 2:59:39So we'll go to the styles for the chat
  3560. 2:59:41input or the text box
  3561. 2:59:45and we'll add the property flex dash
  3562. 2:59:48grow
  3563. 2:59:501 and save.
  3564. 2:59:54And now the text box will grow and take
  3565. 2:59:58up the remaining space.
  3566. 3:00:00If we resize the browser, the text box
  3567. 3:00:03will also resize to take up the
  3568. 3:00:06remaining space.
  3569. 3:00:08So now we created the layout for the
  3570. 3:00:10chat input that we see in the final
  3571. 3:00:13design.
  3572. 3:00:16Next we'll create the layout for the
  3573. 3:00:18overall app. Notice in the final project
  3574. 3:00:22if we resize the browser and make it
  3575. 3:00:25wider the app is limited to a certain
  3576. 3:00:28width. So it's not going to get wider.
  3577. 3:00:32To create this in our project, we'll set
  3578. 3:00:34a maximum width for our app.
  3579. 3:00:38So, let's go to our code and we're going
  3580. 3:00:41to scroll down to the app component,
  3581. 3:00:44which is down here, and we'll go down to
  3582. 3:00:47the HTML.
  3583. 3:00:49And currently, the app is contained in a
  3584. 3:00:52fragment. And remember, we cannot style
  3585. 3:00:55a fragment since fragments don't
  3586. 3:00:58actually appear on the website.
  3587. 3:01:01So again, we're going to switch this to
  3588. 3:01:03a div element so we can style it. So in
  3589. 3:01:07here, we'll type div and at the bottom
  3590. 3:01:11we'll type /ash div. And now we can
  3591. 3:01:15style this.
  3592. 3:01:17First we'll add a class to this div
  3593. 3:01:20using class name equals double quotes.
  3594. 3:01:25And let's give it the class app dash
  3595. 3:01:29container.
  3596. 3:01:30and we'll scroll up to our CSS.
  3597. 3:01:35And at the bottom here,
  3598. 3:01:37we'll type dot to select a class. And
  3599. 3:01:41we'll select the class app dash
  3600. 3:01:43container and curly brackets.
  3601. 3:01:47And to set a maximum width of our app,
  3602. 3:01:50we can just use the property max dash
  3603. 3:01:54width colon. and let's try 600 pixels
  3604. 3:02:00and save.
  3605. 3:02:03So now if we resize our browser the app
  3606. 3:02:06will be limited to 600 pixels just like
  3607. 3:02:11the final design. Next in the final
  3608. 3:02:14design notice that the app is centered
  3609. 3:02:17on the website. Now there are several
  3610. 3:02:20ways to center an element in CSS.
  3611. 3:02:23One way is to use margin. So we'll go
  3612. 3:02:27back to our code
  3613. 3:02:29and in the app container, we're going to
  3614. 3:02:32add the property margin dash left colon
  3615. 3:02:38and we'll set it to auto. And we'll also
  3616. 3:02:41set the margin dash right colon
  3617. 3:02:46auto.
  3618. 3:02:48So this will automatically add a space
  3619. 3:02:52on the left and right of this element
  3620. 3:02:55until it is centered. So if we save our
  3621. 3:02:58code and check our website, you'll
  3622. 3:03:02notice that the entire app is now
  3623. 3:03:04centered on the website. And now we
  3624. 3:03:07created the layout for the overall app.
  3625. 3:03:12Next, we're going to create the layout
  3626. 3:03:14for the chat messages. If you don't have
  3627. 3:03:17any chat messages in your final project,
  3628. 3:03:20you can add some by typing in here and
  3629. 3:03:23then pressing send. So notice that the
  3630. 3:03:26chat messages from the user are on the
  3631. 3:03:29right while the chat messages from the
  3632. 3:03:31robot are on the left. So we're going to
  3633. 3:03:34create this layout. First, we need to
  3634. 3:03:38give class names to these chat messages
  3635. 3:03:41so we can style them.
  3636. 3:03:44So, let's resize our browser for now.
  3637. 3:03:47And we'll go to our code. And we're
  3638. 3:03:50going to scroll down
  3639. 3:03:52to the chat message component. And we'll
  3640. 3:03:56scroll down to the HTML.
  3641. 3:03:59And we'll give each chat message a class
  3642. 3:04:02name equals. And we're going to do
  3643. 3:04:05something different this time. We'll
  3644. 3:04:08type curly brackets. So here we want to
  3645. 3:04:12give a different class name depending on
  3646. 3:04:15if the sender is a user or the sender is
  3647. 3:04:18a robot. This will let us position the
  3648. 3:04:20chat messages differently on the right
  3649. 3:04:23or on the left. To give this a different
  3650. 3:04:26class name depending on the sender.
  3651. 3:04:28We're going to use a feature of
  3652. 3:04:30JavaScript called the turnary operator.
  3653. 3:04:33As a review, the turnary operator looks
  3654. 3:04:36like this. And it works like an if else
  3655. 3:04:40statement.
  3656. 3:04:42If value one is true, then the result is
  3657. 3:04:45value two. Else the result is value
  3658. 3:04:48three.
  3659. 3:04:50The turnary operator lets us insert an
  3660. 3:04:53if else statement directly in the JSX.
  3661. 3:04:57So using a turnary operator, we can give
  3662. 3:05:00a different class name depending on the
  3663. 3:05:03sender.
  3664. 3:05:05Inside here, we're going to type the if
  3665. 3:05:07condition. So, we'll check if the sender
  3666. 3:05:12is equal to the string user. And then
  3667. 3:05:16we'll type question mark. If the sender
  3668. 3:05:19is the user, we'll set the class name to
  3669. 3:05:22the string chat-
  3670. 3:05:25message dash user.
  3671. 3:05:29And then we'll type a colon. So, this
  3672. 3:05:32means else. So if the sender is not the
  3673. 3:05:35user, we're going to give the class name
  3674. 3:05:38the string chat-
  3675. 3:05:41message dash robot.
  3676. 3:05:45So again the way this code works is if
  3677. 3:05:48the sender is equal to user, we'll set
  3678. 3:05:51the class name to chat message user.
  3679. 3:05:55Else we'll set the class name to chat
  3680. 3:05:57message robot. As you can see, the
  3681. 3:06:00turnary operator lets us insert an if
  3682. 3:06:04else statement directly in the JSX. And
  3683. 3:06:08notice that we used curly brackets here
  3684. 3:06:10because we want to save the result of
  3685. 3:06:13this code into class name instead of
  3686. 3:06:16just a string.
  3687. 3:06:19Before we continue, we can add some new
  3688. 3:06:21lines to this code to make it easier to
  3689. 3:06:23read. So, at the beginning, we can add a
  3690. 3:06:26new line. And then we'll add a new line
  3691. 3:06:29before the question mark and press tab.
  3692. 3:06:33And we'll add a new line before the
  3693. 3:06:35colon. And we'll add a new line at the
  3694. 3:06:38back.
  3695. 3:06:40And remove some spaces so that this is
  3696. 3:06:43vertically aligned.
  3697. 3:06:46So, this makes the code a bit easier to
  3698. 3:06:48read. We have the if condition and then
  3699. 3:06:51the first value and then else the second
  3700. 3:06:54value. Now let's save
  3701. 3:06:58and we'll go to our website and let's
  3702. 3:07:01check the HTML. So in our console, we
  3703. 3:07:04can click these three dots here and then
  3704. 3:07:07click this icon to open the console in
  3705. 3:07:10another window. And we'll go to the
  3706. 3:07:12elements tab and we'll click this
  3707. 3:07:15pointer on the top left and we'll select
  3708. 3:07:18one of these chat messages and click it.
  3709. 3:07:22And if we scroll down here, you can see
  3710. 3:07:24that each chat message now has a class.
  3711. 3:07:28And messages from the user have the
  3712. 3:07:30class chat message user. And messages
  3713. 3:07:33from the robot have the class chat
  3714. 3:07:36message robot.
  3715. 3:07:39So now that we added a class to each of
  3716. 3:07:41these chat messages, let's style these
  3717. 3:07:44chat messages.
  3718. 3:07:46We'll go to our code and we'll scroll up
  3719. 3:07:49to our CSS.
  3720. 3:07:52And at the end, let's type a dot. And
  3721. 3:07:55first let's select the class chat dash
  3722. 3:07:59message dash user and curly brackets.
  3723. 3:08:06And now for chat message user we want to
  3724. 3:08:09position these elements the text and the
  3725. 3:08:12profile image on the right. To create
  3726. 3:08:16this flexible layout we can use flexbox.
  3727. 3:08:19Again
  3728. 3:08:21remember there are three steps to use
  3729. 3:08:23flexbox. First, we already have a
  3730. 3:08:27container around the elements. It has a
  3731. 3:08:30class chat message user or chat message
  3732. 3:08:32robot.
  3733. 3:08:34Next, we need to add display flex to
  3734. 3:08:37this container. So, for chat message
  3735. 3:08:39user, we'll add
  3736. 3:08:42display colon flex.
  3737. 3:08:46And now we can use flexbox features.
  3738. 3:08:51Another feature of flexbox is we're
  3739. 3:08:54going to type justify dashcontent
  3740. 3:08:59colon.
  3741. 3:09:01Justify content controls how the
  3742. 3:09:03elements are displayed horizontally.
  3743. 3:09:08If we want these elements to be
  3744. 3:09:09displayed on the right, we're going to
  3745. 3:09:12give this a value end.
  3746. 3:09:15So it will display these elements at the
  3747. 3:09:17end of this row or on the right.
  3748. 3:09:20So now let's save our code.
  3749. 3:09:24And now the chat messages from the user
  3750. 3:09:26are on the right. And we created the
  3751. 3:09:29layout for the chat messages.
  3752. 3:09:33Next, we're going to style the text in
  3753. 3:09:36the chat messages.
  3754. 3:09:38First, we need to add a class name to
  3755. 3:09:41this text so we can style it.
  3756. 3:09:44Let's go to our code and we're going to
  3757. 3:09:47scroll down to the chat message
  3758. 3:09:50component
  3759. 3:09:52and we're going to scroll to the text
  3760. 3:09:53which is here. So currently this text is
  3761. 3:09:57not inside an element. So first we're
  3762. 3:10:01going to put this text inside an element
  3763. 3:10:04so that we can style just the text.
  3764. 3:10:08Above this, we'll put this in a div by
  3765. 3:10:11typing less than div greater than and
  3766. 3:10:15below it less than slashd greater than.
  3767. 3:10:20And we'll add an indent at the front to
  3768. 3:10:22show that this text is inside this div.
  3769. 3:10:26And now let's add a class to this div so
  3770. 3:10:29we can style just the text. So, we'll
  3771. 3:10:32give this a class name equals double
  3772. 3:10:37quotes. And let's give it the class chat
  3773. 3:10:41dash message dash text.
  3774. 3:10:45So now let's style this with CSS. We'll
  3775. 3:10:49scroll up to our CSS
  3776. 3:10:51and at the end we'll type a dot and
  3777. 3:10:55we'll style the class chat dash message
  3778. 3:10:59dash text and curly brackets.
  3779. 3:11:04So the first thing we'll style is we're
  3780. 3:11:07going to change the background color to
  3781. 3:11:09this gray color. To get this exact
  3782. 3:11:12color, we're going to rightclick on this
  3783. 3:11:14text and then click inspect.
  3784. 3:11:19and we'll find this text in the elements
  3785. 3:11:21tab and we're going to go into the
  3786. 3:11:23styles and we'll get the background
  3787. 3:11:25color. So, we'll copy this RGB color
  3788. 3:11:30and we'll go into our code and we'll set
  3789. 3:11:33the background dash color colon and
  3790. 3:11:38we'll paste the RGB color and save.
  3791. 3:11:43And now we'll have the exact color in
  3792. 3:11:46our final design.
  3793. 3:11:48However, after we saved, it kind of
  3794. 3:11:51messed up our layout. So, the chat
  3795. 3:11:54message user looks okay, but the text
  3796. 3:11:56for chat message robot is now appearing
  3797. 3:11:59below the profile image and it's taking
  3798. 3:12:02up the entire line.
  3799. 3:12:04So, what happened? Remember that we just
  3800. 3:12:07surrounded this text in a div element.
  3801. 3:12:11Normally a div element takes up an
  3802. 3:12:14entire line by itself because a div is a
  3803. 3:12:18block element.
  3804. 3:12:20However, remember that we also made chat
  3805. 3:12:23message user a flexbox. When a block
  3806. 3:12:27element is inside a flexbox, it no
  3807. 3:12:30longer takes up the entire line. It only
  3808. 3:12:33takes up as much space as it needs to.
  3809. 3:12:36So that's why this div only takes up as
  3810. 3:12:39much space as it needs because it's
  3811. 3:12:42inside a flexbox.
  3812. 3:12:45One way to fix this layout problem is we
  3813. 3:12:48can make chat message robot a flexbox as
  3814. 3:12:52well. We'll go back to our code and
  3815. 3:12:55below chat message user let's type dot
  3816. 3:12:59and we'll style the chat- message dash
  3817. 3:13:04robot and curly brackets
  3818. 3:13:08and we'll also give it display colon
  3819. 3:13:12flex
  3820. 3:13:14so it will match the layout for the chat
  3821. 3:13:16message user.
  3822. 3:13:18Let's save.
  3823. 3:13:21And now because this div is inside a
  3824. 3:13:24flexbox, it only takes up as much space
  3825. 3:13:27as it needs. So now the background color
  3826. 3:13:31and the layout looks good. Next, the
  3827. 3:13:35style of text or the font is different
  3828. 3:13:38in the final design.
  3829. 3:13:41To change the font, we'll go to our code
  3830. 3:13:44and we'll add the property font dash
  3831. 3:13:48family colon And in the final project, I
  3832. 3:13:52use the font Ariel.
  3833. 3:13:56Let's save.
  3834. 3:13:58And now our font matches the final
  3835. 3:14:01design.
  3836. 3:14:02Now, usually we don't set the font for
  3837. 3:14:06just one element. We set the font for
  3838. 3:14:08the entire website because we want the
  3839. 3:14:12entire website to use the same font or
  3840. 3:14:14the same style of text. to set the font
  3841. 3:14:18for the entire website. We can style the
  3842. 3:14:21body element. So, we'll scroll to the
  3843. 3:14:24top of our CSS.
  3844. 3:14:27And here we'll style the body element
  3845. 3:14:31curly brackets and we'll set the font
  3846. 3:14:35dash family colon to area.
  3847. 3:14:40So this sets the font for all elements
  3848. 3:14:44inside the body, which is basically the
  3849. 3:14:47entire website. So now we can scroll
  3850. 3:14:50back down
  3851. 3:14:52and we can remove this style
  3852. 3:14:55because we're going to set the font for
  3853. 3:14:57the entire website instead.
  3854. 3:15:00Let's save.
  3855. 3:15:02And now we set the font for the entire
  3856. 3:15:05website to Ariel.
  3857. 3:15:08Next, let's add space on the inside of
  3858. 3:15:11this text, which is padding.
  3859. 3:15:15So, here we'll add padding
  3860. 3:15:19colon. And let's try 15 pixels of
  3861. 3:15:23vertical space and 20 pixels of
  3862. 3:15:26horizontal space. And save.
  3863. 3:15:31And that looks pretty good.
  3864. 3:15:34Next, we'll make the corners round.
  3865. 3:15:37To do that, we'll use border dash radius
  3866. 3:15:42colon and 10 pixels and save.
  3867. 3:15:48And that looks pretty good. Next, let's
  3868. 3:15:51add space on the outside of the text on
  3869. 3:15:54the right. So, we'll add margin dash
  3870. 3:15:59right colon and let's try 10 pixels and
  3871. 3:16:05save.
  3872. 3:16:07And that looks close enough.
  3873. 3:16:10And here, if we scroll down to a chat
  3874. 3:16:13message from the robot, we'll add some
  3875. 3:16:16space on the left of the text. So, in
  3876. 3:16:19our code, we'll add margin dash left.
  3877. 3:16:24And let's try 10 pixels and save.
  3878. 3:16:30And that looks pretty good. Finally,
  3879. 3:16:34let's add some space on the bottom of
  3880. 3:16:36the text so we can separate them. Here
  3881. 3:16:39we'll add margin dash bottom colon. And
  3882. 3:16:46let's try 20 pixels and save.
  3883. 3:16:51And I think that looks close enough.
  3884. 3:16:54However, we now have a problem here,
  3885. 3:16:57which is the profile image seems to be
  3886. 3:17:00stretching.
  3887. 3:17:02This happens because this container is a
  3888. 3:17:05flexbox. And by default, elements inside
  3889. 3:17:09a flexbox will stretch vertically.
  3890. 3:17:13So, if this text gets longer because we
  3891. 3:17:15added some margin, this image will
  3892. 3:17:18stretch.
  3893. 3:17:20To fix this, flexbox has another feature
  3894. 3:17:23called align items.
  3895. 3:17:26So in our CSS in chat message user,
  3896. 3:17:30we'll add the property align dash items
  3897. 3:17:35colon
  3898. 3:17:36and align items controls how the
  3899. 3:17:39elements are displayed vertically. By
  3900. 3:17:42default, align items is stretch. That's
  3901. 3:17:46why the elements stretch vertically.
  3902. 3:17:49Instead, we're going to change align
  3903. 3:17:51items to start.
  3904. 3:17:55This will display the elements at the
  3905. 3:17:57start or at the top vertically, and it
  3906. 3:18:00will no longer stretch. Same thing for
  3907. 3:18:03the chat message robot. We're going to
  3908. 3:18:05give it align dash items colon start and
  3909. 3:18:12save.
  3910. 3:18:14And now the elements in the flexbox will
  3911. 3:18:17appear at the start or at the top
  3912. 3:18:19vertically.
  3913. 3:18:21The last thing we'll style for the text
  3914. 3:18:24is if we go to our final project and
  3915. 3:18:27inside the text box we type test and
  3916. 3:18:31press send.
  3917. 3:18:34You'll notice that the response from the
  3918. 3:18:36chatbot has a maximum width. So in our
  3919. 3:18:40project, we're also going to add a
  3920. 3:18:42maximum width to our text. So in our
  3921. 3:18:46code, we'll go to the chat message text
  3922. 3:18:49and let's add a max dash width colon.
  3923. 3:18:54And let's try 300 pixels and save.
  3924. 3:19:00And now if we go to our text box and
  3925. 3:19:02type test and press send and scroll
  3926. 3:19:07down, the text has a maximum width and
  3927. 3:19:11it matches the final design.
  3928. 3:19:14Now we finish styling the text in the
  3929. 3:19:17chat messages.
  3930. 3:19:20Next, we'll style the profile images. So
  3931. 3:19:23in our code, let's scroll down to the
  3932. 3:19:26profile image which is inside the chat
  3933. 3:19:30message component
  3934. 3:19:32and it's these two image elements here.
  3935. 3:19:37Currently, we're using an attribute to
  3936. 3:19:39set the width of this image. However, a
  3937. 3:19:43best practice is to use CSS for styling
  3938. 3:19:46instead of attributes. So let's remove
  3939. 3:19:50this attribute and we'll remove it down
  3940. 3:19:53here as well and we'll add a class to
  3941. 3:19:56these profile images so that we can
  3942. 3:19:59style them with CSS. So here we'll add
  3943. 3:20:03class name equals double quotes and
  3944. 3:20:07we'll give it the class chat-
  3945. 3:20:10message dash profile.
  3946. 3:20:14And we'll also add this class to the
  3947. 3:20:16profile image for the user.
  3948. 3:20:19We'll give it the class name equals
  3949. 3:20:23double quotes
  3950. 3:20:25chat dash message dash profile.
  3951. 3:20:30So now we can style this with our CSS.
  3952. 3:20:33We'll scroll up to our CSS
  3953. 3:20:37and at the end we'll type a dot and
  3954. 3:20:41we'll style the class chat dash message
  3955. 3:20:45dash profile and curly brackets.
  3956. 3:20:50So previously we set the width to 50
  3957. 3:20:53pixels. So let's do that again using
  3958. 3:20:56CSS. This time we'll set the width colon
  3959. 3:21:01to 50 pixels.
  3960. 3:21:03and save.
  3961. 3:21:07Now, if we look at the final design,
  3962. 3:21:11it looks like the profile images here
  3963. 3:21:13are a little bit smaller than our
  3964. 3:21:16profile images. So, let's make this
  3965. 3:21:18width smaller. Let's try 45 pixels
  3966. 3:21:22instead.
  3967. 3:21:23And save.
  3968. 3:21:26And I think that looks close enough. And
  3969. 3:21:29now our profile images are the same size
  3970. 3:21:32as the final design.
  3971. 3:21:35The last thing we're going to change in
  3972. 3:21:37this design is we're going to move the
  3973. 3:21:39text box to the bottom so that it
  3974. 3:21:41matches the design of other popular
  3975. 3:21:44chatbots.
  3976. 3:21:45In the final project, we're going to
  3977. 3:21:48make this a little taller. Now, we can
  3978. 3:21:51move this text box to the bottom of the
  3979. 3:21:54page by clicking this link.
  3980. 3:21:58And now let's create this design in our
  3981. 3:22:01project.
  3982. 3:22:02First I'll open our project and then
  3983. 3:22:05move it back to the same window so that
  3984. 3:22:08we can see this side by side a little
  3985. 3:22:10easier. And to put the text box at the
  3986. 3:22:14bottom of the page, the first step is to
  3987. 3:22:16move the text box below the chat
  3988. 3:22:19messages. We'll go to our code and we're
  3989. 3:22:22going to scroll down to the app
  3990. 3:22:25component
  3991. 3:22:27which is here and we'll scroll to the
  3992. 3:22:30HTML and we're just going to move this
  3993. 3:22:33chat input below the chat messages. So
  3994. 3:22:36we'll select this component and we'll
  3995. 3:22:39cut
  3996. 3:22:41and below chat messages we'll paste
  3997. 3:22:46and save.
  3998. 3:22:49And now the chat input will be below the
  3999. 3:22:52chat messages. And that looks pretty
  4000. 3:22:54good. Next, we also want to put this
  4001. 3:22:57chat input at the bottom of the browser
  4002. 3:23:00here. So there are several ways to do
  4003. 3:23:03this in CSS. One way is to use flexbox
  4004. 3:23:07again. Now before we use flexbox, we're
  4005. 3:23:11going to make the app container stretch
  4006. 3:23:14to the bottom of the browser. So right
  4007. 3:23:17now if we click inside here and we
  4008. 3:23:20rightclick and inspect
  4009. 3:23:24and we go to the elements tab and we
  4010. 3:23:27scroll up and look at the app container.
  4011. 3:23:31Notice that the app container stops at
  4012. 3:23:34the chat input section. And that's
  4013. 3:23:36because this is all the content that we
  4014. 3:23:38have. So the app container only has the
  4015. 3:23:41height that it needs. However, we want
  4016. 3:23:44to stretch the app container to the
  4017. 3:23:46bottom of the browser so that we can put
  4018. 3:23:49the chat input at the bottom. To do
  4019. 3:23:52that, we're going to set the height of
  4020. 3:23:54the app container. So, we'll go back to
  4021. 3:23:57our code
  4022. 3:23:59and we'll scroll up to our CSS
  4023. 3:24:04and we're going to find the styles for
  4024. 3:24:06the app container which is right here.
  4025. 3:24:09And at the end, we're going to set the
  4026. 3:24:11height colon.
  4027. 3:24:15And now we want the height of the app
  4028. 3:24:17container to stretch to be the same as
  4029. 3:24:20the height of the browser here. To do
  4030. 3:24:24that, we're going to give this a special
  4031. 3:24:26value 100 VH.
  4032. 3:24:31So VH stands for viewport height. This
  4033. 3:24:35gives us the height of the browser or
  4034. 3:24:38the part of the page that is visible.
  4035. 3:24:41100 VH means 100% of the height of the
  4036. 3:24:46browser. So this means the app container
  4037. 3:24:49will be 100% of this height.
  4038. 3:24:55So now let's save
  4039. 3:24:58and we'll go to our website and we'll
  4040. 3:25:00open the console again by right-clicking
  4041. 3:25:04and inspect
  4042. 3:25:06and go to the elements tab and we'll
  4043. 3:25:08scroll up to the app container again.
  4044. 3:25:12And now you'll notice the app container
  4045. 3:25:14stretches to the bottom of the browser.
  4046. 3:25:18However, the app container is actually a
  4047. 3:25:21little too tall. So it actually
  4048. 3:25:23stretches below the bottom of the page.
  4049. 3:25:26And now we can scroll a bit here. The
  4050. 3:25:29reason it's a little bit too tall is
  4051. 3:25:31because by default the body element here
  4052. 3:25:35has a default margin of 8 pixels on all
  4053. 3:25:39sides. So the app container is the same
  4054. 3:25:42height as the browser, but then we add
  4055. 3:25:45another 8 pixels on the top and bottom.
  4056. 3:25:50To fix this issue, we can just remove
  4057. 3:25:52the default margin from the body. So,
  4058. 3:25:55we'll go back to our code
  4059. 3:25:58and scroll up to the styles for the
  4060. 3:26:01body. And at the end, let's remove the
  4061. 3:26:05margin at the top and bottom by typing
  4062. 3:26:08margin- top colon. And we'll set it to
  4063. 3:26:12zero pixels. And we'll also set the
  4064. 3:26:16margin dashbottom
  4065. 3:26:19colon to zero pixels and save.
  4066. 3:26:26So now after we remove the default
  4067. 3:26:28margin from the top and the bottom,
  4068. 3:26:33the app container is now exactly the
  4069. 3:26:36same height as the browser.
  4070. 3:26:40Now that the app container stretches to
  4071. 3:26:42the bottom of the browser, we can use a
  4072. 3:26:45flexbox to make the chat messages grow
  4073. 3:26:48and push the chat input to the bottom.
  4074. 3:26:52Again, there are three steps to use a
  4075. 3:26:55flexbox.
  4076. 3:26:56So here we want to position the chat
  4077. 3:26:59messages and the chat input. So we need
  4078. 3:27:03a container around these elements and we
  4079. 3:27:06already have a container for this which
  4080. 3:27:08is the app container.
  4081. 3:27:11The second step is to add display flex
  4082. 3:27:14to the container. So we'll go back to
  4083. 3:27:16our code and we're going to scroll to
  4084. 3:27:19the app container styles which is right
  4085. 3:27:22here and below this we'll add display
  4086. 3:27:27colon flex. Let's save our code.
  4087. 3:27:34And unfortunately, it totally messed up
  4088. 3:27:36our design. So what happened? By
  4089. 3:27:40default, flexbox positions our elements
  4090. 3:27:42horizontally. So it takes all the
  4091. 3:27:45elements inside the container and then
  4092. 3:27:48squeezes them into a row like we see
  4093. 3:27:50here.
  4094. 3:27:52However, this time we want to position
  4095. 3:27:54the elements vertically, not
  4096. 3:27:57horizontally.
  4097. 3:27:58To position the elements vertically,
  4098. 3:28:01we're going to use a flexbox feature
  4099. 3:28:03called flex direction.
  4100. 3:28:07So in here, we'll type flex dash
  4101. 3:28:11direction. colon
  4102. 3:28:14flex direction determines which
  4103. 3:28:17direction the elements are positioned.
  4104. 3:28:20So flex direction is row by default.
  4105. 3:28:23That means it positions the elements
  4106. 3:28:26horizontally like we see here.
  4107. 3:28:29Now we're going to change flex direction
  4108. 3:28:31to column.
  4109. 3:28:34So this positions the elements
  4110. 3:28:36vertically in a column. If we save our
  4111. 3:28:40code
  4112. 3:28:41and check the website, the elements in
  4113. 3:28:44the flexbox are now positioned
  4114. 3:28:46vertically from top to bottom.
  4115. 3:28:50Next, we're going to use flex grow.
  4116. 3:28:52Again, as a review, flex grow makes an
  4117. 3:28:56element grow and take up the remaining
  4118. 3:28:59space.
  4119. 3:29:01Here, we want to make the chat messages
  4120. 3:29:04section grow and take up all the
  4121. 3:29:06remaining vertical space. And this will
  4122. 3:29:09push the chat input down to the bottom.
  4123. 3:29:13To use Flex Grow, we're going to add a
  4124. 3:29:15class to the chat messages section.
  4125. 3:29:19We'll go to our code and we'll scroll
  4126. 3:29:21down
  4127. 3:29:23to the chat messages component which is
  4128. 3:29:26right here. And currently this component
  4129. 3:29:29uses a fragment. And remember, we cannot
  4130. 3:29:33style a fragment since fragments don't
  4131. 3:29:36appear on the website. So again, we're
  4132. 3:29:39going to switch this to a div element.
  4133. 3:29:42and at the end slash div so that we can
  4134. 3:29:47style it.
  4135. 3:29:49And now we'll give this div a class
  4136. 3:29:52using class name equals double quotes
  4137. 3:29:57and we'll give it the class chat dash
  4138. 3:30:00messages dash container.
  4139. 3:30:05And now let's style this class. We'll
  4140. 3:30:07scroll up in our CSS
  4141. 3:30:10and at the end here we'll type dot and
  4142. 3:30:15we'll style the class chat dash messages
  4143. 3:30:19dash container
  4144. 3:30:22and curly brackets
  4145. 3:30:24and we'll use the flexbox feature flex
  4146. 3:30:28dash grow
  4147. 3:30:311.
  4148. 3:30:32So now let's save.
  4149. 3:30:36And this makes the chat message
  4150. 3:30:38container grow and take up the remaining
  4151. 3:30:41vertical space. And this will push the
  4152. 3:30:43chat input to the bottom of the browser.
  4153. 3:30:47So that's how we use a flexbox to
  4154. 3:30:49position the chat input at the bottom.
  4155. 3:30:52Finally, if we look at the final design,
  4156. 3:30:56we have some space at the top of the app
  4157. 3:30:59and at the bottom of the app. So let's
  4158. 3:31:02add that space at the top and bottom to
  4159. 3:31:04our website.
  4160. 3:31:06We'll go to our CSS
  4161. 3:31:09and currently the app container has a
  4162. 3:31:12special height of 100 VH. So let's avoid
  4163. 3:31:16changing this. Instead, let's add space
  4164. 3:31:19above the Chad messages container. So,
  4165. 3:31:22we'll scroll down to the chat messages
  4166. 3:31:25container and we'll add some space at
  4167. 3:31:28the top using margin dash top colon 20
  4168. 3:31:34pixels.
  4169. 3:31:36And we'll also add some space below the
  4170. 3:31:38chat input container. So, we'll scroll
  4171. 3:31:41up to the chat input container which is
  4172. 3:31:45right here. And we'll add space at the
  4173. 3:31:48bottom using margin dash bottom colon.
  4174. 3:31:53And let's try 60 pixels and save.
  4175. 3:32:00And now that looks pretty good. And now
  4176. 3:32:03if we look at our website and compare it
  4177. 3:32:05to the final design,
  4178. 3:32:07you can see that it looks the same as
  4179. 3:32:09the final design. So, we finished
  4180. 3:32:12styling our project to look like the
  4181. 3:32:14final design using CSS.
  4182. 3:32:19Next, we're going to make the chat
  4183. 3:32:21messages scrollable. In the final
  4184. 3:32:24project, if we have more chat messages
  4185. 3:32:27than we can see in the browser, we can
  4186. 3:32:30actually scroll to see all of the chat
  4187. 3:32:32messages. Now, you may need to add more
  4188. 3:32:35chat messages down here in order to see
  4189. 3:32:38this feature. So, we're going to create
  4190. 3:32:40this feature in our project. If we go to
  4191. 3:32:44our project and we add some more
  4192. 3:32:47messages like test and press send,
  4193. 3:32:52notice if we have too many messages, it
  4194. 3:32:55starts to push the chat input down.
  4195. 3:32:58To make our chat messages scrollable,
  4196. 3:33:01we're going to go to the CSS and we're
  4197. 3:33:04going to find the chat messages
  4198. 3:33:05container, which is right here.
  4199. 3:33:09And at the end we'll add a property
  4200. 3:33:12overflow colon scroll.
  4201. 3:33:16So now if the chat messages overflow or
  4202. 3:33:19there's too many to be displayed, it
  4203. 3:33:22will start to scroll instead of pushing
  4204. 3:33:24things down. Let's save our code
  4205. 3:33:29and then add another message test and
  4206. 3:33:33press send.
  4207. 3:33:35And you may need to add a few more
  4208. 3:33:36messages to see this feature. Now, if we
  4209. 3:33:39have too many messages, we're able to
  4210. 3:33:42scroll up and down to see all the
  4211. 3:33:44messages.
  4212. 3:33:46Now, before we continue on Windows, we
  4213. 3:33:49have a small problem, which is we start
  4214. 3:33:52to see these thick scroll bars on the
  4215. 3:33:54sides, which doesn't look very good. To
  4216. 3:33:57hide these scroll bars, we can go back
  4217. 3:33:59to our CSS and at the end we can use the
  4218. 3:34:03property scroll bar dash width colon
  4219. 3:34:09nut.
  4220. 3:34:11So now let's save.
  4221. 3:34:15And if we add a message test and press
  4222. 3:34:18send and you might need to add more
  4223. 3:34:21messages for your project. Now, if we
  4224. 3:34:24have more messages than we can see, we
  4225. 3:34:27can scroll and we hide the scroll bars.
  4226. 3:34:31So, that's how we make the chat messages
  4227. 3:34:33scrollable.
  4228. 3:34:37The last feature we're going to add to
  4229. 3:34:39our project is auto scrolling. For
  4230. 3:34:42example, if we go down here and add the
  4231. 3:34:46message test and press send,
  4232. 3:34:50notice we have to manually scroll down
  4233. 3:34:52to the bottom ourselves. But if we go to
  4234. 3:34:55the final project and at the bottom add
  4235. 3:34:59a message test and press send,
  4236. 3:35:04notice that it automatically scrolls to
  4237. 3:35:07the bottom of the chat messages. So,
  4238. 3:35:09we're going to add this autoscrolling
  4239. 3:35:12feature.
  4240. 3:35:13Let's go back to our website.
  4241. 3:35:16And to create autoscrolling, we're going
  4242. 3:35:19to learn one of the most important
  4243. 3:35:21features of React called hooks.
  4244. 3:35:25Hooks let us insert React features into
  4245. 3:35:29our component. And we've actually used
  4246. 3:35:32hooks before. React.US
  4247. 3:35:35state is a hook. It inserts a React
  4248. 3:35:39feature called state into our component
  4249. 3:35:42and state automatically updates the HTML
  4250. 3:35:46when the data changes. Now, React has
  4251. 3:35:50other hooks that we can use and every
  4252. 3:35:52hook starts with a word use.
  4253. 3:35:56Another hook is called use effect. Use
  4254. 3:36:00effect lets us run some code after the
  4255. 3:36:03component is created or updated.
  4256. 3:36:07So in our project after we add a chat
  4257. 3:36:10message we want to run some code to
  4258. 3:36:13scroll to the bottom. So this is a
  4259. 3:36:16perfect situation for use effect.
  4260. 3:36:20Inside our code we're going to scroll
  4261. 3:36:22down to the chat messages component
  4262. 3:36:27which is here.
  4263. 3:36:30And at the top of the component, we're
  4264. 3:36:32going to type the code capital R react
  4265. 3:36:36dot use capital E effect
  4266. 3:36:41and brackets.
  4267. 3:36:43By the way, in React, we should put our
  4268. 3:36:46hooks at the top of the component and
  4269. 3:36:48they should not be inside anything. So,
  4270. 3:36:51don't put a hook inside an if statement
  4271. 3:36:53or inside a function.
  4272. 3:36:57Next, we'll give use effect a function.
  4273. 3:37:01So, inside these brackets, we'll type a
  4274. 3:37:03function using brackets, arrow, and
  4275. 3:37:07curly brackets.
  4276. 3:37:09As a reminder, when we give a function
  4277. 3:37:11to another function, we usually use an
  4278. 3:37:14arrow function because it's a bit
  4279. 3:37:16cleaner. So, React will run this
  4280. 3:37:19function after the component is created
  4281. 3:37:23and every time the component is updated
  4282. 3:37:28inside here. Let's try console.log
  4283. 3:37:33the string updated
  4284. 3:37:36and save.
  4285. 3:37:39And now if we go to our project and we
  4286. 3:37:42look at the console. So we'll rightclick
  4287. 3:37:45inspect
  4288. 3:37:46and we go to the console.
  4289. 3:37:50You'll see that after the component is
  4290. 3:37:52created, it runs a function and displays
  4291. 3:37:55updated. And then we're going to go to
  4292. 3:37:58our website and we're going to add a new
  4293. 3:38:00message like hello and press send.
  4294. 3:38:05So now after the component is updated,
  4295. 3:38:09React runs the function again. So, as
  4296. 3:38:12you can see, use effect lets us run some
  4297. 3:38:15code after the component is created or
  4298. 3:38:19updated.
  4299. 3:38:21So, now let's go back into our code
  4300. 3:38:24and we'll give use effect a second
  4301. 3:38:27parameter. So, at the end we'll type a
  4302. 3:38:30comma and we'll give a second parameter
  4303. 3:38:33which is an array.
  4304. 3:38:35So, this array controls when use effect
  4305. 3:38:39runs. If we give it an empty array, use
  4306. 3:38:43effect will only run once after the
  4307. 3:38:46component is created.
  4308. 3:38:49Now, inside this array, we can also put
  4309. 3:38:52some data such as our chat messages. So,
  4310. 3:38:56we'll type chat messages.
  4311. 3:39:00And now, React will also run this
  4312. 3:39:02function every time our data changes or
  4313. 3:39:05every time our chat messages changes.
  4314. 3:39:08If we save
  4315. 3:39:11and we look at the console,
  4316. 3:39:14use effect runs after the component is
  4317. 3:39:17created. And now if we add a message
  4318. 3:39:20like hello and press send,
  4319. 3:39:24use effect also runs after chat messages
  4320. 3:39:27changes.
  4321. 3:39:29So now let's go back to our code.
  4322. 3:39:33And this is called a dependency array.
  4323. 3:39:36It lets us control when use effect runs.
  4324. 3:39:41A best practice is to give use effect a
  4325. 3:39:44dependency array to avoid running this
  4326. 3:39:47function too often.
  4327. 3:39:50So now we learned hooks and the use
  4328. 3:39:52effect hook. And now we can run some
  4329. 3:39:55code after the chat messages changes.
  4330. 3:40:00The last step is after the chat messages
  4331. 3:40:04changes, we want to automatically scroll
  4332. 3:40:07to the bottom. To scroll to the bottom,
  4333. 3:40:11first we need to get the HTML element
  4334. 3:40:14that we want to scroll into our
  4335. 3:40:16JavaScript.
  4336. 3:40:18Let's go back to our code. And inside
  4337. 3:40:21this function, instead of console.log,
  4338. 3:40:23log we need to get this div chat
  4339. 3:40:26messages container into our JavaScript.
  4340. 3:40:30Now in normal JavaScript we might use
  4341. 3:40:33the DOM to get this element but remember
  4342. 3:40:36in React we should not use the DOM
  4343. 3:40:39manually. Instead we should use React
  4344. 3:40:42features to get this element
  4345. 3:40:45to get this element using React. We're
  4346. 3:40:48going to use another hook called use
  4347. 3:40:52ref. So at the top of this component,
  4348. 3:40:56we're going to type the code capital R
  4349. 3:40:58react dot use capital R ref and
  4350. 3:41:04brackets.
  4351. 3:41:06Use ref lets us automatically save an
  4352. 3:41:10HTML element from the component.
  4353. 3:41:13So this code creates a ref. A ref is a
  4354. 3:41:19container with special React features.
  4355. 3:41:22One special feature is we can give this
  4356. 3:41:25ref or this container to React and React
  4357. 3:41:29can automatically save an HTML element
  4358. 3:41:33inside this ref or inside this
  4359. 3:41:36container.
  4360. 3:41:37So between these brackets, we're first
  4361. 3:41:40going to put the value null.
  4362. 3:41:43So this is the initial value that is
  4363. 3:41:46saved inside this ref or inside this
  4364. 3:41:49container. So the ref is going to start
  4365. 3:41:52out as empty. So that's why we use null.
  4366. 3:41:56Now let's save an HTML element inside
  4367. 3:42:00this ref. To do that, first we're going
  4368. 3:42:04to save this ref in a variable. So at
  4369. 3:42:07the front we'll create a variable using
  4370. 3:42:10const and we'll name it chat messages
  4371. 3:42:15ref equals this result.
  4372. 3:42:19And then we're going to go to the HTML
  4373. 3:42:22element we want to save, which is this
  4374. 3:42:24div, the chat messages container.
  4375. 3:42:29And we're going to give this element a
  4376. 3:42:31special prop called ref equals curly
  4377. 3:42:35brackets. And between the curly
  4378. 3:42:38brackets, we're going to give it the ref
  4379. 3:42:40that we created above, which is chat
  4380. 3:42:44messages ref.
  4381. 3:42:48So now React will take this HTML element
  4382. 3:42:52and save it inside this ref or inside
  4383. 3:42:55this container.
  4384. 3:42:58To access the HTML element saved inside
  4385. 3:43:01a ref, we're going to go to use effect
  4386. 3:43:05and we're going to type the name of the
  4387. 3:43:07ref, which is chat messages ref and then
  4388. 3:43:14current.
  4389. 3:43:16Now the reason we put this code in use
  4390. 3:43:18effect is because remember use effect
  4391. 3:43:21runs after the component is created. So
  4392. 3:43:25we need to wait for the component to be
  4393. 3:43:27created first and then we can access
  4394. 3:43:30this HTML element.
  4395. 3:43:33So now let's console.log this value to
  4396. 3:43:37see what's inside this ref. So at the
  4397. 3:43:40front we'll console.log log open bracket
  4398. 3:43:45and close bracket and save.
  4399. 3:43:50And we'll open the console for our
  4400. 3:43:52website.
  4401. 3:43:54And you'll see that React saved the chat
  4402. 3:43:57messages container inside our ref.
  4403. 3:44:01Now that we have the HTML element, we
  4404. 3:44:04need to scroll. Let's learn how to
  4405. 3:44:06scroll it to the bottom. Let's go back
  4406. 3:44:09to our code
  4407. 3:44:11and first we're going to save this HTML
  4408. 3:44:14element in a variable. So we'll remove
  4409. 3:44:17this and at the front we'll create a
  4410. 3:44:20variable const and let's name it
  4411. 3:44:24container
  4412. 3:44:26lm equals this result.
  4413. 3:44:30So ending the variable name with lm
  4414. 3:44:33tells us that this variable contains an
  4415. 3:44:36HTML element.
  4416. 3:44:39and I'll resize my code editor so we can
  4417. 3:44:41see more of the code.
  4418. 3:44:44Next, we'll create a new line. And it's
  4419. 3:44:47a good idea to check if this element
  4420. 3:44:49actually exists before we scroll it. So,
  4421. 3:44:53we'll check if brackets container lm.
  4422. 3:44:59Finally, we'll type curly brackets.
  4423. 3:45:03And to scroll this element to the
  4424. 3:45:05bottom, we can use this code container
  4425. 3:45:09lm dot scroll top and we'll make it
  4426. 3:45:14equal container lm dot scroll height.
  4427. 3:45:22So scroll top means how far from the top
  4428. 3:45:26should we scroll and scroll height gives
  4429. 3:45:29us the total height of the element. So,
  4430. 3:45:32if we set the scroll top to the total
  4431. 3:45:34height, it's going to scroll all the way
  4432. 3:45:36down to the bottom, which is exactly
  4433. 3:45:39what we need. So, now let's save this
  4434. 3:45:42code
  4435. 3:45:44and we'll go to our website and we'll
  4436. 3:45:47add a message test and press send.
  4437. 3:45:52It runs the use effect function because
  4438. 3:45:54chat messages changed and it
  4439. 3:45:57automatically scrolls to the bottom.
  4440. 3:46:00Now, depending on the height of your
  4441. 3:46:01browser, you might need to add a few
  4442. 3:46:03more messages to see this effect. So, we
  4443. 3:46:07can also scroll up and then add another
  4444. 3:46:10message like thank you
  4445. 3:46:13and press send.
  4446. 3:46:16It runs use effect again and it
  4447. 3:46:19automatically scrolls to the bottom. And
  4448. 3:46:22that's all the major features that we
  4449. 3:46:24need for this project. I'll leave some
  4450. 3:46:27extra features like removing these
  4451. 3:46:30default messages and creating the
  4452. 3:46:32loading spinner as exercises.
  4453. 3:46:37And that's it. We created all the
  4454. 3:46:39styling and features that we need and we
  4455. 3:46:42finished the chatbot project. In the
  4456. 3:46:45next lesson, we'll learn how to do a
  4457. 3:46:47proper React setup so we can organize
  4458. 3:46:50our code into different files and
  4459. 3:46:52folders instead of putting everything
  4460. 3:46:55into one HTML file. And this will help
  4461. 3:46:58us work on bigger React projects.
  4462. 3:47:02And that's the end of this lesson. In
  4463. 3:47:04this lesson, we learned how to use CSS
  4464. 3:47:07with React. We styled the chatbot
  4465. 3:47:11project. We reviewed flexbox which lets
  4466. 3:47:15us create a flexible layout. We learned
  4467. 3:47:18the turnary operator which lets us
  4468. 3:47:21insert an if else statement directly in
  4469. 3:47:24the JSX. We learned hooks which lets us
  4470. 3:47:28insert React features into our
  4471. 3:47:30components. We learned the use effect
  4472. 3:47:34hook which lets us run some code after
  4473. 3:47:37the component is created or updated. We
  4474. 3:47:40learned the use ref hook which lets us
  4475. 3:47:43automatically save an HTML element from
  4476. 3:47:47the component
  4477. 3:47:49and we created the auto scroll feature
  4478. 3:47:52for the chat messages.
  4479. 3:47:56Here are some exercises to help you
  4480. 3:47:58practice what we learned in this lesson.
  4481. 3:48:53In this lesson, we're going to learn how
  4482. 3:48:55to do a proper React setup. Right now,
  4483. 3:49:00all of our HTML, CSS, and JavaScript
  4484. 3:49:02code is combined into one HTML file,
  4485. 3:49:07chatbot.html.
  4486. 3:49:09Now, it's not good to combine all of our
  4487. 3:49:11code in three different languages into
  4488. 3:49:14one file because if we work on bigger
  4489. 3:49:16projects, this file might contain
  4490. 3:49:19thousands of lines of code, and it will
  4491. 3:49:21be almost impossible to manage all of
  4492. 3:49:23that code if we put it all in one file.
  4493. 3:49:27Instead, we're going to do a proper
  4494. 3:49:29React setup that will let us split up
  4495. 3:49:32this code into different files and
  4496. 3:49:35folders.
  4497. 3:49:36To create this React setup, we'll need
  4498. 3:49:39to use the command line. So, first let's
  4499. 3:49:42review the command line. We're going to
  4500. 3:49:45go to VS Code and I'm going to make this
  4501. 3:49:48full screen.
  4502. 3:49:50And to open the command line, we're
  4503. 3:49:52going to click the menu at the top and
  4504. 3:49:55we're going to click the terminal menu
  4505. 3:49:57and we're going to open a new terminal.
  4506. 3:50:03So here we can give an instruction or a
  4507. 3:50:07command to the computer. For example,
  4508. 3:50:10we're going to type the command mkdir.
  4509. 3:50:16mkdir means make directory. A directory
  4510. 3:50:20is another name for a folder. So make
  4511. 3:50:24directory creates a new folder.
  4512. 3:50:28Then we'll type a space and we'll give
  4513. 3:50:31this folder a name like test.
  4514. 3:50:35If we press enter,
  4515. 3:50:37it creates a new folder named test. So
  4516. 3:50:40if we open our files on the left, you'll
  4517. 3:50:43see that it created a new folder named
  4518. 3:50:45test.
  4519. 3:50:47If that does not work for you, you can
  4520. 3:50:49check the troubleshooting steps in the
  4521. 3:50:51description.
  4522. 3:50:54So on this line here we can give
  4523. 3:50:57instructions or commands to our computer
  4524. 3:51:00and that's why this is called the
  4525. 3:51:02command line
  4526. 3:51:04and a terminal basically lets us use the
  4527. 3:51:07command line and it also lets us view
  4528. 3:51:09the output of the command line up here.
  4529. 3:51:13So we created a new folder using this
  4530. 3:51:16command. Now you might be wondering why
  4531. 3:51:18did the folder get created here inside
  4532. 3:51:21our project folder. As a review, the
  4533. 3:51:25command line runs inside a specific
  4534. 3:51:27folder.
  4535. 3:51:29To see which folder the command line is
  4536. 3:51:32currently running in, we can go to the
  4537. 3:51:35bottom and type this command pwd or
  4538. 3:51:40print working directory and press enter.
  4539. 3:51:45And this command tells us the command
  4540. 3:51:47line is currently running in the
  4541. 3:51:50react-course folder which is our project
  4542. 3:51:53folder right here. That means any
  4543. 3:51:56commands that we give to the computer
  4544. 3:51:58will be done inside this folder. If we
  4545. 3:52:02make a directory, it will create the new
  4546. 3:52:05directory inside this folder like we see
  4547. 3:52:08here. If we give it another command like
  4548. 3:52:11ls or list and press enter,
  4549. 3:52:16it will list everything inside this
  4550. 3:52:19folder. So it will list everything here.
  4551. 3:52:22As you can see, the command line runs
  4552. 3:52:25inside a specific folder.
  4553. 3:52:28Now this folder that the command line is
  4554. 3:52:31running in is called the working
  4555. 3:52:34directory or the working folder.
  4556. 3:52:37The last thing we need to review about
  4557. 3:52:39the command line is how to change the
  4558. 3:52:42working directory. To do that, we're
  4559. 3:52:45going to go to the bottom and we're
  4560. 3:52:47going to type the command cd or change
  4561. 3:52:50directory and then press space and then
  4562. 3:52:54the name of the folder we want to change
  4563. 3:52:56to like test.
  4564. 3:53:00the command line will look for this
  4565. 3:53:01folder in the working directory which is
  4566. 3:53:04here and then it's going to go into this
  4567. 3:53:06folder. So now if we press enter and
  4568. 3:53:11then type print working directory and
  4569. 3:53:13press enter
  4570. 3:53:15it will tell us the command line is now
  4571. 3:53:17running inside the test folder.
  4572. 3:53:21So the command line basically went
  4573. 3:53:23inside this test folder and it's running
  4574. 3:53:26inside here. By the way, this is known
  4575. 3:53:29as a file path. The slash here means
  4576. 3:53:33that the test folder is inside the
  4577. 3:53:35react-course folder.
  4578. 3:53:38So now if we give a command like ls and
  4579. 3:53:41press enter,
  4580. 3:53:43it will list everything inside the test
  4581. 3:53:46folder because the command line is
  4582. 3:53:48running inside this test folder and it
  4583. 3:53:51tells us there's nothing inside the test
  4584. 3:53:53folder.
  4585. 3:53:55Finally, to get out of the test folder,
  4586. 3:53:58we can type cd or change directory and
  4587. 3:54:02then space and use the special value dot
  4588. 3:54:07dot. Dot dot represents the outer
  4589. 3:54:10folder.
  4590. 3:54:12If we press enter,
  4591. 3:54:14the command line will go out of the test
  4592. 3:54:17folder and back into the react-course
  4593. 3:54:20folder.
  4594. 3:54:23If we type the command pwd or print
  4595. 3:54:26working directory and press enter,
  4596. 3:54:30it will tell us the command line is
  4597. 3:54:32running in the react-ourse folder again.
  4598. 3:54:36So using cd or change directory, we can
  4599. 3:54:39change which folder the command line is
  4600. 3:54:42running in and we can basically move
  4601. 3:54:44around our computer. So this is just a
  4602. 3:54:47review of the command line.
  4603. 3:54:51The second thing we need for the React
  4604. 3:54:53setup is NodeJS.
  4605. 3:54:56So, we're going to review NodeJS.
  4606. 3:55:00First, make sure you have the correct
  4607. 3:55:02version of NodeJS installed. To do that,
  4608. 3:55:05you can click this link in the
  4609. 3:55:07description.
  4610. 3:55:09Scroll down to lesson 5 and follow the
  4611. 3:55:12instructions here to install the correct
  4612. 3:55:14version of NodeJS.
  4613. 3:55:17After Node.js is installed, let's go
  4614. 3:55:20back to VS Code. And if you just
  4615. 3:55:23installed Node.js, we need to restart VS
  4616. 3:55:26Code to make sure that Node.js is
  4617. 3:55:28loaded.
  4618. 3:55:30So, we're going to click X in the top
  4619. 3:55:32right of the terminal
  4620. 3:55:34and then close VS Code.
  4621. 3:55:38And then we're going to open VS Code
  4622. 3:55:40again.
  4623. 3:55:43And we're going to open our project
  4624. 3:55:44folder, React-Course.
  4625. 3:55:46and we're going to open a new terminal.
  4626. 3:55:49So, in the top menu, we're going to go
  4627. 3:55:52to the terminal section and then open a
  4628. 3:55:54new terminal.
  4629. 3:55:58Now, when we install NodeJS, it's going
  4630. 3:56:01to add a new command to our command line
  4631. 3:56:05called node.
  4632. 3:56:07So, the node command lets us run
  4633. 3:56:10JavaScript code outside of a browser.
  4634. 3:56:14For example, here we have a file called
  4635. 3:56:17react-basics.js
  4636. 3:56:20and inside we have some simple
  4637. 3:56:22JavaScript code. If you don't have this
  4638. 3:56:24file, feel free to pause the video and
  4639. 3:56:26create this file. Now we can give this
  4640. 3:56:30file to node and it will run the
  4641. 3:56:32JavaScript code in this file. So here
  4642. 3:56:36we'll use the node command and type
  4643. 3:56:38space and give it the file to run which
  4644. 3:56:42is react-basics.js
  4645. 3:56:46and press enter.
  4646. 3:56:49So this will run the JavaScript inside
  4647. 3:56:52this file and a console.logs hello. So
  4648. 3:56:56as you can see the node command lets us
  4649. 3:56:59run JavaScript code outside of our
  4650. 3:57:02browser.
  4651. 3:57:03Now when we install Node.js, it also
  4652. 3:57:06adds another command to our command line
  4653. 3:57:09called npm.
  4654. 3:57:13npm stands for node package manager. A
  4655. 3:57:17package is basically an external
  4656. 3:57:19library. So npm lets us install external
  4657. 3:57:24libraries or packages into our project.
  4658. 3:57:28To do that, we can use the command npm
  4659. 3:57:32space install
  4660. 3:57:35and then space and then the name of an
  4661. 3:57:38external library or package.
  4662. 3:57:41As an example, we'll install the package
  4663. 3:57:44super simple dev.
  4664. 3:57:48This is a package that I created that we
  4665. 3:57:50can use as an example and press enter.
  4666. 3:57:57On Windows, if you get an error like
  4667. 3:58:00this, you'll need to run this command
  4668. 3:58:02first
  4669. 3:58:05and then run npm install super simple
  4670. 3:58:08dev again.
  4671. 3:58:10If that still does not work for you, you
  4672. 3:58:13can check the troubleshooting steps in
  4673. 3:58:15the description.
  4674. 3:58:18Once npm install finishes, npm will
  4675. 3:58:22download this external library or this
  4676. 3:58:25package into a folder called node
  4677. 3:58:28modules. If we open this folder, we see
  4678. 3:58:31the super simple dev package and inside
  4679. 3:58:35this contains the code that we've been
  4680. 3:58:37using in the chatbot project like
  4681. 3:58:39react.js
  4682. 3:58:41and chatbot.js.
  4683. 3:58:44npm contains millions of packages that
  4684. 3:58:47we can download and use in our project
  4685. 3:58:50and you can search for packages on the
  4686. 3:58:53npm website.
  4687. 3:58:57So this is just a review of NodeJS and
  4688. 3:59:00npm.
  4689. 3:59:02Now that we reviewed NodeJS and npm,
  4690. 3:59:05we're ready to do a proper React setup.
  4691. 3:59:09We just learned that npm has millions of
  4692. 3:59:12packages available to use. Some of these
  4693. 3:59:15packages also add a command to our
  4694. 3:59:19command line. For example, there's a
  4695. 3:59:22package called create-vit.
  4696. 3:59:25If we install this package, it also adds
  4697. 3:59:28a command to our command line called
  4698. 3:59:31create-vit.
  4699. 3:59:33The create-vit command helps us set up a
  4700. 3:59:37new React project.
  4701. 3:59:40To use the create-v command, we need to
  4702. 3:59:43do two steps. Number one, we need to
  4703. 3:59:46install the create-vt package. And
  4704. 3:59:49number two, we're going to run the
  4705. 3:59:52create-v command in the command line.
  4706. 3:59:55Now, npm actually has a shortcut for
  4707. 3:59:59this called npx.
  4708. 4:00:02npx does these two steps in one step.
  4709. 4:00:06The x means execute.
  4710. 4:00:10So let's go to our command line and
  4711. 4:00:13let's type the command npx create-vt.
  4712. 4:00:18And then we'll also type the at symbol.
  4713. 4:00:22At means we're going to use a specific
  4714. 4:00:25version of the create-vit command. For
  4715. 4:00:28this video, we're going to use a
  4716. 4:00:30specific version so that you have the
  4717. 4:00:33same code that I do and you can follow
  4718. 4:00:35along.
  4719. 4:00:37To get the version of create-vt we're
  4720. 4:00:40going to use, we're going to click this
  4721. 4:00:42link in the description.
  4722. 4:00:44Go to lesson 5 and here you'll find the
  4723. 4:00:48version of create-vt we're going to use.
  4724. 4:00:51You can also find the full command here.
  4725. 4:00:55By the way, the version that you see in
  4726. 4:00:57this video might be different than the
  4727. 4:00:59version you see on your page. You should
  4728. 4:01:02always use the version that you see on
  4729. 4:01:04your page. I did this because it's hard
  4730. 4:01:07to update the video. So, this lets me
  4731. 4:01:11easily update the version in the future.
  4732. 4:01:16So, select the version on your page.
  4733. 4:01:19Copy.
  4734. 4:01:21And in our command line, we're going to
  4735. 4:01:23paste it after the at.
  4736. 4:01:27So again, this command will install the
  4737. 4:01:30create-vt package and then it will run
  4738. 4:01:33the create-vit command. The create-v
  4739. 4:01:37command will help us set up a new react
  4740. 4:01:40project.
  4741. 4:01:42By the way, in some tutorials, they
  4742. 4:01:44might use the command npm create vit.
  4743. 4:01:48This is just a shortcut for
  4744. 4:01:50npxcreate-vit.
  4745. 4:01:55Now let's press enter.
  4746. 4:01:58If it says it needs to install this
  4747. 4:02:00package, we can press y for yes and
  4748. 4:02:03press enter.
  4749. 4:02:07Now the create-v command will ask us
  4750. 4:02:10some questions. First it will ask us for
  4751. 4:02:14a project name. So let's name it chatbot
  4752. 4:02:18dash project and press enter.
  4753. 4:02:23And then it will ask us what technology
  4754. 4:02:25we want to use in our project. So we're
  4755. 4:02:28going to press the down arrow and select
  4756. 4:02:31react and press enter.
  4757. 4:02:34And it will ask us what other tools we
  4758. 4:02:37want in our project. We're going to
  4759. 4:02:39press down and just select JavaScript
  4760. 4:02:42and press enter.
  4761. 4:02:46and create vit will create an empty
  4762. 4:02:48React project at this location or this
  4763. 4:02:52folder right here.
  4764. 4:02:54Next, we're going to go into this folder
  4765. 4:02:57with our command line. To do that, we
  4766. 4:03:00can use the command cd or change
  4767. 4:03:03directory and give it the folder name
  4768. 4:03:06chatbot dash project and press enter.
  4769. 4:03:12If we type pwd and press enter,
  4770. 4:03:17it will tell us the command line is now
  4771. 4:03:19running inside of the chatbot project
  4772. 4:03:21folder. So, it's running inside of here.
  4773. 4:03:27The next thing we're going to do is
  4774. 4:03:28we're going to run the command npm
  4775. 4:03:31install.
  4776. 4:03:33Inside our chatbot project folder, we
  4777. 4:03:35have a special file called package.json.
  4778. 4:03:40So, package.json JSON contains a list of
  4779. 4:03:43all the packages that are needed for
  4780. 4:03:46this project. So, npm install is going
  4781. 4:03:49to install all of these packages at the
  4782. 4:03:51same time.
  4783. 4:03:54So, we'll go back to the command line
  4784. 4:03:56and scroll down and we'll run the
  4785. 4:03:58command npm install and press enter.
  4786. 4:04:05Now, this might take a while because
  4787. 4:04:07it's going to install a lot of packages.
  4788. 4:04:09So, we just got to wait until it
  4789. 4:04:11finishes.
  4790. 4:04:15After npm install finishes, all of the
  4791. 4:04:18packages here are going to be downloaded
  4792. 4:04:20into the node modules folder. So, if we
  4793. 4:04:23open this, you'll see that we have a lot
  4794. 4:04:25of packages here. Now, you'll notice
  4795. 4:04:28that there's more packages here than we
  4796. 4:04:30have in the package.json file, and
  4797. 4:04:33that's because some packages require
  4798. 4:04:36other packages. So, npm helps us install
  4799. 4:04:39everything that we need.
  4800. 4:04:44The last step is to run the command npm
  4801. 4:04:48rundev.
  4802. 4:04:50So, this will start up our new React
  4803. 4:04:52website. Let's press enter.
  4804. 4:04:57And now we can view this website by
  4805. 4:04:59opening this URL in our browser. So,
  4806. 4:05:02we're going to select this URL and copy
  4807. 4:05:07and then open our browser.
  4808. 4:05:10And I'm going to make it full screen so
  4809. 4:05:12we can see more of it. We'll create a
  4810. 4:05:14new tab and paste and press enter.
  4811. 4:05:20And now we have a new React website that
  4812. 4:05:23we can work with. So that's how we do a
  4813. 4:05:26proper React setup using the command
  4814. 4:05:29line npm and vit.
  4815. 4:05:34Now that we created a proper React
  4816. 4:05:36setup, let's go back to VS Code
  4817. 4:05:40and we'll learn what each of these files
  4818. 4:05:42and folders do. As a review, the node
  4819. 4:05:47modules folder is where npm installs all
  4820. 4:05:50of the packages.
  4821. 4:05:53Next, we're going to look at the src or
  4822. 4:05:56the source folder.
  4823. 4:05:58This folder contains the code for our
  4824. 4:06:01website. So, all our JavaScript and CSS
  4825. 4:06:04code goes in here. And remember, when
  4826. 4:06:07using React, we write JSX code rather
  4827. 4:06:11than normal JavaScript code. So, the
  4828. 4:06:14files here end with JSX instead of JS.
  4829. 4:06:20Next, this assets folder contains images
  4830. 4:06:23that we want to display on our website.
  4831. 4:06:26So, here we have an image of the React
  4832. 4:06:28logo in SVG format.
  4833. 4:06:33The last folder here is the public
  4834. 4:06:35folder. The public folder contains files
  4835. 4:06:39that should be available to the public.
  4836. 4:06:42Public means we can access these using a
  4837. 4:06:45URL. That means that when this website
  4838. 4:06:48is on the internet, I can give you a URL
  4839. 4:06:52to this image and you'll be able to
  4840. 4:06:54access it.
  4841. 4:06:56So here they just put this vit image in
  4842. 4:06:59the public folder as an example. So now
  4843. 4:07:02we understand the folders that we have
  4844. 4:07:05in this setup.
  4845. 4:07:09Next let's learn about all the files in
  4846. 4:07:12this react setup. The first file.get Git
  4847. 4:07:16ignore is used by a tool called git. Git
  4848. 4:07:20helps us track changes in our code. For
  4849. 4:07:24this chatbot project, we're not going to
  4850. 4:07:26use git. So this file doesn't do
  4851. 4:07:28anything. So we don't need to change
  4852. 4:07:30this file.
  4853. 4:07:32The next file here is eslint.config.js.
  4854. 4:07:37So this is used by a tool called eslint.
  4855. 4:07:42eslint highlights problems in our
  4856. 4:07:44JavaScript code. Let's do an example. If
  4857. 4:07:49we open the source folder and we open
  4858. 4:07:52the file app.jsx
  4859. 4:07:56and let's say that here I accidentally
  4860. 4:07:58misspell setc count by removing the last
  4861. 4:08:01t.
  4862. 4:08:03My VS code doesn't really tell me that
  4863. 4:08:05there is a problem here. But ESLint will
  4864. 4:08:09tell me very clearly that there is a
  4865. 4:08:11problem. To use ESLint, we need to
  4866. 4:08:15install the ESLint extension. So we can
  4867. 4:08:18go to the extension section
  4868. 4:08:21and we'll install the extension ESLint
  4869. 4:08:26and we'll install it.
  4870. 4:08:31After it's installed, if we go back to
  4871. 4:08:33app.jsx
  4872. 4:08:36now, it's going to tell me with a red
  4873. 4:08:37underline that this variable name does
  4874. 4:08:41not match the variable name down here.
  4875. 4:08:44So, if we scroll up and we fix the
  4876. 4:08:48spelling by adding back the t, that red
  4877. 4:08:51underline or that error will go away.
  4878. 4:08:55As you can see, eslint is a really
  4879. 4:08:57useful tool that highlights problems in
  4880. 4:09:00our JavaScript code. So, if we go back
  4881. 4:09:03to the eslint.config.js,
  4882. 4:09:07this file configures eslint, it tells it
  4883. 4:09:11what to check for. So, this
  4884. 4:09:13configuration file is actually good
  4885. 4:09:15enough for now. So, we don't need to
  4886. 4:09:17change this file.
  4887. 4:09:20The next file in our setup is index.htm.
  4888. 4:09:23html.
  4889. 4:09:25This contains all the HTML code for our
  4890. 4:09:28React website. So, this is the same
  4891. 4:09:32thing as chatbot.html.
  4892. 4:09:35The main difference is instead of
  4893. 4:09:38putting all of our code inside the HTML
  4894. 4:09:41file, index.html
  4895. 4:09:44loads our JavaScript from a different
  4896. 4:09:46file.
  4897. 4:09:47So this is the one file that we may need
  4898. 4:09:50to change inside this setup. If we want
  4899. 4:09:53to change the HTML for our website,
  4900. 4:09:57for example, we can change the title at
  4901. 4:10:00the top of the tabs by removing this
  4902. 4:10:04and let's change it to chatbot
  4903. 4:10:08project.
  4904. 4:10:09If we save,
  4905. 4:10:12vit will automatically refresh our page
  4906. 4:10:14just like live server does. And if we
  4907. 4:10:17open our website, you'll see that the
  4908. 4:10:19title at the top changed.
  4909. 4:10:23Let's go back.
  4910. 4:10:25So the index.html
  4911. 4:10:27contains the HTML code for this website.
  4912. 4:10:32The next file in this setup is
  4913. 4:10:34package-lock.json.
  4914. 4:10:37As a review, package.json has a list of
  4915. 4:10:41packages that we need to install to run
  4916. 4:10:44this project. What package lock.json
  4917. 4:10:48does is it saves the version numbers of
  4918. 4:10:52all the packages that we installed. So
  4919. 4:10:55the next time we run npm install, this
  4920. 4:10:58file makes sure that we get the same
  4921. 4:11:00versions for all of these packages.
  4922. 4:11:04Now npm automatically updates package
  4923. 4:11:07lock.json. So we don't need to change
  4924. 4:11:10this file. The next file in the setup is
  4925. 4:11:14package.json. JSON and this basically
  4926. 4:11:17contains the list of packages that we
  4927. 4:11:19need in our project as well as some
  4928. 4:11:21other information about our project.
  4929. 4:11:24When we npm install a package, it will
  4930. 4:11:28automatically update this list of
  4931. 4:11:30packages. So we don't need to change
  4932. 4:11:32this file.
  4933. 4:11:34Next, we have the file readme.md.
  4934. 4:11:38So this is just text. It provides more
  4935. 4:11:41information about this project. And this
  4936. 4:11:44is not code. This is just forformational
  4937. 4:11:46purposes. So we don't need to change
  4938. 4:11:48this file.
  4939. 4:11:50Finally, vit.config.js
  4940. 4:11:54configures vit. Vit is the tool that we
  4941. 4:11:58used to set up this project. Vit also
  4942. 4:12:02helps us build the website. That means
  4943. 4:12:04it takes all the JavaScript and CSS and
  4944. 4:12:07then loads them into the HTML file. Vit
  4945. 4:12:11also creates a server. A server puts our
  4946. 4:12:15website at a URL. For example, here it
  4947. 4:12:19put our website at the URL localhost
  4948. 4:12:225173.
  4949. 4:12:23So this lets us view the website in a
  4950. 4:12:26web browser like a real website. When we
  4951. 4:12:29were building the chatbot project in the
  4952. 4:12:31previous lessons, we used an extension
  4953. 4:12:34called live server. Live server
  4954. 4:12:37automatically refreshes the website when
  4955. 4:12:40we change some code. The VIT server also
  4956. 4:12:44refreshes the website when we change
  4957. 4:12:46some code. So the VIT server is actually
  4958. 4:12:49a replacement for the live server that
  4959. 4:12:52we used earlier. So those are three main
  4960. 4:12:55features of Vit and you can see that
  4961. 4:12:57it's really helpful for setting up and
  4962. 4:13:00developing a React website. We'll go
  4963. 4:13:03back to VS Code.
  4964. 4:13:06So the vit config.js file basically
  4965. 4:13:09configures vit. So this config file is
  4966. 4:13:13good enough. So we don't need to change
  4967. 4:13:15this file. And that's it. Now we
  4968. 4:13:18understand all the files and folders in
  4969. 4:13:22this react setup. As you can see most of
  4970. 4:13:26these files are just configuration files
  4971. 4:13:29and we don't need to change them. The
  4972. 4:13:31only files we need to focus on and
  4973. 4:13:33change are the HTML file and the files
  4974. 4:13:37in the source folder which is our CSS
  4975. 4:13:40and JavaScript files.
  4976. 4:13:44Now that we understand all the files and
  4977. 4:13:47folders in this React setup, let's move
  4978. 4:13:50our previous chatbot project into this
  4979. 4:13:53setup. First, we can close the chatbot
  4980. 4:13:57project folder for now. And let's
  4981. 4:14:00actually remove the node modules folder
  4982. 4:14:02that we created earlier for practice. So
  4983. 4:14:05we'll delete this folder
  4984. 4:14:10and let's also delete the test folder
  4985. 4:14:12that we created for practice
  4986. 4:14:15and also the package lock.json and
  4987. 4:14:18package.json.
  4988. 4:14:28Next, let's go into the chatbot project
  4989. 4:14:30folder and we'll open the source folder
  4990. 4:14:34and we'll start with this file app.jsx.
  4991. 4:14:38So notice that in this file we have a
  4992. 4:14:41react component called app and this
  4993. 4:14:44component represents the entire
  4994. 4:14:47application.
  4995. 4:14:48Now previously in chatbot.html
  4996. 4:14:52we also created an app component.
  4997. 4:14:55So all we have to do is to replace this
  4998. 4:14:58app component with our previous app
  4999. 4:15:01component. So let's go to our code and
  5000. 4:15:04we're going to open chatbot.html
  5001. 4:15:08and we're going to scroll down till we
  5002. 4:15:11see the app component which is down
  5003. 4:15:14here. So we're going to select the app
  5004. 4:15:17component
  5005. 4:15:20and make sure you don't select the code
  5006. 4:15:22down here. We just want to copy the
  5007. 4:15:24component over. So, we're going to copy
  5008. 4:15:27this
  5009. 4:15:29and then go back to app.jsx
  5010. 4:15:33and we're going to remove this app
  5011. 4:15:35component. So, we'll select it
  5012. 4:15:39and remove it and we're going to paste
  5013. 4:15:42the app component from our previous
  5014. 4:15:44project.
  5015. 4:15:46And now we're going to scroll up and
  5016. 4:15:50eslint is going to tell us we have some
  5017. 4:15:52errors here. So we'll go through these
  5018. 4:15:55errors one by one.
  5019. 4:15:58First for this error eslint is going to
  5020. 4:16:02tell us that react is not defined.
  5021. 4:16:05Before we loaded the react external
  5022. 4:16:08library using a script tag and this
  5023. 4:16:12provided a variable called react.
  5024. 4:16:15However, now that we're using NodeJS,
  5025. 4:16:19we usually don't use script tags to load
  5026. 4:16:21the external libraries. Instead, we
  5027. 4:16:25download these packages into Node
  5028. 4:16:27modules and then load it from node
  5029. 4:16:30modules. With node modules, we can use a
  5030. 4:16:34JavaScript feature called modules to
  5031. 4:16:37only load the parts of the external
  5032. 4:16:39library that we need. So when using npm,
  5033. 4:16:43a best practice is to load external
  5034. 4:16:46libraries from node modules. Instead of
  5035. 4:16:48using a script tag
  5036. 4:16:52to load an external library from node
  5037. 4:16:55modules, we use the word import. And
  5038. 4:16:58then we tell the computer which external
  5039. 4:17:01library or which package we want to load
  5040. 4:17:04from by typing from and then the name of
  5041. 4:17:07the package like the string react. If we
  5042. 4:17:11just type a name directly like this and
  5043. 4:17:14we don't start with dot slash or slash,
  5044. 4:17:17this tells the computer to look inside
  5045. 4:17:20node modules for the React package. So,
  5046. 4:17:23it's going to look in here and it's
  5047. 4:17:25going to find the React package in here
  5048. 4:17:28and load it from here.
  5049. 4:17:31Also, when importing from node modules,
  5050. 4:17:34we don't have to import the entire React
  5051. 4:17:37library. we can just import what we
  5052. 4:17:39need. For example, here we just import
  5053. 4:17:43the use state feature from React.
  5054. 4:17:47So now that we imported use state, we
  5055. 4:17:50can just use this directly here instead
  5056. 4:17:53of doing react dot use state. So we can
  5057. 4:17:57remove react dot and use state directly.
  5058. 4:18:03Next, these two lines import images that
  5059. 4:18:07were used in the example website. So,
  5060. 4:18:10we're no longer using these images. So,
  5061. 4:18:12ESLint displays an error. To fix this,
  5062. 4:18:16we can just remove these imports.
  5063. 4:18:20Next, let's scroll down
  5064. 4:18:23and it's going to tell us that the
  5065. 4:18:25components chat messages and chat input
  5066. 4:18:29are missing and they don't exist in this
  5067. 4:18:31file. So, let's actually just copy them
  5068. 4:18:34over from chatbot.html.
  5069. 4:18:38So, we'll go back to the previous
  5070. 4:18:40project
  5071. 4:18:42and in chatbot.html, HTML. We're going
  5072. 4:18:44to scroll up
  5073. 4:18:47and above the app component, we're going
  5074. 4:18:50to copy over all of our other
  5075. 4:18:52components. So, we're going to select
  5076. 4:18:53all this code
  5077. 4:18:56and we're going to copy
  5078. 4:19:00and let's go back to app.jsx
  5079. 4:19:04and we'll scroll up above the app. We're
  5080. 4:19:08going to paste all of the other
  5081. 4:19:11components that we created.
  5082. 4:19:14And now if we scroll down and look at
  5083. 4:19:16our app component, you can see that
  5084. 4:19:18there's no more errors because we moved
  5085. 4:19:21these components into this file. So now
  5086. 4:19:24we've moved the app component into this
  5087. 4:19:27new React setup.
  5088. 4:19:31After copying over all of our other
  5089. 4:19:34components, we now have some more ESLint
  5090. 4:19:37errors. So, let's scroll up to the top
  5091. 4:19:40and we'll fix these errors one by one.
  5092. 4:19:43First, you might see an error here for
  5093. 4:19:46the props.
  5094. 4:19:48ESLint tells us that these props are
  5095. 4:19:51missing props validations.
  5096. 4:19:54Props validation was an older feature of
  5097. 4:19:57React. Newer versions of React don't use
  5098. 4:20:00this feature. So, we can actually turn
  5099. 4:20:02off this check in eslint.
  5100. 4:20:05To turn off a check in eslint, we're
  5101. 4:20:08going to go to the eslint config.js
  5102. 4:20:11file, and we're going to scroll down to
  5103. 4:20:15the rules section here. A rule tells
  5104. 4:20:19ESLint what to check for. At the bottom
  5105. 4:20:22of this object, we're going to add the
  5106. 4:20:25string react slprop-types
  5107. 4:20:31colon and we'll set it to the string off
  5108. 4:20:35and save.
  5109. 4:20:38So, this will turn off this particular
  5110. 4:20:40rule or this particular check. And now,
  5111. 4:20:43if we go back to app.jsx, jsx. We no
  5112. 4:20:47longer have those eslint errors because
  5113. 4:20:50we turned off the check.
  5114. 4:20:52Next, remember that we're no longer
  5115. 4:20:54using react.
  5116. 4:20:56We can just use state directly. So, we
  5117. 4:21:00can remove react dot.
  5118. 4:21:03Next, let's scroll down and eslint will
  5119. 4:21:07tell us that chatbot does not exist in
  5120. 4:21:10this file. So, previously we loaded the
  5121. 4:21:14chatbot using a script tag. But
  5122. 4:21:17remember, now that we're using npm, a
  5123. 4:21:20best practice is to load the chatbot
  5124. 4:21:22from node modules.
  5125. 4:21:25First, we need to download the chatbot
  5126. 4:21:27into node modules. To do that, we're
  5127. 4:21:30going to install the package super
  5128. 4:21:32simple dev. Let's go back to the command
  5129. 4:21:35line.
  5130. 4:21:37And currently, it's running the vit
  5131. 4:21:39server. So, we're going to stop the
  5132. 4:21:41server by pressing control C on Windows
  5133. 4:21:44or control C on Mac. Note that on Mac,
  5134. 4:21:48it's control C, not command C. So, this
  5135. 4:21:52will stop the V server and bring us back
  5136. 4:21:55to the command line. Usually, to exit
  5137. 4:21:58something from the command line, we can
  5138. 4:22:00press control C in both Windows and Mac.
  5139. 4:22:04Next, we're going to npm
  5140. 4:22:08install
  5141. 4:22:09the package super simple dev. So, this
  5142. 4:22:15package contains the chatbot code that
  5143. 4:22:17we were using earlier. Let's press
  5144. 4:22:20enter.
  5145. 4:22:23It's going to install the super simple
  5146. 4:22:24dev package inside node modules. And now
  5147. 4:22:28to load it from node modules, we're
  5148. 4:22:31going to scroll up in our code and at
  5149. 4:22:34the top we're going to import
  5150. 4:22:38and type curly brackets. So we're going
  5151. 4:22:41to import the chatbot object and we're
  5152. 4:22:46going to import it from a string and
  5153. 4:22:50we'll import it from the package super
  5154. 4:22:53simple dev. So this will load the super
  5155. 4:22:57simple dev package from inside node
  5156. 4:23:00modules and get the chatbot out of it.
  5157. 4:23:03And now that we have this chatbot
  5158. 4:23:05object, if we scroll down, we have
  5159. 4:23:08access to it. So it's no longer giving
  5160. 4:23:10us an error.
  5161. 4:23:13Finally, we're going to scroll down
  5162. 4:23:16to the chat messages component.
  5163. 4:23:20And here it's telling us that React does
  5164. 4:23:23not exist. So just like with use state,
  5165. 4:23:27we can import use ref and use effect
  5166. 4:23:30directly as well.
  5167. 4:23:33So let's remove the react dot from here
  5168. 4:23:37and remove react dot from here.
  5169. 4:23:41And we're going to scroll up
  5170. 4:23:44and at the top we're already importing
  5171. 4:23:47from the React package. To import
  5172. 4:23:50multiple things we can just add a comma
  5173. 4:23:53at the end. and import use ref and add
  5174. 4:23:58another comma and import use effect.
  5175. 4:24:04This gets these two features out of
  5176. 4:24:06React and now we can use them directly
  5177. 4:24:09in our code.
  5178. 4:24:11And now we've fixed all the ESLint
  5179. 4:24:13errors in our app component. If you did
  5180. 4:24:15the exercises in the previous lesson,
  5181. 4:24:18you may have some more ESLint errors.
  5182. 4:24:21So, make sure you fix any other errors
  5183. 4:24:23that you have. And finally, let's save
  5184. 4:24:26this file.
  5185. 4:24:29Next, we're going to look at the file
  5186. 4:24:32main.jsx.
  5187. 4:24:34So, this code sets up React. It calls
  5188. 4:24:38create root, gives it a container, and
  5189. 4:24:41then renders the app component onto the
  5190. 4:24:45website. If we open our chatbot.html
  5191. 4:24:48HTML and we scroll to the very bottom.
  5192. 4:24:55You notice that we do the same thing. We
  5193. 4:24:58call create root, give it a container,
  5194. 4:25:00and then render the app onto the
  5195. 4:25:03website. So main.jsx
  5196. 4:25:06does the same thing as this code. It
  5197. 4:25:09sets up React and displays the app onto
  5198. 4:25:13the website.
  5199. 4:25:14Now, instead of copying this code over
  5200. 4:25:17to main.j jsx. We're just going to use
  5201. 4:25:20the code in this file because it
  5202. 4:25:22basically does the same thing. It just
  5203. 4:25:25uses an ID for the container instead of
  5204. 4:25:27a class. The one other difference in
  5205. 4:25:30this code is it wraps the app in a
  5206. 4:25:33component called strict mode. Strict
  5207. 4:25:36mode is a special component provided by
  5208. 4:25:39React. Strict mode basically gives us
  5209. 4:25:42some additional checks and warnings when
  5210. 4:25:45developing our app.
  5211. 4:25:47And that's it. We moved all of our
  5212. 4:25:50JavaScript code into this new setup.
  5213. 4:25:53Now, let's open this app in our browser
  5214. 4:25:56to see what it looks like. We'll go down
  5215. 4:25:59to the command line and let's make sure
  5216. 4:26:02we're running inside the chatbot project
  5217. 4:26:05folder by typing pwd and press enter.
  5218. 4:26:10So, that looks good. And we're going to
  5219. 4:26:12start the VIT server by typing npm
  5220. 4:26:17rundev
  5221. 4:26:18and press enter.
  5222. 4:26:21And we'll open this URL in our browser.
  5223. 4:26:25I already have it open.
  5224. 4:26:27So I'll open my browser and refresh this
  5225. 4:26:30page. And now the website displays our
  5226. 4:26:33previous chatbot app. in the text box.
  5227. 4:26:38If we type a message like hello and
  5228. 4:26:41press send,
  5229. 4:26:43it will display a response from the
  5230. 4:26:45chatbot.
  5231. 4:26:47However, obviously it doesn't look
  5232. 4:26:50right, and that's because we haven't
  5233. 4:26:52moved our CSS code and images over into
  5234. 4:26:56the new setup. So, we're going to do
  5235. 4:26:58that next.
  5236. 4:27:01Let's move our CSS code into this new
  5237. 4:27:04React setup.
  5238. 4:27:06We'll go back to VS Code
  5239. 4:27:08and we're going to open chatbot.html
  5240. 4:27:13and we're going to go to the top of this
  5241. 4:27:15file right here.
  5242. 4:27:18So at the top we have all of our CSS
  5243. 4:27:21code. We're going to move this CSS code
  5244. 4:27:24into app.css.
  5245. 4:27:27So, we'll select all of this CSS
  5246. 4:27:33all the way to the bottom and we're
  5247. 4:27:36going to copy it
  5248. 4:27:39and then open app.css
  5249. 4:27:42and we're going to remove this default
  5250. 4:27:44CSS. So, we'll remove this and then
  5251. 4:27:48paste our CSS code and save.
  5252. 4:27:54And now how does this CSS code get
  5253. 4:27:56loaded on the website? If we open
  5254. 4:28:00app.jsx,
  5255. 4:28:02notice at the top we import/app.css.
  5256. 4:28:08So dot slash means the current folder.
  5257. 4:28:11It means the folder that contains this
  5258. 4:28:14file, which is the source folder. So
  5259. 4:28:17it's going to start from source and look
  5260. 4:28:19for a file called app.css.
  5261. 4:28:23and it finds it here and it will load
  5262. 4:28:25all the CSS code in this file onto the
  5263. 4:28:29website. So you might be wondering how
  5264. 4:28:32can we import a CSS file like this?
  5265. 4:28:36Doesn't import only work for JavaScript
  5266. 4:28:38files. So this is actually a feature of
  5267. 4:28:42Vit. Vit lets us import any type of file
  5268. 4:28:46like a CSS file or even an image file.
  5269. 4:28:50Depending on the type of file, it will
  5270. 4:28:53behave differently. If we import a CSS
  5271. 4:28:57file, vit will load all the CSS code in
  5272. 4:29:01this file and apply it to the website.
  5273. 4:29:04So that's how this works. We use Vit to
  5274. 4:29:07load the CSS code inside app.css CSS.
  5275. 4:29:12And this allows us to separate our CSS
  5276. 4:29:15code into its own file to make our code
  5277. 4:29:18more organized.
  5278. 4:29:20Next, you'll also notice that in here we
  5279. 4:29:23have an index.css
  5280. 4:29:25file.
  5281. 4:29:27This CSS file is loaded by main.jsx
  5282. 4:29:32here using this import. So why do we
  5283. 4:29:36have two CSS files? A best practice is
  5284. 4:29:40to split up our CSS into different
  5285. 4:29:42files. All the CSS for the app component
  5286. 4:29:47go inside the app.css file. All the CSS
  5287. 4:29:51for the overall website should go inside
  5288. 4:29:54index.css.
  5289. 4:29:57If we open app.css
  5290. 4:30:00and we scroll to the top,
  5291. 4:30:02the body styles here are not related to
  5292. 4:30:06the app component. This styles the
  5293. 4:30:09overall website. So a best practice is
  5294. 4:30:12to split up this code and move this into
  5295. 4:30:16index.css.
  5296. 4:30:18So we'll select this code and then cut
  5297. 4:30:23and save
  5298. 4:30:25and then inside index.css CSS.
  5299. 4:30:29We're going to remove this default code
  5300. 4:30:33and paste those body styles and save.
  5301. 4:30:40If we go back to app.css,
  5302. 4:30:43the rest of this code is related to the
  5303. 4:30:45app component. So, it makes sense to
  5304. 4:30:48leave it in app.css.
  5305. 4:30:51And now we moved all of our CSS into our
  5306. 4:30:54new React setup. So, let's open our
  5307. 4:30:58project again in the browser to see what
  5308. 4:31:00it looks like.
  5309. 4:31:02And now it's looking pretty good. It's
  5310. 4:31:04looking like our previous chatbot
  5311. 4:31:06project. And we have the layout and
  5312. 4:31:09styling that we need. The last thing
  5313. 4:31:11we're missing here are the images. So,
  5314. 4:31:14we're going to move those. Next,
  5315. 4:31:18let's move the robot image and the user
  5316. 4:31:21image into the new React setup. We'll go
  5317. 4:31:25back to VS Code
  5318. 4:31:28and we're going to scroll down and we're
  5319. 4:31:30going to select the robot.png image and
  5320. 4:31:34we'll copy it by pressing control C on
  5321. 4:31:36Windows or command C on Mac.
  5322. 4:31:40And let's scroll up and we're going to
  5323. 4:31:43put it inside the source/assets
  5324. 4:31:46folder. So, this is typically where we
  5325. 4:31:49put our images. So, let's click on this
  5326. 4:31:52assets folder and we're going to paste
  5327. 4:31:54the image by pressing controlV on
  5328. 4:31:56Windows or commandV on Mac.
  5329. 4:32:00And now we have the robot image in our
  5330. 4:32:02new setup. Let's do the same for the
  5331. 4:32:06user image. So, we'll go down and select
  5332. 4:32:09user.png
  5333. 4:32:11and we'll copy
  5334. 4:32:14and then select the assets folder and
  5335. 4:32:17we'll paste.
  5336. 4:32:20And now we need to load these images
  5337. 4:32:22into our app. Let's open app.jsx.
  5338. 4:32:28And to load this image, we can actually
  5339. 4:32:30just import it. So at the top, let's
  5340. 4:32:34type import
  5341. 4:32:36and we'll give this image a name like
  5342. 4:32:40robot profile image. And we'll import it
  5343. 4:32:46from a string. and we're going to locate
  5344. 4:32:50this image file. So inside here, we'll
  5345. 4:32:54start by typing dot slash, which means
  5346. 4:32:57the current folder or the folder that
  5347. 4:33:00contains app.jsx,
  5348. 4:33:02which is the source folder. So we're
  5349. 4:33:05going to start from the source folder
  5350. 4:33:07and then we're going to go into the
  5351. 4:33:09assets folder. So, we'll type assets.
  5352. 4:33:13And to go into this folder, we're going
  5353. 4:33:15to type a slash. And then we're going to
  5354. 4:33:18look for the file robot.png.
  5355. 4:33:22So, we'll type robot.png.
  5356. 4:33:27So, this is a file path. It tells the
  5357. 4:33:29computer how to locate this file.
  5358. 4:33:33So also notice that in this import we
  5359. 4:33:36just gave it a name instead of using
  5360. 4:33:39curly brackets.
  5361. 4:33:41This is called a default export and
  5362. 4:33:44we'll review this later in the lesson.
  5363. 4:33:47But for now just remember that when we
  5364. 4:33:49import an image we just give it a name
  5365. 4:33:52without curly brackets.
  5366. 4:33:54Let's also load the user image. So,
  5367. 4:33:57we're going to import and give it a
  5368. 4:34:01name, user profile image, and we'll
  5369. 4:34:05import it from a string. And we're going
  5370. 4:34:09to locate the user.png file. So, we'll
  5371. 4:34:12start from the current folder using dot
  5372. 4:34:15slash. We'll look for the assets folder
  5373. 4:34:18and then slash to go into this folder.
  5374. 4:34:22And we'll look for the file user.png.
  5375. 4:34:27So remember this is a feature of vit.
  5376. 4:34:31Vit lets us import any type of file
  5377. 4:34:33including an image file. When we import
  5378. 4:34:37an image like this, vit gives us a file
  5379. 4:34:41path to that image. So this contains a
  5380. 4:34:44file path like this. And we can use this
  5381. 4:34:48directly in an images source attribute.
  5382. 4:34:52So, let's scroll down to the chat
  5383. 4:34:56message component. And in here, we have
  5384. 4:34:59our two image elements. So, in the
  5385. 4:35:02source attribute, instead of robot.png,
  5386. 4:35:07we're going to give it those images that
  5387. 4:35:08we imported by typing curly brackets.
  5388. 4:35:13And here for the robot, we'll insert the
  5389. 4:35:16variable robot profile image. And we'll
  5390. 4:35:22do the same thing for the user. We'll
  5391. 4:35:24remove the source attribute
  5392. 4:35:27and type curly brackets. And we'll
  5393. 4:35:30insert the user profile image. So that's
  5394. 4:35:35how we import images using Vit and use
  5395. 4:35:39them. Now let's save
  5396. 4:35:42and we'll check our website.
  5397. 4:35:45And now all of the images are loaded.
  5398. 4:35:48So, let's try adding a message to make
  5399. 4:35:50sure this is working. We'll type the
  5400. 4:35:53message hello and press send.
  5401. 4:35:57And that's it. We finished moving our
  5402. 4:36:00chatbot project into this new React
  5403. 4:36:03setup.
  5404. 4:36:05We'll go back to VS Code.
  5405. 4:36:09And the big advantage of this setup is
  5406. 4:36:12we can separate our code into different
  5407. 4:36:14files instead of combining everything
  5408. 4:36:17into one file like we had before. Right
  5409. 4:36:20now we already separated our code a bit.
  5410. 4:36:24We separated the app component into
  5411. 4:36:27app.jsx
  5412. 4:36:28and we separated the code that sets up
  5413. 4:36:31React into main.jsx.
  5414. 4:36:35Now we're going to separate our code
  5415. 4:36:37even more. So, inside app.jsx,
  5416. 4:36:41if we scroll to the top,
  5417. 4:36:44notice that there's many other
  5418. 4:36:46components in this file, like the chat
  5419. 4:36:49input component. A best practice is to
  5420. 4:36:52separate each component into its own
  5421. 4:36:55file. This makes our code easier to work
  5422. 4:36:58with because each file only focuses on
  5423. 4:37:02one component.
  5424. 4:37:04So, let's separate the chat input
  5425. 4:37:06component into its own file.
  5426. 4:37:10First, we're going to go to the source
  5427. 4:37:12folder and we're going to create a new
  5428. 4:37:14folder in here to group our components
  5429. 4:37:17together. So, we'll click this folder
  5430. 4:37:20and we're going to click this icon to
  5431. 4:37:23create a new folder and we'll name it
  5432. 4:37:26components
  5433. 4:37:28and press enter.
  5434. 4:37:31And inside this components folder, we're
  5435. 4:37:34going to create a new file for our chat
  5436. 4:37:37input component. So in here, we'll click
  5437. 4:37:40it and then click this icon to create a
  5438. 4:37:43new file. And we'll give it the same
  5439. 4:37:46name as the component. So we'll name it
  5440. 4:37:49capital C chat capital I input.jsx
  5441. 4:37:55and press enter.
  5442. 4:37:58And now we're going to move the chat
  5443. 4:38:01input component into this file. So we'll
  5444. 4:38:04open app.jsx
  5445. 4:38:07and we'll select the code for chat input
  5446. 4:38:10which is down here.
  5447. 4:38:13And we're going to cut
  5448. 4:38:17and then open chatinput.jsx.
  5449. 4:38:21And in here we'll paste.
  5450. 4:38:25and we'll scroll up and we're going to
  5451. 4:38:28fix these eslint errors. We'll start
  5452. 4:38:31with this one. Use state currently
  5453. 4:38:34doesn't exist in this file. So, we need
  5454. 4:38:37to import it again.
  5455. 4:38:39Let's go back to app.jsx.
  5456. 4:38:42And at the top, let's copy this import.
  5457. 4:38:46So, we'll select it and copy.
  5458. 4:38:50And we'll go to the chat input file. And
  5459. 4:38:54at the top of this,
  5460. 4:38:56we're going to paste the import.
  5461. 4:38:59Now that we imported the use state,
  5462. 4:39:02we're able to use it and there's no
  5463. 4:39:04longer an error. Now, eslint also tells
  5464. 4:39:07us that we're not using use ref or use
  5465. 4:39:10effect in this file. So, we can remove
  5466. 4:39:13these.
  5467. 4:39:15Next, we're going to scroll down in this
  5468. 4:39:18file and we'll see that the chatbot
  5469. 4:39:21object doesn't exist anymore in this
  5470. 4:39:24file. So again, we'll go back to app.jsx
  5471. 4:39:29and let's move this import. So we'll
  5472. 4:39:32select it and chatbot is not used
  5473. 4:39:35anymore in this file. So this time we're
  5474. 4:39:38going to cut
  5475. 4:39:41and inside chat input at the top we're
  5476. 4:39:46going to paste that input.
  5477. 4:39:49And now we have access to the chatbot.
  5478. 4:39:53The last error here is that the chat
  5479. 4:39:56input component is not being used. To
  5480. 4:39:59fix this, we need to review a feature of
  5481. 4:40:02JavaScript called modules.
  5482. 4:40:05By default, any variables or functions
  5483. 4:40:08that we create in a file only exist in
  5484. 4:40:12that file. So chat input only exists in
  5485. 4:40:17this file. It cannot be used outside
  5486. 4:40:20this file. This helps us avoid naming
  5487. 4:40:24conflicts because if we use the same
  5488. 4:40:26variable name in different files, it
  5489. 4:40:29won't cause an error. Now, if we want to
  5490. 4:40:33use this variable outside of this file,
  5491. 4:40:35we need to export it first. To do that
  5492. 4:40:39in front of this function, we can just
  5493. 4:40:42type export.
  5494. 4:40:45And now this error is gone because
  5495. 4:40:47ESLint assumes that we're going to use
  5496. 4:40:49this component outside of this file. So
  5497. 4:40:53now let's save.
  5498. 4:40:56And to use chat input in another file,
  5499. 4:40:59we need to import it. So we'll open
  5500. 4:41:02app.jsx.
  5501. 4:41:05And at the top, we're going to import
  5502. 4:41:10and type curly brackets. And in here,
  5503. 4:41:14we'll import the chat input component.
  5504. 4:41:18And we'll import it from a string. And
  5505. 4:41:22we're going to locate the chat input
  5506. 4:41:25file.
  5507. 4:41:26At the front, we'll type dot slash to
  5508. 4:41:30start in the current folder, which is
  5509. 4:41:32the source folder. And then we're going
  5510. 4:41:34to go to the components folder.
  5511. 4:41:38And we'll type a slash to go into this
  5512. 4:41:41folder. And we'll locate the file chat
  5513. 4:41:45input.jsx.
  5514. 4:41:49By the way, when importing a JavaScript
  5515. 4:41:51file or a JSX file, if we're using Vit,
  5516. 4:41:56we don't have to add.js or JSX at the
  5517. 4:42:00end. Vit will actually add this
  5518. 4:42:02automatically. So, as a shortcut, we can
  5519. 4:42:05remove this.
  5520. 4:42:08Also, a common practice is to import
  5521. 4:42:11from packages at the top and then
  5522. 4:42:14JavaScript files and then other types of
  5523. 4:42:16files. So now we learn how to create
  5524. 4:42:19these imports ourselves using the
  5525. 4:42:22JavaScript modules feature and the word
  5526. 4:42:25export. And we can use chat input in
  5527. 4:42:29this file.
  5528. 4:42:32Next, let's move the rest of these
  5529. 4:42:34components into separate files. First,
  5530. 4:42:38we'll move the chat message component.
  5531. 4:42:41In the components folder, we'll click it
  5532. 4:42:44and we'll click this icon to create a
  5533. 4:42:46new file.
  5534. 4:42:48And let's name it the same as the
  5535. 4:42:50component. Capital C chat, capital M
  5536. 4:42:54message.jsx,
  5537. 4:42:58and press enter.
  5538. 4:43:01And now we're going to move the code
  5539. 4:43:02over. So we'll open app.jsx JSX and
  5540. 4:43:06we'll select this component
  5541. 4:43:10and cut.
  5542. 4:43:14And then inside chat message.jsx,
  5543. 4:43:18we're going to paste the component.
  5544. 4:43:21And again, we'll scroll up and fix the
  5545. 4:43:24ESLint errors. So, first it's telling us
  5546. 4:43:28we're not using this component in this
  5547. 4:43:30file. To fix this at the front, we're
  5548. 4:43:33going to export this component so it can
  5549. 4:43:36be used in other files.
  5550. 4:43:39If we scroll down, ESLint tells us that
  5551. 4:43:43these images are missing. So, we need to
  5552. 4:43:46move those imports into this file. We'll
  5553. 4:43:50go back to app.jsx JSX
  5554. 4:43:53and at the top we're going to select
  5555. 4:43:55these two image imports and we're going
  5556. 4:43:58to cut
  5557. 4:44:02and inside chat message.jsx
  5558. 4:44:04we're going to scroll to the top
  5559. 4:44:08and paste.
  5560. 4:44:12Now one thing we need to be careful
  5561. 4:44:14about is that we may need to update
  5562. 4:44:16these file paths. So now this file is
  5563. 4:44:21inside the components folder. That means
  5564. 4:44:24dot slash is going to start at the
  5565. 4:44:26components folder. It's not going to be
  5566. 4:44:28able to find the assets folder anymore.
  5567. 4:44:32To fix this at the front, we're going to
  5568. 4:44:35use dot dot slash.
  5569. 4:44:38So just like in the command line, dot
  5570. 4:44:41dot represents the outer folder. This
  5571. 4:44:44means it's going to go out of this
  5572. 4:44:47components folder and into the source
  5573. 4:44:49folder. And then it's going to look for
  5574. 4:44:51the assets folder. It's going to find it
  5575. 4:44:53and it will go into the assets folder
  5576. 4:44:56and find robot.png.
  5577. 4:44:59And we need to make the same change for
  5578. 4:45:01the user image. At the front, we'll use
  5579. 4:45:05dot dot to go out of the components
  5580. 4:45:08folder into source. And then we can
  5581. 4:45:10locate the assets folder.
  5582. 4:45:13And now these imports will work
  5583. 4:45:15correctly. And we finished moving the
  5584. 4:45:18chat message component into its own
  5585. 4:45:20file.
  5586. 4:45:22Let's save.
  5587. 4:45:24And we'll go back to app.jsx.
  5588. 4:45:28And we'll move the chat messages
  5589. 4:45:30component into its own file. Inside the
  5590. 4:45:34components folder, we'll click it. We'll
  5591. 4:45:37click this icon to create a new file.
  5592. 4:45:41And let's name it the same as the
  5593. 4:45:42component capital C chat capital M
  5594. 4:45:46messages.jsx
  5595. 4:45:50and press enter.
  5596. 4:45:53We'll go back to app.jsx JSX and we'll
  5597. 4:45:56select the code for this component which
  5598. 4:46:00is till here and we're going to cut.
  5599. 4:46:07And inside chat messages we're going to
  5600. 4:46:10paste.
  5601. 4:46:13Let's scroll to the top.
  5602. 4:46:15And this component needs use ref and use
  5603. 4:46:19effect. So let's move those imports into
  5604. 4:46:23this file. We'll go to app.jsx.
  5605. 4:46:28We'll select this import at the top
  5606. 4:46:31and copy.
  5607. 4:46:34And in chat messages
  5608. 4:46:37at the top,
  5609. 4:46:40we're going to paste that input.
  5610. 4:46:44And in this component, we do not use use
  5611. 4:46:46state. So we can remove it.
  5612. 4:46:51Let's scroll down. And in this
  5613. 4:46:53component, we also use the chat message
  5614. 4:46:56component. So let's import that. We'll
  5615. 4:46:59scroll up and at the top, we'll import
  5616. 4:47:04curly brackets the chat message
  5617. 4:47:09component from a string
  5618. 4:47:12and we'll locate this file using dot
  5619. 4:47:17slash. So it starts in the current
  5620. 4:47:19folder which is the components folder
  5621. 4:47:22and we'll look for the file chat message
  5622. 4:47:27and remember we don't have to add JSX at
  5623. 4:47:31the end. Vit can automatically add it.
  5624. 4:47:36Finally eslint tells us this component
  5625. 4:47:39is not used in this file. So we need to
  5626. 4:47:42export it. However we're going to do
  5627. 4:47:45something different this time.
  5628. 4:47:48Instead of typing export at the front of
  5629. 4:47:50this function, we're going to scroll
  5630. 4:47:52down to the bottom and at the end, we're
  5631. 4:47:57going to type export default
  5632. 4:48:01and then what we want to export, which
  5633. 4:48:04is the chat messages component.
  5634. 4:48:09As a review, this is called a default
  5635. 4:48:12export. It's useful if we want to export
  5636. 4:48:15one thing from a file. So now let's
  5637. 4:48:19save.
  5638. 4:48:21And to import this, we're going to go to
  5639. 4:48:24app.jsx.
  5640. 4:48:26And at the top, we're going to import.
  5641. 4:48:30And for a default export, we can just
  5642. 4:48:33type a name without curly brackets. So
  5643. 4:48:37we can type chat messages.
  5644. 4:48:41We'll import it from a string
  5645. 4:48:45and we'll type dot slash to start in the
  5646. 4:48:47current folder. So for this file, we're
  5647. 4:48:50in the source folder and then we're
  5648. 4:48:52going to go into the components folder
  5649. 4:48:55components slash and locate chat
  5650. 4:48:59messages.
  5651. 4:49:02So this imports whatever the default
  5652. 4:49:06export is from this file and names it
  5653. 4:49:09chat messages. This feature is called a
  5654. 4:49:12default export. When we import and we
  5655. 4:49:16use curly brackets like up here, this is
  5656. 4:49:19called a named export.
  5657. 4:49:22So some people like using default
  5658. 4:49:24exports because it encourages each file
  5659. 4:49:28to focus on one thing. However, it's up
  5660. 4:49:31to you which style of export you want to
  5661. 4:49:34use. You'll also find that some packages
  5662. 4:49:37use named exports while other packages
  5663. 4:49:40use default exports.
  5664. 4:49:43So, this is just a review of JavaScript
  5665. 4:49:46modules. If you want to learn about
  5666. 4:49:48modules in more detail, you can check
  5667. 4:49:50out my JavaScript full course in the
  5668. 4:49:52video description.
  5669. 4:49:55Next, let's fix these last few errors.
  5670. 4:49:58So we're not using use ref or use effect
  5671. 4:50:01in this file anymore. So we can remove
  5672. 4:50:04these.
  5673. 4:50:07And finally, this app component is
  5674. 4:50:10already in a file called app.jsx.
  5675. 4:50:14So we can just leave it in this file.
  5676. 4:50:17And usually we put the app component
  5677. 4:50:20outside of the components folder because
  5678. 4:50:23app represents the entire application.
  5679. 4:50:27It's sort of the most outer component of
  5680. 4:50:29our application. So it makes sense to
  5681. 4:50:32put it in the outer folder.
  5682. 4:50:35So now let's save this file.
  5683. 4:50:38And let's make sure that all these other
  5684. 4:50:40files are saved. And now we'll open our
  5685. 4:50:43website in the browser and try it out.
  5686. 4:50:47And it looks pretty good. Let's refresh
  5687. 4:50:50just to be safe. And we'll give it a
  5688. 4:50:53try. We'll click this text box and type
  5689. 4:50:56the message hello and press send.
  5690. 4:51:01So, it's working correctly just like
  5691. 4:51:03before. So, we'll go back and now we
  5692. 4:51:07separated each component into its own
  5693. 4:51:11file. And this makes our code easier to
  5694. 4:51:14work with because each file only focuses
  5695. 4:51:17on one component and each file contains
  5696. 4:51:21less code.
  5697. 4:51:23The last thing we're going to do in this
  5698. 4:51:25lesson is to separate the CSS into
  5699. 4:51:28different files. So, let's open app.css.
  5700. 4:51:33And you'll notice that in here it
  5701. 4:51:35contains CSS for multiple components.
  5702. 4:51:39So, we're also going to separate the CSS
  5703. 4:51:42so that each component has its own CSS
  5704. 4:51:45file.
  5705. 4:51:47For example, this CSS code is related to
  5706. 4:51:50the chat input component. So, we'll go
  5707. 4:51:54to the components folder and click it.
  5708. 4:51:57And we'll click up here to create a new
  5709. 4:51:59file. And we're going to name it the
  5710. 4:52:02same as the component, capital C, chat,
  5711. 4:52:05capital I input
  5712. 4:52:08CSS, and press enter.
  5713. 4:52:13and we'll move all the CSS related to
  5714. 4:52:16the chat input component in here. So in
  5715. 4:52:19app.css,
  5716. 4:52:22we'll select this code for the send
  5717. 4:52:24button, the chat input, and the chat
  5718. 4:52:27input container.
  5719. 4:52:29And we'll cut
  5720. 4:52:32and chat input.css,
  5721. 4:52:35we're going to paste that code
  5722. 4:52:38and save.
  5723. 4:52:41And now to load this CSS code, we're
  5724. 4:52:44going to open chatinput.jsx.
  5725. 4:52:48And at the top, we're going to import a
  5726. 4:52:52string
  5727. 4:52:54and the CSS file we want to load, which
  5728. 4:52:57is this one. So we'll type dot slash to
  5729. 4:53:01start in the current folder, which is
  5730. 4:53:03components, and we'll load chat
  5731. 4:53:07input.css.
  5732. 4:53:11and save.
  5733. 4:53:13As a review, this is a feature of Vit.
  5734. 4:53:17If we import a CSS file, it will load
  5735. 4:53:20all the CSS in the file and apply it to
  5736. 4:53:23the website.
  5737. 4:53:25Next, let's go back to app.css.
  5738. 4:53:30And these styles are for the app
  5739. 4:53:32component. So, it actually makes sense
  5740. 4:53:34to leave them inside the app.css file.
  5741. 4:53:39Below this, these styles are for the
  5742. 4:53:42chat message component. So, let's create
  5743. 4:53:45a CSS file for the chat message
  5744. 4:53:48component. We'll go into the components
  5745. 4:53:50folder and click here to create a new
  5746. 4:53:53file. And we'll name this chat
  5747. 4:53:57message.css
  5748. 4:54:00and press enter.
  5749. 4:54:03And we'll go back to app.css CSS and
  5750. 4:54:07we're going to select the styles for the
  5751. 4:54:09chat message user, the chat message
  5752. 4:54:11robot, the text, as well as the chat
  5753. 4:54:15message profile
  5754. 4:54:17and cut.
  5755. 4:54:21And then inside chat message.css,
  5756. 4:54:24we're going to paste the CSS code and
  5757. 4:54:28save.
  5758. 4:54:30And we're going to load this by going
  5759. 4:54:32into chat message.jsx JSX and at the top
  5760. 4:54:36we're going to import a string and we'll
  5761. 4:54:40import this file using dot slash
  5762. 4:54:45chat message
  5763. 4:54:48CSS and save.
  5764. 4:54:53We'll go back to app.css.
  5765. 4:54:56Finally, these styles are for the chat
  5766. 4:54:59messages component.
  5767. 4:55:01First, we'll create a CSS file for this
  5768. 4:55:04component. So, we'll click this folder
  5769. 4:55:07and click here to create a new file and
  5770. 4:55:10we'll name it chat messages.css
  5771. 4:55:15and press enter.
  5772. 4:55:18And in app.css,
  5773. 4:55:20we're going to select this code and cut.
  5774. 4:55:25And in chat messages.css, CSS. We're
  5775. 4:55:28going to paste
  5776. 4:55:32and save.
  5777. 4:55:35And we'll load this by opening chat
  5778. 4:55:37messages.jsx.
  5779. 4:55:39And at the top, we will import a string.
  5780. 4:55:45And we'll locate the file dot slash chat
  5781. 4:55:50messages.css
  5782. 4:55:53and save.
  5783. 4:55:56And finally, we'll go back to app.css
  5784. 4:56:00and make sure that we save all of our
  5785. 4:56:02files.
  5786. 4:56:05Now, let's open our website in the
  5787. 4:56:07browser.
  5788. 4:56:09And everything should look and work
  5789. 4:56:11correctly.
  5790. 4:56:13Let's go back to our code.
  5791. 4:56:16And that's it. We split up our
  5792. 4:56:18JavaScript and our CSS code into
  5793. 4:56:21separate files. And now each component
  5794. 4:56:25has its own JavaScript file and its own
  5795. 4:56:28CSS file. This makes our code a lot
  5796. 4:56:32easier to work with because each file is
  5797. 4:56:35focused on only one component and each
  5798. 4:56:38file contains less code.
  5799. 4:56:41In the next lesson, we'll use this new
  5800. 4:56:43React setup to work on a bigger React
  5801. 4:56:46project, which is an ecommerce website
  5802. 4:56:49that has multiple pages and more
  5803. 4:56:52advanced features. And that's the end of
  5804. 4:56:55this lesson. In this lesson, we created
  5805. 4:56:58a proper React setup using the command
  5806. 4:57:01line, npm, and vit. We reviewed the
  5807. 4:57:06command line, which lets us give
  5808. 4:57:08commands to our computer. We reviewed
  5809. 4:57:11npm which helps us download and use
  5810. 4:57:15external libraries or packages. We
  5811. 4:57:18learned the create vit package which
  5812. 4:57:21helps us create a proper React setup. We
  5813. 4:57:25moved our chatbot project into the new
  5814. 4:57:28React setup. We learned how to use
  5815. 4:57:31eslint which highlights problems in our
  5816. 4:57:34JavaScript code. We reviewed JavaScript
  5817. 4:57:37modules which lets us separate our code
  5818. 4:57:41into different files. And we separated
  5819. 4:57:44each component in the chatbot project
  5820. 4:57:47into its own JavaScript and CSS files.
  5821. 4:57:52Thanks for watching so far. If you find
  5822. 4:57:54this course valuable, you can support
  5823. 4:57:55the channel by liking this video,
  5824. 4:57:58leaving a comment, and subscribing.
  5825. 4:58:00You can also check out the premium
  5826. 4:58:02version of this course where you'll get
  5827. 4:58:04all the lessons ads free broken down
  5828. 4:58:07into small videos as well as additional
  5829. 4:58:10lessons and more projects that are not
  5830. 4:58:12in the public react course. Currently,
  5831. 4:58:15the premium course contains 11 lessons
  5832. 4:58:17and I'll be adding more lessons in the
  5833. 4:58:19future. You can also get a certificate
  5834. 4:58:22of completion, create comments or
  5835. 4:58:24questions on specific topics, get
  5836. 4:58:27progress tracking, search and course
  5837. 4:58:29notes. You can also check out the
  5838. 4:58:31membership where you can get this course
  5839. 4:58:34ads free separated by each lesson, AI
  5840. 4:58:37videos, backend tutorials, and more.
  5841. 4:58:40Thanks again, and let's continue with
  5842. 4:58:42the course. Here are some exercises to
  5843. 4:58:45help you practice what we learned in
  5844. 4:58:47this lesson.
  5845. 4:59:34In this lesson, we're going to start a
  5846. 4:59:36bigger project using React and we'll
  5847. 4:59:38learn an important feature called
  5848. 4:59:41routing.
  5849. 4:59:43By the way, if something in this lesson
  5850. 4:59:45does not work for you, you can check the
  5851. 4:59:47troubleshooting guide in the
  5852. 4:59:49description.
  5853. 4:59:51First, let's take a look at the project
  5854. 4:59:53we're going to build. In our browser,
  5855. 4:59:57we're going to create a new tab.
  5856. 5:00:00And at the top, we're going to go to
  5857. 5:00:03supersimple.dev/proscommerce.
  5858. 5:00:13And we can close these other tabs.
  5859. 5:00:18This is an e-commerce project or an
  5860. 5:00:20online shopping project. We can add
  5861. 5:00:23products to our cart.
  5862. 5:00:26And then we can click here to view our
  5863. 5:00:28cart.
  5864. 5:00:32And this page will display all the
  5865. 5:00:33products in our cart. It will calculate
  5866. 5:00:36the total for our order. and it will
  5867. 5:00:39also calculate a delivery date. And then
  5868. 5:00:42we can create an order.
  5869. 5:00:45And this page will list all of the
  5870. 5:00:47orders that we created. We can also
  5871. 5:00:50click this button to track the order. So
  5872. 5:00:53depending on the delivery date, it will
  5873. 5:00:55show you the progress of the delivery.
  5874. 5:00:58And we'll click here to go back to the
  5875. 5:01:00orders page.
  5876. 5:01:02So notice that this project has multiple
  5877. 5:01:05pages. So, it's much bigger than the
  5878. 5:01:07chatbot project. And we're working with
  5879. 5:01:10more data than before. So, we have
  5880. 5:01:13products, we have a cart, and we have
  5881. 5:01:16orders.
  5882. 5:01:18By the way, if you ever need to reset
  5883. 5:01:20the cart or the orders, you can click
  5884. 5:01:23this reset button here in the bottom
  5885. 5:01:25right, and it will reset everything back
  5886. 5:01:28to the default data. So, let's click the
  5887. 5:01:32top left to go back to the main page.
  5888. 5:01:35And now that we introduced the project,
  5889. 5:01:38we're going to build this with React.
  5890. 5:01:41We'll go back to VS Code
  5891. 5:01:43and we'll collapse the chatbot project
  5892. 5:01:46folder.
  5893. 5:01:48And before we create a new React
  5894. 5:01:50project, let's group these older files
  5895. 5:01:53into a folder. We'll click this HTML
  5896. 5:01:56file and we'll create a folder. And
  5897. 5:02:00let's name it React-basics.
  5898. 5:02:05and we're going to move all these files
  5899. 5:02:06into this folder. Now, a shortcut here
  5900. 5:02:09is to click this file and then press
  5901. 5:02:12shift on your keyboard and click the
  5902. 5:02:14bottom file and it will select all the
  5903. 5:02:17files between those two and we can drag
  5904. 5:02:20this into the folder
  5905. 5:02:25and we'll collapse it. So, that just
  5906. 5:02:28helps us stay a little more organized.
  5907. 5:02:31Next, we're going to set up a new React
  5908. 5:02:33project using VIT. To do that, we're
  5909. 5:02:37going to go to our terminal.
  5910. 5:02:40If you have the VIT server running, you
  5911. 5:02:42can close it by pressing Ctrl + C on
  5912. 5:02:44Windows or control C on Mac.
  5913. 5:02:48So, if you're continuing from the
  5914. 5:02:50previous lesson, the terminal should be
  5915. 5:02:52running inside the chatbot project
  5916. 5:02:54folder. To confirm that, we can run
  5917. 5:02:57print working directory or pwd.
  5918. 5:03:01And it will tell us that we're inside
  5919. 5:03:02the chatbot project folder. Now, to set
  5920. 5:03:05up a new React project, we want to be
  5921. 5:03:08inside the React-Course folder. So, to
  5922. 5:03:11get out of the chatbot project folder,
  5923. 5:03:14we can run the command cd dot dot.
  5924. 5:03:19And if we press pwd,
  5925. 5:03:22we should be inside the react-course
  5926. 5:03:24folder. Another way to do this is to
  5927. 5:03:27just start a new terminal.
  5928. 5:03:33Now we'll use vit again to set up a new
  5929. 5:03:37react project.
  5930. 5:03:39In the command line, we'll run the
  5931. 5:03:41command npx
  5932. 5:03:43create-ash
  5933. 5:03:45vit and we'll type at and give it a
  5934. 5:03:48version. So, you can find the version in
  5935. 5:03:50the description by clicking this link
  5936. 5:03:53and going to lesson six and you'll find
  5937. 5:03:55the version of vit that we're going to
  5938. 5:03:57use as well as the full command.
  5939. 5:04:00By the way, the version that you see in
  5940. 5:04:03this video might be different than the
  5941. 5:04:05version you see on your page. You should
  5942. 5:04:08always use the version that you see on
  5943. 5:04:10your page. I did this because it's hard
  5944. 5:04:13to update the video. So, this lets me
  5945. 5:04:16easily update the version in the future.
  5946. 5:04:20So, select the version on your page.
  5947. 5:04:23Copy.
  5948. 5:04:25So, I'm going to paste the version here
  5949. 5:04:28and press enter.
  5950. 5:04:32For the project name, let's name it
  5951. 5:04:34ecommerce-
  5952. 5:04:37project. Press enter.
  5953. 5:04:40And from this list, we'll press the down
  5954. 5:04:42arrow and select React.
  5955. 5:04:46And from this list, we'll press down and
  5956. 5:04:48select JavaScript.
  5957. 5:04:51And it will set up a new React project
  5958. 5:04:54inside our React-Course folder, which we
  5959. 5:04:57see right here. And now we need to go
  5960. 5:05:00into this folder using the command cd
  5961. 5:05:04ecommerce-
  5962. 5:05:06project.
  5963. 5:05:09And we're going to run npm install to
  5964. 5:05:11install all of the packages inside
  5965. 5:05:14package.json.
  5966. 5:05:16So we'll run npm install.
  5967. 5:05:24Once we have everything installed, we
  5968. 5:05:26can run this project using the command
  5969. 5:05:30npm rundev.
  5970. 5:05:35And now we can open this project using
  5971. 5:05:37this URL in the browser. So we'll select
  5972. 5:05:40this URL and copy.
  5973. 5:05:44And in the browser,
  5974. 5:05:46we're going to create a new tab.
  5975. 5:05:49And then here we'll paste.
  5976. 5:05:54And now we have a new React project.
  5977. 5:05:59Before we start updating the code, we're
  5978. 5:06:01actually going to review a tool called
  5979. 5:06:04Git. Git helps us track changes in our
  5980. 5:06:07code, and it's useful in bigger projects
  5981. 5:06:10because we're going to be making lots of
  5982. 5:06:12changes, and it's easy to forget what we
  5983. 5:06:15changed. So, Git will help us track our
  5984. 5:06:18changes.
  5985. 5:06:20We're going to go to our code
  5986. 5:06:23and we're going to open the Git section
  5987. 5:06:25here.
  5988. 5:06:27If you already have Git installed, it
  5989. 5:06:30should say initialize repository. If you
  5990. 5:06:32do not have Git installed, you can click
  5991. 5:06:35this link in the description, go to
  5992. 5:06:37lesson six, and follow these
  5993. 5:06:39instructions to install Git.
  5994. 5:06:43Once you have git installed, we'll click
  5995. 5:06:45initialize repository.
  5996. 5:06:48Now, a repository is just a folder where
  5997. 5:06:52git is tracking changes. At first, git
  5998. 5:06:56will tell us that all of these files or
  5999. 5:06:59everything inside our react-ourse folder
  6000. 5:07:02is new. And that's because a repository
  6001. 5:07:05starts out as empty. So, git considers
  6002. 5:07:08all of these files as new files.
  6003. 5:07:11We'll go back to the git section
  6004. 5:07:14and we'll scroll up and we're going to
  6005. 5:07:17save all of these files into git. At the
  6006. 5:07:20top, we'll type a message to describe
  6007. 5:07:23our changes. So, let's type the message
  6008. 5:07:26add the starting files and press commit.
  6009. 5:07:33Commit means we're going to save these
  6010. 5:07:34changes into git.
  6011. 5:07:38Now, you might run into an error when
  6012. 5:07:40using git and it tells you you have to
  6013. 5:07:42configure your username and email. To do
  6014. 5:07:45that, you can run these two commands in
  6015. 5:07:47the command line and choose your
  6016. 5:07:49username and choose the email you want
  6017. 5:07:51to use and try to commit again. And now
  6018. 5:07:55it should work.
  6019. 5:07:58If it asks to confirm, you can press
  6020. 5:08:01yes.
  6021. 5:08:04And now git will start tracking any new
  6022. 5:08:06changes that we make. For example, if we
  6023. 5:08:10go into our code and open the e-commerce
  6024. 5:08:13project and change some of these files,
  6025. 5:08:18we type test and save.
  6026. 5:08:21And in the source app.jsx, we type test
  6027. 5:08:26and save.
  6028. 5:08:29And then we can go to the git section.
  6029. 5:08:32And now git will tell us everything that
  6030. 5:08:34we changed in these two files. So as you
  6031. 5:08:38can see git helps us track changes in
  6032. 5:08:41our code.
  6033. 5:08:43Also git lets us undo our changes. So to
  6034. 5:08:47undo this change we can click this arrow
  6035. 5:08:50button here.
  6036. 5:08:53And to undo all of our changes at the
  6037. 5:08:55top we can click this arrow button here.
  6038. 5:09:02So, that's a review of how Git works. It
  6039. 5:09:05can track our changes and it lets us
  6040. 5:09:07easily undo our changes. Let's go back
  6041. 5:09:10to our files
  6042. 5:09:13and remember that in our React setup, we
  6043. 5:09:16have a file called.git
  6044. 5:09:18ignore. So, now we can learn what this
  6045. 5:09:21file does.
  6046. 5:09:23Git ignore tells git which files and
  6047. 5:09:26folders to ignore or not to track
  6048. 5:09:29changes. For example, in this file we
  6049. 5:09:33can see the folder node modules. So this
  6050. 5:09:36tells git that if there are any changes
  6051. 5:09:38inside node modules it will not be
  6052. 5:09:41tracked. The reason we ignore node
  6053. 5:09:44modules is because this folder is very
  6054. 5:09:46large and none of the changes inside
  6055. 5:09:49this folder are changes that we make.
  6056. 5:09:53Instead, we generate this folder using a
  6057. 5:09:56package.json and npm install. So,
  6058. 5:10:00because this folder is generated, it
  6059. 5:10:03doesn't make a lot of sense to save this
  6060. 5:10:05in git. So, that's a review of Git. Git
  6061. 5:10:09helps us track changes in our code and
  6062. 5:10:12doggit ignore tells Git which files and
  6063. 5:10:15folders to ignore or to not track
  6064. 5:10:18changes.
  6065. 5:10:21Now we're ready to start the project.
  6066. 5:10:24First, we're actually going to download
  6067. 5:10:26the HTML and CSS for this project. We'll
  6068. 5:10:30click this link in the description and
  6069. 5:10:33then click here.
  6070. 5:10:36And this will open GitHub.
  6071. 5:10:40And this contains all the HTML and CSS
  6072. 5:10:45for the e-commerce project. The reason
  6073. 5:10:47we're going to use some starting code is
  6074. 5:10:50because this course is focused on React.
  6075. 5:10:53By using this code, we can focus on
  6076. 5:10:56learning React features rather than
  6077. 5:10:58spending our time writing HTML and CSS.
  6078. 5:11:01If you want to practice HTML and CSS,
  6079. 5:11:04you can try to recreate the design on
  6080. 5:11:06your own after this course.
  6081. 5:11:09Another reason we're using some starting
  6082. 5:11:11code is in the real world, we often work
  6083. 5:11:14on an existing project rather than a new
  6084. 5:11:17project. So this will give us some
  6085. 5:11:19practice working on an existing project.
  6086. 5:11:24So to download the starting code, we're
  6087. 5:11:26going to click this code button here and
  6088. 5:11:29we'll click download zip
  6089. 5:11:34and we'll open our downloads folder.
  6090. 5:11:38For me, it's here. And we're going to
  6091. 5:11:42unzip this file. On Windows, we're going
  6092. 5:11:44to rightclick and click extract all
  6093. 5:11:49and extract.
  6094. 5:11:51And on Mac, you can just doubleclick the
  6095. 5:11:53zip file.
  6096. 5:11:56And then we're going to open this
  6097. 5:11:58folder.
  6098. 5:12:00And we're going to move all of these
  6099. 5:12:01files and folders into VS Code. First,
  6100. 5:12:05let's go into VS Code.
  6101. 5:12:08And we're going to organize all of those
  6102. 5:12:10files into a new folder. So, we'll click
  6103. 5:12:13the e-commerce project and create a new
  6104. 5:12:16folder.
  6105. 5:12:18And we'll name this starting dashcode.
  6106. 5:12:24And we'll open our downloads folder
  6107. 5:12:26again.
  6108. 5:12:28And we're going to move all of these
  6109. 5:12:29files into this new folder. So we'll
  6110. 5:12:32select it. And we're just going to drag
  6111. 5:12:35it into starting code.
  6112. 5:12:40And let's save these changes into Git.
  6113. 5:12:43So then Git will start tracking any new
  6114. 5:12:46changes that we make.
  6115. 5:12:48We'll open the git section and here we
  6116. 5:12:51added all of the starting code and at
  6117. 5:12:54the top let's type the message add the
  6118. 5:12:58starting
  6119. 5:13:00HTML and CSS code and commit.
  6120. 5:13:10Let's go back to our files
  6121. 5:13:14and we're going to go to the starting
  6122. 5:13:15code and look for index.html
  6123. 5:13:20and we're going to rightclick this file
  6124. 5:13:23and we're going to open with live
  6125. 5:13:25server.
  6126. 5:13:27And you'll see we have all the HTML and
  6127. 5:13:30CSS for this project. So now we're going
  6128. 5:13:34to create this using React. We'll start
  6129. 5:13:37by creating this main page. We usually
  6130. 5:13:41call this the homepage.
  6131. 5:13:43By the way, a page or a web page is a
  6132. 5:13:47single page like the homepage or the
  6133. 5:13:50cart page. A website is a group of web
  6134. 5:13:54pages.
  6135. 5:13:56To create this homepage, we're going to
  6136. 5:13:58move all of the HTML and CSS into React.
  6137. 5:14:03We'll go back to VS Code
  6138. 5:14:06and we'll open the starting code
  6139. 5:14:09index.html.
  6140. 5:14:12And this file contains the HTML for the
  6141. 5:14:16homepage. And we're going to move this
  6142. 5:14:18HTML into the index.html
  6143. 5:14:21here for the e-commerce project. So,
  6144. 5:14:25let's actually drag this index.html HTML
  6145. 5:14:28and we'll open it at the bottom
  6146. 5:14:31so we can see these files side by side
  6147. 5:14:35and we'll move the HTML in the starting
  6148. 5:14:38code into our project one by one.
  6149. 5:14:42We already have the doc type HTML inside
  6150. 5:14:45here. So we don't need to move it. And
  6151. 5:14:47we also have the HTML and head elements.
  6152. 5:14:52The first thing that we do need to move
  6153. 5:14:54is this title. So we'll select this
  6154. 5:14:57title and then cut.
  6155. 5:15:00And inside the projects HTML file, let's
  6156. 5:15:03replace the title here.
  6157. 5:15:07And now we'll keep going one by one.
  6158. 5:15:11Next, this meta viewport element makes
  6159. 5:15:14the website look good on smaller
  6160. 5:15:16screens. Now, we already have this
  6161. 5:15:19inside our React setup, so we do not
  6162. 5:15:22need to move it over. Let's actually
  6163. 5:15:25select this code and delete it so that
  6164. 5:15:29we can keep track of our progress.
  6165. 5:15:32Next, this code loads a font or a style
  6166. 5:15:36of text called rooto.
  6167. 5:15:39We're going to use this font in the
  6168. 5:15:41e-commerce project. So, we're going to
  6169. 5:15:43move it over. Let's select this code
  6170. 5:15:47and cut.
  6171. 5:15:50And inside our e-commerce project, we're
  6172. 5:15:53going to paste.
  6173. 5:15:55And if you need to, you can fix this
  6174. 5:15:57spacing by selecting these lines and
  6175. 5:16:00press shift tab a few times.
  6176. 5:16:04Next, these lines of code load the CSS
  6177. 5:16:08for our project. Now, remember, when
  6178. 5:16:11using VIT, we actually import our CSS
  6179. 5:16:14files.
  6180. 5:16:16So, we're not going to move these over
  6181. 5:16:18for now, but we'll keep these for later.
  6182. 5:16:21Finally, if we scroll down, the HTML
  6183. 5:16:25inside the body creates the contents of
  6184. 5:16:28the page. Now, the contents of the page
  6185. 5:16:32should be created by React.
  6186. 5:16:35So, let's move this into a React
  6187. 5:16:37component.
  6188. 5:16:39First, we'll create a component for the
  6189. 5:16:41homepage. So, we'll scroll up to the
  6190. 5:16:45source folder and we'll click this
  6191. 5:16:48folder and we'll create a new file.
  6192. 5:16:53Usually, we create a component for each
  6193. 5:16:55page of the website. So, let's name this
  6194. 5:16:58file capital H home capital Page.jsx.
  6195. 5:17:07Now, this website has multiple pages.
  6196. 5:17:10So, let's create a folder to group all
  6197. 5:17:13the pages together. We'll create a
  6198. 5:17:16folder and we'll name it pages.
  6199. 5:17:21And we're going to move the homepage
  6200. 5:17:23into the pages folder.
  6201. 5:17:28And now let's create a component for the
  6202. 5:17:30homepage using function. and we'll name
  6203. 5:17:35it capital H home capital P page
  6204. 5:17:39brackets and curly brackets and we're
  6205. 5:17:42going to return brackets
  6206. 5:17:46we're going to return the HTML for the
  6207. 5:17:48homepage which is right here. So we're
  6208. 5:17:52going to copy all of this HTML into our
  6209. 5:17:55new component. So, one trick we can do
  6210. 5:17:58is on the left, we're going to click
  6211. 5:18:00this arrow to collapse this element. And
  6212. 5:18:03we're going to click this arrow to
  6213. 5:18:05collapse this element as well. And now
  6214. 5:18:08we can select all of this HTML code
  6215. 5:18:12and cut.
  6216. 5:18:15And inside homepage.jsx,
  6217. 5:18:18we're going to paste that code.
  6218. 5:18:21And now we're going to scroll up and fix
  6219. 5:18:24all of these errors.
  6220. 5:18:27First, if we collapse this code
  6221. 5:18:31and collapse this element,
  6222. 5:18:33you can see we're returning two elements
  6223. 5:18:35from this component.
  6224. 5:18:37But remember, we can only return one
  6225. 5:18:40element from a component. To fix this,
  6226. 5:18:43we can wrap this in a fragment. So, at
  6227. 5:18:47the top, we'll create a fragment using
  6228. 5:18:50less than, greater than, and delete this
  6229. 5:18:53closing tag. And at the bottom, we'll
  6230. 5:18:57create the closing tag less than
  6231. 5:18:59slashgreater than. And let's also fix
  6232. 5:19:03the indenting here. We can select all of
  6233. 5:19:06this code and press tab a few times
  6234. 5:19:10until there's an indent before the
  6235. 5:19:12fragment. And for the first line, we're
  6236. 5:19:14going to remove an indent. And that will
  6237. 5:19:17fix the indenting.
  6238. 5:19:20Next, we'll fix these errors. We'll
  6239. 5:19:24expand these elements.
  6240. 5:19:26And remember in React, we have to use
  6241. 5:19:29class name instead of class. So to fix
  6242. 5:19:33these errors, we need to change all of
  6243. 5:19:35these to use class name. To do that, we
  6244. 5:19:38can press CtrlF on Windows or command F
  6245. 5:19:41on Mac to find some code. And we're
  6246. 5:19:45going to search for the code class
  6247. 5:19:48equals. And then we're going to click
  6248. 5:19:50this arrow on the left. And this will
  6249. 5:19:53open the replace menu. So this lets us
  6250. 5:19:56replace class equals with something
  6251. 5:19:58else. We'll replace it with class
  6252. 5:20:02capital N name equals and then press
  6253. 5:20:06this button to replace all.
  6254. 5:20:10And now you'll see that we fixed all of
  6255. 5:20:12the ESLint errors. Finally, we're going
  6256. 5:20:16to scroll to the top and we need to
  6257. 5:20:19export this component so we can use it
  6258. 5:20:21in other files. At the front, we'll type
  6259. 5:20:25export and save.
  6260. 5:20:29So, we moved all the HTML from the
  6261. 5:20:31homepage into our component and we fixed
  6262. 5:20:34all these errors. Now, let's use this
  6263. 5:20:37component in our app. We're going to go
  6264. 5:20:40to app.jsx JSX
  6265. 5:20:44and we'll remove this default HTML.
  6266. 5:20:48So we'll select it
  6267. 5:20:51and delete it
  6268. 5:20:53and we'll insert our homepage component.
  6269. 5:20:57First we need to import it.
  6270. 5:21:01at the top. We'll import curly brackets
  6271. 5:21:06the home page
  6272. 5:21:09from a string and we'll locate this
  6273. 5:21:13file. So, we're going to type dot slash
  6274. 5:21:17to start in the current folder which is
  6275. 5:21:19the source folder and we'll go into
  6276. 5:21:22pages
  6277. 5:21:23slashhome
  6278. 5:21:25page.
  6279. 5:21:28And now we'll insert the homepage into
  6280. 5:21:30our app. So here we'll type less than
  6281. 5:21:34homepage
  6282. 5:21:36slashgreater than. And next we'll fix
  6283. 5:21:39these eslint errors. The react logo and
  6284. 5:21:43vit logo are not used anymore. So we can
  6285. 5:21:46remove them.
  6286. 5:21:49Count and set count are also not used
  6287. 5:21:52anymore. So we can remove them.
  6288. 5:21:57and use state is not used anymore. We
  6289. 5:21:59can remove this as well
  6290. 5:22:02and save.
  6291. 5:22:05And now let's make sure that we saved
  6292. 5:22:07all of our other files. So we'll open
  6293. 5:22:10index.html
  6294. 5:22:12and we'll save it.
  6295. 5:22:14And let's open the starting code
  6296. 5:22:17index.html
  6297. 5:22:18and we'll save this file.
  6298. 5:22:22And now let's take a look at our
  6299. 5:22:23website. We'll open the browser.
  6300. 5:22:28And we can close GitHub for now. And
  6301. 5:22:30we'll open the e-commerce project.
  6302. 5:22:34And you'll see that we have the HTML and
  6303. 5:22:36the text for the e-commerce project
  6304. 5:22:39homepage.
  6305. 5:22:40Now, obviously, this doesn't look very
  6306. 5:22:42good. So, let's move over the CSS. Next,
  6307. 5:22:48to move the CSS into our React project,
  6308. 5:22:51we're going to go back to our code
  6309. 5:22:53and we're going to open the starting
  6310. 5:22:55code index.html.
  6311. 5:22:58And here we're going to scroll up and we
  6312. 5:23:02load three CSS files. So, we're going to
  6313. 5:23:05move these three files into our React
  6314. 5:23:08project. We'll start with general.css.
  6315. 5:23:13If we open styles/shared
  6316. 5:23:16which is here and shared and open
  6317. 5:23:19general.css
  6318. 5:23:23and this file contains CSS for the
  6319. 5:23:26overall website. CSS for the overall
  6320. 5:23:29website goes into the index.css file in
  6321. 5:23:34React.
  6322. 5:23:35So we'll click general.css CSS and we'll
  6323. 5:23:39press control A on Windows or command A
  6324. 5:23:41on Mac to select all of this code and
  6325. 5:23:45we'll copy
  6326. 5:23:49and index.css in the e-commerce project
  6327. 5:23:53we're going to remove all of this
  6328. 5:23:55default code
  6329. 5:23:57and paste
  6330. 5:24:00and save.
  6331. 5:24:03And now if we go back to the starting
  6332. 5:24:05code, we've moved over general.css and
  6333. 5:24:09we can delete this file.
  6334. 5:24:13And now we'll go back to the starting
  6335. 5:24:14code index.html.
  6336. 5:24:19And we can remove this line.
  6337. 5:24:22And we'll move header.css and index.css
  6338. 5:24:26into our React project. So both of these
  6339. 5:24:30style the homepage. So, we'll just move
  6340. 5:24:32these files beside our homepage
  6341. 5:24:34component.
  6342. 5:24:36First, we'll go into styles, shared, and
  6343. 5:24:39header.css,
  6344. 5:24:41and we're going to drag this file
  6345. 5:24:44into our pages folder.
  6346. 5:24:49So, we keep our CSS beside the component
  6347. 5:24:52that it styles.
  6348. 5:24:54And we'll scroll down and find
  6349. 5:24:57styles/pages.
  6350. 5:24:59And we'll move index.css CSS beside the
  6351. 5:25:02homepage as well.
  6352. 5:25:08So header.css styles the header at the
  6353. 5:25:11top of the page and index.css
  6354. 5:25:15styles the main part of the homepage.
  6355. 5:25:18Now a best practice is to use the same
  6356. 5:25:20name as the component here. So we'll
  6357. 5:25:23rightclick index.css CSS and we'll
  6358. 5:25:26rename it
  6359. 5:25:28capital H home capital P page.
  6360. 5:25:35Next, to load these styles, we need to
  6361. 5:25:38import them. We'll open homepage.jsx
  6362. 5:25:42and at the top,
  6363. 5:25:45we're going to import
  6364. 5:25:47a string and we'll import the file dot /
  6365. 5:25:52header.css. CSS
  6366. 5:25:55and we'll also import a string
  6367. 5:26:00slashhome
  6368. 5:26:01page.css
  6369. 5:26:04and save.
  6370. 5:26:07And now if we go back to the starting
  6371. 5:26:10code index.html,
  6372. 5:26:13we moved all of the HTML and CSS into
  6373. 5:26:16our project. So now we can delete this
  6374. 5:26:19file.
  6375. 5:26:23Finally, we'll open app.css.
  6376. 5:26:27And these styles were for the default
  6377. 5:26:29React website. So we don't need these
  6378. 5:26:32anymore. So we can select all of the CSS
  6379. 5:26:35and just remove it and save.
  6380. 5:26:40So make sure that we saved all of our
  6381. 5:26:42files. And now we're going to view the
  6382. 5:26:45website.
  6383. 5:26:48And now it's looking really good. We
  6384. 5:26:50have the structure and the styling that
  6385. 5:26:53we need. The only thing that's missing
  6386. 5:26:55are the images. So let's move the images
  6387. 5:26:59next.
  6388. 5:27:01To move the images, we'll go back to VS
  6389. 5:27:03Code and we'll go to the starting code
  6390. 5:27:08and find the images folder. So this
  6391. 5:27:11contains all the images that we need.
  6392. 5:27:14So, we're going to move this into the
  6393. 5:27:15React project.
  6394. 5:27:18Now, usually we put images in the assets
  6395. 5:27:21folder and then we import the images.
  6396. 5:27:24However, to save time, we're going to
  6397. 5:27:26move this folder into the public folder.
  6398. 5:27:30So, we're going to move it up here and
  6399. 5:27:33into the public folder.
  6400. 5:27:38The reason we move it into the public
  6401. 5:27:40folder is because if we look at
  6402. 5:27:42homepage.js, jsx and we look at an
  6403. 5:27:45image. Notice the source attribute
  6404. 5:27:48starts with images slash. When using
  6405. 5:27:51vit, if we start with a folder name like
  6406. 5:27:54this, vit will look for this inside the
  6407. 5:27:57public folder. So, it finds it here. So,
  6408. 5:28:00by moving our images into the public
  6409. 5:28:02folder, we don't have to change any of
  6410. 5:28:05this code and it just saves time in the
  6411. 5:28:07video. We can move these images into the
  6412. 5:28:10source/assets folder and import them
  6413. 5:28:14later as an exercise.
  6414. 5:28:16So now let's open our website
  6415. 5:28:20and all of the images are loaded. So
  6416. 5:28:23that's how we move an HTML website into
  6417. 5:28:26a React website. We just move over the
  6418. 5:28:30HTML, CSS, and images one by one.
  6419. 5:28:34Let's go back to VS Code and we'll save
  6420. 5:28:37our changes into Git. We'll open the Git
  6421. 5:28:40section. And here we moved all the HTML,
  6422. 5:28:44CSS, and images for the homepage. We're
  6423. 5:28:48going to type a message at the top. Move
  6424. 5:28:51the home page into React and press
  6425. 5:28:56commit.
  6426. 5:29:02Next, we're going to move the checkout
  6427. 5:29:04page to the React setup. We'll go to our
  6428. 5:29:07browser
  6429. 5:29:09and we're going to open the final
  6430. 5:29:10project at superimple.dev
  6431. 5:29:14and we're going to click the cart icon
  6432. 5:29:16in the top right.
  6433. 5:29:19This will open the checkout page where
  6434. 5:29:21we can see our cart and we can place an
  6435. 5:29:24order. So notice that this is a separate
  6436. 5:29:27page from the homepage and this page is
  6437. 5:29:30also on a different URL/checkout.
  6438. 5:29:34So how do we create multiple pages in
  6439. 5:29:37React? To do that, we're going to learn
  6440. 5:29:40a feature called routing. Routing lets
  6441. 5:29:43us create multiple pages in React.
  6442. 5:29:48Let's go back to our code
  6443. 5:29:51and we'll open our files here.
  6444. 5:29:54and we're going to scroll down first to
  6445. 5:29:56the starting code folder.
  6446. 5:29:59So, normally to create multiple pages,
  6447. 5:30:02we need to create multiple HTML files
  6448. 5:30:05like here. Each page has its own HTML
  6449. 5:30:09file like the checkout page and the
  6450. 5:30:12orders page. However, inside these
  6451. 5:30:15files, notice that there's a lot of
  6452. 5:30:18repeated code. So, each file has the doc
  6453. 5:30:21type. it has some meta and link elements
  6454. 5:30:25and in the orders.html
  6455. 5:30:27it has the same meta and link elements.
  6456. 5:30:30So this is not great because if we want
  6457. 5:30:33to change something on one page we have
  6458. 5:30:36to change all of the pages. On the other
  6459. 5:30:39hand, routing lets us create multiple
  6460. 5:30:42pages using one HTML file, and this lets
  6461. 5:30:46us reuse our HTML code. To use routing,
  6462. 5:30:51we're going to install an MPM package
  6463. 5:30:54called React Router. So, let's reopen
  6464. 5:30:57our terminal. You can click here and
  6465. 5:31:01drag up or press control J on Windows or
  6466. 5:31:05command J on Mac to open this terminal.
  6467. 5:31:10Now we currently have the Vit server
  6468. 5:31:12running. So we'll press control C on
  6469. 5:31:15Windows and Mac to stop the V server.
  6470. 5:31:19And to install React Router, we're going
  6471. 5:31:22to run the command npm install
  6472. 5:31:26react-rowout.
  6473. 5:31:29And we'll also type at. So for this
  6474. 5:31:32video, we'll use a specific version of
  6475. 5:31:35React Router just to make sure that it
  6476. 5:31:38works with the video. So click this link
  6477. 5:31:40in the description, go to lesson six,
  6478. 5:31:43and here you'll find the version of
  6479. 5:31:45React Router that we'll use, and you'll
  6480. 5:31:47also find the full command. So again,
  6481. 5:31:50you should always use the version that
  6482. 5:31:52you see on your page.
  6483. 5:31:55So select the version on your page and
  6484. 5:31:58copy it. And then in our command line,
  6485. 5:32:00we're going to paste that version
  6486. 5:32:03and press enter to install.
  6487. 5:32:08So now, how do we use React Router? Like
  6488. 5:32:11most packages, React Router has a
  6489. 5:32:14documentation page that tells you how to
  6490. 5:32:16use it. You can find the documentation
  6491. 5:32:18in the description, but for the video,
  6492. 5:32:21I'm going to show you the parts of the
  6493. 5:32:22documentation that we need. According to
  6494. 5:32:25the documentation, to use React Router,
  6495. 5:32:28we need to load it when we render the
  6496. 5:32:31app component.
  6497. 5:32:33So, in our project, we're going to
  6498. 5:32:35scroll up and open main.jsx,
  6499. 5:32:42and I'll close these other files so we
  6500. 5:32:44can see more of the code.
  6501. 5:32:47So, in this file, we set up React. We
  6502. 5:32:51render our app component on the page.
  6503. 5:32:55According to the documentation, we need
  6504. 5:32:57to wrap this in a component called
  6505. 5:33:00browser router.
  6506. 5:33:02So at the top first we're going to
  6507. 5:33:06import curly brackets
  6508. 5:33:09the browser
  6509. 5:33:11router
  6510. 5:33:14from a string
  6511. 5:33:16and we'll import it from the package
  6512. 5:33:18react router.
  6513. 5:33:21As a review, if we just start with a
  6514. 5:33:23name like this without dot slash, this
  6515. 5:33:27means it's going to load it from node
  6516. 5:33:29modules from the React router package.
  6517. 5:33:32And now around the app, we're going to
  6518. 5:33:35create less than browser
  6519. 5:33:39router greater than.
  6520. 5:33:42And we're going to put the app inside
  6521. 5:33:44here. So we'll cut this
  6522. 5:33:47and paste it in here.
  6523. 5:33:51Let's save.
  6524. 5:33:55Next, we need to do a bit more setup.
  6525. 5:33:58According to the documentation, inside
  6526. 5:34:00our app component, we need to add a
  6527. 5:34:03component called routes.
  6528. 5:34:06So, let's open app.jsx.
  6529. 5:34:09And at the top,
  6530. 5:34:12we're going to import curly brackets
  6531. 5:34:16the component routes
  6532. 5:34:20from a string. And we'll import this
  6533. 5:34:23from the package react-outer.
  6534. 5:34:28And inside this component, we're going
  6535. 5:34:30to insert the routes component using
  6536. 5:34:33less than routes greater than.
  6537. 5:34:38So the routes component tells React all
  6538. 5:34:41the pages that are in our website. So to
  6539. 5:34:44add a page to our website, we're going
  6540. 5:34:47to use another component called route. A
  6541. 5:34:50route is basically a page.
  6542. 5:34:54So first we need to import that at the
  6543. 5:34:57top. We're already importing from React
  6544. 5:34:59Router. So we're going to add a comma
  6545. 5:35:02and import the route component.
  6546. 5:35:06So inside routes, we're going to add
  6547. 5:35:08less than route greater than. So again,
  6548. 5:35:13a route basically adds a page to our
  6549. 5:35:17website.
  6550. 5:35:18And we're going to give this route two
  6551. 5:35:20props. The first prop is called path
  6552. 5:35:24equals double quotes. This tells React
  6553. 5:35:27the URL path of this page.
  6554. 5:35:30The URL path is the last part of the URL
  6555. 5:35:34like /checkout or slash orders. So first
  6556. 5:35:38let's add the homepage. For the
  6557. 5:35:41homepage, the URL path is empty.
  6558. 5:35:45To set the URL path as empty, we're
  6559. 5:35:48going to set it to slash.
  6560. 5:35:51And then we're going to give route a
  6561. 5:35:52second prop called element equals curly
  6562. 5:35:57brackets. So this tells React which
  6563. 5:36:00element or component to display when we
  6564. 5:36:04go to this URL path for the homepage. We
  6565. 5:36:07want to display this homepage component.
  6566. 5:36:10So let's select it and cut.
  6567. 5:36:15And inside here we're going to paste.
  6568. 5:36:20So this tells React that when we go to
  6569. 5:36:22the URL path slash or empty, it's going
  6570. 5:36:26to display our homepage. So let's try it
  6571. 5:36:30out. We'll save this file
  6572. 5:36:33and then inside the terminal, we're
  6573. 5:36:35going to start the VIT server using npm
  6574. 5:36:39rundev.
  6575. 5:36:42And we'll visit this URL.
  6576. 5:36:44I already have it open. So I'll open my
  6577. 5:36:47browser.
  6578. 5:36:49And now if the URL path ends with slash
  6579. 5:36:52or empty, it's going to display the
  6580. 5:36:55homepage.
  6581. 5:36:58Next, let's use React Router to add a
  6582. 5:37:01checkout page. We'll go back to our code
  6583. 5:37:05and to add another page to our website,
  6584. 5:37:08we're going to use the route component
  6585. 5:37:10again. So, we'll create less than route
  6586. 5:37:14greater than and we'll give it two
  6587. 5:37:16props. the path equals double quotes.
  6588. 5:37:20And for the checkout page, we'll set the
  6589. 5:37:23URL path to checkout.
  6590. 5:37:26This means this page will be displayed
  6591. 5:37:28when we go to /checkout.
  6592. 5:37:32And we'll give it another prop element
  6593. 5:37:35equals curly brackets.
  6594. 5:37:38And right now, we didn't create a
  6595. 5:37:40component for the checkout page yet. So
  6596. 5:37:43let's just create a div here just to
  6597. 5:37:45make sure that this is working. So we'll
  6598. 5:37:48create a div element and inside let's
  6599. 5:37:51type test checkout page and save.
  6600. 5:37:58So now if we go to our project
  6601. 5:38:02and in the URL path we're going to go to
  6602. 5:38:05slash checkout
  6603. 5:38:07and press enter.
  6604. 5:38:10It displays the div with the text that
  6605. 5:38:12we created.
  6606. 5:38:14So, as you can see, routing is actually
  6607. 5:38:17pretty simple. We just tell React which
  6608. 5:38:20pages we want in our website and then
  6609. 5:38:23what to display for each page.
  6610. 5:38:28Now, let's learn some more details about
  6611. 5:38:30routing. If we look at our code, we now
  6612. 5:38:33have multiple pages in our website. And
  6613. 5:38:37all of these pages use one HTML file
  6614. 5:38:41which is index.html.
  6615. 5:38:44So this HTML is shared between all of
  6616. 5:38:47our pages and this reduces repetition.
  6617. 5:38:51By the way, this is called a single page
  6618. 5:38:55application or an SPA. This means we
  6619. 5:38:59only have one HTML file and instead we
  6620. 5:39:03use React to create multiple pages.
  6621. 5:39:07Next, we'll use some shortcuts with
  6622. 5:39:09React Router. The first shortcut is
  6623. 5:39:13instead of path equals slash, React
  6624. 5:39:16Router has a prop for this called index.
  6625. 5:39:21So, this does the same thing as path
  6626. 5:39:23equals slash. The second shortcut is
  6627. 5:39:26currently we don't have anything inside
  6628. 5:39:29these route components. So, we can use
  6629. 5:39:32the self-closing element shortcut. We'll
  6630. 5:39:35remove the closing tag.
  6631. 5:39:38And at the end here, we'll add a space
  6632. 5:39:40and a slash. And same thing here, we'll
  6633. 5:39:43remove the closing tag.
  6634. 5:39:46And add a space and slash and save.
  6635. 5:39:51And now we'll save these changes into
  6636. 5:39:54Git. We'll open the git section.
  6637. 5:39:58And here it tells us we installed React
  6638. 5:40:01Router.
  6639. 5:40:02And in app.jsx jsx. We added some routes
  6640. 5:40:06or some pages to our website. In
  6641. 5:40:09main.jsx,
  6642. 5:40:11we loaded the React router. So, let's
  6643. 5:40:14create a message to describe these
  6644. 5:40:15changes. Create multiple
  6645. 5:40:20pages using React-Router
  6646. 5:40:25and commit.
  6647. 5:40:33Now, let's move the checkout page into
  6648. 5:40:35our app. We'll go back to our files
  6649. 5:40:39and first we'll open app.jsx.
  6650. 5:40:44And instead of displaying a div when we
  6651. 5:40:46go to / checkout, let's remove it.
  6652. 5:40:50And let's create a component for the
  6653. 5:40:52checkout page. Just like the homepage in
  6654. 5:40:56the pages folder, we'll click this and
  6655. 5:40:59we'll create a new file and we'll name
  6656. 5:41:02this capital C checkout capital P
  6657. 5:41:06page.jsx.
  6658. 5:41:11And inside here, we'll create a new
  6659. 5:41:13component using function
  6660. 5:41:16checkout
  6661. 5:41:18page brackets and curly brackets. and
  6662. 5:41:22we'll return brackets
  6663. 5:41:26and we'll move the HTML for the checkout
  6664. 5:41:28page in here. So, we'll go to the
  6665. 5:41:31starting code folder
  6666. 5:41:34and we'll open checkout.html.
  6667. 5:41:38So, this is the HTML file for the
  6668. 5:41:40checkout page.
  6669. 5:41:42We're going to scroll down to the body
  6670. 5:41:44element first and we're going to move
  6671. 5:41:46over this HTML.
  6672. 5:41:49So we'll click here to collapse this
  6673. 5:41:51code and click here to collapse this
  6674. 5:41:53code as well and we'll select all this
  6675. 5:41:56HTML and we'll cut
  6676. 5:42:01and then inside checkout page inside the
  6677. 5:42:04return we're going to paste
  6678. 5:42:08and let's scroll up and we'll fix these
  6679. 5:42:11errors.
  6680. 5:42:14So first if we collapse these two
  6681. 5:42:16elements remember we're returning two
  6682. 5:42:18elements from this component which
  6683. 5:42:20causes an error. To fix this we'll wrap
  6684. 5:42:23this in a fragment
  6685. 5:42:30and we'll fix the indenting. Now you can
  6686. 5:42:32fix it manually like we did before but
  6687. 5:42:35you can also use this shortcut. We can
  6688. 5:42:38press controlshiftp on Windows or
  6689. 5:42:41commandshiftp on Mac and this will open
  6690. 5:42:44up a list of commands that we can use in
  6691. 5:42:47VS Code. So we're going to search for
  6692. 5:42:49the command format document
  6693. 5:42:54and we can click it or press enter
  6694. 5:42:57and it will automatically format the
  6695. 5:42:59code in this document and fix the
  6696. 5:43:01indenting.
  6697. 5:43:03Next, if we expand these elements,
  6698. 5:43:06remember in React, we have to use class
  6699. 5:43:09name instead of class. To replace these,
  6700. 5:43:12we'll press control F or command F to
  6701. 5:43:15find code. And we'll find the code class
  6702. 5:43:19equals and click this arrow. And we're
  6703. 5:43:22going to replace it with class name
  6704. 5:43:25equals and click replace all.
  6705. 5:43:30And finally the last eslint error here
  6706. 5:43:33is this component is not used in this
  6707. 5:43:35file. So we need to export it
  6708. 5:43:39and save.
  6709. 5:43:41And now let's use this component in our
  6710. 5:43:44route. We'll open app.jsx JSX
  6711. 5:43:48and at the top we'll import the page
  6712. 5:43:50using import curly brackets the checkout
  6713. 5:43:56page
  6714. 5:43:58from a string and we'll import dot slash
  6715. 5:44:02pages slashcheckout page
  6716. 5:44:07and we'll use it inside this route using
  6717. 5:44:11less than checkout page slashgreater
  6718. 5:44:16and save.
  6719. 5:44:19And now let's check our website.
  6720. 5:44:22And if we go to the path /checkout, you
  6721. 5:44:25can see we have the HTML for the
  6722. 5:44:28checkout page.
  6723. 5:44:31Now, let's add the rest of the code for
  6724. 5:44:33the checkout page. We'll go back to VS
  6725. 5:44:36Code
  6726. 5:44:37and we're going to go to the starting
  6727. 5:44:39code and open checkout.html.
  6728. 5:44:44and we're going to move this over one by
  6729. 5:44:46one. So, first, what do we do for this
  6730. 5:44:49title? Right now, all of our pages are
  6731. 5:44:52sharing one HTML file here. So, all of
  6732. 5:44:56the pages have the same title,
  6733. 5:44:58e-commerce project. However, we want
  6734. 5:45:01each page to have a different title. So,
  6735. 5:45:04how do we do that? Well, in React, it's
  6736. 5:45:07actually pretty simple. We just have to
  6737. 5:45:09take this title and move it into our
  6738. 5:45:11page component. So, we'll select this
  6739. 5:45:15and cut.
  6740. 5:45:17And then we'll scroll up and open
  6741. 5:45:19checkout page.jsx.
  6742. 5:45:22And if we want this page to have a
  6743. 5:45:24different title, all we have to do is to
  6744. 5:45:27paste the title at the top
  6745. 5:45:31and save.
  6746. 5:45:33So, if we look at our website
  6747. 5:45:36now, the checkout page has the title
  6748. 5:45:38checkout in the tabs. So, we'll go back
  6749. 5:45:41to our code.
  6750. 5:45:43And we're also going to set the title in
  6751. 5:45:45the homepage. So we'll open the
  6752. 5:45:47homepage.
  6753. 5:45:49And in the homepage,
  6754. 5:45:52let's create a title.
  6755. 5:45:55And we're going to set the title to
  6756. 5:45:58ecommerce
  6757. 5:46:00project and save.
  6758. 5:46:04And now we can scroll down
  6759. 5:46:07to the index.html HTML
  6760. 5:46:10and we can remove this title because
  6761. 5:46:12we're going to set it using React
  6762. 5:46:16and save.
  6763. 5:46:19And now let's move the rest of the HTML
  6764. 5:46:21in the checkout page.
  6765. 5:46:24If we scroll down here, these meta and
  6766. 5:46:27link elements have already been moved to
  6767. 5:46:30index.html.
  6768. 5:46:33So this is being shared across all of
  6769. 5:46:35our pages. So, we actually don't need to
  6770. 5:46:38move this over anymore because it's
  6771. 5:46:41shared. So, we can remove all of this
  6772. 5:46:43code.
  6773. 5:46:46And finally, we're going to move over
  6774. 5:46:47the CSS for this page
  6775. 5:46:51for general.css.
  6776. 5:46:53Remember that we already copied these
  6777. 5:46:55styles into index.css,
  6778. 5:46:58so we don't need to move this anymore.
  6779. 5:47:03And now we'll just move checkout
  6780. 5:47:04header.css CSS and checkout.css into our
  6781. 5:47:08React project.
  6782. 5:47:11So, we'll go to styles/pages/checkout
  6783. 5:47:16and we'll move checkout header.css
  6784. 5:47:19into the React project into here
  6785. 5:47:24and scroll down. And we'll also move
  6786. 5:47:27checkout.css CSS into our React project
  6787. 5:47:30beside the component.
  6788. 5:47:35And now that we moved everything over,
  6789. 5:47:37we can delete the checkout.html file.
  6790. 5:47:40So, we can delete this file.
  6791. 5:47:46Finally, let's scroll up
  6792. 5:47:48and we're going to rename the CSS file
  6793. 5:47:51to match the component name. So, we'll
  6794. 5:47:54rightclick and rename.
  6795. 5:47:58checkout page.css.
  6796. 5:48:03And now we'll load these two CSS files
  6797. 5:48:06using import. So in checkout page.jsx
  6798. 5:48:10at the top, we'll import a string and
  6799. 5:48:15we'll import the CSS file slash
  6800. 5:48:19checkout-
  6801. 5:48:21header.css.
  6802. 5:48:25And we'll also import
  6803. 5:48:28dot slashcheckout
  6804. 5:48:30page.css
  6805. 5:48:33and save.
  6806. 5:48:36We'll go to our website.
  6807. 5:48:39And now everything looks really good. So
  6808. 5:48:41we finished moving all the HTML and CSS
  6809. 5:48:45for the checkout page into our React
  6810. 5:48:47project.
  6811. 5:48:50Let's go back to our code and we'll save
  6812. 5:48:52these changes into git. We'll open the
  6813. 5:48:55git section
  6814. 5:48:58and in app.jsx
  6815. 5:49:00we use the checkout page component for
  6816. 5:49:02/checkout
  6817. 5:49:04and then we moved over the CSS files and
  6818. 5:49:07in here we moved over the HTML code as
  6819. 5:49:11well as the title. Let's type a message
  6820. 5:49:14to describe these changes.
  6821. 5:49:16move checkout page into React and
  6822. 5:49:22commit.
  6823. 5:49:28Before we continue moving pages, there's
  6824. 5:49:30an issue that we need to fix. If we open
  6825. 5:49:34the website and click the top left icon,
  6826. 5:49:39normally this should take us to the
  6827. 5:49:41homepage, but now it's blank. So why is
  6828. 5:49:44this not working? Notice in the URL the
  6829. 5:49:48path is /index.html.
  6830. 5:49:52Before this website was split up into
  6831. 5:49:54multiple HTML files and index.html
  6832. 5:49:59created the homepage.
  6833. 5:50:01But now we're using routing. So we set
  6834. 5:50:04the URL paths ourselves. In our project
  6835. 5:50:08we set the path slash to be the
  6836. 5:50:10homepage. and we set the path /checkout
  6837. 5:50:14to be the checkout page. So, we need to
  6838. 5:50:16go into our code and update all of the
  6839. 5:50:19links to go to the correct URL path.
  6840. 5:50:22We'll go back to VS Code.
  6841. 5:50:25And to update all of our links, we're
  6842. 5:50:28going to go to the search section of VS
  6843. 5:50:31Code. We can press control shift F on
  6844. 5:50:35Windows or commandshift F on Mac.
  6845. 5:50:39And here we can search inside all of our
  6846. 5:50:41files. First, let's search for the code
  6847. 5:50:45index.html.
  6848. 5:50:47This will give us a list of a elements
  6849. 5:50:50or link elements.
  6850. 5:50:53And href means where these links should
  6851. 5:50:55go. So now instead of index.html,
  6852. 5:51:00these links should go to slash. To
  6853. 5:51:03replace index.html HTML in all these
  6854. 5:51:06places. We can click this arrow on the
  6855. 5:51:08left to open the replace menu. And we'll
  6856. 5:51:12replace this with slash. And then click
  6857. 5:51:16here to replace all. And confirm.
  6858. 5:51:21And let's replace the links for the
  6859. 5:51:24other pages as well. So up here, we'll
  6860. 5:51:27also search for checkout.html.
  6861. 5:51:31And in this search result, we have a
  6862. 5:51:34link element with the href
  6863. 5:51:35checkout.html.
  6864. 5:51:37And in our router, we set the URL path
  6865. 5:51:40for the checkout page to /checkout.
  6866. 5:51:43So we'll replace all of these with
  6867. 5:51:46slashcheckout
  6868. 5:51:48and replace all.
  6869. 5:51:52And we'll do the same thing for the
  6870. 5:51:54orders page, which is orders.html.
  6871. 5:51:59And here we'll change it to slash orders
  6872. 5:52:03and replace.
  6873. 5:52:08And finally, we also have a tracking
  6874. 5:52:10page. So here we're going to search for
  6875. 5:52:15tracking.html
  6876. 5:52:17and replace it with slash tracking and
  6877. 5:52:21replace all.
  6878. 5:52:25And now all of our links should work
  6879. 5:52:27correctly because they match our router.
  6880. 5:52:31If we open the website
  6881. 5:52:33and at the top we change the URL path to
  6882. 5:52:37/checkout
  6883. 5:52:40it opens the checkout page. If we click
  6884. 5:52:43the top left icon
  6885. 5:52:46this should now go to the URL path slash
  6886. 5:52:48or empty and it will open the homepage.
  6887. 5:52:52So let's save this into Git. We'll open
  6888. 5:52:55the git section
  6889. 5:52:58and we just updated all the links in
  6890. 5:53:00these files to match the router.
  6891. 5:53:04Let's type the message update links to
  6892. 5:53:08match the router and commit.
  6893. 5:53:18Next, let's move the orders page into
  6894. 5:53:21our React project. Feel free to pause
  6895. 5:53:23the video if you want to try it yourself
  6896. 5:53:25first. So, we'll go back to our files
  6897. 5:53:30and first let's create a component for
  6898. 5:53:32the orders page. Inside the pages
  6899. 5:53:35folder, we'll create a new file and
  6900. 5:53:38we'll name it orders page.jsx
  6901. 5:53:43and press enter.
  6902. 5:53:46And inside here we'll create a component
  6903. 5:53:49function
  6904. 5:53:51orders page brackets and curly brackets.
  6905. 5:53:54And we'll return brackets
  6906. 5:53:58and we'll move over the HTML. So we'll
  6907. 5:54:01go back to the starting code folder and
  6908. 5:54:04open orders.html.
  6909. 5:54:08We'll start with the body. So we'll
  6910. 5:54:10scroll down and we can collapse these
  6911. 5:54:13two elements and we'll select the HTML
  6912. 5:54:17and cut
  6913. 5:54:19and inside the orders page inside the
  6914. 5:54:22return we'll paste.
  6915. 5:54:25We'll scroll up
  6916. 5:54:30and collapse these elements
  6917. 5:54:32and we're going to wrap these in a
  6918. 5:54:34fragment less than greater than and
  6919. 5:54:37remove this. and at the end less than
  6920. 5:54:41slashgreater than.
  6921. 5:54:43And to fix the indenting, we can use the
  6922. 5:54:45shortcut control shiftp or commandshiftp
  6923. 5:54:50and search for the command format
  6924. 5:54:52document and press enter.
  6925. 5:54:56Again, we need to rename class to class
  6926. 5:54:59name. So, we'll press control F or
  6927. 5:55:02command F to find code. We'll search for
  6928. 5:55:04class equals. Click here and replace it
  6929. 5:55:07with class name and replace all.
  6930. 5:55:12And finally, at the top, we'll export
  6931. 5:55:15this component.
  6932. 5:55:17Let's save this file.
  6933. 5:55:20Let's go back to orders.html.
  6934. 5:55:25Scroll up and we'll move over this code.
  6935. 5:55:29So, for the title, we're going to move
  6936. 5:55:30it into the component. We'll select it,
  6937. 5:55:34cut, and at the top in orders page,
  6938. 5:55:39we're going to paste it at the top.
  6939. 5:55:43Let's go back to orders.html.
  6940. 5:55:48And these meta and link elements are
  6941. 5:55:50already inside index.html.
  6942. 5:55:53So, we can remove this.
  6943. 5:55:57And finally, we'll move the CSS files
  6944. 5:55:59over. So, general.css CSS was already
  6945. 5:56:02moved into index.css.
  6946. 5:56:05We already moved over header.css up
  6947. 5:56:09here. So, we just need to move the
  6948. 5:56:10orders.css.
  6949. 5:56:12So, inside starting code, we'll go into
  6950. 5:56:15styles/pages
  6951. 5:56:17and we'll move this orders.css file.
  6952. 5:56:23So, we'll move it up here
  6953. 5:56:27beside the orders page component.
  6954. 5:56:30And now we moved over everything inside
  6955. 5:56:33orders.html.
  6956. 5:56:35So we can scroll down and delete this
  6957. 5:56:38file.
  6958. 5:56:42Next, we should rename orders.css
  6959. 5:56:45to match the component name. So we'll
  6960. 5:56:48rightclick and rename
  6961. 5:56:52orders page.css.
  6962. 5:56:57And finally, we'll load the CSS files.
  6963. 5:57:00So, in orders page.jsx
  6964. 5:57:03at the top, we're going to import a
  6965. 5:57:07string. And remember, this requires the
  6966. 5:57:10header and the orders page CSS.
  6967. 5:57:14So, first we'll import dot slash
  6968. 5:57:16header.css.
  6969. 5:57:19And we'll also import
  6970. 5:57:22dot slash orders page.css. CSS and save.
  6971. 5:57:30And now we moved over all the HTML and
  6972. 5:57:33CSS for the orders page.
  6973. 5:57:36So now let's create a route for this
  6974. 5:57:38page. We'll go to app.jsx.
  6975. 5:57:43And first at the top we're going to
  6976. 5:57:46import curly brackets the orders page
  6977. 5:57:53from a string. and we'll locate dot
  6978. 5:57:56slashpages slash orders page and then
  6979. 5:58:01down here we're going to add another
  6980. 5:58:03route. So at the end we'll add less than
  6981. 5:58:07route greater than and we can remove
  6982. 5:58:11this
  6983. 5:58:12and use the self-closing element
  6984. 5:58:15shortcut
  6985. 5:58:17and we'll give it two props. We'll set
  6986. 5:58:19the path equals double quotes and we'll
  6987. 5:58:23put the orders page on the path slash
  6988. 5:58:26orders
  6989. 5:58:28and we'll give it an element to display
  6990. 5:58:31equals curly brackets. And for the
  6991. 5:58:34orders page, we'll display the element
  6992. 5:58:36less than orders page slashgrater than
  6993. 5:58:42and save.
  6994. 5:58:45And now let's view the website. We'll
  6995. 5:58:48open the website
  6996. 5:58:51and on the homepage we're going to click
  6997. 5:58:53this orders link to go to slash orders
  6998. 5:58:58and it displays the orders page. So that
  6999. 5:59:01looks pretty good. And now we finished
  7000. 5:59:03moving the orders page into React.
  7001. 5:59:07So let's go back to VS Code.
  7002. 5:59:10And in the starting code, there's one
  7003. 5:59:14more page to move over, which is the
  7004. 5:59:16tracking page. But I'll leave this as an
  7005. 5:59:19exercise.
  7006. 5:59:20So now let's save these changes into
  7007. 5:59:22Git. Open the Git section.
  7008. 5:59:26In app.jsx, we added a new route for the
  7009. 5:59:29orders page and we moved over the CSS
  7010. 5:59:32and the HTML.
  7011. 5:59:35So we'll type the message move orders
  7012. 5:59:39page to React
  7013. 5:59:42and commit.
  7014. 5:59:50Next, we're going to separate our page
  7015. 5:59:52into smaller components.
  7016. 5:59:54For example, if we look at our website,
  7017. 5:59:58notice that on the orders page, there's
  7018. 6:00:00this header at the top. And if we click
  7019. 6:00:03the top left and go to the homepage, we
  7020. 6:00:06have the same header at the top of the
  7021. 6:00:09page. So, it's a good idea to separate
  7022. 6:00:12the header into a component so we can
  7023. 6:00:14reuse it between these pages. We'll go
  7024. 6:00:18back to VS Code
  7025. 6:00:21and we'll open our files and we're going
  7026. 6:00:24to open homepage.jsx.
  7027. 6:00:28And at the top here, we have the HTML
  7028. 6:00:30for the header. If we collapse it, this
  7029. 6:00:34is all the HTML for the header.
  7030. 6:00:37Let's separate this into a shared
  7031. 6:00:39component.
  7032. 6:00:41Now, because this component is shared
  7033. 6:00:43between multiple pages, we usually put
  7034. 6:00:46shared components in a folder called
  7035. 6:00:48components. So, inside our source
  7036. 6:00:51folder, we're going to click it and
  7037. 6:00:53create a new folder
  7038. 6:00:56and we'll call it components.
  7039. 6:01:01So the components folder is for shared
  7040. 6:01:03components and the pages folder is for
  7041. 6:01:06components that are specific to a page.
  7042. 6:01:10Inside the components folder, we'll
  7043. 6:01:12create a new file.
  7044. 6:01:15And let's name it header.jsx.
  7045. 6:01:21And in here we'll create a component for
  7046. 6:01:23the header using function
  7047. 6:01:26header brackets and curly brackets. And
  7048. 6:01:30we'll return brackets.
  7049. 6:01:34And we'll move over the HTML.
  7050. 6:01:37So we'll open homepage.jsx JSX
  7051. 6:01:41and we're going to select the HTML for
  7052. 6:01:43the header and cut
  7053. 6:01:50inside header.jsx.
  7054. 6:01:52Inside here, we're going to paste.
  7055. 6:01:56And we'll fix the indenting by pressing
  7056. 6:01:58control shiftp or command shiftp and
  7057. 6:02:02search for format document and press
  7058. 6:02:05enter.
  7059. 6:02:08And we'll scroll to the top. And lastly,
  7060. 6:02:12we're going to export this component to
  7061. 6:02:14remove this error.
  7062. 6:02:18Next, we should also move over the CSS
  7063. 6:02:20for the header. Now, luckily the CSS for
  7064. 6:02:24the header has already been separated
  7065. 6:02:26into header.css.
  7066. 6:02:28So, we can just move this over to beside
  7067. 6:02:31the component.
  7068. 6:02:36So, we want to keep the CSS close to the
  7069. 6:02:39component so it's easy to find. So at
  7070. 6:02:42the top of this we can import the CSS
  7071. 6:02:45using import a string and we'll import
  7072. 6:02:49dot slash header.css
  7073. 6:02:53and save.
  7074. 6:02:56So now that we created this header
  7075. 6:02:58component, we can reuse it in the
  7076. 6:03:00homepage and the orders page. We'll open
  7077. 6:03:04homepage.jsx.
  7078. 6:03:06And at the top first, we don't need to
  7079. 6:03:09import the header anymore. It's already
  7080. 6:03:11done by this component. So we can remove
  7081. 6:03:13it. And let's import the component using
  7082. 6:03:17import curly brackets. The header
  7083. 6:03:23from a string.
  7084. 6:03:25And now we need to locate this file. So
  7085. 6:03:28currently we're inside the pages folder.
  7086. 6:03:31So we need to get out of this folder
  7087. 6:03:33using dot dot slash and then go into the
  7088. 6:03:36components folder. components slash
  7089. 6:03:39header.
  7090. 6:03:41And now we'll use this component in the
  7091. 6:03:43HTML. So down here
  7092. 6:03:47we'll insert less than header
  7093. 6:03:50slashgreater than and save.
  7094. 6:03:55So now this HTML is a bit easier to work
  7095. 6:03:58with because we split it up into smaller
  7096. 6:04:01pieces. Let's do the same thing with the
  7097. 6:04:03orders page. We'll open the orders page.
  7098. 6:04:07jsx
  7099. 6:04:08and at the top we don't need to import
  7100. 6:04:10header.css CSS and instead we'll import
  7101. 6:04:14curly brackets the header component from
  7102. 6:04:19a string and we'll locate dot dot slash
  7103. 6:04:23components
  7104. 6:04:25slash header
  7105. 6:04:27and down here instead of this HTML we
  7106. 6:04:30can remove this and just use the
  7107. 6:04:33component less than header slashgrater
  7108. 6:04:38than this helps us reuse and save a lot
  7109. 6:04:41of code. Let's save
  7110. 6:04:45and then view the website.
  7111. 6:04:48The header looks good in the homepage.
  7112. 6:04:50And if we click here to go to the orders
  7113. 6:04:53page, the header also looks good in the
  7114. 6:04:55orders page. So, we're able to reuse the
  7115. 6:04:58header component.
  7116. 6:05:02Next, there's a small issue with the
  7117. 6:05:04header. when we click a link like this
  7118. 6:05:08link in the top left, it actually
  7119. 6:05:11reloads the page when we go to another
  7120. 6:05:13page. So by default, link elements or a
  7121. 6:05:17elements reload the page. Now this made
  7122. 6:05:21sense before when our website had
  7123. 6:05:23multiple HTML files. To go from one file
  7124. 6:05:27to another file, we have to reload the
  7125. 6:05:29page. However, now that we're using
  7126. 6:05:32routing and a single page application,
  7127. 6:05:35we don't have to reload every time, we
  7128. 6:05:38can just use JavaScript to switch
  7129. 6:05:40between pages.
  7130. 6:05:42To do this, React Router provides a
  7131. 6:05:45component called link. So, we'll go back
  7132. 6:05:48to our code
  7133. 6:05:51and we'll open the header component
  7134. 6:05:55and at the top
  7135. 6:05:57we're going to import
  7136. 6:06:00curly brackets
  7137. 6:06:02the link component from
  7138. 6:06:06the package react-rowout.
  7139. 6:06:10So, the link component lets us go to
  7140. 6:06:13another page without reloading.
  7141. 6:06:17To use it, we just have to replace our
  7142. 6:06:20link elements or a elements with this
  7143. 6:06:23link component. So here, let's replace
  7144. 6:06:26this with link and at the end slash
  7145. 6:06:32link.
  7146. 6:06:34And also instead of the href attribute,
  7147. 6:06:37the link component uses a prop called
  7148. 6:06:40to. So this will link to this page.
  7149. 6:06:46Let's scroll down
  7150. 6:06:49and in the header we have two other
  7151. 6:06:51links, the orders link and the cart
  7152. 6:06:54link. So we'll switch both of these as
  7153. 6:06:56well. So here we'll switch it to link
  7154. 6:07:01and slashlink
  7155. 6:07:04and we'll switch the href to two.
  7156. 6:07:08And finally, switch this to link
  7157. 6:07:12and slashlink
  7158. 6:07:14and switch href to two and save.
  7159. 6:07:20And now let's open our website to try it
  7160. 6:07:23out.
  7161. 6:07:25If we click this link to go to the
  7162. 6:07:27orders page, you'll notice it's a little
  7163. 6:07:30bit faster because now it's using
  7164. 6:07:32JavaScript to essentially display a
  7165. 6:07:35different page without reloading.
  7166. 6:07:38So when using React Router, we should
  7167. 6:07:41use the link component instead of the A
  7168. 6:07:44element because the link component lets
  7169. 6:07:47us go to another page without reloading.
  7170. 6:07:51Now let's save these changes into git.
  7171. 6:07:55We'll go to the git section
  7172. 6:07:58and here we split up the header into a
  7173. 6:08:01separate component. And then inside the
  7174. 6:08:04header, we switch to using the link
  7175. 6:08:07component to switch between pages.
  7176. 6:08:10Let's type the message
  7177. 6:08:12split header into a component
  7178. 6:08:17and use link and commit.
  7179. 6:08:27In the next lesson, we'll add a backend
  7180. 6:08:30to this project and make it interactive.
  7181. 6:08:33So, we'll be able to display a list of
  7182. 6:08:35products and add products to a cart and
  7183. 6:08:38create an order. And that's the end of
  7184. 6:08:41this lesson. In this lesson, we started
  7185. 6:08:44the ecommerce project using React and
  7186. 6:08:47Vit. We reviewed Git, which helps us
  7187. 6:08:51track changes in our code. We learned
  7188. 6:08:54routing, which lets us create multiple
  7189. 6:08:57pages in React. We created the homepage,
  7190. 6:09:01the checkout page, and the orders page
  7191. 6:09:03in React. We updated all the links to
  7192. 6:09:06work with routing, and we separated the
  7193. 6:09:10header into a component.
  7194. 6:09:13Here are some exercises to help you
  7195. 6:09:15practice what we learned in this lesson.
  7196. 6:10:04In this lesson, we're going to start
  7197. 6:10:05making our e-commerce project
  7198. 6:10:07interactive and then we'll learn how to
  7199. 6:10:10use a backend with this project.
  7200. 6:10:13By the way, if something in this lesson
  7201. 6:10:15does not work for you, you can check the
  7202. 6:10:18troubleshooting guide in the
  7203. 6:10:19description.
  7204. 6:10:21First, if we look at our website,
  7205. 6:10:25currently we only have three products on
  7206. 6:10:28this website, but in the final project,
  7207. 6:10:32we have a lot more products. So, we're
  7208. 6:10:35going to add all of these products to
  7209. 6:10:37our website. So, let's go to the code
  7210. 6:10:40for the homepage.
  7211. 6:10:42We'll open our code and go to the files
  7212. 6:10:47and we're going to find the pages folder
  7213. 6:10:50and open homepage.jsx.
  7214. 6:10:54So this component represents the
  7215. 6:10:56homepage. And we're going to scroll down
  7216. 6:10:59and find the element with a class
  7217. 6:11:02product-container.
  7218. 6:11:04And we're going to click here to
  7219. 6:11:05collapse this element. And we're going
  7220. 6:11:07to collapse this other product
  7221. 6:11:09container. and this other product
  7222. 6:11:12container as well.
  7223. 6:11:14Each product container represents one
  7224. 6:11:17product on the page. To add more
  7225. 6:11:20products, we could make a copy of this
  7226. 6:11:23code and add each product manually.
  7227. 6:11:26However, that would be very repetitive.
  7228. 6:11:29Instead, we can generate this HTML using
  7229. 6:11:33React. We already learned this technique
  7230. 6:11:36in the chatbot project where we
  7231. 6:11:38generated a list of chat messages on the
  7232. 6:11:41page. So, we're going to review how to
  7233. 6:11:43generate the HTML.
  7234. 6:11:45First, we need to save the data for
  7235. 6:11:48these products in our JavaScript. To
  7236. 6:11:51save time, I already did this. So, if we
  7237. 6:11:53go to our files and we go to the
  7238. 6:11:56starting code folder
  7239. 6:11:58and we open this data folder inside
  7240. 6:12:02products.js, JS I already saved the data
  7241. 6:12:06for the products in this array. So we
  7242. 6:12:09can just use this as our data.
  7243. 6:12:12Each object in the array represents one
  7244. 6:12:15product. So it has a name, an image, and
  7245. 6:12:18a price and some other details. The next
  7246. 6:12:21step is to use this data to generate the
  7247. 6:12:25HTML. We'll go back to the homepage
  7248. 6:12:30and first we're going to import that
  7249. 6:12:32products array. So at the top we're
  7250. 6:12:36going to import curly brackets the
  7251. 6:12:39products array from a string and we're
  7252. 6:12:43going to locate that file. So first we
  7253. 6:12:46need to get out of the pages and source
  7254. 6:12:47folder using dot dot dot slash and then
  7255. 6:12:51we'll find the starting code folder and
  7256. 6:12:54the data folder. starting dashcode slash
  7257. 6:12:58data slash and the products file.
  7258. 6:13:03Next, we're going to loop through this
  7259. 6:13:05products array. So, at the bottom here
  7260. 6:13:09above these product containers, we're
  7261. 6:13:12going to insert using curly brackets the
  7262. 6:13:15products array dot map and brackets. And
  7263. 6:13:19we're going to give map a function
  7264. 6:13:21brackets arrow and curly brackets.
  7265. 6:13:25As a review, do map takes each value in
  7266. 6:13:29an array and transforms it. In this
  7267. 6:13:32parameter, we get each value in this
  7268. 6:13:35array, which is a product.
  7269. 6:13:39So again, a product is just this object
  7270. 6:13:42and it has some details about the
  7271. 6:13:43product inside.
  7272. 6:13:45So, we'll go back
  7273. 6:13:48and we're going to transform this
  7274. 6:13:50product object into this HTML.
  7275. 6:13:54To do that, we're just going to copy
  7276. 6:13:56this HTML
  7277. 6:13:58and inside map, we're going to return
  7278. 6:14:02brackets
  7279. 6:14:04and paste that HTML.
  7280. 6:14:07And to fix the formatting, we're going
  7281. 6:14:09to press control or commandshiftp
  7282. 6:14:13and then search for format document
  7283. 6:14:16and press enter.
  7284. 6:14:19And let's scroll up.
  7285. 6:14:22And remember, whenever we loop through
  7286. 6:14:24an array like this, we need to give each
  7287. 6:14:26element a key prop equals curly
  7288. 6:14:30brackets. So the key helps React keep
  7289. 6:14:33track of changes in this array. Now the
  7290. 6:14:36key has to be unique. So looking at the
  7291. 6:14:39data, each product has a unique ID
  7292. 6:14:43property.
  7293. 6:14:45So we'll go back and we're going to use
  7294. 6:14:47the product. ID as the key. So this
  7295. 6:14:53basically takes each product object and
  7296. 6:14:56transforms it into this HTML.
  7297. 6:14:59So now let's save
  7298. 6:15:02and we'll check the website.
  7299. 6:15:05And as you can see, we took each product
  7300. 6:15:08and transformed it into some HTML.
  7301. 6:15:12Now, obviously, we don't want the
  7302. 6:15:13product to be socks every time. So,
  7303. 6:15:16let's insert the details of each
  7304. 6:15:18product. We'll go back to our code, and
  7305. 6:15:23instead of displaying the socks every
  7306. 6:15:25time, we're going to remove this. and
  7307. 6:15:28insert.
  7308. 6:15:30And each product object has a name
  7309. 6:15:32property. So we'll insert the
  7310. 6:15:35product.name.
  7311. 6:15:37And let's also replace the product
  7312. 6:15:40image. So instead of showing the socks
  7313. 6:15:43image every time, we'll remove this and
  7314. 6:15:46we'll insert the product.
  7315. 6:15:49Property.
  7316. 6:15:51Next, we'll insert the ratings. The way
  7317. 6:15:55this works is that each rating has an
  7318. 6:15:57image. And we just have to multiply the
  7319. 6:16:00rating by 10 to get the image name. So
  7320. 6:16:03for this one, we're going to put open
  7321. 6:16:06curly bracket and close curly bracket
  7322. 6:16:09around this value. And we're going to
  7323. 6:16:11change this to a template string using
  7324. 6:16:14back ticks and back ticks. And then
  7325. 6:16:18instead of 45 every time, we're going to
  7326. 6:16:21insert. And remember for template
  7327. 6:16:23strings to insert we use dollar curly
  7328. 6:16:26brackets. So in here we're going to
  7329. 6:16:28insert the product dot rating dot stars
  7330. 6:16:34and then multiply it by 10. And this
  7331. 6:16:37should give us the ratings image that we
  7332. 6:16:39need. Next we're going to replace the
  7333. 6:16:42rating count. So curly brackets and
  7334. 6:16:45we'll insert the product.
  7335. 6:16:50count.
  7336. 6:16:51Next, we'll insert the price. So,
  7337. 6:16:54instead of this price every time, we'll
  7338. 6:16:57insert the product dot price sets. And
  7339. 6:17:03we usually save money in cents because
  7340. 6:17:06many programming languages have trouble
  7341. 6:17:08calculating decimal numbers. Now, when
  7342. 6:17:11we display it on the website, we want to
  7343. 6:17:13display it as dollars. So to convert
  7344. 6:17:16this back to dollars, we're going to
  7345. 6:17:18divide by 100 since $1 is equal to 100.
  7346. 6:17:24And that's it. We inserted the details
  7347. 6:17:27of each product into this HTML.
  7348. 6:17:30This is the same technique that we used
  7349. 6:17:32in the chatbot project, except our data
  7350. 6:17:35is just a little more complicated. Now,
  7351. 6:17:38let's save
  7352. 6:17:40and check the website.
  7353. 6:17:43And now we have a list of different
  7354. 6:17:45products on the page and we generated
  7355. 6:17:48the HTML for these products. Now there
  7356. 6:17:52is a small problem here which is for
  7357. 6:17:54these socks it's showing the price 10.9
  7358. 6:17:58but usually for price we want to show
  7359. 6:18:0010.90.
  7360. 6:18:02To fix this, we'll go back to our code
  7361. 6:18:06and we're going to scroll down to where
  7362. 6:18:08we insert the price. And to force this
  7363. 6:18:12to show two decimals, we're going to
  7364. 6:18:14wrap this in a open bracket and a closed
  7365. 6:18:18bracket. And then outside of this
  7366. 6:18:20bracket, we'll use the method dot two
  7367. 6:18:23capital Fixed
  7368. 6:18:25and brackets.
  7369. 6:18:27So this converts the number into a
  7370. 6:18:29string and inside these brackets we can
  7371. 6:18:32tell it how many decimals to show. So
  7372. 6:18:35we'll put two
  7373. 6:18:38and save.
  7374. 6:18:40Now if we check the website
  7375. 6:18:43all the prices show two decimals. So
  7376. 6:18:46that's a review of how to generate the
  7377. 6:18:48HTML using some data. Let's go back to
  7378. 6:18:52our code.
  7379. 6:18:54And now that we're generating all this
  7380. 6:18:57HTML, we can scroll to the bottom and
  7381. 6:19:00remove this default HTML that we had
  7382. 6:19:03before and save.
  7383. 6:19:07And now let's save these changes into
  7384. 6:19:09git. We'll go to the git section and at
  7385. 6:19:13the top we'll use a message generate the
  7386. 6:19:17HTML for the home page and commit.
  7387. 6:19:27Now, we're going to take this project to
  7388. 6:19:29the next level and go beyond what we did
  7389. 6:19:32in the chatbot project. We're going to
  7390. 6:19:35add a backend to this project. So, first
  7391. 6:19:38we'll learn what a backend is and why we
  7392. 6:19:41use a backend. So, right now, if we look
  7393. 6:19:45at our files,
  7394. 6:19:47notice that all the products are saved
  7395. 6:19:49in a file on our computer called
  7396. 6:19:52products.js. JS.
  7397. 6:19:54The problem with this is an e-commerce
  7398. 6:19:57website or an online shopping website
  7399. 6:20:00might have thousands of products and it
  7400. 6:20:02does not make sense to save everything
  7401. 6:20:05on our computer or on the customer's
  7402. 6:20:08computer. We might not have enough space
  7403. 6:20:11on our computer. So, what the company or
  7404. 6:20:14the store does is they set up another
  7405. 6:20:17computer in their office or in what we
  7406. 6:20:20call a data center. And this computer
  7407. 6:20:23saves all the thousands of products that
  7408. 6:20:25they offer. And then when we visit the
  7409. 6:20:28website, this computer sends us only the
  7410. 6:20:31products that we need to display. So
  7411. 6:20:34this computer is called the backend.
  7412. 6:20:37It's just another computer that's owned
  7413. 6:20:39by the company and it manages all the
  7414. 6:20:42data.
  7415. 6:20:43Here's another example of why we use a
  7416. 6:20:46backend. Later in this course, we're
  7417. 6:20:48going to add products to a cart. So,
  7418. 6:20:51where do we save this cart? It does not
  7419. 6:20:54make sense to save the cart on the
  7420. 6:20:56customer's computer because if the
  7421. 6:20:58customer uses a different computer to
  7422. 6:21:00visit the website, they would no longer
  7423. 6:21:03have access to their cart. Instead, we
  7424. 6:21:06save the cart in the back end as well.
  7425. 6:21:09When they access the website, the
  7426. 6:21:11backend sends the cart to the website.
  7427. 6:21:14So, the back end also lets us share data
  7428. 6:21:17between computers.
  7429. 6:21:20So again, the backend is another
  7430. 6:21:22computer that manages the data. And this
  7431. 6:21:26is how a React website works in the real
  7432. 6:21:28world. We usually contact a backend to
  7433. 6:21:32get the data. So we're going to practice
  7434. 6:21:34that in this course. We're going to
  7435. 6:21:36learn how to add a backend and then
  7436. 6:21:38we're going to learn how to use the
  7437. 6:21:39backend in React. So you can see how we
  7438. 6:21:42use React in the real world.
  7439. 6:21:47Now that we learned what a backend is
  7440. 6:21:49and why we use a backend, let's add a
  7441. 6:21:52backend to our project. We're going to
  7442. 6:21:55click this link in the description. Go
  7443. 6:21:57to lesson 7 and click this link to open
  7444. 6:22:00the backend code.
  7445. 6:22:03And this is a backend that we can use
  7446. 6:22:06with our project. I called it the
  7447. 6:22:08e-commerce backend AI because this was
  7448. 6:22:11built with AI. And I show you how to do
  7449. 6:22:13that in another video. But for this
  7450. 6:22:16video, I recommend downloading this
  7451. 6:22:18version to make sure it works with a
  7452. 6:22:20video. You can try to recreate the back
  7453. 6:22:23end as an exercise. So, we're going to
  7454. 6:22:25click this code button here and we'll
  7455. 6:22:27click download zip.
  7456. 6:22:31Once that finishes downloading, we're
  7457. 6:22:33going to open the downloads folder. So,
  7458. 6:22:35for me, it's right here. And then we're
  7459. 6:22:37going to unzip this. On Windows, we're
  7460. 6:22:40going to rightclick the file and then
  7461. 6:22:42click extract all.
  7462. 6:22:49And on Mac, you can just doubleclick the
  7463. 6:22:51zip file.
  7464. 6:22:54Then we're going to move this back end
  7465. 6:22:56into our project. So we'll open our
  7466. 6:22:59code.
  7467. 6:23:00And then we're just going to drag this
  7468. 6:23:02folder into our project. But first, I'm
  7469. 6:23:05going to collapse these folders for now.
  7470. 6:23:08And we're going to drag this folder to
  7471. 6:23:10the outside
  7472. 6:23:12and click copy folder
  7473. 6:23:16so that the backend code is right beside
  7474. 6:23:18our website. By the way, we call the
  7475. 6:23:22website or what the user interacts with
  7476. 6:23:24the front end.
  7477. 6:23:27We're also going to rename this folder.
  7478. 6:23:29So, right click and rename.
  7479. 6:23:33and we'll just rename it the e-commerce
  7480. 6:23:35backend to make it easier to understand.
  7481. 6:23:39Next, we're going to start this backend.
  7482. 6:23:43So, we're going to go to our terminal or
  7483. 6:23:45our command line and we'll click here to
  7484. 6:23:48create a new tab. And then in here,
  7485. 6:23:52first we're going to change directory
  7486. 6:23:54into the e-commerce backend folder. So,
  7487. 6:23:57we'll cd ecommerce
  7488. 6:24:00dashbackend.
  7489. 6:24:02So now the command line is running
  7490. 6:24:04inside this folder and the process for
  7491. 6:24:07starting the back end is very similar to
  7492. 6:24:10the website or the front end. So first
  7493. 6:24:13we're going to npm install to install
  7494. 6:24:17all the npm packages that we need
  7495. 6:24:23and then we'll run npm rundev to start
  7496. 6:24:28the back end.
  7497. 6:24:30So, this tells us the back end is
  7498. 6:24:32running on port 3000, which is localhost
  7499. 6:24:36col 3000. To test it, we're going to go
  7500. 6:24:39to our browser,
  7501. 6:24:41and at the top, we're going to create a
  7502. 6:24:43new tab, and we're going to go to
  7503. 6:24:46localhost
  7504. 6:24:48col 3000
  7505. 6:24:51/s.
  7506. 6:24:56And the back end should send back some
  7507. 6:24:59data.
  7508. 6:25:00And this is actually the data for the
  7509. 6:25:03products.
  7510. 6:25:05I can also click this checkbox here to
  7511. 6:25:08show it in an easier to read format. And
  7512. 6:25:10you can see this is the same products
  7513. 6:25:12array that we were using before except
  7514. 6:25:15this is coming from the back end. So the
  7515. 6:25:18way a backend works is when we access
  7516. 6:25:21the backend using a URL, it will send
  7517. 6:25:24back some data. So this is working.
  7518. 6:25:28So now let's go back to our code and
  7519. 6:25:30we'll save these changes into git. Open
  7520. 6:25:33the git section and we'll use the
  7521. 6:25:36message add ecommerce
  7522. 6:25:40backend and commit.
  7523. 6:25:47Next we're going to learn data fetching.
  7524. 6:25:49Data fetching means to get data from the
  7525. 6:25:52back end using our code. So, first we're
  7526. 6:25:56going to go to our files and let's go
  7527. 6:25:59back to the e-commerce project. And
  7528. 6:26:02we're going to open our homepage,
  7529. 6:26:04which is right here.
  7530. 6:26:07And we'll scroll to the top. And one way
  7531. 6:26:10to fetch data or to get data from the
  7532. 6:26:13back end is to use the function fetch
  7533. 6:26:17and brackets.
  7534. 6:26:19So fetch is a built-in function. It's
  7535. 6:26:22provided by JavaScript.
  7536. 6:26:24between the brackets, we're going to
  7537. 6:26:26create a string
  7538. 6:26:28and we're going to put the URL that we
  7539. 6:26:31want to get data from. So, if we go back
  7540. 6:26:34to our browser,
  7541. 6:26:37remember that this URL gave us the
  7542. 6:26:39products data. So, we can just select
  7543. 6:26:42this URL and copy.
  7544. 6:26:46And in our code, we're going to paste it
  7545. 6:26:49into fetch.
  7546. 6:26:52And make sure you have http col double
  7547. 6:26:54slash in front. It doesn't show in the
  7548. 6:26:56browser, but we need this in order for
  7549. 6:26:58our code to work. So if we fetch data
  7550. 6:27:02from this URL, this should give us the
  7551. 6:27:06products data that we saw earlier.
  7552. 6:27:09However, we cannot save this data in a
  7553. 6:27:12variable like this. And that's because
  7554. 6:27:14when we contact a backend, it takes time
  7555. 6:27:18for this code to reach the back end and
  7556. 6:27:20to get a response. So this code does not
  7557. 6:27:24finish right away. It finishes at some
  7558. 6:27:27point in the future. As a review, we
  7559. 6:27:30call this asynchronous code.
  7560. 6:27:33Asynchronous code means code that does
  7561. 6:27:35not finish right away. So how do we
  7562. 6:27:38handle this situation?
  7563. 6:27:40fetch returns a promise and a promise
  7564. 6:27:44lets us wait for asynchronous code to
  7565. 6:27:47finish.
  7566. 6:27:48So a promise has a method called dot
  7567. 6:27:52then and brackets and we give dot then a
  7568. 6:27:56function brackets arrow and curly
  7569. 6:27:59brackets.
  7570. 6:28:00The way this works is that this code
  7571. 6:28:03contacts the backend to get some data
  7572. 6:28:06but it takes some time for the backend
  7573. 6:28:08to respond. So the code is going to keep
  7574. 6:28:11going line by line. Then at some point
  7575. 6:28:15in the future, this is going to finish.
  7576. 6:28:18We're going to get the response from the
  7577. 6:28:19back end. When it finishes in the
  7578. 6:28:22future, it will run the function inside.
  7579. 6:28:26So that's a review of how a promise
  7580. 6:28:29works. So next when fetch gets a
  7581. 6:28:32response from the back end it will save
  7582. 6:28:35it as a parameter in this inner function
  7583. 6:28:38called response.
  7584. 6:28:40By the way when we contact the back end
  7585. 6:28:43using fetch this is called a request.
  7586. 6:28:47When the back end responds with some
  7587. 6:28:49data this is called a response. So this
  7588. 6:28:53parameter contains the response from the
  7589. 6:28:56back end. And inside here, let's just
  7590. 6:28:59console.log
  7591. 6:29:01the response to see what it looks like.
  7592. 6:29:04We'll save
  7593. 6:29:07and then check our website
  7594. 6:29:10and we'll open the tab for our website
  7595. 6:29:13and we're going to open the console.
  7596. 6:29:15Rightclick, inspect,
  7597. 6:29:18and the console.
  7598. 6:29:20And you can see that we got the response
  7599. 6:29:22from the back end. So inside here we
  7600. 6:29:25have some information like the URL that
  7601. 6:29:27we accessed and a status code. 200 means
  7602. 6:29:31it was successful.
  7603. 6:29:33However, notice that we don't have the
  7604. 6:29:36products data in this response. To get
  7605. 6:29:39the products data, we're going to go
  7606. 6:29:41back to our code
  7607. 6:29:44and this response object has a method
  7608. 6:29:47called response.json
  7609. 6:29:50JSON and brackets.
  7610. 6:29:53So,JSON gives us the data that is
  7611. 6:29:57attached to the response. In this case,
  7612. 6:29:59it gives us the products data. Now, the
  7613. 6:30:03tricky part about this is response.json
  7614. 6:30:06is also asynchronous. So, again, we
  7615. 6:30:09can't save this in a variable. Instead,
  7616. 6:30:13this is also a promise. So we have to
  7617. 6:30:16use dot then again and give this another
  7618. 6:30:20function brackets arrow and curly
  7619. 6:30:22brackets
  7620. 6:30:25and then inside this inner function we
  7621. 6:30:28have access to the data that we get from
  7622. 6:30:31the back end. So in this case this is
  7623. 6:30:34the products data.
  7624. 6:30:36So if we console.log log the data and
  7625. 6:30:41save
  7626. 6:30:43and then check the console.
  7627. 6:30:46You'll see that the backend gave us an
  7628. 6:30:48array of products to display on the
  7629. 6:30:51page. So, this is the same thing as the
  7630. 6:30:54products array that we saved in our
  7631. 6:30:56file, but this data is coming from the
  7632. 6:30:59back end.
  7633. 6:31:01Let's go back.
  7634. 6:31:03And that's how we fetch data or get data
  7635. 6:31:07from the back end. We use the built-in
  7636. 6:31:09function fetch and give it the URL of
  7637. 6:31:12the back end and then we use dot then to
  7638. 6:31:16wait for a response.
  7639. 6:31:18Now one shortcut we can use is instead
  7640. 6:31:21of doing dot then inside another dot
  7641. 6:31:24then we can also return the
  7642. 6:31:28response.json JSON and then move this
  7643. 6:31:30dot then to the outside.
  7644. 6:31:34So this code does the same thing as
  7645. 6:31:36before. This waits for this to finish
  7646. 6:31:40and then runs this function. So this is
  7647. 6:31:44just a shortcut that we can use with
  7648. 6:31:46promises.
  7649. 6:31:48Another thing to note is the back end
  7650. 6:31:50can also run on the same computer as the
  7651. 6:31:53website or the front end. So right now
  7652. 6:31:56this back end is actually running on our
  7653. 6:31:58own computer. Locost means our own
  7654. 6:32:02computer. So when we contact the back
  7655. 6:32:04end, we're actually just contacting our
  7656. 6:32:06own computer. We do this during
  7657. 6:32:09development because it's easier. But
  7658. 6:32:11once this website is on the internet,
  7659. 6:32:13the back end will be another computer.
  7660. 6:32:17So now let's save this code
  7661. 6:32:20and we'll save this into git. In the get
  7662. 6:32:23section, we'll use the message fetch
  7663. 6:32:27data from the back end and commit.
  7664. 6:32:35Next, notice that using fetch is still a
  7665. 6:32:38little hard to read. We use a and then
  7666. 6:32:42we use response.json and then another.
  7667. 6:32:46To make this code cleaner, we're going
  7668. 6:32:48to use an npm package called Axios.
  7669. 6:32:52Axios is a cleaner way to make requests
  7670. 6:32:55to the back end. First, we're going to
  7671. 6:32:58go to our command line and we'll open
  7672. 6:33:00the tab for the front end. And here
  7673. 6:33:03we'll press Ctrl C to stop the V server.
  7674. 6:33:07And we're going to npm install Axios and
  7675. 6:33:12type at and we're going to give it a
  7676. 6:33:14version. So, we can click this link in
  7677. 6:33:17the description and then go to lesson 7.
  7678. 6:33:20And this is a version of Axios we're
  7679. 6:33:22going to use. So, again, you should
  7680. 6:33:25always use the version that you see on
  7681. 6:33:27your page.
  7682. 6:33:29So, select the version on your page and
  7683. 6:33:32copy it
  7684. 6:33:34and then paste it in our code and press
  7685. 6:33:37enter to install Axios.
  7686. 6:33:41Once that's installed, we're going to
  7687. 6:33:42run mpm rundev to restart the vit
  7688. 6:33:46server.
  7689. 6:33:48And then to use axios in our code, we're
  7690. 6:33:51going to go to the homepage. And at the
  7691. 6:33:53top, we're going to import axios without
  7692. 6:33:58curly brackets from a string axios.
  7693. 6:34:03And now instead of using fetch, we're
  7694. 6:34:05going to use axios.get.
  7695. 6:34:09So this does the same thing as before
  7696. 6:34:12except the data from the back end will
  7697. 6:34:14be saved directly inside the response.
  7698. 6:34:18So instead of doing all these steps,
  7699. 6:34:21we can just get the data using response.
  7700. 6:34:27So let's console.log this to see if it
  7701. 6:34:29works. So console.log and save.
  7702. 6:34:34And then if we open the console,
  7703. 6:34:37you'll notice it gets the same products
  7704. 6:34:39data as before.
  7705. 6:34:43But if we look at our code, it's a lot
  7706. 6:34:45cleaner because we can just get the data
  7707. 6:34:47directly from the response. So as you
  7708. 6:34:50can see, Axios is a cleaner way to make
  7709. 6:34:53requests to the back end. So now let's
  7710. 6:34:56save this into git. In the get section,
  7711. 6:34:59we'll use a message use Axios to fetch
  7712. 6:35:04the data and commit.
  7713. 6:35:11Now, let's use this data from the back
  7714. 6:35:14end to generate the HTML instead of
  7715. 6:35:17using the products file that we had
  7716. 6:35:19before. But first, we have a problem.
  7717. 6:35:23This code is directly inside the
  7718. 6:35:26homepage component. That means whenever
  7719. 6:35:29the homepage changes or rerenders, it
  7720. 6:35:32will run this code again and send
  7721. 6:35:34another request to the back end.
  7722. 6:35:37However, we just need to load the
  7723. 6:35:39products once on this page. We don't
  7724. 6:35:41need to load the products over and over
  7725. 6:35:44again. To make this code run once, we
  7726. 6:35:47can put it inside a use effect. At the
  7727. 6:35:51top, we're going to import curly
  7728. 6:35:54brackets use effect
  7729. 6:35:58from React.
  7730. 6:36:01And above this code, we're going to do
  7731. 6:36:04use effect brackets and give this a
  7732. 6:36:08function brackets arrow and curly
  7733. 6:36:10brackets.
  7734. 6:36:13Use effect lets us control when some
  7735. 6:36:16code runs. We're going to move this code
  7736. 6:36:20inside the use effect. So we'll cut and
  7737. 6:36:24in here we'll paste.
  7738. 6:36:27And I'll select these lines and press
  7739. 6:36:29tab to reformat.
  7740. 6:36:32By default, use effect runs this code
  7741. 6:36:35every time the component is created or
  7742. 6:36:38updated. But we can control this. At the
  7743. 6:36:41end of this function, we're going to add
  7744. 6:36:44a comma and an empty array. As a review,
  7745. 6:36:48this is called a dependency array. The
  7746. 6:36:52dependency array lets us control when
  7747. 6:36:54use effect runs. If we use an empty
  7748. 6:36:58array, that means this code will only
  7749. 6:37:01run once after the component is created.
  7750. 6:37:05And that's exactly what we need here. We
  7751. 6:37:08just need to load the products once on
  7752. 6:37:10this page. By the way, you might notice
  7753. 6:37:13in the console that use effect runs
  7754. 6:37:15twice instead of once. So, this is
  7755. 6:37:18totally normal and it's caused by using
  7756. 6:37:20strict mode in our app. Strict mode runs
  7757. 6:37:23use effect twice to help us catch bugs
  7758. 6:37:26and it only does this during
  7759. 6:37:28development.
  7760. 6:37:30So now that we have the data, how do we
  7761. 6:37:33use it in this loop down here to
  7762. 6:37:36generate the HTML?
  7763. 6:37:39To do that, we need to save the products
  7764. 6:37:41data inside the state. So at the top,
  7765. 6:37:45we're also going to import use state
  7766. 6:37:49from React.
  7767. 6:37:51And at the top of our component, we're
  7768. 6:37:53going to do const square bracket equals
  7769. 6:37:57use state and brackets.
  7770. 6:38:01As a review, use state gives us an array
  7771. 6:38:04with two values. The first value is the
  7772. 6:38:07name of the data. In this case, we'll
  7773. 6:38:10name it the products. And then we'll
  7774. 6:38:13type a comma. And the second value of
  7775. 6:38:16use state is the updater function. The
  7776. 6:38:19updater function lets us update this
  7777. 6:38:22value and then it will regenerate the
  7778. 6:38:24HTML. We usually name the updater
  7779. 6:38:27function set and then the name of the
  7780. 6:38:30data capital P products.
  7781. 6:38:34And finally between these brackets we're
  7782. 6:38:37going to put the starting value of the
  7783. 6:38:39products. Let's just use an empty array.
  7784. 6:38:43And now after we get the products from
  7785. 6:38:46the back end, we're going to save it
  7786. 6:38:48into the state.
  7787. 6:38:50So instead of console.log, we'll use the
  7788. 6:38:53updater function set products. This will
  7789. 6:38:58save the products data into here. And
  7790. 6:39:01then it's going to use this to generate
  7791. 6:39:04the HTML. So now we're using data from
  7792. 6:39:07the back end to generate the HTML. And
  7793. 6:39:11we don't need to import this file here
  7794. 6:39:13anymore.
  7795. 6:39:15So the only thing to note is that the
  7796. 6:39:17name of the data has to match the name
  7797. 6:39:20that we use down here. So that this code
  7798. 6:39:22is actually using the state.
  7799. 6:39:26Now let's save
  7800. 6:39:28and we'll check the website.
  7801. 6:39:31And everything still works except now
  7802. 6:39:34we're using data from the back end to
  7803. 6:39:36generate this HTML.
  7804. 6:39:39We'll go back to our code
  7805. 6:39:43and we'll save this into git. In the git
  7806. 6:39:46section, we'll create the message use
  7807. 6:39:49data from the back end to generate the
  7808. 6:39:54HTML and commit.
  7809. 6:40:02Next, if we look at the website
  7810. 6:40:05in the homepage, we also need to get the
  7811. 6:40:07cart from the back end because we need
  7812. 6:40:09to display this quantity number up here.
  7813. 6:40:13To do that, we'll go back to our code
  7814. 6:40:17and we're going to make another request
  7815. 6:40:19to the back end to get the cart. So,
  7816. 6:40:22below this request, we're going to use
  7817. 6:40:25axios again. Axios.get get brackets a
  7818. 6:40:30string and we'll give it the URL http
  7819. 6:40:34colhost
  7820. 6:40:38col 3000
  7821. 6:40:40slash API/cart-
  7822. 6:40:43items.
  7823. 6:40:45The way this works is the back end is
  7824. 6:40:48running on http localhost col 3000 and
  7825. 6:40:53after this we add a URL path. Each URL
  7826. 6:40:57path gives us different data. For
  7827. 6:41:00example, the URL path/ API/proucts
  7828. 6:41:04gives us the products data. The URL
  7829. 6:41:07path/ API/cart items gives us the cart
  7830. 6:41:11data. So the URL path can actually be
  7831. 6:41:14anything we want. Whoever creates the
  7832. 6:41:17backend decides what the URL paths will
  7833. 6:41:20be. In the description, you can find a
  7834. 6:41:23documentation page for my backend.
  7835. 6:41:27And this lists all the URL paths that we
  7836. 6:41:30can use. A common naming convention is
  7837. 6:41:33to start the URL path with slash API.
  7838. 6:41:37API means application programming
  7839. 6:41:40interface. Starting with / API makes it
  7840. 6:41:43clear that these URL paths are
  7841. 6:41:46specifically for interacting with the
  7842. 6:41:48backend.
  7843. 6:41:51So we already learned how to use /
  7844. 6:41:53API/roucts.
  7845. 6:41:55Now we're going to learn how to use /
  7846. 6:41:57API/cart items.
  7847. 6:42:01And remember whenever we make a request
  7848. 6:42:03to the back end it's going to take some
  7849. 6:42:05time to get a response. So we need to
  7850. 6:42:08wait for this code to finish using then
  7851. 6:42:13we'll give this a function
  7852. 6:42:15and the response is going to be saved in
  7853. 6:42:17here.
  7854. 6:42:20So when we make a request to /appi/cart
  7855. 6:42:23items, we're going to get the cart in
  7856. 6:42:25the response. So in here, let's
  7857. 6:42:28console.log
  7858. 6:42:30the response data to see what the cart
  7859. 6:42:34looks like. We'll save
  7860. 6:42:37and we'll check the console.
  7861. 6:42:40And you might need to refresh. And the
  7862. 6:42:43back end responded with an array. And
  7863. 6:42:46this array represents the cart that we
  7864. 6:42:49have on the website. So each item in the
  7865. 6:42:52cart contains the product ID that we
  7866. 6:42:55want to buy and the quantity of that
  7867. 6:42:58product. So next we're going to use this
  7868. 6:43:00to calculate the quantity and display it
  7869. 6:43:02on the page. So we'll go back to our
  7870. 6:43:05code and first we need to save this cart
  7871. 6:43:09into state so we can use it to create
  7872. 6:43:11the HTML.
  7873. 6:43:13At the top, we'll create another state
  7874. 6:43:16const brackets equals use state
  7875. 6:43:20brackets.
  7876. 6:43:22And this state will save the cart,
  7877. 6:43:25comma, and the updater function set
  7878. 6:43:28capital C cart.
  7879. 6:43:31And let's set the starting value of the
  7880. 6:43:33cart to an empty array. And we'll scroll
  7881. 6:43:37down. And now once we get the cart from
  7882. 6:43:40the back end, we're going to save it in
  7883. 6:43:43the state. So instead of console.log,
  7884. 6:43:46we're going to use the updater function
  7885. 6:43:48set cart. And now the cart from the back
  7886. 6:43:52end will be saved inside this variable.
  7887. 6:43:55And remember that the quantity for the
  7888. 6:43:57cart is inside the header. So we need to
  7889. 6:44:00pass the cart into the header using a
  7890. 6:44:03prop. So here we'll add a prop. cart
  7891. 6:44:07equals curly brackets and we'll give it
  7892. 6:44:10the cart.
  7893. 6:44:12Let's save this file
  7894. 6:44:14and we'll go into our code and go to
  7895. 6:44:18components header.jsx
  7896. 6:44:20and then inside the header we're going
  7897. 6:44:22to get the cart from the props. To do
  7898. 6:44:25that at the top, we'll type curly
  7899. 6:44:27brackets and the cart. As a reminder,
  7900. 6:44:31this is a shortcut for this code.
  7901. 6:44:34And now we're going to calculate the
  7902. 6:44:36total quantity in this cart. So at the
  7903. 6:44:39top, let's create a variable. Let total
  7904. 6:44:43quantity equals zero to start. And then
  7905. 6:44:47we'll loop through the cart using cart
  7906. 6:44:50for each brackets. And we'll give it a
  7907. 6:44:54function.
  7908. 6:44:58And the inner function will get one
  7909. 6:45:00parameter which is each item in the
  7910. 6:45:02cart. So, we'll call this the cart item.
  7911. 6:45:06And then each cart item has a quantity.
  7912. 6:45:09So, we're going to add that to this
  7913. 6:45:11total quantity up here. We'll do total
  7914. 6:45:15quantity plus equal the cart item.
  7915. 6:45:22And now we can scroll down
  7916. 6:45:25till we see the cart quantity down here.
  7917. 6:45:28And instead of three every time, we're
  7918. 6:45:30going to insert the total quantity
  7919. 6:45:35and save.
  7920. 6:45:37And now if we look at our project
  7921. 6:45:40now, it will display the total quantity
  7922. 6:45:43using data from the back end. So now
  7923. 6:45:46let's go back to our code.
  7924. 6:45:48And if we scroll up, you might see an
  7925. 6:45:51error here for props validation. Again,
  7926. 6:45:55this is an older feature of React. So,
  7927. 6:45:57we can disable this check by going into
  7928. 6:46:00our files and opening eslint.config.js.
  7929. 6:46:04And then we'll scroll down inside this
  7930. 6:46:07rules object. We're going to add a
  7931. 6:46:10string react/prop-
  7932. 6:46:14types colon the string off. So, make
  7933. 6:46:18sure it's inside this rules object. We
  7934. 6:46:22can save.
  7935. 6:46:24And now if we go back to the header, the
  7936. 6:46:26props validation error should be gone.
  7937. 6:46:29So now let's save this code into git.
  7938. 6:46:32We'll open the git section and we'll
  7939. 6:46:34create a message get cart from the back
  7940. 6:46:38end and display quantity and commit.
  7941. 6:46:49Next, if we go back to the homepage, so
  7942. 6:46:52in our files, go to pages and homepage
  7943. 6:46:57and scroll up. Notice that for each
  7944. 6:47:00request, we're sending it to localhost
  7945. 6:47:033000. So, we can actually set up a
  7946. 6:47:05shortcut in Vit. So, we don't have to
  7947. 6:47:08type localhost 3000 every time. So, we
  7948. 6:47:11can actually remove these
  7949. 6:47:16and save.
  7950. 6:47:19And then we're going to go to our files
  7951. 6:47:21and we're going to open vit.config.js.
  7952. 6:47:27And inside here we're going to add the
  7953. 6:47:29property server colon and an object. And
  7954. 6:47:33inside we'll add the property proxy
  7955. 6:47:37colon another object.
  7956. 6:47:39And in here we'll add a string slash API
  7957. 6:47:44colon. And we're going to give this an
  7958. 6:47:47object. And inside we'll add a target
  7959. 6:47:50property colon and the string http colon
  7960. 6:47:55double slash localhost colon 3000.
  7961. 6:48:01So this is called a server proxy
  7962. 6:48:03configuration. This just means that if
  7963. 6:48:06the URL path starts with / API like we
  7964. 6:48:10had it before, the request will
  7965. 6:48:12automatically go to localhost 3000. So
  7966. 6:48:15we don't have to type it every time.
  7967. 6:48:18Another thing we can set up is when we
  7968. 6:48:20load images, we also want to load them
  7969. 6:48:23from the back end. Remember the back end
  7970. 6:48:25should manage the data including the
  7971. 6:48:28product images. So in here we'll also
  7972. 6:48:31add a comma and add a string slash
  7973. 6:48:35images colon and an object
  7974. 6:48:39and we'll give it the same target. So
  7975. 6:48:41we'll select this and copy and in here
  7976. 6:48:45we'll paste
  7977. 6:48:47and save.
  7978. 6:48:50And we need to do one more thing. We're
  7979. 6:48:52going to go inside index.html HTML and
  7980. 6:48:55in the head we're going to add less than
  7981. 6:48:59base slashgreater than and we're going
  7982. 6:49:02to give this an attribute href equals
  7983. 6:49:05double quotes and a slash.
  7984. 6:49:08So this adds slash in front of any
  7985. 6:49:10relative urls. All the image URLs start
  7986. 6:49:14with images and this will convert them
  7987. 6:49:16into slash images so that our redirect
  7988. 6:49:20will work. Let's save
  7989. 6:49:23and we'll go back to the vit config.
  7990. 6:49:27So now when we get images, we're going
  7991. 6:49:29to get them from the back end. And now
  7992. 6:49:32because we changed the vit
  7993. 6:49:33configuration, it's a good idea to stop
  7994. 6:49:36the server with control c and then
  7995. 6:49:38restart the server using mpm rundev.
  7996. 6:49:43And now let's check the website to make
  7997. 6:49:46sure everything is working. So I'll just
  7998. 6:49:48refresh this to make sure. And
  7999. 6:49:50everything still looks good. So now
  8000. 6:49:53we'll go back to our code and we'll save
  8001. 6:49:55these changes into git. Inside git,
  8002. 6:49:59we're going to type the message add
  8003. 6:50:02server proxy config for slash API and
  8004. 6:50:08slash images
  8005. 6:50:11and commit.
  8006. 6:50:17Next, we're going to move on to the
  8007. 6:50:19checkout page. So, let's open the
  8008. 6:50:22website and we'll click the cart icon in
  8009. 6:50:25the top right
  8010. 6:50:27and we're going to work on this page.
  8011. 6:50:29So, on this page, we also need the cart
  8012. 6:50:32to generate this HTML.
  8013. 6:50:34So, we'll go back to our code
  8014. 6:50:38and we'll open our files and we're going
  8015. 6:50:41to open checkout page.jsx.
  8016. 6:50:44And now inside here we could do another
  8017. 6:50:47axios.get/appi/cart
  8018. 6:50:50again. But remember we already loaded
  8019. 6:50:53the cart on the homepage. It does not
  8020. 6:50:56make sense to reload the cart whenever
  8021. 6:50:59we switch pages. We just need to load
  8022. 6:51:02the cart once for the entire website. To
  8023. 6:51:05do that, we're going to go to the
  8024. 6:51:07homepage
  8025. 6:51:09and we're going to share this cart
  8026. 6:51:11between the two pages using the
  8027. 6:51:13technique lifting the state up. As a
  8028. 6:51:16review, this means we move the cart
  8029. 6:51:19state up to an outer component. In this
  8030. 6:51:22case, the outer component is the app
  8031. 6:51:26component here, which contains a
  8032. 6:51:28homepage. So, we're going to lift the
  8033. 6:51:31cart up into the app component. And from
  8034. 6:51:35here, we can share the cart between the
  8035. 6:51:37homepage and the checkout page. So,
  8036. 6:51:40first, let's go into the homepage again.
  8037. 6:51:43We're going to select this cart and cut
  8038. 6:51:48and go back to the app. And we're going
  8039. 6:51:51to lift it up in here by pasting it. And
  8040. 6:51:55we also need the code that loads the
  8041. 6:51:57cart. So in the homepage,
  8042. 6:52:00we're going to select this code and cut
  8043. 6:52:06and save
  8044. 6:52:08inside app.jsx.
  8045. 6:52:11Below this, we're going to paste.
  8046. 6:52:14And remember, we need to put this inside
  8047. 6:52:16a use effect so that it only runs once.
  8048. 6:52:19So use effect brackets, give it a
  8049. 6:52:22function,
  8050. 6:52:24and a empty array at the end. And we're
  8051. 6:52:28going to select it and cut. And inside
  8052. 6:52:31here, paste.
  8053. 6:52:34And I'll select these lines and press
  8054. 6:52:36tab to reformat them. And next, we'll
  8055. 6:52:40need to import this code. So at the top,
  8056. 6:52:43we're going to import curly brackets,
  8057. 6:52:47use state, and use effect
  8058. 6:52:52from React.
  8059. 6:52:55And we'll also import axios. So at the
  8060. 6:52:58top again, we're going to import axios
  8061. 6:53:03from the string axios.
  8062. 6:53:06So now that we have the cart, we're
  8063. 6:53:08going to share it between these two
  8064. 6:53:10pages using a prop. So for the homepage,
  8065. 6:53:13we'll add a prop. Cart equals curly
  8066. 6:53:16brackets and we'll give it the cart. And
  8067. 6:53:20for the checkout page as well, cart
  8068. 6:53:22equals curly brackets. and we'll save
  8069. 6:53:24the cart.
  8070. 6:53:27Let's save this file.
  8071. 6:53:29And we'll go into the homepage.
  8072. 6:53:32And at the top, we need to access the
  8073. 6:53:34cart from the props using curly
  8074. 6:53:36brackets, the cart, and save.
  8075. 6:53:41So, the code should work the same as
  8076. 6:53:42before, except we're just getting the
  8077. 6:53:44cart from the props. Now, let's open our
  8078. 6:53:47website to make sure it works. We're
  8079. 6:53:49going to go to the homepage and refresh.
  8080. 6:53:53And that looks good. We'll go back to
  8081. 6:53:55our code. So now that we lifted the
  8082. 6:53:59state up, we can just load the card once
  8083. 6:54:02for the entire website instead of
  8084. 6:54:04reloading it on every page. We'll open
  8085. 6:54:07the get section and we'll give it the
  8086. 6:54:10message lift cart up and share it
  8087. 6:54:16between pages and commit.
  8088. 6:54:25Next, we're going to use the cart to
  8089. 6:54:28generate the HTML for the checkout page.
  8090. 6:54:31So, first make sure that we pass the
  8091. 6:54:33cart to the checkout page using this
  8092. 6:54:35prop. And now we're going to open the
  8093. 6:54:38checkout page, which is here. And we're
  8094. 6:54:42going to access the cart from the props
  8095. 6:54:44using curly brackets and the cart.
  8096. 6:54:48And I will use this cart to generate the
  8097. 6:54:50HTML down here. Now inside this code,
  8098. 6:54:54we're going to press Ctrl+F to find some
  8099. 6:54:57code. And we're going to search for
  8100. 6:54:59cart- item-container.
  8101. 6:55:03And here we're going to collapse this
  8102. 6:55:05element. And we'll collapse this element
  8103. 6:55:08as well.
  8104. 6:55:10So each of these represents a cart item
  8105. 6:55:13on the page. So let's generate this
  8106. 6:55:16using the cart. Above this we're going
  8107. 6:55:19to insert cart map brackets and we'll
  8108. 6:55:24give it a function brackets arrow and
  8109. 6:55:27curly brackets. And this inner function
  8110. 6:55:30gets one parameter which is the cart
  8111. 6:55:33item. And for each item we're going to
  8112. 6:55:36return brackets some HTML.
  8113. 6:55:40So for each cart item, we want to
  8114. 6:55:42generate this HTML. So let's just select
  8115. 6:55:45it and copy. And inside here, we're
  8116. 6:55:49going to paste.
  8117. 6:55:52And to fix the formatting, we're going
  8118. 6:55:53to press control or commandshiftp
  8119. 6:55:56and run format document.
  8120. 6:56:00Now let's scroll up.
  8121. 6:56:03And remember, when we loop through an
  8122. 6:56:05array, each element needs a key
  8123. 6:56:08property.
  8124. 6:56:11Each cart item has a product ID which is
  8125. 6:56:14unique. So we're going to save the key
  8126. 6:56:16as cart item.
  8127. 6:56:20ID.
  8128. 6:56:21And now instead of showing the same
  8129. 6:56:24product every time, we're going to
  8130. 6:56:26insert the details from each cart item.
  8131. 6:56:29However, there is a problem here.
  8132. 6:56:31Remember that the cart looks like this.
  8133. 6:56:34It doesn't have any product data on it.
  8134. 6:56:38To fix this, we're going to go to the
  8135. 6:56:40app component where we load the cart.
  8136. 6:56:44And at the end of this URL, we can add
  8137. 6:56:47question mark expand equals product.
  8138. 6:56:52So this is called a query parameter. A
  8139. 6:56:55query parameter lets us add additional
  8140. 6:56:58information to our request. When the
  8141. 6:57:00backend receives this query parameter,
  8142. 6:57:03it's going to add product details to the
  8143. 6:57:06cart. So, it's going to expand the cart
  8144. 6:57:09with product details. So, now let's save
  8145. 6:57:12this file
  8146. 6:57:14and we're going to open the website and
  8147. 6:57:17we're going to go to the checkout page
  8148. 6:57:20and we're going to open the console. So,
  8149. 6:57:22we'll rightclick inspect
  8150. 6:57:25and inside the console we're going to
  8151. 6:57:27open the network tab.
  8152. 6:57:30So, I'm going to remove any search that
  8153. 6:57:32I have. And in here, we can see all the
  8154. 6:57:36requests that we make to the back end
  8155. 6:57:38and the responses. So, by the way,
  8156. 6:57:41requests only show up here if the
  8157. 6:57:43console was open before the request. So,
  8158. 6:57:45we may need to refresh
  8159. 6:57:48to get the request to show up in the
  8160. 6:57:50network tab. And now we can see our
  8161. 6:57:52request to cart- items. If you don't see
  8162. 6:57:56it, you may need to search for it up
  8163. 6:57:58here by typing cart. And we have it
  8164. 6:58:01here. So we can open this by clicking it
  8165. 6:58:04and then go into the response tab. And
  8166. 6:58:08here you can see the response from the
  8167. 6:58:10back end. So now you can see that each
  8168. 6:58:12cart item has a property called product.
  8169. 6:58:16And this contains all the product
  8170. 6:58:18details that we need for this page.
  8171. 6:58:22So now let's go back to our code.
  8172. 6:58:26And we're going to go to the checkout
  8173. 6:58:27page.
  8174. 6:58:29And now instead of the socks every time,
  8175. 6:58:32we're going to remove this
  8176. 6:58:35and insert the cart item.
  8177. 6:58:42And we'll also replace the image. So
  8178. 6:58:45we'll remove this and insert the cart
  8179. 6:58:48item.
  8180. 6:58:51image and down here we're going to
  8181. 6:58:54insert the price. Now, if we go to the
  8182. 6:58:57homepage,
  8183. 6:58:59remember that in the homepage, we wrote
  8184. 6:59:02some code here to display the price. So,
  8185. 6:59:05let's actually move this code into a
  8186. 6:59:07function so we can reuse it. Inside our
  8187. 6:59:10files, we're going to click the source
  8188. 6:59:13folder and we'll create a new folder and
  8189. 6:59:16let's call it utils for utilities.
  8190. 6:59:21And inside the utmost folder, we'll
  8191. 6:59:23create a new file and we'll name it
  8192. 6:59:26money.js.
  8193. 6:59:29So this file contains utilities or code
  8194. 6:59:31that helps us work with money. So in
  8195. 6:59:34here, let's create a function and we'll
  8196. 6:59:37name it format money brackets and curly
  8197. 6:59:41brackets.
  8198. 6:59:43And we're going to copy the code from
  8199. 6:59:45the homepage. So, inside the homepage,
  8200. 6:59:48we're going to take all of this code and
  8201. 6:59:50copy it.
  8202. 6:59:53And inside money.js, inside here, we'll
  8203. 6:59:56paste it.
  8204. 6:59:58And now we're going to turn this into a
  8205. 7:00:00string. At the front, we'll type backick
  8206. 7:00:03and at the end a back tick to create a
  8207. 7:00:06template string. And remember in
  8208. 7:00:08template strings to insert something, we
  8209. 7:00:10need dollar curly brackets. And now,
  8210. 7:00:14instead of getting the product.pricent
  8211. 7:00:15price cents. We're going to pass it into
  8212. 7:00:18this function. So, let's name the
  8213. 7:00:20parameter amount cents. And we're going
  8214. 7:00:24to remove this and replace it with
  8215. 7:00:27amount cents. So, this function lets us
  8216. 7:00:31convert any amount cents into this
  8217. 7:00:33format. At the end, let's return this
  8218. 7:00:37value from the function. and we'll
  8219. 7:00:40export it so we can use it in other
  8220. 7:00:42files and save.
  8221. 7:00:46So now we can go back to the homepage
  8222. 7:00:49and at the top we're going to import
  8223. 7:00:53curly brackets the format money function
  8224. 7:00:58that we just created from a string and
  8225. 7:01:01we're going to locate the utils folder.
  8226. 7:01:04So, we'll go out of this folder with dot
  8227. 7:01:06dot slash utils slashmoney.
  8228. 7:01:10And now we'll scroll back down
  8229. 7:01:14and instead of this code, we're going to
  8230. 7:01:17remove it and insert format money
  8231. 7:01:21brackets and we'll give it product.pric
  8232. 7:01:25sets and save.
  8233. 7:01:28And now let's reuse this function in the
  8234. 7:01:30checkout page. So, we'll open the
  8235. 7:01:33checkout page, and at the top, we're
  8236. 7:01:37going to import curly brackets, the
  8237. 7:01:41format money function from a string, and
  8238. 7:01:46we're going to locate this file. And for
  8239. 7:01:49me, I'll go out of this folder using dot
  8240. 7:01:51dot slash utils/money.
  8241. 7:01:55Now, if you did the exercises from the
  8242. 7:01:57previous lesson, this file might be
  8243. 7:01:59inside a folder called checkout. In that
  8244. 7:02:02case, make sure you have two dot dot
  8245. 7:02:04slash in front. And now we're going to
  8246. 7:02:07scroll back down
  8247. 7:02:11and we're going to replace this money
  8248. 7:02:13with curly brackets.
  8249. 7:02:16Format money brackets. And we're going
  8250. 7:02:20to give it the cart item.
  8251. 7:02:24price sets and we'll replace the
  8252. 7:02:27quantity. So, insert
  8253. 7:02:30cart item dot quantity and save.
  8254. 7:02:37And now if we go back to the website,
  8255. 7:02:40now we are generating these cart items
  8256. 7:02:43using data from the back end. And this
  8257. 7:02:46was the default HTML we had before. So
  8258. 7:02:49now we can go back to our code and we
  8259. 7:02:52can scroll down
  8260. 7:02:55and we can remove the default HTML
  8261. 7:02:59and save.
  8262. 7:03:02So now let's save these changes into
  8263. 7:03:04git. We'll open the get section
  8264. 7:03:08and at the top we'll type the message
  8265. 7:03:11generate
  8266. 7:03:12HTML for the cart items and commit.
  8267. 7:03:23Next, we're going to generate the HTML
  8268. 7:03:25for the delivery options. So, we'll go
  8269. 7:03:28back to the checkout page and we'll
  8270. 7:03:31scroll to the top of this component. And
  8271. 7:03:34here we'll load the delivery options
  8272. 7:03:36from the back end. So first let's import
  8273. 7:03:40Axios. At the top we'll import Axios
  8274. 7:03:45from the Axios npm package. And we'll
  8275. 7:03:50also need import curly brackets. Use
  8276. 7:03:54state and use effect to save the
  8277. 7:03:58delivery options. So import it from
  8278. 7:04:00React.
  8279. 7:04:02And now inside the component
  8280. 7:04:05we'll create some state to save the
  8281. 7:04:07delivery options const
  8282. 7:04:10bracket equals use state and brackets
  8283. 7:04:15and we'll name this delivery options
  8284. 7:04:19colon and the updater function set
  8285. 7:04:22delivery options
  8286. 7:04:25and we'll set the starting value to an
  8287. 7:04:27empty array. Next, we'll get the
  8288. 7:04:30delivery options from the back end. So
  8289. 7:04:32here we'll do use effect brackets and
  8290. 7:04:37we'll give it a function brackets arrow
  8291. 7:04:39and curly brackets. And at the end we'll
  8292. 7:04:42give it a comma and an empty dependency
  8293. 7:04:44array so that this code only runs once
  8294. 7:04:47on this page. And inside here we're
  8295. 7:04:50going to call axials.get
  8296. 7:04:52get brackets and the URL to get the
  8297. 7:04:56delivery options is slash API slashd
  8298. 7:05:00delivery-options
  8299. 7:05:03and then we're going to wait for the
  8300. 7:05:04response to come back using then
  8301. 7:05:08brackets and give it a function brackets
  8302. 7:05:11arrow and curly brackets and inside the
  8303. 7:05:14function the response from the back end
  8304. 7:05:16will be saved as the response parameter.
  8305. 7:05:20So once we get this response, we're
  8306. 7:05:22going to save it in the delivery options
  8307. 7:05:24up here using set delivery options
  8308. 7:05:30brackets
  8309. 7:05:31response.
  8310. 7:05:34Now let's save this code and we're going
  8311. 7:05:38to check the network tab to see if it
  8312. 7:05:40works. So, we'll open the console and
  8313. 7:05:43we'll open the network tab. And we might
  8314. 7:05:45need to refresh to make sure we get the
  8315. 7:05:48request and we'll remove this and search
  8316. 7:05:51for delivery options. And we'll click
  8317. 7:05:54one of these and then look at the
  8318. 7:05:57response tab. So, the backend responded
  8319. 7:06:00with three delivery options. Each
  8320. 7:06:03delivery option has the number of days
  8321. 7:06:05it takes to deliver and the price in
  8322. 7:06:08cents. However, if we open our website,
  8323. 7:06:12notice that each delivery option also
  8324. 7:06:14has a estimated delivery time. So, we
  8325. 7:06:17could use this delivery days to
  8326. 7:06:20calculate this date on the front end.
  8327. 7:06:23But remember, it's generally the
  8328. 7:06:25backend's job to manage the data and do
  8329. 7:06:28these calculations.
  8330. 7:06:29So, if we go back to our code,
  8331. 7:06:33this URL path also supports a query
  8332. 7:06:36parameter called expand. So at the end
  8333. 7:06:39we'll add question mark expand equals
  8334. 7:06:43and I called it estimated
  8335. 7:06:47delivery time.
  8336. 7:06:50So when the back end sees this expand
  8337. 7:06:52parameter it will calculate the
  8338. 7:06:54estimated delivery time for us and then
  8339. 7:06:56give it back in our response. If we save
  8340. 7:07:01and check the console
  8341. 7:07:04and refresh
  8342. 7:07:07and open the response and now you'll see
  8343. 7:07:10that each delivery option now has an
  8344. 7:07:12estimated delivery time in milliseconds.
  8345. 7:07:16So now we'll go back to our code and we
  8346. 7:07:19have what we need to display the date.
  8347. 7:07:22As you can see it's generally the
  8348. 7:07:24backend's job to manage the data and to
  8349. 7:07:26do the calculations. And now the front
  8350. 7:07:29end's job is to present that data in an
  8351. 7:07:32easy to read way on the website.
  8352. 7:07:35So now let's use these delivery options
  8353. 7:07:38to generate the HTML. We're going to
  8354. 7:07:41click in this file and press Ctrl+F to
  8355. 7:07:43search for code. And we'll search for
  8356. 7:07:45delivery dash options.
  8357. 7:07:49And inside here we should see an element
  8358. 7:07:51with a class name delivery option. So
  8359. 7:07:54we're going to collapse this element and
  8360. 7:07:57also collapse the other delivery options
  8361. 7:08:00and each of these represents one
  8362. 7:08:02delivery option on the page. So let's
  8363. 7:08:05generate this HTML using React. Above
  8364. 7:08:08this we'll insert and we'll run the code
  8365. 7:08:12delivery options dot map brackets and
  8366. 7:08:17give it a function brackets arrow and
  8367. 7:08:19curly brackets and the parameter will be
  8368. 7:08:22each delivery option
  8369. 7:08:26and here we're going to return brackets
  8370. 7:08:30some HTML for each delivery option. So
  8371. 7:08:33we want to return this HTML. So we can
  8372. 7:08:36select it and copy. And inside here
  8373. 7:08:39we'll paste.
  8374. 7:08:42And we can fix the formatting using
  8375. 7:08:44control or commandshiftp
  8376. 7:08:46and press format document.
  8377. 7:08:50And let's scroll up. And as always, we
  8378. 7:08:53need a key prop whenever we're looping
  8379. 7:08:55through an array. So we'll add key
  8380. 7:08:58equals curly brackets. And each delivery
  8381. 7:09:01option has a unique ID. So we'll just
  8382. 7:09:03set it to the delivery option
  8383. 7:09:07ID.
  8384. 7:09:08Next, we're going to replace these
  8385. 7:09:10values here. So first we'll do the
  8386. 7:09:13delivery date. Currently, we only have
  8387. 7:09:16the delivery time in milliseconds. So
  8388. 7:09:19how do we display it as a date like
  8389. 7:09:21this? To do that, we can use another npm
  8390. 7:09:25package called Dayjs.
  8391. 7:09:28So, we're going to go to the terminal
  8392. 7:09:29and go to the tab that's running our
  8393. 7:09:31front end. And we can press Ctrl C to
  8394. 7:09:34stop the server. And we're going to npm
  8395. 7:09:38install dayjs. And we'll type at to give
  8396. 7:09:42it a version. And we're going to click
  8397. 7:09:44this link in the description to get the
  8398. 7:09:46version of day.js we're going to use.
  8399. 7:09:49And we'll copy that version and paste it
  8400. 7:09:51in here and press enter to install
  8401. 7:09:55day.js.
  8402. 7:09:57And after that we're going to npm rundev
  8403. 7:10:00to restart the server.
  8404. 7:10:04So the dayjs package helps us work with
  8405. 7:10:07dates and it helps us display a date in
  8406. 7:10:10a particular format. To use it, we're
  8407. 7:10:13going to scroll to the top and at the
  8408. 7:10:15top we're going to import dayjs
  8409. 7:10:20from the package day js.
  8410. 7:10:24And now we'll scroll back down or we can
  8411. 7:10:27control F and search for delivery
  8412. 7:10:29options.
  8413. 7:10:32And we'll scroll down here. And now
  8414. 7:10:35above this date, we're going to insert.
  8415. 7:10:38And we're going to run day JS and
  8416. 7:10:42brackets. And we're going to give Day.js
  8417. 7:10:44DJs a time in milliseconds which we
  8418. 7:10:47actually have inside delivery option dot
  8419. 7:10:52estimated
  8420. 7:10:54delivery time ms or milliseconds.
  8421. 7:10:59And now that we wrapped this in dayjs we
  8422. 7:11:02can format it into this format. So after
  8423. 7:11:06this bracket here, we're going to use
  8424. 7:11:08the method dot format and brackets and
  8425. 7:11:12give it a string. To show the time in
  8426. 7:11:15this format, we're going to type D four
  8427. 7:11:18times,
  8428. 7:11:20which is the day of the week. And then
  8429. 7:11:22we'll type a comma to show this comma
  8430. 7:11:25and a space. And we'll type capital M
  8431. 7:11:28four times
  8432. 7:11:30to show the month and a space. and we'll
  8433. 7:11:33type capital D which will show this
  8434. 7:11:36date. And now we can remove this code.
  8435. 7:11:41Next, we're going to replace this price.
  8436. 7:11:44If the price is free, we'll show free
  8437. 7:11:46shipping. Otherwise, we're going to show
  8438. 7:11:48the price dash shipping. So, we're going
  8439. 7:11:51to scroll up to the inside of this loop
  8440. 7:11:54here,
  8441. 7:11:56and we're going to create a variable for
  8442. 7:11:58the price. So, we'll do let price string
  8443. 7:12:03equals a string. And to make it easier,
  8444. 7:12:07we're going to set this to a default of
  8445. 7:12:10free shipping.
  8446. 7:12:13And then we're going to overwrite this
  8447. 7:12:15if we have a price. So, below this,
  8448. 7:12:19we're going to check if brackets the
  8449. 7:12:22delivery option.
  8450. 7:12:26Sense is greater than zero.
  8451. 7:12:29curly brackets. So if it is not free,
  8452. 7:12:32we're going to overwrite this price
  8453. 7:12:36string equals a template string
  8454. 7:12:39and inside we're going to insert and
  8455. 7:12:42remember we have the format money
  8456. 7:12:44function to display the price. So as I
  8457. 7:12:47here we'll call format money and
  8458. 7:12:51brackets and we're going to give it the
  8459. 7:12:54delivery option price sets.
  8460. 7:12:59And at the end we're going to add a dash
  8461. 7:13:02and shipping.
  8462. 7:13:05And now we can scroll back down
  8463. 7:13:08and instead of free shipping every time
  8464. 7:13:10we're going to insert the price string.
  8465. 7:13:15Next, we're going to scroll up a bit and
  8466. 7:13:18there's a few things that we need to
  8467. 7:13:19replace for this input type radio
  8468. 7:13:22element. So, this represents the
  8469. 7:13:25selector and there's a few special
  8470. 7:13:27attributes that we need to replace. The
  8471. 7:13:29first one is the name attribute. So, the
  8472. 7:13:32name groups the selectors together. So,
  8473. 7:13:36each set of selectors should have a
  8474. 7:13:38unique name. This makes sure that for
  8475. 7:13:41each product we can only select one of
  8476. 7:13:43them. So here instead of doing delivery
  8477. 7:13:46option one every time we're going to
  8478. 7:13:48replace this with a product ID because
  8479. 7:13:51each set of selectors should belong to
  8480. 7:13:54one product.
  8481. 7:13:56So in this code we're going to replace
  8482. 7:13:58it using curly brackets
  8483. 7:14:01and change this to a template string
  8484. 7:14:03using a back tick and a back tick and
  8485. 7:14:06instead of one every time we're going to
  8486. 7:14:08insert and we'll insert the product ID.
  8487. 7:14:12Now in this code we actually have access
  8488. 7:14:14to the cart item from above. So inside
  8489. 7:14:17here we can just insert the cart item.
  8490. 7:14:22ID.
  8491. 7:14:24Next we also need to update this checked
  8492. 7:14:26property. So this determines if the
  8493. 7:14:29selector is checked. So we don't want
  8494. 7:14:32the selector to be checked every time.
  8495. 7:14:35Each cart item has a delivery option ID
  8496. 7:14:38and this is the one that should be
  8497. 7:14:40checked. So, inside here, we're going to
  8498. 7:14:43add a new line. And we're going to make
  8499. 7:14:45check equal and insert some code. And
  8500. 7:14:49we're going to compare if this delivery
  8501. 7:14:52option do ID is equal to the cart items.
  8502. 7:15:00Option ID.
  8503. 7:15:02So only the delivery option that matches
  8504. 7:15:05the delivery option ID in the cart will
  8505. 7:15:08be checked. So that's all the things
  8506. 7:15:10that we have to replace in the delivery
  8507. 7:15:12options. So now let's scroll down and we
  8508. 7:15:16can remove this default code
  8509. 7:15:20and save
  8510. 7:15:22and let's check the website.
  8511. 7:15:25And now we're generating these delivery
  8512. 7:15:28options and the correct ones are checked
  8513. 7:15:31and we have the estimated delivery dates
  8514. 7:15:33for each displayed in an easyto read
  8515. 7:15:36format.
  8516. 7:15:37The last thing we need to generate is
  8517. 7:15:39this delivery date up here. So, we'll go
  8518. 7:15:42back to the code
  8519. 7:15:44and we're going to scroll up
  8520. 7:15:47um to the cart item and we're going to
  8521. 7:15:50find the cart item container and this
  8522. 7:15:52delivery date here. So, this date is
  8523. 7:15:55determined by whichever delivery option
  8524. 7:15:57is selected. So, first we need to find
  8525. 7:16:00the delivery option that is selected.
  8526. 7:16:03So inside this loop, we'll create a
  8527. 7:16:06variable const for the selected
  8528. 7:16:10delivery option and equals.
  8529. 7:16:14And to find this, we're going to get the
  8530. 7:16:17delivery options and then use the method
  8531. 7:16:21dot find and brackets.
  8532. 7:16:24And find gets a function. So brackets,
  8533. 7:16:27arrow, and curly brackets.
  8534. 7:16:30And in this inner function, it receives
  8535. 7:16:33each delivery option. So we'll call it
  8536. 7:16:36delivery option.
  8537. 7:16:38And I'll just add a new line here and
  8538. 7:16:41select this code and press tab to format
  8539. 7:16:44it a little bit.
  8540. 7:16:46So the way that find works is it's going
  8541. 7:16:49to loop through each delivery option.
  8542. 7:16:51And the first function here that returns
  8543. 7:16:53true is going to be the result. So
  8544. 7:16:56that's how we find something. And then
  8545. 7:16:57it's going to save it in this variable.
  8546. 7:17:00So inside here we want to return the
  8547. 7:17:04delivery option id that is equal to the
  8548. 7:17:09cart items
  8549. 7:17:12dot delivery option id. So the delivery
  8550. 7:17:16option that matches what is inside the
  8551. 7:17:18cart item will return true and that's
  8552. 7:17:21going to be saved in here. And now we
  8553. 7:17:24can replace this date. So we'll type
  8554. 7:17:27curly brackets and we'll use day js
  8555. 7:17:30brackets and the selected
  8556. 7:17:34delivery option dot estimated delivery
  8557. 7:17:40time ms
  8558. 7:17:43and outside this bracket we'll use a
  8559. 7:17:45method dot format brackets and give it a
  8560. 7:17:49string and to get this date format we'll
  8561. 7:17:52give it d four times a comma and capital
  8562. 7:17:58M four times space and capital D.
  8563. 7:18:03And now we can remove this date here.
  8564. 7:18:07And before we run this code, there is
  8565. 7:18:09one small problem here because remember
  8566. 7:18:11the delivery options start out as empty.
  8567. 7:18:14So if they are empty, we might not find
  8568. 7:18:17the selected delivery option. To fix
  8569. 7:18:20this in front of this code, we're going
  8570. 7:18:22to check if delivery options.length
  8571. 7:18:27is greater than zero and type and and
  8572. 7:18:32as a review. This works like an if
  8573. 7:18:34statement. It will check if delivery
  8574. 7:18:36options.length is greater than zero and
  8575. 7:18:39then only if it's true, it will run the
  8576. 7:18:42rest of the code. So now we can save
  8577. 7:18:46and check the website.
  8578. 7:18:48And now we're generating everything here
  8579. 7:18:51using data from the back end. So let's
  8580. 7:18:54go back to our code
  8581. 7:18:56and we'll save this into git. Inside the
  8582. 7:18:59git section, we'll type the message
  8583. 7:19:03generate the HTML for the delivery
  8584. 7:19:09options
  8585. 7:19:10and commit.
  8586. 7:19:16Next, if we go back to the website,
  8587. 7:19:19we're going to generate the HTML for the
  8588. 7:19:21payment summary here. Now, we could
  8589. 7:19:24calculate all these numbers inside
  8590. 7:19:26React. However, we're going to use the
  8591. 7:19:29back end to calculate these numbers.
  8592. 7:19:32Remember, the backend's job is to manage
  8593. 7:19:34the data and to do the calculations. The
  8594. 7:19:37front end's job is to present the data
  8595. 7:19:40in an easy to read way. So let's go back
  8596. 7:19:43to our code and we'll go back to the
  8597. 7:19:45checkout page and we'll scroll to the
  8598. 7:19:48top and we're going to create a state
  8599. 7:19:51for the payment summary. So here we'll
  8600. 7:19:54type const square brackets equals use
  8601. 7:19:58state and brackets
  8602. 7:20:01and we'll name this payment summary,
  8603. 7:20:06and the updater function set payment
  8604. 7:20:09summary.
  8605. 7:20:11And as a starting value, we're going to
  8606. 7:20:14use null because the payment summary is
  8607. 7:20:17going to be an object. And it's a little
  8608. 7:20:19easier to check if the object is not
  8609. 7:20:21loaded if we set it to null at the
  8610. 7:20:23start.
  8611. 7:20:24Next, we're going to use Axios to get
  8612. 7:20:27the payment summary from the back end.
  8613. 7:20:29So down here, we'll use Axios.get get
  8614. 7:20:35brackets and we'll give it the URL path
  8615. 7:20:38slash API slash payment-
  8616. 7:20:42summary
  8617. 7:20:43and we'll use dot then to wait for a
  8618. 7:20:46response and give it a function
  8619. 7:20:50and inside here we can get the response.
  8620. 7:20:54Once we get the response, we're going to
  8621. 7:20:56use set payment summary brackets
  8622. 7:21:02response data to save the data inside
  8623. 7:21:06here. So now let's save and check the
  8624. 7:21:09response from the back end. So we'll go
  8625. 7:21:11to our website and open the console and
  8626. 7:21:15open the network tab and we'll refresh
  8627. 7:21:17just in case. We can click this to
  8628. 7:21:20remove the search and type payment
  8629. 7:21:24and we get the payment summary. So we
  8630. 7:21:26can click this and you can see that in
  8631. 7:21:29the response the back end calculated all
  8632. 7:21:31the numbers that we need. So now we just
  8633. 7:21:33need to present this on the page.
  8634. 7:21:36Let's go back and we'll press CtrlF in
  8635. 7:21:40this file and we'll search for payment
  8636. 7:21:44dash summary and press enter to find the
  8637. 7:21:48next result. And this section represents
  8638. 7:21:51the payment summary on the page. And now
  8639. 7:21:55we'll replace these numbers with the
  8640. 7:21:56numbers we got from the back end. So
  8641. 7:21:59first we'll replace the total quantity
  8642. 7:22:01here using curly brackets and we'll
  8643. 7:22:04replace it with payment summary dot
  8644. 7:22:07total items.
  8645. 7:22:10And here we'll replace the product cost.
  8646. 7:22:14I'll type a new line to make it a little
  8647. 7:22:16easier to read. And we'll insert.
  8648. 7:22:19And remember for money we need to format
  8649. 7:22:21it using the function format money
  8650. 7:22:25brackets. and we'll get the payment
  8651. 7:22:29summary dot product cost set.
  8652. 7:22:35We'll scroll down and we'll replace this
  8653. 7:22:37next number. So we'll remove it and new
  8654. 7:22:40line and curly brackets and we'll run
  8655. 7:22:43format money again brackets and this one
  8656. 7:22:47is a shipping cost which is payment
  8657. 7:22:50summary dot shipping cost sense.
  8658. 7:22:55Let's keep going. And this is the total
  8659. 7:22:58before tax. So we'll replace this and
  8660. 7:23:02insert format money brackets
  8661. 7:23:06payment summary dot total cost before
  8662. 7:23:12tax cents.
  8663. 7:23:14And we'll scroll down here and replace
  8664. 7:23:17this. insert format money brackets the
  8665. 7:23:23payment summary dot tax sets
  8666. 7:23:27and then down here we're going to
  8667. 7:23:29replace this one and insert format money
  8668. 7:23:35brackets the payment summary dot total
  8669. 7:23:39cost sense
  8670. 7:23:42and finally we only want to display this
  8671. 7:23:45if the payment summary is loaded
  8672. 7:23:48otherwise we don't have this
  8673. 7:23:49information. So above this code,
  8674. 7:23:53we're going to type curly brackets and
  8675. 7:23:56we'll check if payment summary and and
  8676. 7:24:00brackets.
  8677. 7:24:02So this checks if the payment summary
  8678. 7:24:04object exists. If it's null at the
  8679. 7:24:06start, it's not going to run the rest of
  8680. 7:24:08the code. And now we're going to put all
  8681. 7:24:11of this code inside here. So we'll
  8682. 7:24:13select all this code
  8683. 7:24:16up to the place order button and we'll
  8684. 7:24:19cut
  8685. 7:24:22and inside here we're going to paste.
  8686. 7:24:27Now a problem here is that we're
  8687. 7:24:28returning multiple elements inside this
  8688. 7:24:31brackets. So to fix that we need to wrap
  8689. 7:24:33this in a fragment using less than
  8690. 7:24:36greater than and remove this for now.
  8691. 7:24:40And at the end
  8692. 7:24:42after the button we'll do less than
  8693. 7:24:44slashgreater than. So we're returning
  8694. 7:24:47one element inside this brackets. And
  8695. 7:24:50now we'll format our code using control
  8696. 7:24:52or commandshiftp
  8697. 7:24:54and run format document
  8698. 7:24:58and save.
  8699. 7:25:00And that's it. Now we're generating the
  8700. 7:25:03payment summary using data from the back
  8701. 7:25:05end. We'll go to our website. And that
  8702. 7:25:08looks good. So now let's go back to our
  8703. 7:25:11code and we'll save this into git. In
  8704. 7:25:15the git section, we're going to type the
  8705. 7:25:17message generate HTML for the payment
  8706. 7:25:24summary and commit.
  8707. 7:25:31Next, we're going to work on the orders
  8708. 7:25:33page. So, we'll go back to our files
  8709. 7:25:37and we're going to open the orders page.
  8710. 7:25:41So, first of all, the orders page has a
  8711. 7:25:44header component. And remember, the
  8712. 7:25:46header now needs the cart in order to
  8713. 7:25:49display the quantity. So, we need to
  8714. 7:25:51share the cart with the orders page
  8715. 7:25:53first. We'll go to our files and open
  8716. 7:25:57app.jsx.
  8717. 7:26:00And we'll scroll down. And here we'll
  8718. 7:26:02share the cart with the orders page
  8719. 7:26:04using a prop cart equals curly brackets.
  8720. 7:26:08And we'll save the cart and save.
  8721. 7:26:12And now we'll open the orders page
  8722. 7:26:14again. And at the top we're going to
  8723. 7:26:16access the prop using curly brackets and
  8724. 7:26:19cart. And we're going to pass this cart
  8725. 7:26:22into the header using cart equals curly
  8726. 7:26:26brackets and the cart.
  8727. 7:26:29Let's save.
  8728. 7:26:31and we'll open the website and we're
  8729. 7:26:34going to go to the orders page. So,
  8730. 7:26:36first we'll click here to go to the
  8731. 7:26:38homepage and then we'll click here to
  8732. 7:26:41open the orders page and it should be
  8733. 7:26:43working. So, now we're going to generate
  8734. 7:26:46this HTML using data from the back end.
  8735. 7:26:49So, we'll go back to the orders page and
  8736. 7:26:53at the top we're going to import what we
  8737. 7:26:56need. So first we'll import axios from
  8738. 7:27:01Axios and we'll also import curly
  8739. 7:27:04brackets use state and use effect from
  8740. 7:27:10react.
  8741. 7:27:12And now down here first we'll create
  8742. 7:27:15some state to save the orders using
  8743. 7:27:18const square brackets equals use state
  8744. 7:27:22and brackets.
  8745. 7:27:24And we'll call this data the orders,
  8746. 7:27:27comma, and the updater function set
  8747. 7:27:31orders.
  8748. 7:27:33And at the start, we'll set the orders
  8749. 7:27:35to an empty array. And below this, we're
  8750. 7:27:38going to make a request to the back end
  8751. 7:27:41using use effect brackets and a function
  8752. 7:27:46brackets arrow and curly brackets. And
  8753. 7:27:48down here, we'll give it a empty array
  8754. 7:27:51so that it only runs once. And inside
  8755. 7:27:55we're going to use axios.get
  8756. 7:27:58again. So brackets and the URL path for
  8757. 7:28:01the orders is slappi/ord
  8758. 7:28:05orders
  8759. 7:28:06and then we'll do dot then to wait for
  8760. 7:28:09the response from the back end. We'll
  8761. 7:28:12give this a function brackets arrow and
  8762. 7:28:14curly brackets. Inside this parameter is
  8763. 7:28:17the response from the back end. And once
  8764. 7:28:22we get the response, we're going to save
  8765. 7:28:24it inside orders using set orders
  8766. 7:28:28brackets
  8767. 7:28:30response
  8768. 7:28:31data. Let's save.
  8769. 7:28:35And now we can check the network tab to
  8770. 7:28:38see if we got the orders. We'll open the
  8771. 7:28:40console.
  8772. 7:28:42Inside the network tab, we can refresh
  8773. 7:28:46and remove this and search for orders.
  8774. 7:28:50And down here, we'll click this request
  8775. 7:28:53and go to the response tab. And the back
  8776. 7:28:56end gave us the orders that we need to
  8777. 7:28:59display. Now, a problem here is that we
  8778. 7:29:02only have the product ID in the order,
  8779. 7:29:05but we also need to display some product
  8780. 7:29:07details on the website. And again, the
  8781. 7:29:10backend supports an expand parameter for
  8782. 7:29:13this URL path. So at the end here, we'll
  8783. 7:29:16add question mark expand equals
  8784. 7:29:21products. This time, make sure you have
  8785. 7:29:23an s at the end. So now let's save
  8786. 7:29:28and check the network tab again. And
  8787. 7:29:31we'll refresh.
  8788. 7:29:34And now inside the orders request,
  8789. 7:29:36you'll see that each order has the
  8790. 7:29:38product ID, but it also has the other
  8791. 7:29:41data for the product. And this is what
  8792. 7:29:44we need to display on the website.
  8793. 7:29:47So now we'll go into our code and we're
  8794. 7:29:49going to generate the HTML. So first
  8795. 7:29:52we'll press control or command F and
  8796. 7:29:55we're going to search for order dash
  8797. 7:29:58container
  8798. 7:30:00which is down here. And we can collapse
  8799. 7:30:03this code and collapse the other order
  8800. 7:30:06container. Each of these represents an
  8801. 7:30:09order on the website. So, we're going to
  8802. 7:30:12generate this HTML.
  8803. 7:30:14At the top, we'll insert and we'll run
  8804. 7:30:17orders.m map brackets. And we'll give it
  8805. 7:30:21a function.
  8806. 7:30:23And this parameter is going to be each
  8807. 7:30:25order.
  8808. 7:30:28And for each order, we're going to
  8809. 7:30:30return some HTML.
  8810. 7:30:33So, we want each order to be converted
  8811. 7:30:35into this HTML. We're going to select it
  8812. 7:30:38and copy it inside here. We'll paste.
  8813. 7:30:43Press control or command shiftp and run
  8814. 7:30:46format document.
  8815. 7:30:49And we'll scroll back up.
  8816. 7:30:52As always, we need a key prop when we're
  8817. 7:30:54looping through an array. We'll set the
  8818. 7:30:57key equals. And each order has an order
  8819. 7:31:01ID property.
  8820. 7:31:03As usual, we're going to replace the
  8821. 7:31:05values in here. Feel free to pause if
  8822. 7:31:07you want to try it yourself first. So
  8823. 7:31:10for the delivery date, we're going to
  8824. 7:31:13insert and we're going to use dayjs
  8825. 7:31:17brackets and each order has an order dot
  8826. 7:31:21order time ms
  8827. 7:31:24and outside this bracket we use dot
  8828. 7:31:26format brackets and a string. And to
  8829. 7:31:30display the date, we're going to use
  8830. 7:31:32four capital M's for the month and a
  8831. 7:31:35capital D for the date. So now we also
  8832. 7:31:38need to import day.js. So we'll scroll
  8833. 7:31:41to the top and at the top we'll import
  8834. 7:31:45dayjs from a string dayjs.
  8835. 7:31:50And let's also import format money
  8836. 7:31:52because we're going to use it later. So,
  8837. 7:31:54import curly brackets format money from
  8838. 7:31:59a string dot dot slashyouutills/
  8839. 7:32:03money.
  8840. 7:32:05Now, we're going to scroll back down
  8841. 7:32:08and we're going to replace the total
  8842. 7:32:10order cost. So here we're going to
  8843. 7:32:12remove this and curly brackets we're
  8844. 7:32:15going to insert format money brackets
  8845. 7:32:20and give it the order dot total cost
  8846. 7:32:24sense.
  8847. 7:32:26We'll scroll down and here we're going
  8848. 7:32:28to replace this with the order ID. So
  8849. 7:32:31curly brackets order ID.
  8850. 7:32:36Next, inside this element order details
  8851. 7:32:39grid. This displays the list of products
  8852. 7:32:42in the order and we're going to generate
  8853. 7:32:44this using React as well. So above this
  8854. 7:32:49we're going to use curly brackets and
  8855. 7:32:52we're going to get the order products
  8856. 7:32:55array and we're going to loop through it
  8857. 7:32:57using map brackets and give it a
  8858. 7:33:00function
  8859. 7:33:02and save each product
  8860. 7:33:05and for each product we're going to
  8861. 7:33:07return brackets some HTML.
  8862. 7:33:11So if we collapse this code and collapse
  8863. 7:33:14this element and this element and we
  8864. 7:33:17also collapse these.
  8865. 7:33:21So three of these elements represents
  8866. 7:33:23one product. It's a little different
  8867. 7:33:26because we don't have an element that
  8868. 7:33:28contains all of these because we're
  8869. 7:33:30using a grid here, but it works in a
  8870. 7:33:33similar way. We're going to copy this
  8871. 7:33:36code and inside return we're going to
  8872. 7:33:39paste
  8873. 7:33:41and the one difference here is that if
  8874. 7:33:43we scroll up
  8875. 7:33:45we're returning multiple elements from
  8876. 7:33:47this function. So we need to wrap these
  8877. 7:33:49in a fragment. So at the top we'll type
  8878. 7:33:53less than greater than. But then we have
  8879. 7:33:55another problem here which is remember
  8880. 7:33:58when we loop through an array each
  8881. 7:34:00element that we return should have a key
  8882. 7:34:02property and we can't add props to
  8883. 7:34:05fragments like this. To solve this we
  8884. 7:34:08need to use the full fragment syntax. So
  8885. 7:34:11we're going to scroll to the top and for
  8886. 7:34:14React we're going to import using comma
  8887. 7:34:18the fragment component.
  8888. 7:34:21So we'll scroll back down and in here
  8889. 7:34:24we're going to use a full fragment
  8890. 7:34:26syntax
  8891. 7:34:28and remove this. So this does the same
  8892. 7:34:31thing except now we can add a key prop
  8893. 7:34:34to it equals curly brackets and let's
  8894. 7:34:37set the prop to the product ID.
  8895. 7:34:42and we'll scroll to the bottom.
  8896. 7:34:45And before these brackets, remember to
  8897. 7:34:47add the closing fragment tag.
  8898. 7:34:52And finally, we'll press control or
  8899. 7:34:54commandshiftp
  8900. 7:34:56and format document.
  8901. 7:34:58And now we can replace this default code
  8902. 7:35:01that we have down here.
  8903. 7:35:06Finally, we're going to replace each
  8904. 7:35:08product's details. So here we're going
  8905. 7:35:12to replace the name and insert the
  8906. 7:35:14product name. And here let's also
  8907. 7:35:18replace the image
  8908. 7:35:20curly brackets. Replace it with product.
  8909. 7:35:25And actually I made a mistake. I forgot
  8910. 7:35:27that all the product details are saved
  8911. 7:35:29in a property called product. So let's
  8912. 7:35:32actually rename this to order product.
  8913. 7:35:37And then here we're going to do the
  8914. 7:35:39order product dotproduct ID and the
  8915. 7:35:44order product.product.im
  8916. 7:35:46image and the order
  8917. 7:35:49product.product.name.
  8918. 7:35:52And then down here to replace the
  8919. 7:35:54delivery date. This is directly on the
  8920. 7:35:57order product. So we can remove this and
  8921. 7:36:01insert and we'll use dayjs and bracket
  8922. 7:36:05and give it the order product dot
  8923. 7:36:08estimated delivery time ms and outside
  8924. 7:36:13these brackets we'll do dot format
  8925. 7:36:17brackets and a string and we'll use the
  8926. 7:36:20format capital M four times for the
  8927. 7:36:24month and capital D for the date. And
  8928. 7:36:27then down here for the quantity, we're
  8929. 7:36:30going to insert and the quantity is
  8930. 7:36:33directly on the order product dot
  8931. 7:36:36quantity
  8932. 7:36:38and save.
  8933. 7:36:40And now if we scroll down, we can also
  8934. 7:36:44remove the rest of this default HTML
  8935. 7:36:48and save this file. And now let's check
  8936. 7:36:52our website.
  8937. 7:36:54And everything should be working except
  8938. 7:36:56we're generating the HTML for these
  8939. 7:36:58orders using data from the back end. So
  8940. 7:37:01now let's go back and we'll save this
  8941. 7:37:04into git. We'll open the git section
  8942. 7:37:08and we'll give it the message generate
  8943. 7:37:11the HTML for the orders page and commit.
  8944. 7:37:22Next, if we look at our code right now,
  8945. 7:37:25you'll notice we have a lot of code in
  8946. 7:37:27one file. To make our code cleaner, we
  8947. 7:37:30should separate our code into smaller
  8948. 7:37:33components. So, let's go to our files
  8949. 7:37:36and we're going to start with the
  8950. 7:37:38homepage.
  8951. 7:37:40So, inside the homepage, we're showing a
  8952. 7:37:43grid of products. To make the code
  8953. 7:37:46cleaner, we can separate this into its
  8954. 7:37:48own component. So, we'll go to the pages
  8955. 7:37:51folder and we'll click it and we're
  8956. 7:37:54going to create a new file and let's
  8957. 7:37:57call it the products grid.jsx.
  8958. 7:38:04And now we have multiple components that
  8959. 7:38:07are related to the homepage. So, usually
  8960. 7:38:10we organize this into a folder. So,
  8961. 7:38:13inside here, we're going to create a new
  8962. 7:38:15folder and we're going to call it home
  8963. 7:38:19and press enter.
  8964. 7:38:21And we group all the components related
  8965. 7:38:23to the homepage inside here. So, we'll
  8966. 7:38:26move the homepage.jsx.
  8967. 7:38:29And let's update the imports
  8968. 7:38:32and also homepage.css
  8969. 7:38:36and also the products grid.
  8970. 7:38:41And whenever we move files, we need to
  8971. 7:38:43make sure the imports are still correct.
  8972. 7:38:45So in homepage, I'm going to scroll up
  8973. 7:38:48and we should make sure that we have two
  8974. 7:38:51dot dot slashes in front because now
  8975. 7:38:53we're inside an additional folder. So
  8976. 7:38:55that looks good to me. And we're also
  8977. 7:38:58going to go to app.jsx
  8978. 7:39:01and scroll up and make sure that the
  8979. 7:39:04homepage is now inside the home folder.
  8980. 7:39:07So now let's save and check our website
  8981. 7:39:11and go to the homepage to make sure it
  8982. 7:39:14still works. So now let's go back to our
  8983. 7:39:17code and we're going to separate the
  8984. 7:39:19products grid into its own file. So in
  8985. 7:39:22here we'll create the component using
  8986. 7:39:25export function
  8987. 7:39:28products grid brackets and curly
  8988. 7:39:32brackets and we'll return some HTML
  8989. 7:39:36and we're just going to copy the HTML to
  8990. 7:39:38here. So, inside the homepage, we're
  8991. 7:39:41going to scroll down to the products
  8992. 7:39:44grid and we can collapse this element
  8993. 7:39:48and select all this code and copy
  8994. 7:39:52inside the products grid. Inside here,
  8995. 7:39:55we can paste
  8996. 7:39:58and press control or commandshiftp and
  8997. 7:40:01format document.
  8998. 7:40:03Now, we'll scroll up and we're going to
  8999. 7:40:05fix these errors. So first we need
  9000. 7:40:08access to the products. So we can pass
  9001. 7:40:10those in as a prop curly brackets
  9002. 7:40:14products.
  9003. 7:40:15And down here we also need the format
  9004. 7:40:18money function. So at the top we're
  9005. 7:40:22going to import curly brackets format
  9006. 7:40:27money
  9007. 7:40:28from a string and we'll find that utils
  9008. 7:40:31file. So we need to get out of two
  9009. 7:40:33folders this time using dot dot slash
  9010. 7:40:36dot dot slash utils/money
  9011. 7:40:40and save.
  9012. 7:40:42And now inside the homepage at the top
  9013. 7:40:46we're going to import our products grid.
  9014. 7:40:48So import curly brackets
  9015. 7:40:51the products grid
  9016. 7:40:55from a string and it's right beside the
  9017. 7:40:58homepage. So we can just do dot slash
  9018. 7:41:01products grid.
  9019. 7:41:04And then down here we can remove this
  9020. 7:41:06HTML
  9021. 7:41:09and just use our component less than
  9022. 7:41:12products grid slashgreater than and we
  9023. 7:41:16need to give it the products. So we'll
  9024. 7:41:18create a prop products equals curly
  9025. 7:41:22brackets and we'll give it the products.
  9026. 7:41:26So, as you can see, this makes the code
  9027. 7:41:28much cleaner when we separate it into
  9028. 7:41:30smaller components. The last step is at
  9029. 7:41:33the top, we can remove this format money
  9030. 7:41:36function since we're not using it
  9031. 7:41:38anymore. Let's save
  9032. 7:41:41and let's open the website and make sure
  9033. 7:41:44everything still works. So, that looks
  9034. 7:41:47good.
  9035. 7:41:48Next, we're going to do the checkout
  9036. 7:41:50page. So, we'll go back to our code
  9037. 7:41:54and we're going to open the checkout
  9038. 7:41:56page and we're going to scroll to the
  9039. 7:41:59top.
  9040. 7:42:02And I'll leave separating the checkout
  9041. 7:42:04header as an exercise. But we're going
  9042. 7:42:07to scroll down here and we can move the
  9043. 7:42:10order summary section into its own
  9044. 7:42:12component. So, inside the pages folder,
  9045. 7:42:16I'm going to click it and create a new
  9046. 7:42:18folder called checkout.
  9047. 7:42:21Now, you might have already done this if
  9048. 7:42:23you did the exercises from the previous
  9049. 7:42:25lesson, but we're going to use this
  9050. 7:42:27folder checkout. And I'm going to move
  9051. 7:42:29the checkout page inside here.
  9052. 7:42:33And update the imports and also move the
  9053. 7:42:36CSS files.
  9054. 7:42:40And I'll make sure that the imports look
  9055. 7:42:42good. So, in here at the top, make sure
  9056. 7:42:46there's two dot dot slashes in front.
  9057. 7:42:49And inside app.jsx,
  9058. 7:42:52make sure that there is a checkout
  9059. 7:42:53folder in front of this component. So
  9060. 7:42:56I'll go back to the checkout page and
  9061. 7:43:00I'll create a new file. Let's call it
  9062. 7:43:03the order summary.jsx.
  9063. 7:43:09And we're going to create export
  9064. 7:43:12function order summary brackets and
  9065. 7:43:16curly brackets. and we're going to
  9066. 7:43:18return brackets the HTML for the order
  9067. 7:43:23summary. Inside the checkout page, we're
  9068. 7:43:26going to scroll down
  9069. 7:43:29and find the order summary. We can
  9070. 7:43:31collapse it and select this code and
  9071. 7:43:35copy.
  9072. 7:43:36Inside the order summary, we'll paste
  9073. 7:43:40and press control or command shift P and
  9074. 7:43:43format document.
  9075. 7:43:46and we'll scroll to the top and we're
  9076. 7:43:48going to need the delivery options and
  9077. 7:43:50the cart inside here. So, we're going to
  9078. 7:43:53get this from the props using curly
  9079. 7:43:55brackets, the cart, comma, and the
  9080. 7:43:58delivery options.
  9081. 7:44:01And we also need dayjs and the format
  9082. 7:44:04money function. So at the top we'll also
  9083. 7:44:07import dayjs
  9084. 7:44:09from day js and import curly brackets
  9085. 7:44:15format money
  9086. 7:44:18from a string
  9087. 7:44:21and we'll find the utils file which is
  9088. 7:44:24dot dot slash dot dot slash utils
  9089. 7:44:28slashmoney
  9090. 7:44:30and all the errors should now be gone.
  9091. 7:44:33We'll save.
  9092. 7:44:35And inside checkout page, we can remove
  9093. 7:44:38this code.
  9094. 7:44:40And we can use our new component less
  9095. 7:44:42than order summary /grater than. And we
  9096. 7:44:48need to give it the cart equals curly
  9097. 7:44:50brackets and save the cart. And we need
  9098. 7:44:53to give it the delivery options equals
  9099. 7:44:57curly brackets. And give it the delivery
  9100. 7:45:01options. And we need to import this at
  9101. 7:45:04the top. So we'll scroll up and here we
  9102. 7:45:08can import curly brackets the order
  9103. 7:45:12summary
  9104. 7:45:14from a string dot slash order summary.
  9105. 7:45:21And we don't need dayjs anymore.
  9106. 7:45:24So let's save
  9107. 7:45:26and check the website. And everything
  9108. 7:45:29still works.
  9109. 7:45:32We'll go back
  9110. 7:45:33and scroll down.
  9111. 7:45:36And next, we can also move the payment
  9112. 7:45:38summary into its own component. Feel
  9113. 7:45:40free to pause the video if you want to
  9114. 7:45:42try it yourself first. So, inside the
  9115. 7:45:45checkout folder, we'll click here and
  9116. 7:45:47create a new file, payment summary.jsx,
  9117. 7:45:52and we'll create the component export
  9118. 7:45:55function payment summary brackets and
  9119. 7:45:59curly brackets. and we'll return the
  9120. 7:46:02HTML.
  9121. 7:46:03Inside the checkout page, we're going to
  9122. 7:46:06collapse this element and select the
  9123. 7:46:08HTML and copy
  9124. 7:46:11inside the payment summary. We're going
  9125. 7:46:14to paste
  9126. 7:46:15control or command shiftp format
  9127. 7:46:18document
  9128. 7:46:19and we're going to scroll back up and
  9129. 7:46:22we're going to get the payment summary
  9130. 7:46:24from the props. So curly brackets
  9131. 7:46:27payment summary and we need the format
  9132. 7:46:30money function. So up here import curly
  9133. 7:46:34brackets format money from a string dot
  9134. 7:46:39dot slash dot do/youutils/money
  9135. 7:46:43and save.
  9136. 7:46:46In the checkout page, we can remove this
  9137. 7:46:49code and replace it with less than
  9138. 7:46:52payment summary slashgreater than. And
  9139. 7:46:56we need to give this the payment summary
  9140. 7:47:00equals curly brackets and save the
  9141. 7:47:02payment summary.
  9142. 7:47:06And as you can see, our code is much
  9143. 7:47:08cleaner now because we separated it into
  9144. 7:47:10smaller components. So now we need to
  9145. 7:47:13import this. So, we're going to scroll
  9146. 7:47:15to the top and here we'll import curly
  9147. 7:47:19brackets the payment summary
  9148. 7:47:23from a string dot slashpayment
  9149. 7:47:26summary.
  9150. 7:47:28And we don't need format money anymore
  9151. 7:47:32and save.
  9152. 7:47:35Let's go back to the website and
  9153. 7:47:37everything is still working.
  9154. 7:47:40We'll go back to our code
  9155. 7:47:43and we're going to go inside the order
  9156. 7:47:46summary component.
  9157. 7:47:48So in here the code is still a little
  9158. 7:47:50bit too complicated. So what we can do
  9159. 7:47:53is we can find this element with the
  9160. 7:47:56class delivery options and we can
  9161. 7:47:58separate this into its own component as
  9162. 7:48:00well. Feel free to pause the video if
  9163. 7:48:02you want to try this yourself first. So,
  9164. 7:48:05I'm going to go to the checkout folder
  9165. 7:48:07and create a new file and name it
  9166. 7:48:10delivery options.jsx.
  9167. 7:48:15And we'll create a component export
  9168. 7:48:18function delivery options brackets and
  9169. 7:48:22curly brackets and return some HTML. And
  9170. 7:48:26back to the order summary. And here we
  9171. 7:48:29can collapse this element and select it
  9172. 7:48:33and copy.
  9173. 7:48:34Inside delivery options inside here we
  9174. 7:48:38can paste. Control or command shiftp and
  9175. 7:48:42format document.
  9176. 7:48:44And we'll scroll to the top. Here we
  9177. 7:48:47need to get the delivery options using
  9178. 7:48:49the props. So curly brackets delivery
  9179. 7:48:52options.
  9180. 7:48:54And we're going to import format money.
  9181. 7:48:57So up here we're going to import curly
  9182. 7:49:00brackets format money from a string dot
  9183. 7:49:05dot slash dot dot slash utils/money
  9184. 7:49:09and we also need dayjs. So up here we'll
  9185. 7:49:13import dayjs from a string dayjs
  9186. 7:49:19and I think we also need another thing
  9187. 7:49:22here which is the cart item. So we're
  9188. 7:49:24going to get this from the props as
  9189. 7:49:26well.
  9190. 7:49:26So up here we're also going to get the
  9191. 7:49:30cart item and save.
  9192. 7:49:34And now in the order summary we can
  9193. 7:49:37replace all this code with our component
  9194. 7:49:40less than delivery options /grater than.
  9195. 7:49:45And we're going to give it the cart item
  9196. 7:49:48equals curly brackets. save the cart
  9197. 7:49:50item and give it the delivery options
  9198. 7:49:55equals curly brackets save the delivery
  9199. 7:49:58options
  9200. 7:50:00and we also need to import this. So at
  9201. 7:50:03the top
  9202. 7:50:05we can import curly brackets the
  9203. 7:50:09delivery options
  9204. 7:50:12from a string
  9205. 7:50:14dot slash delivery options
  9206. 7:50:18and save
  9207. 7:50:20and let's check our website and make
  9208. 7:50:22sure it still works. So that looks good
  9209. 7:50:25to me. So if we go back to our code, you
  9210. 7:50:29can see that our code is cleaner now
  9211. 7:50:31after we separate this into another
  9212. 7:50:33component. And remember, it's up to us
  9213. 7:50:36how we want to separate our components.
  9214. 7:50:38We can actually keep separating this
  9215. 7:50:40into smaller components if we want. For
  9216. 7:50:43example, we can also have a cart item
  9217. 7:50:46details component or a delivery date
  9218. 7:50:48component, but I'll leave these as
  9219. 7:50:50exercises. I'm also going to leave the
  9220. 7:50:53orders page as an exercise, but we can
  9221. 7:50:56just get started on it. So, here inside
  9222. 7:50:59the pages folder, we're going to create
  9223. 7:51:02a new folder for the orders page. So,
  9224. 7:51:05orders
  9225. 7:51:07and we're going to move this component
  9226. 7:51:09inside here
  9227. 7:51:12and update the imports.
  9228. 7:51:14And we're going to move this inside here
  9229. 7:51:16as well. And just make sure the imports
  9230. 7:51:19look okay. So in here, we'll scroll up.
  9231. 7:51:22Make sure that these two have two dot
  9232. 7:51:25dot slashes in front. That looks good.
  9233. 7:51:28And in app.jsx,
  9234. 7:51:30make sure that the orders page is now
  9235. 7:51:32inside an orders folder. So we'll save
  9236. 7:51:36this and open the website.
  9237. 7:51:39And we're going to click here to go to
  9238. 7:51:41home and click here to go to orders. And
  9239. 7:51:44the orders page should still be working.
  9240. 7:51:47So I'll leave separating this as an
  9241. 7:51:49exercise. We'll go back to our code and
  9242. 7:51:52we'll save this into git. Inside the git
  9243. 7:51:56section, we'll create the message
  9244. 7:51:59separate into smaller components and
  9245. 7:52:04commit.
  9246. 7:52:10Next, we're going to go back to our
  9247. 7:52:12files and we're going to go to our
  9248. 7:52:15homepage.
  9249. 7:52:17and we're going to review async await.
  9250. 7:52:21Async await is a feature of JavaScript
  9251. 7:52:24and it lets us write asynchronous code
  9252. 7:52:26like normal code. Right now we're using
  9253. 7:52:30promises which means we need to use to
  9254. 7:52:33wait for this code to finish and then
  9255. 7:52:36run this function with async await. We
  9256. 7:52:39can actually write this like normal
  9257. 7:52:42code. To do that in front of axios we
  9258. 7:52:45can type await and space. So this is
  9259. 7:52:50going to wait for this code to finish
  9260. 7:52:53and then it's going to move on to the
  9261. 7:52:55next line. And now because we're waiting
  9262. 7:52:58for this code to finish, we can actually
  9263. 7:53:01save this result in a variable. So at
  9264. 7:53:03the front we can save it in const
  9265. 7:53:07response equals await this code.
  9266. 7:53:12And now we don't need to use dot then
  9267. 7:53:14anymore
  9268. 7:53:16and we can remove it.
  9269. 7:53:19However, in order to use a weight, we
  9270. 7:53:22need to be inside an async function. So
  9271. 7:53:25in front of this function, we need to
  9272. 7:53:27add async and space. As you can see,
  9273. 7:53:32async8 lets us write asynchronous code
  9274. 7:53:36like normal code or code that runs line
  9275. 7:53:39by line. And async await makes the code
  9276. 7:53:42easier to understand. So in react it's
  9277. 7:53:46recommended to use async await instead
  9278. 7:53:48of promises when we can. However, in
  9279. 7:53:52React there is a small problem with
  9280. 7:53:54using async await and use effect. As a
  9281. 7:53:58review, when we put async in front of a
  9282. 7:54:01function, this function will now return
  9283. 7:54:04a promise. That's because this function
  9284. 7:54:07has some asynchronous code inside or
  9285. 7:54:10code that takes some time to finish. So
  9286. 7:54:12that means that this entire function is
  9287. 7:54:15going to take some time to finish.
  9288. 7:54:17That's why it has to return a promise.
  9289. 7:54:20Now the problem is the inner function in
  9290. 7:54:22use effect should not return a promise.
  9291. 7:54:26It should only return nothing or a
  9292. 7:54:28cleanup function like this. A cleanup
  9293. 7:54:31function is useful if we want to do some
  9294. 7:54:33cleanup when this component is removed.
  9295. 7:54:36So because this inner function is now
  9296. 7:54:38returning a promise, it's sort of
  9297. 7:54:40breaking the rules of use effect. To
  9298. 7:54:44solve this, we usually create a new
  9299. 7:54:46function inside use effect like const
  9300. 7:54:50get home data equals and create a new
  9301. 7:54:54function brackets arrow and curly
  9302. 7:54:57brackets. And we're going to make this
  9303. 7:54:59function async. And we'll leave this as
  9304. 7:55:03an ordinary function.
  9305. 7:55:06And now we're going to move our code
  9306. 7:55:08inside here. So we'll cut it and paste
  9307. 7:55:11it. And now because we're inside an
  9308. 7:55:13async function, we can now use a weight
  9309. 7:55:16and it will also not mess up use effect.
  9310. 7:55:19At the end, we just need to run this
  9311. 7:55:21function using get home data and
  9312. 7:55:26brackets.
  9313. 7:55:28So this gets the same data as before but
  9314. 7:55:31it uses async await. So remember when
  9315. 7:55:34using async await inside use effect we
  9316. 7:55:37need to create a new function and then
  9317. 7:55:40run the function otherwise it will break
  9318. 7:55:42the rules of use effect. So let's save
  9319. 7:55:47and check the homepage
  9320. 7:55:50and go to the homepage and everything
  9321. 7:55:52still works. So let's go back
  9322. 7:55:56and we'll use async await in the app
  9323. 7:55:59component
  9324. 7:56:01where we get the cart. So here feel free
  9325. 7:56:05to pause if you want to try yourself
  9326. 7:56:07first. We'll create a new function using
  9327. 7:56:10const. Let's call it fetch app data
  9328. 7:56:15equals a function brackets arrow and
  9329. 7:56:18curly brackets.
  9330. 7:56:20And we'll move this code into the
  9331. 7:56:22function. So we'll cut and paste and
  9332. 7:56:26we'll select this and press tab.
  9333. 7:56:29In front of the function we'll use
  9334. 7:56:32async. And now we can use await inside.
  9335. 7:56:36So in front of axios we'll type await to
  9336. 7:56:39wait for this to finish. And then we're
  9337. 7:56:42going to save the result in a variable
  9338. 7:56:44const response equals this result.
  9339. 7:56:49And now we can remove
  9340. 7:56:51and these brackets
  9341. 7:56:56and the code is much cleaner. Finally,
  9342. 7:56:58we're going to run this function fetch
  9343. 7:57:01app data and brackets and save.
  9344. 7:57:06And let's do the same thing in the
  9345. 7:57:08checkout page. So we'll go to the
  9346. 7:57:10checkout page here.
  9347. 7:57:12Feel free to pause if you want to try it
  9348. 7:57:14yourself. const fetch checkout data
  9349. 7:57:19equals brackets arrow and curly brackets
  9350. 7:57:23and we'll move this code inside cut
  9351. 7:57:28and in here paste and select these lines
  9352. 7:57:32and press tab and we're going to turn
  9353. 7:57:35this into an async function so we can
  9354. 7:57:38use await inside. So in front of this
  9355. 7:57:41request, we're going to await.
  9356. 7:57:44And we're going to save it in a variable
  9357. 7:57:46let response
  9358. 7:57:49equals. So we're going to use let
  9359. 7:57:51because we have two requests here and we
  9360. 7:57:53might want to reuse this variable. And
  9361. 7:57:56I'll add a new line to make it a little
  9362. 7:57:58easier to read. And now we can remove.
  9363. 7:58:05And then down here, we'll also use await
  9364. 7:58:08to wait for this to finish. And we're
  9365. 7:58:11going to save the result inside response
  9366. 7:58:14equals.
  9367. 7:58:16And now we can remove then.
  9368. 7:58:21And the last step is to run this
  9369. 7:58:23function. So fetch checkout data and
  9370. 7:58:28brackets and save.
  9371. 7:58:31We'll open the website. Make sure the
  9372. 7:58:34homepage is working and then go to the
  9373. 7:58:36checkout page and make sure this is
  9374. 7:58:38still working. So that looks good to me.
  9375. 7:58:42We'll go back to our code
  9376. 7:58:44and that's async await. It lets us write
  9377. 7:58:47asynchronous code like normal code. I'll
  9378. 7:58:51leave converting the orders page to
  9379. 7:58:53async aait as an exercise. So now we can
  9380. 7:58:56save this into git.
  9381. 7:58:59We'll give it the message convert home
  9382. 7:59:03and checkout
  9383. 7:59:05to async await and commit.
  9384. 7:59:14In the next lesson, we're going to learn
  9385. 7:59:16data mutation, which means instead of
  9386. 7:59:19just getting data from the back end,
  9387. 7:59:21we'll update data in the back end, and
  9388. 7:59:24we'll be able to add products to our
  9389. 7:59:26cart and create orders. And that's the
  9390. 7:59:29end of this lesson. In this lesson, we
  9391. 7:59:32reviewed backend, which manages the data
  9392. 7:59:35for a website. We set up a backend for
  9393. 7:59:38our project. We learned how to fetch
  9394. 7:59:41data or get data from the back end. We
  9395. 7:59:44learned Axios, which is an easier way to
  9396. 7:59:47make requests to the back end. We
  9397. 7:59:49learned how to generate the HTML using
  9398. 7:59:52the data from the back end. We separated
  9399. 7:59:55our pages into smaller components and we
  9400. 7:59:59learned async a weight which lets us
  9401. 8:00:01write asynchronous code like normal
  9402. 8:00:04code. Here are some exercises to help
  9403. 8:00:07you practice what we learned in this
  9404. 8:00:09lesson.
  9405. 8:01:03In this lesson, we're going to learn
  9406. 8:01:05data mutation and we're going to make
  9407. 8:01:07the website interactive. Data mutation
  9408. 8:01:10means we update data in the back end. If
  9409. 8:01:14we open the website
  9410. 8:01:16and I'll close some of these other tabs.
  9411. 8:01:20If we go to the homepage,
  9412. 8:01:23notice that right now we can only view
  9413. 8:01:26the data. If we try to update this data,
  9414. 8:01:29like we try to add to cart, it doesn't
  9415. 8:01:32do anything. So, we're going to make
  9416. 8:01:34this add to cart button work. We'll go
  9417. 8:01:37to our code
  9418. 8:01:39and we're going to find the add to cart
  9419. 8:01:41button. So, I'll open my files and I'm
  9420. 8:01:45going to go to the homepage and we're
  9421. 8:01:48going to open the products grid.
  9422. 8:01:51And we're going to scroll down in this
  9423. 8:01:52file to this add to cart button. The
  9424. 8:01:57first step is to make this button
  9425. 8:01:59interactive. So at the end we're going
  9426. 8:02:02to add an on click attribute equals
  9427. 8:02:05curly brackets and give this a function
  9428. 8:02:08brackets arrow and curly brackets.
  9429. 8:02:12As a review on click is called an event
  9430. 8:02:15handler. When we click this button it
  9431. 8:02:17will run this function.
  9432. 8:02:20Now, inside here, we're going to add the
  9433. 8:02:22product to the cart. So, we could try to
  9434. 8:02:25update the cart here in React, but then
  9435. 8:02:28we have to figure out where to save the
  9436. 8:02:30cart. Instead, in the real world, we use
  9437. 8:02:34the backend to update the cart and to
  9438. 8:02:37save the cart. The advantage of using
  9439. 8:02:39the backend is it will simplify our
  9440. 8:02:42React code and we can share the data in
  9441. 8:02:45the back end. So if we access this
  9442. 8:02:47website from another computer, we can
  9443. 8:02:49access the updated cart using the back
  9444. 8:02:52end.
  9445. 8:02:53So here to use a backend to add a
  9446. 8:02:56product to the cart. We're just going to
  9447. 8:02:58make another request to the back end.
  9448. 8:03:02We're going to go to the top
  9449. 8:03:04and at the top we're going to import
  9450. 8:03:07axios so that we can make another
  9451. 8:03:09request.
  9452. 8:03:12and we'll scroll back down.
  9453. 8:03:15And when we click this button, we're
  9454. 8:03:17going to make a request to the back end
  9455. 8:03:19using axios. And this time we're going
  9456. 8:03:22to use post and brackets.
  9457. 8:03:26So previously we used axios.get
  9458. 8:03:30to get data from the backend. Post is a
  9459. 8:03:34different type of request.
  9460. 8:03:37Is used to create data in the back end.
  9461. 8:03:40In this case, we are adding a product to
  9462. 8:03:43the back end. So, we're creating a new
  9463. 8:03:45cart item. That's why we use post.
  9464. 8:03:49Inside here, we're going to give it a
  9465. 8:03:51URL path slash API/cart-
  9466. 8:03:56items. So, notice this is the same URL
  9467. 8:04:00path that we used to get the cart. But
  9468. 8:04:03when we use post, the backend will
  9469. 8:04:05behave differently. This will create a
  9470. 8:04:08cart item instead of getting the cart
  9471. 8:04:10items.
  9472. 8:04:12Next, we need to tell the backend which
  9473. 8:04:14product to add to the cart. To do that,
  9474. 8:04:17we can give axios.post another value
  9475. 8:04:21which is an object.
  9476. 8:04:24When we use post, we can also send
  9477. 8:04:27information to the back end using an
  9478. 8:04:30object. And this is called the request
  9479. 8:04:32body. Now the back end needs to know two
  9480. 8:04:36things in order to add a product to the
  9481. 8:04:38cart. First, it needs to know the
  9482. 8:04:41product ID that we want to add. In this
  9483. 8:04:46case, this button is for if we scroll
  9484. 8:04:49up, the button is for this product right
  9485. 8:04:53here. So, at the bottom,
  9486. 8:04:57we're going to tell the back end we want
  9487. 8:04:58to add this product
  9488. 8:05:01ID to the cart. And we also need another
  9489. 8:05:05information which is the quantity that
  9490. 8:05:07we want to add. For now, let's just use
  9491. 8:05:10the quantity one. So this code will send
  9492. 8:05:13a request to the back end telling it to
  9493. 8:05:16add this product to the cart with a
  9494. 8:05:19quantity of one.
  9495. 8:05:21So now let's save this file
  9496. 8:05:24and then we'll go to the website
  9497. 8:05:28and we're going to go to this t-shirt
  9498. 8:05:30product and we're going to click add to
  9499. 8:05:32cart.
  9500. 8:05:34And then we're going to refresh the
  9501. 8:05:36page.
  9502. 8:05:38And now if we look at the cart here, we
  9503. 8:05:40have one more product. The quantity is
  9504. 8:05:43four. If we open the cart and we scroll
  9505. 8:05:46to the bottom, notice that it added the
  9506. 8:05:49t-shirt to the cart. So that's how we do
  9507. 8:05:51data mutation or update data in the back
  9508. 8:05:54end. We just send a request to the back
  9509. 8:05:57end and the backend takes care of
  9510. 8:06:00updating and saving the data. So, in the
  9511. 8:06:03real world, we use the back end to
  9512. 8:06:05manage and update the data.
  9513. 8:06:09Next, notice that when we added this
  9514. 8:06:11product to the cart, we had to refresh
  9515. 8:06:14the page to see the new cart. It would
  9516. 8:06:17be better if we could just reload the
  9517. 8:06:19cart and update the page without needing
  9518. 8:06:22to refresh.
  9519. 8:06:24We'll go to our code
  9520. 8:06:26and at the bottom, we're going to go to
  9521. 8:06:29app.jsx.
  9522. 8:06:32So here this code reloads the cart from
  9523. 8:06:36the back end and it updates the state.
  9524. 8:06:40After it updates the state, React will
  9525. 8:06:42update the page. So when we add a
  9526. 8:06:45product to the cart, we just need to
  9527. 8:06:48rerun this code and it will update the
  9528. 8:06:50page and then we don't have to refresh
  9529. 8:06:53the page every time.
  9530. 8:06:55We're going to select this entire
  9531. 8:06:57function and we'll cut.
  9532. 8:07:00And outside the use effect, we're going
  9533. 8:07:02to paste it.
  9534. 8:07:05And now that it's outside the use
  9535. 8:07:07effect, we can share this function with
  9536. 8:07:09our components.
  9537. 8:07:11Now, let's actually rename this function
  9538. 8:07:14to something more accurate like load
  9539. 8:07:17cart. And in here, load cart.
  9540. 8:07:22And now we can share this with our
  9541. 8:07:24components using a prop. So we're going
  9542. 8:07:27to scroll down here and in the homepage
  9543. 8:07:31we're going to give it the prop load
  9544. 8:07:33cart equals curly brackets and we'll
  9545. 8:07:37give it the function load cart and save.
  9546. 8:07:42And now if we go to the homepage
  9547. 8:07:45at the top, we'll get load cart out of
  9548. 8:07:49the props and then give it to the
  9549. 8:07:52products grid component. So remember
  9550. 8:07:55this component contains the add to cart
  9551. 8:07:58button. So in here we'll give it another
  9552. 8:08:01prop load cart equals curly brackets and
  9553. 8:08:05give it the load cart function and save.
  9554. 8:08:10And now inside the products grid
  9555. 8:08:14at the top, we're going to get load cart
  9556. 8:08:19out of the props. And then if we scroll
  9557. 8:08:22back down,
  9558. 8:08:24after we update the cart, we're going to
  9559. 8:08:27call the function load cart.
  9560. 8:08:31So this means we will update the cart in
  9561. 8:08:33the back end and then we'll get the new
  9562. 8:08:36cart and update the page without needing
  9563. 8:08:39to refresh.
  9564. 8:08:41One other thing we have to do here is
  9565. 8:08:43remember axios.post
  9566. 8:08:46contacts the back end. So this is
  9567. 8:08:49asynchronous code. It does not finish
  9568. 8:08:51right away. That means we need to wait
  9569. 8:08:54for this code to finish before going on
  9570. 8:08:57to the next line. To wait for this to
  9571. 8:09:00finish at the front of this code we can
  9572. 8:09:03type await and space. And remember in
  9573. 8:09:07order to use await we need to be inside
  9574. 8:09:10an async function. So we'll add async.
  9575. 8:09:15And also this code is not inside a use
  9576. 8:09:17effect. So we can just use async
  9577. 8:09:20directly here like this.
  9578. 8:09:22After that load cart is also
  9579. 8:09:25asynchronous. So it's a good idea to
  9580. 8:09:28await this code as well to be
  9581. 8:09:31consistent. So now let's save
  9582. 8:09:35and we'll check the website.
  9583. 8:09:38And first we'll go back to the homepage.
  9584. 8:09:42And this time let's click add to cart on
  9585. 8:09:44this toaster. So we'll click add to
  9586. 8:09:47cart.
  9587. 8:09:49And now notice that the quantity here
  9588. 8:09:51updated to five without needing to
  9589. 8:09:54refresh. If we go to the cart and scroll
  9590. 8:09:57down,
  9591. 8:09:59it also updated the cart on the page
  9592. 8:10:02without needing to refresh.
  9593. 8:10:05We'll go back to the homepage
  9594. 8:10:08and we're going to make these quantity
  9595. 8:10:10selectors work. So, this lets us add
  9596. 8:10:13more than one product to the cart at a
  9597. 8:10:15time. We're going to go to our code,
  9598. 8:10:19and first we need to get the quantity in
  9599. 8:10:22the quantity selector. To do that, we're
  9600. 8:10:25going to use a controlled input. We
  9601. 8:10:28learned controlled inputs when creating
  9602. 8:10:30the text box in the chatbot project. So,
  9603. 8:10:33as a review, we'll scroll up
  9604. 8:10:36and we're going to create a state to
  9605. 8:10:38save the quantity. At the top, we're
  9606. 8:10:42going to import curly brackets use state
  9607. 8:10:47from React.
  9608. 8:10:49And in this component,
  9609. 8:10:51we're going to create a state const
  9610. 8:10:54bracket equals use state and brackets.
  9611. 8:11:00And we're going to name the state the
  9612. 8:11:02quantity,
  9613. 8:11:04comma, and the updater function set
  9614. 8:11:07quantity.
  9615. 8:11:09And we'll set the starting value of the
  9616. 8:11:11quantity to one.
  9617. 8:11:13Then we're going to scroll down and
  9618. 8:11:16we're going to find this select element.
  9619. 8:11:19So this element represents the selector
  9620. 8:11:22on the page and we're going to give this
  9621. 8:11:24two props. The first prop is the value
  9622. 8:11:28equals curly brackets and the value is
  9623. 8:11:31the quantity.
  9624. 8:11:33The second prop is on change equals
  9625. 8:11:37curly brackets. And we're going to give
  9626. 8:11:39this a function. So brackets, arrow and
  9627. 8:11:42curly brackets.
  9628. 8:11:44And whenever we use an event handler
  9629. 8:11:46like on click or on change, this
  9630. 8:11:50function gets a parameter called event.
  9631. 8:11:53So event contains details about the
  9632. 8:11:56event. In this case, the event will
  9633. 8:11:58contain the quantity that we selected,
  9634. 8:12:00which is one of these values. To get the
  9635. 8:12:04quantity that we selected, we can use
  9636. 8:12:07event.target.
  9637. 8:12:10value
  9638. 8:12:11and let's save the quantity was selected
  9639. 8:12:14in a variable at the front. We'll create
  9640. 8:12:17const quantity selected equals this
  9641. 8:12:22value.
  9642. 8:12:24Now one thing to be careful of is this
  9643. 8:12:27value is going to be one of these values
  9644. 8:12:30here and these are going to be a string.
  9645. 8:12:32So we need to convert it into a number.
  9646. 8:12:35To do that around this value, we're
  9647. 8:12:38going to use the code capital n number
  9648. 8:12:41open bracket and close bracket.
  9649. 8:12:45So this will convert the string here
  9650. 8:12:48into a number. So now that we have the
  9651. 8:12:51quantity that we selected, let's save it
  9652. 8:12:54into the state using set quantity
  9653. 8:12:59brackets
  9654. 8:13:01quantity selected.
  9655. 8:13:04So this is called a controlled input.
  9656. 8:13:07When we have a value and an onchange
  9657. 8:13:09property when we change the selector,
  9658. 8:13:12it's going to set the quantity in the
  9659. 8:13:14state and then the new state is going to
  9660. 8:13:16be set as the value and then it updates
  9661. 8:13:19the page. So we're basically putting
  9662. 8:13:21react in the middle of this process so
  9663. 8:13:23that we have access to the quantity that
  9664. 8:13:26we selected. And that's a review of how
  9665. 8:13:28controlled inputs work. And let's also
  9666. 8:13:32console.log log the quantity selected to
  9667. 8:13:38see if it works. So, we'll save
  9668. 8:13:42and we'll go to the website
  9669. 8:13:44and we're going to open the console with
  9670. 8:13:46rightclick inspect and the console. And
  9671. 8:13:51now on the website, we're going to click
  9672. 8:13:53the quantity selector here and we'll
  9673. 8:13:56select three.
  9674. 8:13:58And now inside the console, it displays
  9675. 8:14:01the quantity that we selected.
  9676. 8:14:04However, there is a problem here. Notice
  9677. 8:14:07on this website when we change one
  9678. 8:14:10quantity selector, it changes all of
  9679. 8:14:12them. So if I change this one here to
  9680. 8:14:14five, it changes all the quantities. So
  9681. 8:14:18let's fix this. We'll go back to the
  9682. 8:14:20code.
  9683. 8:14:22And if we scroll to the top,
  9684. 8:14:25this is happening because the state here
  9685. 8:14:29is outside of this loop. So that means
  9686. 8:14:32the state is shared across all the
  9687. 8:14:35products. That's why when we change one
  9688. 8:14:38selector, it changes all of them. To
  9689. 8:14:41solve this problem, we need to move this
  9690. 8:14:43state inside this loop so that each
  9691. 8:14:46product has its own quantity. So, we're
  9692. 8:14:50going to select this code and cut.
  9693. 8:14:54And inside this loop, we're going to
  9694. 8:14:57paste.
  9695. 8:15:00However, this code actually breaks the
  9696. 8:15:03rules of React hooks. We can only put
  9697. 8:15:06hooks at the top level of a component.
  9698. 8:15:09We should not put hooks inside a loop
  9699. 8:15:12like this. To fix this problem, we can
  9700. 8:15:15separate each product into its own
  9701. 8:15:18component.
  9702. 8:15:19So, inside the home folder, we're going
  9703. 8:15:22to create a new file and we'll name it
  9704. 8:15:25product.jsx.
  9705. 8:15:29And we'll create a component for each
  9706. 8:15:31product, which is export function
  9707. 8:15:35product brackets and curly brackets. And
  9708. 8:15:39we're going to return the HTML for each
  9709. 8:15:43product. Inside the products grid, we're
  9710. 8:15:47going to collapse this code. And this is
  9711. 8:15:50the code for each product. So, we'll
  9712. 8:15:53select it and copy.
  9713. 8:15:56And inside the product component, we're
  9714. 8:15:59going to paste
  9715. 8:16:02and then control or command shiftp
  9716. 8:16:04format document.
  9717. 8:16:07We'll scroll up
  9718. 8:16:10and we'll go back to the products grid.
  9719. 8:16:13And we also need to copy over the state.
  9720. 8:16:16So we're going to select this copy and
  9721. 8:16:20inside product at the top we're going to
  9722. 8:16:24paste.
  9723. 8:16:26And we'll fix these errors. So first we
  9724. 8:16:29need to import use state at the top. So
  9725. 8:16:33import curly brackets use state from
  9726. 8:16:36react
  9727. 8:16:38and the product we'll get it from the
  9728. 8:16:40props using curly brackets product.
  9729. 8:16:44Scroll down
  9730. 8:16:46we'll need the format money function as
  9731. 8:16:49well as axios and load cart. So first
  9732. 8:16:54we'll import curly brackets format money
  9733. 8:16:59from a string and we'll find dot dot
  9734. 8:17:03slash do/youutils/money.
  9735. 8:17:07At the top we're going to import axios
  9736. 8:17:12from axios
  9737. 8:17:14and we'll get the load cart function
  9738. 8:17:16from the props. So, comma load cart and
  9739. 8:17:20save.
  9740. 8:17:22So, now each product has a separate
  9741. 8:17:25state. It has its own quantity. And this
  9742. 8:17:28is no longer breaking the rules of hooks
  9743. 8:17:31because this state is at the top level
  9744. 8:17:33of this component.
  9745. 8:17:35We'll go back to the products grid.
  9746. 8:17:38And here we can remove this HTML and
  9747. 8:17:42just use our component less than product
  9748. 8:17:46/greater than and we need to give it the
  9749. 8:17:49product
  9750. 8:17:51equals product and also the load cart
  9751. 8:17:54function equals load cart
  9752. 8:17:58and we can remove this state and
  9753. 8:18:01remember when we loop through an array
  9754. 8:18:04we need to give it a key prop. So at the
  9755. 8:18:07front here we'll set a key equals curly
  9756. 8:18:09brackets and we'll set it to the product
  9757. 8:18:13id.
  9758. 8:18:15And finally we need to import this
  9759. 8:18:17component. So we'll scroll up and at the
  9760. 8:18:20top we'll import curly brackets the
  9761. 8:18:24product component from a string and it's
  9762. 8:18:28right beside this file. So, we'll do dot
  9763. 8:18:30slash product and we can remove these
  9764. 8:18:34because they're no longer being used and
  9765. 8:18:37save.
  9766. 8:18:38And since we have the key in this loop
  9767. 8:18:41here, we can also go into the product
  9768. 8:18:44and remove the key in here and save.
  9769. 8:18:50Now, let's open the website.
  9770. 8:18:54And if we change one of these selectors,
  9771. 8:18:57notice that each product can now have
  9772. 8:18:59its own quantity.
  9773. 8:19:02Now that we have this quantity, when we
  9774. 8:19:04add a product to the cart, let's use
  9775. 8:19:07this quantity. We'll go to our code
  9776. 8:19:11and we're going to go inside each
  9777. 8:19:13product and we're going to scroll down
  9778. 8:19:18to the add to cart button here. And
  9779. 8:19:21instead of adding a quantity of one
  9780. 8:19:23every time, we're going to add the
  9781. 8:19:26quantity that is saved in the state
  9782. 8:19:28above. So now let's save
  9783. 8:19:32and we'll try out this code. We'll open
  9784. 8:19:35the website and let's go to this plate
  9785. 8:19:39product and we're going to select a
  9786. 8:19:41quantity of three this time and we'll
  9787. 8:19:44click add to cart.
  9788. 8:19:47And notice that the cart updated and it
  9789. 8:19:50now has a quantity of eight, which is
  9790. 8:19:52three more than before. If we open the
  9791. 8:19:55cart
  9792. 8:19:56and we scroll to the bottom, we now have
  9793. 8:20:00three of these plates in the cart. So
  9794. 8:20:03now the quantity selector is working.
  9795. 8:20:06Let's go back to the homepage.
  9796. 8:20:09And this time, let's try adding a
  9797. 8:20:11product that already exists in the cart.
  9798. 8:20:14So, for the plate product, we're going
  9799. 8:20:16to select the quantity of three again,
  9800. 8:20:20and we're going to click add to cart.
  9801. 8:20:23Notice the cart quantity increased by
  9802. 8:20:25three. And if we open the cart and
  9803. 8:20:29scroll down, instead of adding a new
  9804. 8:20:32plate product, it just increased the
  9805. 8:20:34quantity by three for a total of six. So
  9806. 8:20:38if the product already exists in the
  9807. 8:20:40cart, the backend handles this situation
  9808. 8:20:42for us and just increases the quantity.
  9809. 8:20:46Remember the purpose of the backend is
  9810. 8:20:48to manage the data. By letting the
  9811. 8:20:51backend manage and update the data, we
  9812. 8:20:53can simplify our React code.
  9813. 8:20:57Let's go back to our code
  9814. 8:20:59and we're going to do some cleanup in
  9815. 8:21:01this component.
  9816. 8:21:03First, instead of doing quantity colon
  9817. 8:21:05quantity, we can use the shorthand
  9818. 8:21:08property syntax and just use the
  9819. 8:21:11quantity. It does the same thing as
  9820. 8:21:13before.
  9821. 8:21:14Next, instead of putting our event
  9822. 8:21:16handlers directly in the HTML like this,
  9823. 8:21:20we usually save this in a separate
  9824. 8:21:22function. So, let's select all this code
  9825. 8:21:26and copy.
  9826. 8:21:28And we're going to scroll up.
  9827. 8:21:32And at the top we're going to create a
  9828. 8:21:34function const add to cart equals and
  9829. 8:21:40we'll paste that code.
  9830. 8:21:43So we usually save the function
  9831. 8:21:45separately like this. And then we can
  9832. 8:21:47scroll down.
  9833. 8:21:49We can remove this code and just use the
  9834. 8:21:52function name add to cart. And this
  9835. 8:21:56makes the code easier to read. Next,
  9836. 8:21:59we'll scroll up to the quantity
  9837. 8:22:02selector. We can remove the console.log
  9838. 8:22:07and we'll move this into a separate
  9839. 8:22:09function as well. We'll select this code
  9840. 8:22:12and copy
  9841. 8:22:14and we'll scroll up
  9842. 8:22:17and here we're going to create another
  9843. 8:22:20function const select quantity and make
  9844. 8:22:24it equal and we'll paste that code and
  9845. 8:22:28we'll use this function below. So down
  9846. 8:22:31here,
  9847. 8:22:33instead of putting this function
  9848. 8:22:34directly in the HTML, we use the
  9849. 8:22:37function name select quantity and save.
  9850. 8:22:43And that's it. We created the add to
  9851. 8:22:46cart feature. In the final project,
  9852. 8:22:48there's a small message that pops up
  9853. 8:22:50when we click add to cart, but I'll
  9854. 8:22:52leave this as an exercise. Now, let's
  9855. 8:22:55save this into Git. We'll open the git
  9856. 8:22:58section and let's use the message create
  9857. 8:23:02add to cart feature and commit.
  9858. 8:23:11Before we continue, I want to go back to
  9859. 8:23:13the code that we wrote. So, we're going
  9860. 8:23:15to scroll up in this component and I
  9861. 8:23:18want to explain axios.post
  9862. 8:23:21a little more. As a review, when we send
  9863. 8:23:24a request to the backend, we can set a
  9864. 8:23:27URL path. Based on the URL path, the
  9865. 8:23:31backend can do different things. For
  9866. 8:23:34example, if we use / API/roucts,
  9867. 8:23:38the backend gives us the products. If we
  9868. 8:23:40use / API/cart items, the backend gives
  9869. 8:23:44us the cart.
  9870. 8:23:46In addition to the URL path, every
  9871. 8:23:49request also has a type. In the previous
  9872. 8:23:53lesson when we did axios.get/api/cart
  9873. 8:23:57items, this actually sends the type get
  9874. 8:24:01and the URL path/ API/cart items to the
  9875. 8:24:05back end. So it sends two pieces of
  9876. 8:24:08information to the back end. The type
  9877. 8:24:11and the URL path.
  9878. 8:24:14When we did axios.post/appi/cart
  9879. 8:24:17post/api/cart
  9880. 8:24:19items. This sends the type post and the
  9881. 8:24:23URL path /api/cart
  9882. 8:24:26items to the back end.
  9883. 8:24:28Now when the backend receives our
  9884. 8:24:31request, it looks at both the type and
  9885. 8:24:35the URL path to decide what to do.
  9886. 8:24:39For example, if the backend receives get
  9887. 8:24:43and/api/cart
  9888. 8:24:45items, it will get the cart items and
  9889. 8:24:48return it. If the backend receives post
  9890. 8:24:52and/appi/cart
  9891. 8:24:54items, it will create a new cart item.
  9892. 8:24:58So both the type and the URL path
  9893. 8:25:01together determine what the backend
  9894. 8:25:04does.
  9895. 8:25:06In the description, I have a
  9896. 8:25:08documentation page for my backend,
  9897. 8:25:12and it lists all the different requests
  9898. 8:25:14that my backend supports. Notice that
  9899. 8:25:17each request has a type and a URL path.
  9900. 8:25:22By the way, the type is also called a
  9901. 8:25:24HTTP method.
  9902. 8:25:27When working with a backend, there are
  9903. 8:25:30four common types of requests that we
  9904. 8:25:32use. Get, post, put, and delete. Get
  9905. 8:25:38means we want to get some data. Post
  9906. 8:25:41means we want to create some data. Put
  9907. 8:25:44means we want to update some data and
  9908. 8:25:47delete means we want to delete some
  9909. 8:25:50data.
  9910. 8:25:51So, Axios has a method for each type.
  9911. 8:25:55Axios.get Get sets the type of request
  9912. 8:25:58to get. Axios.post
  9913. 8:26:01sets the type to post. Axios.put sets
  9914. 8:26:05the type to put. And axios.delete
  9915. 8:26:08sets the type to delete. And then
  9916. 8:26:11between the brackets, we put the URL
  9917. 8:26:13path.
  9918. 8:26:16So in our code, this sends a request to
  9919. 8:26:19the back end with the type post and the
  9920. 8:26:22URL path/ API/cart items. So the most
  9921. 8:26:27important thing to remember here is that
  9922. 8:26:29each request has a type and a URL path
  9923. 8:26:33and both of these determine what the
  9924. 8:26:35backend will do.
  9925. 8:26:38In the rest of this lesson, we'll get
  9926. 8:26:40more practice sending different types of
  9927. 8:26:42requests to the backend.
  9928. 8:26:45Next, let's go back to the website.
  9929. 8:26:48And inside the checkout page for each
  9930. 8:26:51product, we have these delivery options.
  9931. 8:26:54So, we're going to add the ability to
  9932. 8:26:57update the delivery option. Because
  9933. 8:27:00we're updating something in the cart,
  9934. 8:27:02we're going to practice using a put
  9935. 8:27:04request.
  9936. 8:27:06First, let's find the code for these
  9937. 8:27:08delivery options. We'll go back to our
  9938. 8:27:10code
  9939. 8:27:12and open our files.
  9940. 8:27:15and we're going to find the pages and
  9941. 8:27:18checkout folder and we're going to open
  9942. 8:27:20delivery options.jsx
  9943. 8:27:25and we'll scroll down
  9944. 8:27:27and here each of these represents one
  9945. 8:27:30delivery option on the page. So we're
  9946. 8:27:33going to put an on click property on
  9947. 8:27:35this element. Now, usually we put the on
  9948. 8:27:38click on the input, but in this case,
  9949. 8:27:41this will make only the radial selector
  9950. 8:27:43clickable. Instead, we want the entire
  9951. 8:27:46delivery option, including the text, to
  9952. 8:27:49be clickable. So, that's why we're going
  9953. 8:27:51to put the on click on the outside
  9954. 8:27:53component.
  9955. 8:27:55So, we're going to add the on click
  9956. 8:27:57attribute equals curly brackets. And
  9957. 8:28:01let's create a function for this. Above
  9958. 8:28:04this, we'll create a function with const
  9959. 8:28:08and let's name it update delivery option
  9960. 8:28:12equals. And let's make it an async
  9961. 8:28:15function brackets arrow and curly
  9962. 8:28:18brackets because we know we're going to
  9963. 8:28:20make a request to the back end to update
  9964. 8:28:22the delivery option.
  9965. 8:28:26Also, we create the function here inside
  9966. 8:28:29of this loop because we need access to
  9967. 8:28:31each delivery option.
  9968. 8:28:34So next to update the delivery option
  9969. 8:28:37for a product, we're going to make a
  9970. 8:28:39request to the back end. We're going to
  9971. 8:28:42scroll to the top and at the top we'll
  9972. 8:28:45import axials from Axios
  9973. 8:28:51and we'll scroll back down
  9974. 8:28:54and inside here according to my
  9975. 8:28:56documentation my backend supports the
  9976. 8:28:59request put/ API/cart items. This lets
  9977. 8:29:04us update the cart. For example, we can
  9978. 8:29:07update the delivery option for a
  9979. 8:29:09product.
  9980. 8:29:11So, we're going to do axios.put
  9981. 8:29:15brackets.
  9982. 8:29:17This sends a request with the type put
  9983. 8:29:19to the back end. And we'll set the URL
  9984. 8:29:22path to / API/cart-ite
  9985. 8:29:26items. And we'll add another slash. And
  9986. 8:29:30we're going to insert the product ID
  9987. 8:29:33that we want to update here at the end
  9988. 8:29:35of the URL path. So in my documentation,
  9989. 8:29:39colon product ID is known as a URL
  9990. 8:29:42parameter. This means my backend wants
  9991. 8:29:44the product ID we want to update inside
  9992. 8:29:47the URL. This is just a naming
  9993. 8:29:50convention that is popular with
  9994. 8:29:52backends. When we update something, we
  9995. 8:29:54usually put the ID of what we want to
  9996. 8:29:57update inside the URL. So here first
  9997. 8:30:01we're going to change this to a template
  9998. 8:30:02string using back ticks
  9999. 8:30:06and then at the end we'll insert dollar
  10000. 8:30:08curly brackets and we're going to insert
  10001. 8:30:10the product ID that we want to update.
  10002. 8:30:14So the product ID is saved inside the
  10003. 8:30:16cart item. So inside here we're going to
  10004. 8:30:20insert the cart item
  10005. 8:30:24product ID.
  10006. 8:30:26Next, we need to tell the backend which
  10007. 8:30:29delivery option we want to update to. To
  10008. 8:30:32do that, we're going to give the backend
  10009. 8:30:34some data. At the end of this, we'll add
  10010. 8:30:37a comma and an object.
  10011. 8:30:41And in my documentation, this section
  10012. 8:30:44tells us what data we can send to the
  10013. 8:30:46back end. Here, we can send the delivery
  10014. 8:30:50option ID that we want to update to.
  10015. 8:30:54So, we're going to give the back end the
  10016. 8:30:57delivery option ID that we want to
  10017. 8:31:01update to. So, if we scroll up here, we
  10018. 8:31:05want to update to this delivery option.
  10019. 8:31:09So, we'll tell the backend to update to
  10020. 8:31:12the delivery option ID.
  10021. 8:31:17And after we update the delivery option,
  10022. 8:31:20we're going to reload the cart and
  10023. 8:31:22update the page.
  10024. 8:31:24So in front of this, let's wait for this
  10025. 8:31:27to finish using await.
  10026. 8:31:30And remember, we created a function for
  10027. 8:31:32reloading the cart called load cart. So
  10028. 8:31:36inside app.jsx,
  10029. 8:31:40we're going to share load cart with the
  10030. 8:31:43checkout page. So down here we'll add a
  10031. 8:31:46prop load cart equals curly brackets
  10032. 8:31:51load cart and save.
  10033. 8:31:55And then inside the checkout page
  10034. 8:31:59at the top we're going to get load cart.
  10035. 8:32:04And now we need to share this with the
  10036. 8:32:06delivery options. Now the delivery
  10037. 8:32:08options are actually inside the order
  10038. 8:32:10summary. So we're going to share it with
  10039. 8:32:12the order summary first.
  10040. 8:32:14We'll scroll down to the order summary
  10041. 8:32:17and here we're going to give it the load
  10042. 8:32:20cart equals curly brackets load cart and
  10043. 8:32:25save
  10044. 8:32:27inside the order summary
  10045. 8:32:30at the top we're going to get from the
  10046. 8:32:33props load cart and then we're going to
  10047. 8:32:36share it with the delivery options. So,
  10048. 8:32:38we'll scroll down to these delivery
  10049. 8:32:41options and we'll give it the prop load
  10050. 8:32:45cart equals curly brackets load cart and
  10051. 8:32:49save.
  10052. 8:32:51And finally, in the delivery options, we
  10053. 8:32:54can get load cart from the props. So,
  10054. 8:32:57comma load cart and scroll down.
  10055. 8:33:02After we update the delivery option,
  10056. 8:33:05we're going to await load cart and
  10057. 8:33:09brackets so that we reload the cart and
  10058. 8:33:12update the page and we don't have to
  10059. 8:33:15refresh to see the updates. Finally,
  10060. 8:33:18we're going to take this function and
  10061. 8:33:20put it in this on click. So, update
  10062. 8:33:23delivery option
  10063. 8:33:26and save.
  10064. 8:33:29Now let's go to the website
  10065. 8:33:31and we'll try changing a delivery
  10066. 8:33:33option.
  10067. 8:33:35And now you can see that it updates when
  10068. 8:33:37we change the delivery option.
  10069. 8:33:40Now one last thing here is if we open
  10070. 8:33:43the console,
  10071. 8:33:45we're going to get this warning message
  10072. 8:33:47saying we provided a check property
  10073. 8:33:49without an onchange handler. So let's
  10074. 8:33:52fix this warning. We're going to go back
  10075. 8:33:55to the code
  10076. 8:33:57and the warning means that for an input
  10077. 8:34:00element, if we have a checked prop, we
  10078. 8:34:03should also have an onchange prop. On
  10079. 8:34:07change lets us change the value of this
  10080. 8:34:09input.
  10081. 8:34:11However, in our situation, we already
  10082. 8:34:14use this on click up here to change the
  10083. 8:34:17value of the input. So, we don't really
  10084. 8:34:19need an onchange property. So in here to
  10085. 8:34:23get rid of that warning, we can just add
  10086. 8:34:25an on change property equals curly
  10087. 8:34:28brackets and give it an empty function.
  10088. 8:34:31And this will just make the warning go
  10089. 8:34:33away. We'll save
  10090. 8:34:37and go back to our website.
  10091. 8:34:39And let's refresh.
  10092. 8:34:42And now if we change the delivery option
  10093. 8:34:45and check the console, we no longer get
  10094. 8:34:48that warning.
  10095. 8:34:51Next, we have another problem on this
  10096. 8:34:53page, which is when we update the
  10097. 8:34:56delivery option. The payment summary
  10098. 8:34:59here is not updating to show the new
  10099. 8:35:02delivery price.
  10100. 8:35:04To fix this, after we update a delivery
  10101. 8:35:06option, we also want to reload the
  10102. 8:35:09payment summary. So, let's find the code
  10103. 8:35:12for reloading the payment summary. We'll
  10104. 8:35:15go to our code
  10105. 8:35:18and we're going to open the checkout
  10106. 8:35:20page
  10107. 8:35:22and we're going to scroll up
  10108. 8:35:25and inside this use effect we have this
  10109. 8:35:29code here that reloads the payment
  10110. 8:35:32summary.
  10111. 8:35:33So we want to rerun this code after
  10112. 8:35:36updating the delivery option. Now we
  10113. 8:35:39could separate this into a function and
  10114. 8:35:41then share it like we did with load
  10115. 8:35:44cart. However, we have an easier option
  10116. 8:35:47here. Remember that in use effect at the
  10117. 8:35:51end we have this dependency array. So
  10118. 8:35:55this array determines when use effect
  10119. 8:35:57runs. Right now it's empty which means
  10120. 8:36:00use effect will only run once. However,
  10121. 8:36:04we can put values inside this array. And
  10122. 8:36:08whenever a value changes, it will rerun
  10123. 8:36:11the use effect. In our situation, we can
  10124. 8:36:15put the value cart inside this
  10125. 8:36:18dependency array.
  10126. 8:36:20And now whenever the cart changes, it
  10127. 8:36:24will rerun the use effect and update the
  10128. 8:36:27payment summary. And that makes sense.
  10129. 8:36:30Every time the cart changes, we do want
  10130. 8:36:32to update the payment summary in case
  10131. 8:36:34the cost of the order changes.
  10132. 8:36:37So in React there are often multiple
  10133. 8:36:40solutions to do something and we should
  10134. 8:36:42try to pick the cleanest solution.
  10135. 8:36:45Also note that this will cause the
  10136. 8:36:47delivery options to be reloaded which is
  10137. 8:36:50not really necessary. So we could
  10138. 8:36:53separate reloading the payment summary
  10139. 8:36:55into another use effect but I'll leave
  10140. 8:36:57this as an exercise.
  10141. 8:37:00So now let's save
  10142. 8:37:03and we'll go to our website.
  10143. 8:37:06And now when we update the delivery
  10144. 8:37:08option, notice that the payment summary
  10145. 8:37:10also updates to show the new delivery
  10146. 8:37:13price. And now this feature is working.
  10147. 8:37:17So now let's save this into git. We'll
  10148. 8:37:20go back to our code and open the git
  10149. 8:37:23section.
  10150. 8:37:25And here we'll use a message create
  10151. 8:37:28update delivery option feature and
  10152. 8:37:33commit.
  10153. 8:37:39Next, we're going to go to our website
  10154. 8:37:42and we're going to work on deleting an
  10155. 8:37:44item from the cart. So, we're going to
  10156. 8:37:47practice making a delete request to the
  10157. 8:37:49back end. First, we're going to find the
  10158. 8:37:52code for this delete link. We'll go to
  10159. 8:37:55our code
  10160. 8:37:57and in our files we're going to find the
  10161. 8:38:00order summary component
  10162. 8:38:03and we're going to scroll down in this
  10163. 8:38:05component and find the delete link. If
  10164. 8:38:09you did the exercises, this might be
  10165. 8:38:11inside a cart item details component.
  10166. 8:38:15Now, for this delete link, we're going
  10167. 8:38:17to add an on click attribute equals
  10168. 8:38:20curly brackets. And we'll give this a
  10169. 8:38:23function to run. Let's call it delete
  10170. 8:38:27cart item. And let's create this
  10171. 8:38:30function above. So, I'm going to scroll
  10172. 8:38:32up
  10173. 8:38:34and I'm going to create the function
  10174. 8:38:36inside this loop because I need access
  10175. 8:38:38to this cart item to delete. So in here
  10176. 8:38:42we'll create the function const delete
  10177. 8:38:46cart item equals a function.
  10178. 8:38:51And let's make this an async function
  10179. 8:38:54because we know we're going to make a
  10180. 8:38:56request to the back end. And first let's
  10181. 8:38:59import axios. So we'll scroll up and at
  10182. 8:39:02the top we'll import axios from a string
  10183. 8:39:07axios.
  10184. 8:39:09And then inside this function,
  10185. 8:39:12my backend can receive a request called
  10186. 8:39:15delete/appi/cart
  10187. 8:39:17items. And this will delete a product
  10188. 8:39:20from the cart. So we'll do axials
  10189. 8:39:23dodelete
  10190. 8:39:25and brackets.
  10191. 8:39:27This sends a request with the type
  10192. 8:39:29delete to the back end. For the URL
  10193. 8:39:32path, we're going to use a template
  10194. 8:39:34string and we'll do / API/cart-
  10195. 8:39:39items slash and we're going to insert
  10196. 8:39:42the product ID in the URL. Again, this
  10197. 8:39:46is just a naming convention that my
  10198. 8:39:47backend uses. So, we're going to insert
  10199. 8:39:51the ID of what we want to delete. In
  10200. 8:39:54this case, we need to give the backend
  10201. 8:39:56the product ID we want to delete. So the
  10202. 8:39:59product ID is saved inside the cart
  10203. 8:40:02item. So we're going to give the backend
  10204. 8:40:04the cart item dot product ID. And that's
  10205. 8:40:10it. When my backend receives a request
  10206. 8:40:13with the type delete and this URL path,
  10207. 8:40:17it's going to delete this product from
  10208. 8:40:18the cart. So after deleting the product,
  10209. 8:40:22let's reload the cart. First, at the
  10210. 8:40:25front of this, we'll type await to wait
  10211. 8:40:28for this code to finish. And then we're
  10212. 8:40:31going to await load the cart and
  10213. 8:40:35brackets.
  10214. 8:40:38So, I have load cart already in the
  10215. 8:40:40order summary. If you did the exercises
  10216. 8:40:43from the previous lesson, you may need
  10217. 8:40:45to pass this function into your
  10218. 8:40:47component using a prop.
  10219. 8:40:50So, now let's scroll down. And that's
  10220. 8:40:52all we need to delete a product from the
  10221. 8:40:55cart and then reload and update the
  10222. 8:40:57page. Let's save.
  10223. 8:41:01We'll go to the website
  10224. 8:41:03and we're going to delete this first
  10225. 8:41:05product.
  10226. 8:41:07And notice it updates the cart and it
  10227. 8:41:09also updates the payment summary. If we
  10228. 8:41:12delete another product, you'll notice it
  10229. 8:41:15updates the cart and updates the payment
  10230. 8:41:17summary. So that's how we make the
  10231. 8:41:19delete link work. I'll leave updating
  10232. 8:41:23the quantity in the cart as an exercise.
  10233. 8:41:26So now let's save this into git. We'll
  10234. 8:41:29open the git section and then here we'll
  10235. 8:41:32use the message create delete cart item
  10236. 8:41:38feature and commit.
  10237. 8:41:45Let's go back to the website.
  10238. 8:41:49And in the checkout page, we're going to
  10239. 8:41:51add the ability to create an order. So,
  10240. 8:41:55first let's find the code for this place
  10241. 8:41:57order button. We'll go back to our code
  10242. 8:42:01and in our files,
  10243. 8:42:04we're going to open the payment summary
  10244. 8:42:06component and we're going to scroll down
  10245. 8:42:09to the bottom to this place order
  10246. 8:42:12button. And to this button, we're going
  10247. 8:42:15to add a prop on click equals curly
  10248. 8:42:19brackets. And we'll give it a function.
  10249. 8:42:22Let's call it create order.
  10250. 8:42:27And now let's scroll up and create this
  10251. 8:42:29function. So up here
  10252. 8:42:34we're going to create const and we'll
  10253. 8:42:37name it create order equals an async
  10254. 8:42:41function brackets arrow and curly
  10255. 8:42:44brackets.
  10256. 8:42:46Again we're going to make a request to
  10257. 8:42:48the back end to create the order. So at
  10258. 8:42:51the top we're going to import axios from
  10259. 8:42:55a string axios
  10260. 8:42:59and in here for my back end we can send
  10261. 8:43:02a request with the type post and the URL
  10262. 8:43:06path /api/orders
  10263. 8:43:08to create an order. So we'll use the
  10264. 8:43:11code axios.post post
  10265. 8:43:15to make a request with the type post and
  10266. 8:43:18then give it the URL path slash API/
  10267. 8:43:23orders.
  10268. 8:43:24And that's all we need. Remember the
  10269. 8:43:27cart is already saved in the back end.
  10270. 8:43:29So the back end can take that cart and
  10271. 8:43:31create an order for us.
  10272. 8:43:34So let's wait for this code to finish
  10273. 8:43:37using a wait.
  10274. 8:43:40And after we create an order, the back
  10275. 8:43:43end will remove everything from our
  10276. 8:43:45cart. So we also need to reload our cart
  10277. 8:43:48here. First we need to get the load cart
  10278. 8:43:51function and that can be found in the
  10279. 8:43:54checkout page.
  10280. 8:43:57So here we get load cart from the props
  10281. 8:43:59and we're going to share this with the
  10282. 8:44:02payment summary. So, we'll scroll down
  10283. 8:44:04and in the payment summary, we'll share
  10284. 8:44:07load cart using a prop load cart equals
  10285. 8:44:11curly brackets load cart and save.
  10286. 8:44:17And then inside the payment summary
  10287. 8:44:20at the top in the props, we'll get comma
  10288. 8:44:24load cart.
  10289. 8:44:27And now after we create an order, we're
  10290. 8:44:30going to await load cart and brackets to
  10291. 8:44:35reload the cart and update the page.
  10292. 8:44:40Finally, after we create an order, let's
  10293. 8:44:42also redirect to the orders page so we
  10294. 8:44:45can see our new order. To do that, React
  10295. 8:44:49Router provides a hook that lets us
  10296. 8:44:51navigate to another page or go to
  10297. 8:44:54another page. At the top, we're going to
  10298. 8:44:58import curly brackets use navigate
  10299. 8:45:03from React-Router.
  10300. 8:45:07And at the top of our component, we're
  10301. 8:45:11going to use const
  10302. 8:45:14equals use navigate and brackets.
  10303. 8:45:19So use navigate gives us a function and
  10304. 8:45:23this function lets us navigate to or go
  10305. 8:45:26to another page in our app. So after we
  10306. 8:45:30create an order and we reload the cart,
  10307. 8:45:33let's navigate to the orders page using
  10308. 8:45:37navigate brackets and a string. And
  10309. 8:45:41we're going to navigate to slash orders.
  10310. 8:45:46Now, navigate is not asynchronous, so it
  10311. 8:45:50finishes immediately and we don't need
  10312. 8:45:52to await this code. Let's save.
  10313. 8:45:57And we'll go to the website and test out
  10314. 8:46:00the place order button.
  10315. 8:46:03So, we could update the delivery option
  10316. 8:46:05to get a shipping cost. And then click
  10317. 8:46:08place order.
  10318. 8:46:10And now you can see that it created a
  10319. 8:46:13new order.
  10320. 8:46:15You'll also see that it updated the
  10321. 8:46:17cart. So, the quantity is now zero. And
  10322. 8:46:20it navigated us to the orders page.
  10323. 8:46:24Once we are on the orders page, there's
  10324. 8:46:26some code at the top of the orders page
  10325. 8:46:29that loads the orders from the back end
  10326. 8:46:31and updates the page. And that's how
  10327. 8:46:34we're able to see our newest order here.
  10328. 8:46:39So, that's how we create an order. I'll
  10329. 8:46:42leave making these add to cart buttons
  10330. 8:46:44work as an exercise.
  10331. 8:46:47So now we finished all the major
  10332. 8:46:49features of our project. We can add
  10333. 8:46:51products to our cart. We can update the
  10334. 8:46:54cart and we can create an order. And
  10335. 8:46:57you'll see that in the real world, we
  10336. 8:46:59use a backend to manage and update all
  10337. 8:47:02the data.
  10338. 8:47:04And in our React code, we just make
  10339. 8:47:07requests to the back end and then update
  10340. 8:47:09the page. The advantage of letting the
  10341. 8:47:12backend manage the data is that it will
  10342. 8:47:15simplify our code a lot. So now let's
  10343. 8:47:18save this into git. Go to the git
  10344. 8:47:21section and at the top we'll type make
  10345. 8:47:26create order button work and commit.
  10346. 8:47:35And now we finished all the major
  10347. 8:47:37features of our project. In the next
  10348. 8:47:40lesson, we'll learn how to create
  10349. 8:47:42automated tests in React to make sure
  10350. 8:47:45our code still works whenever we make
  10351. 8:47:47changes. And that's the end of this
  10352. 8:47:50lesson. In this lesson, we learned data
  10353. 8:47:52mutation, which means to update data in
  10354. 8:47:55the back end. We learned different types
  10355. 8:47:58of requests like get, post, put, and
  10356. 8:48:02delete. We sent a post request to add
  10357. 8:48:06products to the cart and to create an
  10358. 8:48:09order. We sent a put request to update
  10359. 8:48:12the cart. We sent a delete request to
  10360. 8:48:15delete a product from the cart. We used
  10361. 8:48:18the dependency array of use effect to
  10362. 8:48:21update the payment summary whenever the
  10363. 8:48:24cart changes. We learned the use
  10364. 8:48:27navigate hook which lets us navigate to
  10365. 8:48:29another page using our code.
  10366. 8:48:33Thanks for watching so far. If you find
  10367. 8:48:35this course valuable, you can support
  10368. 8:48:37the channel by liking this video,
  10369. 8:48:39leaving a comment, and subscribing.
  10370. 8:48:42You can also check out the premium
  10371. 8:48:44version of this course where you'll get
  10372. 8:48:46all the lessons ads free broken down
  10373. 8:48:48into small videos, as well as additional
  10374. 8:48:51lessons and more projects that are not
  10375. 8:48:53in the public React course. Currently,
  10376. 8:48:56the premium course contains 11 lessons,
  10377. 8:48:58and I'll be adding more lessons in the
  10378. 8:49:00future. You can also get a certificate
  10379. 8:49:03of completion, create comments or
  10380. 8:49:06questions on specific topics, get
  10381. 8:49:08progress tracking, search and course
  10382. 8:49:10notes. You can also check out the
  10383. 8:49:13membership where you can get this course
  10384. 8:49:15ads free separated by each lesson, AI
  10385. 8:49:18videos, backend tutorials, and more.
  10386. 8:49:21Thanks again, and let's continue with
  10387. 8:49:23the course. Here are some exercises to
  10388. 8:49:26help you practice what we learned in
  10389. 8:49:28this lesson.
  10390. 8:50:15In this lesson, we're going to add
  10391. 8:50:17automated tests to our app. Automated
  10392. 8:50:20tests means using code to test our app.
  10393. 8:50:24This is more efficient than manually
  10394. 8:50:26opening the app and clicking around to
  10395. 8:50:29test it.
  10396. 8:50:31To create automated tests in React,
  10397. 8:50:33we're going to use an npm package. The
  10398. 8:50:37two most popular packages for automated
  10399. 8:50:39testing are justest and vest.
  10400. 8:50:43We're going to use VEST because it
  10401. 8:50:45integrates well with Vit, but just and
  10402. 8:50:48Vest are very similar. If you learn one,
  10403. 8:50:51you can easily learn the other. First,
  10404. 8:50:54we're going to install Vest to our
  10405. 8:50:56project. So, we're going to go to our
  10406. 8:50:58command line and open the tab that's
  10407. 8:51:01running the front end. And we'll stop
  10408. 8:51:04the VIT server using C.
  10409. 8:51:07And we're going to npm install
  10410. 8:51:11dash save-dev
  10411. 8:51:16test.
  10412. 8:51:17So, save dev means that vest is only for
  10413. 8:51:21development. This package is not needed
  10414. 8:51:23to run our app or to run our website.
  10415. 8:51:27And we're also going to add at to give
  10416. 8:51:31vest a specific version. So you can
  10417. 8:51:33click this link in the description, go
  10418. 8:51:36to lesson 9, and here you'll find the
  10419. 8:51:39version of Vest that we'll install. So
  10420. 8:51:42we'll copy that version and then paste
  10421. 8:51:44it in here
  10422. 8:51:46and press enter to install VEST.
  10423. 8:51:53Now that we installed Vest, we're ready
  10424. 8:51:55to create our first automated test. So
  10425. 8:51:58there are different types of automated
  10426. 8:52:00tests that we can create. The first type
  10427. 8:52:03is called unit tests. A unit test means
  10428. 8:52:07we test one unit or one piece of the
  10429. 8:52:10code. For example, if we look at our
  10430. 8:52:13files
  10431. 8:52:15and we open our utils folder and open
  10432. 8:52:18money.js, JS.
  10433. 8:52:21If we test a function like format money,
  10434. 8:52:24this would be considered a unit test
  10435. 8:52:27because it's just one piece of the code.
  10436. 8:52:30However, if we test something like the
  10437. 8:52:33product component here, testing
  10438. 8:52:35components like this are not unit tests
  10439. 8:52:39because they use many different pieces
  10440. 8:52:41of code together.
  10441. 8:52:44So, we'll start by creating unit tests
  10442. 8:52:46for format money.
  10443. 8:52:49So, we're going to go to this money.js
  10444. 8:52:52file, and beside this file, we're going
  10445. 8:52:55to create a new file for the tests. And
  10446. 8:52:58we'll name it the same money, but this
  10447. 8:53:01time we'll end with test.js.
  10448. 8:53:07So, the naming convention is to put it
  10449. 8:53:09beside the original file and end it with
  10450. 8:53:13test.js.
  10451. 8:53:15Now inside this test file at the top
  10452. 8:53:18we'll import curly brackets
  10453. 8:53:22it
  10454. 8:53:23from a string v test.
  10455. 8:53:28So it lets us create a test and we'll
  10456. 8:53:32also import
  10457. 8:53:34the format
  10458. 8:53:36money function from a string and import
  10459. 8:53:40it from this file dot / money.
  10460. 8:53:46And now to create our first test, we're
  10461. 8:53:49going to use it and brackets and we'll
  10462. 8:53:52give this a string.
  10463. 8:53:55So this creates a test and the string
  10464. 8:53:58here is the name of the test. So this
  10465. 8:54:01describes what we are testing. So let's
  10466. 8:54:04give it a name like formats1
  10467. 8:54:081999
  10468. 8:54:10cents as $119.99.
  10469. 8:54:16If we run format money with 19.99, it
  10470. 8:54:19should give us $119.99
  10471. 8:54:22as the result. So this name is
  10472. 8:54:25describing what we want to test and a
  10473. 8:54:28naming convention is to make this read
  10474. 8:54:30like English. For example, it formats 1
  10475. 8:54:341999 as 19.99.
  10476. 8:54:37And then we're going to give this a
  10477. 8:54:39function brackets arrow and curly
  10478. 8:54:42brackets.
  10479. 8:54:44And in here we're going to test if
  10480. 8:54:46format money works correctly.
  10481. 8:54:49To do that, we're also going to import
  10482. 8:54:53expect from vest. So expect lets us
  10483. 8:54:58check if the result is correct. So in
  10484. 8:55:01here we're going to run expect and
  10485. 8:55:04brackets.
  10486. 8:55:06And in here we put the code that we want
  10487. 8:55:09to test. For example, let's put the code
  10488. 8:55:12format money brackets and 1 n99.
  10489. 8:55:19And then expect has different methods
  10490. 8:55:21that we can use to check this result.
  10491. 8:55:24One method is called dot to b and
  10492. 8:55:29brackets.
  10493. 8:55:32So in this second bracket we can put the
  10494. 8:55:35expected result of this code. So we said
  10495. 8:55:39that the expected result should be this
  10496. 8:55:41string here. So we'll give it
  10497. 8:55:44dollar19.99.
  10498. 8:55:48So this code tests if this value is
  10499. 8:55:51equal to this value. If they are equal,
  10500. 8:55:55this check has passed. If they are not
  10501. 8:55:58equal, this check has failed.
  10502. 8:56:01And then expect is going to send this
  10503. 8:56:04result to vest. and V test will display
  10504. 8:56:08a summary of what passed and what
  10505. 8:56:10failed.
  10506. 8:56:12So now let's run this test. We're going
  10507. 8:56:15to save this file
  10508. 8:56:17and then in our command line, we're
  10509. 8:56:20going to create a new tab for running
  10510. 8:56:22the tests.
  10511. 8:56:24And first we have to be inside the
  10512. 8:56:26e-commerce project folder. So we'll cd
  10513. 8:56:30ecommerce
  10514. 8:56:32dash project
  10515. 8:56:35and we're going to run npx
  10516. 8:56:39vest.
  10517. 8:56:43So v test is going to search our code
  10518. 8:56:46for all the files that end with test.js
  10519. 8:56:50and runs the tests inside and then it
  10520. 8:56:53displays the result in the command line.
  10521. 8:56:56So this tells us that our format money
  10522. 8:56:59test here has passed.
  10523. 8:57:03Now let's create another test for format
  10524. 8:57:06money. We'll go back to this test file
  10525. 8:57:09and to create another test, we're just
  10526. 8:57:12going to use it again
  10527. 8:57:15and a string. And remember that we had
  10528. 8:57:19some problems with money not showing two
  10529. 8:57:21decimals.
  10530. 8:57:23So let's test if it displays two
  10531. 8:57:28decimals
  10532. 8:57:31and give it a function.
  10533. 8:57:34And now inside here we can run expect
  10534. 8:57:38and brackets
  10535. 8:57:41format money brackets. And let's give it
  10536. 8:57:45a value that might not show two decimals
  10537. 8:57:49like 1 090.
  10538. 8:57:53And then we'll check if this is correct
  10539. 8:57:55using
  10540. 8:57:572 tob and brackets and a string. And the
  10541. 8:58:03expected result of this code should be
  10542. 8:58:05dollar 10.90.
  10543. 8:58:09So we're going to check if this result
  10544. 8:58:11is equal to this result. If we save,
  10545. 8:58:17vest is actually watching our files for
  10546. 8:58:20changes. So when we save it will
  10547. 8:58:22automatically rerun our tests. And now
  10548. 8:58:25you can see we have two tests that
  10549. 8:58:27passed.
  10550. 8:58:30Next inside each test we can have
  10551. 8:58:33multiple checks. So in here let's add
  10552. 8:58:36another check using expect brackets.
  10553. 8:58:41And let's try format money brackets with
  10554. 8:58:46a different value like 100. So we expect
  10555. 8:58:51this to be dollar1.00.
  10556. 8:58:54So we'll do 2v brackets dollar 1.0
  10557. 8:59:03and save.
  10558. 8:59:05Now v test will rerun all of our tests.
  10559. 8:59:08And now you can see that both tests
  10560. 8:59:10still passed. So when we have multiple
  10561. 8:59:13checks like this, all of the checks have
  10562. 8:59:16to pass in order for this test to pass.
  10563. 8:59:20If one of these checks fails, the entire
  10564. 8:59:22test will fail and it will show up here.
  10565. 8:59:25So as a quick example, if I change this
  10566. 8:59:28and save,
  10567. 8:59:30one of these checks failed. So we can
  10568. 8:59:31see that this test has failed. So I'm
  10569. 8:59:35going to add it back and save.
  10570. 8:59:42Next, if we look at our file here,
  10571. 8:59:45notice that these tests are related to
  10572. 8:59:48the format money function. A best
  10573. 8:59:51practice is to group these tests
  10574. 8:59:53together. To do that, at the top, we're
  10575. 8:59:57also going to import describe
  10576. 9:00:01from vest. So, describe groups test
  10577. 9:00:05together. we call a group of tests a
  10578. 9:00:09test suite.
  10579. 9:00:11So above these tests, we're going to use
  10580. 9:00:14the code describe and brackets and we'll
  10581. 9:00:18give this a string
  10582. 9:00:21and this is the name of the test suite
  10583. 9:00:23or the group of tests. So these tests
  10584. 9:00:26are for the format money function and
  10585. 9:00:31we'll give it a function brackets arrow
  10586. 9:00:34and curly brackets
  10587. 9:00:36and we're going to move our test inside.
  10588. 9:00:39So we'll select it and cut and paste
  10589. 9:00:44and save
  10590. 9:00:47and everything still passes
  10591. 9:00:50and we can see the test suite name over
  10592. 9:00:53here. So test suites basically help us
  10593. 9:00:56keep our tests more organized. If we
  10594. 9:00:59want to test a different function inside
  10595. 9:01:01this file, we would create another test
  10596. 9:01:04suite and group our tests in there. So a
  10597. 9:01:07best practice is to always put our tests
  10598. 9:01:10inside a test suite, even if we only
  10599. 9:01:12have one test suite. So now let's save
  10600. 9:01:16this into git. We're going to go to the
  10601. 9:01:19get section
  10602. 9:01:21and let's use the message create unit
  10603. 9:01:25test for format
  10604. 9:01:29money and commit.
  10605. 9:01:36Next, we'll create another type of test
  10606. 9:01:40called an integration test. Integration
  10607. 9:01:43test means we test multiple units or
  10608. 9:01:46pieces of code working together.
  10609. 9:01:50For example, if we look at our files and
  10610. 9:01:53we open the product component,
  10611. 9:01:57testing a component would be considered
  10612. 9:01:59an integration test. Notice this uses
  10613. 9:02:03multiple pieces of code like the format
  10614. 9:02:06money function and the load cart
  10615. 9:02:08function.
  10616. 9:02:10So now let's create tests for this file.
  10617. 9:02:13Beside product.js,
  10618. 9:02:15we're going to create a new file
  10619. 9:02:18and we'll name it product.est.jsx.
  10620. 9:02:28So we end with JSX this time because
  10621. 9:02:31this will let us use component syntax in
  10622. 9:02:34this file like this.
  10623. 9:02:36So, first let's actually copy the
  10624. 9:02:38imports from our money test file because
  10625. 9:02:42we're going to be using these again. So,
  10626. 9:02:44we'll copy
  10627. 9:02:46and in here
  10628. 9:02:49we'll paste,
  10629. 9:02:51but this time we're going to import the
  10630. 9:02:55product component from the dot /rouct
  10631. 9:03:00file.
  10632. 9:03:04And first we're going to create a test
  10633. 9:03:06suite. So we use describe brackets and a
  10634. 9:03:11string. And we're going to be testing
  10635. 9:03:13the product component.
  10636. 9:03:16And give this a function.
  10637. 9:03:20And inside we'll create our first
  10638. 9:03:22integration test using it brackets and a
  10639. 9:03:26string.
  10640. 9:03:28And for components, the first thing we
  10641. 9:03:30can check is if the component is
  10642. 9:03:33displayed correctly. So for the product
  10643. 9:03:36component, we can check if the name is
  10644. 9:03:38correct, the image is correct and so on.
  10645. 9:03:41So we'll name this test it
  10646. 9:03:45displays the product details correctly.
  10647. 9:03:52And we'll give it a function.
  10648. 9:03:56And now inside here, how do we check if
  10649. 9:03:59this component displayed the details
  10650. 9:04:02correctly? So when testing a function,
  10651. 9:04:05we run the function and check the
  10652. 9:04:07result. When testing a component, we
  10653. 9:04:10render the component and check the
  10654. 9:04:13result. So render means to display the
  10655. 9:04:16component on the page. To render the
  10656. 9:04:19component in our test, we need to add
  10657. 9:04:22some more npm packages. So, we're going
  10658. 9:04:25to go to our command line and right now
  10659. 9:04:29vest is watching all of our files for
  10660. 9:04:31changes. So, we're going to stop vest
  10661. 9:04:34using C or you can press Q to quit.
  10662. 9:04:39And now we're going to click this link
  10663. 9:04:41in the description, go to lesson 9, and
  10664. 9:04:44we're going to install all of these npm
  10665. 9:04:47packages that we need. So, we're going
  10666. 9:04:50to select this and copy. And then in our
  10667. 9:04:54command line, we're going to paste this
  10668. 9:04:56command
  10669. 9:04:58and press enter to install all these
  10670. 9:05:01packages.
  10671. 9:05:04After installing, we also need to create
  10672. 9:05:07some config files to test our
  10673. 9:05:09components. So, we'll go to our files
  10674. 9:05:13and we'll scroll down and find
  10675. 9:05:16vit.config.js
  10676. 9:05:18and click it. And we're going to create
  10677. 9:05:20a new file and name this
  10678. 9:05:23vtest.config.js.
  10679. 9:05:30And we'll click this link in the
  10680. 9:05:31description and go to lesson 9. We're
  10681. 9:05:35going to copy this setup code. So we're
  10682. 9:05:38going to select this and copy.
  10683. 9:05:42And then in here we'll paste that code
  10684. 9:05:47and save.
  10685. 9:05:50And we're also going to create another
  10686. 9:05:52file beside this.
  10687. 9:05:54So here create a new file called setup
  10688. 9:05:58capital T tests.js.
  10689. 9:06:04And we'll click the link in the
  10690. 9:06:06description again and go to lesson 9.
  10691. 9:06:09And we're going to copy this code
  10692. 9:06:13and paste it in here
  10693. 9:06:16and save.
  10694. 9:06:19So that's just some setup that we need
  10695. 9:06:21to do. If we look at this config file,
  10696. 9:06:25you don't have to worry too much about
  10697. 9:06:26how this works. This is just some
  10698. 9:06:28standard setup. These two are required
  10699. 9:06:32for component tests or integration tests
  10700. 9:06:34to work. And this basically runs the
  10701. 9:06:38code inside this file before all of our
  10702. 9:06:41tests. So in this file we import just
  10703. 9:06:45DOM and this basically adds a bunch of
  10704. 9:06:49methods to expect that help us test
  10705. 9:06:52components.
  10706. 9:06:55Now that we have the setup, we're going
  10707. 9:06:58to go back to our test
  10708. 9:07:01and at the top we're going to import
  10709. 9:07:05curly brackets render
  10710. 9:07:08from the package at testing-
  10711. 9:07:12library/react.
  10712. 9:07:16So this renders or displays a component
  10713. 9:07:20in a fake web page specifically for
  10714. 9:07:23testing.
  10715. 9:07:25Inside our test, we're going to use the
  10716. 9:07:28render function and brackets
  10717. 9:07:32and we'll tell it to render the product
  10718. 9:07:34component using less than product
  10719. 9:07:38slashgreater than.
  10720. 9:07:41So next the product component has some
  10721. 9:07:44props. So we need to provide these props
  10722. 9:07:47in our test. So in here we're going to
  10723. 9:07:50add the prop product equals curly
  10724. 9:07:54brackets
  10725. 9:07:55and we're going to provide a sample
  10726. 9:07:57product to test with. To do that in our
  10727. 9:08:01files we're going to go to the starting
  10728. 9:08:03code folder and open the data folder and
  10729. 9:08:07open products.js.
  10730. 9:08:10And here we have a bunch of sample
  10731. 9:08:12products that we can use. So, let's just
  10732. 9:08:15select the first product and copy it.
  10733. 9:08:20And then in our test,
  10734. 9:08:23we're going to save the sample product
  10735. 9:08:26in a variable const product equals and
  10736. 9:08:31paste.
  10737. 9:08:34And we're going to take this product and
  10738. 9:08:37save it as the product prop.
  10739. 9:08:41Next, we also need to provide a load
  10740. 9:08:44cart function to this component. So in
  10741. 9:08:47our app, load cart actually contacts the
  10742. 9:08:51backend. However, in our tests, we
  10743. 9:08:54should not contact a real backend
  10744. 9:08:56because the backend might not be
  10745. 9:08:58available during tests. In our tests
  10746. 9:09:01might accidentally update real data. So
  10747. 9:09:05instead, a best practice is to mock this
  10748. 9:09:08function in our tests. Mock means to
  10749. 9:09:12create a fake version of this function.
  10750. 9:09:15To create a mock at the top, we're going
  10751. 9:09:19to import vi from vest.
  10752. 9:09:24And we'll scroll back down and we'll
  10753. 9:09:27create const load cart equals vi.fn. fn
  10754. 9:09:34and brackets.
  10755. 9:09:37So, vi.fn
  10756. 9:09:39basically creates a fake function that
  10757. 9:09:42doesn't do anything. So, this fake
  10758. 9:09:45function is called a mock and we're
  10759. 9:09:48going to give this mock to the
  10760. 9:09:50component. So, here we'll create a prop
  10761. 9:09:53load cart equals curly bracket and our
  10762. 9:09:57mock load cart. And this will prevent
  10763. 9:10:02our component from accidentally
  10764. 9:10:04contacting the back end in our tests.
  10765. 9:10:07And that's it. This will render the
  10766. 9:10:10product component on a fake web page. So
  10767. 9:10:14now, how do we check if this component
  10768. 9:10:16was displayed correctly? We can't see
  10769. 9:10:20the fake web page, but we can check it
  10770. 9:10:22using our code. To do that, we're going
  10771. 9:10:25to scroll up
  10772. 9:10:27and we'll also import the screen from
  10773. 9:10:31testing library/react.
  10774. 9:10:34So screen lets us check the screen or
  10775. 9:10:38the fake web page to see if everything
  10776. 9:10:40was rendered correctly
  10777. 9:10:43in our test. After we render the
  10778. 9:10:46component, we're going to use screen and
  10779. 9:10:50the method
  10780. 9:10:52get by text and brackets.
  10781. 9:10:56So this searches the screen or the fake
  10782. 9:11:00web page for an element with a specific
  10783. 9:11:04text. So we'll give it a string
  10784. 9:11:07and first let's check if the product
  10785. 9:11:09name is displayed correctly. So, we'll
  10786. 9:11:13scroll up and we're going to copy this
  10787. 9:11:16product name.
  10788. 9:11:19And then in here, we're going to paste.
  10789. 9:11:23And this will search the screen or the
  10790. 9:11:26fake web page for an element with this
  10791. 9:11:29text inside.
  10792. 9:11:31So, now we're going to check if this
  10793. 9:11:32element exists. That will tell us if we
  10794. 9:11:35displayed the correct product on the
  10795. 9:11:37page. To do that before this code, we'll
  10796. 9:11:41use expect and open bracket
  10797. 9:11:46and at the end a close bracket and we'll
  10798. 9:11:50use the method dot to be in the document
  10799. 9:11:57and brackets.
  10800. 9:11:59So this method is added by testing
  10801. 9:12:02library/jestdom
  10802. 9:12:04and this checks if this element exists
  10803. 9:12:08on the screen or on the fake web page.
  10804. 9:12:12So now let's save
  10805. 9:12:14and we'll run this test using vest in
  10806. 9:12:18the command line. We'll run npx v test.
  10807. 9:12:27And all of our tests passed.
  10808. 9:12:31Next, let's add some more checks to this
  10809. 9:12:34test. So, let's also check if the price
  10810. 9:12:37was displayed correctly. So, here we'll
  10811. 9:12:40add another expect brackets and we'll
  10812. 9:12:45use screen.get get by text
  10813. 9:12:49and a string and we're going to search
  10814. 9:12:52for the price on the screen. So for the
  10815. 9:12:55sample product, the price should be
  10816. 9:12:58dollar10.90.
  10817. 9:13:01So, we'll scroll down and search for
  10818. 9:13:04dollar 10.90 on the screen. And we
  10819. 9:13:08expect this dot to be in the document
  10820. 9:13:14and brackets
  10821. 9:13:17and save
  10822. 9:13:20and feed will rerun the test and tell us
  10823. 9:13:23that this passed.
  10824. 9:13:25Next, let's check if the product image
  10825. 9:13:28is correct.
  10826. 9:13:29So down here we'll use another expect
  10827. 9:13:33brackets
  10828. 9:13:35screen dot but this time we can't use
  10829. 9:13:39get by text because we're trying to find
  10830. 9:13:42an image. Instead we're going to add a
  10831. 9:13:45test ID to the image element. So in our
  10832. 9:13:49files we'll open the product component
  10833. 9:13:53and we're going to scroll down until we
  10834. 9:13:56find the product image here.
  10835. 9:14:00And for this element, we're going to add
  10836. 9:14:02an attribute data-
  10837. 9:14:05test id equals a string product dash
  10838. 9:14:11image. So this ID lets us find the image
  10839. 9:14:15element in our tests. We could also use
  10840. 9:14:18the class name to find this element, but
  10841. 9:14:21if we change our class name, it will
  10842. 9:14:23break the test. To avoid this, we
  10843. 9:14:25usually use an ID specifically for
  10844. 9:14:28testing like this. So now let's save.
  10845. 9:14:33And in our test, we'll use screen dot
  10846. 9:14:38get by test ID and brackets and a string
  10847. 9:14:44and the test ID product dash image. So
  10848. 9:14:49this will give us the image element in
  10849. 9:14:51the product component.
  10850. 9:14:54Now let's check if this image element
  10851. 9:14:56source attribute is equal to the
  10852. 9:15:00products image up here. So we'll select
  10853. 9:15:04this image and copy
  10854. 9:15:09and then down here
  10855. 9:15:12dot to have attribute
  10856. 9:15:18brackets and we'll check the source
  10857. 9:15:21attribute if it's equal to the string
  10858. 9:15:24and paste
  10859. 9:15:26the image from our sample product
  10860. 9:15:30and Save
  10861. 9:15:33V test will rerun our tests and
  10862. 9:15:35everything passed. So we know we're
  10863. 9:15:37displaying the correct image.
  10864. 9:15:40Next, let's check the ratings image and
  10865. 9:15:42the rating count. Feel free to pause if
  10866. 9:15:45you want to try it yourself first. So
  10867. 9:15:48first we'll do expect
  10868. 9:15:52open bracket the screen dot and we'll
  10869. 9:15:56check the ratings image first.
  10870. 9:15:59So, we'll go to the product component
  10871. 9:16:02and we're going to scroll down until we
  10872. 9:16:04find the image product rating stars and
  10873. 9:16:08we'll give this a data dash test id
  10874. 9:16:12equals
  10875. 9:16:14product rating stars image and save.
  10876. 9:16:21And let's just select this test ID and
  10877. 9:16:24copy.
  10878. 9:16:26And in our test,
  10879. 9:16:28we'll get by test ID brackets a string
  10880. 9:16:35and paste.
  10881. 9:16:38And we expect this dot to have attribute
  10882. 9:16:43brackets the source attribute equals a
  10883. 9:16:47string.
  10884. 9:16:48And we'll go back to the product
  10885. 9:16:50component.
  10886. 9:16:52And let's just copy the source attribute
  10887. 9:16:54here. So we'll select it and copy.
  10888. 9:16:59And inside our test we're going to
  10889. 9:17:02paste.
  10890. 9:17:05Now for our sample product we have a
  10891. 9:17:08rating of
  10892. 9:17:104.5.
  10893. 9:17:12So that means we take 4.5 and multiply
  10894. 9:17:15it by 10. We're going to get the value
  10895. 9:17:1945.
  10896. 9:17:21And this is the rating image that should
  10897. 9:17:23be displayed on the page. So we'll save.
  10898. 9:17:29And v test tells us everything passed.
  10899. 9:17:32Finally, let's check the rating count.
  10900. 9:17:36So we'll expect brackets screen dot. And
  10901. 9:17:41this time we're looking for text. So we
  10902. 9:17:43can just use get by text brackets. And
  10903. 9:17:49for this sample product, the count is
  10904. 9:17:5287.
  10905. 9:17:54So we'll look for the string 87.
  10906. 9:17:58And we expect this to be in the document
  10907. 9:18:06and save.
  10908. 9:18:09Vest will rerun and everything passed.
  10909. 9:18:12So now we tested if the product
  10910. 9:18:15component displayed all the details
  10911. 9:18:17correctly. So let's save this into git.
  10912. 9:18:21We'll open the git section
  10913. 9:18:24and we'll use the message create
  10914. 9:18:27integration test for product component
  10915. 9:18:32and commit.
  10916. 9:18:39Next we're going to test user
  10917. 9:18:41interactions.
  10918. 9:18:42In the product component, we have an add
  10919. 9:18:45to cart button. We can also test
  10920. 9:18:48clicking this add to cart button to see
  10921. 9:18:50if it behaves correctly.
  10922. 9:18:53So in our test file, let's create a new
  10923. 9:18:56test using it brackets and a string. And
  10924. 9:19:02let's name it adds a product to the cart
  10925. 9:19:08and give it a function.
  10926. 9:19:13So first let's render the product
  10927. 9:19:16component again. So we'll scroll up
  10928. 9:19:20and let's just copy all of this code. So
  10929. 9:19:23we'll select it
  10930. 9:19:26to here and copy.
  10931. 9:19:29And then in our second test,
  10932. 9:19:32we're going to paste.
  10933. 9:19:35So this sets up the sample product and
  10934. 9:19:38the mock and then renders the product
  10935. 9:19:40component again. So now that we rendered
  10936. 9:19:43this, how do we click the add to cart
  10937. 9:19:46button in our test?
  10938. 9:19:48Earlier we installed a package called
  10939. 9:19:51testing library/ user event. This
  10940. 9:19:55package lets us simulate events like
  10941. 9:19:58clicking a button. So at the top of our
  10942. 9:20:01test, we're going to scroll up and we'll
  10943. 9:20:05import user event from the package at
  10944. 9:20:11testing- library/
  10945. 9:20:14user-event.
  10946. 9:20:18So this lets us simulate events like a
  10947. 9:20:20click.
  10948. 9:20:22So now we'll scroll back down to our
  10949. 9:20:24test
  10950. 9:20:26and first we need to set up user event
  10951. 9:20:30using const user equals user event dot
  10952. 9:20:35setup and brackets.
  10953. 9:20:39So now we can simulate events using
  10954. 9:20:43user.click.
  10955. 9:20:47So this simulates a click event and
  10956. 9:20:50inside the brackets we're going to tell
  10957. 9:20:52it which element to click. So we want it
  10958. 9:20:55to click the add to cart button. So
  10959. 9:20:58first we need to get the add to cart
  10960. 9:21:00button inside our test. We'll go to our
  10961. 9:21:04files
  10962. 9:21:06and in the product component,
  10963. 9:21:09let's scroll down to the add to cart
  10964. 9:21:12button and we're going to give this a
  10965. 9:21:15data dash test ID equals a string add to
  10966. 9:21:22cart button.
  10967. 9:21:25And let's select this test ID and copy
  10968. 9:21:29and save.
  10969. 9:21:32And then in the product test, we're
  10970. 9:21:35going to get the button using screen dot
  10971. 9:21:40get by test ID brackets
  10972. 9:21:45and a string and we'll paste the test
  10973. 9:21:48ID.
  10974. 9:21:50So this will get the add to cart button
  10975. 9:21:52from the screen.
  10976. 9:21:54So let's save it in a variable const add
  10977. 9:21:58to cart button
  10978. 9:22:01equals
  10979. 9:22:03and we're going to give this to
  10980. 9:22:04user.click
  10981. 9:22:06add to cart button.
  10982. 9:22:11Next when we click this button it takes
  10983. 9:22:14some time for the page to process the
  10984. 9:22:17click. So user.click takes some time to
  10985. 9:22:21finish. That means this is asynchronous
  10986. 9:22:24code and it returns a promise. To wait
  10987. 9:22:28for this to finish at the front, we're
  10988. 9:22:31going to use await.
  10989. 9:22:33And remember to use await we need to be
  10990. 9:22:36inside an async function. So above in
  10991. 9:22:40this function, we're going to make it
  10992. 9:22:43async.
  10993. 9:22:45And we'll scroll back down.
  10994. 9:22:49And now once we click the add to cart
  10995. 9:22:52button, this is going to make a request
  10996. 9:22:54to the back end using axios.post.
  10997. 9:22:58But remember in our tests we should not
  10998. 9:23:01contact a real backend because we might
  10999. 9:23:03accidentally update real data.
  11000. 9:23:07So earlier we mocked load cart and
  11001. 9:23:10replaced it with a fake function.
  11002. 9:23:13Now we're also going to mock axios. So
  11003. 9:23:17we can actually mock an entire npm
  11004. 9:23:19package. To do that at the top,
  11005. 9:23:25we're going to use the code vi
  11006. 9:23:28mock brackets
  11007. 9:23:31and the string axios.
  11008. 9:23:34So this mocks the entire axios package.
  11009. 9:23:38This means that when we are testing,
  11010. 9:23:40when we import from axios, we will get a
  11011. 9:23:43fake version of axios instead.
  11012. 9:23:46So when we click add to cart, it will
  11013. 9:23:49use the fake version of Axios and it
  11014. 9:23:52will do nothing. It will not contact the
  11015. 9:23:54back end. So now what should we test? We
  11016. 9:23:59can still test that our code ran
  11017. 9:24:02axios.post
  11018. 9:24:03and we can test if we gave axios.post
  11019. 9:24:06the correct values.
  11020. 9:24:09To do that at the top, we're also going
  11021. 9:24:12to import Axios from the package Axios.
  11022. 9:24:19And because of vi.mmock,
  11023. 9:24:22this is a fake version of Axios.
  11024. 9:24:25So now we can scroll back down
  11025. 9:24:30and after we click add to cart, we can
  11026. 9:24:34expect brackets axios.post. post.
  11027. 9:24:40If we give a mock to expect, we can
  11028. 9:24:44check if our code ran axios.post
  11029. 9:24:47and if we gave it the correct values
  11030. 9:24:51using the method dot to have been called
  11031. 9:24:57with and brackets.
  11032. 9:25:01So we expect our code to call axios.post
  11033. 9:25:04with /api/cart items. So in here we'll
  11034. 9:25:09check if the first value we gave it was
  11035. 9:25:12slappi/cart-
  11036. 9:25:15items
  11037. 9:25:17and we also expect our code to give
  11038. 9:25:20axios.post an object with a product ID
  11039. 9:25:24and a quantity.
  11040. 9:25:26So in here we can also check if we give
  11041. 9:25:28it an object with a product ID colon and
  11042. 9:25:34we're going to get the product ID of our
  11043. 9:25:37sample product. So here we'll select
  11044. 9:25:40this ID and copy
  11045. 9:25:45and scroll back down
  11046. 9:25:47and create a string and paste the ID
  11047. 9:25:52and we expect the quantity colon to be
  11048. 9:25:56one because we did not change the
  11049. 9:25:59quantity selector.
  11050. 9:26:01So this basically checks that when we
  11051. 9:26:03click add to cart does our code run
  11052. 9:26:06axios.post post and give it these
  11053. 9:26:09values. So even though mocks don't do
  11054. 9:26:12anything, we can still check if we
  11055. 9:26:15called them and we gave them the correct
  11056. 9:26:17values.
  11057. 9:26:19Next, we can also check if our code ran
  11058. 9:26:22load cart. So remember that load cart is
  11059. 9:26:26also a mock. So we can see if our code
  11060. 9:26:29ran load cart. So down here we can also
  11061. 9:26:34expect
  11062. 9:26:36load cart and we don't give load cart
  11063. 9:26:40any values. So we can just use dot2
  11064. 9:26:45have been called and brackets.
  11065. 9:26:49So this just checks if our code called
  11066. 9:26:52load cart and does not check what values
  11067. 9:26:54we gave it. So now let's save
  11068. 9:26:59and v test will run our code and
  11069. 9:27:02everything still passed. So that's how
  11070. 9:27:05we test user interactions. We create
  11071. 9:27:08some mocks and then we check if our code
  11072. 9:27:11ran the mock and gave it the correct
  11073. 9:27:13values. So now let's save this into git.
  11074. 9:27:18In the get section, we'll use a message
  11075. 9:27:21test clicking add to cart button
  11076. 9:27:27and commit.
  11077. 9:27:32Before we move on, we're going to do
  11078. 9:27:34some code cleanup. Notice that in each
  11079. 9:27:38of our tests, we have some setup code at
  11080. 9:27:41the top where we create some sample data
  11081. 9:27:44and mock load cart. So we can actually
  11082. 9:27:47share this setup code between our tests.
  11083. 9:27:51To do that, we can move these two
  11084. 9:27:53variables outside of this it function.
  11085. 9:27:57So we'll select this code
  11086. 9:28:02and cut.
  11087. 9:28:05And outside of this function, we'll
  11088. 9:28:08paste.
  11089. 9:28:11Now that these two variables are outside
  11090. 9:28:14of this function because of scope these
  11091. 9:28:17can be accessed anywhere inside this
  11092. 9:28:20outer describe function. So that means
  11093. 9:28:23it can be accessed in both tests. So in
  11094. 9:28:26the second test
  11095. 9:28:29we can just remove this setup code
  11096. 9:28:32because now it's shared between the two
  11097. 9:28:35tests up here.
  11098. 9:28:38Another recommendation is to recreate
  11099. 9:28:40shared variables before each test
  11100. 9:28:43because if one test modifies a variable,
  11101. 9:28:47that change is going to carry over into
  11102. 9:28:49the next test which can cause bugs.
  11103. 9:28:53So before each test, we're going to
  11104. 9:28:55recreate these variables.
  11105. 9:28:58To do that, we can use before capital E
  11106. 9:29:03each brackets. And we'll give this a
  11107. 9:29:06function brackets arrow and curly
  11108. 9:29:09brackets.
  11109. 9:29:11So before each runs some code before
  11110. 9:29:14each test
  11111. 9:29:16and also we need to import before each.
  11112. 9:29:19So at the top
  11113. 9:29:21we'll import before each from vest.
  11114. 9:29:29So we want to recreate these shared
  11115. 9:29:31variables before each test. So we're
  11116. 9:29:34going to do that in here. Inside we'll
  11117. 9:29:37use product equals
  11118. 9:29:40and let's just select this value and
  11119. 9:29:44cut.
  11120. 9:29:46And in here we'll paste.
  11121. 9:29:51And because we're modifying this
  11122. 9:29:53variable, we need to change from const
  11123. 9:29:55to let.
  11124. 9:29:58And we'll also recreate load cart in
  11125. 9:30:00before each. So here we'll do load cart
  11126. 9:30:04equals vi.fn
  11127. 9:30:07and brackets
  11128. 9:30:10and above we can remove this
  11129. 9:30:13and change this to a let.
  11130. 9:30:17So now before each test we're going to
  11131. 9:30:21recreate the setup code and we'll have a
  11132. 9:30:24fresh set of variables and this will
  11133. 9:30:26make our tests more consistent. So now
  11134. 9:30:29let's save.
  11135. 9:30:32V test will rerun our test and
  11136. 9:30:35everything still passes.
  11137. 9:30:37So before each is known as a test hook.
  11138. 9:30:41There are other test hooks that we can
  11139. 9:30:42use to help us do some setup or some
  11140. 9:30:45cleanup in our tests.
  11141. 9:30:48So now let's save this into git. In the
  11142. 9:30:51get section, we'll use the message use
  11143. 9:30:55before each to remove duplication
  11144. 9:31:01and commit.
  11145. 9:31:07So, we just learned how to create mocks
  11146. 9:31:10that don't do anything. Now, we're going
  11147. 9:31:13to learn how to mock the implementation.
  11148. 9:31:16Mock the implementation means we make
  11149. 9:31:19the mock do whatever we want. To do
  11150. 9:31:22this, we're going to test another
  11151. 9:31:24component. So in our file,
  11152. 9:31:27we're going to go to the homepage
  11153. 9:31:29component.
  11154. 9:31:31And here we're going to create a new
  11155. 9:31:33file
  11156. 9:31:35and name it homepage.est.jsx.
  11157. 9:31:44And let's just copy all the imports from
  11158. 9:31:46the product component test because we'll
  11159. 9:31:49use most of them. So we'll select this
  11160. 9:31:52and copy.
  11161. 9:31:55And in homepage.test
  11162. 9:31:57we're going to paste.
  11163. 9:32:00And this time we'll import homepage
  11164. 9:32:04from dot /home page.
  11165. 9:32:09And as always, we'll start by creating a
  11166. 9:32:11test suite with describe.
  11167. 9:32:15And we'll name this homepage
  11168. 9:32:18component
  11169. 9:32:20and a function.
  11170. 9:32:23And we'll create a test using it
  11171. 9:32:26brackets. And let's call it displays the
  11172. 9:32:31products correctly.
  11173. 9:32:35Brackets, arrow, and curly brackets.
  11174. 9:32:39And now let's render the homepage using
  11175. 9:32:42render bracket less than homepage
  11176. 9:32:47slashgreater than.
  11177. 9:32:50And the homepage requires two props cart
  11178. 9:32:54and load cart. So for the cart equals
  11179. 9:32:58curly brackets. We're not going to test
  11180. 9:33:00the cart right now. So we'll just set it
  11181. 9:33:03as an empty array.
  11182. 9:33:05And for load cart equals curly brackets
  11183. 9:33:10again we'll mock load cart. So above
  11184. 9:33:13this test
  11185. 9:33:15we'll create let node cart and we'll use
  11186. 9:33:20before each brackets
  11187. 9:33:24and a function brackets arrow and curly
  11188. 9:33:27brackets
  11189. 9:33:29and we'll set load cart equals a mock
  11190. 9:33:33function vi.fn Fn and brackets
  11191. 9:33:38and we're going to give this mock to the
  11192. 9:33:41homepage component.
  11193. 9:33:45Next, the homepage component is special.
  11194. 9:33:49If we open the homepage,
  11195. 9:33:52notice at the start, we load products
  11196. 9:33:56from the back end using
  11197. 9:33:58axios.get/appi/roucts.
  11198. 9:34:01Again, in our test, we should not
  11199. 9:34:03contact a real backend. So, we're going
  11200. 9:34:06to mock Axios.
  11201. 9:34:09So, here
  11202. 9:34:11at the top, we'll mock Axios using
  11203. 9:34:14vi.mmock
  11204. 9:34:16brackets and the package axios.
  11205. 9:34:20Now, this just makes axios.get do
  11206. 9:34:23nothing. However, in the homepage, we
  11207. 9:34:27actually need axios.get get to return
  11208. 9:34:30something because we're going to return
  11209. 9:34:32a response and the homepage is going to
  11210. 9:34:35use this response to display the
  11211. 9:34:38products. So here axios.get cannot just
  11212. 9:34:42do nothing. We need to mock the
  11213. 9:34:44implementation which means we're going
  11214. 9:34:47to make axio.get do whatever we want.
  11215. 9:34:51So in our test inside before each
  11216. 9:34:56we're going to do axials.get
  11217. 9:35:00dot mock implementation
  11218. 9:35:05and brackets
  11219. 9:35:07and we're going to give this a function
  11220. 9:35:09brackets arrow and curly brackets.
  11221. 9:35:13So this means that in our code whenever
  11222. 9:35:16we use axios.get get it's going to run
  11223. 9:35:19this fake function instead. So basically
  11224. 9:35:22we can make axios.get do whatever we
  11225. 9:35:25want. That's what mock implementation
  11226. 9:35:28means.
  11227. 9:35:30So remember we can give axios.get some
  11228. 9:35:34values. Those values are going to be
  11229. 9:35:36saved as parameters in here. So the
  11230. 9:35:40first value we give to axios.get is the
  11231. 9:35:43URL path. So in here our fake function
  11232. 9:35:46will also get the URL path.
  11233. 9:35:51Next inside our fake function let's
  11234. 9:35:54check if the URL path is equal to the
  11235. 9:36:00string /
  11236. 9:36:03products.
  11237. 9:36:06So for the URL path /api/proucts,
  11238. 9:36:11usually the backend returns all the
  11239. 9:36:13products in an array. But in our test,
  11240. 9:36:17we don't really need all the products.
  11241. 9:36:19So let's just return two products to
  11242. 9:36:21test with. So in here, we'll just return
  11243. 9:36:27two products. Now, whatever we return
  11244. 9:36:30from this fake function should match
  11245. 9:36:33what axios.get normally returns. So
  11246. 9:36:37axios.get
  11247. 9:36:38normally returns a response with a
  11248. 9:36:42property data. So in our test
  11249. 9:36:46this mock implementation should also
  11250. 9:36:49return an object with a property data.
  11251. 9:36:56And normally
  11252. 9:36:58response data is an array of products.
  11253. 9:37:02So in our test, our data property should
  11254. 9:37:06also be an array.
  11255. 9:37:09So basically in this fake function, we
  11256. 9:37:12should match what axios.get normally
  11257. 9:37:15returns. And this makes sure that the
  11258. 9:37:17rest of the code still works with our
  11259. 9:37:20mock.
  11260. 9:37:21So now in our test,
  11261. 9:37:24let's just return two products to test
  11262. 9:37:26with. to find two sample products. We
  11263. 9:37:30can go back to the starting code folder
  11264. 9:37:33and the data and the products.js.
  11265. 9:37:37And here, let's just collapse the first
  11266. 9:37:39two products and we'll select them and
  11267. 9:37:43copy.
  11268. 9:37:45And inside the homepage test,
  11269. 9:37:48we're going to paste.
  11270. 9:37:51So now in our test, axio.get get will
  11271. 9:37:55return these two products.
  11272. 9:37:58Now there's one more thing we need to do
  11273. 9:38:01which is remember that axios.get
  11274. 9:38:04normally returns a promise that we have
  11275. 9:38:07to await. So in our test we should also
  11276. 9:38:11return a promise. To make this return a
  11277. 9:38:14promise we can wrap this value in
  11278. 9:38:17promise.resolve like this. Or an easier
  11279. 9:38:21way to do this is to make this function
  11280. 9:38:24async.
  11281. 9:38:26So remember that async makes a function
  11282. 9:38:29return a promise.
  11283. 9:38:32Now axios.get will return exactly what
  11284. 9:38:35we want. And that's how we mock the
  11285. 9:38:37implementation which means we make the
  11286. 9:38:40mock do whatever we want.
  11287. 9:38:44Now let's run our tests to make sure
  11288. 9:38:46that this works. We'll save
  11289. 9:38:50and v test will rerun and we actually
  11290. 9:38:53got an error. So if we look at this
  11291. 9:38:57error and we scroll up
  11292. 9:39:01to the details of the error, you'll see
  11293. 9:39:04that we got this error here and it came
  11294. 9:39:07from React Router. And this happens
  11295. 9:39:10because if we look at our homepage
  11296. 9:39:14component,
  11297. 9:39:15remember that the homepage also contains
  11298. 9:39:18a header component. And if we open the
  11299. 9:39:22components folder and the header
  11300. 9:39:24component,
  11301. 9:39:26inside the header component, we have
  11302. 9:39:29these link components.
  11303. 9:39:31In order for a link components to work,
  11304. 9:39:34they need to be inside a router. In
  11305. 9:39:37main.jsx, JSX the entire app is inside a
  11306. 9:39:41router. So that's why link components
  11307. 9:39:44work normally but in our test our
  11308. 9:39:47homepage component is not inside any
  11309. 9:39:50router. So the link component doesn't
  11310. 9:39:53work and it creates an error. To fix
  11311. 9:39:56this we just need to put a router around
  11312. 9:39:59the homepage in our test. So, we'll go
  11313. 9:40:02back to our test
  11314. 9:40:05and first we'll scroll to the top and
  11315. 9:40:08we're going to import a router. So,
  11316. 9:40:10we'll import curly brackets and we're
  11317. 9:40:14going to import the memory router
  11318. 9:40:19from the package react-outer.
  11319. 9:40:23So, the memory router is specifically
  11320. 9:40:26for testing.
  11321. 9:40:28So we'll scroll down
  11322. 9:40:31and when we render the homepage, we're
  11323. 9:40:34going to put it inside a memory router.
  11324. 9:40:37So less than memory router
  11325. 9:40:42and we'll select the homepage and cut.
  11326. 9:40:46And in here we'll paste
  11327. 9:40:49and we'll fix the indenting.
  11328. 9:40:54And now if we save
  11329. 9:40:57the link components are inside a router.
  11330. 9:41:00And now our tests should pass.
  11331. 9:41:04Now let's check if the homepage
  11332. 9:41:06displayed the products correctly. So
  11333. 9:41:09after we render this homepage because we
  11334. 9:41:13mocked Axios to return these two sample
  11335. 9:41:16products, there should be two products
  11336. 9:41:19on the page. Let's add a test ID to
  11337. 9:41:22these elements so we can get them in our
  11338. 9:41:25test. In the product component,
  11339. 9:41:29we're going to scroll up to the top of
  11340. 9:41:33the HTML and we're going to find the
  11341. 9:41:36product container
  11342. 9:41:38and we're going to add the attribute
  11343. 9:41:40data- test id equals a string the
  11344. 9:41:45product dash container
  11345. 9:41:48and save.
  11346. 9:41:51And in our test,
  11347. 9:41:54we're going to look for the test ID
  11348. 9:41:57using screen.get
  11349. 9:42:01all by test ID and brackets and the test
  11350. 9:42:06ID product dash container.
  11351. 9:42:10So we use all here because we're going
  11352. 9:42:12to have multiple products on the page.
  11353. 9:42:15So we're going to get multiple product
  11354. 9:42:17container elements.
  11355. 9:42:19However, there's actually a problem
  11356. 9:42:21here.
  11357. 9:42:23When the homepage is first created, the
  11358. 9:42:26products are empty. So, there will not
  11359. 9:42:29be any products on the page. And we need
  11360. 9:42:32to wait for the homepage to run use
  11361. 9:42:34effect and load the products and then
  11362. 9:42:37update the state. So to wait for
  11363. 9:42:40homepage to finish loading in our test,
  11364. 9:42:45we're going to use a different method
  11365. 9:42:47called
  11366. 9:42:49find all by test ID.
  11367. 9:42:52So find does the same thing as get, but
  11368. 9:42:56it will wait until it finds this
  11369. 9:42:58element.
  11370. 9:43:00So this is useful when our component
  11371. 9:43:02needs to load something and we need to
  11372. 9:43:05wait for it to load. Now, because this
  11373. 9:43:08code is waiting until it finds these
  11374. 9:43:10elements, this code is asynchronous and
  11375. 9:43:14it returns a promise. So, we're going to
  11376. 9:43:16wait for this code to finish using a
  11377. 9:43:19wait. And again, to use a wait, we need
  11378. 9:43:23to be inside an async function.
  11379. 9:43:27So now this will wait until it finds
  11380. 9:43:30these product containers. And then let's
  11381. 9:43:33save these in a variable using const
  11382. 9:43:37product containers equals this result.
  11383. 9:43:43And now we can check this result to see
  11384. 9:43:46if it was displayed correctly. So down
  11385. 9:43:49here
  11386. 9:43:50let's check if we have two product
  11387. 9:43:53containers on the page. So we can expect
  11388. 9:43:57brackets the product containers.length
  11389. 9:44:04dot 2b
  11390. 9:44:062.
  11391. 9:44:08So this gives us an array of elements
  11392. 9:44:11and we're just checking if the length is
  11393. 9:44:13equal to two. If we save,
  11394. 9:44:18v test will rerun. And now this test
  11395. 9:44:21passed. So we know we're displaying the
  11396. 9:44:24correct number of elements on the
  11397. 9:44:26homepage.
  11398. 9:44:28Next, we'll check inside each product
  11399. 9:44:30container to see if they have the
  11400. 9:44:33correct product name. Now, before we
  11401. 9:44:36used screen get by text to search for
  11402. 9:44:39some text on the page. However, this
  11403. 9:44:43searches the entire page. Here we want
  11404. 9:44:47to check specifically the first product
  11405. 9:44:49container to see if it has the correct
  11406. 9:44:52name. To do that we can use a feature
  11407. 9:44:55called within. So we'll scroll up
  11408. 9:45:00and here we're also going to import
  11409. 9:45:03within
  11410. 9:45:05from testing library/react.
  11411. 9:45:08And we'll scroll back down.
  11412. 9:45:12And within lets us find things within or
  11413. 9:45:16inside a specific element. So to find
  11414. 9:45:19things inside the first product
  11415. 9:45:21container, we're going to use within
  11416. 9:45:25brackets the product containers square
  11417. 9:45:30bracket zero.
  11418. 9:45:32This gives us the first product
  11419. 9:45:34container and we're going to search
  11420. 9:45:35inside this container. And then here
  11421. 9:45:39we're going to do dot get by text and
  11422. 9:45:43brackets.
  11423. 9:45:45And for the first container, we're going
  11424. 9:45:47to search for the first product name. If
  11425. 9:45:50we scroll up,
  11426. 9:45:52this is the first product name. So we'll
  11427. 9:45:55select it and copy.
  11428. 9:45:58And down here,
  11429. 9:46:00we're going to paste.
  11430. 9:46:03So again, this searches inside the first
  11431. 9:46:06product container for this text. And
  11432. 9:46:09I'll add a new line to make it a little
  11433. 9:46:11easier to read. And now we expect this
  11434. 9:46:14to exist. So here we'll type expect open
  11435. 9:46:19bracket
  11436. 9:46:21and just going to reformat this code a
  11437. 9:46:24little bit and at the end close bracket
  11438. 9:46:27and use
  11439. 9:46:292B in the document and brackets.
  11440. 9:46:35Now let's save
  11441. 9:46:38and v test will tell us that this
  11442. 9:46:40passed. So, we know we're displaying the
  11443. 9:46:43correct product in the first product
  11444. 9:46:45container. Now, let's check the second
  11445. 9:46:48product container. We'll select this
  11446. 9:46:50code and copy.
  11447. 9:46:53And down here, paste.
  11448. 9:46:57And we'll change the zero to a one. This
  11449. 9:47:00gets us the second product container.
  11450. 9:47:03And we're going to get the second
  11451. 9:47:04product name. So up here, we're going to
  11452. 9:47:08select this name and copy
  11453. 9:47:12and scroll back down.
  11454. 9:47:16And here we're going to paste
  11455. 9:47:19and save
  11456. 9:47:23and the test passed. So now we know that
  11457. 9:47:26the second product container is
  11458. 9:47:28displaying the correct product. Now we
  11459. 9:47:31could check all the other details of the
  11460. 9:47:33product as an exercise. However, we
  11461. 9:47:36don't have to do it because remember
  11462. 9:47:39that all the product details have
  11463. 9:47:41already been tested in the product
  11464. 9:47:44component test.
  11465. 9:47:47So instead for the homepage component,
  11466. 9:47:50we focus on testing things that are
  11467. 9:47:52specific to the homepage such as
  11468. 9:47:55rendering multiple products and loading
  11469. 9:47:57products using Axios.
  11470. 9:48:00So that's how we test a more complicated
  11471. 9:48:02component like the homepage. We need to
  11472. 9:48:05mock the implementation of axios.get
  11473. 9:48:09and we may need to wrap the component
  11474. 9:48:11inside a router. I'll leave adding more
  11475. 9:48:15tests for the homepage as an exercise.
  11476. 9:48:18So now let's save to git. We'll open the
  11477. 9:48:21git section and we'll use the message
  11478. 9:48:24create integration test for home page
  11479. 9:48:31and commit.
  11480. 9:48:36So now whenever we make changes to our
  11481. 9:48:39code we can run these automated tests to
  11482. 9:48:42make sure that format money the product
  11483. 9:48:45component and the homepage are still
  11484. 9:48:47correct. Now, usually we create tests
  11485. 9:48:50for all of our components, but I'll
  11486. 9:48:53leave that as exercises.
  11487. 9:48:56In the next lesson, we'll learn
  11488. 9:48:57deploying, which means how to put our
  11489. 9:49:00React app on the internet.
  11490. 9:49:04And that's the end of this lesson. In
  11491. 9:49:06this lesson, we learned how to create
  11492. 9:49:08automated tests with VEST. We learned
  11493. 9:49:11how to create unit tests, which test one
  11494. 9:49:14piece of the code. We learned it,
  11495. 9:49:18expect, and describe, which lets us
  11496. 9:49:20create a test, check the results, and
  11497. 9:49:23group the tests together into a test
  11498. 9:49:26suite. We learned how to create
  11499. 9:49:28integration tests, which test multiple
  11500. 9:49:31pieces of code working together. We
  11501. 9:49:34learned how to test a component using
  11502. 9:49:36render and screen. We learned how to
  11503. 9:49:39create a mock, which is a fake version
  11504. 9:49:42of a function or a package. And we learn
  11505. 9:49:45how to mock the implementation, which
  11506. 9:49:47means we make a mock do whatever we
  11507. 9:49:50want.
  11508. 9:49:52Here are some exercises to help you
  11509. 9:49:54practice what we learned in this lesson.
  11510. 9:50:35In this lesson, we're going to learn
  11511. 9:50:37deploying, which means how to put a
  11512. 9:50:40React app on the internet. So, there are
  11513. 9:50:43many services we can use to deploy. In
  11514. 9:50:46this lesson, we're going to use AWS or
  11515. 9:50:49Amazon Web Services. AWS is the biggest
  11516. 9:50:53service for putting websites on the
  11517. 9:50:55internet. Most big tech companies use
  11518. 9:50:58AWS, so it's the most useful to learn
  11519. 9:51:00for your career.
  11520. 9:51:03So AWS stands for Amazon Web Services.
  11521. 9:51:07It's a collection of services that help
  11522. 9:51:10us put stuff on the internet. And AWS
  11523. 9:51:13has a free tier where we can use many of
  11524. 9:51:16their services for free using some
  11525. 9:51:18credits.
  11526. 9:51:20So, let's click this link in the
  11527. 9:51:22description. Go to lesson 10.
  11528. 9:51:26And here we have a link to AWS.
  11529. 9:51:30Now, before we begin, AWS might change
  11530. 9:51:33their website design in the future. If
  11531. 9:51:35you have any issues, please check this
  11532. 9:51:37AWS troubleshooting guide here.
  11533. 9:51:42Now, let's get started with AWS.
  11534. 9:51:45We'll click this link to open AWS.
  11535. 9:51:49And the free tier is free for new
  11536. 9:51:51accounts. So, we're going to create a
  11537. 9:51:54new AWS account. If you don't see this,
  11538. 9:51:57you can also click sign in
  11539. 9:52:00and then scroll down and click create a
  11540. 9:52:03new AWS account.
  11541. 9:52:07Before we continue, because AWS is the
  11542. 9:52:10biggest web service, it has to follow a
  11543. 9:52:12lot of security and compliance
  11544. 9:52:14regulations. So, even though it has a
  11545. 9:52:16free tier, it will ask you for a credit
  11546. 9:52:19card, billing address, and phone number
  11547. 9:52:21when signing up. If you're unable to
  11548. 9:52:23provide this information, please check
  11549. 9:52:25the AWS troubleshooting guide in the
  11550. 9:52:28description.
  11551. 9:52:30However, I still recommend at least
  11552. 9:52:32watching this tutorial to see how AWS
  11553. 9:52:35works since it's the most useful for
  11554. 9:52:37your career. So, here we'll enter our
  11555. 9:52:41email address for logging in. So, for
  11556. 9:52:44me, I'll use this email
  11557. 9:52:50and then we'll go through the rest of
  11558. 9:52:52the sign up process.
  11559. 9:53:05After signing up, it might take a few
  11560. 9:53:07minutes for the account to be activated.
  11561. 9:53:10So, we just need to give it a bit of
  11562. 9:53:11time.
  11563. 9:53:14Now that we created a new AWS account,
  11564. 9:53:17one thing to note is when signing into
  11565. 9:53:20AWS,
  11566. 9:53:22if you see an account ID and I am here,
  11567. 9:53:26we're going to click this sign in with
  11568. 9:53:28root email button.
  11569. 9:53:31And then here we can sign in with the
  11570. 9:53:33email and password we created earlier.
  11571. 9:53:37After signing in, you can see the
  11572. 9:53:39details of the free tier here or by
  11573. 9:53:42clicking here.
  11574. 9:53:53After we sign in, we're going to land on
  11575. 9:53:55this AWS dashboard. So, as I said, AWS
  11576. 9:53:59is a collection of services that help us
  11577. 9:54:02put stuff on the internet. So, here we
  11578. 9:54:06can see all the services that are part
  11579. 9:54:08of AWS. And there's a lot of services.
  11580. 9:54:12At the top in the search bar, we can
  11581. 9:54:15search for a service. So, we're going to
  11582. 9:54:17start with the most basic service called
  11583. 9:54:20EC2.
  11584. 9:54:22And click here to open it.
  11585. 9:54:27EC2 stands for Elastic Compute Cloud. It
  11586. 9:54:31basically lets us rent a computer from
  11587. 9:54:34AWS.
  11588. 9:54:35Earlier in the course, I mentioned that
  11589. 9:54:38many websites use a backend to manage
  11590. 9:54:40the data and the backend is another
  11591. 9:54:43computer on the internet. Now, we could
  11592. 9:54:46buy a physical computer ourselves and
  11593. 9:54:49use that as the back end, but then we
  11594. 9:54:52have to keep this computer running 24
  11595. 9:54:54hours a day and put it in our office
  11596. 9:54:56somewhere. So this is a lot of work and
  11597. 9:55:00what most companies do is they just rent
  11598. 9:55:02a computer from AWS and AWS will manage
  11599. 9:55:07the computer and keep it running 24
  11600. 9:55:09hours a day and this saves a lot of
  11601. 9:55:12work. So EC2 basically lets us rent a
  11602. 9:55:16computer from AWS. This is called
  11603. 9:55:19infrastructure as a service or IAS. This
  11604. 9:55:23means that AWS manages the
  11605. 9:55:25infrastructure or the computers for us.
  11606. 9:55:30So to rent a computer in here, we can
  11607. 9:55:32click the instances section and then
  11608. 9:55:36click launch instance. By the way, one
  11609. 9:55:39instance is one EC2 computer.
  11610. 9:55:43However, we're not going to use this
  11611. 9:55:45because there's still some problems with
  11612. 9:55:47this process. EC2 just gives us a
  11613. 9:55:51computer. We still need to install
  11614. 9:55:53NodeJS on this computer. We still need
  11615. 9:55:56to connect this computer to the
  11616. 9:55:57internet. And we need to upload our code
  11617. 9:56:00to this computer. So even though AWS
  11618. 9:56:04manages the computer, there's still a
  11619. 9:56:06lot of manual work we have to do. To
  11620. 9:56:09solve this problem, AWS provides another
  11621. 9:56:12service.
  11622. 9:56:14At the top, we're going to search for
  11623. 9:56:17elastic
  11624. 9:56:19beantock
  11625. 9:56:21and click here to open this service.
  11626. 9:56:25You can think of elastic beantock as a
  11627. 9:56:28layer on top of EC2. It uses EC2 to rent
  11628. 9:56:32a computer, but it also installs all the
  11629. 9:56:35software we need on this computer and it
  11630. 9:56:38connects the computer to the internet.
  11631. 9:56:40So, Elastic Beantock also does all the
  11632. 9:56:43software setup for us. All we have to do
  11633. 9:56:47is just upload our code to elastic
  11634. 9:56:49beantock and it will just work. This is
  11635. 9:56:53called a platform as a service or a
  11636. 9:56:56path. This manages the infrastructure
  11637. 9:56:59and the software for us. If you want to
  11638. 9:57:02learn more about these concepts, you can
  11639. 9:57:04check out my backend overview video in
  11640. 9:57:07the description.
  11641. 9:57:10Now that we have an overview of AWS and
  11642. 9:57:13some of the services we can use, let's
  11643. 9:57:15learn how to use Elastic Beantock. Now,
  11644. 9:57:18Amazon might change the design of these
  11645. 9:57:21pages in the future. If you run into any
  11646. 9:57:23issues, make sure to check the AWS
  11647. 9:57:26troubleshooting guide in the
  11648. 9:57:27description.
  11649. 9:57:29We're going to start by creating an
  11650. 9:57:31application. Or we can click here, click
  11651. 9:57:35environments,
  11652. 9:57:37and create environment.
  11653. 9:57:40And on this page, we're going to select
  11654. 9:57:43a web server environment. And we're
  11655. 9:57:46going to give a name to our application.
  11656. 9:57:48So let's just use e-commerce-
  11657. 9:57:52project-
  11658. 9:57:54react.
  11659. 9:57:56And we can use the default values for
  11660. 9:57:58this section. And we'll scroll down. And
  11661. 9:58:02for the platform section, we're going to
  11662. 9:58:04choose the technology we want to use
  11663. 9:58:06here. For us, we're going to use NodeJS.
  11664. 9:58:12And down here, we'll just use the sample
  11665. 9:58:15application. We can upload our own code
  11666. 9:58:18later.
  11667. 9:58:19And we'll scroll down. And here we'll
  11668. 9:58:22choose single instance. If you see free
  11669. 9:58:25tier allegible, pick that one.
  11670. 9:58:27Otherwise, just pick the single
  11671. 9:58:29instance.
  11672. 9:58:31And then here we'll click next.
  11673. 9:58:35And here we need to create a role. A
  11674. 9:58:38role gives elastic beantock permission
  11675. 9:58:41to use other services in AWS.
  11676. 9:58:45So in here, if you already have a role
  11677. 9:58:48for elastic beantock, you can use that.
  11678. 9:58:51Otherwise, we'll click create ro.
  11679. 9:58:57And we should be able to use the default
  11680. 9:58:59values that they set here. And we'll
  11681. 9:59:02click next.
  11682. 9:59:04And then click next.
  11683. 9:59:07And at the bottom, we'll click create
  11684. 9:59:10roll.
  11685. 9:59:12And then we'll go back to Elastic
  11686. 9:59:14Beantock. And we'll click this button to
  11687. 9:59:17refresh.
  11688. 9:59:19And we'll select the RO that we just
  11689. 9:59:21created.
  11690. 9:59:23For this one, we also need to create a
  11691. 9:59:24role. So, we'll click here.
  11692. 9:59:28And then we can also use the default
  11693. 9:59:30values that it gives. Click next
  11694. 9:59:33and next.
  11695. 9:59:35And at the bottom, create RO.
  11696. 9:59:41And we'll go back and refresh
  11697. 9:59:45and select the RO that we just created.
  11698. 9:59:48And we can keep this as blank. And we'll
  11699. 9:59:51click next.
  11700. 9:59:54For the networking and database
  11701. 9:59:56settings, we can just use the default
  11702. 9:59:58values here. So, we'll scroll down and
  11703. 10:00:01click next.
  11704. 10:00:04And for the instance settings, we're
  11705. 10:00:07going to scroll down and find the
  11706. 10:00:09section called capacity.
  11707. 10:00:11And down here, we're going to scroll
  11708. 10:00:13down
  11709. 10:00:18and we're going to find a section like
  11710. 10:00:20this called instance types. And it has a
  11711. 10:00:23drop down here. So, this lets us choose
  11712. 10:00:26the size of the computer that we want to
  11713. 10:00:28rent from EC2.
  11714. 10:00:31So, in the description, we're going to
  11715. 10:00:33click this link and go to lesson 10 and
  11716. 10:00:37then follow the instructions here to
  11717. 10:00:39select the correct instance types.
  11718. 10:00:43So, now we'll scroll down and click next
  11719. 10:00:49and we can leave monitoring and logging
  11720. 10:00:51settings as the default. So, we'll
  11721. 10:00:54scroll down
  11722. 10:00:58and click next.
  11723. 10:01:02And finally, we'll scroll to the bottom
  11724. 10:01:05and click create.
  11725. 10:01:10So now, Elastic Beantock is going to
  11726. 10:01:13rent a computer for us using EC2. It
  11727. 10:01:17will also install all the software that
  11728. 10:01:19we need on this computer like NodeJS and
  11729. 10:01:22npm. And it will do all the
  11730. 10:01:25configuration to connect this computer
  11731. 10:01:27to the internet.
  11732. 10:01:30So, this will take a few minutes to
  11733. 10:01:31complete. So, we need to wait for this
  11734. 10:01:34to finish.
  11735. 10:01:35And you might need to refresh the page
  11736. 10:01:38to check the progress.
  11737. 10:01:43Once that's done, it should look
  11738. 10:01:45something like this. And we can click
  11739. 10:01:47this URL here or click here to open this
  11740. 10:01:52website.
  11741. 10:01:55And now we should see a sample NodeJS
  11742. 10:01:58application on this page. If something
  11743. 10:02:01goes wrong and this doesn't work, please
  11744. 10:02:03check the AWS troubleshooting guide in
  11745. 10:02:05the description.
  11746. 10:02:09Now that we set up Elastic Beantock,
  11747. 10:02:11we're ready to put our code on the
  11748. 10:02:13internet. We'll go back to Elastic
  11749. 10:02:16Beantock.
  11750. 10:02:18And our code has two parts, the back end
  11751. 10:02:21and the front end. We put the back end
  11752. 10:02:24on the internet first and then we put
  11753. 10:02:27the front end. To put the back end on
  11754. 10:02:30the internet, we just need to upload our
  11755. 10:02:33backend code to elastic beantock. So
  11756. 10:02:36we'll click upload and deploy.
  11757. 10:02:40And here we're going to upload our
  11758. 10:02:43backend code. However, we need to upload
  11759. 10:02:46the code as a zip file. So we'll go back
  11760. 10:02:49to VS Code.
  11761. 10:02:52And I'll collapse this for now. And I
  11762. 10:02:56actually set up the e-commerce backend
  11763. 10:02:59so that it works with elastic beantock.
  11764. 10:03:02So this backend has a command that will
  11765. 10:03:06save all the code in a zip file. So
  11766. 10:03:09we're going to go to the command line
  11767. 10:03:12and we're going to create a new tab.
  11768. 10:03:15And in here we're going to cd into
  11769. 10:03:19e-commerce backend.
  11770. 10:03:23and we'll run the command npm run zip.
  11771. 10:03:29So this will save all the backend code
  11772. 10:03:32into a zip file which is right here. If
  11773. 10:03:36I open this zip file,
  11774. 10:03:38you'll see that it just contains all of
  11775. 10:03:40the backend code.
  11776. 10:03:43So now we just need to upload this zip
  11777. 10:03:46file to Elastic Beantock. We'll go back
  11778. 10:03:49to the browser
  11779. 10:03:51and we'll click this to choose a file.
  11780. 10:03:55And for me, my code is on the desktop
  11781. 10:03:59inside the React course folder inside
  11782. 10:04:02e-commerce backend.
  11783. 10:04:04And if I scroll down, I'll see this
  11784. 10:04:06e-commerce backend one, which is a zip
  11785. 10:04:09file. So, I'll click this and open.
  11786. 10:04:14Wait for it to upload and then click
  11787. 10:04:16deploy.
  11788. 10:04:21So now elastic beantock will take the
  11789. 10:04:24backend code and put it on the internet.
  11790. 10:04:27So we'll wait for a few minutes for this
  11791. 10:04:29to complete.
  11792. 10:04:33Once that's done, we're going to click
  11793. 10:04:35this URL again.
  11794. 10:04:38And now instead of the sample
  11795. 10:04:40application, it should say index not
  11796. 10:04:43found, which is normal. This is how I
  11797. 10:04:45designed my back end.
  11798. 10:04:47Now in the URL, we're going to click up
  11799. 10:04:50here and go to the end. We're going to
  11800. 10:04:53enter the URL path slash API slash
  11801. 10:04:57products and press enter.
  11802. 10:05:01So this will give us the products data
  11803. 10:05:04that we were using in our project on our
  11804. 10:05:07computer. So this is the same data that
  11805. 10:05:10we saw before except now our backend is
  11806. 10:05:13on the internet.
  11807. 10:05:17Now that our back end is on the
  11808. 10:05:19internet, let's put our front end on the
  11809. 10:05:21internet as well. For the front end, we
  11810. 10:05:24also need to prepare our code. So, we'll
  11811. 10:05:28go back to VS Code.
  11812. 10:05:31And I'll collapse this folder for now.
  11813. 10:05:34And if we look at the front-end code,
  11814. 10:05:37notice that in here we use JSX code.
  11815. 10:05:42However, remember that the browser does
  11816. 10:05:44not understand JSX. It only understands
  11817. 10:05:47normal JavaScript. So, we need to
  11818. 10:05:50convert this code into normal JavaScript
  11819. 10:05:54first. To do that, VIT has a command for
  11820. 10:05:57this. Inside the command line, we'll
  11821. 10:06:00create a new tab.
  11822. 10:06:03And this time we'll cd into
  11823. 10:06:06ecommerce
  11824. 10:06:08dash project.
  11825. 10:06:12And vit has a command called npm run
  11826. 10:06:16build.
  11827. 10:06:18This is a shortcut for vit build.
  11828. 10:06:22So we'll press enter.
  11829. 10:06:27And this basically converts all of our
  11830. 10:06:29JSX code into normal JavaScript and
  11831. 10:06:33saves the code in a folder called dist.
  11832. 10:06:36So inside here we have an index.html
  11833. 10:06:40file and this is the HTML for our React
  11834. 10:06:43website. And inside the assets folder we
  11835. 10:06:47have this JavaScript file. So this is
  11836. 10:06:51all of our React code but converted into
  11837. 10:06:54normal JavaScript. You'll also notice
  11838. 10:06:57that Vit combined all of our code into
  11839. 10:06:59one file and it also minified or
  11840. 10:07:02compressed the code. So, it shortened
  11841. 10:07:05the variable names and removed the extra
  11842. 10:07:07spaces. This is because when we open a
  11843. 10:07:11website, our browser needs to download
  11844. 10:07:13all the front-end code and run it. By
  11845. 10:07:17combining and compressing the code, it
  11846. 10:07:19makes our website load faster. And Vit
  11847. 10:07:22did the same thing with the CSS.
  11848. 10:07:25You can see that it combined all of our
  11849. 10:07:27CSS files together and compressed it. So
  11850. 10:07:30now our front end is optimized and it's
  11851. 10:07:33ready to be put on the internet.
  11852. 10:07:36So now when we open a website, we need
  11853. 10:07:39to download this React code from
  11854. 10:07:42somewhere. So where do we put this
  11855. 10:07:44front-end code? So we could put this in
  11856. 10:07:47another service like GitHub pages and
  11857. 10:07:50then when we visit the website our
  11858. 10:07:52browser will download this front-end
  11859. 10:07:54code from there.
  11860. 10:07:56However, notice in our current setup
  11861. 10:07:59that we already have this backend
  11862. 10:08:01computer that's running in elastic
  11863. 10:08:04beantock.
  11864. 10:08:05So we can just reuse this backend
  11865. 10:08:07computer. we can put our React code into
  11866. 10:08:11the backend computer and our browser
  11867. 10:08:13will download the React code from the
  11868. 10:08:15backend computer. So, this is a common
  11869. 10:08:18setup and it simplifies our setup
  11870. 10:08:21because now we only need to set up one
  11871. 10:08:24computer. So, to do this step, all we
  11872. 10:08:27need to do is to take this disc folder
  11873. 10:08:30and then drag it into the e-commerce
  11874. 10:08:32back end like this.
  11875. 10:08:36And I added some code in the e-commerce
  11876. 10:08:39back end so that it works with this disc
  11877. 10:08:41folder. So now in the command line,
  11878. 10:08:45we'll go back to the tab that's running
  11879. 10:08:47in the back end and we're going to
  11880. 10:08:49create the zip file again. So we'll do
  11881. 10:08:52npm runzip.
  11882. 10:08:59So this will create another zip file but
  11883. 10:09:02with our front-end code inside.
  11884. 10:09:05And now we just need to upload this to
  11885. 10:09:08Elastic Beantock. We'll go back to the
  11886. 10:09:11browser
  11887. 10:09:13and open Elastic Beantock and we'll
  11888. 10:09:16click upload and deploy.
  11889. 10:09:19And we'll choose a file.
  11890. 10:09:22And this time we'll choose the second
  11891. 10:09:25file here
  11892. 10:09:29and click deploy.
  11893. 10:09:35And we'll wait a few minutes for this to
  11894. 10:09:37complete.
  11895. 10:09:40Once that's finished, we're going to
  11896. 10:09:42click this URL again.
  11897. 10:09:45And now we should have our React app on
  11898. 10:09:48the internet. So, let's try adding some
  11899. 10:09:51stuff to the cart to make sure it works.
  11900. 10:09:53I'll add a toaster and two plates.
  11901. 10:09:58And now, if I look at the cart, it
  11902. 10:10:00updated. If I open it, we have our new
  11903. 10:10:03products in the cart and we can update
  11904. 10:10:06them like usual.
  11905. 10:10:08And if we click create order,
  11906. 10:10:12it works. So everything is working the
  11907. 10:10:15same as on our computer, but this React
  11908. 10:10:18app is now on the internet. If we go
  11909. 10:10:21back to the homepage,
  11910. 10:10:23I'm going to give you a overview of how
  11911. 10:10:26this works. So visually, this is what's
  11912. 10:10:28happening. First we put our back end on
  11913. 10:10:31the internet. So the back end is running
  11914. 10:10:32on a separate computer and then we put
  11915. 10:10:35our front end code in the backend
  11916. 10:10:37computer and I set up some code in the
  11917. 10:10:39back end so that when we visit the
  11918. 10:10:41website using the URL path slash the
  11919. 10:10:45backend sends back the react code to our
  11920. 10:10:48computer. So now we have the react code
  11921. 10:10:50running on our computer and it
  11922. 10:10:53communicates with the backend running on
  11923. 10:10:55another computer. So, this is how a
  11924. 10:10:58realworld React app works. When we're
  11925. 10:11:01developing, the front end and the back
  11926. 10:11:04end run on the same computer because
  11927. 10:11:06it's easier for development.
  11928. 10:11:09But when we put the React app on the
  11929. 10:11:11internet, the front-end code runs on our
  11930. 10:11:14computer and the backend code runs on
  11931. 10:11:16another computer.
  11932. 10:11:20Next, we can add a shortcut to Vit to
  11933. 10:11:23make this process a little easier. We'll
  11934. 10:11:26go back to VS Code
  11935. 10:11:29and we'll go to the e-commerce project
  11936. 10:11:32folder which is here. So remember that
  11937. 10:11:36we created a disc folder and then
  11938. 10:11:38manually dragged it into e-commerce
  11939. 10:11:41backend. We can configure VIT to put the
  11940. 10:11:45disc folder in e-commerce backend
  11941. 10:11:47directly.
  11942. 10:11:49We're going to scroll down
  11943. 10:11:51to vit.config.js.
  11944. 10:11:56And in this configuration at the bottom
  11945. 10:12:00we're going to add a property called
  11946. 10:12:02build colon curly brackets. And in here
  11947. 10:12:07we'll use a property out capital D dur
  11948. 10:12:12colon and a string.
  11949. 10:12:14So this tells vit where to put the dist
  11950. 10:12:17folder.
  11951. 10:12:18So we want to put it inside the
  11952. 10:12:20e-commerce backend folder. So we'll give
  11953. 10:12:23it dot dot slash to go out of e-commerce
  11954. 10:12:26project and then into e-commerce
  11955. 10:12:30dashbackend
  11956. 10:12:32slashdist
  11957. 10:12:34and save.
  11958. 10:12:37So now if we go into the back end and we
  11959. 10:12:40rightclick and delete this disc folder
  11960. 10:12:46and then run npm run build. So in the
  11961. 10:12:49command line we'll open the tab that's
  11962. 10:12:52running in the e-commerce project and
  11963. 10:12:54then run npm run build and press enter.
  11964. 10:13:02It will create the disc folder directly
  11965. 10:13:04in here. So directly in the e-commerce
  11966. 10:13:07back end. So this just saves a step in
  11967. 10:13:10our process. We can also automate this
  11968. 10:13:13process further but I'll leave that as
  11969. 10:13:15an exercise.
  11970. 10:13:17So now we'll save this change into git.
  11971. 10:13:20We'll go to the git section
  11972. 10:13:24and we'll use the message save build
  11973. 10:13:28result in ecommerce dashbackend
  11974. 10:13:33and commit.
  11975. 10:13:38Before we continue, I want to discuss
  11976. 10:13:41how to deploy a react app without a
  11977. 10:13:44backend.
  11978. 10:13:46So notice that so far to put a React app
  11979. 10:13:49on the internet, we needed a backend.
  11980. 10:13:52But what if we don't know how to write
  11981. 10:13:54backend code yet? What if we just know
  11982. 10:13:57React? So if a React app does not have a
  11983. 10:14:01backend, we can actually just put the
  11984. 10:14:03code on the internet directly.
  11985. 10:14:07For example, in our code here, we're
  11986. 10:14:10going to go back to our chatbot project
  11987. 10:14:13from earlier. And notice that this
  11988. 10:14:16project does not use a backend. It only
  11989. 10:14:19has React code. So to deploy this
  11990. 10:14:23project, we're going to use npm run
  11991. 10:14:25build again. So in our command line,
  11992. 10:14:28we'll create a new tab
  11993. 10:14:30and we'll cd into the chatbot-
  11994. 10:14:34project folder.
  11995. 10:14:37And if we need to, we can npm install
  11996. 10:14:44and then npm run build.
  11997. 10:14:49And this will convert all the React code
  11998. 10:14:51into normal JavaScript and save it in a
  11999. 10:14:54disc folder again. So notice that inside
  12000. 10:14:58the disc folder, we actually just have
  12001. 10:15:00normal HTML, CSS, and JavaScript code.
  12002. 10:15:04So this is actually just an HTML website
  12003. 10:15:08and we can actually upload this code to
  12004. 10:15:10any service that puts an HTML website on
  12005. 10:15:13the internet. For example, here I use a
  12006. 10:15:17service called GitHub pages and I can
  12007. 10:15:19just upload this code to GitHub pages
  12008. 10:15:22and it will put it on the internet. No
  12009. 10:15:24backend knowledge required.
  12010. 10:15:27To learn more, you can actually follow
  12011. 10:15:29the same steps as my video, how to put a
  12012. 10:15:31HTML website on the internet. However,
  12013. 10:15:35make sure you do the exercises in this
  12014. 10:15:37lesson first since there's still some
  12015. 10:15:39small configuration changes that we need
  12016. 10:15:42to make for this to work. So, basically,
  12017. 10:15:45if we just have a front end, we can just
  12018. 10:15:48upload this code directly to the
  12019. 10:15:50internet using something like GitHub
  12020. 10:15:52pages. But if our project has a backend
  12021. 10:15:55as well, then we need to use a more
  12022. 10:15:58complicated service like AWS to put the
  12023. 10:16:01back end and the front end on the
  12024. 10:16:04internet.
  12025. 10:16:06In the rest of this lesson, we're going
  12026. 10:16:09to improve our AWS setup to make it more
  12027. 10:16:12real world and professional.
  12028. 10:16:15So, we'll go back to the browser
  12029. 10:16:17and we'll open AWS again. And I'm
  12030. 10:16:21actually going to close some of these
  12031. 10:16:24other tabs because we don't need them.
  12032. 10:16:27And inside Elastic Beantock in this
  12033. 10:16:30project, we're going to go to the
  12034. 10:16:32configuration section.
  12035. 10:16:36And we're going to scroll down and find
  12036. 10:16:38the configuration for instance traffic.
  12037. 10:16:41If you don't see this, you can check the
  12038. 10:16:43AWS troubleshooting guide in the
  12039. 10:16:45description.
  12040. 10:16:47So here we'll click edit.
  12041. 10:16:51and we're going to scroll until we find
  12042. 10:16:54the capacity section. And here we'll
  12043. 10:16:58look for something like environment type
  12044. 10:17:00and a drop down that says single
  12045. 10:17:02instance. So we're going to change this
  12046. 10:17:04to load balanced.
  12047. 10:17:08So what this does is it adds another
  12048. 10:17:11computer in front of our backend
  12049. 10:17:13computer. This is called a load
  12050. 10:17:15balancer. So what a load balancer does
  12051. 10:17:18is if we have a really busy website, one
  12052. 10:17:21computer might not be enough to handle
  12053. 10:17:24all the requests. So we can actually run
  12054. 10:17:27the back end on multiple computers and
  12055. 10:17:29this load balancer will distribute the
  12056. 10:17:32requests across these computers. This
  12057. 10:17:34allows our website to handle much more
  12058. 10:17:37users.
  12059. 10:17:39So here we can decide how many back-end
  12060. 10:17:43computers to run. Since we're just
  12061. 10:17:45testing, we'll set this to a minimum of
  12062. 10:17:47one and a maximum of one.
  12063. 10:17:52And then we'll scroll down
  12064. 10:17:56and click apply.
  12065. 10:18:02And now Elastic Beantock will set up a
  12066. 10:18:04load balancer computer in front of our
  12067. 10:18:07back end. And this will take a few
  12068. 10:18:09minutes to complete.
  12069. 10:18:12After it finishes, we're going to go
  12070. 10:18:14back to the configuration again
  12071. 10:18:19and we'll scroll down to the instance
  12072. 10:18:21traffic settings and just make sure that
  12073. 10:18:24min is one and max is one. If it's not
  12074. 10:18:27one, we can click edit
  12075. 10:18:31and then scroll back down and then
  12076. 10:18:34change it to one. And we'll scroll down
  12077. 10:18:38and click apply again.
  12078. 10:18:47So when that finishes, I'll just check
  12079. 10:18:49it again.
  12080. 10:18:52And now it's one and one.
  12081. 10:18:57So now our website setup looks like
  12082. 10:19:00this. We have three computers, the front
  12083. 10:19:03end, the back end, and the load
  12084. 10:19:05balancer. This is closer to a real world
  12085. 10:19:08setup. Again, you can watch my video on
  12086. 10:19:12backend overview to learn more about
  12087. 10:19:14these concepts.
  12088. 10:19:17Next, currently all the data for our
  12089. 10:19:20React app like the products, the cart,
  12090. 10:19:22and the orders are saved in a database
  12091. 10:19:26and the database is saved in a file.
  12092. 10:19:30However, when we put our React app on
  12093. 10:19:32the internet, we usually put the
  12094. 10:19:35database on a separate computer and this
  12095. 10:19:38lets us share the data between backend
  12096. 10:19:41computers.
  12097. 10:19:43So, inside AWS, we're going to go to the
  12098. 10:19:47configuration again
  12099. 10:19:50and we'll scroll down to the database
  12100. 10:19:53configuration and we'll click edit.
  12101. 10:19:58And inside the database section, we're
  12102. 10:20:01going to scroll down
  12103. 10:20:04and find database and we'll click enable
  12104. 10:20:07database.
  12105. 10:20:10Elastic Beantock will automatically
  12106. 10:20:12create a new computer that's running a
  12107. 10:20:15database and it will connect this
  12108. 10:20:17database to our back end.
  12109. 10:20:20So, we'll scroll down and look at these
  12110. 10:20:23options. And here we can select the
  12111. 10:20:26database that we want to use. So the
  12112. 10:20:28e-commerce back end works with the
  12113. 10:20:30database MySQL. So we're going to use
  12114. 10:20:33that.
  12115. 10:20:35And then here we're going to look at the
  12116. 10:20:38instance class setting. So this is the
  12117. 10:20:41size of the computer running the
  12118. 10:20:43database.
  12119. 10:20:45In the description, we'll click this
  12120. 10:20:47link, go to lesson 10, and then here
  12121. 10:20:51we'll follow the instructions to select
  12122. 10:20:53the correct instance class for the
  12123. 10:20:55database.
  12124. 10:21:03And finally, we'll set a password.
  12125. 10:21:08And now we'll scroll down to the bottom
  12126. 10:21:11and click apply.
  12127. 10:21:16So AWS will set up a separate computer
  12128. 10:21:19to run the database. And I already added
  12129. 10:21:22some code in my e-commerce back end to
  12130. 10:21:24connect to this database. So it should
  12131. 10:21:27just work. If you have issues, please
  12132. 10:21:30check the AWS troubleshooting guide.
  12133. 10:21:35After that finishes, we can open the
  12134. 10:21:38website again
  12135. 10:21:40and it still works. If we add something
  12136. 10:21:43to the cart, notice that the database
  12137. 10:21:46still works. And if we open the orders
  12138. 10:21:49page,
  12139. 10:21:51remember that earlier I created a new
  12140. 10:21:53order.
  12141. 10:21:55And now the orders are back to the
  12142. 10:21:57default values.
  12143. 10:21:59So this means that we're using the new
  12144. 10:22:01MySQL database that we set up. And now
  12145. 10:22:05our AWS setup looks like this. We have a
  12146. 10:22:08load balancer, a backend, and it
  12147. 10:22:10connects to a database on another
  12148. 10:22:13computer. And this is getting even
  12149. 10:22:15closer to a real world setup.
  12150. 10:22:20Next, if we go back to AWS,
  12151. 10:22:24notice that to open our website, we use
  12152. 10:22:27this default URL, which is really long.
  12153. 10:22:31So, now we're going to set up a domain
  12154. 10:22:32name to make our website easier to
  12155. 10:22:35access. A domain name is like myite.com
  12156. 10:22:39or youtube.com. It's a short name that
  12157. 10:22:43makes it easy to access our website.
  12158. 10:22:46So instead of putting this long URL in
  12159. 10:22:48the browser, we can just type myite.com
  12160. 10:22:52and it will open this website. So I'll
  12161. 10:22:55show you how to set up a domain name
  12162. 10:22:57with AWS.
  12163. 10:22:59First, we need to purchase a domain
  12164. 10:23:02name. Most domain names cost around $10
  12165. 10:23:05or $20 per year, so this section is
  12166. 10:23:08optional. If you already have a domain
  12167. 10:23:11name that you purchased before and you
  12168. 10:23:12want to use, feel free to skip to the
  12169. 10:23:15next section. Otherwise, I'm going to
  12170. 10:23:17show you how to purchase your own domain
  12171. 10:23:20name. There are many services we can use
  12172. 10:23:22to purchase domain names. I use a
  12173. 10:23:25service called namecheep for all my
  12174. 10:23:27websites like superimple.dev.
  12175. 10:23:30In the description, we'll click this
  12176. 10:23:32link, go to lesson 10, and click here to
  12177. 10:23:36go to namecheep. So, this is an
  12178. 10:23:38affiliate link. If you decide to
  12179. 10:23:40purchase a domain name using this link,
  12180. 10:23:43part of your purchase will help support
  12181. 10:23:45this channel. Thank you so much.
  12182. 10:23:50Once we're on NameCheep, we're going to
  12183. 10:23:52search for a domain name that we want.
  12184. 10:23:54If you see a search box here, you can
  12185. 10:23:57search for it. Otherwise, we can go to
  12186. 10:23:59domains and domain name search.
  12187. 10:24:03and we'll scroll down here
  12188. 10:24:05and search for a domain name. So, domain
  12189. 10:24:08names are unique. Once they are taken,
  12190. 10:24:11like youtube.com, we can't purchase it
  12191. 10:24:14anymore. If you don't know what domain
  12192. 10:24:17name you want, my recommendation is to
  12193. 10:24:20try to register your first and last
  12194. 10:24:22name. This might be useful for a
  12195. 10:24:24personal website. So, for me, it's Simon
  12196. 10:24:28Bao. and we need to give it an ending
  12197. 10:24:32like
  12198. 10:24:35do stands for commercial. It's the most
  12199. 10:24:37popular ending for domain names. Now,
  12200. 10:24:40you could pick another ending that you
  12201. 10:24:42want, but be careful because some domain
  12202. 10:24:44endings don't work with normal browsers.
  12203. 10:24:48If you're not sure which ending you
  12204. 10:24:49want, I recommend using com or.net just
  12205. 10:24:52to be safe because they are the most
  12206. 10:24:54popular.
  12207. 10:24:56So now I'm going to click search to see
  12208. 10:24:59if this domain has already been taken.
  12209. 10:25:03So it looks like this one has already
  12210. 10:25:05been taken. So I'll just add something
  12211. 10:25:07to it like Simon Balenge or Engineer.
  12212. 10:25:12And this one is available. So you might
  12213. 10:25:15need to modify your domain name to find
  12214. 10:25:17one that works for you.
  12215. 10:25:19So, we'll add this to the cart
  12216. 10:25:24and then open the cart.
  12217. 10:25:29If you want to autorenew this domain
  12218. 10:25:31name, you can click here and then we'll
  12219. 10:25:34click confirm order.
  12220. 10:25:37And if you don't have a name account,
  12221. 10:25:39you need to create an account first.
  12222. 10:25:47And after filling out all the
  12223. 10:25:48information, we'll purchase the domain.
  12224. 10:25:55And that's it. Now we have a domain name
  12225. 10:25:58and we own a little piece of the
  12226. 10:26:00internet.
  12227. 10:26:02Once we have a domain name, we're going
  12228. 10:26:04to set up the domain name in AWS.
  12229. 10:26:08So, we'll go back to AWS
  12230. 10:26:11and at the top we're going to search for
  12231. 10:26:14the service route 53
  12232. 10:26:18and we're going to write click this and
  12233. 10:26:21open in a new tab.
  12234. 10:26:26So, route 53 lets us manage our domain
  12235. 10:26:29names in AWS. We can click get started
  12236. 10:26:34and click create hosted zones
  12237. 10:26:37and then click get started.
  12238. 10:26:40If you don't see that, we can also click
  12239. 10:26:43this menu here,
  12240. 10:26:45click hosted zones
  12241. 10:26:47and then click create hosted zone.
  12242. 10:26:52A hosted zone lets us connect a domain
  12243. 10:26:55name to our app. So currently a hosted
  12244. 10:26:58zone costs about 1.6 6 cents per day. So
  12245. 10:27:01you can use it for free for a few hours.
  12246. 10:27:05So here we're going to enter the domain
  12247. 10:27:07name that we have. So for me it's simon
  12248. 10:27:12balenge.com
  12249. 10:27:16and scroll down and we'll leave it as a
  12250. 10:27:18public and then click create hosted
  12251. 10:27:21zone.
  12252. 10:27:24And now inside here, we're going to
  12253. 10:27:27scroll down and click create record.
  12254. 10:27:31So this sets where the domain name will
  12255. 10:27:34go. We're going to leave the subdomain
  12256. 10:27:36as blank and we'll create an A record
  12257. 10:27:40and we're going to check alias.
  12258. 10:27:43And here we can select which AWS service
  12259. 10:27:47we want to connect to.
  12260. 10:27:49So for us, we're using elastic beantock
  12261. 10:27:55and we're going to choose the region. If
  12262. 10:27:57we go back to the tab that's running
  12263. 10:27:59elastic beantock, we can see the region
  12264. 10:28:03up here. So for me, I'm using the region
  12265. 10:28:05Ohio.
  12266. 10:28:07So go back to route 53 and I'll search
  12267. 10:28:10for the region Ohio and select it. And
  12268. 10:28:14we should see our app here. So I'll
  12269. 10:28:17select that.
  12270. 10:28:20and then scroll down and click create
  12271. 10:28:22records.
  12272. 10:28:26And finally, we need to tell NameCheep
  12273. 10:28:28or the company that you use for your
  12274. 10:28:30domain name to redirect to AWS.
  12275. 10:28:35So inside NameCheep or your domain
  12276. 10:28:38company, we're going to go to the
  12277. 10:28:41settings
  12278. 10:28:44and we're going to find the domain. So
  12279. 10:28:46in namecheep it's in domain list
  12280. 10:28:50and here you may need to verify your
  12281. 10:28:53email. So we'll click this and check
  12282. 10:28:55your email for the verification. So this
  12283. 10:28:58might take a while. So we just have to
  12284. 10:29:00wait until the verification is complete.
  12285. 10:29:04And we may need to log out and log back
  12286. 10:29:07in to our NameCheep account for the
  12287. 10:29:09verification to update.
  12288. 10:29:14Once verification is complete, we should
  12289. 10:29:16be able to go back to the domain list,
  12290. 10:29:19find our domain, and open the settings
  12291. 10:29:22for the domain.
  12292. 10:29:25And in the settings, we're going to look
  12293. 10:29:28for a section that's called name
  12294. 10:29:30servers. If you're having trouble
  12295. 10:29:32finding this in namecheep or your domain
  12296. 10:29:35company, please check the AWS
  12297. 10:29:37troubleshooting guide in the
  12298. 10:29:39description.
  12299. 10:29:40And then in here, we're going to click
  12300. 10:29:42this dropown
  12301. 10:29:44and we're going to set custom DNS
  12302. 10:29:47because we're going to use AWS for this.
  12303. 10:29:51So now we need to enter some name
  12304. 10:29:53servers. To find these, we're going to
  12305. 10:29:56go back to AWS route 53.
  12306. 10:30:00And I need to zoom out a little bit to
  12307. 10:30:03see this. Inside the hosted zone, we
  12308. 10:30:07have some records here. and we're going
  12309. 10:30:09to look for the record NS or name
  12310. 10:30:12server.
  12311. 10:30:13So in the NS record, we should see these
  12312. 10:30:17four URLs here. So we just need to copy
  12313. 10:30:20these over to namecheep or your domain
  12314. 10:30:23company. So we'll select this and copy.
  12315. 10:30:27And then inside NameCheep
  12316. 10:30:30and here we'll paste.
  12317. 10:30:33And we'll go back and select the second
  12318. 10:30:37name server.
  12319. 10:30:38copy
  12320. 10:30:40and paste.
  12321. 10:30:42If we need to, we can add two more name
  12322. 10:30:45servers.
  12323. 10:30:47And here we'll select this and copy.
  12324. 10:30:52And here we'll paste.
  12325. 10:30:55And we'll select the last one. Copy.
  12326. 10:31:00And here paste. And then save.
  12327. 10:31:07So what this does is when we open our
  12328. 10:31:09domain name in the browser, NameCheep is
  12329. 10:31:12going to redirect to AWS and then AWS
  12330. 10:31:16will redirect to Elastic Beantock or to
  12331. 10:31:19our app.
  12332. 10:31:21So this setting will take a while to
  12333. 10:31:23update. Sometimes it finishes in just 10
  12334. 10:31:26minutes, but it could take up to 24 to
  12335. 10:31:2948 hours. So we just have to be patient.
  12336. 10:31:33So once we waited for a while, we can
  12337. 10:31:36try opening a new tab.
  12338. 10:31:39And in here we'll type http
  12339. 10:31:43colon double slash and our domain name.
  12340. 10:31:46So for me it's simon baenge.com
  12341. 10:31:51and press enter.
  12342. 10:31:55And this will redirect to our app. So
  12343. 10:31:58now we're able to access our React app
  12344. 10:32:00using a short domain name that's easy to
  12345. 10:32:03remember.
  12346. 10:32:06Next, I'm going to explain why we enter
  12347. 10:32:09HTTP in front and why we see this not
  12348. 10:32:12secure warning here. So this means that
  12349. 10:32:16our website is not using encryption.
  12350. 10:32:19This means that any data we send to this
  12351. 10:32:21website can be intercepted by a hacker.
  12352. 10:32:25To enable encryption on this website and
  12353. 10:32:28make it secure, we need to set up an SSL
  12354. 10:32:31certificate, which is a special file
  12355. 10:32:33that enables encryption.
  12356. 10:32:36To get an SSL certificate, we can use
  12357. 10:32:39AWS.
  12358. 10:32:41So, we'll go back to AWS Route 53 and
  12359. 10:32:45then at the top we'll search for another
  12360. 10:32:48service called certificate
  12361. 10:32:51manager
  12362. 10:32:53and click here
  12363. 10:32:58and we can request a certificate. If you
  12364. 10:33:01don't see it, you can also go in here
  12365. 10:33:03and click request certificate.
  12366. 10:33:07We'll use the default value and click
  12367. 10:33:10next.
  12368. 10:33:12And here we're going to enter our domain
  12369. 10:33:14name. Make sure to enter your domain
  12370. 10:33:16name.
  12371. 10:33:18And we'll scroll down and make sure DNS
  12372. 10:33:21validation is selected.
  12373. 10:33:24And scroll down and click request.
  12374. 10:33:28And then we'll wait for this to finish
  12375. 10:33:30loading.
  12376. 10:33:34And now AWS needs to confirm that we own
  12377. 10:33:38the domain name. That's why it says
  12378. 10:33:40pending validation.
  12379. 10:33:42To do that, we can just click this
  12380. 10:33:44create records in Route 53 button. If
  12381. 10:33:47you don't see this, please check the AWS
  12382. 10:33:50troubleshooting guide.
  12383. 10:33:53And it should show up here. And we'll
  12384. 10:33:56click create records.
  12385. 10:33:59And we'll wait for this to finish. We
  12386. 10:34:02may need to refresh a few times until it
  12387. 10:34:06says it was successful.
  12388. 10:34:08And now that we have an SSL certificate,
  12389. 10:34:12we can use this in elastic beantock. So
  12390. 10:34:15we'll go back to elastic beantock
  12391. 10:34:18and then in the configurations here,
  12392. 10:34:23we're going to scroll down to the
  12393. 10:34:26instance traffic settings and click
  12394. 10:34:29edit.
  12395. 10:34:31And we're going to scroll down
  12396. 10:34:38until we find this listeners section.
  12397. 10:34:42And here we'll click add listener.
  12398. 10:34:45For the port we're going to use 443.
  12399. 10:34:49And for the protocol we're going to use
  12400. 10:34:52HTTPS.
  12401. 10:34:55HTTPS means we're going to have a secure
  12402. 10:34:57connection to our website. And this
  12403. 10:35:00usually uses port 443.
  12404. 10:35:03And here we're going to choose the SSL
  12405. 10:35:05certificate that we just created.
  12406. 10:35:09And we should be able to leave the SSL
  12407. 10:35:11policy blank and click save.
  12408. 10:35:16If that didn't work, please check the
  12409. 10:35:18AWS troubleshooting guide. So this
  12410. 10:35:21basically adds an SSL certificate to the
  12411. 10:35:24load balancer. And now any data sent to
  12412. 10:35:27our website will be encrypted.
  12413. 10:35:30So, we're going to scroll down and click
  12414. 10:35:32apply.
  12415. 10:35:38And again, this will take a few minutes
  12416. 10:35:40to complete. So, we just have to wait
  12417. 10:35:42for it.
  12418. 10:35:46Once that's complete, I'll close some of
  12419. 10:35:49these other tabs
  12420. 10:35:52and I'll create a new tab.
  12421. 10:35:55And in the URL, we're going to type
  12422. 10:35:58https
  12423. 10:36:00colon double slash. So this tells the
  12424. 10:36:03browser to use the secure connection to
  12425. 10:36:05our website
  12426. 10:36:07and then we'll type our domain name. For
  12427. 10:36:10me, it's simonbowenge.com
  12428. 10:36:14and press enter.
  12429. 10:36:16So now the not secure warning is gone
  12430. 10:36:19and we're using a secure connection to
  12431. 10:36:22our website that uses encryption.
  12432. 10:36:25Also now if we just type our domain name
  12433. 10:36:28in the URL without HTTPS in front and
  12434. 10:36:32press enter
  12435. 10:36:35it will default to using the secure
  12436. 10:36:37version. So that's how we set up an SSL
  12437. 10:36:40certificate and HTTPS in AWS.
  12438. 10:36:44And now we have a professional and
  12439. 10:36:47realworld setup for our React app.
  12440. 10:36:51In this section, I'll show you how to
  12441. 10:36:53delete everything we created in AWS.
  12442. 10:36:57However, I recommend to do the exercises
  12443. 10:36:59of this lesson first to get more
  12444. 10:37:01practice with AWS and then do this
  12445. 10:37:04deletion.
  12446. 10:37:06Also, I try to keep everything we
  12447. 10:37:08created in the free tier. The only
  12448. 10:37:11exception is the hosted zone in Route
  12449. 10:37:1353. You can use it for free for a few
  12450. 10:37:16hours. So you could keep running these
  12451. 10:37:18services and practice AWS if you want to
  12452. 10:37:22check your AWS bill. We can go to the
  12453. 10:37:25profile and click billing and cost
  12454. 10:37:28management.
  12455. 10:37:29And now after you finish the exercises,
  12456. 10:37:32I'll show you how to delete everything
  12457. 10:37:34just in case. So in elastic beantock, we
  12458. 10:37:38can click this actions dropown and click
  12459. 10:37:41terminate environment.
  12460. 10:37:44And in here we need to type the name of
  12461. 10:37:46the environment which is here
  12462. 10:37:53and click terminate.
  12463. 10:37:59Once that's finished, we can go up here
  12464. 10:38:02and go to route 53.
  12465. 10:38:07And in this menu, we can go to hosted
  12466. 10:38:10zones.
  12467. 10:38:12and we'll open the hosted zone.
  12468. 10:38:15And inside here, we're going to select
  12469. 10:38:18the A record and the CNAME record and
  12470. 10:38:23delete these records.
  12471. 10:38:28And then at the top, we can delete the
  12472. 10:38:31hosted zone.
  12473. 10:38:33And in here, we'll type delete and click
  12474. 10:38:37delete.
  12475. 10:38:40Once that's finished, at the top, we'll
  12476. 10:38:42go to certificate manager.
  12477. 10:38:48And in this menu, we're going to click
  12478. 10:38:50list certificates.
  12479. 10:38:53And we'll select the certificate and
  12480. 10:38:55click delete.
  12481. 10:38:58Type delete and click delete.
  12482. 10:39:05You can also delete your entire account
  12483. 10:39:08if you want by clicking your profile and
  12484. 10:39:11clicking account
  12485. 10:39:16and then close account. Although you
  12486. 10:39:19lose access to the free tier if you do
  12487. 10:39:21this. I usually keep my account open for
  12488. 10:39:23a while to practice with the free tier.
  12489. 10:39:28So now we put our React app on the
  12490. 10:39:31internet and we created a professional
  12491. 10:39:34real world setup and that's the end of
  12492. 10:39:37this lesson. In this lesson we learned
  12493. 10:39:40AWS or Amazon Web Services which is a
  12494. 10:39:44collection of services that help us put
  12495. 10:39:46stuff on the internet. We learned EC2
  12496. 10:39:49which lets us rent a computer from AWS
  12497. 10:39:52and is known as infrastructure as a
  12498. 10:39:55service or IAS. We learned Elastic
  12499. 10:39:59Beantock which manages EC2 and sets up
  12500. 10:40:02all the software that we need. This is
  12501. 10:40:05also known as a platform as a service or
  12502. 10:40:08path. We put our backend on the
  12503. 10:40:11internet. We learned how to build and
  12504. 10:40:14optimize the front end and put it on the
  12505. 10:40:16internet. We learned how to deploy React
  12506. 10:40:19without a back end. We added a load
  12507. 10:40:22balancer and database to create a more
  12508. 10:40:25realworld setup. And we learned how to
  12509. 10:40:28set up a domain name, SSL certificate,
  12510. 10:40:31and HTTPS for our website.
  12511. 10:40:36Here are some exercises to help you
  12512. 10:40:38practice what we learned in this lesson.
  12513. 10:41:11In this lesson, we're going to learn
  12514. 10:41:13some of the updates in the new version
  12515. 10:41:15of React, and we'll learn how to use
  12516. 10:41:17TypeScript with React.
  12517. 10:41:20On the React website, there's a page
  12518. 10:41:22that lists the updates in the new
  12519. 10:41:25version of React. You can find a link to
  12520. 10:41:28this page in the description.
  12521. 10:41:30So there's a lot of changes here and
  12522. 10:41:33many of these changes are related to
  12523. 10:41:35using React in a framework which is
  12524. 10:41:38basically a layer on top of React.
  12525. 10:41:41For now I'm just going to go over the
  12526. 10:41:43changes that are related to this course.
  12527. 10:41:48The first change is called support for
  12528. 10:41:50document metadata. So this means that we
  12529. 10:41:53can put elements that normally go inside
  12530. 10:41:55the head section like the title, link
  12531. 10:41:58and meta elements directly inside our
  12532. 10:42:01component like this. So this lets us
  12533. 10:42:04change the head section when we render
  12534. 10:42:06this component. For example, in our
  12535. 10:42:09project, we used a title element to
  12536. 10:42:12change the text in the tab when we
  12537. 10:42:14switch pages. The way this works is when
  12538. 10:42:17this component renders, React will put
  12539. 10:42:20this title inside the head section and
  12540. 10:42:23this will change the text in the tab.
  12541. 10:42:26Before React 19, we had to use an npm
  12542. 10:42:29package like React helmet to do this.
  12543. 10:42:33After React 19, we can just put these
  12544. 10:42:36elements directly in our components. So,
  12545. 10:42:38it's easier.
  12546. 10:42:41Another change is called ref as a prop.
  12547. 10:42:44Earlier in this course, we learned about
  12548. 10:42:46refs and we can use a ref to get access
  12549. 10:42:49to an HTML element directly.
  12550. 10:42:53We can also give a ref to a component.
  12551. 10:42:56For example, if we want to get an HTML
  12552. 10:42:59element inside this component.
  12553. 10:43:02Before React 19, ref was a special prop.
  12554. 10:43:06We can't access it inside this
  12555. 10:43:08component. We had to use a feature
  12556. 10:43:10called forward ref.
  12557. 10:43:13After React 19, ref is now a normal
  12558. 10:43:16prop. We can access it like any other
  12559. 10:43:19prop and we don't need to know about
  12560. 10:43:21forward ref anymore, so it's easier.
  12561. 10:43:25The next change is the introduction of
  12562. 10:43:27the React compiler. The React compiler
  12563. 10:43:31optimizes our React code. For example,
  12564. 10:43:35let's say we had a component like this.
  12565. 10:43:37We have a counter and two other
  12566. 10:43:39components inside. When we click the
  12567. 10:43:42counter, this element will update, but
  12568. 10:43:46it will also recreate these other two
  12569. 10:43:49components. Even though clicking the
  12570. 10:43:51counter does not affect the other two
  12571. 10:43:53components.
  12572. 10:43:55That's because by default, when a
  12573. 10:43:57component updates, it will also update
  12574. 10:44:00or recreate all the components inside.
  12575. 10:44:03And this may cause performance issues.
  12576. 10:44:07To avoid this issue before React
  12577. 10:44:10compiler, we had to use three features.
  12578. 10:44:13Use memo, use callback, and memo. And
  12579. 10:44:17this prevents a component from being
  12580. 10:44:19recreated unless one of the props
  12581. 10:44:22changed.
  12582. 10:44:24So don't worry if you don't understand
  12583. 10:44:25this code because after the React
  12584. 10:44:28compiler, the compiler will just add
  12585. 10:44:30these optimizations automatically.
  12586. 10:44:34And now we can just write our code as
  12587. 10:44:36normal and we don't need to worry about
  12588. 10:44:39use memo, use callback or memo anymore
  12589. 10:44:42in most cases.
  12590. 10:44:45So again, the React compiler optimizes
  12591. 10:44:48our React code. All we have to do is to
  12592. 10:44:52set up the React compiler and we will
  12593. 10:44:54get these optimizations. We don't have
  12594. 10:44:57to change any of our code.
  12595. 10:45:02Now, let's learn how to set up the React
  12596. 10:45:04compiler in our project. In the
  12597. 10:45:07description, we'll click this link, go
  12598. 10:45:09to lesson 11, and here we have the
  12599. 10:45:12instructions for setting up the React
  12600. 10:45:14compiler. I put the setup instructions
  12601. 10:45:17here because this process might change
  12602. 10:45:20in the future, and it's not easy to
  12603. 10:45:22update the video. So, follow the
  12604. 10:45:24instructions that you see on your page.
  12605. 10:45:27For me, the first step is to install the
  12606. 10:45:30React compiler npm package. So, I'm
  12607. 10:45:33going to select this command and copy
  12608. 10:45:36it. And then in my project,
  12609. 10:45:40I'm going to go to the command line and
  12610. 10:45:42create a new tab. And I'll close these
  12611. 10:45:45other tabs so that I can start fresh.
  12612. 10:45:49And first, I need to make sure I'm
  12613. 10:45:50running inside e-commerce-pro.
  12614. 10:45:53So, cd ecommerce-
  12615. 10:45:57project
  12616. 10:45:59and then I'll paste the command
  12617. 10:46:02and press enter to install the react
  12618. 10:46:05compiler npm package.
  12619. 10:46:09And then I'll go back and the next
  12620. 10:46:11instruction is to copy this react
  12621. 10:46:14config. So, I'll select this config and
  12622. 10:46:17copy it.
  12623. 10:46:19And then I need to update vit.config.js.
  12624. 10:46:22js and put that react config in here. So
  12625. 10:46:26I'll go to the project
  12626. 10:46:28and open vit. doconfig.js.
  12627. 10:46:32And at the top I see this react config.
  12628. 10:46:35So I'm just going to replace it and
  12629. 10:46:39paste
  12630. 10:46:41and save.
  12631. 10:46:43And that's the setup. Again, the
  12632. 10:46:45instructions might be different when you
  12633. 10:46:47watch this video. So follow the
  12634. 10:46:49instructions on your page.
  12635. 10:46:53Next, let's check if the React compiler
  12636. 10:46:55is working. So, we'll go back to the
  12637. 10:46:58instructions
  12638. 10:47:00and down here we have some instructions
  12639. 10:47:02for checking if React compiler is
  12640. 10:47:05working. Again, follow the instructions
  12641. 10:47:07on your page because they might have
  12642. 10:47:09changed. So, first I'll start the back
  12643. 10:47:12end and the front end using mpm rundev.
  12644. 10:47:16I'll go back to VS Code
  12645. 10:47:18and in the command line I'll create a
  12646. 10:47:21new tab
  12647. 10:47:23and I'm going to go into the backend
  12648. 10:47:25folder using cd ecommerce-backend
  12649. 10:47:31and run mpm rundev.
  12650. 10:47:36And I'll go to the front end tab
  12651. 10:47:39and I'll also run mpm rundev.
  12652. 10:47:45So now both of them are running. I'll go
  12653. 10:47:48back to the instructions
  12654. 10:47:50and the next step is to install the
  12655. 10:47:53React DevTools Chrome extension. So
  12656. 10:47:56we'll click here.
  12657. 10:47:58So this is an extension made
  12658. 10:48:00specifically for React. It has a bunch
  12659. 10:48:02of features that help us with React
  12660. 10:48:04development.
  12661. 10:48:06So here we're going to add this
  12662. 10:48:08extension to our browser.
  12663. 10:48:13And once that's finished, I'll go back
  12664. 10:48:15to the instructions.
  12665. 10:48:19And the next step is in your project,
  12666. 10:48:21open the console and open the components
  12667. 10:48:23tab. So, in VS Code, we're going to open
  12668. 10:48:28this project in the browser
  12669. 10:48:33and I'm going to rightclick
  12670. 10:48:35and inspect.
  12671. 10:48:38And in the console, we're going to click
  12672. 10:48:40here. And we should have a new
  12673. 10:48:43components tab.
  12674. 10:48:46So, we'll click this.
  12675. 10:48:48If it gets stuck, you can refresh.
  12676. 10:48:53And this shows us all the React
  12677. 10:48:55components that are on our page. If we
  12678. 10:48:58scroll down and if we look at our
  12679. 10:49:01components, they should have a badge
  12680. 10:49:03beside them called memo. This means that
  12681. 10:49:06the React compiler is working and it's
  12682. 10:49:09optimizing our React components.
  12683. 10:49:13Now, let's go back and look at some more
  12684. 10:49:15changes in the new version of React.
  12685. 10:49:19The next change is removed prop types
  12686. 10:49:22and default props. So prop types were an
  12687. 10:49:25older feature of React. It basically
  12688. 10:49:28lets us check the type of a prop.
  12689. 10:49:32For example, here we can check if the
  12690. 10:49:35prop text is a string. If we run this
  12691. 10:49:38app and text is not a string, it would
  12692. 10:49:41show a warning in the console.
  12693. 10:49:45So now we use a different tool called
  12694. 10:49:47TypeScript which is better at this type
  12695. 10:49:50of type checking. So React removed the
  12696. 10:49:53prop types feature in favor of
  12697. 10:49:55TypeScript.
  12698. 10:49:57React also removed default props. If a
  12699. 10:50:00prop is undefined, this feature lets us
  12700. 10:50:03set a default value. So now we can do
  12701. 10:50:07this with normal JavaScript. So we don't
  12702. 10:50:09need this React feature anymore.
  12703. 10:50:14Now that we learned that propt types was
  12704. 10:50:16replaced by TypeScript, let's learn how
  12705. 10:50:19to set up TypeScript with React, we'll
  12706. 10:50:22go back to VS Code.
  12707. 10:50:25And to set up TypeScript with React, we
  12708. 10:50:28can use the create vit command. So in
  12709. 10:50:32the command line, we'll create a new tab
  12710. 10:50:36and make sure this is running in the
  12711. 10:50:38React course folder. And we're going to
  12712. 10:50:41run the command npx
  12713. 10:50:44create-ash vit. And we'll type at to
  12714. 10:50:48give it a specific version. And we'll
  12715. 10:50:51click this link in the description. Go
  12716. 10:50:53to lesson 11.
  12717. 10:50:55And we'll use this version of vit. So
  12718. 10:50:58copy the version that you see on your
  12719. 10:51:00page.
  12720. 10:51:01And here we'll paste
  12721. 10:51:04and press enter.
  12722. 10:51:08If it asks to install the package, press
  12723. 10:51:10Y and press enter.
  12724. 10:51:14And as a review, create VIT creates a
  12725. 10:51:17new project. So let's name this project
  12726. 10:51:20ecommerce-
  12727. 10:51:23project-ts
  12728. 10:51:26or TypeScript and press enter.
  12729. 10:51:30For the framework,
  12730. 10:51:32we're going to press down and choose
  12731. 10:51:34React.
  12732. 10:51:37And for the variant, we're going to
  12733. 10:51:39choose TypeScript.
  12734. 10:51:43So this will create a new React project
  12735. 10:51:46that uses TypeScript.
  12736. 10:51:48If we look at the code inside source,
  12737. 10:51:52notice that it uses tsx files instead of
  12738. 10:51:55JSX.
  12739. 10:51:56TSX means this code uses TypeScript
  12740. 10:52:00while JSX means it uses JavaScript.
  12741. 10:52:04So now to run this project, we're going
  12742. 10:52:06to follow the instructions that we see.
  12743. 10:52:08So for me, I'll cd into e-commerce
  12744. 10:52:13dash project-ts
  12745. 10:52:17and run npm install
  12746. 10:52:21to install all the packages that we
  12747. 10:52:23need.
  12748. 10:52:26And then I'm going to open the tab
  12749. 10:52:27that's running the front end. And I'll
  12750. 10:52:30press Ctrl C to stop the front end so we
  12751. 10:52:34don't have any issues. And then in the
  12752. 10:52:36TypeScript tab, we're going to run mpm
  12753. 10:52:40rundev to start this project.
  12754. 10:52:45And now we'll open this in the browser.
  12755. 10:52:48So I already have it open. So I'm going
  12756. 10:52:50to go in here and open this tab and
  12757. 10:52:53refresh.
  12758. 10:52:56Now we have a new React project. But
  12759. 10:52:59this project uses TypeScript instead of
  12760. 10:53:01JavaScript.
  12761. 10:53:04And now I'll give you an intro to
  12762. 10:53:06TypeScript.
  12763. 10:53:08Let's go back to our code.
  12764. 10:53:11And inside the TypeScript project, we're
  12765. 10:53:14going to open app.tsx.
  12766. 10:53:18So first of all, what is Typescript?
  12767. 10:53:21Typescript is just JavaScript with extra
  12768. 10:53:24features.
  12769. 10:53:27So notice that this TypeScript code is
  12770. 10:53:30the same as JavaScript code. The syntax
  12771. 10:53:33is the same and the features are the
  12772. 10:53:36same. For example, in here
  12773. 10:53:40in Typescript, we can create a variable
  12774. 10:53:43the same way as JavaScript using const
  12775. 10:53:47message equals the string hello.
  12776. 10:53:51And then we can console.log log message.
  12777. 10:53:56So as you can see, TypeScript is just
  12778. 10:53:59JavaScript.
  12779. 10:54:00However, TypeScript gives us some extra
  12780. 10:54:03features on top of JavaScript. The main
  12781. 10:54:06feature is we can add types to our
  12782. 10:54:09variables.
  12783. 10:54:11So what are types? Remember that
  12784. 10:54:13JavaScript has different types of values
  12785. 10:54:16like numbers, strings, and objects.
  12786. 10:54:20In Typescript, we can set what type of
  12787. 10:54:23value is inside a variable.
  12788. 10:54:27For example, in this variable, we have a
  12789. 10:54:30string. So, after the variable name, we
  12790. 10:54:34can type colon space and string.
  12791. 10:54:39So, this tells TypeScript that this
  12792. 10:54:41variable contains a string.
  12793. 10:54:45So now below this if I type message and
  12794. 10:54:50then dot TypeScript knows that this is a
  12795. 10:54:54string. So now it's able to suggest all
  12796. 10:54:57the features of a string like two
  12797. 10:55:01lowercase
  12798. 10:55:03and brackets.
  12799. 10:55:06So using TypeScript our computer can
  12800. 10:55:08give us better autocomplete suggestions.
  12801. 10:55:12Next, if I type message
  12802. 10:55:16to fixed and brackets,
  12803. 10:55:20two fixed only works with numbers.
  12804. 10:55:24Because TypeScript knows that this is a
  12805. 10:55:26string, it's going to give us an error
  12806. 10:55:29saying that too does not work on
  12807. 10:55:31strings.
  12808. 10:55:34So, this feature is called type
  12809. 10:55:36checking. TypeScript checks if this code
  12810. 10:55:40works with this type of value and this
  12811. 10:55:43helps us avoid errors. In JavaScript,
  12812. 10:55:46there is no type checking. So if I want
  12813. 10:55:49to put this code on my website,
  12814. 10:55:51JavaScript will just let me do it and it
  12815. 10:55:53will crash my website. But in
  12816. 10:55:56Typescript, if I want to put this code
  12817. 10:55:58on my website, I would save this code
  12818. 10:56:02and then in my command line, press
  12819. 10:56:04control C to stop the server and run npm
  12820. 10:56:08run build.
  12821. 10:56:12Notice that TypeScript actually blocks
  12822. 10:56:14us from building this code and putting
  12823. 10:56:17it on our website because there's an
  12824. 10:56:19error with the types.
  12825. 10:56:21So, as you can see, TypeScript gives us
  12826. 10:56:24type checking, which helps us avoid a
  12827. 10:56:27lot of these coding errors and helps us
  12828. 10:56:29keep our code safer.
  12829. 10:56:32Another feature of TypeScript is type
  12830. 10:56:35inference. Type inference means
  12831. 10:56:38TypeScript can figure out the type
  12832. 10:56:40automatically.
  12833. 10:56:42For example, in this code, this variable
  12834. 10:56:46obviously contains a string. So we
  12835. 10:56:49actually don't need to set the type
  12836. 10:56:51manually here. If we delete this type,
  12837. 10:56:56TypeScript will infer or figure out that
  12838. 10:56:59this is a string. So I can remove it and
  12839. 10:57:02save
  12840. 10:57:04and the error check still works because
  12841. 10:57:06TypeScript knows that this is a string.
  12842. 10:57:09Another example of type inference is up
  12843. 10:57:12here in use state. So this count
  12844. 10:57:16variable contains the number zero.
  12845. 10:57:19TypeScript will infer or figure out that
  12846. 10:57:22count is a number.
  12847. 10:57:25So if we do count to two lowercase and
  12848. 10:57:30brackets, it's going to give us an error
  12849. 10:57:33because two lowercase only works with
  12850. 10:57:35strings and TypeScript figured out that
  12851. 10:57:38this is a number. So, as you can see,
  12852. 10:57:41TypeScript is just JavaScript, but it
  12853. 10:57:44adds extra features like type checking
  12854. 10:57:47and type inference to our code, and this
  12855. 10:57:50helps us find and avoid errors. So, now
  12856. 10:57:53let's save this code. And we'll save
  12857. 10:57:56these changes into git. In the git
  12858. 10:57:59section
  12859. 10:58:01at the top, we're going to use the
  12860. 10:58:03message create new React TypeScript
  12861. 10:58:09project
  12862. 10:58:10and press commit.
  12863. 10:58:16So, we just learned how to create a new
  12864. 10:58:19React project that uses TypeScript.
  12865. 10:58:22Now, let's learn how to convert an
  12866. 10:58:24existing project, like our e-commerce
  12867. 10:58:27project, to Typescript. To do that,
  12868. 10:58:30we're just going to move all of our
  12869. 10:58:32changes over to the new TypeScript
  12870. 10:58:35project.
  12871. 10:58:36Before we begin, if you have any issues
  12872. 10:58:39during this process, please check the
  12873. 10:58:41troubleshooting guide in the description
  12874. 10:58:44since things might change depending on
  12875. 10:58:46the version of VIT.
  12876. 10:58:50To make this easier in the e-commerce
  12877. 10:58:52project, we're going to delete the node
  12878. 10:58:54modules folder for now.
  12879. 10:59:01And then we'll make a copy of this
  12880. 10:59:03folder. So we'll rightclick,
  12881. 10:59:06copy,
  12882. 10:59:08and we'll collapse these folders and
  12883. 10:59:11click the outside and rightclick and
  12884. 10:59:15paste.
  12885. 10:59:17So, this just helps us keep track of
  12886. 10:59:19progress when we're moving the changes
  12887. 10:59:21over. So, we're going to start from top
  12888. 10:59:24to bottom. First, we'll look at the
  12889. 10:59:26public folder. So, in here, the change
  12890. 10:59:30we made is we added this images folder.
  12891. 10:59:33So, we're going to take the public
  12892. 10:59:35folder and we're going to drag it to the
  12893. 10:59:38TypeScript project
  12894. 10:59:40and click move
  12895. 10:59:42and click replace.
  12896. 10:59:46Next, let's go into the source folder
  12897. 10:59:50and we're going to move our changes over
  12898. 10:59:52one by one. So, first in the assets
  12899. 10:59:55folder,
  12900. 10:59:56we didn't make any changes to this
  12901. 10:59:58folder, so we're not going to move it
  12902. 11:00:00over and we can just delete this folder.
  12903. 11:00:06As a reminder, we're not trying to move
  12904. 11:00:09all of our code over. We want to start
  12905. 11:00:12from a default TypeScript project and
  12906. 11:00:14then only move our changes over to the
  12907. 11:00:17new project.
  12908. 11:00:19So let's continue and look at the
  12909. 11:00:21components folder. So this folder is
  12910. 11:00:24completely new. So we're going to move
  12911. 11:00:26this entire folder over to the
  12912. 11:00:28TypeScript project inside the source
  12913. 11:00:31folder
  12914. 11:00:34and click move.
  12915. 11:00:37And if it asks to update the imports,
  12916. 11:00:39we're going to click no because we're
  12917. 11:00:41going to move everything over.
  12918. 11:00:44Next, we're going to go back up and the
  12919. 11:00:48pages folder is new. So, we're going to
  12920. 11:00:50drag this folder into the TypeScript
  12921. 11:00:53project inside source
  12922. 11:00:56and click move
  12923. 11:00:58and update the imports. Click no.
  12924. 11:01:03Next, we'll scroll up and the utils
  12925. 11:01:06folder is new. So, we're going to select
  12926. 11:01:08this folder and drag it into the
  12927. 11:01:10TypeScript folder inside source. So,
  12928. 11:01:14move and click no.
  12929. 11:01:18And next, we'll keep going one by one.
  12930. 11:01:22For app.css,
  12931. 11:01:24we made it empty. So, we're going to
  12932. 11:01:26move this over. So, drag it into source.
  12933. 11:01:31Click move and click replace
  12934. 11:01:35and scroll back up for app.jsx.
  12935. 11:01:40We made a bunch of changes in here. So,
  12936. 11:01:42we're going to move this over. So, drag
  12937. 11:01:45it into source
  12938. 11:01:48and click move.
  12939. 11:01:50However, notice in the TypeScript
  12940. 11:01:53project, the file is called app.tsx.
  12941. 11:01:57So, we're going to replace this code
  12942. 11:01:58with our file. So, first we're going to
  12943. 11:02:01select this and delete app.tsx.
  12944. 11:02:07And then we'll rightclick app.jsx
  12945. 11:02:10and rename
  12946. 11:02:12and rename it to tsx.
  12947. 11:02:17Now, we'll scroll back up and continue
  12948. 11:02:20moving our changes.
  12949. 11:02:22So, index.css,
  12950. 11:02:24we completely changed this file. So,
  12951. 11:02:27we're going to move it into the
  12952. 11:02:29TypeScript project into source.
  12953. 11:02:33Click move and click replace
  12954. 11:02:37and scroll back up. And finally, in
  12955. 11:02:41main.jsx,
  12956. 11:02:42we just added the router. So, let's move
  12957. 11:02:45this file as well. We'll select it and
  12958. 11:02:48drag it into source
  12959. 11:02:51and move.
  12960. 11:02:53And again in Typescript the file is
  12961. 11:02:55called main.tsx.
  12962. 11:02:58We'll select it and delete.
  12963. 11:03:02And we're going to rename our file.
  12964. 11:03:06Right click, rename
  12965. 11:03:08main.tsx,
  12966. 11:03:11and press enter.
  12967. 11:03:14Update the imports. Select no.
  12968. 11:03:18We'll scroll back up and we moved all of
  12969. 11:03:21the code from the source folder so we
  12970. 11:03:24can delete it.
  12971. 11:03:27Next, we don't need to move the starting
  12972. 11:03:30code folder because this was just for
  12973. 11:03:32practice when we were setting up this
  12974. 11:03:34project. So, we can just delete this
  12975. 11:03:37folder.
  12976. 11:03:41Now, let's move all the files over to
  12977. 11:03:44the TypeScript project. So in git
  12978. 11:03:47ignore, we didn't make any changes to
  12979. 11:03:50this file, so we don't need to move it
  12980. 11:03:52over and we can delete it.
  12981. 11:03:57In eslint.config.js,
  12982. 11:04:00we added one change to this file at the
  12983. 11:04:02bottom, which is we added this rule to
  12984. 11:04:06disable prop types. So we'll select this
  12985. 11:04:08code and copy.
  12986. 11:04:12And in the TypeScript project, we're
  12987. 11:04:15going to open eslint.config.
  12988. 11:04:18And at the bottom in this rules object,
  12989. 11:04:22we're going to paste and move that
  12990. 11:04:24change over and save.
  12991. 11:04:28So again, we don't want to move all of
  12992. 11:04:30our code over. We just want to move the
  12993. 11:04:32changes over because a lot of this code
  12994. 11:04:35is specific to TypeScript.
  12995. 11:04:38So, we'll scroll back up here and we can
  12996. 11:04:41delete this file because we moved it
  12997. 11:04:44over.
  12998. 11:04:46Next, let's keep going in here. For
  12999. 11:04:50index.html,
  13000. 11:04:52we changed a few parts of this code. So,
  13001. 11:04:55we're going to move these changes over
  13002. 11:04:58to help us compare this code with the
  13003. 11:05:00TypeScript version. I'm going to scroll
  13004. 11:05:03down
  13005. 11:05:04and take this index.html HTML and then
  13006. 11:05:07move it at the bottom here so that I can
  13007. 11:05:10compare these side by side.
  13008. 11:05:13So first the first three lines here we
  13009. 11:05:16did not make any changes so we can skip
  13010. 11:05:19them.
  13011. 11:05:20And then here the next three lines we
  13012. 11:05:24also did not make any changes so we can
  13013. 11:05:26skip them. And here we added a base
  13014. 11:05:30element. So we're going to select this
  13015. 11:05:32and copy. And in the TypeScript version,
  13016. 11:05:36we're going to paste that change.
  13017. 11:05:39And here we also deleted the title. So
  13018. 11:05:42in the TypeScript version, we're also
  13019. 11:05:44going to delete the title.
  13020. 11:05:47And here we added a font called Roboto.
  13021. 11:05:51So we're going to move this change by
  13022. 11:05:53selecting it and copy.
  13023. 11:05:56And in here we'll paste the font. And if
  13024. 11:06:01we need to fix the formatting, we can
  13025. 11:06:03select this and press shift tab to
  13026. 11:06:06remove some indents
  13027. 11:06:10and I'll scroll down.
  13028. 11:06:14And for the rest of the code, we did not
  13029. 11:06:16make any changes. So now we moved all
  13030. 11:06:19the changes over. I'm going to save this
  13031. 11:06:22file and close this for now.
  13032. 11:06:26And then in the copy, we can delete
  13033. 11:06:28index.html.
  13034. 11:06:32And next, we don't need to move packaged
  13035. 11:06:35lock.json over because this will be
  13036. 11:06:38autogenerated when we npm install. So,
  13037. 11:06:41we can just delete this file
  13038. 11:06:46and I'll scroll back up
  13039. 11:06:51and we'll move over package.json.
  13040. 11:06:55In this file, we didn't change anything
  13041. 11:06:57at the top, but we did add some
  13042. 11:07:00dependencies and dev dependencies. So,
  13043. 11:07:02we're going to move these over. We'll
  13044. 11:07:05select the dependencies and copy.
  13045. 11:07:09And then in the TypeScript project, open
  13046. 11:07:12package.json.
  13047. 11:07:15And in these dependencies,
  13048. 11:07:18we'll type comma and paste.
  13049. 11:07:21And then we'll remove any duplicates.
  13050. 11:07:24So, we'll remove React and React DOM and
  13051. 11:07:30save.
  13052. 11:07:32And we'll scroll back up
  13053. 11:07:35and open package.json.
  13054. 11:07:37And we'll do the same thing with the dev
  13055. 11:07:39dependencies. So, we'll select all these
  13056. 11:07:42dependencies
  13057. 11:07:44and copy.
  13058. 11:07:46And in the TypeScript project
  13059. 11:07:49package.json JSON in the dev
  13060. 11:07:51dependencies at the bottom we'll type
  13061. 11:07:54comma and paste
  13062. 11:07:58and we'll remove all these duplicates.
  13063. 11:08:01So I'll remove eslint
  13064. 11:08:05and I'll remove these duplicates
  13065. 11:08:08and these duplicates
  13066. 11:08:11and these duplicates
  13067. 11:08:15and remove this
  13068. 11:08:18until there are no more duplicates
  13069. 11:08:20remaining.
  13070. 11:08:22And now we'll save.
  13071. 11:08:24And then we're going to run npm install
  13072. 11:08:26to make sure we successfully install all
  13073. 11:08:29these packages. In the command line,
  13074. 11:08:32we'll go to the tab for the TypeScript
  13075. 11:08:34project and we'll run npm install.
  13076. 11:08:41And now we can scroll up and delete
  13077. 11:08:44package.json.
  13078. 11:08:48And we'll keep going for readme.md. We
  13079. 11:08:52didn't change this file. So we can
  13080. 11:08:54delete it.
  13081. 11:08:59And this file setup test.js is new. So
  13082. 11:09:03we'll move this over to the TypeScript
  13083. 11:09:05project.
  13084. 11:09:07And we'll scroll up and we're going to
  13085. 11:09:10move over vit.config.js.
  13086. 11:09:12So in here we added a bunch of
  13087. 11:09:14configurations.
  13088. 11:09:16So in the TypeScript project
  13089. 11:09:19we're going to drag vit.config.ts ts at
  13090. 11:09:23the bottom
  13091. 11:09:25and we're going to move our changes
  13092. 11:09:27over. So, inside the config,
  13093. 11:09:32we added some stuff for the React
  13094. 11:09:34config. So, we're going to select this
  13095. 11:09:37React config and copy
  13096. 11:09:40and then select this code, remove it,
  13097. 11:09:44and paste.
  13098. 11:09:46And we'll keep going. So, all of these
  13099. 11:09:49configs are new. So, we can just select
  13100. 11:09:52this code
  13101. 11:09:54and then copy.
  13102. 11:09:57And inside the TypeScript version, we're
  13103. 11:10:00going to paste
  13104. 11:10:03and save.
  13105. 11:10:06So, now we moved all of our changes over
  13106. 11:10:08to the new project. And we can delete
  13107. 11:10:11this file.
  13108. 11:10:15And I'll close this for now.
  13109. 11:10:17And finally, vtest.config.js.
  13110. 11:10:20JS is new. So, we can drag it into the
  13111. 11:10:23project.
  13112. 11:10:26And now we moved all of our changes over
  13113. 11:10:29into the TypeScript project. And I can
  13114. 11:10:32delete this copy.
  13115. 11:10:38Next, we're going to go into the
  13116. 11:10:39TypeScript project and we're going to
  13117. 11:10:42fix the errors. First, we'll open
  13118. 11:10:45main.tsx.
  13119. 11:10:47And in here, instead of importing from
  13120. 11:10:50app.jsx,
  13121. 11:10:51the file is now app.tsx.
  13122. 11:10:55So we're going to update that.
  13123. 11:10:58Also, we have an error here. So this
  13124. 11:11:01code tries to get an element from the
  13125. 11:11:03page using the ID root. However, this
  13126. 11:11:07element might not exist. So TypeScript
  13127. 11:11:10gives us an error telling us this value
  13128. 11:11:13might be null. However, if we check our
  13129. 11:11:17index.html
  13130. 11:11:20and scroll down, we have an element here
  13131. 11:11:23with the ID of root. So, we know that
  13132. 11:11:26this element definitely exists.
  13133. 11:11:29In main.tsx,
  13134. 11:11:31to tell Typescript that this value
  13135. 11:11:34definitely exists. At the end, we're
  13136. 11:11:37going to add exclamation and save.
  13137. 11:11:42So this tells Typescript that this value
  13138. 11:11:45definitely exists and it will not be
  13139. 11:11:48null. And now the error is gone.
  13140. 11:11:52Next we'll open app.tsx.
  13141. 11:11:57And up here we get some errors when we
  13142. 11:11:59are trying to import these files. The
  13143. 11:12:02reason we get these errors is because
  13144. 11:12:04these files are still.jsx
  13145. 11:12:08files or JavaScript files. So, we could
  13146. 11:12:11rename all of our files totsx,
  13147. 11:12:15but then Typescript will start doing
  13148. 11:12:16type checking in these files and we may
  13149. 11:12:19get a bunch of errors.
  13150. 11:12:22Instead,
  13151. 11:12:23in our files, we can scroll down to
  13152. 11:12:27tsconfig.app.json.
  13153. 11:12:31And inside here,
  13154. 11:12:34we're going to add a setting double
  13155. 11:12:36quotes allow capital J S
  13156. 11:12:40colon true and comma
  13157. 11:12:44and save.
  13158. 11:12:47So this setting lets us mix TypeScript
  13159. 11:12:50and JavaScript files in our project. So
  13160. 11:12:53now in app.tsx, tsx.
  13161. 11:12:56Our TypeScript code can also import from
  13162. 11:12:59JavaScript files and it won't cause an
  13163. 11:13:02error. So the allowJS setting lets us
  13164. 11:13:06gradually convert a JavaScript project
  13165. 11:13:08to TypeScript, one file at a time.
  13166. 11:13:12Now that we fixed the errors, let's run
  13167. 11:13:15this project again to see if our
  13168. 11:13:17e-commerce project works with
  13169. 11:13:18TypeScript. in the command line. Make
  13170. 11:13:22sure you're in e-commerce project ts and
  13171. 11:13:25we're going to npm rundev
  13172. 11:13:28and press enter.
  13173. 11:13:32And now if we open this URL in the
  13174. 11:13:34browser
  13175. 11:13:36and refresh
  13176. 11:13:39and now you can see that our e-commerce
  13177. 11:13:42project is working with TypeScript. If
  13178. 11:13:45we try some of the features like add to
  13179. 11:13:47cart, you'll see that everything still
  13180. 11:13:50works. If this does not work for you,
  13181. 11:13:53you can check the troubleshooting guide
  13182. 11:13:54in the description.
  13183. 11:13:57Now, let's go back to our code
  13184. 11:14:00and let's rename our folders so that we
  13185. 11:14:03use TypeScript by default. We're going
  13186. 11:14:06to scroll up and we're going to rename
  13187. 11:14:08e-commerce project. Right click, rename
  13188. 11:14:13to e-commerce project dashjs
  13189. 11:14:16for JavaScript.
  13190. 11:14:18And then we're going to rename
  13191. 11:14:20e-commerce project-ts
  13192. 11:14:23to e-commerce project.
  13193. 11:14:28So now by default our project will be
  13194. 11:14:31using TypeScript.
  13195. 11:14:33And just to make sure, we're going to go
  13196. 11:14:34to the command line and stop the V
  13197. 11:14:37server. And we're going to delete all
  13198. 11:14:39our tabs. So here we'll also control C
  13199. 11:14:42to stop the back end and delete all the
  13200. 11:14:45tabs. And then we'll reopen the command
  13201. 11:14:48line using the terminal menu and new
  13202. 11:14:51terminal.
  13203. 11:14:54And we're going to restart the back end
  13204. 11:14:56using cd ecommerce-backend
  13205. 11:15:01mpm rundev
  13206. 11:15:05and create a new tab
  13207. 11:15:10and we'll run the front end cd
  13208. 11:15:12ecommerce- project
  13209. 11:15:15and npm rundev.
  13210. 11:15:20And in the browser, we'll refresh
  13211. 11:15:25and our TypeScript project still works.
  13212. 11:15:28We'll go to VS Code. And finally, we're
  13213. 11:15:32going to organize these folders a little
  13214. 11:15:34bit. So, right now, we're no longer
  13215. 11:15:36using the e-commerce project JavaScript
  13216. 11:15:39version or the React basics folder. So,
  13217. 11:15:42let's click outside and we'll click here
  13218. 11:15:44to create a new folder. and let's call
  13219. 11:15:47it old dash projects.
  13220. 11:15:51And then we'll drag the JavaScript
  13221. 11:15:53version into here.
  13222. 11:15:57And we'll move React Basics into here.
  13223. 11:16:04So now our folders are a little bit more
  13224. 11:16:06organized.
  13225. 11:16:09So now let's save this into Git. We'll
  13226. 11:16:11open the git section and at the top
  13227. 11:16:15we'll use the message convert e-commerce
  13228. 11:16:20project to type script and commit.
  13229. 11:16:30Next we're going to learn an intro to
  13230. 11:16:32TypeScript with React. First, we'll go
  13231. 11:16:36into our files
  13232. 11:16:38and in the project, we're going to go to
  13233. 11:16:41source the utils folder and we're going
  13234. 11:16:45to look at money.js.
  13235. 11:16:48Currently, this is a JavaScript file.
  13236. 11:16:51That means there is no type checking in
  13237. 11:16:54this code. So, we're going to convert
  13238. 11:16:56this to TypeScript by right-clicking
  13239. 11:17:00and rename.
  13240. 11:17:02And we'll rename it to money.t. ts or
  13241. 11:17:06typescript.
  13242. 11:17:09So this converts the file into
  13243. 11:17:11typescript and now it will enable type
  13244. 11:17:14checking and now we will get an error
  13245. 11:17:16here.
  13246. 11:17:18So this error says amount sent
  13247. 11:17:21implicitly has an any type. So what this
  13248. 11:17:25means is that earlier we learned the
  13249. 11:17:27feature type inference which means
  13250. 11:17:29typescript can figure out the type
  13251. 11:17:32automatically.
  13252. 11:17:34However, in this situation, TypeScript
  13253. 11:17:37does not have enough information to
  13254. 11:17:40figure out the type of amount sets. So,
  13255. 11:17:43we have to set the type manually.
  13256. 11:17:46To set the type of a parameter after
  13257. 11:17:50this parameter, we're going to type
  13258. 11:17:52colon space and then the type of this
  13259. 11:17:56value. So, in this case, amount sent
  13260. 11:17:59should be a number. So, we're going to
  13261. 11:18:01type number and save.
  13262. 11:18:06And now that TypeScript knows what type
  13263. 11:18:09of value this is, the error will go
  13264. 11:18:11away. And now we successfully converted
  13265. 11:18:14this file to TypeScript.
  13266. 11:18:17Let's do another example. We're going to
  13267. 11:18:19go to the components folder and we're
  13268. 11:18:22going to convert header.jsx into
  13269. 11:18:25TypeScript. So we're going to rename
  13270. 11:18:27this file
  13271. 11:18:29to header.tsx.
  13272. 11:18:33So tsx is just a TypeScript file, but
  13273. 11:18:37the X means we can write HTML inside
  13274. 11:18:40this file. So it's just like JSX except
  13275. 11:18:44it uses TypeScript.
  13276. 11:18:47So now this converts the file to
  13277. 11:18:48TypeScript and it enables type checking
  13278. 11:18:51and we get some errors here.
  13279. 11:18:54If we look at this error,
  13280. 11:18:57we get a similar error. Cart implicitly
  13281. 11:19:00has an any type. So again, this means
  13282. 11:19:03that TypeScript does not have enough
  13283. 11:19:06information to figure out the type of
  13284. 11:19:08cart automatically. So we have to set
  13285. 11:19:11the type manually.
  13286. 11:19:14We might try to add the type after cart
  13287. 11:19:16like this. However, this is not how we
  13288. 11:19:19add a type to this code because remember
  13289. 11:19:23this code is actually a shortcut for
  13290. 11:19:26this code. This is called the
  13291. 11:19:28dstructuring shortcut.
  13292. 11:19:31So this whole thing is the parameter.
  13293. 11:19:35So we need to add the type here
  13294. 11:19:39after the parameter.
  13295. 11:19:42So this code is a shortcut for props
  13296. 11:19:45which is an object. To tell Typescript
  13297. 11:19:48that this is an object here we're going
  13298. 11:19:51to type curly brackets
  13299. 11:19:54and then inside here we can also tell
  13300. 11:19:57TypeScript what properties are in this
  13301. 11:20:00object. So in this object we have a cart
  13302. 11:20:04property. So in the type we're also
  13303. 11:20:06going to add a cart property. And
  13304. 11:20:10remember that the cart is an array like
  13305. 11:20:13this.
  13306. 11:20:15So we're also going to tell Typescript
  13307. 11:20:17that this cart is an array using colon
  13308. 11:20:20and square bracket semicolon.
  13309. 11:20:24So again, this code tells Typescript
  13310. 11:20:27that this value is an object with a
  13311. 11:20:30property called cart and the cart is an
  13312. 11:20:33array.
  13313. 11:20:35Next, we can also tell Typescript what
  13314. 11:20:38is inside this array. In front of the
  13315. 11:20:41array, we can type curly brackets to
  13316. 11:20:44tell Typescript that this is an array of
  13317. 11:20:47objects.
  13318. 11:20:50And inside this object, we can also tell
  13319. 11:20:52TypeScript what properties each object
  13320. 11:20:55has. So inside our cart array, each
  13321. 11:20:59object has a product ID and this product
  13322. 11:21:03ID is a string. So we can tell that to
  13323. 11:21:06TypeScript using colon string semicolon
  13324. 11:21:12and each object also has a quantity and
  13325. 11:21:15this is a number. So we can tell that to
  13326. 11:21:18Typescript using colon number semicolon
  13327. 11:21:23and each object has a delivery option ID
  13328. 11:21:28which is a string. So colon string
  13329. 11:21:31semicolon.
  13330. 11:21:34So using this code we can tell
  13331. 11:21:36Typescript exactly what type of value
  13332. 11:21:39this is. It's an object with a property
  13333. 11:21:42called cart. And the cart is an array.
  13334. 11:21:46And each value in the array has these
  13335. 11:21:49three properties.
  13336. 11:21:51So now TypeScript can actually look at
  13337. 11:21:53the rest of our code and check if each
  13338. 11:21:56value in the array has a quantity
  13339. 11:21:59and if we accidentally misspell quantity
  13340. 11:22:02here like this. TypeScript can use this
  13341. 11:22:06type here to check if we made a mistake.
  13342. 11:22:09So this helps us avoid a lot of mistakes
  13343. 11:22:11when we're coding. So, let's add that
  13344. 11:22:14back and save.
  13345. 11:22:20Next, we're going to organize this code
  13346. 11:22:22a little bit. So, putting a complex type
  13347. 11:22:26directly in the code like this is a
  13348. 11:22:29little hard to read. So, we can actually
  13349. 11:22:32take this type and then save it
  13350. 11:22:34separately.
  13351. 11:22:36above this component we can use type
  13352. 11:22:40space and then give this a name.
  13353. 11:22:44So here this value is the props for the
  13354. 11:22:47header component. So we'll name this
  13355. 11:22:50header props and we'll type equal and
  13356. 11:22:55we're going to move this type up here.
  13357. 11:22:57So we'll select this type and cut.
  13358. 11:23:01And then in here we're going to paste.
  13359. 11:23:07So this is called a type alias and it
  13360. 11:23:10works just like a variable but for
  13361. 11:23:12types. So we can save a type in here and
  13362. 11:23:17then we can use this type later in our
  13363. 11:23:19code. For example, down here, we can
  13364. 11:23:23just use this type header props
  13365. 11:23:28and it will substitute this type into
  13366. 11:23:31our code just like a variable.
  13367. 11:23:35And now our code is much cleaner. So
  13368. 11:23:38that's an intro to how to use TypeScript
  13369. 11:23:40with React. We basically add types to
  13370. 11:23:44React features like props.
  13371. 11:23:48So now let's save
  13372. 11:23:50and we'll save these changes into git.
  13373. 11:23:53In the git section
  13374. 11:23:56at the top we'll use the message convert
  13375. 11:24:00money utils and header to type script
  13376. 11:24:07and commit.
  13377. 11:24:12Finally, we're going to learn how to use
  13378. 11:24:14AI or artificial intelligence with
  13379. 11:24:18React. After we learn React, we can use
  13380. 11:24:21AI to help us write React code faster.
  13381. 11:24:25The easiest way to get started with AI
  13382. 11:24:28is to use GitHub Copilot, which is built
  13383. 11:24:31into VS Code. Before we begin, AI tools
  13384. 11:24:35are changing very quickly. If something
  13385. 11:24:38in this section is not working, you can
  13386. 11:24:40check the troubleshooting guide in the
  13387. 11:24:42description.
  13388. 11:24:44To set up GitHub Copilot, we're going to
  13389. 11:24:47install the GitHub Copilot extension.
  13390. 11:24:50So, in the extension section, we need to
  13391. 11:24:53make sure we have the GitHub Copilot
  13392. 11:24:56extension installed. If it's not
  13393. 11:24:58installed, we can search up here for
  13394. 11:25:02GitHub Copilot
  13395. 11:25:06and install this extension.
  13396. 11:25:12Once we have that installed,
  13397. 11:25:14we're going to click this C-pilot icon
  13398. 11:25:17up here and we'll need to log in to our
  13399. 11:25:20GitHub account. So, we'll click sign in
  13400. 11:25:25and then we need to log in to GitHub. If
  13401. 11:25:28you don't have a GitHub account, you can
  13402. 11:25:30create one by clicking here or going to
  13403. 11:25:33github.com.
  13404. 11:25:34So, I'm going to log into my GitHub
  13405. 11:25:37account
  13406. 11:25:39and then I'll click continue
  13407. 11:25:43and I'll click here to go back to VS
  13408. 11:25:46Code.
  13409. 11:25:48Once we finish the setup, we can start
  13410. 11:25:50using some features of AI. The first
  13411. 11:25:54feature is autocomplete.
  13412. 11:25:57So, let's say that we're in the header
  13413. 11:26:00and we'll close this window for now.
  13414. 11:26:04And let's scroll down to the HTML
  13415. 11:26:08and we're going to go to the right
  13416. 11:26:10section.
  13417. 11:26:11Let's say that inside the header, we
  13418. 11:26:13want to add a link to a login page. So
  13419. 11:26:17here we can just start typing in this
  13420. 11:26:20code
  13421. 11:26:22and the AI will give us some
  13422. 11:26:24autocomplete suggestions. To accept
  13423. 11:26:28these suggestions, we can press tab.
  13424. 11:26:31So this creates a new link and some
  13425. 11:26:33text. And I'll press enter.
  13426. 11:26:37And it gives another suggestion. So I
  13427. 11:26:39press tab.
  13428. 11:26:41And this creates the closing tag for the
  13429. 11:26:43link.
  13430. 11:26:45So you notice that the autocomplete is
  13431. 11:26:47very smart because the AI can read and
  13432. 11:26:51understand the rest of our code and it
  13433. 11:26:54can suggest code that follows the same
  13434. 11:26:56patterns as the rest of our code. For
  13435. 11:26:58example, here it creates a link element
  13436. 11:27:02and it gave it a class name that is
  13437. 11:27:04similar to the other class names.
  13438. 11:27:07However, when using AI, we may need to
  13439. 11:27:09go in and modify things to make sure we
  13440. 11:27:12get exactly what we want. So, here the
  13441. 11:27:16AI created a link to the homepage, but
  13442. 11:27:19what we actually want is a link to a
  13443. 11:27:21login page. So, I can click here and
  13444. 11:27:24modify the URL path to slash login.
  13445. 11:27:29And I can also change the text to login.
  13446. 11:27:34And I'll change this to login. And I
  13447. 11:27:37don't really have an icon for the login
  13448. 11:27:39page. So we can delete it.
  13449. 11:27:42And I'll change this to login.
  13450. 11:27:45So using AI, I'm able to write this code
  13451. 11:27:48a lot faster.
  13452. 11:27:50Another feature of AI is we can give the
  13453. 11:27:53AI instructions.
  13454. 11:27:55For example, let's remove this code for
  13455. 11:27:58now.
  13456. 11:28:00And we can click this copilot icon.
  13457. 11:28:04and it will open this chat window. And
  13458. 11:28:07inside here we can just tell the AI what
  13459. 11:28:11we want to do. For example, in here we
  13460. 11:28:14can say in the header
  13461. 11:28:18add a link to the login page and make
  13462. 11:28:24sure in this dropdown you have agent or
  13463. 11:28:28edit selected
  13464. 11:28:30and we'll press enter.
  13465. 11:28:33and the AI will make edits to our code.
  13466. 11:28:38So here you can see that it added a
  13467. 11:28:40login link like before. So we can press
  13468. 11:28:44keep to keep these changes or undo if we
  13469. 11:28:47don't like these changes. So this looks
  13470. 11:28:49good to me. So I'm going to press keep.
  13471. 11:28:54And again I'll go in here and modify it
  13472. 11:28:56a bit. So here it added the login link
  13473. 11:29:00after the orders link but I want to put
  13474. 11:29:02this before the orders. So I can just
  13475. 11:29:05select this code and cut
  13476. 11:29:09and at the top
  13477. 11:29:12paste that code.
  13478. 11:29:16So, as you can see, by using AI, we can
  13479. 11:29:19write React code faster. But you'll
  13480. 11:29:22notice that we still need to understand
  13481. 11:29:24the code to check if it's what we want
  13482. 11:29:27and to modify the code if we need to.
  13483. 11:29:30So, it's important to learn how to code
  13484. 11:29:33first and then we can use AI to help us
  13485. 11:29:36code faster. So now I'll go into the git
  13486. 11:29:40section and I'm actually going to undo
  13487. 11:29:43these changes because this was just an
  13488. 11:29:46example.
  13489. 11:29:50And that's the end of this lesson. In
  13490. 11:29:52this lesson, we learned some of the
  13491. 11:29:54updates in the new version of React. We
  13492. 11:29:58learned how to set up the React compiler
  13493. 11:30:00to automatically optimize our React
  13494. 11:30:03code. We set up a new Typescript React
  13495. 11:30:07project using create vit. We did an
  13496. 11:30:10intro to TypeScript and learned basic
  13497. 11:30:13features like type checking and type
  13498. 11:30:15inference. We converted our e-commerce
  13499. 11:30:18project to TypeScript.
  13500. 11:30:21We learned how to use TypeScript with
  13501. 11:30:23React. And we learned how to use AI with
  13502. 11:30:27React to help us write code faster. Here
  13503. 11:30:32are some exercises to help you practice
  13504. 11:30:34what we learned in this lesson.
  13505. 11:31:09Good job on finishing this React
  13506. 11:31:11tutorial. If you want to continue
  13507. 11:31:13learning with me, you can check out the
  13508. 11:31:15premium version of this course where I
  13509. 11:31:17will be adding extra lessons and
  13510. 11:31:19projects in the future. Currently, the
  13511. 11:31:22premium version has 11 lessons, but I'll
  13512. 11:31:24be adding more lessons in the future.
  13513. 11:31:27You can also check out the membership
  13514. 11:31:29for extra content on AI and backend.
  13515. 11:31:33As the next step after learning React, I
  13516. 11:31:36recommend moving to the back end and
  13517. 11:31:38learning the command line and NodeJS.
  13518. 11:31:41I'll be putting my work in progress on
  13519. 11:31:43these technologies inside the membership
  13520. 11:31:45first, so be on the lookout for that.
  13521. 11:31:49Thanks so much for watching and taking
  13522. 11:31:50this course with me and I'll see you in
  13523. 11:31:52the next one.

About this transcript

This page contains the full transcript of YouTube transcript (TtPXvEcE11E) , generated from the public captions YouTube serves with the video. The transcript has 85,918 words across 13,523 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.