YouTube transcript (TtPXvEcE11E) — Transcript
Full transcript
- 0:00Welcome to the React full course. In
- 0:03this course, we'll learn how to build
- 0:04complex websites with React. And by the
- 0:07end of this course, we'll build two
- 0:09projects, a chatbot and an e-commerce
- 0:12project.
- 0:14We'll start from the very basics of
- 0:16React like how to set up React with
- 0:19JavaScript and we'll learn each feature
- 0:21in detail step by step. We'll learn the
- 0:24major features of React as well as
- 0:27advanced features like how to use React
- 0:30with a backend, deploy React to the
- 0:33internet with AWS,
- 0:35React with TypeScript, and React with
- 0:38AI.
- 0:40You can find the sections of this course
- 0:43below the video here and here.
- 0:47You can also change the speed of the
- 0:49video here.
- 0:53And you can find subtitles for different
- 0:55languages here.
- 0:59And after each lesson, I'll give you a
- 1:01bunch of exercises that you can do on
- 1:03your own to practice the skills that we
- 1:06learned. In total, this course has over
- 1:10125 exercises.
- 1:12So, with that being said, let's get
- 1:14started.
- 1:25I'm going to be doing this course on
- 1:26Windows, but the steps are the same if
- 1:28you're using a Mac. Before we begin,
- 1:31we're going to need two pieces of
- 1:33software. Number one is a web browser,
- 1:36and number two is a code editor. The
- 1:40most popular web browser for web
- 1:41development is Google Chrome and the
- 1:44most popular code editor for web
- 1:45development is VS Code. So I recommend
- 1:49using these. If you need to install
- 1:51these, you can search for them on
- 1:53Google.
- 1:55Click the link and follow the
- 1:57instructions to download and install.
- 2:02Now you'll also need to know the basics
- 2:05of JavaScript.
- 2:07React is built on JavaScript and we're
- 2:09going to be writing a lot of JavaScript
- 2:11when using React. Don't worry, you don't
- 2:14need to be an expert in JavaScript. You
- 2:16just need to know some basic features
- 2:18like the ones listed here. And I'll also
- 2:21try to review JavaScript features as we
- 2:23go through this course. If you want to
- 2:26learn JavaScript in more detail, you can
- 2:28check out my JavaScript full course in
- 2:31the video description.
- 2:34Finally, you'll need to know the basics
- 2:36of HTML.
- 2:38When using React, we're going to be
- 2:40writing a lot of HTML.
- 2:43Again, don't worry. You don't need to be
- 2:45an expert in HTML. You just need to know
- 2:48some basic features like the ones listed
- 2:50here. And I'll try my best to review
- 2:53HTML features as we go through this
- 2:56course.
- 2:57If you want to learn HTML in more
- 2:59detail, you can check out my HTML and
- 3:02CSS full course in the video
- 3:04description.
- 3:06If you have any issues in this course,
- 3:08you can check the troubleshooting guide
- 3:10in the description.
- 3:13Now that we have all the requirements
- 3:15that we need, we're ready to learn
- 3:17React.
- 3:21On our computer, we're going to go to
- 3:22our desktop and we're going to create a
- 3:25new folder. So here I'm going to
- 3:28rightclick and create a new folder.
- 3:33And this folder is going to contain all
- 3:35the code that we write in this course.
- 3:38So let's name it react dashc course and
- 3:42press enter.
- 3:45Next we're going to open this folder in
- 3:47our code editor VS Code. So, we'll open
- 3:51VS Code
- 3:53and then click File,
- 3:56Open Folder
- 3:58and select the folder we just created.
- 4:01So, I created my folder on the desktop
- 4:04and it's this folder right here. So,
- 4:06we'll select this folder and then click
- 4:09select folder
- 4:12and we can close this welcome page.
- 4:15So, now let's create a new file to learn
- 4:18React. So up here, we're going to create
- 4:20a new file and we're going to name it
- 4:24react-basics.html
- 4:28and press enter.
- 4:32As a review, a file that ends inh
- 4:35contains HTML code and this creates a
- 4:39website.
- 4:42Next, we're going to create a website
- 4:44and we're going to set up React on this
- 4:46website. Now, to save time, we're
- 4:49actually going to download some starting
- 4:51code.
- 4:54So, we're going to open our web browser,
- 4:56Google Chrome.
- 4:58And at the top, we're going to type
- 5:01super simple.dev/react-basics
- 5:05[Music]
- 5:07and press enter.
- 5:10So, this page has some starting HTML
- 5:13code we can use to learn React. So now
- 5:16let's click on this page and we're going
- 5:18to select all this code by pressing
- 5:20control A on Windows or command A on Mac
- 5:24and we're going to copy paste this code
- 5:27into our file. So we'll right click
- 5:30copy. We can also press control C on
- 5:34Windows or command C on Mac
- 5:39and then open our code editor and then
- 5:42inside this HTML file we're going to
- 5:44rightclick and paste. We can also press
- 5:48control +VV on Windows or commandV on
- 5:51Mac.
- 5:54And finally let's save this file by
- 5:57pressing control S on Windows or command
- 6:00S on Mac.
- 6:03Next, we're going to open this file in
- 6:05our web browser to see what the website
- 6:08looks like. To make this easier, we're
- 6:11going to use a VS Code extension called
- 6:13Live Server. So, the benefit of live
- 6:16server is it will automatically refresh
- 6:19the web page whenever we change some
- 6:21code. So, it's going to make our coding
- 6:23easier.
- 6:24If you don't have live server installed,
- 6:27you can click the extension section and
- 6:30then search for live server at the top
- 6:35and then click install.
- 6:39Once liveer is installed, we're going to
- 6:42go back to our code and we're going to
- 6:44open this file in live server by
- 6:46rightclicking in the file and click open
- 6:50with live server.
- 6:55So live server will open the HTML file
- 6:58using our browser and it's going to
- 7:00create a website and we should see the
- 7:03message welcome to super simple dev
- 7:05react course on this website.
- 7:09So this message is displayed by React
- 7:12and this means that we successfully set
- 7:14up React on this website.
- 7:18Now, let's go back to VS Code.
- 7:22And this is the minimum amount of code
- 7:24that we need to set up React. We're
- 7:28going to go through this code step by
- 7:29step to make sure that you understand
- 7:32everything here.
- 7:35First of all, what is React? React is an
- 7:39external library that helps us create
- 7:41websites easier. Now, there are two
- 7:44parts to this statement. Number one,
- 7:46React is an external library. And number
- 7:50two, React helps us create websites
- 7:52easier. Let's go through these one by
- 7:55one. So number one, React is an external
- 7:58library. First, let's review what an
- 8:02external library is. So here we have a
- 8:05bunch of HTML code. An HTML code creates
- 8:09a website. And now, how do we load
- 8:12JavaScript onto our website? There are
- 8:15several ways to load JavaScript on a
- 8:17website. One way is to use an HTML
- 8:20element called a script element.
- 8:23For example, under this div element,
- 8:26let's create a script element using less
- 8:29than script greater than and less
- 8:33than/cript greater than. But our code
- 8:35editor might autocomplete this for us.
- 8:38Now, inside this script element, we can
- 8:41write some JavaScript code. For example,
- 8:44console.log
- 8:46the string hello.
- 8:49As a review, this code displays the text
- 8:52hello in the console. So now let's save
- 8:58and we're going to open the console in
- 8:59our browser. So we'll open Google Chrome
- 9:04and Liveer is going to automatically
- 9:06refresh the page and run that JavaScript
- 9:08code. And now we're going to check the
- 9:10console by right-clicking and then click
- 9:13inspect
- 9:15and open the console tab.
- 9:18And you'll see that inside the console
- 9:20we displayed the text hello. And that's
- 9:24exactly what this code does. So we
- 9:26successfully loaded this code onto our
- 9:29website.
- 9:31We'll go back to VS Code. So this is one
- 9:35way to run JavaScript code on a website.
- 9:38First, we create a script element and
- 9:40then we put our JavaScript code inside.
- 9:44Another way to run JavaScript is to save
- 9:46our JavaScript code in a file. So on the
- 9:49left, let's create a new file
- 9:53and we'll name it React-basics.js
- 9:57and press enter.
- 10:00So when we end a file with js, this file
- 10:03contains JavaScript code. So now let's
- 10:05move that code into this file. So we'll
- 10:08go to our HTML file. We're going to
- 10:11select this code and then rightclick
- 10:15cut. We can also press controlX on
- 10:18Windows or command X on Mac.
- 10:23And inside our JavaScript file, we're
- 10:26going to rightclick and paste
- 10:30and save this file.
- 10:33Now to run the code inside this file,
- 10:36we're going to go back to our HTML
- 10:40and we're going to add an HTML attribute
- 10:42to this script element. So after this
- 10:45script, we're going to type space and
- 10:48then type src equals double quotes. So
- 10:53the src attribute lets us load
- 10:56JavaScript from a file.
- 10:58Inside these double quotes, we're going
- 11:00to tell the computer which JavaScript
- 11:02file to load. So we'll type
- 11:05react-basics.js.
- 11:10So this will look for a file called
- 11:12react-basics.js
- 11:14beside the current HTML file. So it's
- 11:18going to find the file here, and it's
- 11:20going to run all the code inside this
- 11:22file. So now let's save
- 11:25and open our browser and check the
- 11:27result.
- 11:29Live server will automatically refresh
- 11:32the page. And inside the console you can
- 11:34see that we display the message hello.
- 11:37So we're successfully loading code from
- 11:40inside a file.
- 11:43We'll go back to VS Code.
- 11:46And this is a second way to load
- 11:48JavaScript. We save our JavaScript in a
- 11:50file and then we load it from the file.
- 11:55Next, the script element can actually
- 11:57load code from anywhere on the internet,
- 12:01not just on our computer. For example,
- 12:04we're going to open Google Chrome. And
- 12:06then we're going to create a new tab.
- 12:09And at the top we're going to type
- 12:12unpkg.com/sups
- 12:17simpledev/external
- 12:22dash library.js
- 12:26and press enter.
- 12:30So on this website we have some
- 12:32JavaScript code. So this is just normal
- 12:35JavaScript code. it displays a message.
- 12:38This is an external library inside the
- 12:40console. So using the script element, we
- 12:44can actually take this code from this
- 12:46website and run it on our own website.
- 12:50To do that, we're going to open VS Code
- 12:54and inside the src attribute, instead of
- 12:58loading the JavaScript code from a file,
- 13:00we're going to load the JavaScript code
- 13:02from a website. So, first we need the
- 13:05URL of the website. So, we'll go back to
- 13:08Google Chrome and at the top this is a
- 13:11URL and we're going to copy
- 13:15and VS Code we're going to remove this
- 13:19file and we're going to paste the URL.
- 13:24And now let's save.
- 13:27So now let's go back to our website and
- 13:30we're going to go to the tab for our
- 13:32website.
- 13:33And we're going to check the console.
- 13:36And inside the console, you can see we
- 13:38displayed the message, this is an
- 13:40external library.
- 13:42So we successfully loaded JavaScript
- 13:44code from another website.
- 13:47Let's go back to VS Code.
- 13:50So using the script element, we can load
- 13:53JavaScript code from anywhere on the
- 13:55internet. So this code is called an
- 13:59external library. An external library is
- 14:03just code that is outside of our
- 14:05computer. It's code that someone else
- 14:08wrote and we can load that code into our
- 14:10website using the script element.
- 14:15Now, React is an external library. So,
- 14:19React is just a bunch of code that is
- 14:21outside our computer that someone else
- 14:23wrote and we can load this code on our
- 14:26website and use it.
- 14:29So inside our starting file at the
- 14:32bottom here, these two script elements
- 14:35load the React external library. So
- 14:38loads React.js
- 14:40and React-dom.js.
- 14:44So let's open these URLs in our browser
- 14:46and take a look. So we're going to
- 14:49select this URL and we're going to copy.
- 14:54And inside Google Chrome, we're going to
- 14:56create a new tab. And at the top, we're
- 14:59going to paste the URL and press enter.
- 15:04And on this website, you can see that we
- 15:06have a bunch of JavaScript code. So,
- 15:09this is the code for React. We're just
- 15:13taking all of this code and then loading
- 15:15it onto our website.
- 15:18Let's go back to VS Code. And the same
- 15:21thing applies to this second URL. If we
- 15:25select it and then copy
- 15:29and in our browser create a new tab and
- 15:33then at the top paste
- 15:36and press enter.
- 15:39You can see that this website also has a
- 15:41bunch of JavaScript code
- 15:44and this is also the code for React. So,
- 15:48as you can see, React is an external
- 15:51library. It's just a bunch of code that
- 15:54is outside of our computer that someone
- 15:56else wrote. And we can load this code on
- 15:59our website and use it.
- 16:03Now, before we move on, there's two
- 16:05things you might be wondering about.
- 16:08Number one, why are we loading React
- 16:10from Super Simple Dev instead of some
- 16:13official React URL? So, this just lets
- 16:17me control the version of React. If we
- 16:20loaded React from the official URL, you
- 16:22might get a different version of React
- 16:24than the one I'm using in this video,
- 16:26which might cause issues. So, for the
- 16:29video, we're going to use a super simple
- 16:32dev URL to make sure that we have the
- 16:34same version of React and you're able to
- 16:37follow along.
- 16:39The second thing you might be wondering
- 16:41is why are there two external libraries
- 16:44for React React and React DOM. So React
- 16:49is designed to be used in different
- 16:50places like websites and also mobile
- 16:53apps. So the creators of React decided
- 16:56to divide it into two external
- 16:59libraries. React which contains all the
- 17:02shared features that are used in both
- 17:04websites and mobile apps and React DOM
- 17:08which contain features that are specific
- 17:10to websites. So if we're using React to
- 17:13create websites, we need to load the
- 17:16React external library and the React DOM
- 17:20external library. If we were using React
- 17:23to create mobile apps, we would load the
- 17:25React external library and a different
- 17:28library called React Native.
- 17:32In this course, we're going to use React
- 17:34to create websites. So, we're going to
- 17:36load React and React DOM.
- 17:41Okay, so we learned that React is an
- 17:44external library. It's just a bunch of
- 17:46code that is outside of our computer and
- 17:48we're going to load it onto our website.
- 17:51The next part is React helps us create
- 17:54websites easier. So now let's learn how
- 17:57to use React.
- 18:00We'll go back to VS Code
- 18:04and we're going to scroll down and these
- 18:07two lines of JavaScript at the bottom
- 18:10sets up React. So let's go through this
- 18:13step by step. In the first line, we're
- 18:16using document.query query selector to
- 18:19get an HTML element from the website and
- 18:22put it into our JavaScript. As a review,
- 18:26this is a feature of JavaScript called
- 18:27the DOM or the document object model.
- 18:31Basically, JavaScript gives us this
- 18:33document object which sort of combines
- 18:36JavaScript and HTML together. It lets us
- 18:39get HTML elements from the page and put
- 18:42them into our JavaScript and it also
- 18:45lets our JavaScript modify HTML elements
- 18:48on the page. So the DOM or the document
- 18:52object sort of combines HTML and
- 18:55JavaScript together and it lets the two
- 18:57languages interact with each other.
- 19:00Don't worry, you don't need to know a
- 19:02lot about the DOM to learn React. I'm
- 19:05just explaining what this code does so
- 19:07you understand what's going on here.
- 19:10So the code document.query selector lets
- 19:14us get an HTML element from the page and
- 19:17put it into our JavaScript.
- 19:20Inside these brackets, we tell the
- 19:22computer which HTML element we want to
- 19:25get from the page. So if we start with a
- 19:28dot, this means we're going to look for
- 19:29a class. And here we're looking for the
- 19:32class js-container.
- 19:35Up here, this div element has the class
- 19:39js-container.
- 19:40So, it's going to take this element and
- 19:42put it into our JavaScript. And finally,
- 19:45we're going to save it inside a variable
- 19:48so we can use it later.
- 19:51Next, let's go to the second line of
- 19:53code and react.create
- 19:57routt basically sets up React
- 20:01To set up React, we have to give it an
- 20:04HTML element. So, we're going to give it
- 20:06the element that we saved up here, which
- 20:08is this div element. So, basically the
- 20:11way this works is that this element is
- 20:14going to act as a container.
- 20:17Everything that we create with React is
- 20:19going to be displayed inside this
- 20:21element or inside this container. The
- 20:25reason React uses a container is because
- 20:28it keeps things organized.
- 20:30Everything inside this element will be
- 20:32controlled by React and React will not
- 20:35affect anything outside of this element.
- 20:38So it keeps things isolated and
- 20:40organized.
- 20:43So after we set up React with a
- 20:45container, we can start using the
- 20:48features of React.
- 20:52One feature of React is render. Render
- 20:56means to display something. So render
- 21:00lets us display something inside this
- 21:02container or inside this element. Now
- 21:06between the brackets of render, we're
- 21:09going to tell React what to display
- 21:11inside the container. So right now we
- 21:14told it to display the text welcome to
- 21:17super simple dev react course. And if we
- 21:21go back to our website and open the tab
- 21:24for our website
- 21:26and after we open the console, this time
- 21:28we're going to go to the elements tab.
- 21:32So this lets us see the HTML that's
- 21:34currently on the page. We're going to
- 21:36open this body element and inside we can
- 21:40see our container and you can see that
- 21:42React rendered or displayed the message
- 21:46welcome to super simple dev react course
- 21:49inside our container. So we'll go back
- 21:52to our code
- 21:54and that's how we set up and use React.
- 21:57First we give it a container and then we
- 22:00can display things like a message inside
- 22:03this container.
- 22:06Before we continue, let's review the
- 22:09rest of the code in this file to make
- 22:11sure that we understand every line of
- 22:13code. So, the HTML at the top here
- 22:18as well as the bottom here.
- 22:25This HTML is called the HTML structure.
- 22:30As a review, this is a standard
- 22:32structure that we follow when writing
- 22:34HTML.
- 22:36At the top, dock type HTML tells a
- 22:39computer to use a modern version of
- 22:42HTML.
- 22:44The HTML element represents a website.
- 22:47So all the code for our website should
- 22:50go inside the HTML element. Now every
- 22:53website is split into two sections. the
- 22:56head section and the body section. The
- 23:00head section generally contains things
- 23:02that are not visible on the page. For
- 23:05example, the title element sets the text
- 23:08at the top of the tab. So, this is not
- 23:11visible on the page. So, we put this
- 23:13inside the head.
- 23:16The body section generally contains
- 23:18things that are visible on the page. For
- 23:21example, the div element will be visible
- 23:24on the page. So we put it inside the
- 23:27body.
- 23:29Finally, we usually put our script
- 23:31elements at the bottom of the body
- 23:33because we want our HTML to be created
- 23:36first and then our JavaScript can modify
- 23:39the HTML.
- 23:41So that's a review of the HTML
- 23:43structure. This is a standard structure
- 23:46that we follow when writing HTML.
- 23:51So now we understand almost every line
- 23:54of code in this file. This is just the
- 23:57HTML structure that we have to follow.
- 24:00This is a container that we give to
- 24:02React to keep things isolated and
- 24:05organized.
- 24:06This is an example of an external
- 24:08library and this loads the React
- 24:12external library or loads the code for
- 24:14React.
- 24:17And this sets up and uses React.
- 24:21The last two lines that we haven't
- 24:23learned yet are these two lines right
- 24:26here. So here we're loading another
- 24:29external library. And this external
- 24:32library is called Babel.
- 24:34So again if I open this URL in my
- 24:37browser copy
- 24:41and in my browser going to create a new
- 24:44tab and at the top paste and press
- 24:48enter.
- 24:50You'll see that this website contains a
- 24:52bunch of JavaScript code and this is the
- 24:55code for another external library called
- 24:58Babel.
- 25:00So we'll go back to VS Code and let's
- 25:03learn what is the Babel external
- 25:05library.
- 25:07Babel is known as a JavaScript compiler.
- 25:10So it basically translates other
- 25:12languages into JavaScript.
- 25:17So why do we need Babel? When using
- 25:20React, we actually don't use normal
- 25:22JavaScript. We use an enhanced version
- 25:26of JavaScript called JSX.
- 25:29JSX stands for JavaScript XML. It's the
- 25:33same as JavaScript, but we can also
- 25:35write HTML directly in our JavaScript
- 25:38code. So, it's just JavaScript with
- 25:41extra features. So, let's do an example
- 25:44of JSX to learn how to use it. Inside
- 25:48the script element at the bottom, we're
- 25:51going to create a new variable using
- 25:54const and let's call it button
- 25:58equals. And now let's create a button
- 26:01HTML element. So here we're going to
- 26:05type HTML directly less than button
- 26:09greater than and then less than /button
- 26:12greater than.
- 26:14So this is actually HTML code and we're
- 26:18writing it directly inside our
- 26:20JavaScript. So this HTML code creates a
- 26:24button and then inside our button we can
- 26:27put the text hello.
- 26:30So this code here is actually using JSX.
- 26:34So JSX works the same way as JavaScript
- 26:37except we can also write HTML directly
- 26:40in our JavaScript code.
- 26:43So, what does this code do? It's
- 26:46actually pretty straightforward. This
- 26:48code creates a new button element inside
- 26:52our JavaScript and then we save it in a
- 26:55variable.
- 26:57We can actually do this with normal
- 26:59JavaScript as well. We can use the DOM
- 27:02and we can create a button element in
- 27:04our JavaScript like this. So you can
- 27:08think of this JSX code as a shortcut for
- 27:11this JavaScript code.
- 27:14So that's JSX. JSX is an enhanced
- 27:18version of JavaScript. So it's the same
- 27:20as JavaScript, but we can also write
- 27:23HTML directly in our JavaScript code. So
- 27:27when using React, we usually use JSX
- 27:30instead of normal JavaScript because, as
- 27:33you can see, JSX is simpler.
- 27:38Now there is one problem with JSX which
- 27:41is our web browser doesn't understand
- 27:44JSX. It only understands normal
- 27:47JavaScript.
- 27:49In order to use JSX we need to translate
- 27:52it into normal JavaScript first and then
- 27:56our web browser can understand this
- 27:58code.
- 28:00To do that we use the Babel external
- 28:03library. Babel translates JSX into
- 28:07normal JavaScript so our web browser can
- 28:10understand it. To use Babel, we need to
- 28:13do two steps. First, we load the Babel
- 28:17external library, which is what we do
- 28:19here. And then when we want to translate
- 28:23something into JavaScript, we have to
- 28:25give the script element an attribute
- 28:28type equals text/babel.
- 28:31So this attribute tells Babel to
- 28:34translate all the code inside here into
- 28:37normal JavaScript.
- 28:39So that's what these two lines of code
- 28:41do. First, we load the Babel external
- 28:44library and then we tell Babel to
- 28:47translate the code from JSX into normal
- 28:50JavaScript so our web browser can
- 28:52understand it. So now we understand all
- 28:56of the code in our starting file. And
- 28:59again, this is the minimum amount of
- 29:02code that we need to set up React.
- 29:06Next, let's learn some more features of
- 29:08React. So, we'll scroll back down. And
- 29:12currently, we're using React to display
- 29:15some text inside this container. Now, in
- 29:19addition to text, we can also display
- 29:21HTML elements inside this container. So
- 29:25to do that, we're going to remove this
- 29:27string
- 29:29and instead we're going to give render
- 29:31an HTML element.
- 29:34So up here, we actually created an HTML
- 29:37element or a button. So we're going to
- 29:39give this to the render function. So
- 29:42here we're going to give it the button.
- 29:46And now it's going to display this
- 29:47button inside our container or inside
- 29:50this div here. Let's save and we'll open
- 29:55the browser
- 29:57and open the tab for our website. And
- 30:00you'll see it displayed the button with
- 30:02the text hello on the website.
- 30:05And if we check the elements tab, you
- 30:08can see that React displayed the button
- 30:10inside our container.
- 30:13We'll go back to our code.
- 30:15So that's what this code does. It
- 30:18displays an element inside this
- 30:20container.
- 30:23Now let's do another example. Let's
- 30:25display a different HTML element inside
- 30:28this container. So below this button,
- 30:32let's use JSX to create a paragraph
- 30:35element. So first we'll create a new
- 30:37variable using const and we'll name the
- 30:40variable paragraph
- 30:43equals and we'll create a paragraph
- 30:46element using the code less than p
- 30:50greater than less than /p greater than.
- 30:54So this html code creates a paragraph or
- 30:58a paragraph of text. Inside our
- 31:01paragraph, let's just put paragraph of
- 31:05text.
- 31:06And now let's display this paragraph on
- 31:09our website. So inside render, instead
- 31:12of the button, we're going to render the
- 31:15paragraph
- 31:17and save.
- 31:20If we open our website,
- 31:22you can see it displayed a paragraph of
- 31:25text. If we go into the elements tab and
- 31:28we open this div container,
- 31:31we can see that React displayed this
- 31:33paragraph element inside this container.
- 31:37So let's go back to our code. And that's
- 31:40basically how we use React. We give
- 31:43React a container and then we can
- 31:45display an HTML element inside this
- 31:48container.
- 31:51Next, we're currently using React to
- 31:53display one element at a time on our
- 31:56website, which is not that useful. So
- 31:58now we're going to learn how to use
- 32:00React to display multiple elements on
- 32:02our website. So one thing we can't do is
- 32:06give render multiple elements. So this
- 32:09is not supported. We also can't save
- 32:12multiple elements inside a variable.
- 32:15This is also not supported. Instead, we
- 32:18need to use another HTML element called
- 32:21a div element. So, under our paragraph,
- 32:24let's create a new variable using const
- 32:28and let's name it div and make it equal.
- 32:32And we'll create an HTML div element
- 32:35using the code less than div greater
- 32:38than less than /d greater than.
- 32:42So this is the HTML code to create a div
- 32:46element. As a review, the div element
- 32:49represents a container. The purpose of a
- 32:52div element is to contain other elements
- 32:55like buttons and paragraphs. So that's
- 32:58actually why we used a div element up
- 33:01here. This element is a container. It's
- 33:04going to contain everything that we
- 33:05create using React. So let's go back
- 33:08down.
- 33:10Inside our div element, we can put other
- 33:13HTML elements like a button. So, let's
- 33:17actually select this code and we'll copy
- 33:22and then inside this div, paste.
- 33:27And let's also put a paragraph element
- 33:29inside our div. So we'll select this
- 33:32code for the paragraph element and we'll
- 33:35copy
- 33:37and our div paste.
- 33:42So this code works because a div is
- 33:45considered as one element. So we're just
- 33:47taking one element and saving it in one
- 33:50variable which is supported in
- 33:52JavaScript. The only thing special about
- 33:54this is that this one element can
- 33:57contain other elements inside.
- 34:00Next, we're going to give this div to
- 34:02the render function here. So, instead of
- 34:05the paragraph, we're going to give
- 34:07render our div and save.
- 34:12And if we check our website,
- 34:15you can see that React displayed
- 34:17multiple elements on the website, a
- 34:20button and a paragraph. If we look at
- 34:23the elements tab and open this div
- 34:25container,
- 34:28you can see that React displayed our div
- 34:30inside this container.
- 34:33Let's go back to our code. And that's
- 34:36how we display multiple elements on a
- 34:38website using React.
- 34:41First, we group the elements inside a
- 34:43div element. And then we render the div
- 34:46element.
- 34:49Before we continue, we're going to do
- 34:51some code cleanup. When we have multiple
- 34:54lines of HTML like this, a best practice
- 34:57is to add brackets around it. So, at the
- 35:00front, we're going to add an open
- 35:02bracket and at the end, a closed
- 35:05bracket.
- 35:06And we're going to put the HTML on its
- 35:08own line. So, here we have JavaScript
- 35:11and HTML together. We're going to add a
- 35:14new line here by pressing enter. and
- 35:17we're going to put the HTML on its own
- 35:19line. Same thing here. Click here and
- 35:22press enter to put the HTML on its own
- 35:26line.
- 35:28Next, we have two elements that are
- 35:30inside a div. So, we're also going to
- 35:32give this line an indent by pressing
- 35:35tab. And then, we're going to give this
- 35:37line an indent by pressing tab. So that
- 35:41it's easy to see that these two elements
- 35:43are inside this div element.
- 35:46So this is how we should format our code
- 35:48when we have multiple lines of HTML.
- 35:52Another way to think about this is
- 35:54inside here, you're actually just
- 35:56writing HTML normally.
- 35:59So don't worry, the more that you use
- 36:02React, the more you'll get used to this
- 36:04format.
- 36:05So let's save this file.
- 36:09Now that we learn the basics of React,
- 36:11let's talk about how React helps us
- 36:14create websites easier.
- 36:17If we look at this code, creating a
- 36:19website with React feels natural. It's a
- 36:23lot simpler than using the DOM feature
- 36:25in JavaScript. We can just create the
- 36:28HTML elements we want directly and then
- 36:31give it to React and React will display
- 36:34it on the website.
- 36:36Another benefit is JSX lets us find
- 36:40errors easier.
- 36:42For example, if I misspell button, it
- 36:45will actually tell me that these two
- 36:47don't match because this is an actual
- 36:51element rather than just text or a
- 36:53string. So, it makes it easier to find
- 36:56errors when we're coding. So, I'll add
- 36:59the O back.
- 37:02Another benefit is we can insert values
- 37:05inside our JSX elements. For example,
- 37:09inside this paragraph, we're going to
- 37:11type space and we're going to insert a
- 37:13value here by typing open curly bracket
- 37:16close curly bracket. And inside here, we
- 37:20can give it any JavaScript code. For
- 37:22example, 2 + 2. So this will calculate 2
- 37:27+ 2 and then insert it into this
- 37:30element. If we save
- 37:34and then check our website,
- 37:36we can see that it calculated 2 + 2
- 37:39which is four and inserted it into this
- 37:42paragraph element. So we'll go back to
- 37:45our code and that's another benefit of
- 37:48using React. We can insert JavaScript
- 37:50code directly into our JSX elements. So
- 37:54this is similar to string interpolation
- 37:57where we can insert values into a string
- 38:01except here we're inserting values into
- 38:04actual HTML elements. So those are three
- 38:07ways that React helps us create websites
- 38:10easier. Number one, it's more natural.
- 38:13We can create the elements that we want
- 38:15and then just give it to React and it
- 38:17will put it on the website. Number two,
- 38:20it helps us find errors easier because
- 38:22these are actual elements. And number
- 38:25three, we can insert our JavaScript code
- 38:27into our elements. So this helps us use
- 38:30JavaScript and HTML together more
- 38:33easily.
- 38:35Now, React has many other features that
- 38:37helps us create websites easier, but
- 38:40we're going to learn those features
- 38:41later on as we go through the course.
- 38:44The last thing we're going to do is
- 38:46we're going to scroll up and we're just
- 38:49going to remove this example external
- 38:51library
- 38:54and save
- 38:56so that we just have the code that we
- 38:58need for React.
- 39:02And that's the end of this lesson. In
- 39:04this lesson, we learned that React is an
- 39:07external library that helps us create
- 39:09websites easier. We learned how to load
- 39:12the React external library. We learned
- 39:15how to set up React and use render. We
- 39:19reviewed the basics of HTML and
- 39:22JavaScript.
- 39:23We learned JSX, an enhanced version of
- 39:27JavaScript.
- 39:28We created elements directly with JSX.
- 39:32And we learned how to insert values into
- 39:35our JSX elements. Here are some
- 39:38exercises to help you practice what we
- 39:40learned in this lesson. You can find the
- 39:43solutions to these exercises in the
- 39:45video description.
- 40:20In this lesson, we're going to learn two
- 40:22of the most important features of React,
- 40:25which are components and props. To learn
- 40:29this, I actually prepared a project that
- 40:31we can do for this lesson. We're going
- 40:34to go to our browser
- 40:36and we can delete these extra tabs for
- 40:39now.
- 40:42And we're going to create a new tab.
- 40:45And at the top, we're going to go to
- 40:48super simple.dev/projects/
- 40:54chatbot and press enter.
- 40:58This is a chatbot project. We can send a
- 41:02message to a robot up here and the robot
- 41:05will respond to our message. For
- 41:08example, we can click in here and type
- 41:11hello chatbot and then press send.
- 41:18And the robot will respond with hello,
- 41:20how can I help you? Now, if we scroll to
- 41:24the bottom of this page, we can also
- 41:26move the text box to the bottom by
- 41:28clicking this link.
- 41:31So, a lot of chatbot applications use
- 41:33this design where the text box is at the
- 41:36bottom. However, because we're watching
- 41:39a video, this text box might get cut off
- 41:42or it might be covered by the video
- 41:44progress bar. So, just for this video,
- 41:47we're going to click this again and
- 41:50we're going to start with a text box at
- 41:52the top.
- 41:53Next, this is a simple chatbot and
- 41:57currently it can only do three things.
- 41:59get today's date, flip a coin, and roll
- 42:02a dice. For example, up here, we can
- 42:05type, "Can you get me today's date?"
- 42:12and press send.
- 42:16And it will respond with today's date.
- 42:19And let's try typing, "How about flip a
- 42:25coin?" and press send.
- 42:30and it will respond with heads or tails.
- 42:34Finally, let's type okay, thank you and
- 42:39press send.
- 42:42And it responds with no problem.
- 42:46So, this is a chatbot project we're
- 42:49going to work on for the next few
- 42:50lessons. Now, this is a simple chatbot
- 42:52and it can only do these three things.
- 42:55But in the future, we can also switch
- 42:57this to a more powerful AI that can
- 42:59respond to any message we send.
- 43:03But for now, we'll just use this simple
- 43:05chatbot to learn React. Now, let's use
- 43:09React to build this chatbot project.
- 43:12We'll go back to VS Code
- 43:15and let's make a copy of React Basics.
- 43:19So, first we'll select this file and
- 43:21then rightclick,
- 43:24copy.
- 43:26And inside here, we're going to
- 43:28rightclick and paste.
- 43:32And let's rename this file. So, we'll
- 43:34rightclick and rename
- 43:38and rename it to chatbot.html
- 43:42and press enter.
- 43:46And let's close the React Basics files
- 43:48for now.
- 43:51So, we're going to start with the same
- 43:52code as the React Basics file, except
- 43:56we're just going to change the title up
- 43:58here. So, we'll remove React Basics and
- 44:01just change it to chatbot
- 44:05and save.
- 44:07And now, let's open this file in our
- 44:09browser. We'll rightclick,
- 44:12open with a live server.
- 44:16And it should say chatbot in the tab up
- 44:19here. So now we can close the React
- 44:22basics tabs which are these two here.
- 44:27And we'll go back to VS Code.
- 44:30And now we're ready to begin the chatbot
- 44:32project.
- 44:35First we're going to learn one of the
- 44:37most important features of React called
- 44:40components. A component is just a piece
- 44:44of the website.
- 44:47For example, let's go back to our
- 44:49browser and open the final chatbot
- 44:52project.
- 44:54So looking at this project, we can split
- 44:57it up into different pieces. One piece
- 45:00can be the text box and the button at
- 45:02the top and another piece are each of
- 45:05these chat messages.
- 45:07So these are called components. A
- 45:10component is just a piece of the
- 45:12website. Now, when building websites,
- 45:15it's better to split up the website into
- 45:18pieces or components so that we can work
- 45:21on a small piece of the website at a
- 45:23time.
- 45:26Now that we know what components are,
- 45:29let's create our first component in
- 45:31React. So, we mentioned that one way we
- 45:34can split up this website is to have one
- 45:37component at the top for this text box
- 45:40and the button. So, let's create a
- 45:42component for this in React. We'll go
- 45:45back to our code
- 45:48and we'll scroll down and first let's
- 45:51remove the example button and paragraph
- 45:54that we used. So, we'll select this and
- 45:57remove it.
- 46:00And to create a component in React,
- 46:02we're going to use a function. So we'll
- 46:05type function. And now we're going to
- 46:08name this component. Since this piece of
- 46:11the website is for inputting a message
- 46:13and sending it to the chatbot, let's
- 46:16name this component capital C chat
- 46:19capital I input brackets and curly
- 46:23brackets.
- 46:25Now in React the component name must
- 46:28start with a capital letter. So make
- 46:30sure that chat input starts with a
- 46:32capital C. This is also known as Pascal
- 46:36case where each word in the name starts
- 46:38with a capital letter.
- 46:43Next this function will return some
- 46:45HTML. So we're going to type return and
- 46:50we're going to return the HTML for this
- 46:52piece of the website. So this piece of
- 46:55the website has a text box and a button.
- 46:58So let's type brackets and press enter
- 47:01between these brackets.
- 47:04And to create a text box, we're going to
- 47:06use the HTML element less than input
- 47:10greater than. And we also need to type
- 47:12less than /input greater than. Now you
- 47:17may notice that in normal HTML input
- 47:20doesn't need this closing tag. However,
- 47:24JSX is more strict than normal HTML. So,
- 47:28all elements need a closing tag,
- 47:30including input. Now, there's actually a
- 47:33shortcut we can use here. If there's
- 47:36nothing inside an element, we can
- 47:38actually remove the closing tag.
- 47:42And at the end of this opening tag, we
- 47:45can just type space and then slash. So,
- 47:48this code is a shortcut for this code.
- 47:52And this is called a self-closing
- 47:54element. So in JSX we have to either
- 47:57have a closing tag or use this
- 48:00self-closing element shortcut.
- 48:03Next, this piece of the website also has
- 48:05a button. So let's also create a button
- 48:08element using less than button greater
- 48:11than less than /button greater than. And
- 48:16inside this button we have the text
- 48:18send.
- 48:20And now we're going to get an error here
- 48:22because we're trying to return two
- 48:24values or two elements from this
- 48:26function. But we can only return one
- 48:29value from a function. To solve this
- 48:31problem, we can use a div element like
- 48:34down here to group these two elements
- 48:36together.
- 48:38So above this, let's create a div
- 48:40element.
- 48:43And at the bottom, less than /d greater
- 48:47than. And let's also add indents before
- 48:50these two lines. So we'll click here and
- 48:53press tab. And also click here and press
- 48:56tab.
- 48:57So this shows that these two elements
- 48:59are inside this div element. And now
- 49:02this code no longer has an error because
- 49:05we're returning one value from this
- 49:07function.
- 49:09Now that we created this component,
- 49:11let's learn how to display it on the
- 49:13website. If we scroll down, right now
- 49:18we're using React to display this div on
- 49:21the website. So all we have to do is to
- 49:24insert our component inside this div and
- 49:27it will be displayed on the website. So
- 49:30one way to do this is to use curly
- 49:32brackets to insert something like we did
- 49:35before. So here we'll type curly
- 49:38brackets. And now we're just going to
- 49:41run this function
- 49:44capital C chat input and brackets.
- 49:49So this will run the function and it
- 49:52will return this HTML from the function
- 49:55and then the curly brackets will insert
- 49:57the HTML into the div and then display
- 50:00it on the page.
- 50:02So, as you can see, we can put any
- 50:04JavaScript code between these curly
- 50:06brackets. As long as this code results
- 50:09in a value, we can insert it into this
- 50:12HTML.
- 50:14Let's remove the button and the
- 50:16paragraph that we used as an example
- 50:19and save.
- 50:22And let's check our website. We'll open
- 50:24the browser and go to our chatbot
- 50:27project. And now we created the elements
- 50:30for the top section of the chatbot
- 50:32project, the text box and the send
- 50:35button. So don't worry about how it
- 50:37looks for now. Later in the course,
- 50:40we're going to style this with CSS and
- 50:42make it look like the final project.
- 50:46Let's go back to our code.
- 50:50And next, React has a special syntax we
- 50:53can use to insert a component. Instead
- 50:57of using curly brackets and running the
- 50:59function, we can type this code less
- 51:03than and then the name of the component
- 51:06capital C chat input greater than and
- 51:11then less than slash chat input greater
- 51:16than.
- 51:17So this is called the component syntax.
- 51:21This does the same thing as the code
- 51:23above. It runs the chat input function
- 51:27and then inserts the result into the
- 51:30HTML.
- 51:31If we save
- 51:34and we check our project, you'll notice
- 51:36that that syntax does the same thing and
- 51:39it creates another text box and button.
- 51:43Let's go back to our code.
- 51:46And again, this code does the same thing
- 51:49as this code. Now this syntax has a big
- 51:53advantage.
- 51:55This actually looks like an HTML
- 51:58element. It has an opening tag and a
- 52:01closing tag. So you can think of this
- 52:03like we're creating our own HTML element
- 52:07called chat input.
- 52:10HTML provides us with some default
- 52:12elements like div, input and button. And
- 52:16now using React, we can actually create
- 52:19our own HTML element like chat input.
- 52:23Then we can use our own HTML elements to
- 52:26build the website instead of just the
- 52:29default HTML elements. And this is
- 52:32actually one of the main ideas of React.
- 52:35We can create our own HTML elements. So
- 52:39when using React, we should use this
- 52:42component syntax instead of running the
- 52:44component function directly because this
- 52:47syntax lets us create our own HTML
- 52:50element.
- 52:52And again, we can use the self-closing
- 52:55element shortcut like we did up here. If
- 52:58there is nothing inside this element, we
- 53:01can remove this closing tag.
- 53:04And inside the opening tag at the end,
- 53:07we can type space and slash.
- 53:11And now we can remove this example code
- 53:13above.
- 53:16And let's save and check our project.
- 53:21And that creates the top section of the
- 53:23chatbot project.
- 53:27So go back to VS Code.
- 53:30And we mentioned in the previous lesson
- 53:32that React helps us create websites
- 53:34easier. And this is another way that
- 53:37React helps us create websites easier.
- 53:40It lets us split our website into
- 53:42smaller pieces or components. And this
- 53:45lets us work on a small piece of the
- 53:47website at a time.
- 53:51Before we create the rest of the
- 53:53project, we're going to improve our code
- 53:55a little bit. First, notice that in this
- 53:59component, we're using a div to group
- 54:02these two elements together. However,
- 54:05this is also going to insert an extra
- 54:08div element into our website. So, if we
- 54:11go to our website
- 54:15and rightclick and click inspect,
- 54:19and we're going to go to the elements
- 54:21tab, and we're going to go to our
- 54:24container here, and we're going to
- 54:25expand it.
- 54:27So notice here that we have a bunch of
- 54:30extra divs on this website. So what if
- 54:33we don't want to insert these extra
- 54:35divs? What if we just want to insert the
- 54:38text box and the button on our website?
- 54:41Let's go back to our code and learn how
- 54:43to do that.
- 54:46React has another way to group elements
- 54:49together called a fragment. To use a
- 54:52fragment, we can remove the word div.
- 54:56and div from the closing tag and just
- 55:00have a less than greater than and a less
- 55:02than slashgreater than. So this is
- 55:05called a fragment and it groups the two
- 55:08elements together. So we can return it
- 55:10from the function but it won't insert an
- 55:13extra div. So, if we save this code and
- 55:17then go to our website
- 55:19and take a look at the HTML,
- 55:22you'll notice now we have one less extra
- 55:25div on the website.
- 55:27Let's go back
- 55:29and we can also use a fragment down
- 55:31here. If we don't want to insert this
- 55:34extra div, we can remove it and remove
- 55:38it at the bottom and just use a
- 55:40fragment. If we save and check the
- 55:44website,
- 55:47now we no longer have those extra divs.
- 55:49We have our container and we just
- 55:52inserted the text box and the button.
- 55:56So that's fragments. It allows us to
- 55:58group elements together without creating
- 56:01an extra div.
- 56:04Let's go back to our code.
- 56:07And since this variable no longer
- 56:10contains a div, let's rename it. So
- 56:13right now we're building a chatbot
- 56:15application or an app. So let's rename
- 56:18this variable to app instead. And here
- 56:22as well, we're going to render the app
- 56:25and save.
- 56:28So again in React, when returning
- 56:30multiple elements or saving multiple
- 56:33elements, we can use a fragment to group
- 56:36them together. And this makes our HTML a
- 56:39little bit cleaner because it doesn't
- 56:41create an extra div element.
- 56:45Next, another improvement we can make is
- 56:47to add a placeholder to the text box at
- 56:50the top. So the gray text inside the
- 56:53text box is called a placeholder. It
- 56:56tells us what to type inside the text
- 56:59box. And when we start typing, it will
- 57:02disappear.
- 57:03To create a placeholder, we give this
- 57:06input element an attribute. So at the
- 57:09end, we're going to type the attribute
- 57:12placeholder
- 57:14equals double quotes.
- 57:17And then inside these double quotes,
- 57:19we're going to put the text that we
- 57:21want, which is send a message to
- 57:26chatbot.
- 57:29If we save this code
- 57:31and look at our website,
- 57:34now we have a placeholder in the text
- 57:36box. And when we start typing in it, it
- 57:40will disappear. So the placeholder is
- 57:42working. Now there is one problem here,
- 57:45which is if we remove this text, you'll
- 57:48notice that this placeholder is too long
- 57:50and it's getting cut off. To fix this,
- 57:53let's make this text box wider. We'll go
- 57:56back to our code.
- 57:58And to make this input wider, we're
- 58:00going to give it another attribute. So
- 58:03at the end, we'll type another attribute
- 58:06called size equals double quotes. And
- 58:10this sets how many characters can fit in
- 58:12this input. By the way, a character is
- 58:16just a letter, number, or symbol.
- 58:19Let's set the size to 30 characters and
- 58:23save and take a look.
- 58:26Okay, so that looks pretty good for now.
- 58:29Again, don't worry about how this looks.
- 58:32We'll style it later in the course using
- 58:34CSS.
- 58:37So, we just created our first component
- 58:40or our first piece of the website, which
- 58:42is the section at the top. Now, let's
- 58:45get some practice and create more
- 58:47components.
- 58:49Let's go back to the final chatbot
- 58:51project
- 58:53and let's ask ourselves how can we split
- 58:56up the rest of this website into pieces.
- 59:00Another piece of this website could be
- 59:02one chat message here. So let's create a
- 59:05component for this piece of the website.
- 59:08We'll go back to VS Code
- 59:12and to create a component we're going to
- 59:14use a function. So down here, let's
- 59:18create a function
- 59:21and let's name it capital C chat capital
- 59:25M message
- 59:27brackets and curly brackets. So remember
- 59:31a component name must start with a
- 59:33capital letter and we use Pascal case.
- 59:36So each word starts with a capital
- 59:38letter.
- 59:40Now inside this function, we're going to
- 59:43return the HTML for this piece of the
- 59:46website or this component.
- 59:49So let's also type brackets and enter.
- 59:52So we can write the HTML on multiple
- 59:55lines. And now each chat message has two
- 59:59things, some text and a profile image.
- 1:00:03Since we have multiple things inside
- 1:00:05this component, let's group them
- 1:00:07together using a fragment. So we can
- 1:00:10return one value from this function. So
- 1:00:13we'll type less than greater than enter
- 1:00:16and then less than /g greater than to
- 1:00:19create a fragment. And now inside here
- 1:00:24each message has some text. So let's
- 1:00:26create the first chat message in our
- 1:00:29project. We'll go back to the final
- 1:00:31project.
- 1:00:33And let's just copy paste the text from
- 1:00:35the first message here which is hello
- 1:00:38chatbot. So we'll select this and then
- 1:00:41copy by pressing control C on Windows or
- 1:00:44command C on Mac.
- 1:00:47And then inside our code
- 1:00:50in the chat message component we're
- 1:00:52going to paste by pressing controlV on
- 1:00:55Windows or commandV on Mac.
- 1:00:59And let's fix the indent by pressing tab
- 1:01:01at the start.
- 1:01:03Next, each chat message has a profile
- 1:01:06image. To create an image, we can use an
- 1:01:10HTML element called img. So, let's
- 1:01:13create a new line. And we're going to
- 1:01:15type less than img greater than and then
- 1:01:19less than / img greater than. So,
- 1:01:23remember that in JSX, every element has
- 1:01:26to have a closing tag. And again, we can
- 1:01:30use the self-closing element shortcut
- 1:01:32like we did down here. If there's
- 1:01:34nothing inside this element, we can
- 1:01:37remove the closing tag and then just add
- 1:01:40a space and a slash at the end of the
- 1:01:43opening tag. And now we have the two
- 1:01:46things that we need for each chat
- 1:01:48message, the text and an image.
- 1:01:53Next, we need to tell the computer which
- 1:01:56image to load. Right now, if we look at
- 1:01:59our final chatbot project, we have two
- 1:02:02images that we use, but we currently
- 1:02:04don't have these in our project. So,
- 1:02:06let's download them. We're going to
- 1:02:09rightclick this first image and then
- 1:02:12click save image.
- 1:02:15And we're going to find the folder that
- 1:02:17contains all of our code. So, for me,
- 1:02:19it's on the desktop
- 1:02:22and it's in the folder React-Course.
- 1:02:25and we're going to save it inside of
- 1:02:27this folder.
- 1:02:30And same things for this robot image.
- 1:02:32We're going to rightclick
- 1:02:35and click save image.
- 1:02:38Go to the React-Course folder. And we're
- 1:02:41also going to save the robot image.
- 1:02:46And now if we look at our code,
- 1:02:48we should have two images inside our
- 1:02:51project. User.png png and robot.png.
- 1:02:57And now let's go back to our code.
- 1:03:00To load an image on the website, we need
- 1:03:03to tell the computer which image to
- 1:03:05load. To do that, we're going to give
- 1:03:08this image element an attribute called
- 1:03:11src or source equals double quotes. And
- 1:03:16inside these double quotes, we're going
- 1:03:18to tell the computer which image to load
- 1:03:21for the first chat message. The image is
- 1:03:24the user image. So let's type this file
- 1:03:27name. userpng.
- 1:03:32So this will look for a file called
- 1:03:34user.png
- 1:03:36beside the current HTML file. This is a
- 1:03:40current HTML file. So we'll look for
- 1:03:42user.png beside it, which is right here.
- 1:03:46and then it's going to load this image
- 1:03:48on the website.
- 1:03:50Okay, so we created a component for a
- 1:03:53chat message. Now let's insert this
- 1:03:56component on the website. We're going to
- 1:03:58scroll down
- 1:04:00to the app and let's insert this
- 1:04:03component using the component syntax
- 1:04:05like we did up here. So we'll type a new
- 1:04:08line and type less than and then the
- 1:04:11name of the component which is capital C
- 1:04:14chat
- 1:04:16capital M message.
- 1:04:19And currently we don't have anything to
- 1:04:21put inside this component. So we use the
- 1:04:24self-closing element shortcut like we
- 1:04:26did up here. So now let's save
- 1:04:31and check our website
- 1:04:33which is this tab. And let's close the
- 1:04:36console so we can see it. And we created
- 1:04:40a chat message.
- 1:04:43However, we have a few problems here.
- 1:04:45The first problem is that the profile
- 1:04:47image is too big. To make it smaller,
- 1:04:50we're going to go back to our code
- 1:04:53and inside the img element, we're going
- 1:04:56to give it another attribute called
- 1:04:59width equals double quotes.
- 1:05:03And let's try setting the width to 50
- 1:05:06and save. And then check our website.
- 1:05:10And that looks close enough.
- 1:05:14Another problem is the chat message is
- 1:05:17appearing on the same line as the chat
- 1:05:19input, but in the final project, we want
- 1:05:22it to appear below. To fix this, we'll
- 1:05:25go back to our code
- 1:05:28and inside the chat message component,
- 1:05:31instead of using a fragment to group
- 1:05:33these elements together, we're going to
- 1:05:35change it back to a div. So, inside
- 1:05:38we'll type div and inside here /d.
- 1:05:44The reason we're using a div is because
- 1:05:46in HTML, a div is known as a block
- 1:05:49element. A block element will always
- 1:05:53take up an entire line by itself. So if
- 1:05:56we group this in a div element, instead
- 1:05:59of appearing beside the chat input, the
- 1:06:02div is going to take up an entire line
- 1:06:04by itself. So it's going to take up the
- 1:06:06entire line under the chat input. So
- 1:06:10let's save this code and then check our
- 1:06:13website.
- 1:06:15So now this div is taking up this entire
- 1:06:18line. If we rightclick and click inspect
- 1:06:23and then click this arrow icon in the
- 1:06:26top left and then click on this div,
- 1:06:30you can see that this div now takes up
- 1:06:32this entire line. So now we have the
- 1:06:35layout that we want. So in React, we
- 1:06:39still use divs for layout purposes.
- 1:06:42If you want to learn divs, layout, and
- 1:06:45HTML in more detail, you can check out
- 1:06:48my HTML and CSS full course in the video
- 1:06:52description.
- 1:06:54So now we created our first chat
- 1:06:56message.
- 1:07:00Next, let's create the second chat
- 1:07:03message. We'll go back to our code
- 1:07:07and instead of creating a whole new
- 1:07:09component, we can actually reuse our
- 1:07:12chat message component. Components are
- 1:07:14actually designed to be reusable.
- 1:07:18To reuse this component, we don't want
- 1:07:21the message to be hello chatbot every
- 1:07:23time. We want to be able to display
- 1:07:26different messages in this component. To
- 1:07:29do that, we can give our component an
- 1:07:32attribute. So down here, we're going to
- 1:07:35go to the chat message component and
- 1:07:37we're going to give it an attribute. So
- 1:07:40this works just like HTML attributes.
- 1:07:43We're going to give our attribute a
- 1:07:45name. Let's call it message
- 1:07:49equals double quotes. And inside these
- 1:07:52double quotes, we're going to give the
- 1:07:54attribute a value.
- 1:07:57Let's put the text for the first message
- 1:07:59which is hello chatbot. So let's select
- 1:08:02this code and then copy
- 1:08:06and we're going to paste it inside here.
- 1:08:10So now we have an attribute called
- 1:08:12message with a value hello chatbot. So
- 1:08:15again you can see that this syntax looks
- 1:08:18like HTML. We can even add our own
- 1:08:21attributes.
- 1:08:23Now that we added this message
- 1:08:24attribute, how do we access it inside
- 1:08:27this component and use it? To do that,
- 1:08:31we're going to use a feature called
- 1:08:33props.
- 1:08:34Every component function gets one
- 1:08:37parameter up here called props.
- 1:08:42This props parameter is an object and it
- 1:08:46contains all the attributes that we give
- 1:08:49to this component. So up here, let's
- 1:08:52actually console.log
- 1:08:55props to see what it looks like. Let's
- 1:08:59save and then open our website and check
- 1:09:03the console.
- 1:09:06And you'll see that props is an object
- 1:09:08and it contains the attributes that we
- 1:09:10gave to our component. So here we have
- 1:09:13the message attribute and it has a value
- 1:09:15hello chatbot. So the attribute name is
- 1:09:18saved as a property and the attributes
- 1:09:20value is saved as a value of this
- 1:09:23object.
- 1:09:24So let's go back to VS code
- 1:09:27and using this props object we can
- 1:09:30access our attributes. By the way props
- 1:09:34is short for properties. We call this
- 1:09:37props because the attributes are saved
- 1:09:39as properties in this object.
- 1:09:42So now to access the message attribute
- 1:09:45we can remove this and type props
- 1:09:50dot and the attribute name which is
- 1:09:54message
- 1:09:57and let's save this in a variable so we
- 1:09:59can use it later. So at the front we'll
- 1:10:02create a variable using const name name
- 1:10:05it message
- 1:10:07equals props dossage
- 1:10:10and we'll add a new line to separate the
- 1:10:12code a little bit.
- 1:10:14Now instead of displaying hello chatbot
- 1:10:17every time let's display whatever is in
- 1:10:20the message attribute. So we'll remove
- 1:10:23this
- 1:10:24and let's insert this variable into this
- 1:10:27element. To do that we can use curly
- 1:10:30brackets and type the variable name
- 1:10:34message.
- 1:10:36So again we can put any JavaScript code
- 1:10:39between these curly brackets and as long
- 1:10:41as it results in a value it will insert
- 1:10:44it into this element. So now let's save
- 1:10:48and we'll check the website
- 1:10:51and it displays a message hello chatbot.
- 1:10:55That's the message attribute that we
- 1:10:57gave it.
- 1:11:00Let's go back to our code.
- 1:11:02So that's props. Just like a normal HTML
- 1:11:06element, we can give attributes to our
- 1:11:09component and then access these
- 1:11:11attributes using this props object.
- 1:11:15So props are really useful because props
- 1:11:18make our component reusable.
- 1:11:20For example, let's try creating the
- 1:11:23second chat message. To do that, we can
- 1:11:26just create another chat message
- 1:11:28component. So down here, let's type less
- 1:11:32than chat message/grater
- 1:11:37than.
- 1:11:39And we just need to give it a different
- 1:11:41message attribute. So here we'll give it
- 1:11:44an attribute message equals double
- 1:11:47quotes. And we'll copy paste the second
- 1:11:51message in the chatbot project. We'll go
- 1:11:53to our browser
- 1:11:55and go to the chatbot project and let's
- 1:11:58copy and paste this second message.
- 1:12:01We'll select it and then copy.
- 1:12:05And inside our code in the message
- 1:12:08attribute, we're going to paste.
- 1:12:12So this will save the message hello how
- 1:12:15can I help you inside props and then we
- 1:12:19get it out of props and insert it into
- 1:12:21the component. Let's save and check the
- 1:12:26website
- 1:12:28and go to our tab.
- 1:12:30And now we created the second chat
- 1:12:33message. So let's go back to our code.
- 1:12:37And you can see that props make our
- 1:12:39component reusable. We can reuse this
- 1:12:43component, but just give it a different
- 1:12:45attribute or prop and it will display a
- 1:12:48different message.
- 1:12:50Let's get some practice and use props to
- 1:12:53create two more chat messages. Feel free
- 1:12:56to pause if you want to try it yourself
- 1:12:58first.
- 1:12:59So down here, let's create our third
- 1:13:02message using less than chat
- 1:13:05message/grater
- 1:13:07than. and we'll give it a message
- 1:13:11attribute equals double quotes and we'll
- 1:13:14copy paste the third message. We'll go
- 1:13:16to the chatbot project
- 1:13:20and select this third message and then
- 1:13:23copy.
- 1:13:25And in our code inside the attribute,
- 1:13:28we're going to paste.
- 1:13:31And finally, let's create the fourth
- 1:13:33message less than chat message
- 1:13:37slashgreater than and then the message
- 1:13:40attribute equals double quotes and we'll
- 1:13:43copy paste the fourth message which is
- 1:13:46this one. So don't worry if your
- 1:13:48messages are different from mine. We're
- 1:13:50just doing an example. So we'll select
- 1:13:53this and copy.
- 1:13:56And inside here, we're going to paste
- 1:14:00and save.
- 1:14:03And now, if we check our website
- 1:14:06and go to our tab, you'll see that we
- 1:14:09now have four chat messages in our
- 1:14:12project. So, that's how we use props to
- 1:14:15reuse the chat message component.
- 1:14:19Next, in the final chatbot project,
- 1:14:22notice that these two messages are
- 1:14:24different. These two messages are from
- 1:14:27the chatbot. So there are two
- 1:14:30differences here. The profile image is a
- 1:14:33robot and the image is on the left. To
- 1:14:37create this, we can add another prop to
- 1:14:40chat message. Let's go back to VS Code
- 1:14:46and for each chat message, we're going
- 1:14:48to add another attribute.
- 1:14:51And let's call this attribute the sender
- 1:14:55equals double quotes.
- 1:14:58This attribute tells us who sent the
- 1:15:00message. And we can use this to
- 1:15:02determine which image to load and where
- 1:15:05to place the image. So if we send the
- 1:15:08message, let's make the sender the user.
- 1:15:13And for the second chat message, we're
- 1:15:16going to add another sender attribute
- 1:15:19equals double quotes. And the second
- 1:15:21message is from the chatbot. So let's
- 1:15:24make the sender attribute the robot.
- 1:15:28And we'll do the same thing for the
- 1:15:30third and fourth message. We'll give it
- 1:15:32a sender attribute.
- 1:15:35The third message is from the user. So
- 1:15:38let's make the sender user.
- 1:15:41And the fourth message sender equals
- 1:15:45double quotes. This message is from the
- 1:15:47robot. So let's make the sender robot.
- 1:15:53Next, let's access the sender attribute
- 1:15:56inside our component. To do that, all
- 1:15:59the attributes are saved inside this
- 1:16:02props object. To access the sender
- 1:16:05attribute, we can type props dot sender.
- 1:16:11So this gives us the value of the sender
- 1:16:13attribute which is either user or robot.
- 1:16:17So let's save this in a variable so we
- 1:16:19can use it later. Type const and let's
- 1:16:23call it sender equals props.
- 1:16:28So now that we have the sender
- 1:16:30attribute, let's use it to decide which
- 1:16:33profile image to load and where to place
- 1:16:35this image. To do that, we can just use
- 1:16:38an if statement.
- 1:16:41So inside here, we can type if brackets
- 1:16:45and curly brackets.
- 1:16:47And inside these brackets, we're going
- 1:16:49to check if the sender
- 1:16:53is equal to the string robot.
- 1:16:57By the way, by default, if the attribute
- 1:17:01just has double quotes like this, the
- 1:17:03value is a string. So here the value is
- 1:17:06going to be the string user or the
- 1:17:08string robot.
- 1:17:10So now inside our component, if the
- 1:17:12sender is the robot, we're going to
- 1:17:15return some different HTML.
- 1:17:17So let's type return brackets. So we can
- 1:17:21write this HTML on multiple lines. And
- 1:17:25let's just copy paste the HTML from down
- 1:17:27here and modify it. So we'll select this
- 1:17:31HTML and then copy
- 1:17:34and then inside this return we're going
- 1:17:37to paste.
- 1:17:40And let's also select these lines and
- 1:17:42press tab to fix the indenting.
- 1:17:46So the first thing we need to change if
- 1:17:48the sender is a robot is that we want to
- 1:17:51load the robot image instead.
- 1:17:54So, we already downloaded the robot
- 1:17:56image here. To load it, we're just going
- 1:17:59to change the src attribute or the
- 1:18:02source attribute of this image. So,
- 1:18:05we're going to change this from user to
- 1:18:07robot.png.
- 1:18:10And we also want this image to be on the
- 1:18:13left. So, let's just move this image
- 1:18:16before the message. So we'll select this
- 1:18:18code and then cut by pressing control X
- 1:18:22on Windows or command X on Mac.
- 1:18:26And then before the message we're going
- 1:18:28to paste by pressing controlV on Windows
- 1:18:31or commandV on Mac.
- 1:18:34And we can remove this extra new line.
- 1:18:37So as you can see inside a component we
- 1:18:40can use any JavaScript feature like if
- 1:18:43statement and we can return a different
- 1:18:46HTML and whatever we return will be
- 1:18:50inserted inside the element here. So now
- 1:18:54let's save this code
- 1:18:56and then check our website
- 1:18:59and then open our tab
- 1:19:02and you can see that now we have two
- 1:19:04messages from the chatbot. that loads
- 1:19:07the robot image and it puts the image on
- 1:19:09the left.
- 1:19:12We'll go back to our code
- 1:19:15and using this sender prop, we're able
- 1:19:18to reuse this component to create a chat
- 1:19:21message from the robot. So again, props
- 1:19:25make our component reusable.
- 1:19:28And that's it. If we scroll down,
- 1:19:31using components, we split up the
- 1:19:34website or the app into smaller pieces
- 1:19:37like the chat input section at the top
- 1:19:40and each chat message. And we created
- 1:19:43all the HTML that we need for this
- 1:19:45project.
- 1:19:48The last thing we're going to learn in
- 1:19:49this lesson are some shortcuts that are
- 1:19:51commonly used in React, and we can use
- 1:19:54these to clean up our code.
- 1:19:57First, let's scroll up to the chat
- 1:20:00message component.
- 1:20:02So, instead of doing con message equals
- 1:20:04props message, we actually have a
- 1:20:07shortcut for this in JavaScript. So,
- 1:20:10down here we can just type const curly
- 1:20:14brackets and inside we can type message
- 1:20:19and make it equal to props.
- 1:20:24So this takes the message property out
- 1:20:27of this object and saves it in a
- 1:20:29variable called message. So this does
- 1:20:32the same thing as this line above. This
- 1:20:36code is a shortcut for this code. And
- 1:20:39this shortcut is called dstructuring
- 1:20:42because we're taking stuff out of this
- 1:20:44object. We're deconstructing it. We can
- 1:20:48also use this shortcut with the sender
- 1:20:50property to add another property to take
- 1:20:54out of this object. After the first
- 1:20:56property, we can type comma and the
- 1:20:59second property sender.
- 1:21:03So this will get the message property
- 1:21:05and the sender property out of this
- 1:21:08object and it will save it in a message
- 1:21:10variable and a sender variable. So this
- 1:21:14line of code is a shortcut for these two
- 1:21:17lines above.
- 1:21:19So now let's actually turn these two
- 1:21:21lines into comments or comment out these
- 1:21:24lines. At the front we're going to type
- 1:21:28double slash and at the front here we'll
- 1:21:31type double slash.
- 1:21:34By the way, if we start with double
- 1:21:36slash, it will turn this code into
- 1:21:38comments and it will be ignored by the
- 1:21:40computer.
- 1:21:42So, we'll just keep this here as a
- 1:21:44reminder that this is a shortcut for
- 1:21:47these two.
- 1:21:49Next, JavaScript has an even shorter
- 1:21:52shortcut for this. Since props is a
- 1:21:55parameter up here, we can just
- 1:21:58dstructure it directly up here. So here
- 1:22:01instead of the props object we can just
- 1:22:04type curly brackets directly
- 1:22:07and then type message
- 1:22:11and sender.
- 1:22:14This takes the message and sender
- 1:22:16properties directly out of the first
- 1:22:18parameter and then saves them in a
- 1:22:20variable called message and a variable
- 1:22:23called sender. So it does the same thing
- 1:22:25as this code down here, but it's an even
- 1:22:28shorter shortcut. We're dstructuring the
- 1:22:31parameter directly at the top. So this
- 1:22:34shortcut is used a lot in React
- 1:22:36tutorials and documentation. So it's
- 1:22:39useful to know how this works.
- 1:22:43So let's comment out this line here at
- 1:22:46the front. We'll type double slash.
- 1:22:49So we remember that this code is a
- 1:22:52shortcut for this line of code and these
- 1:22:55two lines of code.
- 1:22:57Now, let's save and check our website to
- 1:23:01make sure that everything is still
- 1:23:02working. So, that looks good.
- 1:23:06We'll go back. And that is a
- 1:23:08dstructuring shortcut. It allows us to
- 1:23:11take properties directly out of an
- 1:23:13object.
- 1:23:16Next, we're going to learn how to use if
- 1:23:18statements directly inside JSX.
- 1:23:21So currently we have some JSX here and
- 1:23:25we put an if statement outside of this
- 1:23:28JSX.
- 1:23:29However, this means we're duplicating
- 1:23:31some code like the div and the message.
- 1:23:36To make this code more reusable, we can
- 1:23:39actually move this if statement directly
- 1:23:42inside the JSX down here. So what we
- 1:23:46want to do is if the sender is a robot,
- 1:23:50we want to put the image before the
- 1:23:52message. So down here, let's go before
- 1:23:55the message and press enter. And we're
- 1:23:58going to type curly brackets and we're
- 1:24:01going to insert the if statement
- 1:24:03directly inside here. So here before the
- 1:24:08message we want to do something like if
- 1:24:11the sender is equal to the string robot
- 1:24:16we're going to insert this image.
- 1:24:20However we can't actually use the word
- 1:24:23if inside of these curly brackets.
- 1:24:26Instead we need to use another feature
- 1:24:28of JavaScript called a guard operator or
- 1:24:32and.
- 1:24:34In JavaScript, you can write value one
- 1:24:37and value two. If value one is true,
- 1:24:40then the result will be value two. So
- 1:24:43this works just like an if statement
- 1:24:46except we can write it on one line and
- 1:24:48it results in a value. So if you want to
- 1:24:51learn more about this feature, I cover
- 1:24:53it in the booleans lesson of my
- 1:24:55JavaScript course and you can check out
- 1:24:58that in the video description.
- 1:25:00For now, the only thing we need to know
- 1:25:03is we can use and as sort of an if
- 1:25:06statement in our JSX.
- 1:25:08So, first let's remove this
- 1:25:12and then we're going to use and to
- 1:25:14insert this if statement directly inside
- 1:25:17the JSX.
- 1:25:19So, the first value is going to be our
- 1:25:21if condition, which is sender equals the
- 1:25:25robot. So we'll check if the sender is
- 1:25:29equal to the string robot
- 1:25:33and then we're going to use and.
- 1:25:36So if this is true, the result is going
- 1:25:39to be the second value here. So here if
- 1:25:43the sender is a robot, we want to insert
- 1:25:46the robot image. So let's just copy and
- 1:25:49paste this code. We're going to select
- 1:25:52this
- 1:25:53and then copy it.
- 1:25:56And then down here after and we're going
- 1:25:58to paste.
- 1:26:01So again the way this works is if the
- 1:26:04value on the left is true then the
- 1:26:07result will be the value on the right.
- 1:26:10So this and is sort of a shortcut for
- 1:26:13this if statement and it lets us insert
- 1:26:16if statements directly inside our JSX.
- 1:26:20So let's actually comment out this if
- 1:26:22statement out here. So we remember that
- 1:26:25this code is a shortcut for this code.
- 1:26:29So here before the if statement we'll
- 1:26:32type slash and star and after the if
- 1:26:35statement we'll type star and slash and
- 1:26:39remove the indent. So this is known as a
- 1:26:42multi-line comment. All the code between
- 1:26:45these two stars will be ignored by the
- 1:26:48computer. So now let's save
- 1:26:52and check our website.
- 1:26:55And as you can see, if the sender is a
- 1:26:58robot, we're going to add an image on
- 1:27:01the left before the message. So now, one
- 1:27:04problem here is that we're also showing
- 1:27:07the user's profile image at the back,
- 1:27:10but we only want to show one profile
- 1:27:12image. We'll go back to our code
- 1:27:16and here we only want to show the user
- 1:27:19profile image if the sender is the user.
- 1:27:23So let's insert another if statement
- 1:27:25down here. Feel free to pause this video
- 1:27:28if you want to try it yourself first.
- 1:27:32So at the front we'll type open curly
- 1:27:34bracket. At the end a close curly
- 1:27:37bracket.
- 1:27:38And at the front we're going to type the
- 1:27:41if condition. So we'll check if the
- 1:27:45sender is equal to the string user and
- 1:27:50then we'll type ant. So if this is true
- 1:27:54then we will show this image. If this is
- 1:27:57not true, we will not show this image.
- 1:28:00So this code is a shortcut for this
- 1:28:02code.
- 1:28:05Let's save and check our website.
- 1:28:10And it works. The robot image is on the
- 1:28:13left and the user image is on the right.
- 1:28:18So let's go back.
- 1:28:20Using the guard operator or and we can
- 1:28:24insert an if statement directly inside
- 1:28:26our JSX.
- 1:28:29Next, we're going to do some code
- 1:28:31cleanup. So, let's scroll up and we're
- 1:28:34going to go to the chat input component
- 1:28:37first. So, you notice that in here, this
- 1:28:40line is a little bit too long. To make
- 1:28:43our code easier to read, we can actually
- 1:28:46put each attribute on its own line. So,
- 1:28:49here we'll type enter to put placeholder
- 1:28:52on its own line. And here we'll type
- 1:28:55enter to put size on its own line. And
- 1:28:58let's also type enter at the back. So
- 1:29:01the attributes are on their own lines.
- 1:29:04And then let's add an indent in front of
- 1:29:06these attributes by pressing tab and
- 1:29:08pressing tab. This makes it clear that
- 1:29:11these attributes are part of this
- 1:29:13element. And we also want to make sure
- 1:29:16that these two are vertically aligned.
- 1:29:19This makes it easy to see where this
- 1:29:21element starts and where it ends.
- 1:29:24Next, let's scroll down to the chat
- 1:29:27message component. And notice that these
- 1:29:31two lines of code with the and are also
- 1:29:34a bit too long. To improve this, we can
- 1:29:37surround this value with brackets. So
- 1:29:40we'll type open bracket and at the end
- 1:29:43close bracket and then we can put this
- 1:29:46code on its own line. So here we'll type
- 1:29:49enter and at the end we'll type enter.
- 1:29:53So this will still have the same value,
- 1:29:55but the brackets allow us to put the
- 1:29:57code on multiple lines. We'll do the
- 1:30:00same thing for the second image.
- 1:30:03We'll surround it in brackets, open
- 1:30:05bracket, and close bracket. And we'll
- 1:30:08put this value on a separate line
- 1:30:11so that it's no longer too long.
- 1:30:15Next, let's scroll down even more to the
- 1:30:18app variable. And you'll notice that
- 1:30:21these lines are also a bit too long
- 1:30:24because of the attributes. Again, we can
- 1:30:26put each attribute on its own line. So
- 1:30:30here we'll put message on its own line
- 1:30:32and sender on its own line. And we'll
- 1:30:35put the end of the component on its own
- 1:30:37line so that this is vertically aligned
- 1:30:40with this. And let's also add indents
- 1:30:42before the attributes. So we'll press
- 1:30:44tab and tab. So it's easy to see that
- 1:30:48these attributes are part of this
- 1:30:50component. And we'll do the same thing
- 1:30:52for the other three chat messages. So
- 1:30:55we'll put this on its own line
- 1:30:58and add indents.
- 1:31:02And put this on its own line.
- 1:31:05Add indents.
- 1:31:08And put this on its own line.
- 1:31:12And add indents.
- 1:31:15and save
- 1:31:19and check our website and everything
- 1:31:21should still work because we just
- 1:31:23changed the formatting of our code. So
- 1:31:26let's go back and that's how we avoid
- 1:31:28our lines being too long. We can put
- 1:31:31each attribute on its own line
- 1:31:34and we can also surround a value with
- 1:31:36brackets and put it on its own line.
- 1:31:41Finally, down here, instead of using a
- 1:31:44variable to save our app, a best
- 1:31:47practice is to use a component to create
- 1:31:50this app.
- 1:31:52So here, let's create a component using
- 1:31:56function
- 1:31:58and we'll name it capital A app
- 1:32:02brackets and curly brackets.
- 1:32:05And then this component will return all
- 1:32:09of this HTML.
- 1:32:11So we'll return brackets so we can write
- 1:32:14it on multiple lines.
- 1:32:17And we're going to move all this code up
- 1:32:19here. So let's select this code. And
- 1:32:23we'll cut
- 1:32:26and inside the return
- 1:32:28we're going to paste.
- 1:32:32And we can select these lines
- 1:32:36and press tab to fix the indenting.
- 1:32:40So now we're going to use this component
- 1:32:42instead of the variable to create our
- 1:32:44app. So down here, let's remove this
- 1:32:48variable.
- 1:32:50And then inside render, we're going to
- 1:32:53create the app. We can type less than
- 1:32:57capital A app greater than and then less
- 1:33:01than slash app greater than.
- 1:33:05So this creates an entire chatbot
- 1:33:07application. And then we give this to
- 1:33:10React to display on the website.
- 1:33:13And again since there's nothing inside
- 1:33:16this component, we can use the
- 1:33:18self-closing element shortcut. So we can
- 1:33:21remove this closing tag. And then at the
- 1:33:24end here we can type space and slash.
- 1:33:29And now let's save
- 1:33:31and check our website.
- 1:33:34And it displays the app on the website.
- 1:33:38Let's go back to our code.
- 1:33:41So a best practice is to use a component
- 1:33:44called app to create the website because
- 1:33:47it gives us access to extra features of
- 1:33:50components like props.
- 1:33:53And also notice that we can have
- 1:33:55components inside a component. So this
- 1:33:59lets us split up a website into smaller
- 1:34:01and smaller components or smaller and
- 1:34:04smaller pieces.
- 1:34:06So again, React helps us create websites
- 1:34:09easier because it lets us work on a
- 1:34:12small piece of the website at a time.
- 1:34:17In the next lesson, we're going to learn
- 1:34:19a very important feature of React called
- 1:34:22state. State lets us make our website
- 1:34:25interactive, and it will let us add new
- 1:34:28chat messages to our website.
- 1:34:32And that's the end of this lesson. In
- 1:34:34this lesson, we learned that a component
- 1:34:37is just a piece of the website. We also
- 1:34:40learned that components let us create
- 1:34:43our own HTML elements. We started the
- 1:34:47chatbot project. We split the chatbot
- 1:34:50into chat input and chat message
- 1:34:53components. We learned props which let
- 1:34:56us insert values into components and
- 1:34:59make components reusable.
- 1:35:02We learned the destructuring and guard
- 1:35:04operator shortcuts. We did some code
- 1:35:07cleanup and split longer lines into
- 1:35:10multiple lines. And we created an app
- 1:35:14component which represents the entire
- 1:35:17application.
- 1:35:19Thanks for watching so far. If you find
- 1:35:22this course valuable, you can support
- 1:35:23the channel by liking this video,
- 1:35:26leaving a comment, and subscribing.
- 1:35:28You can also check out the premium
- 1:35:30version of this course where you'll get
- 1:35:32all the lessons ads free broken down
- 1:35:35into small videos as well as additional
- 1:35:38lessons and more projects that are not
- 1:35:40in the public React course. Currently,
- 1:35:42the premium course contains 11 lessons
- 1:35:45and I'll be adding more lessons in the
- 1:35:47future. You can also get a certificate
- 1:35:50of completion, create comments or
- 1:35:52questions on specific topics, get
- 1:35:55progress tracking, search and course
- 1:35:57notes. You can also check out the
- 1:35:59membership where you can get this course
- 1:36:02ads free separated by each lesson, AI
- 1:36:05videos, backend tutorials, and more.
- 1:36:08Thanks again, and let's continue with
- 1:36:10the course. Here are some exercises to
- 1:36:13help you practice what we learned in
- 1:36:15this lesson.
- 1:36:54In this lesson, we're going to learn two
- 1:36:56of the most important features of React
- 1:36:59called state and event handlers. And
- 1:37:02we're going to use these features to
- 1:37:04create the functionality for the chatbot
- 1:37:07project.
- 1:37:09Before we learn state, we're going to
- 1:37:11improve our code a bit. Currently to add
- 1:37:14a new chat message we have to manually
- 1:37:17type a new chat message component.
- 1:37:20Instead of manually typing this code we
- 1:37:22can actually use JavaScript to generate
- 1:37:25these components.
- 1:37:27To do that we're going to do two steps.
- 1:37:30The first step is to save the data. Data
- 1:37:34means information.
- 1:37:36Here the data is the information about
- 1:37:39our chat messages like the message and
- 1:37:42the sender. So we need to save this data
- 1:37:46or this information in our JavaScript.
- 1:37:50Let's use a variable to save this data.
- 1:37:53At the top of this component, we're
- 1:37:56going to create a variable using const
- 1:37:59and we'll name it chat messages
- 1:38:03equals and we'll save it as an array by
- 1:38:06typing square brackets.
- 1:38:09As a review, an array is a list of
- 1:38:12values. So here we want to save a list
- 1:38:15of chat messages. So it makes sense to
- 1:38:18save it as an array.
- 1:38:21Next, each chat message has multiple
- 1:38:23pieces of data, like the message and the
- 1:38:27sender. So, inside this array, we're
- 1:38:30going to save each chat message as an
- 1:38:33object by typing curly brackets.
- 1:38:37As a review, an object groups related
- 1:38:40values together. So, it's going to help
- 1:38:42us group the message and the sender
- 1:38:45together.
- 1:38:47And then inside this object, we're going
- 1:38:49to create a property called message
- 1:38:53colon and a string. And we're going to
- 1:38:56use this to save the message. So let's
- 1:38:59copy the first message. We'll select it
- 1:39:02and copy. And we're going to paste it in
- 1:39:05here.
- 1:39:07And then we'll add a comma. And we're
- 1:39:10going to add a sender property colon and
- 1:39:14a string. and we're going to save the
- 1:39:16first sender which is the user.
- 1:39:21So that's how we save the data for the
- 1:39:23first chat message in our JavaScript.
- 1:39:27Now let's save the data for the second
- 1:39:29chat message.
- 1:39:31So after the first object, we're going
- 1:39:33to type comma and create another object.
- 1:39:38I will give this one a message property
- 1:39:40as well colon a string. and we'll save
- 1:39:44the second message.
- 1:39:47We'll select this copy and then inside
- 1:39:51here we're going to paste.
- 1:39:54Add a comma and a sender property colon
- 1:39:58and a string. And the second chat
- 1:40:01message is from the robot. So inside the
- 1:40:04sender, we'll save robot.
- 1:40:08Let's do the same thing for the third
- 1:40:10and fourth chat messages. we'll save
- 1:40:12them in our JavaScript. Feel free to
- 1:40:14pause the video if you want to try it
- 1:40:16yourself.
- 1:40:18So, for the third chat message, we're
- 1:40:20going to add a comma at the end and
- 1:40:22another object, a message property, a
- 1:40:27string,
- 1:40:29and we're going to copy the third
- 1:40:31message,
- 1:40:33and paste it in here.
- 1:40:37and a sender property. The third message
- 1:40:40is from the user.
- 1:40:43And then we're going to type comma and
- 1:40:46another object the message property
- 1:40:49colon and a string. And we'll copy paste
- 1:40:52the fourth message. So we'll select it,
- 1:40:56copy, and then inside here we're going
- 1:41:00to paste
- 1:41:02and a sender property.
- 1:41:05And the fourth message is from the
- 1:41:08robot.
- 1:41:09So that's how we save the data or the
- 1:41:12information for the chat messages.
- 1:41:17After we save the data, the second step
- 1:41:20is to generate the HTML. So we're going
- 1:41:23to use JavaScript to convert this data
- 1:41:27into these components. So we don't have
- 1:41:29to type these components manually.
- 1:41:32To do that, after the data,
- 1:41:36we're going to type the name of the
- 1:41:38data, which is chat messages.
- 1:41:43And this is an array. And arrays have a
- 1:41:46feature called dot map and brackets.
- 1:41:51As a review, do map lets us go through
- 1:41:55each value of an array and convert it
- 1:41:58into a new value.
- 1:42:01To use dot map, we need to give it a
- 1:42:04function. So between these brackets,
- 1:42:07we're going to type a function using
- 1:42:10brackets, arrow, and curly brackets.
- 1:42:14As a review, this is an arrow function.
- 1:42:18It mostly works the same way as a
- 1:42:21function, but it's shorter. When we give
- 1:42:25a function to another function like map,
- 1:42:28we usually use an arrow function because
- 1:42:31it makes the code a bit cleaner.
- 1:42:34This is what React tutorials and
- 1:42:35documentations use.
- 1:42:39Next, this function gets a parameter. So
- 1:42:42at the top, we'll create a new parameter
- 1:42:45and name it chat message.
- 1:42:49So now dom takes each value in this
- 1:42:53array and converts it into a new value.
- 1:42:58The way it works is it starts with the
- 1:43:00first value and then it saves it in this
- 1:43:03parameter and runs this function and
- 1:43:06whatever we return from this function
- 1:43:08will be the new first value. Then it
- 1:43:11takes the second value. It saves it in
- 1:43:14this parameter and runs the function.
- 1:43:17And whatever we return from the function
- 1:43:19will be the new second value. Then it
- 1:43:22takes the third value, saves it, runs a
- 1:43:26function and whatever we return will be
- 1:43:28the new third value and so on. So map
- 1:43:33basically goes through each value in
- 1:43:35this array, runs a function and converts
- 1:43:38it into a new value.
- 1:43:44So here we want to convert each chat
- 1:43:47message object into a chat message
- 1:43:50component. So inside this inner
- 1:43:53function, we're going to return a chat
- 1:43:56message component by typing brackets
- 1:44:00and then here less than chat
- 1:44:04message/grater
- 1:44:06than to return a chat message component.
- 1:44:11Next, each chat message component has
- 1:44:14two props, message and sender. So we're
- 1:44:18going to add those props to this
- 1:44:20component.
- 1:44:21So here
- 1:44:23we'll create a prop message
- 1:44:26equals
- 1:44:28and now what do we save in this prop?
- 1:44:31Remember that map takes each object in
- 1:44:35this array and saves it in this
- 1:44:38parameter. So this parameter contains a
- 1:44:41chat message object and each chat
- 1:44:44message object has a message and a
- 1:44:47sender. So we want to save the message
- 1:44:50inside this prop.
- 1:44:52To do that we're going to type curly
- 1:44:55brackets and inside we'll type this
- 1:44:59object chat message and get the message
- 1:45:04property which is dot message.
- 1:45:08This gets the message out of this object
- 1:45:11and saves it in the prop.
- 1:45:15So why do we use curly brackets here
- 1:45:18instead of double quotes? If we use
- 1:45:21double quotes, it will save a string
- 1:45:24into this prop, which is not what we
- 1:45:26want. But if we use curly brackets, it
- 1:45:30will save the result of this code into
- 1:45:33this prop.
- 1:45:36So for the first chat message, the
- 1:45:38result of chat message dossage is going
- 1:45:42to be the string hello chatbot. So it's
- 1:45:45going to take this and save it into the
- 1:45:47message prop here. For the second chat
- 1:45:51message, the result of this code is
- 1:45:54hello, how can I help you? And it will
- 1:45:56save this into the message prop and so
- 1:45:59on. So using curly brackets we can put
- 1:46:03any JavaScript code inside and it will
- 1:46:06save the result of this code into the
- 1:46:09prop not just a string.
- 1:46:12So next each chat message component has
- 1:46:15a sender prop. So here we're also going
- 1:46:19to add a sender
- 1:46:22equals and again we want to save the
- 1:46:25sender property of this chat message
- 1:46:28object. So we'll type curly brackets and
- 1:46:32the object
- 1:46:34chat message dot sender.
- 1:46:38So this will get the sender property out
- 1:46:41of each chat message object and save it
- 1:46:44in here. And that's it. Now map will
- 1:46:48take each value of this array and
- 1:46:51convert it into a chat message
- 1:46:53component.
- 1:46:55And here's what the result looks like.
- 1:47:02Next, we're going to save the result of
- 1:47:04this code in a variable. So, up here,
- 1:47:08let's type const and we'll name the
- 1:47:11variable chat message components equals
- 1:47:17this code.
- 1:47:19And now we have an array of components
- 1:47:23or a list of components.
- 1:47:26Finally, we're going to insert this
- 1:47:28array into the JSX.
- 1:47:31So, down here, we can actually remove
- 1:47:34all of these chat message components
- 1:47:36because we're generating them now.
- 1:47:40And we're going to type curly brackets,
- 1:47:42and we're going to insert this array.
- 1:47:46In here, we'll type chat message
- 1:47:50components.
- 1:47:52If we insert an array of components,
- 1:47:56React will just display all of these
- 1:47:58components on the page. If we save
- 1:48:03and then go to our project,
- 1:48:06it displays all the chat message
- 1:48:08components that we generated with our
- 1:48:10JavaScript.
- 1:48:11So now we're generating this HTML with
- 1:48:14JavaScript and we no longer have to type
- 1:48:17out each component manually.
- 1:48:20Now, there's one more thing that's
- 1:48:22useful to know. Let's go back to our
- 1:48:24code.
- 1:48:27And there's actually a shortcut that we
- 1:48:29can use here. Instead of saving this
- 1:48:32result in a variable, we can actually
- 1:48:35just insert this result directly in the
- 1:48:37JSX.
- 1:48:39So, we'll select this code
- 1:48:42and then cut.
- 1:48:45And between the curly brackets, we're
- 1:48:47going to remove the variable and just
- 1:48:49paste that code.
- 1:48:52And we'll remove the semicolon at the
- 1:48:54end.
- 1:48:56And we'll select this code and press tab
- 1:48:59a few times to fix the indenting.
- 1:49:02And finally, we can remove this variable
- 1:49:05up here.
- 1:49:09Now we're just inserting the result of
- 1:49:11this code directly in the JSX.
- 1:49:14This shortcut is used a lot in React
- 1:49:17tutorials and documentation. So it's
- 1:49:20useful to know how it works.
- 1:49:23Now let's save
- 1:49:25and then check our website.
- 1:49:28And everything still works. So that's
- 1:49:30how we generate the HTML.
- 1:49:35Before we move on, there's actually an
- 1:49:37issue that we need to fix here. If we
- 1:49:40look at our console and we scroll up,
- 1:49:43we'll see an error saying each child in
- 1:49:46a list should have a unique key prop. In
- 1:49:50React, if we insert an array of
- 1:49:53components, we need to give each
- 1:49:55component a prop called key. The key
- 1:49:59helps React track changes in the array.
- 1:50:03So, we should give each component a key.
- 1:50:06Let's go back to VS Code
- 1:50:10and for each chat message, we're going
- 1:50:12to add a prop called key equals.
- 1:50:17And this key has to be unique. So, what
- 1:50:21should we use as the key?
- 1:50:23Usually when we have some data like this
- 1:50:27in the real world, each piece of data
- 1:50:30should have a unique ID that lets us
- 1:50:33identify it. So let's add a unique ID to
- 1:50:36each message to match what we would see
- 1:50:39in the real world. So for the first
- 1:50:41message after the sender, we'll add a
- 1:50:44property called ID colon. And let's just
- 1:50:49make it a string ID1 to keep it simple.
- 1:50:53For the second message, we'll add an ID
- 1:50:56with the string ID 2. For the third
- 1:51:00message, we'll make the ID ID3. And for
- 1:51:04the fourth message, we'll add an ID
- 1:51:07string ID 4. So, we'll use these IDs for
- 1:51:12practice. They just have to be unique.
- 1:51:16And now we'll use this unique ID as the
- 1:51:19key. So down here we'll set the key to
- 1:51:22be equal to curly brackets and we'll get
- 1:51:26the chat message do ID.
- 1:51:30So chat message
- 1:51:33id.
- 1:51:35Again we use curly brackets here because
- 1:51:38we want to save the result of this code
- 1:51:41as the key. And now each component in
- 1:51:44this array will have a unique key. Let's
- 1:51:48save and check our website.
- 1:51:52And now the error is gone from the
- 1:51:54console.
- 1:51:57Let's go back to our code.
- 1:52:00And now we have a lot of code in our app
- 1:52:04component.
- 1:52:05To make our code easier to work with,
- 1:52:08we're going to separate this code into
- 1:52:10smaller pieces.
- 1:52:12Notice that this code and this code are
- 1:52:16related to chat messages.
- 1:52:19So it makes sense to separate these two
- 1:52:22pieces of code into a chat messages
- 1:52:25component.
- 1:52:26So we'll scroll up
- 1:52:29above app. Let's create a new component
- 1:52:32using function and we'll name it capital
- 1:52:36C chat capital M messages
- 1:52:41brackets and curly brackets.
- 1:52:44And now we're going to move all the code
- 1:52:46related to chat messages into this new
- 1:52:49component. So we'll select this code
- 1:52:54and cut.
- 1:52:57And inside here, we're going to paste
- 1:53:02and create some new lines. And we're
- 1:53:04going to select this code
- 1:53:08and cut.
- 1:53:10And inside here, we're going to paste.
- 1:53:15And at the end of this component, we
- 1:53:18need to return the HTML.
- 1:53:20So at the end we're going to return open
- 1:53:24bracket and we'll remove the closed
- 1:53:27bracket for now and we're going to use a
- 1:53:29fragment to group all of these
- 1:53:31components together. So we'll type less
- 1:53:34than greater than and at the end of this
- 1:53:38less than/greater than and we'll add the
- 1:53:41close bracket at the end. And finally,
- 1:53:45we can fix the indenting by pressing
- 1:53:48tab. And we'll remove the indent here
- 1:53:51and here as well, so that these are
- 1:53:54vertically aligned.
- 1:53:56Finally, now that we have this chat
- 1:53:59messages component, we're going to
- 1:54:01insert this into our app. So, we'll
- 1:54:04scroll down into app and remove these
- 1:54:07lines.
- 1:54:10And to insert the chat messages
- 1:54:12component, we'll use the component
- 1:54:14syntax again. Less than capital C chat
- 1:54:19capital M messages
- 1:54:22slash greater than.
- 1:54:25So now we just separated all that code
- 1:54:27into its own component and it makes our
- 1:54:30code much cleaner. Let's save
- 1:54:34and check our website.
- 1:54:37And everything still works. We just
- 1:54:39reorganized our code into a chat
- 1:54:41messages component.
- 1:54:44Okay, so far we saved the data and we
- 1:54:48generated the HTML. The last step to
- 1:54:51building a website is to make it
- 1:54:53interactive.
- 1:54:55For example, when we click this send
- 1:54:58button, we want to add a chat message to
- 1:55:00the website. To make this website
- 1:55:03interactive, we're going to use event
- 1:55:06handlers.
- 1:55:07An event handler lets us run a function
- 1:55:11when we interact with a website.
- 1:55:14Let's do an example. We'll go back to
- 1:55:16our code
- 1:55:19and we're going to scroll up to the chat
- 1:55:21messages component
- 1:55:23and then inside this fragment we're
- 1:55:26going to create a new line and let's
- 1:55:28create a new button using less than
- 1:55:32button greater than and less than/button
- 1:55:36greater than and inside this button
- 1:55:39let's put the text send message.
- 1:55:44So, we'll create a new button for
- 1:55:45practice.
- 1:55:47Next, we're going to give this button a
- 1:55:49special prop. So, here we'll add a new
- 1:55:53prop called on capital C click equals
- 1:55:59curly brackets.
- 1:56:01So, on click lets us run a function when
- 1:56:05we click this button. So, above this,
- 1:56:08let's create a function to run. So we'll
- 1:56:11type function and let's name it send
- 1:56:16message brackets and curly brackets
- 1:56:21and inside this function let's just
- 1:56:23console.log
- 1:56:25the string send message.
- 1:56:30Now that we have this function we're
- 1:56:32going to save this function inside on
- 1:56:35click. So between these curly brackets,
- 1:56:38we're going to give the name of the
- 1:56:39function send message.
- 1:56:43So notice that again we used curly
- 1:56:46brackets. Curly brackets lets us save
- 1:56:49any type of value in a prop including a
- 1:56:52function.
- 1:56:54Now that we gave on click this function,
- 1:56:57when we click this button, it's going to
- 1:56:59run this function and console.log this
- 1:57:02message. Let's save
- 1:57:06and we'll go to our website
- 1:57:09and try clicking this new send message
- 1:57:11button
- 1:57:13and this will run the function and
- 1:57:15display the message send message in the
- 1:57:18console.
- 1:57:19Let's go back to our code
- 1:57:23and we'll learn more details about event
- 1:57:25handlers.
- 1:57:27So on click is known as the event and
- 1:57:31send message is known as the event
- 1:57:33handler. It does something when this
- 1:57:36event happens or it handles the event.
- 1:57:41Now there's two other things to note.
- 1:57:43The event prop always starts with on and
- 1:57:47then the name of the event like click
- 1:57:50and the event prop must use camelc case.
- 1:57:54As a review, camelc case means the first
- 1:57:57word is lowercase and all other words
- 1:58:00start with a capital. So that's why on
- 1:58:03is lowercase and click starts with a
- 1:58:05capital C.
- 1:58:08Another thing we should know is that
- 1:58:10here we should not run the event handler
- 1:58:13function. So we should not use brackets.
- 1:58:17So this will run the function and then
- 1:58:19the result of the function will be
- 1:58:21undefined. and then it's going to save
- 1:58:24undefined into on click which doesn't
- 1:58:27work. Instead, we should just give the
- 1:58:30function directly to on click without
- 1:58:33running it.
- 1:58:35So that's event handlers and event
- 1:58:37handler lets us run a function when we
- 1:58:41interact with a website and they make
- 1:58:43the website interactive.
- 1:58:47Next, when we click this button, instead
- 1:58:50of just console.log, log. Let's add a
- 1:58:54new chat message. To do that, we can add
- 1:58:57a new object to the chat messages array
- 1:59:00up here. So, inside this function, let's
- 1:59:04remove console.log
- 1:59:06and then type the name of the array chat
- 1:59:10messages and then use the code.push
- 1:59:15and brackets.
- 1:59:17As a review, dot push adds a value to
- 1:59:21the end of an array. So inside here,
- 1:59:25we're going to add another object using
- 1:59:27curly brackets.
- 1:59:30And each object will have a message
- 1:59:32property. So we'll type message colon
- 1:59:37and then a string. And for our new
- 1:59:39message, let's just make the message
- 1:59:42test.
- 1:59:43And we'll add a comma. And we'll add a
- 1:59:46sender property sender colon a string
- 1:59:51and this message will be from the user
- 1:59:54comma and we'll give it an ID colon and
- 1:59:58let's just use the string ID5.
- 2:00:02Now there's one problem with using ID5
- 2:00:05in this situation.
- 2:00:07Every message that we create when we
- 2:00:10click this button is going to have the
- 2:00:12ID ID5. But the ID should be unique. To
- 2:00:17create a unique ID, we can use some
- 2:00:20JavaScript code. So instead of ID5,
- 2:00:23we're going to use the code crypto dot
- 2:00:28random capital U, capital U, capital I,
- 2:00:32capital D, and brackets.
- 2:00:36So this is some JavaScript code that
- 2:00:38will generate a unique ID string every
- 2:00:41time we run this code. So now every
- 2:00:44message that we create using this button
- 2:00:46will have a unique ID
- 2:00:50at the end. Let's create some new lines
- 2:00:53and let's console.log
- 2:00:56the chat messages array to see if it has
- 2:01:00a new message. Let's save
- 2:01:04and we'll go to our website
- 2:01:06and we'll click send message.
- 2:01:10And now inside chat messages, we have
- 2:01:13five messages. So we added this last one
- 2:01:16here. It has the message test. And
- 2:01:20notice it has a unique ID. So we
- 2:01:23successfully added a chat message when
- 2:01:26clicking this button.
- 2:01:30Next, notice that even though we updated
- 2:01:33the data, it did not update the website.
- 2:01:36So, it did not display a new chat
- 2:01:39message component on the website. To fix
- 2:01:42this, we're going to learn one of the
- 2:01:44most important features of React called
- 2:01:47state. State is data that is connected
- 2:01:52to the HTML. When we update this data,
- 2:01:56it will update the HTML.
- 2:02:00Let's go back to our code.
- 2:02:03And if we scroll up,
- 2:02:06currently chat messages is just a normal
- 2:02:09variable. When we update chat messages,
- 2:02:13it will not update the HTML.
- 2:02:16However, if we convert chat messages
- 2:02:19into state, when we update chat
- 2:02:22messages, it will update the HTML. So
- 2:02:25now let's learn how to convert chat
- 2:02:27messages into state.
- 2:02:31At the top of this component, we're
- 2:02:33going to use this code capital R react
- 2:02:37dot use capital S state and brackets.
- 2:02:44And between the brackets, we're going to
- 2:02:47give it the chat messages data. So,
- 2:02:50we're going to select this data
- 2:02:53and cut.
- 2:02:56and between these brackets we're going
- 2:02:58to paste.
- 2:03:05So this will convert this data into
- 2:03:08state. So now if we update this data, it
- 2:03:12will also update the HTML.
- 2:03:16So at the bottom here, we can remove the
- 2:03:18chat messages variable because we
- 2:03:21converted it into state.
- 2:03:25Next, react doouse state returns an
- 2:03:28array. So at the front, let's save the
- 2:03:31result in a variable const array equals
- 2:03:36react. State.
- 2:03:40This array has two values. To get the
- 2:03:43first value, we can click here to
- 2:03:46collapse the code for now. And to get
- 2:03:49the first value, we're going to type the
- 2:03:51name of the array. Array and then square
- 2:03:54brackets zero.
- 2:03:57So remember, an array is a list of
- 2:03:59values and square bracket zero gets us
- 2:04:02the first value in the list or in the
- 2:04:04array.
- 2:04:06So the first value we get from use state
- 2:04:09is the current data.
- 2:04:12So here chat messages can change over
- 2:04:16time. It's going to start off as this
- 2:04:18value which is the initial value and
- 2:04:22then we might add new chat messages in
- 2:04:24the future.
- 2:04:27So the first value in this array gives
- 2:04:29us the current value of chat messages or
- 2:04:32the current data.
- 2:04:35Let's save this in a variable so we can
- 2:04:37use it later. We'll do const and let's
- 2:04:41name it chat messages equals array zero.
- 2:04:47So now we have the most current chat
- 2:04:49messages and we can use it down here to
- 2:04:52generate the HTML.
- 2:04:55Next use state gives us a second value
- 2:04:58in this array. To get the second value
- 2:05:02we're going to type array square bracket
- 2:05:041.
- 2:05:07And the second value of this array is a
- 2:05:10function that updates this data.
- 2:05:14So let's save this function in a
- 2:05:16variable using const. And the naming
- 2:05:20convention for this function is a use
- 2:05:23set. And then the name of the data which
- 2:05:26is capital C chat capital M messages
- 2:05:32equals this value. And we should also
- 2:05:35use camel case here. So the first word
- 2:05:37starts with a lowercase and every other
- 2:05:40word starts with a capital.
- 2:05:43So this function lets us update the
- 2:05:45data. In React, we should not update the
- 2:05:49data directly like we did down here
- 2:05:52using push. We should always use this
- 2:05:55function to update the data. And that's
- 2:05:58because this function tells React that
- 2:06:01the data changed and to update the HTML.
- 2:06:05If we update the data directly, React
- 2:06:07will not update the HTML.
- 2:06:10If we use this function to update the
- 2:06:13data, React will update the HTML. So we
- 2:06:17should always use this function to
- 2:06:19update the data. This is also known as
- 2:06:22the updater function.
- 2:06:27So now let's give it a try. Instead of
- 2:06:29updating this data directly, we're going
- 2:06:32to use this updater function by typing
- 2:06:36set chat messages and brackets.
- 2:06:42And between the brackets, we're going to
- 2:06:44put the new value of chat messages.
- 2:06:48So the way that this updator function
- 2:06:50works is it's going to replace the
- 2:06:53entire chat messages array with a new
- 2:06:56array. So let's give it a try. First, we
- 2:07:00need to make a copy of this array.
- 2:07:04In React, we should not modify the data
- 2:07:06directly. We should always create a copy
- 2:07:09of the data and then modify the copy.
- 2:07:13This helps React be more efficient.
- 2:07:16To create a copy, we're going to click
- 2:07:19between these brackets and create a new
- 2:07:22array using square brackets.
- 2:07:26And to copy chat messages, we can use
- 2:07:29this code dot dot dot and the array that
- 2:07:33we want to copy, which is chat messages.
- 2:07:38As a review, this is a JavaScript
- 2:07:41feature called the spread operator. It
- 2:07:44basically takes the values in an array
- 2:07:47and then copies them into a new array.
- 2:07:51You can think of it like we're taking
- 2:07:53the values in this array and then
- 2:07:55spreading them out into this new array.
- 2:07:58That's why it's called the spread
- 2:08:00operator.
- 2:08:03The spread operator is used a lot in
- 2:08:05React tutorials and documentation. So,
- 2:08:08it's useful to know how it works.
- 2:08:12So, we created a copy of chat messages.
- 2:08:16And now we want to add a new chat
- 2:08:19message at the end of this copy because
- 2:08:22we're trying to send a new message. To
- 2:08:24do that, we can just add a comma at the
- 2:08:27end and then add a new chat message
- 2:08:30directly using curly brackets.
- 2:08:34So each chat message has a message
- 2:08:36property colon and we'll give it the
- 2:08:39string test for now. And each chat
- 2:08:43message has a sender property colon. And
- 2:08:46let's make the sender the user.
- 2:08:49And we'll give each chat message an ID.
- 2:08:53And to create a unique ID we can use
- 2:08:56crypto
- 2:08:58random capital U capital U capital I
- 2:09:03capital D and brackets.
- 2:09:07So using the spread operator we made a
- 2:09:10copy of the chat messages array and then
- 2:09:13we added a value at the end of this new
- 2:09:16array.
- 2:09:17And now this updater function will
- 2:09:20replace chat messages with this new
- 2:09:23array. And because we use this updater
- 2:09:26function, React will regenerate the
- 2:09:29HTML. So it will basically rerun all
- 2:09:32this code and it will update the
- 2:09:34website. So now let's save
- 2:09:38and let's check our website
- 2:09:41and we'll press send message.
- 2:09:44it will create a new chat message in our
- 2:09:47data and it will also update the HTML.
- 2:09:51So it will create a new chat message
- 2:09:53component and display it on the website.
- 2:09:57Now let's go back
- 2:10:01and that is how state works. Let's do a
- 2:10:04short review. State is basically data
- 2:10:07that is connected to the HTML. When we
- 2:10:11update this data or we update the state,
- 2:10:14it will update the HTML.
- 2:10:17To convert some data into state, we use
- 2:10:20react doouse state. So here it will
- 2:10:23convert this data into state and use
- 2:10:27state gives us two values.
- 2:10:31The first value is the current data and
- 2:10:35then the second value is a function to
- 2:10:37update this data. If we use the updater
- 2:10:41function, it will also update the HTML.
- 2:10:45So don't worry if state is a little bit
- 2:10:48confusing at first. The more you work
- 2:10:50with state, the more you'll get used to
- 2:10:52it. And we'll do more examples of state
- 2:10:54in this course and in the exercises so
- 2:10:57you can get more practice.
- 2:11:01Next, we're going to learn a shortcut
- 2:11:03called array destructuring.
- 2:11:06Here we get the first value out of this
- 2:11:09array and save it in a variable called
- 2:11:12chat messages.
- 2:11:14Now JavaScript actually has a shortcut
- 2:11:17for this. Above this we can type const
- 2:11:22and then square bracket equals this
- 2:11:25array
- 2:11:27and inside the square brackets we can
- 2:11:29type chat messages.
- 2:11:33So this is similar to object
- 2:11:35dstructuring. This gets the first value
- 2:11:39out of this array and saves it in a
- 2:11:41variable called chat messages. So this
- 2:11:44code is a shortcut for this code. We can
- 2:11:48also get the second value out of this
- 2:11:51array. At the end here we can type comma
- 2:11:55and then another variable name. Let's
- 2:11:57use set chat messages.
- 2:12:02So this gets the second value out of
- 2:12:05this array and saves it in a variable
- 2:12:08called set chat messages. So this code
- 2:12:12is a shortcut for these two lines of
- 2:12:15code. And this shortcut is called array
- 2:12:18dstructuring because we're taking stuff
- 2:12:21out of this array. We're deconstructing
- 2:12:24it. Now in array dstructuring the order
- 2:12:28matters. So this will always get the
- 2:12:31first value out of the array and this
- 2:12:33will always get the second value out of
- 2:12:36the array.
- 2:12:38So now let's comment out these two
- 2:12:40lines. So we remember that this is just
- 2:12:42a shortcut. So we'll type double slash
- 2:12:45at the front and here double slash at
- 2:12:48the front.
- 2:12:50Now another shortcut we can use is
- 2:12:52instead of saving the result of use
- 2:12:55state in this variable and then
- 2:12:57dstructuring it we can just dstructure
- 2:13:00this up here.
- 2:13:02So instead of array we can just directly
- 2:13:05dstructure using square brackets and
- 2:13:08then the first value of u state is going
- 2:13:11to be chat messages
- 2:13:14and the second value from state is going
- 2:13:16to be the updater function which is set
- 2:13:20chat messages.
- 2:13:23So this code is a shortcut for this
- 2:13:26code. We just don't save it in a
- 2:13:28variable in the middle.
- 2:13:31So at the front of this code, let's also
- 2:13:33comment it out using double slash.
- 2:13:37So that is array dstructuring. It gives
- 2:13:40us an easier way to take values out of
- 2:13:43an array.
- 2:13:45Now this shortcut is used a lot in React
- 2:13:47tutorials and documentation. So it's
- 2:13:50useful to know how it works.
- 2:13:54Next, instead of adding the same message
- 2:13:57test every time, we should add whatever
- 2:14:00message is in the text box. So, we'll
- 2:14:03learn how to get the text in a text box.
- 2:14:07In normal JavaScript, we might use the
- 2:14:09DOM to get the input element and then
- 2:14:13use a property called dot value to get
- 2:14:16the text inside the text box.
- 2:14:21However, in React, a best practice is do
- 2:14:24not use the DOM manually. That's because
- 2:14:27React is managing the website. So, if we
- 2:14:30use the DOM manually, it might interfere
- 2:14:33with React.
- 2:14:36Instead, we should use React features to
- 2:14:39get the text from a text box. One way to
- 2:14:42do this is we're going to scroll up to
- 2:14:46the text box which is inside the chat
- 2:14:50input component and it's the input
- 2:14:52element here. So to get the text in this
- 2:14:55text box we can use a prop called on
- 2:15:00capital C change equals curly brackets.
- 2:15:06Earlier we learned about the event on
- 2:15:09click which runs a function when we
- 2:15:11click an element. On change is another
- 2:15:15event we can use in React. You can tell
- 2:15:18it's an event because it starts with on.
- 2:15:22So onchange runs a function when we
- 2:15:24change the text inside an input.
- 2:15:28So above this let's create a function to
- 2:15:31run. We'll type function
- 2:15:35and let's name it save input text
- 2:15:40brackets and curly brackets because
- 2:15:43we're going to save the text inside this
- 2:15:46input.
- 2:15:48And now let's save this function inside
- 2:15:50on change. So between the curly brackets
- 2:15:53we'll give it the function save input
- 2:15:57text. So again we use curly brackets
- 2:16:01here because we want to save a function
- 2:16:04inside this prop rather than just a
- 2:16:06string. So now every time we change the
- 2:16:10text in this input it will run this
- 2:16:13function.
- 2:16:15Next when onchange runs this function
- 2:16:19it's going to give this function one
- 2:16:21parameter up here called event.
- 2:16:26event is an object and it contains
- 2:16:28details about the event or about the
- 2:16:31change. So one detail is event dot
- 2:16:37target
- 2:16:39and this gives us the element that we're
- 2:16:41typing in or that we changed. So in this
- 2:16:45case it's going to give us this input
- 2:16:47element. So React gives us access to
- 2:16:50this input element automatically using
- 2:16:53event.target. target.
- 2:16:55And now to get the text inside this
- 2:16:58input, we're going to use the property
- 2:17:01value.
- 2:17:03So now let's console.log this to see
- 2:17:05what it looks like. console.log
- 2:17:10and let's save our code
- 2:17:14and we'll go back to our website
- 2:17:17and we'll give it a try. So we'll click
- 2:17:19this text box and we're going to type
- 2:17:22hello.
- 2:17:24And you'll see that every time we type
- 2:17:26or change the text, it will console.log
- 2:17:29the text in the text box. So that is how
- 2:17:33we get the text inside the text box
- 2:17:36using an onchange event.
- 2:17:40Let's go back to our code.
- 2:17:44And now we need to save this text so we
- 2:17:48can use it later when creating a chat
- 2:17:50message. To save this text, we're going
- 2:17:53to use state. In React, we should use
- 2:17:56state to save data that changes over
- 2:17:59time, like the text in the text box. So,
- 2:18:03at the top of this component,
- 2:18:06let's use the code react dot use state
- 2:18:11and brackets.
- 2:18:14And between the brackets, we're going to
- 2:18:16put the initial value. In this case, the
- 2:18:20initial value in the text box.
- 2:18:23The text box originally starts as empty.
- 2:18:26So let's set the value to the empty
- 2:18:28string.
- 2:18:30Next, use state returns an array with
- 2:18:34two values. So at the front, let's use
- 2:18:37array dstructuring using const and we'll
- 2:18:41type square brackets equals this result.
- 2:18:46And now to get the first value out of
- 2:18:48this array inside the square brackets
- 2:18:51we're going to type input text.
- 2:18:56So this is the current data or the
- 2:18:59current text in the text box. So that's
- 2:19:03why we name it input text.
- 2:19:06To get the second value out of this
- 2:19:08array after this we can type comma and
- 2:19:12give a name for the second value. So the
- 2:19:15naming convention is to use the word set
- 2:19:18and then the name of the data which is
- 2:19:22capital I input capital T text.
- 2:19:27So this second value is an updator
- 2:19:30function. It lets us update the data.
- 2:19:34Now that we set up the state, every time
- 2:19:37we change the text, we're going to save
- 2:19:39it up here inside input text. To do
- 2:19:43that, instead of console.log,
- 2:19:46we're going to use the updater function
- 2:19:49set input text.
- 2:19:53And now, every time we change the text,
- 2:19:56it's going to run this function. And
- 2:19:58it's going to save the text inside input
- 2:20:02text.
- 2:20:05Next, when we click the send button, we
- 2:20:08want to use this text to create a new
- 2:20:11chat message. First, we need to make the
- 2:20:14send button interactive.
- 2:20:16To do that, inside this button element,
- 2:20:20we're going to add the prop on click
- 2:20:23equals curly brackets.
- 2:20:27Again, on click lets us run a function
- 2:20:30when we click this button.
- 2:20:33And now above this, let's create a
- 2:20:35function to run. We'll use function
- 2:20:39and let's name it send message brackets
- 2:20:44and curly brackets and we're going to
- 2:20:46give this function to on click. So down
- 2:20:49here we'll give the function name send
- 2:20:52message.
- 2:20:55Now when we click this button it will
- 2:20:58run this function and let's just
- 2:21:00console.log input text to see what the
- 2:21:03value is. So in here we'll console.log
- 2:21:09brackets the input text and save.
- 2:21:15And we'll go back to our website
- 2:21:18and in the text box we're going to type
- 2:21:22hello and then press send.
- 2:21:26And you'll see that it console.logs the
- 2:21:29text inside the text box.
- 2:21:34Next, we're going to create a new chat
- 2:21:36message using this text. Let's go back
- 2:21:40to our code.
- 2:21:42And if we scroll down in our code
- 2:21:47to the chat messages component, we
- 2:21:51already have code here that adds a new
- 2:21:54message to chat messages. So, let's just
- 2:21:57select this code and we'll copy.
- 2:22:01And at the top
- 2:22:04inside send message, we're going to
- 2:22:07paste. And we can remove this
- 2:22:09console.log.
- 2:22:12And we'll scroll back down.
- 2:22:16And we created this function and this
- 2:22:18button for practice. But now we don't
- 2:22:21need it anymore. So we can actually
- 2:22:23remove this code
- 2:22:25and remove this practice button.
- 2:22:30Now, let's scroll back up
- 2:22:35and we're going to use this code to add
- 2:22:37a new chat message.
- 2:22:40However, we have a problem here. We
- 2:22:43don't actually have access to set chat
- 2:22:45messages inside this component because
- 2:22:49this is inside a different component. To
- 2:22:52solve this problem, we're going to learn
- 2:22:54a technique called lifting the state up.
- 2:22:59First we need to visualize the
- 2:23:01components in our app. So we have an app
- 2:23:05component which represents the entire
- 2:23:07application and inside we have a chat
- 2:23:10input component and a chat messages
- 2:23:13component. Inside the chat messages
- 2:23:16component we have some chat message
- 2:23:18components.
- 2:23:20This is called the component tree
- 2:23:22because it looks like the branch of a
- 2:23:25tree and this helps us visualize the
- 2:23:28structure of the components in our app.
- 2:23:32Currently, chat messages and set chat
- 2:23:35messages are located inside the chat
- 2:23:38messages component, but we want to use
- 2:23:41it in the chat input component as well.
- 2:23:45To solve this problem, we're going to
- 2:23:47move this state up into the app
- 2:23:50component and then app can share chat
- 2:23:53messages and set chat messages between
- 2:23:56these two components.
- 2:23:58This technique is called lifting the
- 2:24:00state up. By moving the state up to an
- 2:24:04outer component, it lets us share state
- 2:24:07between multiple components.
- 2:24:11So, let's give this a try. First, we're
- 2:24:14going to move the chat messages state up
- 2:24:18into app. So, in our code, we're going
- 2:24:22to scroll down
- 2:24:24to the chat messages state, which is
- 2:24:26right here, and we're going to move it
- 2:24:29into the app component. So, we'll select
- 2:24:32all this code,
- 2:24:34and then cut
- 2:24:37and remove these lines.
- 2:24:40And then inside the app component,
- 2:24:43we'll create some new lines and paste.
- 2:24:48So now we lifted the state up into the
- 2:24:52app component.
- 2:24:54Next, we're going to share the state
- 2:24:56between the two components using props.
- 2:25:02So first, the chat input component needs
- 2:25:05set chat messages and chat messages.
- 2:25:10So down here, let's create two props for
- 2:25:14this component.
- 2:25:16We'll create a prop called chat messages
- 2:25:21equals curly brackets. And we're going
- 2:25:23to save the value of chat messages from
- 2:25:26the state. So we'll save chat messages.
- 2:25:31And we'll give it another prop called
- 2:25:34set chat messages equals curly brackets.
- 2:25:39And we'll give it the set chat messages
- 2:25:41function from the state.
- 2:25:45So set chat messages.
- 2:25:50By the way, notice that chat messages is
- 2:25:53an array. So using curly brackets, we
- 2:25:56can save an array into this prop. So we
- 2:26:00can save any type of value inside a
- 2:26:02prop.
- 2:26:05And then inside chat input, we can
- 2:26:08access these two props by scrolling up.
- 2:26:13And in the chat input component at the
- 2:26:16top, we'll type curly brackets and we'll
- 2:26:19get the chat messages prop,
- 2:26:24and we'll also get the set chat messages
- 2:26:28prop.
- 2:26:31And now this code has access to set chat
- 2:26:34messages and chat messages.
- 2:26:39Next, let's scroll back down
- 2:26:42to the chat messages component.
- 2:26:45So here we just need the chat messages
- 2:26:48array to generate the HTML. So let's
- 2:26:52provide chat messages using a prop at
- 2:26:55the bottom
- 2:26:58at the chat messages component. We're
- 2:27:00going to add a prop. chat messages
- 2:27:04equals curly brackets and we'll give it
- 2:27:07the chat messages array. So here chat
- 2:27:12messages.
- 2:27:15Now I want to talk about the naming
- 2:27:17convention used here. You'll notice that
- 2:27:19the component, the prop and the data all
- 2:27:23use the same name. Instead of using
- 2:27:26names like this,
- 2:27:30this is the naming convention used in
- 2:27:32the React documentation.
- 2:27:35The documentation tries to use the same
- 2:27:37name for many things like the component,
- 2:27:40the props, and the data. This keeps
- 2:27:43things simple and consistent. We don't
- 2:27:45have to come up with different names
- 2:27:47every time. And when we look at other
- 2:27:49people's React code, the names will be
- 2:27:52the same.
- 2:27:54So, don't worry. It might be a bit
- 2:27:56confusing at first because many things
- 2:27:58use the same name. The more you work
- 2:28:01with React, the more you'll get used to
- 2:28:03this naming convention, and we'll get
- 2:28:06lots of practice in this course.
- 2:28:10Now to access this prop, we're going to
- 2:28:13scroll up
- 2:28:16and up here we'll type curly bracket
- 2:28:20and we'll get the chat messages prop.
- 2:28:24And now this code has access to
- 2:28:27everything that it needs. Let's save our
- 2:28:30code and then check our website.
- 2:28:34And you'll notice that everything still
- 2:28:36works. We just lifted the state up into
- 2:28:40the app component and shared it between
- 2:28:43chat input and chat messages.
- 2:28:50Let's go back to our code
- 2:28:53and we're going to scroll up to the chat
- 2:28:56input component.
- 2:28:59So now that chat input has access to set
- 2:29:02chat messages, let's use it to create a
- 2:29:06new chat message. So when we click send,
- 2:29:10we're going to run set chat messages and
- 2:29:12we're going to give it a copy of the
- 2:29:15chat messages array with a new message
- 2:29:18at the end. In this new message, instead
- 2:29:22of test every time, we're going to use
- 2:29:24the text inside the text box, which is
- 2:29:27saved in input text. So, we'll remove
- 2:29:30this and replace it with input text.
- 2:29:36The sender will be the user because we
- 2:29:39sent this message. And we'll give the
- 2:29:41message a unique ID. And that's it. This
- 2:29:45will update the chat messages array with
- 2:29:48this new message. And because we use the
- 2:29:51updater function, React will also update
- 2:29:54the HTML.
- 2:29:56So, let's save.
- 2:29:59And we'll go to our project.
- 2:30:02Inside this text box, let's type hello
- 2:30:06and press send.
- 2:30:09It will take the text in the text box,
- 2:30:12create a new chat message, and it will
- 2:30:14update the HTML.
- 2:30:19Next, there is one improvement that we
- 2:30:22can make at this point. If we scroll up
- 2:30:25after we press send, we should also
- 2:30:28remove the text in the text box. So, we
- 2:30:31don't have to manually remove it if we
- 2:30:33want to send another message.
- 2:30:35To do that, we're going to learn a
- 2:30:37feature called controlled inputs.
- 2:30:42Let's go back to our code.
- 2:30:45And after adding a new message, we're
- 2:30:48going to remove the text in the text
- 2:30:50box. So after this, let's use the
- 2:30:54updater function. Set input text
- 2:30:59brackets
- 2:31:00will change the input text to the empty
- 2:31:03string.
- 2:31:05So this will update the input text up
- 2:31:08here to be empty.
- 2:31:11However, it will not update the HTML
- 2:31:14yet.
- 2:31:16To update the HTML, we need to use this
- 2:31:19input text inside this HTML.
- 2:31:24To do that, we're going to give the
- 2:31:25input element a prop called value equals
- 2:31:30curly brackets.
- 2:31:34So value lets us change the value of
- 2:31:37this input or the text inside this
- 2:31:40input.
- 2:31:42So we're going to set the value to
- 2:31:44whatever input text is.
- 2:31:48So now if we set the input text to be
- 2:31:51empty, this will set the value to be
- 2:31:54empty as well and it will make the text
- 2:31:56box empty.
- 2:31:59Let's save.
- 2:32:01We'll go back to our website.
- 2:32:04Inside the text box, we're going to type
- 2:32:07hello and press send.
- 2:32:10And you'll notice that it makes the text
- 2:32:12box empty after we send. So let's go
- 2:32:16back to our code.
- 2:32:19And this is called a controlled input.
- 2:32:22Using the value prop, we can control the
- 2:32:26text inside this text box using our
- 2:32:29code.
- 2:32:32The last feature we're going to work on
- 2:32:33in this lesson is if we scroll up after
- 2:32:38we send a message, we're going to get a
- 2:32:40response from the chatbot.
- 2:32:45To get a response, we're going to use
- 2:32:47another external library. So, we'll
- 2:32:50scroll up to the script elements up
- 2:32:53here. And to add another external
- 2:32:56library, we're going to add another
- 2:32:58script element.
- 2:33:00And we're going to give this element the
- 2:33:02source attribute equals double quotes.
- 2:33:07And we're going to give it the URL of
- 2:33:09the external library. Now, this is
- 2:33:12another external library that I wrote.
- 2:33:15So, let's just copy one of these URLs up
- 2:33:17here with super simple dev. And we'll
- 2:33:20copy.
- 2:33:22And inside here, we're going to paste.
- 2:33:25And then at the end, instead of
- 2:33:27react.js,
- 2:33:29we're going to load chatbot.js
- 2:33:33and save.
- 2:33:36So now, let's take a look at the code in
- 2:33:38this URL.
- 2:33:40On Windows, you can press control and
- 2:33:42click this link. And on Mac, you can
- 2:33:44press command and click this link.
- 2:33:47If it doesn't open, you can also type
- 2:33:49the URL in the browser.
- 2:33:52So again, an external library is just
- 2:33:55normal JavaScript code that someone else
- 2:33:58wrote. In this case, it's some code that
- 2:34:00I wrote. And this JavaScript code
- 2:34:03creates a simple chatbot.
- 2:34:06This is an object and it has a method
- 2:34:09called get response.
- 2:34:12We just have to give it our message and
- 2:34:15it will return a response.
- 2:34:19So now let's use this external library.
- 2:34:23We'll go back to VS Code
- 2:34:26and we'll scroll down to chat input. And
- 2:34:30now after we send a message below this,
- 2:34:35let's use the chatbot external library
- 2:34:38to get a response. So the external
- 2:34:40library creates a variable called
- 2:34:43capital C chatbot.
- 2:34:47This is an object and it has a method
- 2:34:49called dot get response and brackets.
- 2:34:56Between the brackets, we're just going
- 2:34:58to put the message that we sent, which
- 2:35:00is input text. So in here, we'll type
- 2:35:04input text
- 2:35:07and get response will return a response.
- 2:35:10So let's save it in a variable using
- 2:35:13const and we'll name it response.
- 2:35:16equals this result. And next, let's just
- 2:35:20console.log the response to see what it
- 2:35:23looks like. console.log
- 2:35:26the response
- 2:35:29and save.
- 2:35:32And now let's go to our website
- 2:35:35and we're going to close the external
- 2:35:37library for now. And then we're going to
- 2:35:39click this text box and we'll type the
- 2:35:42message hello and press send.
- 2:35:47And now inside our console it will
- 2:35:50display the response from the chatbot.
- 2:35:52And the chatbot responded with hello how
- 2:35:55can I help you? So using the chatbot
- 2:35:58external library we're able to get a
- 2:36:01response from the chatbot.
- 2:36:05The last step is we're going to display
- 2:36:08this chatbot response on the website.
- 2:36:12Let's go back to VS Code.
- 2:36:16And to display the response on the
- 2:36:18website, we can just add another chat
- 2:36:21message using set chat messages.
- 2:36:25Because this is an updater function from
- 2:36:27the state, React will automatically
- 2:36:30update the website as well. So down here
- 2:36:34we'll remove console.log
- 2:36:38and let's just copy paste this code up
- 2:36:40here to create a new message. So we'll
- 2:36:43copy and then down here we'll paste
- 2:36:49and here the message should be the
- 2:36:52response from the chatbot. So we'll
- 2:36:54replace this with response
- 2:36:58and the sender is going to be the robot
- 2:37:01this time. and we'll give it a unique
- 2:37:04ID.
- 2:37:06So now let's save this code and try it
- 2:37:09out. We'll open the website and then in
- 2:37:12the text box we'll type hello and press
- 2:37:16send.
- 2:37:19And if we look at the website, it sort
- 2:37:22of worked. It displayed the response
- 2:37:24from the chatbot on the website.
- 2:37:28However, it also removed our hello
- 2:37:30message. So why did this happen? Let's
- 2:37:33go back to VS Code.
- 2:37:36And if we look at this code
- 2:37:39here, we updated chat messages by making
- 2:37:43a copy and adding a new message at the
- 2:37:46end. However, in React, state does not
- 2:37:50update immediately. The state is updated
- 2:37:54after all of the code is finished.
- 2:37:59So here all of the code is not finished
- 2:38:02yet. So chat messages is not updated to
- 2:38:06this new array immediately.
- 2:38:09So when we get to this point in the
- 2:38:11code, this chat messages is still the
- 2:38:15old array without the new message that
- 2:38:17we added up here. And then we take the
- 2:38:20old array and we add the chatbots
- 2:38:22response. And we essentially lost our
- 2:38:26message.
- 2:38:28So one way to fix this issue is to save
- 2:38:30this updated chat messages in a
- 2:38:33variable. So above this we're going to
- 2:38:37create a new variable using const and
- 2:38:40let's name it new chat messages
- 2:38:45equals
- 2:38:47and we're going to save this value. So,
- 2:38:49we'll select this code
- 2:38:52and then cut.
- 2:38:54And up here, we're going to paste.
- 2:39:00And we'll set the chat messages to this
- 2:39:02variable, new chat messages.
- 2:39:07So, this does the same thing as before,
- 2:39:10except we saved the new chat messages in
- 2:39:13this variable.
- 2:39:15And then down here, instead of making a
- 2:39:18copy of chat messages, we're going to
- 2:39:20make a copy of new chat messages, which
- 2:39:25contains our new message that we just
- 2:39:28added. So now it will have our message,
- 2:39:31and we're going to add the chatbots
- 2:39:33response. So now let's save this code
- 2:39:38and go to our website.
- 2:39:41In the text box, we're going to type
- 2:39:44hello and press send.
- 2:39:48And it displays our message, hello, and
- 2:39:50the chatbot's response, hello, how can I
- 2:39:53help you?
- 2:39:56Let's try another example. In the text
- 2:39:59box, we can type, "Can you flip a coin?"
- 2:40:05and press send.
- 2:40:08It will add and display our message and
- 2:40:11it will add and display the chatbot's
- 2:40:13response which is sure you got heads or
- 2:40:16tails and everything is working
- 2:40:19correctly.
- 2:40:20So as you can see state is another way
- 2:40:24that React helps us create websites
- 2:40:26easier. All we have to do is update the
- 2:40:30state and React will automatically
- 2:40:32update the website. We don't have to
- 2:40:35update the website manually ourselves.
- 2:40:41And that's it. Using state, we created
- 2:40:44the functionality that we need for this
- 2:40:47project. In the next lesson, we'll style
- 2:40:50this with CSS to make it look like the
- 2:40:53final chatbot project.
- 2:40:56And that's the end of this lesson. In
- 2:40:59this lesson, we saved the data for our
- 2:41:02website using arrays and objects. We
- 2:41:06generated the HTML using map and the key
- 2:41:11prop. We made the website interactive
- 2:41:14using on click and onchange. We learned
- 2:41:18state which is data that changes over
- 2:41:21time and is connected to the HTML. We
- 2:41:25used the updater function to update the
- 2:41:28state and update the HTML at the same
- 2:41:31time. We learned the array destructuring
- 2:41:34shortcut. We learned a technique called
- 2:41:37lifting the state up which lets us share
- 2:41:40state between components. We made the
- 2:41:44chat input component interactive and
- 2:41:47created new chat messages. And we got
- 2:41:50responses from the chatbot.
- 2:41:53Here
- 2:41:55are some exercises to help you practice
- 2:41:57what we learned in this lesson.
- 2:42:46In this lesson, we're going to learn how
- 2:42:48to use CSS with React and we'll finish
- 2:42:52the chatbot project. Let's go back to
- 2:42:55our code
- 2:42:57and we'll start by reviewing CSS.
- 2:43:00CSS is another language and it lets us
- 2:43:04change the appearance of the website. To
- 2:43:07use CSS with React, we just use CSS
- 2:43:11normally. So, one way to use CSS is to
- 2:43:14create a style element.
- 2:43:18So, we'll scroll to the top of our code
- 2:43:21to the head element and then inside
- 2:43:23here, we'll create a style element.
- 2:43:29So, the style element doesn't appear on
- 2:43:31the website. That's why we put it in the
- 2:43:34head section.
- 2:43:36And the special thing about the style
- 2:43:38element is we can write CSS code inside.
- 2:43:42In here we'll type the CSS code button
- 2:43:46and curly brackets. And inside we'll
- 2:43:50type background dash color
- 2:43:55green and semicolon.
- 2:43:57So this is some CSS code. As a review,
- 2:44:01this code at the front is called the CSS
- 2:44:04selector. This tells a computer which
- 2:44:07elements we want to change the
- 2:44:09appearance. And inside we have CSS
- 2:44:13styles. This tells a computer how to
- 2:44:16change the appearance of these elements.
- 2:44:19The left side is called the CSS
- 2:44:22property. This is what we're changing.
- 2:44:25And the right side is called the CSS
- 2:44:27value. This is what we're changing the
- 2:44:29property to. So this code changes all
- 2:44:34buttons on the website and changes their
- 2:44:36background color to green. If we save
- 2:44:41and then check our website,
- 2:44:45you can see that it changed the
- 2:44:46background color of the button to green.
- 2:44:50Let's go back to our code.
- 2:44:53And one problem here is button will
- 2:44:56style all buttons on the website. But
- 2:45:00usually we want to just style a specific
- 2:45:02button. For example, let's scroll down
- 2:45:06to the chat input component and we're
- 2:45:09going to scroll down to this send button
- 2:45:12here. So let's say we just want to style
- 2:45:15this send button. As a review to style a
- 2:45:19specific element, we can add an HTML
- 2:45:22attribute called class. In React to set
- 2:45:26the class attribute, we're going to give
- 2:45:28this element a prop called class capital
- 2:45:33N name equals double quotes.
- 2:45:38And we'll give it the class send dash
- 2:45:41button.
- 2:45:43In React, we use class name to set the
- 2:45:46class. That's because React is actually
- 2:45:49just JavaScript code and JavaScript
- 2:45:52already has a feature called class. So
- 2:45:55class is a reserved word and we can't
- 2:45:58use it. That's why React uses class name
- 2:46:01to set the class.
- 2:46:04Now that this button has the class
- 2:46:06send-button,
- 2:46:08we can style this specific element.
- 2:46:11We'll go to our CSS
- 2:46:15and in here we're going to remove button
- 2:46:18and we're going to type dot and then the
- 2:46:22class send dash button.
- 2:46:26As a review, if we start the CSS
- 2:46:28selector with a dot, it will only style
- 2:46:31elements with this class. So this will
- 2:46:35only style elements with the class
- 2:46:37send-ash button. If we save
- 2:46:42and check our website,
- 2:46:44it styles our send button because we
- 2:46:47gave it the send-button class.
- 2:46:50If we open the console and go to the
- 2:46:52elements tab and then we look at our
- 2:46:55HTML here and we find the send button,
- 2:46:59you'll notice that it has the class
- 2:47:02attribute. So class name in React is
- 2:47:05converted to class in HTML.
- 2:47:10Now that we reviewed CSS, we're going to
- 2:47:13style this button to look like the final
- 2:47:16design. So, first we're going to put the
- 2:47:19final design and our website side by
- 2:47:21side so it's easier to compare. So, I'll
- 2:47:24take the final design here and I'll put
- 2:47:27it on the bottom left and I'll take our
- 2:47:30website and put it in the top left. And
- 2:47:33I'm going to resize our code editor
- 2:47:37so we can see everything side by side.
- 2:47:42And I'll hide the sidebar.
- 2:47:45Next, to style our send button to look
- 2:47:48like the final project, we just have to
- 2:47:51style it one by one with CSS. So, we
- 2:47:55already made the background color green.
- 2:47:58Next, let's make the text color white.
- 2:48:01To do that, we'll type the CSS property
- 2:48:04color colon and set it to white and
- 2:48:09save.
- 2:48:12And that will make the text color white.
- 2:48:16Next, let's make our button bigger. To
- 2:48:19do that, we'll add space on the inside
- 2:48:22of the button. As a review, space on the
- 2:48:26inside of an element is called padding.
- 2:48:29So in our CSS we're going to add padding
- 2:48:34colon and we'll give the padding two
- 2:48:36values 12 px and 20 px.
- 2:48:42So the px here stands for pixels. A
- 2:48:46pixel is a unit of measurement in
- 2:48:48computers. For example, my computer
- 2:48:51screen is about 3,800 pixels wide. So 12
- 2:48:55pixels and 20 pixels looks like this.
- 2:48:59The first value is the vertical space
- 2:49:02inside this button. And the second value
- 2:49:06is the horizontal space inside this
- 2:49:08button. So we're going to add space all
- 2:49:11around the inside of this button. So
- 2:49:14let's save.
- 2:49:16And it adds space on the inside.
- 2:49:19And I think that looks close enough.
- 2:49:22Next, let's add some space on the
- 2:49:25outside of this button on the left. As a
- 2:49:28review, space on the outside of an
- 2:49:31element is called margin.
- 2:49:33So, in our CSS, we'll add margin dash
- 2:49:38and we'll add it on the left colon. And
- 2:49:42let's add 10 pixels of margin. And save.
- 2:49:48And that looks close enough.
- 2:49:51Next, our button has a border around the
- 2:49:54button, but in the final design, there
- 2:49:56is no border. To remove the borders,
- 2:49:59we'll type the property border colon and
- 2:50:03set it to none and save.
- 2:50:08And that will remove the borders.
- 2:50:11Next, the corners of the button in the
- 2:50:14final design are round. To make our
- 2:50:18corners round, we're going to use the
- 2:50:20CSS property border dash radius colon
- 2:50:26and then how round we want the corners
- 2:50:28to be. Let's try 10 pixels and save.
- 2:50:35And I think that looks close enough.
- 2:50:38Next, the text in the button is a little
- 2:50:42bigger in the final design. To change
- 2:50:44the size of the text, we'll use the CSS
- 2:50:47property font dash size colon and let's
- 2:50:52try 15 pixels and save.
- 2:50:58And that looks pretty good. Next, if we
- 2:51:01put our mouse over our button, we no
- 2:51:04longer have the hand pointer icon that
- 2:51:07we see here. to use this hand pointer
- 2:51:10icon. We're going to set the property
- 2:51:13cursor colon pointer
- 2:51:18and save.
- 2:51:21And now if we put our mouse over the
- 2:51:23button, we have the hand pointer icon
- 2:51:25again.
- 2:51:27Finally, the background color of our
- 2:51:29button is a little different from the
- 2:51:32final design. So to get this exact green
- 2:51:35color, we can go to the final project.
- 2:51:38And then we're going to rightclick this
- 2:51:40button and then click inspect.
- 2:51:45And we can click these three dots here
- 2:51:47and click this icon to open the console
- 2:51:51in a different window.
- 2:51:54And we're going to go to the elements
- 2:51:55tab and we'll find the send button in
- 2:51:58the HTML. And we're going to look at
- 2:52:00these styles. So you can see that this
- 2:52:03button here has a background color of
- 2:52:06this RGB value. So we're going to select
- 2:52:09this RGB value and we're going to copy
- 2:52:12it.
- 2:52:14And in our code, instead of the color
- 2:52:17green, we're going to paste this RGB
- 2:52:21color.
- 2:52:22As a review, RGB stands for red, green,
- 2:52:27blue, and it's a way to measure color in
- 2:52:30computers. Computers can create any
- 2:52:33color using a combination of red, green,
- 2:52:36and blue. The first value here is how
- 2:52:39much red we want in the color. The
- 2:52:42second value is how much green we want
- 2:52:44in the color. And the third value is how
- 2:52:47much blue we want. And each value goes
- 2:52:50from 0 to 255.
- 2:52:52So this color combines a little bit of
- 2:52:55red, a lot of green, and a bit of blue
- 2:52:59to get this exact green color. So let's
- 2:53:02save our code.
- 2:53:04And now our button matches the final
- 2:53:07project.
- 2:53:09And that's how we style the send button.
- 2:53:12We just style it one by one using CSS
- 2:53:15until it looks like the final design. So
- 2:53:18this is just a review of CSS. If you
- 2:53:21want to learn CSS in more detail, you
- 2:53:24can check out my HTML and CSS full
- 2:53:26course in the video description.
- 2:53:31Next, we're going to style this text box
- 2:53:34to look like the final design. Again,
- 2:53:37we're just going to style it one by one
- 2:53:39with CSS.
- 2:53:41First, we need to add a class to this
- 2:53:44element so we can select it in our CSS.
- 2:53:47We'll go to our code and we're going to
- 2:53:49scroll down to this text box which is
- 2:53:52inside the chat input component and it's
- 2:53:56this input element here. So to add a
- 2:53:59class we're going to use class name. So
- 2:54:03inside the input element we'll add a
- 2:54:06class name equals double quotes and
- 2:54:10let's give it the class chat dash input.
- 2:54:16Now, let's scroll back up to the CSS.
- 2:54:20And below these styles,
- 2:54:22we're going to type dot to select a
- 2:54:25class. And we're going to select the
- 2:54:27class we just added, which is chat dash
- 2:54:31input and curly brackets. And now we're
- 2:54:35ready to style the text box.
- 2:54:38The first thing we'll change is there's
- 2:54:40a lot of space on the inside of this
- 2:54:42text box in the final design. So again,
- 2:54:46space on the inside of an element is
- 2:54:48called padding.
- 2:54:51In our CSS, we'll add padding colon. And
- 2:54:56let's do 12 pixels for the vertical
- 2:54:59space and 15 pixels for the horizontal
- 2:55:03space. Now let's save.
- 2:55:08And that adds space on the inside of our
- 2:55:10text box. And I think that looks close
- 2:55:13enough. Next, we'll make the corners
- 2:55:16round. To do that, we'll use border dash
- 2:55:21radius colon. And let's try 10 pixels
- 2:55:27and save.
- 2:55:29And that looks pretty good. Next, our
- 2:55:32border is too thick compared to the
- 2:55:35final design. To change the thickness of
- 2:55:38the border, we'll use border dash width
- 2:55:44colon. And let's change it to 1 pixel
- 2:55:48and save.
- 2:55:50So that looks pretty good. Finally, the
- 2:55:53text is bigger in the final design. To
- 2:55:56change the size of the text, we can use
- 2:56:00font dash size colon. And let's use 15
- 2:56:05pixels again. So 15px
- 2:56:08and save.
- 2:56:11And I think that looks good enough for
- 2:56:13now. So we styled the text box one by
- 2:56:17one to look like the final design.
- 2:56:21Next in the final design, the text box
- 2:56:25is wider. And this text box is also
- 2:56:28flexible. That means if we resize the
- 2:56:31browser, the text box will also resize.
- 2:56:36To make our text box flexible, we're
- 2:56:39going to review a feature of CSS called
- 2:56:42flexbox.
- 2:56:43Flexbox lets us create a flexible
- 2:56:46layout. Layout means how the elements
- 2:56:50are positioned on the website.
- 2:56:53Here we have a horizontal layout where
- 2:56:57the elements are beside each other. And
- 2:57:00also this element is flexible. So
- 2:57:03flexbox helps us create this flexible
- 2:57:06layout. To use flexbox we need to do
- 2:57:10three steps. The first step is to create
- 2:57:13a container around the elements that we
- 2:57:16want to position. So let's go back to
- 2:57:18our code and we're going to scroll down
- 2:57:22to the chat input component and we'll
- 2:57:25scroll down to the HTML.
- 2:57:27So here we want to position the text box
- 2:57:30and the button. So we need to create a
- 2:57:34container around these two elements.
- 2:57:37Right now we have a fragment around
- 2:57:40these two elements. However, we cannot
- 2:57:43style a fragment with CSS since
- 2:57:46fragments don't actually appear on the
- 2:57:48website.
- 2:57:50Instead, we're going to switch this
- 2:57:52container to a div element because we
- 2:57:55can style a div element. So, inside
- 2:57:58here, we'll type div and at the end,
- 2:58:02we'll do slash div to convert this
- 2:58:06container to a div element.
- 2:58:09And now we have a container around the
- 2:58:12elements.
- 2:58:14The second step is to add the property
- 2:58:16display flex to the container.
- 2:58:20First, let's add a class name to this
- 2:58:22container so we can select it in our
- 2:58:24CSS. In here, we'll type class name
- 2:58:30equals double quotes and we'll give it
- 2:58:33the class chat-input
- 2:58:36dash container.
- 2:58:39And then we'll scroll up to our CSS.
- 2:58:45And at the end we'll type dot to select
- 2:58:49a class and we'll select the class
- 2:58:52chat-input
- 2:58:54dash container and curly brackets.
- 2:58:59And finally we'll add the property
- 2:59:02display colon flex and that will turn
- 2:59:06this container into a flexbox.
- 2:59:10The third step is to use flexbox
- 2:59:13features. Flexbox has many features we
- 2:59:16can use to control the layout or to
- 2:59:19control how these elements are
- 2:59:21positioned. One feature is called flex
- 2:59:24grow. Flex grow makes an element grow
- 2:59:29and take up the remaining space. So here
- 2:59:32we want this text box to grow and take
- 2:59:36up this remaining space.
- 2:59:39So we'll go to the styles for the chat
- 2:59:41input or the text box
- 2:59:45and we'll add the property flex dash
- 2:59:48grow
- 2:59:501 and save.
- 2:59:54And now the text box will grow and take
- 2:59:58up the remaining space.
- 3:00:00If we resize the browser, the text box
- 3:00:03will also resize to take up the
- 3:00:06remaining space.
- 3:00:08So now we created the layout for the
- 3:00:10chat input that we see in the final
- 3:00:13design.
- 3:00:16Next we'll create the layout for the
- 3:00:18overall app. Notice in the final project
- 3:00:22if we resize the browser and make it
- 3:00:25wider the app is limited to a certain
- 3:00:28width. So it's not going to get wider.
- 3:00:32To create this in our project, we'll set
- 3:00:34a maximum width for our app.
- 3:00:38So, let's go to our code and we're going
- 3:00:41to scroll down to the app component,
- 3:00:44which is down here, and we'll go down to
- 3:00:47the HTML.
- 3:00:49And currently, the app is contained in a
- 3:00:52fragment. And remember, we cannot style
- 3:00:55a fragment since fragments don't
- 3:00:58actually appear on the website.
- 3:01:01So again, we're going to switch this to
- 3:01:03a div element so we can style it. So in
- 3:01:07here, we'll type div and at the bottom
- 3:01:11we'll type /ash div. And now we can
- 3:01:15style this.
- 3:01:17First we'll add a class to this div
- 3:01:20using class name equals double quotes.
- 3:01:25And let's give it the class app dash
- 3:01:29container.
- 3:01:30and we'll scroll up to our CSS.
- 3:01:35And at the bottom here,
- 3:01:37we'll type dot to select a class. And
- 3:01:41we'll select the class app dash
- 3:01:43container and curly brackets.
- 3:01:47And to set a maximum width of our app,
- 3:01:50we can just use the property max dash
- 3:01:54width colon. and let's try 600 pixels
- 3:02:00and save.
- 3:02:03So now if we resize our browser the app
- 3:02:06will be limited to 600 pixels just like
- 3:02:11the final design. Next in the final
- 3:02:14design notice that the app is centered
- 3:02:17on the website. Now there are several
- 3:02:20ways to center an element in CSS.
- 3:02:23One way is to use margin. So we'll go
- 3:02:27back to our code
- 3:02:29and in the app container, we're going to
- 3:02:32add the property margin dash left colon
- 3:02:38and we'll set it to auto. And we'll also
- 3:02:41set the margin dash right colon
- 3:02:46auto.
- 3:02:48So this will automatically add a space
- 3:02:52on the left and right of this element
- 3:02:55until it is centered. So if we save our
- 3:02:58code and check our website, you'll
- 3:03:02notice that the entire app is now
- 3:03:04centered on the website. And now we
- 3:03:07created the layout for the overall app.
- 3:03:12Next, we're going to create the layout
- 3:03:14for the chat messages. If you don't have
- 3:03:17any chat messages in your final project,
- 3:03:20you can add some by typing in here and
- 3:03:23then pressing send. So notice that the
- 3:03:26chat messages from the user are on the
- 3:03:29right while the chat messages from the
- 3:03:31robot are on the left. So we're going to
- 3:03:34create this layout. First, we need to
- 3:03:38give class names to these chat messages
- 3:03:41so we can style them.
- 3:03:44So, let's resize our browser for now.
- 3:03:47And we'll go to our code. And we're
- 3:03:50going to scroll down
- 3:03:52to the chat message component. And we'll
- 3:03:56scroll down to the HTML.
- 3:03:59And we'll give each chat message a class
- 3:04:02name equals. And we're going to do
- 3:04:05something different this time. We'll
- 3:04:08type curly brackets. So here we want to
- 3:04:12give a different class name depending on
- 3:04:15if the sender is a user or the sender is
- 3:04:18a robot. This will let us position the
- 3:04:20chat messages differently on the right
- 3:04:23or on the left. To give this a different
- 3:04:26class name depending on the sender.
- 3:04:28We're going to use a feature of
- 3:04:30JavaScript called the turnary operator.
- 3:04:33As a review, the turnary operator looks
- 3:04:36like this. And it works like an if else
- 3:04:40statement.
- 3:04:42If value one is true, then the result is
- 3:04:45value two. Else the result is value
- 3:04:48three.
- 3:04:50The turnary operator lets us insert an
- 3:04:53if else statement directly in the JSX.
- 3:04:57So using a turnary operator, we can give
- 3:05:00a different class name depending on the
- 3:05:03sender.
- 3:05:05Inside here, we're going to type the if
- 3:05:07condition. So, we'll check if the sender
- 3:05:12is equal to the string user. And then
- 3:05:16we'll type question mark. If the sender
- 3:05:19is the user, we'll set the class name to
- 3:05:22the string chat-
- 3:05:25message dash user.
- 3:05:29And then we'll type a colon. So, this
- 3:05:32means else. So if the sender is not the
- 3:05:35user, we're going to give the class name
- 3:05:38the string chat-
- 3:05:41message dash robot.
- 3:05:45So again the way this code works is if
- 3:05:48the sender is equal to user, we'll set
- 3:05:51the class name to chat message user.
- 3:05:55Else we'll set the class name to chat
- 3:05:57message robot. As you can see, the
- 3:06:00turnary operator lets us insert an if
- 3:06:04else statement directly in the JSX. And
- 3:06:08notice that we used curly brackets here
- 3:06:10because we want to save the result of
- 3:06:13this code into class name instead of
- 3:06:16just a string.
- 3:06:19Before we continue, we can add some new
- 3:06:21lines to this code to make it easier to
- 3:06:23read. So, at the beginning, we can add a
- 3:06:26new line. And then we'll add a new line
- 3:06:29before the question mark and press tab.
- 3:06:33And we'll add a new line before the
- 3:06:35colon. And we'll add a new line at the
- 3:06:38back.
- 3:06:40And remove some spaces so that this is
- 3:06:43vertically aligned.
- 3:06:46So, this makes the code a bit easier to
- 3:06:48read. We have the if condition and then
- 3:06:51the first value and then else the second
- 3:06:54value. Now let's save
- 3:06:58and we'll go to our website and let's
- 3:07:01check the HTML. So in our console, we
- 3:07:04can click these three dots here and then
- 3:07:07click this icon to open the console in
- 3:07:10another window. And we'll go to the
- 3:07:12elements tab and we'll click this
- 3:07:15pointer on the top left and we'll select
- 3:07:18one of these chat messages and click it.
- 3:07:22And if we scroll down here, you can see
- 3:07:24that each chat message now has a class.
- 3:07:28And messages from the user have the
- 3:07:30class chat message user. And messages
- 3:07:33from the robot have the class chat
- 3:07:36message robot.
- 3:07:39So now that we added a class to each of
- 3:07:41these chat messages, let's style these
- 3:07:44chat messages.
- 3:07:46We'll go to our code and we'll scroll up
- 3:07:49to our CSS.
- 3:07:52And at the end, let's type a dot. And
- 3:07:55first let's select the class chat dash
- 3:07:59message dash user and curly brackets.
- 3:08:06And now for chat message user we want to
- 3:08:09position these elements the text and the
- 3:08:12profile image on the right. To create
- 3:08:16this flexible layout we can use flexbox.
- 3:08:19Again
- 3:08:21remember there are three steps to use
- 3:08:23flexbox. First, we already have a
- 3:08:27container around the elements. It has a
- 3:08:30class chat message user or chat message
- 3:08:32robot.
- 3:08:34Next, we need to add display flex to
- 3:08:37this container. So, for chat message
- 3:08:39user, we'll add
- 3:08:42display colon flex.
- 3:08:46And now we can use flexbox features.
- 3:08:51Another feature of flexbox is we're
- 3:08:54going to type justify dashcontent
- 3:08:59colon.
- 3:09:01Justify content controls how the
- 3:09:03elements are displayed horizontally.
- 3:09:08If we want these elements to be
- 3:09:09displayed on the right, we're going to
- 3:09:12give this a value end.
- 3:09:15So it will display these elements at the
- 3:09:17end of this row or on the right.
- 3:09:20So now let's save our code.
- 3:09:24And now the chat messages from the user
- 3:09:26are on the right. And we created the
- 3:09:29layout for the chat messages.
- 3:09:33Next, we're going to style the text in
- 3:09:36the chat messages.
- 3:09:38First, we need to add a class name to
- 3:09:41this text so we can style it.
- 3:09:44Let's go to our code and we're going to
- 3:09:47scroll down to the chat message
- 3:09:50component
- 3:09:52and we're going to scroll to the text
- 3:09:53which is here. So currently this text is
- 3:09:57not inside an element. So first we're
- 3:10:01going to put this text inside an element
- 3:10:04so that we can style just the text.
- 3:10:08Above this, we'll put this in a div by
- 3:10:11typing less than div greater than and
- 3:10:15below it less than slashd greater than.
- 3:10:20And we'll add an indent at the front to
- 3:10:22show that this text is inside this div.
- 3:10:26And now let's add a class to this div so
- 3:10:29we can style just the text. So, we'll
- 3:10:32give this a class name equals double
- 3:10:37quotes. And let's give it the class chat
- 3:10:41dash message dash text.
- 3:10:45So now let's style this with CSS. We'll
- 3:10:49scroll up to our CSS
- 3:10:51and at the end we'll type a dot and
- 3:10:55we'll style the class chat dash message
- 3:10:59dash text and curly brackets.
- 3:11:04So the first thing we'll style is we're
- 3:11:07going to change the background color to
- 3:11:09this gray color. To get this exact
- 3:11:12color, we're going to rightclick on this
- 3:11:14text and then click inspect.
- 3:11:19and we'll find this text in the elements
- 3:11:21tab and we're going to go into the
- 3:11:23styles and we'll get the background
- 3:11:25color. So, we'll copy this RGB color
- 3:11:30and we'll go into our code and we'll set
- 3:11:33the background dash color colon and
- 3:11:38we'll paste the RGB color and save.
- 3:11:43And now we'll have the exact color in
- 3:11:46our final design.
- 3:11:48However, after we saved, it kind of
- 3:11:51messed up our layout. So, the chat
- 3:11:54message user looks okay, but the text
- 3:11:56for chat message robot is now appearing
- 3:11:59below the profile image and it's taking
- 3:12:02up the entire line.
- 3:12:04So, what happened? Remember that we just
- 3:12:07surrounded this text in a div element.
- 3:12:11Normally a div element takes up an
- 3:12:14entire line by itself because a div is a
- 3:12:18block element.
- 3:12:20However, remember that we also made chat
- 3:12:23message user a flexbox. When a block
- 3:12:27element is inside a flexbox, it no
- 3:12:30longer takes up the entire line. It only
- 3:12:33takes up as much space as it needs to.
- 3:12:36So that's why this div only takes up as
- 3:12:39much space as it needs because it's
- 3:12:42inside a flexbox.
- 3:12:45One way to fix this layout problem is we
- 3:12:48can make chat message robot a flexbox as
- 3:12:52well. We'll go back to our code and
- 3:12:55below chat message user let's type dot
- 3:12:59and we'll style the chat- message dash
- 3:13:04robot and curly brackets
- 3:13:08and we'll also give it display colon
- 3:13:12flex
- 3:13:14so it will match the layout for the chat
- 3:13:16message user.
- 3:13:18Let's save.
- 3:13:21And now because this div is inside a
- 3:13:24flexbox, it only takes up as much space
- 3:13:27as it needs. So now the background color
- 3:13:31and the layout looks good. Next, the
- 3:13:35style of text or the font is different
- 3:13:38in the final design.
- 3:13:41To change the font, we'll go to our code
- 3:13:44and we'll add the property font dash
- 3:13:48family colon And in the final project, I
- 3:13:52use the font Ariel.
- 3:13:56Let's save.
- 3:13:58And now our font matches the final
- 3:14:01design.
- 3:14:02Now, usually we don't set the font for
- 3:14:06just one element. We set the font for
- 3:14:08the entire website because we want the
- 3:14:12entire website to use the same font or
- 3:14:14the same style of text. to set the font
- 3:14:18for the entire website. We can style the
- 3:14:21body element. So, we'll scroll to the
- 3:14:24top of our CSS.
- 3:14:27And here we'll style the body element
- 3:14:31curly brackets and we'll set the font
- 3:14:35dash family colon to area.
- 3:14:40So this sets the font for all elements
- 3:14:44inside the body, which is basically the
- 3:14:47entire website. So now we can scroll
- 3:14:50back down
- 3:14:52and we can remove this style
- 3:14:55because we're going to set the font for
- 3:14:57the entire website instead.
- 3:15:00Let's save.
- 3:15:02And now we set the font for the entire
- 3:15:05website to Ariel.
- 3:15:08Next, let's add space on the inside of
- 3:15:11this text, which is padding.
- 3:15:15So, here we'll add padding
- 3:15:19colon. And let's try 15 pixels of
- 3:15:23vertical space and 20 pixels of
- 3:15:26horizontal space. And save.
- 3:15:31And that looks pretty good.
- 3:15:34Next, we'll make the corners round.
- 3:15:37To do that, we'll use border dash radius
- 3:15:42colon and 10 pixels and save.
- 3:15:48And that looks pretty good. Next, let's
- 3:15:51add space on the outside of the text on
- 3:15:54the right. So, we'll add margin dash
- 3:15:59right colon and let's try 10 pixels and
- 3:16:05save.
- 3:16:07And that looks close enough.
- 3:16:10And here, if we scroll down to a chat
- 3:16:13message from the robot, we'll add some
- 3:16:16space on the left of the text. So, in
- 3:16:19our code, we'll add margin dash left.
- 3:16:24And let's try 10 pixels and save.
- 3:16:30And that looks pretty good. Finally,
- 3:16:34let's add some space on the bottom of
- 3:16:36the text so we can separate them. Here
- 3:16:39we'll add margin dash bottom colon. And
- 3:16:46let's try 20 pixels and save.
- 3:16:51And I think that looks close enough.
- 3:16:54However, we now have a problem here,
- 3:16:57which is the profile image seems to be
- 3:17:00stretching.
- 3:17:02This happens because this container is a
- 3:17:05flexbox. And by default, elements inside
- 3:17:09a flexbox will stretch vertically.
- 3:17:13So, if this text gets longer because we
- 3:17:15added some margin, this image will
- 3:17:18stretch.
- 3:17:20To fix this, flexbox has another feature
- 3:17:23called align items.
- 3:17:26So in our CSS in chat message user,
- 3:17:30we'll add the property align dash items
- 3:17:35colon
- 3:17:36and align items controls how the
- 3:17:39elements are displayed vertically. By
- 3:17:42default, align items is stretch. That's
- 3:17:46why the elements stretch vertically.
- 3:17:49Instead, we're going to change align
- 3:17:51items to start.
- 3:17:55This will display the elements at the
- 3:17:57start or at the top vertically, and it
- 3:18:00will no longer stretch. Same thing for
- 3:18:03the chat message robot. We're going to
- 3:18:05give it align dash items colon start and
- 3:18:12save.
- 3:18:14And now the elements in the flexbox will
- 3:18:17appear at the start or at the top
- 3:18:19vertically.
- 3:18:21The last thing we'll style for the text
- 3:18:24is if we go to our final project and
- 3:18:27inside the text box we type test and
- 3:18:31press send.
- 3:18:34You'll notice that the response from the
- 3:18:36chatbot has a maximum width. So in our
- 3:18:40project, we're also going to add a
- 3:18:42maximum width to our text. So in our
- 3:18:46code, we'll go to the chat message text
- 3:18:49and let's add a max dash width colon.
- 3:18:54And let's try 300 pixels and save.
- 3:19:00And now if we go to our text box and
- 3:19:02type test and press send and scroll
- 3:19:07down, the text has a maximum width and
- 3:19:11it matches the final design.
- 3:19:14Now we finish styling the text in the
- 3:19:17chat messages.
- 3:19:20Next, we'll style the profile images. So
- 3:19:23in our code, let's scroll down to the
- 3:19:26profile image which is inside the chat
- 3:19:30message component
- 3:19:32and it's these two image elements here.
- 3:19:37Currently, we're using an attribute to
- 3:19:39set the width of this image. However, a
- 3:19:43best practice is to use CSS for styling
- 3:19:46instead of attributes. So let's remove
- 3:19:50this attribute and we'll remove it down
- 3:19:53here as well and we'll add a class to
- 3:19:56these profile images so that we can
- 3:19:59style them with CSS. So here we'll add
- 3:20:03class name equals double quotes and
- 3:20:07we'll give it the class chat-
- 3:20:10message dash profile.
- 3:20:14And we'll also add this class to the
- 3:20:16profile image for the user.
- 3:20:19We'll give it the class name equals
- 3:20:23double quotes
- 3:20:25chat dash message dash profile.
- 3:20:30So now we can style this with our CSS.
- 3:20:33We'll scroll up to our CSS
- 3:20:37and at the end we'll type a dot and
- 3:20:41we'll style the class chat dash message
- 3:20:45dash profile and curly brackets.
- 3:20:50So previously we set the width to 50
- 3:20:53pixels. So let's do that again using
- 3:20:56CSS. This time we'll set the width colon
- 3:21:01to 50 pixels.
- 3:21:03and save.
- 3:21:07Now, if we look at the final design,
- 3:21:11it looks like the profile images here
- 3:21:13are a little bit smaller than our
- 3:21:16profile images. So, let's make this
- 3:21:18width smaller. Let's try 45 pixels
- 3:21:22instead.
- 3:21:23And save.
- 3:21:26And I think that looks close enough. And
- 3:21:29now our profile images are the same size
- 3:21:32as the final design.
- 3:21:35The last thing we're going to change in
- 3:21:37this design is we're going to move the
- 3:21:39text box to the bottom so that it
- 3:21:41matches the design of other popular
- 3:21:44chatbots.
- 3:21:45In the final project, we're going to
- 3:21:48make this a little taller. Now, we can
- 3:21:51move this text box to the bottom of the
- 3:21:54page by clicking this link.
- 3:21:58And now let's create this design in our
- 3:22:01project.
- 3:22:02First I'll open our project and then
- 3:22:05move it back to the same window so that
- 3:22:08we can see this side by side a little
- 3:22:10easier. And to put the text box at the
- 3:22:14bottom of the page, the first step is to
- 3:22:16move the text box below the chat
- 3:22:19messages. We'll go to our code and we're
- 3:22:22going to scroll down to the app
- 3:22:25component
- 3:22:27which is here and we'll scroll to the
- 3:22:30HTML and we're just going to move this
- 3:22:33chat input below the chat messages. So
- 3:22:36we'll select this component and we'll
- 3:22:39cut
- 3:22:41and below chat messages we'll paste
- 3:22:46and save.
- 3:22:49And now the chat input will be below the
- 3:22:52chat messages. And that looks pretty
- 3:22:54good. Next, we also want to put this
- 3:22:57chat input at the bottom of the browser
- 3:23:00here. So there are several ways to do
- 3:23:03this in CSS. One way is to use flexbox
- 3:23:07again. Now before we use flexbox, we're
- 3:23:11going to make the app container stretch
- 3:23:14to the bottom of the browser. So right
- 3:23:17now if we click inside here and we
- 3:23:20rightclick and inspect
- 3:23:24and we go to the elements tab and we
- 3:23:27scroll up and look at the app container.
- 3:23:31Notice that the app container stops at
- 3:23:34the chat input section. And that's
- 3:23:36because this is all the content that we
- 3:23:38have. So the app container only has the
- 3:23:41height that it needs. However, we want
- 3:23:44to stretch the app container to the
- 3:23:46bottom of the browser so that we can put
- 3:23:49the chat input at the bottom. To do
- 3:23:52that, we're going to set the height of
- 3:23:54the app container. So, we'll go back to
- 3:23:57our code
- 3:23:59and we'll scroll up to our CSS
- 3:24:04and we're going to find the styles for
- 3:24:06the app container which is right here.
- 3:24:09And at the end, we're going to set the
- 3:24:11height colon.
- 3:24:15And now we want the height of the app
- 3:24:17container to stretch to be the same as
- 3:24:20the height of the browser here. To do
- 3:24:24that, we're going to give this a special
- 3:24:26value 100 VH.
- 3:24:31So VH stands for viewport height. This
- 3:24:35gives us the height of the browser or
- 3:24:38the part of the page that is visible.
- 3:24:41100 VH means 100% of the height of the
- 3:24:46browser. So this means the app container
- 3:24:49will be 100% of this height.
- 3:24:55So now let's save
- 3:24:58and we'll go to our website and we'll
- 3:25:00open the console again by right-clicking
- 3:25:04and inspect
- 3:25:06and go to the elements tab and we'll
- 3:25:08scroll up to the app container again.
- 3:25:12And now you'll notice the app container
- 3:25:14stretches to the bottom of the browser.
- 3:25:18However, the app container is actually a
- 3:25:21little too tall. So it actually
- 3:25:23stretches below the bottom of the page.
- 3:25:26And now we can scroll a bit here. The
- 3:25:29reason it's a little bit too tall is
- 3:25:31because by default the body element here
- 3:25:35has a default margin of 8 pixels on all
- 3:25:39sides. So the app container is the same
- 3:25:42height as the browser, but then we add
- 3:25:45another 8 pixels on the top and bottom.
- 3:25:50To fix this issue, we can just remove
- 3:25:52the default margin from the body. So,
- 3:25:55we'll go back to our code
- 3:25:58and scroll up to the styles for the
- 3:26:01body. And at the end, let's remove the
- 3:26:05margin at the top and bottom by typing
- 3:26:08margin- top colon. And we'll set it to
- 3:26:12zero pixels. And we'll also set the
- 3:26:16margin dashbottom
- 3:26:19colon to zero pixels and save.
- 3:26:26So now after we remove the default
- 3:26:28margin from the top and the bottom,
- 3:26:33the app container is now exactly the
- 3:26:36same height as the browser.
- 3:26:40Now that the app container stretches to
- 3:26:42the bottom of the browser, we can use a
- 3:26:45flexbox to make the chat messages grow
- 3:26:48and push the chat input to the bottom.
- 3:26:52Again, there are three steps to use a
- 3:26:55flexbox.
- 3:26:56So here we want to position the chat
- 3:26:59messages and the chat input. So we need
- 3:27:03a container around these elements and we
- 3:27:06already have a container for this which
- 3:27:08is the app container.
- 3:27:11The second step is to add display flex
- 3:27:14to the container. So we'll go back to
- 3:27:16our code and we're going to scroll to
- 3:27:19the app container styles which is right
- 3:27:22here and below this we'll add display
- 3:27:27colon flex. Let's save our code.
- 3:27:34And unfortunately, it totally messed up
- 3:27:36our design. So what happened? By
- 3:27:40default, flexbox positions our elements
- 3:27:42horizontally. So it takes all the
- 3:27:45elements inside the container and then
- 3:27:48squeezes them into a row like we see
- 3:27:50here.
- 3:27:52However, this time we want to position
- 3:27:54the elements vertically, not
- 3:27:57horizontally.
- 3:27:58To position the elements vertically,
- 3:28:01we're going to use a flexbox feature
- 3:28:03called flex direction.
- 3:28:07So in here, we'll type flex dash
- 3:28:11direction. colon
- 3:28:14flex direction determines which
- 3:28:17direction the elements are positioned.
- 3:28:20So flex direction is row by default.
- 3:28:23That means it positions the elements
- 3:28:26horizontally like we see here.
- 3:28:29Now we're going to change flex direction
- 3:28:31to column.
- 3:28:34So this positions the elements
- 3:28:36vertically in a column. If we save our
- 3:28:40code
- 3:28:41and check the website, the elements in
- 3:28:44the flexbox are now positioned
- 3:28:46vertically from top to bottom.
- 3:28:50Next, we're going to use flex grow.
- 3:28:52Again, as a review, flex grow makes an
- 3:28:56element grow and take up the remaining
- 3:28:59space.
- 3:29:01Here, we want to make the chat messages
- 3:29:04section grow and take up all the
- 3:29:06remaining vertical space. And this will
- 3:29:09push the chat input down to the bottom.
- 3:29:13To use Flex Grow, we're going to add a
- 3:29:15class to the chat messages section.
- 3:29:19We'll go to our code and we'll scroll
- 3:29:21down
- 3:29:23to the chat messages component which is
- 3:29:26right here. And currently this component
- 3:29:29uses a fragment. And remember, we cannot
- 3:29:33style a fragment since fragments don't
- 3:29:36appear on the website. So again, we're
- 3:29:39going to switch this to a div element.
- 3:29:42and at the end slash div so that we can
- 3:29:47style it.
- 3:29:49And now we'll give this div a class
- 3:29:52using class name equals double quotes
- 3:29:57and we'll give it the class chat dash
- 3:30:00messages dash container.
- 3:30:05And now let's style this class. We'll
- 3:30:07scroll up in our CSS
- 3:30:10and at the end here we'll type dot and
- 3:30:15we'll style the class chat dash messages
- 3:30:19dash container
- 3:30:22and curly brackets
- 3:30:24and we'll use the flexbox feature flex
- 3:30:28dash grow
- 3:30:311.
- 3:30:32So now let's save.
- 3:30:36And this makes the chat message
- 3:30:38container grow and take up the remaining
- 3:30:41vertical space. And this will push the
- 3:30:43chat input to the bottom of the browser.
- 3:30:47So that's how we use a flexbox to
- 3:30:49position the chat input at the bottom.
- 3:30:52Finally, if we look at the final design,
- 3:30:56we have some space at the top of the app
- 3:30:59and at the bottom of the app. So let's
- 3:31:02add that space at the top and bottom to
- 3:31:04our website.
- 3:31:06We'll go to our CSS
- 3:31:09and currently the app container has a
- 3:31:12special height of 100 VH. So let's avoid
- 3:31:16changing this. Instead, let's add space
- 3:31:19above the Chad messages container. So,
- 3:31:22we'll scroll down to the chat messages
- 3:31:25container and we'll add some space at
- 3:31:28the top using margin dash top colon 20
- 3:31:34pixels.
- 3:31:36And we'll also add some space below the
- 3:31:38chat input container. So, we'll scroll
- 3:31:41up to the chat input container which is
- 3:31:45right here. And we'll add space at the
- 3:31:48bottom using margin dash bottom colon.
- 3:31:53And let's try 60 pixels and save.
- 3:32:00And now that looks pretty good. And now
- 3:32:03if we look at our website and compare it
- 3:32:05to the final design,
- 3:32:07you can see that it looks the same as
- 3:32:09the final design. So, we finished
- 3:32:12styling our project to look like the
- 3:32:14final design using CSS.
- 3:32:19Next, we're going to make the chat
- 3:32:21messages scrollable. In the final
- 3:32:24project, if we have more chat messages
- 3:32:27than we can see in the browser, we can
- 3:32:30actually scroll to see all of the chat
- 3:32:32messages. Now, you may need to add more
- 3:32:35chat messages down here in order to see
- 3:32:38this feature. So, we're going to create
- 3:32:40this feature in our project. If we go to
- 3:32:44our project and we add some more
- 3:32:47messages like test and press send,
- 3:32:52notice if we have too many messages, it
- 3:32:55starts to push the chat input down.
- 3:32:58To make our chat messages scrollable,
- 3:33:01we're going to go to the CSS and we're
- 3:33:04going to find the chat messages
- 3:33:05container, which is right here.
- 3:33:09And at the end we'll add a property
- 3:33:12overflow colon scroll.
- 3:33:16So now if the chat messages overflow or
- 3:33:19there's too many to be displayed, it
- 3:33:22will start to scroll instead of pushing
- 3:33:24things down. Let's save our code
- 3:33:29and then add another message test and
- 3:33:33press send.
- 3:33:35And you may need to add a few more
- 3:33:36messages to see this feature. Now, if we
- 3:33:39have too many messages, we're able to
- 3:33:42scroll up and down to see all the
- 3:33:44messages.
- 3:33:46Now, before we continue on Windows, we
- 3:33:49have a small problem, which is we start
- 3:33:52to see these thick scroll bars on the
- 3:33:54sides, which doesn't look very good. To
- 3:33:57hide these scroll bars, we can go back
- 3:33:59to our CSS and at the end we can use the
- 3:34:03property scroll bar dash width colon
- 3:34:09nut.
- 3:34:11So now let's save.
- 3:34:15And if we add a message test and press
- 3:34:18send and you might need to add more
- 3:34:21messages for your project. Now, if we
- 3:34:24have more messages than we can see, we
- 3:34:27can scroll and we hide the scroll bars.
- 3:34:31So, that's how we make the chat messages
- 3:34:33scrollable.
- 3:34:37The last feature we're going to add to
- 3:34:39our project is auto scrolling. For
- 3:34:42example, if we go down here and add the
- 3:34:46message test and press send,
- 3:34:50notice we have to manually scroll down
- 3:34:52to the bottom ourselves. But if we go to
- 3:34:55the final project and at the bottom add
- 3:34:59a message test and press send,
- 3:35:04notice that it automatically scrolls to
- 3:35:07the bottom of the chat messages. So,
- 3:35:09we're going to add this autoscrolling
- 3:35:12feature.
- 3:35:13Let's go back to our website.
- 3:35:16And to create autoscrolling, we're going
- 3:35:19to learn one of the most important
- 3:35:21features of React called hooks.
- 3:35:25Hooks let us insert React features into
- 3:35:29our component. And we've actually used
- 3:35:32hooks before. React.US
- 3:35:35state is a hook. It inserts a React
- 3:35:39feature called state into our component
- 3:35:42and state automatically updates the HTML
- 3:35:46when the data changes. Now, React has
- 3:35:50other hooks that we can use and every
- 3:35:52hook starts with a word use.
- 3:35:56Another hook is called use effect. Use
- 3:36:00effect lets us run some code after the
- 3:36:03component is created or updated.
- 3:36:07So in our project after we add a chat
- 3:36:10message we want to run some code to
- 3:36:13scroll to the bottom. So this is a
- 3:36:16perfect situation for use effect.
- 3:36:20Inside our code we're going to scroll
- 3:36:22down to the chat messages component
- 3:36:27which is here.
- 3:36:30And at the top of the component, we're
- 3:36:32going to type the code capital R react
- 3:36:36dot use capital E effect
- 3:36:41and brackets.
- 3:36:43By the way, in React, we should put our
- 3:36:46hooks at the top of the component and
- 3:36:48they should not be inside anything. So,
- 3:36:51don't put a hook inside an if statement
- 3:36:53or inside a function.
- 3:36:57Next, we'll give use effect a function.
- 3:37:01So, inside these brackets, we'll type a
- 3:37:03function using brackets, arrow, and
- 3:37:07curly brackets.
- 3:37:09As a reminder, when we give a function
- 3:37:11to another function, we usually use an
- 3:37:14arrow function because it's a bit
- 3:37:16cleaner. So, React will run this
- 3:37:19function after the component is created
- 3:37:23and every time the component is updated
- 3:37:28inside here. Let's try console.log
- 3:37:33the string updated
- 3:37:36and save.
- 3:37:39And now if we go to our project and we
- 3:37:42look at the console. So we'll rightclick
- 3:37:45inspect
- 3:37:46and we go to the console.
- 3:37:50You'll see that after the component is
- 3:37:52created, it runs a function and displays
- 3:37:55updated. And then we're going to go to
- 3:37:58our website and we're going to add a new
- 3:38:00message like hello and press send.
- 3:38:05So now after the component is updated,
- 3:38:09React runs the function again. So, as
- 3:38:12you can see, use effect lets us run some
- 3:38:15code after the component is created or
- 3:38:19updated.
- 3:38:21So, now let's go back into our code
- 3:38:24and we'll give use effect a second
- 3:38:27parameter. So, at the end we'll type a
- 3:38:30comma and we'll give a second parameter
- 3:38:33which is an array.
- 3:38:35So, this array controls when use effect
- 3:38:39runs. If we give it an empty array, use
- 3:38:43effect will only run once after the
- 3:38:46component is created.
- 3:38:49Now, inside this array, we can also put
- 3:38:52some data such as our chat messages. So,
- 3:38:56we'll type chat messages.
- 3:39:00And now, React will also run this
- 3:39:02function every time our data changes or
- 3:39:05every time our chat messages changes.
- 3:39:08If we save
- 3:39:11and we look at the console,
- 3:39:14use effect runs after the component is
- 3:39:17created. And now if we add a message
- 3:39:20like hello and press send,
- 3:39:24use effect also runs after chat messages
- 3:39:27changes.
- 3:39:29So now let's go back to our code.
- 3:39:33And this is called a dependency array.
- 3:39:36It lets us control when use effect runs.
- 3:39:41A best practice is to give use effect a
- 3:39:44dependency array to avoid running this
- 3:39:47function too often.
- 3:39:50So now we learned hooks and the use
- 3:39:52effect hook. And now we can run some
- 3:39:55code after the chat messages changes.
- 3:40:00The last step is after the chat messages
- 3:40:04changes, we want to automatically scroll
- 3:40:07to the bottom. To scroll to the bottom,
- 3:40:11first we need to get the HTML element
- 3:40:14that we want to scroll into our
- 3:40:16JavaScript.
- 3:40:18Let's go back to our code. And inside
- 3:40:21this function, instead of console.log,
- 3:40:23log we need to get this div chat
- 3:40:26messages container into our JavaScript.
- 3:40:30Now in normal JavaScript we might use
- 3:40:33the DOM to get this element but remember
- 3:40:36in React we should not use the DOM
- 3:40:39manually. Instead we should use React
- 3:40:42features to get this element
- 3:40:45to get this element using React. We're
- 3:40:48going to use another hook called use
- 3:40:52ref. So at the top of this component,
- 3:40:56we're going to type the code capital R
- 3:40:58react dot use capital R ref and
- 3:41:04brackets.
- 3:41:06Use ref lets us automatically save an
- 3:41:10HTML element from the component.
- 3:41:13So this code creates a ref. A ref is a
- 3:41:19container with special React features.
- 3:41:22One special feature is we can give this
- 3:41:25ref or this container to React and React
- 3:41:29can automatically save an HTML element
- 3:41:33inside this ref or inside this
- 3:41:36container.
- 3:41:37So between these brackets, we're first
- 3:41:40going to put the value null.
- 3:41:43So this is the initial value that is
- 3:41:46saved inside this ref or inside this
- 3:41:49container. So the ref is going to start
- 3:41:52out as empty. So that's why we use null.
- 3:41:56Now let's save an HTML element inside
- 3:42:00this ref. To do that, first we're going
- 3:42:04to save this ref in a variable. So at
- 3:42:07the front we'll create a variable using
- 3:42:10const and we'll name it chat messages
- 3:42:15ref equals this result.
- 3:42:19And then we're going to go to the HTML
- 3:42:22element we want to save, which is this
- 3:42:24div, the chat messages container.
- 3:42:29And we're going to give this element a
- 3:42:31special prop called ref equals curly
- 3:42:35brackets. And between the curly
- 3:42:38brackets, we're going to give it the ref
- 3:42:40that we created above, which is chat
- 3:42:44messages ref.
- 3:42:48So now React will take this HTML element
- 3:42:52and save it inside this ref or inside
- 3:42:55this container.
- 3:42:58To access the HTML element saved inside
- 3:43:01a ref, we're going to go to use effect
- 3:43:05and we're going to type the name of the
- 3:43:07ref, which is chat messages ref and then
- 3:43:14current.
- 3:43:16Now the reason we put this code in use
- 3:43:18effect is because remember use effect
- 3:43:21runs after the component is created. So
- 3:43:25we need to wait for the component to be
- 3:43:27created first and then we can access
- 3:43:30this HTML element.
- 3:43:33So now let's console.log this value to
- 3:43:37see what's inside this ref. So at the
- 3:43:40front we'll console.log log open bracket
- 3:43:45and close bracket and save.
- 3:43:50And we'll open the console for our
- 3:43:52website.
- 3:43:54And you'll see that React saved the chat
- 3:43:57messages container inside our ref.
- 3:44:01Now that we have the HTML element, we
- 3:44:04need to scroll. Let's learn how to
- 3:44:06scroll it to the bottom. Let's go back
- 3:44:09to our code
- 3:44:11and first we're going to save this HTML
- 3:44:14element in a variable. So we'll remove
- 3:44:17this and at the front we'll create a
- 3:44:20variable const and let's name it
- 3:44:24container
- 3:44:26lm equals this result.
- 3:44:30So ending the variable name with lm
- 3:44:33tells us that this variable contains an
- 3:44:36HTML element.
- 3:44:39and I'll resize my code editor so we can
- 3:44:41see more of the code.
- 3:44:44Next, we'll create a new line. And it's
- 3:44:47a good idea to check if this element
- 3:44:49actually exists before we scroll it. So,
- 3:44:53we'll check if brackets container lm.
- 3:44:59Finally, we'll type curly brackets.
- 3:45:03And to scroll this element to the
- 3:45:05bottom, we can use this code container
- 3:45:09lm dot scroll top and we'll make it
- 3:45:14equal container lm dot scroll height.
- 3:45:22So scroll top means how far from the top
- 3:45:26should we scroll and scroll height gives
- 3:45:29us the total height of the element. So,
- 3:45:32if we set the scroll top to the total
- 3:45:34height, it's going to scroll all the way
- 3:45:36down to the bottom, which is exactly
- 3:45:39what we need. So, now let's save this
- 3:45:42code
- 3:45:44and we'll go to our website and we'll
- 3:45:47add a message test and press send.
- 3:45:52It runs the use effect function because
- 3:45:54chat messages changed and it
- 3:45:57automatically scrolls to the bottom.
- 3:46:00Now, depending on the height of your
- 3:46:01browser, you might need to add a few
- 3:46:03more messages to see this effect. So, we
- 3:46:07can also scroll up and then add another
- 3:46:10message like thank you
- 3:46:13and press send.
- 3:46:16It runs use effect again and it
- 3:46:19automatically scrolls to the bottom. And
- 3:46:22that's all the major features that we
- 3:46:24need for this project. I'll leave some
- 3:46:27extra features like removing these
- 3:46:30default messages and creating the
- 3:46:32loading spinner as exercises.
- 3:46:37And that's it. We created all the
- 3:46:39styling and features that we need and we
- 3:46:42finished the chatbot project. In the
- 3:46:45next lesson, we'll learn how to do a
- 3:46:47proper React setup so we can organize
- 3:46:50our code into different files and
- 3:46:52folders instead of putting everything
- 3:46:55into one HTML file. And this will help
- 3:46:58us work on bigger React projects.
- 3:47:02And that's the end of this lesson. In
- 3:47:04this lesson, we learned how to use CSS
- 3:47:07with React. We styled the chatbot
- 3:47:11project. We reviewed flexbox which lets
- 3:47:15us create a flexible layout. We learned
- 3:47:18the turnary operator which lets us
- 3:47:21insert an if else statement directly in
- 3:47:24the JSX. We learned hooks which lets us
- 3:47:28insert React features into our
- 3:47:30components. We learned the use effect
- 3:47:34hook which lets us run some code after
- 3:47:37the component is created or updated. We
- 3:47:40learned the use ref hook which lets us
- 3:47:43automatically save an HTML element from
- 3:47:47the component
- 3:47:49and we created the auto scroll feature
- 3:47:52for the chat messages.
- 3:47:56Here are some exercises to help you
- 3:47:58practice what we learned in this lesson.
- 3:48:53In this lesson, we're going to learn how
- 3:48:55to do a proper React setup. Right now,
- 3:49:00all of our HTML, CSS, and JavaScript
- 3:49:02code is combined into one HTML file,
- 3:49:07chatbot.html.
- 3:49:09Now, it's not good to combine all of our
- 3:49:11code in three different languages into
- 3:49:14one file because if we work on bigger
- 3:49:16projects, this file might contain
- 3:49:19thousands of lines of code, and it will
- 3:49:21be almost impossible to manage all of
- 3:49:23that code if we put it all in one file.
- 3:49:27Instead, we're going to do a proper
- 3:49:29React setup that will let us split up
- 3:49:32this code into different files and
- 3:49:35folders.
- 3:49:36To create this React setup, we'll need
- 3:49:39to use the command line. So, first let's
- 3:49:42review the command line. We're going to
- 3:49:45go to VS Code and I'm going to make this
- 3:49:48full screen.
- 3:49:50And to open the command line, we're
- 3:49:52going to click the menu at the top and
- 3:49:55we're going to click the terminal menu
- 3:49:57and we're going to open a new terminal.
- 3:50:03So here we can give an instruction or a
- 3:50:07command to the computer. For example,
- 3:50:10we're going to type the command mkdir.
- 3:50:16mkdir means make directory. A directory
- 3:50:20is another name for a folder. So make
- 3:50:24directory creates a new folder.
- 3:50:28Then we'll type a space and we'll give
- 3:50:31this folder a name like test.
- 3:50:35If we press enter,
- 3:50:37it creates a new folder named test. So
- 3:50:40if we open our files on the left, you'll
- 3:50:43see that it created a new folder named
- 3:50:45test.
- 3:50:47If that does not work for you, you can
- 3:50:49check the troubleshooting steps in the
- 3:50:51description.
- 3:50:54So on this line here we can give
- 3:50:57instructions or commands to our computer
- 3:51:00and that's why this is called the
- 3:51:02command line
- 3:51:04and a terminal basically lets us use the
- 3:51:07command line and it also lets us view
- 3:51:09the output of the command line up here.
- 3:51:13So we created a new folder using this
- 3:51:16command. Now you might be wondering why
- 3:51:18did the folder get created here inside
- 3:51:21our project folder. As a review, the
- 3:51:25command line runs inside a specific
- 3:51:27folder.
- 3:51:29To see which folder the command line is
- 3:51:32currently running in, we can go to the
- 3:51:35bottom and type this command pwd or
- 3:51:40print working directory and press enter.
- 3:51:45And this command tells us the command
- 3:51:47line is currently running in the
- 3:51:50react-course folder which is our project
- 3:51:53folder right here. That means any
- 3:51:56commands that we give to the computer
- 3:51:58will be done inside this folder. If we
- 3:52:02make a directory, it will create the new
- 3:52:05directory inside this folder like we see
- 3:52:08here. If we give it another command like
- 3:52:11ls or list and press enter,
- 3:52:16it will list everything inside this
- 3:52:19folder. So it will list everything here.
- 3:52:22As you can see, the command line runs
- 3:52:25inside a specific folder.
- 3:52:28Now this folder that the command line is
- 3:52:31running in is called the working
- 3:52:34directory or the working folder.
- 3:52:37The last thing we need to review about
- 3:52:39the command line is how to change the
- 3:52:42working directory. To do that, we're
- 3:52:45going to go to the bottom and we're
- 3:52:47going to type the command cd or change
- 3:52:50directory and then press space and then
- 3:52:54the name of the folder we want to change
- 3:52:56to like test.
- 3:53:00the command line will look for this
- 3:53:01folder in the working directory which is
- 3:53:04here and then it's going to go into this
- 3:53:06folder. So now if we press enter and
- 3:53:11then type print working directory and
- 3:53:13press enter
- 3:53:15it will tell us the command line is now
- 3:53:17running inside the test folder.
- 3:53:21So the command line basically went
- 3:53:23inside this test folder and it's running
- 3:53:26inside here. By the way, this is known
- 3:53:29as a file path. The slash here means
- 3:53:33that the test folder is inside the
- 3:53:35react-course folder.
- 3:53:38So now if we give a command like ls and
- 3:53:41press enter,
- 3:53:43it will list everything inside the test
- 3:53:46folder because the command line is
- 3:53:48running inside this test folder and it
- 3:53:51tells us there's nothing inside the test
- 3:53:53folder.
- 3:53:55Finally, to get out of the test folder,
- 3:53:58we can type cd or change directory and
- 3:54:02then space and use the special value dot
- 3:54:07dot. Dot dot represents the outer
- 3:54:10folder.
- 3:54:12If we press enter,
- 3:54:14the command line will go out of the test
- 3:54:17folder and back into the react-course
- 3:54:20folder.
- 3:54:23If we type the command pwd or print
- 3:54:26working directory and press enter,
- 3:54:30it will tell us the command line is
- 3:54:32running in the react-ourse folder again.
- 3:54:36So using cd or change directory, we can
- 3:54:39change which folder the command line is
- 3:54:42running in and we can basically move
- 3:54:44around our computer. So this is just a
- 3:54:47review of the command line.
- 3:54:51The second thing we need for the React
- 3:54:53setup is NodeJS.
- 3:54:56So, we're going to review NodeJS.
- 3:55:00First, make sure you have the correct
- 3:55:02version of NodeJS installed. To do that,
- 3:55:05you can click this link in the
- 3:55:07description.
- 3:55:09Scroll down to lesson 5 and follow the
- 3:55:12instructions here to install the correct
- 3:55:14version of NodeJS.
- 3:55:17After Node.js is installed, let's go
- 3:55:20back to VS Code. And if you just
- 3:55:23installed Node.js, we need to restart VS
- 3:55:26Code to make sure that Node.js is
- 3:55:28loaded.
- 3:55:30So, we're going to click X in the top
- 3:55:32right of the terminal
- 3:55:34and then close VS Code.
- 3:55:38And then we're going to open VS Code
- 3:55:40again.
- 3:55:43And we're going to open our project
- 3:55:44folder, React-Course.
- 3:55:46and we're going to open a new terminal.
- 3:55:49So, in the top menu, we're going to go
- 3:55:52to the terminal section and then open a
- 3:55:54new terminal.
- 3:55:58Now, when we install NodeJS, it's going
- 3:56:01to add a new command to our command line
- 3:56:05called node.
- 3:56:07So, the node command lets us run
- 3:56:10JavaScript code outside of a browser.
- 3:56:14For example, here we have a file called
- 3:56:17react-basics.js
- 3:56:20and inside we have some simple
- 3:56:22JavaScript code. If you don't have this
- 3:56:24file, feel free to pause the video and
- 3:56:26create this file. Now we can give this
- 3:56:30file to node and it will run the
- 3:56:32JavaScript code in this file. So here
- 3:56:36we'll use the node command and type
- 3:56:38space and give it the file to run which
- 3:56:42is react-basics.js
- 3:56:46and press enter.
- 3:56:49So this will run the JavaScript inside
- 3:56:52this file and a console.logs hello. So
- 3:56:56as you can see the node command lets us
- 3:56:59run JavaScript code outside of our
- 3:57:02browser.
- 3:57:03Now when we install Node.js, it also
- 3:57:06adds another command to our command line
- 3:57:09called npm.
- 3:57:13npm stands for node package manager. A
- 3:57:17package is basically an external
- 3:57:19library. So npm lets us install external
- 3:57:24libraries or packages into our project.
- 3:57:28To do that, we can use the command npm
- 3:57:32space install
- 3:57:35and then space and then the name of an
- 3:57:38external library or package.
- 3:57:41As an example, we'll install the package
- 3:57:44super simple dev.
- 3:57:48This is a package that I created that we
- 3:57:50can use as an example and press enter.
- 3:57:57On Windows, if you get an error like
- 3:58:00this, you'll need to run this command
- 3:58:02first
- 3:58:05and then run npm install super simple
- 3:58:08dev again.
- 3:58:10If that still does not work for you, you
- 3:58:13can check the troubleshooting steps in
- 3:58:15the description.
- 3:58:18Once npm install finishes, npm will
- 3:58:22download this external library or this
- 3:58:25package into a folder called node
- 3:58:28modules. If we open this folder, we see
- 3:58:31the super simple dev package and inside
- 3:58:35this contains the code that we've been
- 3:58:37using in the chatbot project like
- 3:58:39react.js
- 3:58:41and chatbot.js.
- 3:58:44npm contains millions of packages that
- 3:58:47we can download and use in our project
- 3:58:50and you can search for packages on the
- 3:58:53npm website.
- 3:58:57So this is just a review of NodeJS and
- 3:59:00npm.
- 3:59:02Now that we reviewed NodeJS and npm,
- 3:59:05we're ready to do a proper React setup.
- 3:59:09We just learned that npm has millions of
- 3:59:12packages available to use. Some of these
- 3:59:15packages also add a command to our
- 3:59:19command line. For example, there's a
- 3:59:22package called create-vit.
- 3:59:25If we install this package, it also adds
- 3:59:28a command to our command line called
- 3:59:31create-vit.
- 3:59:33The create-vit command helps us set up a
- 3:59:37new React project.
- 3:59:40To use the create-v command, we need to
- 3:59:43do two steps. Number one, we need to
- 3:59:46install the create-vt package. And
- 3:59:49number two, we're going to run the
- 3:59:52create-v command in the command line.
- 3:59:55Now, npm actually has a shortcut for
- 3:59:59this called npx.
- 4:00:02npx does these two steps in one step.
- 4:00:06The x means execute.
- 4:00:10So let's go to our command line and
- 4:00:13let's type the command npx create-vt.
- 4:00:18And then we'll also type the at symbol.
- 4:00:22At means we're going to use a specific
- 4:00:25version of the create-vit command. For
- 4:00:28this video, we're going to use a
- 4:00:30specific version so that you have the
- 4:00:33same code that I do and you can follow
- 4:00:35along.
- 4:00:37To get the version of create-vt we're
- 4:00:40going to use, we're going to click this
- 4:00:42link in the description.
- 4:00:44Go to lesson 5 and here you'll find the
- 4:00:48version of create-vt we're going to use.
- 4:00:51You can also find the full command here.
- 4:00:55By the way, the version that you see in
- 4:00:57this video might be different than the
- 4:00:59version you see on your page. You should
- 4:01:02always use the version that you see on
- 4:01:04your page. I did this because it's hard
- 4:01:07to update the video. So, this lets me
- 4:01:11easily update the version in the future.
- 4:01:16So, select the version on your page.
- 4:01:19Copy.
- 4:01:21And in our command line, we're going to
- 4:01:23paste it after the at.
- 4:01:27So again, this command will install the
- 4:01:30create-vt package and then it will run
- 4:01:33the create-vit command. The create-v
- 4:01:37command will help us set up a new react
- 4:01:40project.
- 4:01:42By the way, in some tutorials, they
- 4:01:44might use the command npm create vit.
- 4:01:48This is just a shortcut for
- 4:01:50npxcreate-vit.
- 4:01:55Now let's press enter.
- 4:01:58If it says it needs to install this
- 4:02:00package, we can press y for yes and
- 4:02:03press enter.
- 4:02:07Now the create-v command will ask us
- 4:02:10some questions. First it will ask us for
- 4:02:14a project name. So let's name it chatbot
- 4:02:18dash project and press enter.
- 4:02:23And then it will ask us what technology
- 4:02:25we want to use in our project. So we're
- 4:02:28going to press the down arrow and select
- 4:02:31react and press enter.
- 4:02:34And it will ask us what other tools we
- 4:02:37want in our project. We're going to
- 4:02:39press down and just select JavaScript
- 4:02:42and press enter.
- 4:02:46and create vit will create an empty
- 4:02:48React project at this location or this
- 4:02:52folder right here.
- 4:02:54Next, we're going to go into this folder
- 4:02:57with our command line. To do that, we
- 4:03:00can use the command cd or change
- 4:03:03directory and give it the folder name
- 4:03:06chatbot dash project and press enter.
- 4:03:12If we type pwd and press enter,
- 4:03:17it will tell us the command line is now
- 4:03:19running inside of the chatbot project
- 4:03:21folder. So, it's running inside of here.
- 4:03:27The next thing we're going to do is
- 4:03:28we're going to run the command npm
- 4:03:31install.
- 4:03:33Inside our chatbot project folder, we
- 4:03:35have a special file called package.json.
- 4:03:40So, package.json JSON contains a list of
- 4:03:43all the packages that are needed for
- 4:03:46this project. So, npm install is going
- 4:03:49to install all of these packages at the
- 4:03:51same time.
- 4:03:54So, we'll go back to the command line
- 4:03:56and scroll down and we'll run the
- 4:03:58command npm install and press enter.
- 4:04:05Now, this might take a while because
- 4:04:07it's going to install a lot of packages.
- 4:04:09So, we just got to wait until it
- 4:04:11finishes.
- 4:04:15After npm install finishes, all of the
- 4:04:18packages here are going to be downloaded
- 4:04:20into the node modules folder. So, if we
- 4:04:23open this, you'll see that we have a lot
- 4:04:25of packages here. Now, you'll notice
- 4:04:28that there's more packages here than we
- 4:04:30have in the package.json file, and
- 4:04:33that's because some packages require
- 4:04:36other packages. So, npm helps us install
- 4:04:39everything that we need.
- 4:04:44The last step is to run the command npm
- 4:04:48rundev.
- 4:04:50So, this will start up our new React
- 4:04:52website. Let's press enter.
- 4:04:57And now we can view this website by
- 4:04:59opening this URL in our browser. So,
- 4:05:02we're going to select this URL and copy
- 4:05:07and then open our browser.
- 4:05:10And I'm going to make it full screen so
- 4:05:12we can see more of it. We'll create a
- 4:05:14new tab and paste and press enter.
- 4:05:20And now we have a new React website that
- 4:05:23we can work with. So that's how we do a
- 4:05:26proper React setup using the command
- 4:05:29line npm and vit.
- 4:05:34Now that we created a proper React
- 4:05:36setup, let's go back to VS Code
- 4:05:40and we'll learn what each of these files
- 4:05:42and folders do. As a review, the node
- 4:05:47modules folder is where npm installs all
- 4:05:50of the packages.
- 4:05:53Next, we're going to look at the src or
- 4:05:56the source folder.
- 4:05:58This folder contains the code for our
- 4:06:01website. So, all our JavaScript and CSS
- 4:06:04code goes in here. And remember, when
- 4:06:07using React, we write JSX code rather
- 4:06:11than normal JavaScript code. So, the
- 4:06:14files here end with JSX instead of JS.
- 4:06:20Next, this assets folder contains images
- 4:06:23that we want to display on our website.
- 4:06:26So, here we have an image of the React
- 4:06:28logo in SVG format.
- 4:06:33The last folder here is the public
- 4:06:35folder. The public folder contains files
- 4:06:39that should be available to the public.
- 4:06:42Public means we can access these using a
- 4:06:45URL. That means that when this website
- 4:06:48is on the internet, I can give you a URL
- 4:06:52to this image and you'll be able to
- 4:06:54access it.
- 4:06:56So here they just put this vit image in
- 4:06:59the public folder as an example. So now
- 4:07:02we understand the folders that we have
- 4:07:05in this setup.
- 4:07:09Next let's learn about all the files in
- 4:07:12this react setup. The first file.get Git
- 4:07:16ignore is used by a tool called git. Git
- 4:07:20helps us track changes in our code. For
- 4:07:24this chatbot project, we're not going to
- 4:07:26use git. So this file doesn't do
- 4:07:28anything. So we don't need to change
- 4:07:30this file.
- 4:07:32The next file here is eslint.config.js.
- 4:07:37So this is used by a tool called eslint.
- 4:07:42eslint highlights problems in our
- 4:07:44JavaScript code. Let's do an example. If
- 4:07:49we open the source folder and we open
- 4:07:52the file app.jsx
- 4:07:56and let's say that here I accidentally
- 4:07:58misspell setc count by removing the last
- 4:08:01t.
- 4:08:03My VS code doesn't really tell me that
- 4:08:05there is a problem here. But ESLint will
- 4:08:09tell me very clearly that there is a
- 4:08:11problem. To use ESLint, we need to
- 4:08:15install the ESLint extension. So we can
- 4:08:18go to the extension section
- 4:08:21and we'll install the extension ESLint
- 4:08:26and we'll install it.
- 4:08:31After it's installed, if we go back to
- 4:08:33app.jsx
- 4:08:36now, it's going to tell me with a red
- 4:08:37underline that this variable name does
- 4:08:41not match the variable name down here.
- 4:08:44So, if we scroll up and we fix the
- 4:08:48spelling by adding back the t, that red
- 4:08:51underline or that error will go away.
- 4:08:55As you can see, eslint is a really
- 4:08:57useful tool that highlights problems in
- 4:09:00our JavaScript code. So, if we go back
- 4:09:03to the eslint.config.js,
- 4:09:07this file configures eslint, it tells it
- 4:09:11what to check for. So, this
- 4:09:13configuration file is actually good
- 4:09:15enough for now. So, we don't need to
- 4:09:17change this file.
- 4:09:20The next file in our setup is index.htm.
- 4:09:23html.
- 4:09:25This contains all the HTML code for our
- 4:09:28React website. So, this is the same
- 4:09:32thing as chatbot.html.
- 4:09:35The main difference is instead of
- 4:09:38putting all of our code inside the HTML
- 4:09:41file, index.html
- 4:09:44loads our JavaScript from a different
- 4:09:46file.
- 4:09:47So this is the one file that we may need
- 4:09:50to change inside this setup. If we want
- 4:09:53to change the HTML for our website,
- 4:09:57for example, we can change the title at
- 4:10:00the top of the tabs by removing this
- 4:10:04and let's change it to chatbot
- 4:10:08project.
- 4:10:09If we save,
- 4:10:12vit will automatically refresh our page
- 4:10:14just like live server does. And if we
- 4:10:17open our website, you'll see that the
- 4:10:19title at the top changed.
- 4:10:23Let's go back.
- 4:10:25So the index.html
- 4:10:27contains the HTML code for this website.
- 4:10:32The next file in this setup is
- 4:10:34package-lock.json.
- 4:10:37As a review, package.json has a list of
- 4:10:41packages that we need to install to run
- 4:10:44this project. What package lock.json
- 4:10:48does is it saves the version numbers of
- 4:10:52all the packages that we installed. So
- 4:10:55the next time we run npm install, this
- 4:10:58file makes sure that we get the same
- 4:11:00versions for all of these packages.
- 4:11:04Now npm automatically updates package
- 4:11:07lock.json. So we don't need to change
- 4:11:10this file. The next file in the setup is
- 4:11:14package.json. JSON and this basically
- 4:11:17contains the list of packages that we
- 4:11:19need in our project as well as some
- 4:11:21other information about our project.
- 4:11:24When we npm install a package, it will
- 4:11:28automatically update this list of
- 4:11:30packages. So we don't need to change
- 4:11:32this file.
- 4:11:34Next, we have the file readme.md.
- 4:11:38So this is just text. It provides more
- 4:11:41information about this project. And this
- 4:11:44is not code. This is just forformational
- 4:11:46purposes. So we don't need to change
- 4:11:48this file.
- 4:11:50Finally, vit.config.js
- 4:11:54configures vit. Vit is the tool that we
- 4:11:58used to set up this project. Vit also
- 4:12:02helps us build the website. That means
- 4:12:04it takes all the JavaScript and CSS and
- 4:12:07then loads them into the HTML file. Vit
- 4:12:11also creates a server. A server puts our
- 4:12:15website at a URL. For example, here it
- 4:12:19put our website at the URL localhost
- 4:12:225173.
- 4:12:23So this lets us view the website in a
- 4:12:26web browser like a real website. When we
- 4:12:29were building the chatbot project in the
- 4:12:31previous lessons, we used an extension
- 4:12:34called live server. Live server
- 4:12:37automatically refreshes the website when
- 4:12:40we change some code. The VIT server also
- 4:12:44refreshes the website when we change
- 4:12:46some code. So the VIT server is actually
- 4:12:49a replacement for the live server that
- 4:12:52we used earlier. So those are three main
- 4:12:55features of Vit and you can see that
- 4:12:57it's really helpful for setting up and
- 4:13:00developing a React website. We'll go
- 4:13:03back to VS Code.
- 4:13:06So the vit config.js file basically
- 4:13:09configures vit. So this config file is
- 4:13:13good enough. So we don't need to change
- 4:13:15this file. And that's it. Now we
- 4:13:18understand all the files and folders in
- 4:13:22this react setup. As you can see most of
- 4:13:26these files are just configuration files
- 4:13:29and we don't need to change them. The
- 4:13:31only files we need to focus on and
- 4:13:33change are the HTML file and the files
- 4:13:37in the source folder which is our CSS
- 4:13:40and JavaScript files.
- 4:13:44Now that we understand all the files and
- 4:13:47folders in this React setup, let's move
- 4:13:50our previous chatbot project into this
- 4:13:53setup. First, we can close the chatbot
- 4:13:57project folder for now. And let's
- 4:14:00actually remove the node modules folder
- 4:14:02that we created earlier for practice. So
- 4:14:05we'll delete this folder
- 4:14:10and let's also delete the test folder
- 4:14:12that we created for practice
- 4:14:15and also the package lock.json and
- 4:14:18package.json.
- 4:14:28Next, let's go into the chatbot project
- 4:14:30folder and we'll open the source folder
- 4:14:34and we'll start with this file app.jsx.
- 4:14:38So notice that in this file we have a
- 4:14:41react component called app and this
- 4:14:44component represents the entire
- 4:14:47application.
- 4:14:48Now previously in chatbot.html
- 4:14:52we also created an app component.
- 4:14:55So all we have to do is to replace this
- 4:14:58app component with our previous app
- 4:15:01component. So let's go to our code and
- 4:15:04we're going to open chatbot.html
- 4:15:08and we're going to scroll down till we
- 4:15:11see the app component which is down
- 4:15:14here. So we're going to select the app
- 4:15:17component
- 4:15:20and make sure you don't select the code
- 4:15:22down here. We just want to copy the
- 4:15:24component over. So, we're going to copy
- 4:15:27this
- 4:15:29and then go back to app.jsx
- 4:15:33and we're going to remove this app
- 4:15:35component. So, we'll select it
- 4:15:39and remove it and we're going to paste
- 4:15:42the app component from our previous
- 4:15:44project.
- 4:15:46And now we're going to scroll up and
- 4:15:50eslint is going to tell us we have some
- 4:15:52errors here. So we'll go through these
- 4:15:55errors one by one.
- 4:15:58First for this error eslint is going to
- 4:16:02tell us that react is not defined.
- 4:16:05Before we loaded the react external
- 4:16:08library using a script tag and this
- 4:16:12provided a variable called react.
- 4:16:15However, now that we're using NodeJS,
- 4:16:19we usually don't use script tags to load
- 4:16:21the external libraries. Instead, we
- 4:16:25download these packages into Node
- 4:16:27modules and then load it from node
- 4:16:30modules. With node modules, we can use a
- 4:16:34JavaScript feature called modules to
- 4:16:37only load the parts of the external
- 4:16:39library that we need. So when using npm,
- 4:16:43a best practice is to load external
- 4:16:46libraries from node modules. Instead of
- 4:16:48using a script tag
- 4:16:52to load an external library from node
- 4:16:55modules, we use the word import. And
- 4:16:58then we tell the computer which external
- 4:17:01library or which package we want to load
- 4:17:04from by typing from and then the name of
- 4:17:07the package like the string react. If we
- 4:17:11just type a name directly like this and
- 4:17:14we don't start with dot slash or slash,
- 4:17:17this tells the computer to look inside
- 4:17:20node modules for the React package. So,
- 4:17:23it's going to look in here and it's
- 4:17:25going to find the React package in here
- 4:17:28and load it from here.
- 4:17:31Also, when importing from node modules,
- 4:17:34we don't have to import the entire React
- 4:17:37library. we can just import what we
- 4:17:39need. For example, here we just import
- 4:17:43the use state feature from React.
- 4:17:47So now that we imported use state, we
- 4:17:50can just use this directly here instead
- 4:17:53of doing react dot use state. So we can
- 4:17:57remove react dot and use state directly.
- 4:18:03Next, these two lines import images that
- 4:18:07were used in the example website. So,
- 4:18:10we're no longer using these images. So,
- 4:18:12ESLint displays an error. To fix this,
- 4:18:16we can just remove these imports.
- 4:18:20Next, let's scroll down
- 4:18:23and it's going to tell us that the
- 4:18:25components chat messages and chat input
- 4:18:29are missing and they don't exist in this
- 4:18:31file. So, let's actually just copy them
- 4:18:34over from chatbot.html.
- 4:18:38So, we'll go back to the previous
- 4:18:40project
- 4:18:42and in chatbot.html, HTML. We're going
- 4:18:44to scroll up
- 4:18:47and above the app component, we're going
- 4:18:50to copy over all of our other
- 4:18:52components. So, we're going to select
- 4:18:53all this code
- 4:18:56and we're going to copy
- 4:19:00and let's go back to app.jsx
- 4:19:04and we'll scroll up above the app. We're
- 4:19:08going to paste all of the other
- 4:19:11components that we created.
- 4:19:14And now if we scroll down and look at
- 4:19:16our app component, you can see that
- 4:19:18there's no more errors because we moved
- 4:19:21these components into this file. So now
- 4:19:24we've moved the app component into this
- 4:19:27new React setup.
- 4:19:31After copying over all of our other
- 4:19:34components, we now have some more ESLint
- 4:19:37errors. So, let's scroll up to the top
- 4:19:40and we'll fix these errors one by one.
- 4:19:43First, you might see an error here for
- 4:19:46the props.
- 4:19:48ESLint tells us that these props are
- 4:19:51missing props validations.
- 4:19:54Props validation was an older feature of
- 4:19:57React. Newer versions of React don't use
- 4:20:00this feature. So, we can actually turn
- 4:20:02off this check in eslint.
- 4:20:05To turn off a check in eslint, we're
- 4:20:08going to go to the eslint config.js
- 4:20:11file, and we're going to scroll down to
- 4:20:15the rules section here. A rule tells
- 4:20:19ESLint what to check for. At the bottom
- 4:20:22of this object, we're going to add the
- 4:20:25string react slprop-types
- 4:20:31colon and we'll set it to the string off
- 4:20:35and save.
- 4:20:38So, this will turn off this particular
- 4:20:40rule or this particular check. And now,
- 4:20:43if we go back to app.jsx, jsx. We no
- 4:20:47longer have those eslint errors because
- 4:20:50we turned off the check.
- 4:20:52Next, remember that we're no longer
- 4:20:54using react.
- 4:20:56We can just use state directly. So, we
- 4:21:00can remove react dot.
- 4:21:03Next, let's scroll down and eslint will
- 4:21:07tell us that chatbot does not exist in
- 4:21:10this file. So, previously we loaded the
- 4:21:14chatbot using a script tag. But
- 4:21:17remember, now that we're using npm, a
- 4:21:20best practice is to load the chatbot
- 4:21:22from node modules.
- 4:21:25First, we need to download the chatbot
- 4:21:27into node modules. To do that, we're
- 4:21:30going to install the package super
- 4:21:32simple dev. Let's go back to the command
- 4:21:35line.
- 4:21:37And currently, it's running the vit
- 4:21:39server. So, we're going to stop the
- 4:21:41server by pressing control C on Windows
- 4:21:44or control C on Mac. Note that on Mac,
- 4:21:48it's control C, not command C. So, this
- 4:21:52will stop the V server and bring us back
- 4:21:55to the command line. Usually, to exit
- 4:21:58something from the command line, we can
- 4:22:00press control C in both Windows and Mac.
- 4:22:04Next, we're going to npm
- 4:22:08install
- 4:22:09the package super simple dev. So, this
- 4:22:15package contains the chatbot code that
- 4:22:17we were using earlier. Let's press
- 4:22:20enter.
- 4:22:23It's going to install the super simple
- 4:22:24dev package inside node modules. And now
- 4:22:28to load it from node modules, we're
- 4:22:31going to scroll up in our code and at
- 4:22:34the top we're going to import
- 4:22:38and type curly brackets. So we're going
- 4:22:41to import the chatbot object and we're
- 4:22:46going to import it from a string and
- 4:22:50we'll import it from the package super
- 4:22:53simple dev. So this will load the super
- 4:22:57simple dev package from inside node
- 4:23:00modules and get the chatbot out of it.
- 4:23:03And now that we have this chatbot
- 4:23:05object, if we scroll down, we have
- 4:23:08access to it. So it's no longer giving
- 4:23:10us an error.
- 4:23:13Finally, we're going to scroll down
- 4:23:16to the chat messages component.
- 4:23:20And here it's telling us that React does
- 4:23:23not exist. So just like with use state,
- 4:23:27we can import use ref and use effect
- 4:23:30directly as well.
- 4:23:33So let's remove the react dot from here
- 4:23:37and remove react dot from here.
- 4:23:41And we're going to scroll up
- 4:23:44and at the top we're already importing
- 4:23:47from the React package. To import
- 4:23:50multiple things we can just add a comma
- 4:23:53at the end. and import use ref and add
- 4:23:58another comma and import use effect.
- 4:24:04This gets these two features out of
- 4:24:06React and now we can use them directly
- 4:24:09in our code.
- 4:24:11And now we've fixed all the ESLint
- 4:24:13errors in our app component. If you did
- 4:24:15the exercises in the previous lesson,
- 4:24:18you may have some more ESLint errors.
- 4:24:21So, make sure you fix any other errors
- 4:24:23that you have. And finally, let's save
- 4:24:26this file.
- 4:24:29Next, we're going to look at the file
- 4:24:32main.jsx.
- 4:24:34So, this code sets up React. It calls
- 4:24:38create root, gives it a container, and
- 4:24:41then renders the app component onto the
- 4:24:45website. If we open our chatbot.html
- 4:24:48HTML and we scroll to the very bottom.
- 4:24:55You notice that we do the same thing. We
- 4:24:58call create root, give it a container,
- 4:25:00and then render the app onto the
- 4:25:03website. So main.jsx
- 4:25:06does the same thing as this code. It
- 4:25:09sets up React and displays the app onto
- 4:25:13the website.
- 4:25:14Now, instead of copying this code over
- 4:25:17to main.j jsx. We're just going to use
- 4:25:20the code in this file because it
- 4:25:22basically does the same thing. It just
- 4:25:25uses an ID for the container instead of
- 4:25:27a class. The one other difference in
- 4:25:30this code is it wraps the app in a
- 4:25:33component called strict mode. Strict
- 4:25:36mode is a special component provided by
- 4:25:39React. Strict mode basically gives us
- 4:25:42some additional checks and warnings when
- 4:25:45developing our app.
- 4:25:47And that's it. We moved all of our
- 4:25:50JavaScript code into this new setup.
- 4:25:53Now, let's open this app in our browser
- 4:25:56to see what it looks like. We'll go down
- 4:25:59to the command line and let's make sure
- 4:26:02we're running inside the chatbot project
- 4:26:05folder by typing pwd and press enter.
- 4:26:10So, that looks good. And we're going to
- 4:26:12start the VIT server by typing npm
- 4:26:17rundev
- 4:26:18and press enter.
- 4:26:21And we'll open this URL in our browser.
- 4:26:25I already have it open.
- 4:26:27So I'll open my browser and refresh this
- 4:26:30page. And now the website displays our
- 4:26:33previous chatbot app. in the text box.
- 4:26:38If we type a message like hello and
- 4:26:41press send,
- 4:26:43it will display a response from the
- 4:26:45chatbot.
- 4:26:47However, obviously it doesn't look
- 4:26:50right, and that's because we haven't
- 4:26:52moved our CSS code and images over into
- 4:26:56the new setup. So, we're going to do
- 4:26:58that next.
- 4:27:01Let's move our CSS code into this new
- 4:27:04React setup.
- 4:27:06We'll go back to VS Code
- 4:27:08and we're going to open chatbot.html
- 4:27:13and we're going to go to the top of this
- 4:27:15file right here.
- 4:27:18So at the top we have all of our CSS
- 4:27:21code. We're going to move this CSS code
- 4:27:24into app.css.
- 4:27:27So, we'll select all of this CSS
- 4:27:33all the way to the bottom and we're
- 4:27:36going to copy it
- 4:27:39and then open app.css
- 4:27:42and we're going to remove this default
- 4:27:44CSS. So, we'll remove this and then
- 4:27:48paste our CSS code and save.
- 4:27:54And now how does this CSS code get
- 4:27:56loaded on the website? If we open
- 4:28:00app.jsx,
- 4:28:02notice at the top we import/app.css.
- 4:28:08So dot slash means the current folder.
- 4:28:11It means the folder that contains this
- 4:28:14file, which is the source folder. So
- 4:28:17it's going to start from source and look
- 4:28:19for a file called app.css.
- 4:28:23and it finds it here and it will load
- 4:28:25all the CSS code in this file onto the
- 4:28:29website. So you might be wondering how
- 4:28:32can we import a CSS file like this?
- 4:28:36Doesn't import only work for JavaScript
- 4:28:38files. So this is actually a feature of
- 4:28:42Vit. Vit lets us import any type of file
- 4:28:46like a CSS file or even an image file.
- 4:28:50Depending on the type of file, it will
- 4:28:53behave differently. If we import a CSS
- 4:28:57file, vit will load all the CSS code in
- 4:29:01this file and apply it to the website.
- 4:29:04So that's how this works. We use Vit to
- 4:29:07load the CSS code inside app.css CSS.
- 4:29:12And this allows us to separate our CSS
- 4:29:15code into its own file to make our code
- 4:29:18more organized.
- 4:29:20Next, you'll also notice that in here we
- 4:29:23have an index.css
- 4:29:25file.
- 4:29:27This CSS file is loaded by main.jsx
- 4:29:32here using this import. So why do we
- 4:29:36have two CSS files? A best practice is
- 4:29:40to split up our CSS into different
- 4:29:42files. All the CSS for the app component
- 4:29:47go inside the app.css file. All the CSS
- 4:29:51for the overall website should go inside
- 4:29:54index.css.
- 4:29:57If we open app.css
- 4:30:00and we scroll to the top,
- 4:30:02the body styles here are not related to
- 4:30:06the app component. This styles the
- 4:30:09overall website. So a best practice is
- 4:30:12to split up this code and move this into
- 4:30:16index.css.
- 4:30:18So we'll select this code and then cut
- 4:30:23and save
- 4:30:25and then inside index.css CSS.
- 4:30:29We're going to remove this default code
- 4:30:33and paste those body styles and save.
- 4:30:40If we go back to app.css,
- 4:30:43the rest of this code is related to the
- 4:30:45app component. So, it makes sense to
- 4:30:48leave it in app.css.
- 4:30:51And now we moved all of our CSS into our
- 4:30:54new React setup. So, let's open our
- 4:30:58project again in the browser to see what
- 4:31:00it looks like.
- 4:31:02And now it's looking pretty good. It's
- 4:31:04looking like our previous chatbot
- 4:31:06project. And we have the layout and
- 4:31:09styling that we need. The last thing
- 4:31:11we're missing here are the images. So,
- 4:31:14we're going to move those. Next,
- 4:31:18let's move the robot image and the user
- 4:31:21image into the new React setup. We'll go
- 4:31:25back to VS Code
- 4:31:28and we're going to scroll down and we're
- 4:31:30going to select the robot.png image and
- 4:31:34we'll copy it by pressing control C on
- 4:31:36Windows or command C on Mac.
- 4:31:40And let's scroll up and we're going to
- 4:31:43put it inside the source/assets
- 4:31:46folder. So, this is typically where we
- 4:31:49put our images. So, let's click on this
- 4:31:52assets folder and we're going to paste
- 4:31:54the image by pressing controlV on
- 4:31:56Windows or commandV on Mac.
- 4:32:00And now we have the robot image in our
- 4:32:02new setup. Let's do the same for the
- 4:32:06user image. So, we'll go down and select
- 4:32:09user.png
- 4:32:11and we'll copy
- 4:32:14and then select the assets folder and
- 4:32:17we'll paste.
- 4:32:20And now we need to load these images
- 4:32:22into our app. Let's open app.jsx.
- 4:32:28And to load this image, we can actually
- 4:32:30just import it. So at the top, let's
- 4:32:34type import
- 4:32:36and we'll give this image a name like
- 4:32:40robot profile image. And we'll import it
- 4:32:46from a string. and we're going to locate
- 4:32:50this image file. So inside here, we'll
- 4:32:54start by typing dot slash, which means
- 4:32:57the current folder or the folder that
- 4:33:00contains app.jsx,
- 4:33:02which is the source folder. So we're
- 4:33:05going to start from the source folder
- 4:33:07and then we're going to go into the
- 4:33:09assets folder. So, we'll type assets.
- 4:33:13And to go into this folder, we're going
- 4:33:15to type a slash. And then we're going to
- 4:33:18look for the file robot.png.
- 4:33:22So, we'll type robot.png.
- 4:33:27So, this is a file path. It tells the
- 4:33:29computer how to locate this file.
- 4:33:33So also notice that in this import we
- 4:33:36just gave it a name instead of using
- 4:33:39curly brackets.
- 4:33:41This is called a default export and
- 4:33:44we'll review this later in the lesson.
- 4:33:47But for now just remember that when we
- 4:33:49import an image we just give it a name
- 4:33:52without curly brackets.
- 4:33:54Let's also load the user image. So,
- 4:33:57we're going to import and give it a
- 4:34:01name, user profile image, and we'll
- 4:34:05import it from a string. And we're going
- 4:34:09to locate the user.png file. So, we'll
- 4:34:12start from the current folder using dot
- 4:34:15slash. We'll look for the assets folder
- 4:34:18and then slash to go into this folder.
- 4:34:22And we'll look for the file user.png.
- 4:34:27So remember this is a feature of vit.
- 4:34:31Vit lets us import any type of file
- 4:34:33including an image file. When we import
- 4:34:37an image like this, vit gives us a file
- 4:34:41path to that image. So this contains a
- 4:34:44file path like this. And we can use this
- 4:34:48directly in an images source attribute.
- 4:34:52So, let's scroll down to the chat
- 4:34:56message component. And in here, we have
- 4:34:59our two image elements. So, in the
- 4:35:02source attribute, instead of robot.png,
- 4:35:07we're going to give it those images that
- 4:35:08we imported by typing curly brackets.
- 4:35:13And here for the robot, we'll insert the
- 4:35:16variable robot profile image. And we'll
- 4:35:22do the same thing for the user. We'll
- 4:35:24remove the source attribute
- 4:35:27and type curly brackets. And we'll
- 4:35:30insert the user profile image. So that's
- 4:35:35how we import images using Vit and use
- 4:35:39them. Now let's save
- 4:35:42and we'll check our website.
- 4:35:45And now all of the images are loaded.
- 4:35:48So, let's try adding a message to make
- 4:35:50sure this is working. We'll type the
- 4:35:53message hello and press send.
- 4:35:57And that's it. We finished moving our
- 4:36:00chatbot project into this new React
- 4:36:03setup.
- 4:36:05We'll go back to VS Code.
- 4:36:09And the big advantage of this setup is
- 4:36:12we can separate our code into different
- 4:36:14files instead of combining everything
- 4:36:17into one file like we had before. Right
- 4:36:20now we already separated our code a bit.
- 4:36:24We separated the app component into
- 4:36:27app.jsx
- 4:36:28and we separated the code that sets up
- 4:36:31React into main.jsx.
- 4:36:35Now we're going to separate our code
- 4:36:37even more. So, inside app.jsx,
- 4:36:41if we scroll to the top,
- 4:36:44notice that there's many other
- 4:36:46components in this file, like the chat
- 4:36:49input component. A best practice is to
- 4:36:52separate each component into its own
- 4:36:55file. This makes our code easier to work
- 4:36:58with because each file only focuses on
- 4:37:02one component.
- 4:37:04So, let's separate the chat input
- 4:37:06component into its own file.
- 4:37:10First, we're going to go to the source
- 4:37:12folder and we're going to create a new
- 4:37:14folder in here to group our components
- 4:37:17together. So, we'll click this folder
- 4:37:20and we're going to click this icon to
- 4:37:23create a new folder and we'll name it
- 4:37:26components
- 4:37:28and press enter.
- 4:37:31And inside this components folder, we're
- 4:37:34going to create a new file for our chat
- 4:37:37input component. So in here, we'll click
- 4:37:40it and then click this icon to create a
- 4:37:43new file. And we'll give it the same
- 4:37:46name as the component. So we'll name it
- 4:37:49capital C chat capital I input.jsx
- 4:37:55and press enter.
- 4:37:58And now we're going to move the chat
- 4:38:01input component into this file. So we'll
- 4:38:04open app.jsx
- 4:38:07and we'll select the code for chat input
- 4:38:10which is down here.
- 4:38:13And we're going to cut
- 4:38:17and then open chatinput.jsx.
- 4:38:21And in here we'll paste.
- 4:38:25and we'll scroll up and we're going to
- 4:38:28fix these eslint errors. We'll start
- 4:38:31with this one. Use state currently
- 4:38:34doesn't exist in this file. So, we need
- 4:38:37to import it again.
- 4:38:39Let's go back to app.jsx.
- 4:38:42And at the top, let's copy this import.
- 4:38:46So, we'll select it and copy.
- 4:38:50And we'll go to the chat input file. And
- 4:38:54at the top of this,
- 4:38:56we're going to paste the import.
- 4:38:59Now that we imported the use state,
- 4:39:02we're able to use it and there's no
- 4:39:04longer an error. Now, eslint also tells
- 4:39:07us that we're not using use ref or use
- 4:39:10effect in this file. So, we can remove
- 4:39:13these.
- 4:39:15Next, we're going to scroll down in this
- 4:39:18file and we'll see that the chatbot
- 4:39:21object doesn't exist anymore in this
- 4:39:24file. So again, we'll go back to app.jsx
- 4:39:29and let's move this import. So we'll
- 4:39:32select it and chatbot is not used
- 4:39:35anymore in this file. So this time we're
- 4:39:38going to cut
- 4:39:41and inside chat input at the top we're
- 4:39:46going to paste that input.
- 4:39:49And now we have access to the chatbot.
- 4:39:53The last error here is that the chat
- 4:39:56input component is not being used. To
- 4:39:59fix this, we need to review a feature of
- 4:40:02JavaScript called modules.
- 4:40:05By default, any variables or functions
- 4:40:08that we create in a file only exist in
- 4:40:12that file. So chat input only exists in
- 4:40:17this file. It cannot be used outside
- 4:40:20this file. This helps us avoid naming
- 4:40:24conflicts because if we use the same
- 4:40:26variable name in different files, it
- 4:40:29won't cause an error. Now, if we want to
- 4:40:33use this variable outside of this file,
- 4:40:35we need to export it first. To do that
- 4:40:39in front of this function, we can just
- 4:40:42type export.
- 4:40:45And now this error is gone because
- 4:40:47ESLint assumes that we're going to use
- 4:40:49this component outside of this file. So
- 4:40:53now let's save.
- 4:40:56And to use chat input in another file,
- 4:40:59we need to import it. So we'll open
- 4:41:02app.jsx.
- 4:41:05And at the top, we're going to import
- 4:41:10and type curly brackets. And in here,
- 4:41:14we'll import the chat input component.
- 4:41:18And we'll import it from a string. And
- 4:41:22we're going to locate the chat input
- 4:41:25file.
- 4:41:26At the front, we'll type dot slash to
- 4:41:30start in the current folder, which is
- 4:41:32the source folder. And then we're going
- 4:41:34to go to the components folder.
- 4:41:38And we'll type a slash to go into this
- 4:41:41folder. And we'll locate the file chat
- 4:41:45input.jsx.
- 4:41:49By the way, when importing a JavaScript
- 4:41:51file or a JSX file, if we're using Vit,
- 4:41:56we don't have to add.js or JSX at the
- 4:42:00end. Vit will actually add this
- 4:42:02automatically. So, as a shortcut, we can
- 4:42:05remove this.
- 4:42:08Also, a common practice is to import
- 4:42:11from packages at the top and then
- 4:42:14JavaScript files and then other types of
- 4:42:16files. So now we learn how to create
- 4:42:19these imports ourselves using the
- 4:42:22JavaScript modules feature and the word
- 4:42:25export. And we can use chat input in
- 4:42:29this file.
- 4:42:32Next, let's move the rest of these
- 4:42:34components into separate files. First,
- 4:42:38we'll move the chat message component.
- 4:42:41In the components folder, we'll click it
- 4:42:44and we'll click this icon to create a
- 4:42:46new file.
- 4:42:48And let's name it the same as the
- 4:42:50component. Capital C chat, capital M
- 4:42:54message.jsx,
- 4:42:58and press enter.
- 4:43:01And now we're going to move the code
- 4:43:02over. So we'll open app.jsx JSX and
- 4:43:06we'll select this component
- 4:43:10and cut.
- 4:43:14And then inside chat message.jsx,
- 4:43:18we're going to paste the component.
- 4:43:21And again, we'll scroll up and fix the
- 4:43:24ESLint errors. So, first it's telling us
- 4:43:28we're not using this component in this
- 4:43:30file. To fix this at the front, we're
- 4:43:33going to export this component so it can
- 4:43:36be used in other files.
- 4:43:39If we scroll down, ESLint tells us that
- 4:43:43these images are missing. So, we need to
- 4:43:46move those imports into this file. We'll
- 4:43:50go back to app.jsx JSX
- 4:43:53and at the top we're going to select
- 4:43:55these two image imports and we're going
- 4:43:58to cut
- 4:44:02and inside chat message.jsx
- 4:44:04we're going to scroll to the top
- 4:44:08and paste.
- 4:44:12Now one thing we need to be careful
- 4:44:14about is that we may need to update
- 4:44:16these file paths. So now this file is
- 4:44:21inside the components folder. That means
- 4:44:24dot slash is going to start at the
- 4:44:26components folder. It's not going to be
- 4:44:28able to find the assets folder anymore.
- 4:44:32To fix this at the front, we're going to
- 4:44:35use dot dot slash.
- 4:44:38So just like in the command line, dot
- 4:44:41dot represents the outer folder. This
- 4:44:44means it's going to go out of this
- 4:44:47components folder and into the source
- 4:44:49folder. And then it's going to look for
- 4:44:51the assets folder. It's going to find it
- 4:44:53and it will go into the assets folder
- 4:44:56and find robot.png.
- 4:44:59And we need to make the same change for
- 4:45:01the user image. At the front, we'll use
- 4:45:05dot dot to go out of the components
- 4:45:08folder into source. And then we can
- 4:45:10locate the assets folder.
- 4:45:13And now these imports will work
- 4:45:15correctly. And we finished moving the
- 4:45:18chat message component into its own
- 4:45:20file.
- 4:45:22Let's save.
- 4:45:24And we'll go back to app.jsx.
- 4:45:28And we'll move the chat messages
- 4:45:30component into its own file. Inside the
- 4:45:34components folder, we'll click it. We'll
- 4:45:37click this icon to create a new file.
- 4:45:41And let's name it the same as the
- 4:45:42component capital C chat capital M
- 4:45:46messages.jsx
- 4:45:50and press enter.
- 4:45:53We'll go back to app.jsx JSX and we'll
- 4:45:56select the code for this component which
- 4:46:00is till here and we're going to cut.
- 4:46:07And inside chat messages we're going to
- 4:46:10paste.
- 4:46:13Let's scroll to the top.
- 4:46:15And this component needs use ref and use
- 4:46:19effect. So let's move those imports into
- 4:46:23this file. We'll go to app.jsx.
- 4:46:28We'll select this import at the top
- 4:46:31and copy.
- 4:46:34And in chat messages
- 4:46:37at the top,
- 4:46:40we're going to paste that input.
- 4:46:44And in this component, we do not use use
- 4:46:46state. So we can remove it.
- 4:46:51Let's scroll down. And in this
- 4:46:53component, we also use the chat message
- 4:46:56component. So let's import that. We'll
- 4:46:59scroll up and at the top, we'll import
- 4:47:04curly brackets the chat message
- 4:47:09component from a string
- 4:47:12and we'll locate this file using dot
- 4:47:17slash. So it starts in the current
- 4:47:19folder which is the components folder
- 4:47:22and we'll look for the file chat message
- 4:47:27and remember we don't have to add JSX at
- 4:47:31the end. Vit can automatically add it.
- 4:47:36Finally eslint tells us this component
- 4:47:39is not used in this file. So we need to
- 4:47:42export it. However we're going to do
- 4:47:45something different this time.
- 4:47:48Instead of typing export at the front of
- 4:47:50this function, we're going to scroll
- 4:47:52down to the bottom and at the end, we're
- 4:47:57going to type export default
- 4:48:01and then what we want to export, which
- 4:48:04is the chat messages component.
- 4:48:09As a review, this is called a default
- 4:48:12export. It's useful if we want to export
- 4:48:15one thing from a file. So now let's
- 4:48:19save.
- 4:48:21And to import this, we're going to go to
- 4:48:24app.jsx.
- 4:48:26And at the top, we're going to import.
- 4:48:30And for a default export, we can just
- 4:48:33type a name without curly brackets. So
- 4:48:37we can type chat messages.
- 4:48:41We'll import it from a string
- 4:48:45and we'll type dot slash to start in the
- 4:48:47current folder. So for this file, we're
- 4:48:50in the source folder and then we're
- 4:48:52going to go into the components folder
- 4:48:55components slash and locate chat
- 4:48:59messages.
- 4:49:02So this imports whatever the default
- 4:49:06export is from this file and names it
- 4:49:09chat messages. This feature is called a
- 4:49:12default export. When we import and we
- 4:49:16use curly brackets like up here, this is
- 4:49:19called a named export.
- 4:49:22So some people like using default
- 4:49:24exports because it encourages each file
- 4:49:28to focus on one thing. However, it's up
- 4:49:31to you which style of export you want to
- 4:49:34use. You'll also find that some packages
- 4:49:37use named exports while other packages
- 4:49:40use default exports.
- 4:49:43So, this is just a review of JavaScript
- 4:49:46modules. If you want to learn about
- 4:49:48modules in more detail, you can check
- 4:49:50out my JavaScript full course in the
- 4:49:52video description.
- 4:49:55Next, let's fix these last few errors.
- 4:49:58So we're not using use ref or use effect
- 4:50:01in this file anymore. So we can remove
- 4:50:04these.
- 4:50:07And finally, this app component is
- 4:50:10already in a file called app.jsx.
- 4:50:14So we can just leave it in this file.
- 4:50:17And usually we put the app component
- 4:50:20outside of the components folder because
- 4:50:23app represents the entire application.
- 4:50:27It's sort of the most outer component of
- 4:50:29our application. So it makes sense to
- 4:50:32put it in the outer folder.
- 4:50:35So now let's save this file.
- 4:50:38And let's make sure that all these other
- 4:50:40files are saved. And now we'll open our
- 4:50:43website in the browser and try it out.
- 4:50:47And it looks pretty good. Let's refresh
- 4:50:50just to be safe. And we'll give it a
- 4:50:53try. We'll click this text box and type
- 4:50:56the message hello and press send.
- 4:51:01So, it's working correctly just like
- 4:51:03before. So, we'll go back and now we
- 4:51:07separated each component into its own
- 4:51:11file. And this makes our code easier to
- 4:51:14work with because each file only focuses
- 4:51:17on one component and each file contains
- 4:51:21less code.
- 4:51:23The last thing we're going to do in this
- 4:51:25lesson is to separate the CSS into
- 4:51:28different files. So, let's open app.css.
- 4:51:33And you'll notice that in here it
- 4:51:35contains CSS for multiple components.
- 4:51:39So, we're also going to separate the CSS
- 4:51:42so that each component has its own CSS
- 4:51:45file.
- 4:51:47For example, this CSS code is related to
- 4:51:50the chat input component. So, we'll go
- 4:51:54to the components folder and click it.
- 4:51:57And we'll click up here to create a new
- 4:51:59file. And we're going to name it the
- 4:52:02same as the component, capital C, chat,
- 4:52:05capital I input
- 4:52:08CSS, and press enter.
- 4:52:13and we'll move all the CSS related to
- 4:52:16the chat input component in here. So in
- 4:52:19app.css,
- 4:52:22we'll select this code for the send
- 4:52:24button, the chat input, and the chat
- 4:52:27input container.
- 4:52:29And we'll cut
- 4:52:32and chat input.css,
- 4:52:35we're going to paste that code
- 4:52:38and save.
- 4:52:41And now to load this CSS code, we're
- 4:52:44going to open chatinput.jsx.
- 4:52:48And at the top, we're going to import a
- 4:52:52string
- 4:52:54and the CSS file we want to load, which
- 4:52:57is this one. So we'll type dot slash to
- 4:53:01start in the current folder, which is
- 4:53:03components, and we'll load chat
- 4:53:07input.css.
- 4:53:11and save.
- 4:53:13As a review, this is a feature of Vit.
- 4:53:17If we import a CSS file, it will load
- 4:53:20all the CSS in the file and apply it to
- 4:53:23the website.
- 4:53:25Next, let's go back to app.css.
- 4:53:30And these styles are for the app
- 4:53:32component. So, it actually makes sense
- 4:53:34to leave them inside the app.css file.
- 4:53:39Below this, these styles are for the
- 4:53:42chat message component. So, let's create
- 4:53:45a CSS file for the chat message
- 4:53:48component. We'll go into the components
- 4:53:50folder and click here to create a new
- 4:53:53file. And we'll name this chat
- 4:53:57message.css
- 4:54:00and press enter.
- 4:54:03And we'll go back to app.css CSS and
- 4:54:07we're going to select the styles for the
- 4:54:09chat message user, the chat message
- 4:54:11robot, the text, as well as the chat
- 4:54:15message profile
- 4:54:17and cut.
- 4:54:21And then inside chat message.css,
- 4:54:24we're going to paste the CSS code and
- 4:54:28save.
- 4:54:30And we're going to load this by going
- 4:54:32into chat message.jsx JSX and at the top
- 4:54:36we're going to import a string and we'll
- 4:54:40import this file using dot slash
- 4:54:45chat message
- 4:54:48CSS and save.
- 4:54:53We'll go back to app.css.
- 4:54:56Finally, these styles are for the chat
- 4:54:59messages component.
- 4:55:01First, we'll create a CSS file for this
- 4:55:04component. So, we'll click this folder
- 4:55:07and click here to create a new file and
- 4:55:10we'll name it chat messages.css
- 4:55:15and press enter.
- 4:55:18And in app.css,
- 4:55:20we're going to select this code and cut.
- 4:55:25And in chat messages.css, CSS. We're
- 4:55:28going to paste
- 4:55:32and save.
- 4:55:35And we'll load this by opening chat
- 4:55:37messages.jsx.
- 4:55:39And at the top, we will import a string.
- 4:55:45And we'll locate the file dot slash chat
- 4:55:50messages.css
- 4:55:53and save.
- 4:55:56And finally, we'll go back to app.css
- 4:56:00and make sure that we save all of our
- 4:56:02files.
- 4:56:05Now, let's open our website in the
- 4:56:07browser.
- 4:56:09And everything should look and work
- 4:56:11correctly.
- 4:56:13Let's go back to our code.
- 4:56:16And that's it. We split up our
- 4:56:18JavaScript and our CSS code into
- 4:56:21separate files. And now each component
- 4:56:25has its own JavaScript file and its own
- 4:56:28CSS file. This makes our code a lot
- 4:56:32easier to work with because each file is
- 4:56:35focused on only one component and each
- 4:56:38file contains less code.
- 4:56:41In the next lesson, we'll use this new
- 4:56:43React setup to work on a bigger React
- 4:56:46project, which is an ecommerce website
- 4:56:49that has multiple pages and more
- 4:56:52advanced features. And that's the end of
- 4:56:55this lesson. In this lesson, we created
- 4:56:58a proper React setup using the command
- 4:57:01line, npm, and vit. We reviewed the
- 4:57:06command line, which lets us give
- 4:57:08commands to our computer. We reviewed
- 4:57:11npm which helps us download and use
- 4:57:15external libraries or packages. We
- 4:57:18learned the create vit package which
- 4:57:21helps us create a proper React setup. We
- 4:57:25moved our chatbot project into the new
- 4:57:28React setup. We learned how to use
- 4:57:31eslint which highlights problems in our
- 4:57:34JavaScript code. We reviewed JavaScript
- 4:57:37modules which lets us separate our code
- 4:57:41into different files. And we separated
- 4:57:44each component in the chatbot project
- 4:57:47into its own JavaScript and CSS files.
- 4:57:52Thanks for watching so far. If you find
- 4:57:54this course valuable, you can support
- 4:57:55the channel by liking this video,
- 4:57:58leaving a comment, and subscribing.
- 4:58:00You can also check out the premium
- 4:58:02version of this course where you'll get
- 4:58:04all the lessons ads free broken down
- 4:58:07into small videos as well as additional
- 4:58:10lessons and more projects that are not
- 4:58:12in the public react course. Currently,
- 4:58:15the premium course contains 11 lessons
- 4:58:17and I'll be adding more lessons in the
- 4:58:19future. You can also get a certificate
- 4:58:22of completion, create comments or
- 4:58:24questions on specific topics, get
- 4:58:27progress tracking, search and course
- 4:58:29notes. You can also check out the
- 4:58:31membership where you can get this course
- 4:58:34ads free separated by each lesson, AI
- 4:58:37videos, backend tutorials, and more.
- 4:58:40Thanks again, and let's continue with
- 4:58:42the course. Here are some exercises to
- 4:58:45help you practice what we learned in
- 4:58:47this lesson.
- 4:59:34In this lesson, we're going to start a
- 4:59:36bigger project using React and we'll
- 4:59:38learn an important feature called
- 4:59:41routing.
- 4:59:43By the way, if something in this lesson
- 4:59:45does not work for you, you can check the
- 4:59:47troubleshooting guide in the
- 4:59:49description.
- 4:59:51First, let's take a look at the project
- 4:59:53we're going to build. In our browser,
- 4:59:57we're going to create a new tab.
- 5:00:00And at the top, we're going to go to
- 5:00:03supersimple.dev/proscommerce.
- 5:00:13And we can close these other tabs.
- 5:00:18This is an e-commerce project or an
- 5:00:20online shopping project. We can add
- 5:00:23products to our cart.
- 5:00:26And then we can click here to view our
- 5:00:28cart.
- 5:00:32And this page will display all the
- 5:00:33products in our cart. It will calculate
- 5:00:36the total for our order. and it will
- 5:00:39also calculate a delivery date. And then
- 5:00:42we can create an order.
- 5:00:45And this page will list all of the
- 5:00:47orders that we created. We can also
- 5:00:50click this button to track the order. So
- 5:00:53depending on the delivery date, it will
- 5:00:55show you the progress of the delivery.
- 5:00:58And we'll click here to go back to the
- 5:01:00orders page.
- 5:01:02So notice that this project has multiple
- 5:01:05pages. So, it's much bigger than the
- 5:01:07chatbot project. And we're working with
- 5:01:10more data than before. So, we have
- 5:01:13products, we have a cart, and we have
- 5:01:16orders.
- 5:01:18By the way, if you ever need to reset
- 5:01:20the cart or the orders, you can click
- 5:01:23this reset button here in the bottom
- 5:01:25right, and it will reset everything back
- 5:01:28to the default data. So, let's click the
- 5:01:32top left to go back to the main page.
- 5:01:35And now that we introduced the project,
- 5:01:38we're going to build this with React.
- 5:01:41We'll go back to VS Code
- 5:01:43and we'll collapse the chatbot project
- 5:01:46folder.
- 5:01:48And before we create a new React
- 5:01:50project, let's group these older files
- 5:01:53into a folder. We'll click this HTML
- 5:01:56file and we'll create a folder. And
- 5:02:00let's name it React-basics.
- 5:02:05and we're going to move all these files
- 5:02:06into this folder. Now, a shortcut here
- 5:02:09is to click this file and then press
- 5:02:12shift on your keyboard and click the
- 5:02:14bottom file and it will select all the
- 5:02:17files between those two and we can drag
- 5:02:20this into the folder
- 5:02:25and we'll collapse it. So, that just
- 5:02:28helps us stay a little more organized.
- 5:02:31Next, we're going to set up a new React
- 5:02:33project using VIT. To do that, we're
- 5:02:37going to go to our terminal.
- 5:02:40If you have the VIT server running, you
- 5:02:42can close it by pressing Ctrl + C on
- 5:02:44Windows or control C on Mac.
- 5:02:48So, if you're continuing from the
- 5:02:50previous lesson, the terminal should be
- 5:02:52running inside the chatbot project
- 5:02:54folder. To confirm that, we can run
- 5:02:57print working directory or pwd.
- 5:03:01And it will tell us that we're inside
- 5:03:02the chatbot project folder. Now, to set
- 5:03:05up a new React project, we want to be
- 5:03:08inside the React-Course folder. So, to
- 5:03:11get out of the chatbot project folder,
- 5:03:14we can run the command cd dot dot.
- 5:03:19And if we press pwd,
- 5:03:22we should be inside the react-course
- 5:03:24folder. Another way to do this is to
- 5:03:27just start a new terminal.
- 5:03:33Now we'll use vit again to set up a new
- 5:03:37react project.
- 5:03:39In the command line, we'll run the
- 5:03:41command npx
- 5:03:43create-ash
- 5:03:45vit and we'll type at and give it a
- 5:03:48version. So, you can find the version in
- 5:03:50the description by clicking this link
- 5:03:53and going to lesson six and you'll find
- 5:03:55the version of vit that we're going to
- 5:03:57use as well as the full command.
- 5:04:00By the way, the version that you see in
- 5:04:03this video might be different than the
- 5:04:05version you see on your page. You should
- 5:04:08always use the version that you see on
- 5:04:10your page. I did this because it's hard
- 5:04:13to update the video. So, this lets me
- 5:04:16easily update the version in the future.
- 5:04:20So, select the version on your page.
- 5:04:23Copy.
- 5:04:25So, I'm going to paste the version here
- 5:04:28and press enter.
- 5:04:32For the project name, let's name it
- 5:04:34ecommerce-
- 5:04:37project. Press enter.
- 5:04:40And from this list, we'll press the down
- 5:04:42arrow and select React.
- 5:04:46And from this list, we'll press down and
- 5:04:48select JavaScript.
- 5:04:51And it will set up a new React project
- 5:04:54inside our React-Course folder, which we
- 5:04:57see right here. And now we need to go
- 5:05:00into this folder using the command cd
- 5:05:04ecommerce-
- 5:05:06project.
- 5:05:09And we're going to run npm install to
- 5:05:11install all of the packages inside
- 5:05:14package.json.
- 5:05:16So we'll run npm install.
- 5:05:24Once we have everything installed, we
- 5:05:26can run this project using the command
- 5:05:30npm rundev.
- 5:05:35And now we can open this project using
- 5:05:37this URL in the browser. So we'll select
- 5:05:40this URL and copy.
- 5:05:44And in the browser,
- 5:05:46we're going to create a new tab.
- 5:05:49And then here we'll paste.
- 5:05:54And now we have a new React project.
- 5:05:59Before we start updating the code, we're
- 5:06:01actually going to review a tool called
- 5:06:04Git. Git helps us track changes in our
- 5:06:07code, and it's useful in bigger projects
- 5:06:10because we're going to be making lots of
- 5:06:12changes, and it's easy to forget what we
- 5:06:15changed. So, Git will help us track our
- 5:06:18changes.
- 5:06:20We're going to go to our code
- 5:06:23and we're going to open the Git section
- 5:06:25here.
- 5:06:27If you already have Git installed, it
- 5:06:30should say initialize repository. If you
- 5:06:32do not have Git installed, you can click
- 5:06:35this link in the description, go to
- 5:06:37lesson six, and follow these
- 5:06:39instructions to install Git.
- 5:06:43Once you have git installed, we'll click
- 5:06:45initialize repository.
- 5:06:48Now, a repository is just a folder where
- 5:06:52git is tracking changes. At first, git
- 5:06:56will tell us that all of these files or
- 5:06:59everything inside our react-ourse folder
- 5:07:02is new. And that's because a repository
- 5:07:05starts out as empty. So, git considers
- 5:07:08all of these files as new files.
- 5:07:11We'll go back to the git section
- 5:07:14and we'll scroll up and we're going to
- 5:07:17save all of these files into git. At the
- 5:07:20top, we'll type a message to describe
- 5:07:23our changes. So, let's type the message
- 5:07:26add the starting files and press commit.
- 5:07:33Commit means we're going to save these
- 5:07:34changes into git.
- 5:07:38Now, you might run into an error when
- 5:07:40using git and it tells you you have to
- 5:07:42configure your username and email. To do
- 5:07:45that, you can run these two commands in
- 5:07:47the command line and choose your
- 5:07:49username and choose the email you want
- 5:07:51to use and try to commit again. And now
- 5:07:55it should work.
- 5:07:58If it asks to confirm, you can press
- 5:08:01yes.
- 5:08:04And now git will start tracking any new
- 5:08:06changes that we make. For example, if we
- 5:08:10go into our code and open the e-commerce
- 5:08:13project and change some of these files,
- 5:08:18we type test and save.
- 5:08:21And in the source app.jsx, we type test
- 5:08:26and save.
- 5:08:29And then we can go to the git section.
- 5:08:32And now git will tell us everything that
- 5:08:34we changed in these two files. So as you
- 5:08:38can see git helps us track changes in
- 5:08:41our code.
- 5:08:43Also git lets us undo our changes. So to
- 5:08:47undo this change we can click this arrow
- 5:08:50button here.
- 5:08:53And to undo all of our changes at the
- 5:08:55top we can click this arrow button here.
- 5:09:02So, that's a review of how Git works. It
- 5:09:05can track our changes and it lets us
- 5:09:07easily undo our changes. Let's go back
- 5:09:10to our files
- 5:09:13and remember that in our React setup, we
- 5:09:16have a file called.git
- 5:09:18ignore. So, now we can learn what this
- 5:09:21file does.
- 5:09:23Git ignore tells git which files and
- 5:09:26folders to ignore or not to track
- 5:09:29changes. For example, in this file we
- 5:09:33can see the folder node modules. So this
- 5:09:36tells git that if there are any changes
- 5:09:38inside node modules it will not be
- 5:09:41tracked. The reason we ignore node
- 5:09:44modules is because this folder is very
- 5:09:46large and none of the changes inside
- 5:09:49this folder are changes that we make.
- 5:09:53Instead, we generate this folder using a
- 5:09:56package.json and npm install. So,
- 5:10:00because this folder is generated, it
- 5:10:03doesn't make a lot of sense to save this
- 5:10:05in git. So, that's a review of Git. Git
- 5:10:09helps us track changes in our code and
- 5:10:12doggit ignore tells Git which files and
- 5:10:15folders to ignore or to not track
- 5:10:18changes.
- 5:10:21Now we're ready to start the project.
- 5:10:24First, we're actually going to download
- 5:10:26the HTML and CSS for this project. We'll
- 5:10:30click this link in the description and
- 5:10:33then click here.
- 5:10:36And this will open GitHub.
- 5:10:40And this contains all the HTML and CSS
- 5:10:45for the e-commerce project. The reason
- 5:10:47we're going to use some starting code is
- 5:10:50because this course is focused on React.
- 5:10:53By using this code, we can focus on
- 5:10:56learning React features rather than
- 5:10:58spending our time writing HTML and CSS.
- 5:11:01If you want to practice HTML and CSS,
- 5:11:04you can try to recreate the design on
- 5:11:06your own after this course.
- 5:11:09Another reason we're using some starting
- 5:11:11code is in the real world, we often work
- 5:11:14on an existing project rather than a new
- 5:11:17project. So this will give us some
- 5:11:19practice working on an existing project.
- 5:11:24So to download the starting code, we're
- 5:11:26going to click this code button here and
- 5:11:29we'll click download zip
- 5:11:34and we'll open our downloads folder.
- 5:11:38For me, it's here. And we're going to
- 5:11:42unzip this file. On Windows, we're going
- 5:11:44to rightclick and click extract all
- 5:11:49and extract.
- 5:11:51And on Mac, you can just doubleclick the
- 5:11:53zip file.
- 5:11:56And then we're going to open this
- 5:11:58folder.
- 5:12:00And we're going to move all of these
- 5:12:01files and folders into VS Code. First,
- 5:12:05let's go into VS Code.
- 5:12:08And we're going to organize all of those
- 5:12:10files into a new folder. So, we'll click
- 5:12:13the e-commerce project and create a new
- 5:12:16folder.
- 5:12:18And we'll name this starting dashcode.
- 5:12:24And we'll open our downloads folder
- 5:12:26again.
- 5:12:28And we're going to move all of these
- 5:12:29files into this new folder. So we'll
- 5:12:32select it. And we're just going to drag
- 5:12:35it into starting code.
- 5:12:40And let's save these changes into Git.
- 5:12:43So then Git will start tracking any new
- 5:12:46changes that we make.
- 5:12:48We'll open the git section and here we
- 5:12:51added all of the starting code and at
- 5:12:54the top let's type the message add the
- 5:12:58starting
- 5:13:00HTML and CSS code and commit.
- 5:13:10Let's go back to our files
- 5:13:14and we're going to go to the starting
- 5:13:15code and look for index.html
- 5:13:20and we're going to rightclick this file
- 5:13:23and we're going to open with live
- 5:13:25server.
- 5:13:27And you'll see we have all the HTML and
- 5:13:30CSS for this project. So now we're going
- 5:13:34to create this using React. We'll start
- 5:13:37by creating this main page. We usually
- 5:13:41call this the homepage.
- 5:13:43By the way, a page or a web page is a
- 5:13:47single page like the homepage or the
- 5:13:50cart page. A website is a group of web
- 5:13:54pages.
- 5:13:56To create this homepage, we're going to
- 5:13:58move all of the HTML and CSS into React.
- 5:14:03We'll go back to VS Code
- 5:14:06and we'll open the starting code
- 5:14:09index.html.
- 5:14:12And this file contains the HTML for the
- 5:14:16homepage. And we're going to move this
- 5:14:18HTML into the index.html
- 5:14:21here for the e-commerce project. So,
- 5:14:25let's actually drag this index.html HTML
- 5:14:28and we'll open it at the bottom
- 5:14:31so we can see these files side by side
- 5:14:35and we'll move the HTML in the starting
- 5:14:38code into our project one by one.
- 5:14:42We already have the doc type HTML inside
- 5:14:45here. So we don't need to move it. And
- 5:14:47we also have the HTML and head elements.
- 5:14:52The first thing that we do need to move
- 5:14:54is this title. So we'll select this
- 5:14:57title and then cut.
- 5:15:00And inside the projects HTML file, let's
- 5:15:03replace the title here.
- 5:15:07And now we'll keep going one by one.
- 5:15:11Next, this meta viewport element makes
- 5:15:14the website look good on smaller
- 5:15:16screens. Now, we already have this
- 5:15:19inside our React setup, so we do not
- 5:15:22need to move it over. Let's actually
- 5:15:25select this code and delete it so that
- 5:15:29we can keep track of our progress.
- 5:15:32Next, this code loads a font or a style
- 5:15:36of text called rooto.
- 5:15:39We're going to use this font in the
- 5:15:41e-commerce project. So, we're going to
- 5:15:43move it over. Let's select this code
- 5:15:47and cut.
- 5:15:50And inside our e-commerce project, we're
- 5:15:53going to paste.
- 5:15:55And if you need to, you can fix this
- 5:15:57spacing by selecting these lines and
- 5:16:00press shift tab a few times.
- 5:16:04Next, these lines of code load the CSS
- 5:16:08for our project. Now, remember, when
- 5:16:11using VIT, we actually import our CSS
- 5:16:14files.
- 5:16:16So, we're not going to move these over
- 5:16:18for now, but we'll keep these for later.
- 5:16:21Finally, if we scroll down, the HTML
- 5:16:25inside the body creates the contents of
- 5:16:28the page. Now, the contents of the page
- 5:16:32should be created by React.
- 5:16:35So, let's move this into a React
- 5:16:37component.
- 5:16:39First, we'll create a component for the
- 5:16:41homepage. So, we'll scroll up to the
- 5:16:45source folder and we'll click this
- 5:16:48folder and we'll create a new file.
- 5:16:53Usually, we create a component for each
- 5:16:55page of the website. So, let's name this
- 5:16:58file capital H home capital Page.jsx.
- 5:17:07Now, this website has multiple pages.
- 5:17:10So, let's create a folder to group all
- 5:17:13the pages together. We'll create a
- 5:17:16folder and we'll name it pages.
- 5:17:21And we're going to move the homepage
- 5:17:23into the pages folder.
- 5:17:28And now let's create a component for the
- 5:17:30homepage using function. and we'll name
- 5:17:35it capital H home capital P page
- 5:17:39brackets and curly brackets and we're
- 5:17:42going to return brackets
- 5:17:46we're going to return the HTML for the
- 5:17:48homepage which is right here. So we're
- 5:17:52going to copy all of this HTML into our
- 5:17:55new component. So, one trick we can do
- 5:17:58is on the left, we're going to click
- 5:18:00this arrow to collapse this element. And
- 5:18:03we're going to click this arrow to
- 5:18:05collapse this element as well. And now
- 5:18:08we can select all of this HTML code
- 5:18:12and cut.
- 5:18:15And inside homepage.jsx,
- 5:18:18we're going to paste that code.
- 5:18:21And now we're going to scroll up and fix
- 5:18:24all of these errors.
- 5:18:27First, if we collapse this code
- 5:18:31and collapse this element,
- 5:18:33you can see we're returning two elements
- 5:18:35from this component.
- 5:18:37But remember, we can only return one
- 5:18:40element from a component. To fix this,
- 5:18:43we can wrap this in a fragment. So, at
- 5:18:47the top, we'll create a fragment using
- 5:18:50less than, greater than, and delete this
- 5:18:53closing tag. And at the bottom, we'll
- 5:18:57create the closing tag less than
- 5:18:59slashgreater than. And let's also fix
- 5:19:03the indenting here. We can select all of
- 5:19:06this code and press tab a few times
- 5:19:10until there's an indent before the
- 5:19:12fragment. And for the first line, we're
- 5:19:14going to remove an indent. And that will
- 5:19:17fix the indenting.
- 5:19:20Next, we'll fix these errors. We'll
- 5:19:24expand these elements.
- 5:19:26And remember in React, we have to use
- 5:19:29class name instead of class. So to fix
- 5:19:33these errors, we need to change all of
- 5:19:35these to use class name. To do that, we
- 5:19:38can press CtrlF on Windows or command F
- 5:19:41on Mac to find some code. And we're
- 5:19:45going to search for the code class
- 5:19:48equals. And then we're going to click
- 5:19:50this arrow on the left. And this will
- 5:19:53open the replace menu. So this lets us
- 5:19:56replace class equals with something
- 5:19:58else. We'll replace it with class
- 5:20:02capital N name equals and then press
- 5:20:06this button to replace all.
- 5:20:10And now you'll see that we fixed all of
- 5:20:12the ESLint errors. Finally, we're going
- 5:20:16to scroll to the top and we need to
- 5:20:19export this component so we can use it
- 5:20:21in other files. At the front, we'll type
- 5:20:25export and save.
- 5:20:29So, we moved all the HTML from the
- 5:20:31homepage into our component and we fixed
- 5:20:34all these errors. Now, let's use this
- 5:20:37component in our app. We're going to go
- 5:20:40to app.jsx JSX
- 5:20:44and we'll remove this default HTML.
- 5:20:48So we'll select it
- 5:20:51and delete it
- 5:20:53and we'll insert our homepage component.
- 5:20:57First we need to import it.
- 5:21:01at the top. We'll import curly brackets
- 5:21:06the home page
- 5:21:09from a string and we'll locate this
- 5:21:13file. So, we're going to type dot slash
- 5:21:17to start in the current folder which is
- 5:21:19the source folder and we'll go into
- 5:21:22pages
- 5:21:23slashhome
- 5:21:25page.
- 5:21:28And now we'll insert the homepage into
- 5:21:30our app. So here we'll type less than
- 5:21:34homepage
- 5:21:36slashgreater than. And next we'll fix
- 5:21:39these eslint errors. The react logo and
- 5:21:43vit logo are not used anymore. So we can
- 5:21:46remove them.
- 5:21:49Count and set count are also not used
- 5:21:52anymore. So we can remove them.
- 5:21:57and use state is not used anymore. We
- 5:21:59can remove this as well
- 5:22:02and save.
- 5:22:05And now let's make sure that we saved
- 5:22:07all of our other files. So we'll open
- 5:22:10index.html
- 5:22:12and we'll save it.
- 5:22:14And let's open the starting code
- 5:22:17index.html
- 5:22:18and we'll save this file.
- 5:22:22And now let's take a look at our
- 5:22:23website. We'll open the browser.
- 5:22:28And we can close GitHub for now. And
- 5:22:30we'll open the e-commerce project.
- 5:22:34And you'll see that we have the HTML and
- 5:22:36the text for the e-commerce project
- 5:22:39homepage.
- 5:22:40Now, obviously, this doesn't look very
- 5:22:42good. So, let's move over the CSS. Next,
- 5:22:48to move the CSS into our React project,
- 5:22:51we're going to go back to our code
- 5:22:53and we're going to open the starting
- 5:22:55code index.html.
- 5:22:58And here we're going to scroll up and we
- 5:23:02load three CSS files. So, we're going to
- 5:23:05move these three files into our React
- 5:23:08project. We'll start with general.css.
- 5:23:13If we open styles/shared
- 5:23:16which is here and shared and open
- 5:23:19general.css
- 5:23:23and this file contains CSS for the
- 5:23:26overall website. CSS for the overall
- 5:23:29website goes into the index.css file in
- 5:23:34React.
- 5:23:35So we'll click general.css CSS and we'll
- 5:23:39press control A on Windows or command A
- 5:23:41on Mac to select all of this code and
- 5:23:45we'll copy
- 5:23:49and index.css in the e-commerce project
- 5:23:53we're going to remove all of this
- 5:23:55default code
- 5:23:57and paste
- 5:24:00and save.
- 5:24:03And now if we go back to the starting
- 5:24:05code, we've moved over general.css and
- 5:24:09we can delete this file.
- 5:24:13And now we'll go back to the starting
- 5:24:14code index.html.
- 5:24:19And we can remove this line.
- 5:24:22And we'll move header.css and index.css
- 5:24:26into our React project. So both of these
- 5:24:30style the homepage. So, we'll just move
- 5:24:32these files beside our homepage
- 5:24:34component.
- 5:24:36First, we'll go into styles, shared, and
- 5:24:39header.css,
- 5:24:41and we're going to drag this file
- 5:24:44into our pages folder.
- 5:24:49So, we keep our CSS beside the component
- 5:24:52that it styles.
- 5:24:54And we'll scroll down and find
- 5:24:57styles/pages.
- 5:24:59And we'll move index.css CSS beside the
- 5:25:02homepage as well.
- 5:25:08So header.css styles the header at the
- 5:25:11top of the page and index.css
- 5:25:15styles the main part of the homepage.
- 5:25:18Now a best practice is to use the same
- 5:25:20name as the component here. So we'll
- 5:25:23rightclick index.css CSS and we'll
- 5:25:26rename it
- 5:25:28capital H home capital P page.
- 5:25:35Next, to load these styles, we need to
- 5:25:38import them. We'll open homepage.jsx
- 5:25:42and at the top,
- 5:25:45we're going to import
- 5:25:47a string and we'll import the file dot /
- 5:25:52header.css. CSS
- 5:25:55and we'll also import a string
- 5:26:00slashhome
- 5:26:01page.css
- 5:26:04and save.
- 5:26:07And now if we go back to the starting
- 5:26:10code index.html,
- 5:26:13we moved all of the HTML and CSS into
- 5:26:16our project. So now we can delete this
- 5:26:19file.
- 5:26:23Finally, we'll open app.css.
- 5:26:27And these styles were for the default
- 5:26:29React website. So we don't need these
- 5:26:32anymore. So we can select all of the CSS
- 5:26:35and just remove it and save.
- 5:26:40So make sure that we saved all of our
- 5:26:42files. And now we're going to view the
- 5:26:45website.
- 5:26:48And now it's looking really good. We
- 5:26:50have the structure and the styling that
- 5:26:53we need. The only thing that's missing
- 5:26:55are the images. So let's move the images
- 5:26:59next.
- 5:27:01To move the images, we'll go back to VS
- 5:27:03Code and we'll go to the starting code
- 5:27:08and find the images folder. So this
- 5:27:11contains all the images that we need.
- 5:27:14So, we're going to move this into the
- 5:27:15React project.
- 5:27:18Now, usually we put images in the assets
- 5:27:21folder and then we import the images.
- 5:27:24However, to save time, we're going to
- 5:27:26move this folder into the public folder.
- 5:27:30So, we're going to move it up here and
- 5:27:33into the public folder.
- 5:27:38The reason we move it into the public
- 5:27:40folder is because if we look at
- 5:27:42homepage.js, jsx and we look at an
- 5:27:45image. Notice the source attribute
- 5:27:48starts with images slash. When using
- 5:27:51vit, if we start with a folder name like
- 5:27:54this, vit will look for this inside the
- 5:27:57public folder. So, it finds it here. So,
- 5:28:00by moving our images into the public
- 5:28:02folder, we don't have to change any of
- 5:28:05this code and it just saves time in the
- 5:28:07video. We can move these images into the
- 5:28:10source/assets folder and import them
- 5:28:14later as an exercise.
- 5:28:16So now let's open our website
- 5:28:20and all of the images are loaded. So
- 5:28:23that's how we move an HTML website into
- 5:28:26a React website. We just move over the
- 5:28:30HTML, CSS, and images one by one.
- 5:28:34Let's go back to VS Code and we'll save
- 5:28:37our changes into Git. We'll open the Git
- 5:28:40section. And here we moved all the HTML,
- 5:28:44CSS, and images for the homepage. We're
- 5:28:48going to type a message at the top. Move
- 5:28:51the home page into React and press
- 5:28:56commit.
- 5:29:02Next, we're going to move the checkout
- 5:29:04page to the React setup. We'll go to our
- 5:29:07browser
- 5:29:09and we're going to open the final
- 5:29:10project at superimple.dev
- 5:29:14and we're going to click the cart icon
- 5:29:16in the top right.
- 5:29:19This will open the checkout page where
- 5:29:21we can see our cart and we can place an
- 5:29:24order. So notice that this is a separate
- 5:29:27page from the homepage and this page is
- 5:29:30also on a different URL/checkout.
- 5:29:34So how do we create multiple pages in
- 5:29:37React? To do that, we're going to learn
- 5:29:40a feature called routing. Routing lets
- 5:29:43us create multiple pages in React.
- 5:29:48Let's go back to our code
- 5:29:51and we'll open our files here.
- 5:29:54and we're going to scroll down first to
- 5:29:56the starting code folder.
- 5:29:59So, normally to create multiple pages,
- 5:30:02we need to create multiple HTML files
- 5:30:05like here. Each page has its own HTML
- 5:30:09file like the checkout page and the
- 5:30:12orders page. However, inside these
- 5:30:15files, notice that there's a lot of
- 5:30:18repeated code. So, each file has the doc
- 5:30:21type. it has some meta and link elements
- 5:30:25and in the orders.html
- 5:30:27it has the same meta and link elements.
- 5:30:30So this is not great because if we want
- 5:30:33to change something on one page we have
- 5:30:36to change all of the pages. On the other
- 5:30:39hand, routing lets us create multiple
- 5:30:42pages using one HTML file, and this lets
- 5:30:46us reuse our HTML code. To use routing,
- 5:30:51we're going to install an MPM package
- 5:30:54called React Router. So, let's reopen
- 5:30:57our terminal. You can click here and
- 5:31:01drag up or press control J on Windows or
- 5:31:05command J on Mac to open this terminal.
- 5:31:10Now we currently have the Vit server
- 5:31:12running. So we'll press control C on
- 5:31:15Windows and Mac to stop the V server.
- 5:31:19And to install React Router, we're going
- 5:31:22to run the command npm install
- 5:31:26react-rowout.
- 5:31:29And we'll also type at. So for this
- 5:31:32video, we'll use a specific version of
- 5:31:35React Router just to make sure that it
- 5:31:38works with the video. So click this link
- 5:31:40in the description, go to lesson six,
- 5:31:43and here you'll find the version of
- 5:31:45React Router that we'll use, and you'll
- 5:31:47also find the full command. So again,
- 5:31:50you should always use the version that
- 5:31:52you see on your page.
- 5:31:55So select the version on your page and
- 5:31:58copy it. And then in our command line,
- 5:32:00we're going to paste that version
- 5:32:03and press enter to install.
- 5:32:08So now, how do we use React Router? Like
- 5:32:11most packages, React Router has a
- 5:32:14documentation page that tells you how to
- 5:32:16use it. You can find the documentation
- 5:32:18in the description, but for the video,
- 5:32:21I'm going to show you the parts of the
- 5:32:22documentation that we need. According to
- 5:32:25the documentation, to use React Router,
- 5:32:28we need to load it when we render the
- 5:32:31app component.
- 5:32:33So, in our project, we're going to
- 5:32:35scroll up and open main.jsx,
- 5:32:42and I'll close these other files so we
- 5:32:44can see more of the code.
- 5:32:47So, in this file, we set up React. We
- 5:32:51render our app component on the page.
- 5:32:55According to the documentation, we need
- 5:32:57to wrap this in a component called
- 5:33:00browser router.
- 5:33:02So at the top first we're going to
- 5:33:06import curly brackets
- 5:33:09the browser
- 5:33:11router
- 5:33:14from a string
- 5:33:16and we'll import it from the package
- 5:33:18react router.
- 5:33:21As a review, if we just start with a
- 5:33:23name like this without dot slash, this
- 5:33:27means it's going to load it from node
- 5:33:29modules from the React router package.
- 5:33:32And now around the app, we're going to
- 5:33:35create less than browser
- 5:33:39router greater than.
- 5:33:42And we're going to put the app inside
- 5:33:44here. So we'll cut this
- 5:33:47and paste it in here.
- 5:33:51Let's save.
- 5:33:55Next, we need to do a bit more setup.
- 5:33:58According to the documentation, inside
- 5:34:00our app component, we need to add a
- 5:34:03component called routes.
- 5:34:06So, let's open app.jsx.
- 5:34:09And at the top,
- 5:34:12we're going to import curly brackets
- 5:34:16the component routes
- 5:34:20from a string. And we'll import this
- 5:34:23from the package react-outer.
- 5:34:28And inside this component, we're going
- 5:34:30to insert the routes component using
- 5:34:33less than routes greater than.
- 5:34:38So the routes component tells React all
- 5:34:41the pages that are in our website. So to
- 5:34:44add a page to our website, we're going
- 5:34:47to use another component called route. A
- 5:34:50route is basically a page.
- 5:34:54So first we need to import that at the
- 5:34:57top. We're already importing from React
- 5:34:59Router. So we're going to add a comma
- 5:35:02and import the route component.
- 5:35:06So inside routes, we're going to add
- 5:35:08less than route greater than. So again,
- 5:35:13a route basically adds a page to our
- 5:35:17website.
- 5:35:18And we're going to give this route two
- 5:35:20props. The first prop is called path
- 5:35:24equals double quotes. This tells React
- 5:35:27the URL path of this page.
- 5:35:30The URL path is the last part of the URL
- 5:35:34like /checkout or slash orders. So first
- 5:35:38let's add the homepage. For the
- 5:35:41homepage, the URL path is empty.
- 5:35:45To set the URL path as empty, we're
- 5:35:48going to set it to slash.
- 5:35:51And then we're going to give route a
- 5:35:52second prop called element equals curly
- 5:35:57brackets. So this tells React which
- 5:36:00element or component to display when we
- 5:36:04go to this URL path for the homepage. We
- 5:36:07want to display this homepage component.
- 5:36:10So let's select it and cut.
- 5:36:15And inside here we're going to paste.
- 5:36:20So this tells React that when we go to
- 5:36:22the URL path slash or empty, it's going
- 5:36:26to display our homepage. So let's try it
- 5:36:30out. We'll save this file
- 5:36:33and then inside the terminal, we're
- 5:36:35going to start the VIT server using npm
- 5:36:39rundev.
- 5:36:42And we'll visit this URL.
- 5:36:44I already have it open. So I'll open my
- 5:36:47browser.
- 5:36:49And now if the URL path ends with slash
- 5:36:52or empty, it's going to display the
- 5:36:55homepage.
- 5:36:58Next, let's use React Router to add a
- 5:37:01checkout page. We'll go back to our code
- 5:37:05and to add another page to our website,
- 5:37:08we're going to use the route component
- 5:37:10again. So, we'll create less than route
- 5:37:14greater than and we'll give it two
- 5:37:16props. the path equals double quotes.
- 5:37:20And for the checkout page, we'll set the
- 5:37:23URL path to checkout.
- 5:37:26This means this page will be displayed
- 5:37:28when we go to /checkout.
- 5:37:32And we'll give it another prop element
- 5:37:35equals curly brackets.
- 5:37:38And right now, we didn't create a
- 5:37:40component for the checkout page yet. So
- 5:37:43let's just create a div here just to
- 5:37:45make sure that this is working. So we'll
- 5:37:48create a div element and inside let's
- 5:37:51type test checkout page and save.
- 5:37:58So now if we go to our project
- 5:38:02and in the URL path we're going to go to
- 5:38:05slash checkout
- 5:38:07and press enter.
- 5:38:10It displays the div with the text that
- 5:38:12we created.
- 5:38:14So, as you can see, routing is actually
- 5:38:17pretty simple. We just tell React which
- 5:38:20pages we want in our website and then
- 5:38:23what to display for each page.
- 5:38:28Now, let's learn some more details about
- 5:38:30routing. If we look at our code, we now
- 5:38:33have multiple pages in our website. And
- 5:38:37all of these pages use one HTML file
- 5:38:41which is index.html.
- 5:38:44So this HTML is shared between all of
- 5:38:47our pages and this reduces repetition.
- 5:38:51By the way, this is called a single page
- 5:38:55application or an SPA. This means we
- 5:38:59only have one HTML file and instead we
- 5:39:03use React to create multiple pages.
- 5:39:07Next, we'll use some shortcuts with
- 5:39:09React Router. The first shortcut is
- 5:39:13instead of path equals slash, React
- 5:39:16Router has a prop for this called index.
- 5:39:21So, this does the same thing as path
- 5:39:23equals slash. The second shortcut is
- 5:39:26currently we don't have anything inside
- 5:39:29these route components. So, we can use
- 5:39:32the self-closing element shortcut. We'll
- 5:39:35remove the closing tag.
- 5:39:38And at the end here, we'll add a space
- 5:39:40and a slash. And same thing here, we'll
- 5:39:43remove the closing tag.
- 5:39:46And add a space and slash and save.
- 5:39:51And now we'll save these changes into
- 5:39:54Git. We'll open the git section.
- 5:39:58And here it tells us we installed React
- 5:40:01Router.
- 5:40:02And in app.jsx jsx. We added some routes
- 5:40:06or some pages to our website. In
- 5:40:09main.jsx,
- 5:40:11we loaded the React router. So, let's
- 5:40:14create a message to describe these
- 5:40:15changes. Create multiple
- 5:40:20pages using React-Router
- 5:40:25and commit.
- 5:40:33Now, let's move the checkout page into
- 5:40:35our app. We'll go back to our files
- 5:40:39and first we'll open app.jsx.
- 5:40:44And instead of displaying a div when we
- 5:40:46go to / checkout, let's remove it.
- 5:40:50And let's create a component for the
- 5:40:52checkout page. Just like the homepage in
- 5:40:56the pages folder, we'll click this and
- 5:40:59we'll create a new file and we'll name
- 5:41:02this capital C checkout capital P
- 5:41:06page.jsx.
- 5:41:11And inside here, we'll create a new
- 5:41:13component using function
- 5:41:16checkout
- 5:41:18page brackets and curly brackets. and
- 5:41:22we'll return brackets
- 5:41:26and we'll move the HTML for the checkout
- 5:41:28page in here. So, we'll go to the
- 5:41:31starting code folder
- 5:41:34and we'll open checkout.html.
- 5:41:38So, this is the HTML file for the
- 5:41:40checkout page.
- 5:41:42We're going to scroll down to the body
- 5:41:44element first and we're going to move
- 5:41:46over this HTML.
- 5:41:49So we'll click here to collapse this
- 5:41:51code and click here to collapse this
- 5:41:53code as well and we'll select all this
- 5:41:56HTML and we'll cut
- 5:42:01and then inside checkout page inside the
- 5:42:04return we're going to paste
- 5:42:08and let's scroll up and we'll fix these
- 5:42:11errors.
- 5:42:14So first if we collapse these two
- 5:42:16elements remember we're returning two
- 5:42:18elements from this component which
- 5:42:20causes an error. To fix this we'll wrap
- 5:42:23this in a fragment
- 5:42:30and we'll fix the indenting. Now you can
- 5:42:32fix it manually like we did before but
- 5:42:35you can also use this shortcut. We can
- 5:42:38press controlshiftp on Windows or
- 5:42:41commandshiftp on Mac and this will open
- 5:42:44up a list of commands that we can use in
- 5:42:47VS Code. So we're going to search for
- 5:42:49the command format document
- 5:42:54and we can click it or press enter
- 5:42:57and it will automatically format the
- 5:42:59code in this document and fix the
- 5:43:01indenting.
- 5:43:03Next, if we expand these elements,
- 5:43:06remember in React, we have to use class
- 5:43:09name instead of class. To replace these,
- 5:43:12we'll press control F or command F to
- 5:43:15find code. And we'll find the code class
- 5:43:19equals and click this arrow. And we're
- 5:43:22going to replace it with class name
- 5:43:25equals and click replace all.
- 5:43:30And finally the last eslint error here
- 5:43:33is this component is not used in this
- 5:43:35file. So we need to export it
- 5:43:39and save.
- 5:43:41And now let's use this component in our
- 5:43:44route. We'll open app.jsx JSX
- 5:43:48and at the top we'll import the page
- 5:43:50using import curly brackets the checkout
- 5:43:56page
- 5:43:58from a string and we'll import dot slash
- 5:44:02pages slashcheckout page
- 5:44:07and we'll use it inside this route using
- 5:44:11less than checkout page slashgreater
- 5:44:16and save.
- 5:44:19And now let's check our website.
- 5:44:22And if we go to the path /checkout, you
- 5:44:25can see we have the HTML for the
- 5:44:28checkout page.
- 5:44:31Now, let's add the rest of the code for
- 5:44:33the checkout page. We'll go back to VS
- 5:44:36Code
- 5:44:37and we're going to go to the starting
- 5:44:39code and open checkout.html.
- 5:44:44and we're going to move this over one by
- 5:44:46one. So, first, what do we do for this
- 5:44:49title? Right now, all of our pages are
- 5:44:52sharing one HTML file here. So, all of
- 5:44:56the pages have the same title,
- 5:44:58e-commerce project. However, we want
- 5:45:01each page to have a different title. So,
- 5:45:04how do we do that? Well, in React, it's
- 5:45:07actually pretty simple. We just have to
- 5:45:09take this title and move it into our
- 5:45:11page component. So, we'll select this
- 5:45:15and cut.
- 5:45:17And then we'll scroll up and open
- 5:45:19checkout page.jsx.
- 5:45:22And if we want this page to have a
- 5:45:24different title, all we have to do is to
- 5:45:27paste the title at the top
- 5:45:31and save.
- 5:45:33So, if we look at our website
- 5:45:36now, the checkout page has the title
- 5:45:38checkout in the tabs. So, we'll go back
- 5:45:41to our code.
- 5:45:43And we're also going to set the title in
- 5:45:45the homepage. So we'll open the
- 5:45:47homepage.
- 5:45:49And in the homepage,
- 5:45:52let's create a title.
- 5:45:55And we're going to set the title to
- 5:45:58ecommerce
- 5:46:00project and save.
- 5:46:04And now we can scroll down
- 5:46:07to the index.html HTML
- 5:46:10and we can remove this title because
- 5:46:12we're going to set it using React
- 5:46:16and save.
- 5:46:19And now let's move the rest of the HTML
- 5:46:21in the checkout page.
- 5:46:24If we scroll down here, these meta and
- 5:46:27link elements have already been moved to
- 5:46:30index.html.
- 5:46:33So this is being shared across all of
- 5:46:35our pages. So, we actually don't need to
- 5:46:38move this over anymore because it's
- 5:46:41shared. So, we can remove all of this
- 5:46:43code.
- 5:46:46And finally, we're going to move over
- 5:46:47the CSS for this page
- 5:46:51for general.css.
- 5:46:53Remember that we already copied these
- 5:46:55styles into index.css,
- 5:46:58so we don't need to move this anymore.
- 5:47:03And now we'll just move checkout
- 5:47:04header.css CSS and checkout.css into our
- 5:47:08React project.
- 5:47:11So, we'll go to styles/pages/checkout
- 5:47:16and we'll move checkout header.css
- 5:47:19into the React project into here
- 5:47:24and scroll down. And we'll also move
- 5:47:27checkout.css CSS into our React project
- 5:47:30beside the component.
- 5:47:35And now that we moved everything over,
- 5:47:37we can delete the checkout.html file.
- 5:47:40So, we can delete this file.
- 5:47:46Finally, let's scroll up
- 5:47:48and we're going to rename the CSS file
- 5:47:51to match the component name. So, we'll
- 5:47:54rightclick and rename.
- 5:47:58checkout page.css.
- 5:48:03And now we'll load these two CSS files
- 5:48:06using import. So in checkout page.jsx
- 5:48:10at the top, we'll import a string and
- 5:48:15we'll import the CSS file slash
- 5:48:19checkout-
- 5:48:21header.css.
- 5:48:25And we'll also import
- 5:48:28dot slashcheckout
- 5:48:30page.css
- 5:48:33and save.
- 5:48:36We'll go to our website.
- 5:48:39And now everything looks really good. So
- 5:48:41we finished moving all the HTML and CSS
- 5:48:45for the checkout page into our React
- 5:48:47project.
- 5:48:50Let's go back to our code and we'll save
- 5:48:52these changes into git. We'll open the
- 5:48:55git section
- 5:48:58and in app.jsx
- 5:49:00we use the checkout page component for
- 5:49:02/checkout
- 5:49:04and then we moved over the CSS files and
- 5:49:07in here we moved over the HTML code as
- 5:49:11well as the title. Let's type a message
- 5:49:14to describe these changes.
- 5:49:16move checkout page into React and
- 5:49:22commit.
- 5:49:28Before we continue moving pages, there's
- 5:49:30an issue that we need to fix. If we open
- 5:49:34the website and click the top left icon,
- 5:49:39normally this should take us to the
- 5:49:41homepage, but now it's blank. So why is
- 5:49:44this not working? Notice in the URL the
- 5:49:48path is /index.html.
- 5:49:52Before this website was split up into
- 5:49:54multiple HTML files and index.html
- 5:49:59created the homepage.
- 5:50:01But now we're using routing. So we set
- 5:50:04the URL paths ourselves. In our project
- 5:50:08we set the path slash to be the
- 5:50:10homepage. and we set the path /checkout
- 5:50:14to be the checkout page. So, we need to
- 5:50:16go into our code and update all of the
- 5:50:19links to go to the correct URL path.
- 5:50:22We'll go back to VS Code.
- 5:50:25And to update all of our links, we're
- 5:50:28going to go to the search section of VS
- 5:50:31Code. We can press control shift F on
- 5:50:35Windows or commandshift F on Mac.
- 5:50:39And here we can search inside all of our
- 5:50:41files. First, let's search for the code
- 5:50:45index.html.
- 5:50:47This will give us a list of a elements
- 5:50:50or link elements.
- 5:50:53And href means where these links should
- 5:50:55go. So now instead of index.html,
- 5:51:00these links should go to slash. To
- 5:51:03replace index.html HTML in all these
- 5:51:06places. We can click this arrow on the
- 5:51:08left to open the replace menu. And we'll
- 5:51:12replace this with slash. And then click
- 5:51:16here to replace all. And confirm.
- 5:51:21And let's replace the links for the
- 5:51:24other pages as well. So up here, we'll
- 5:51:27also search for checkout.html.
- 5:51:31And in this search result, we have a
- 5:51:34link element with the href
- 5:51:35checkout.html.
- 5:51:37And in our router, we set the URL path
- 5:51:40for the checkout page to /checkout.
- 5:51:43So we'll replace all of these with
- 5:51:46slashcheckout
- 5:51:48and replace all.
- 5:51:52And we'll do the same thing for the
- 5:51:54orders page, which is orders.html.
- 5:51:59And here we'll change it to slash orders
- 5:52:03and replace.
- 5:52:08And finally, we also have a tracking
- 5:52:10page. So here we're going to search for
- 5:52:15tracking.html
- 5:52:17and replace it with slash tracking and
- 5:52:21replace all.
- 5:52:25And now all of our links should work
- 5:52:27correctly because they match our router.
- 5:52:31If we open the website
- 5:52:33and at the top we change the URL path to
- 5:52:37/checkout
- 5:52:40it opens the checkout page. If we click
- 5:52:43the top left icon
- 5:52:46this should now go to the URL path slash
- 5:52:48or empty and it will open the homepage.
- 5:52:52So let's save this into Git. We'll open
- 5:52:55the git section
- 5:52:58and we just updated all the links in
- 5:53:00these files to match the router.
- 5:53:04Let's type the message update links to
- 5:53:08match the router and commit.
- 5:53:18Next, let's move the orders page into
- 5:53:21our React project. Feel free to pause
- 5:53:23the video if you want to try it yourself
- 5:53:25first. So, we'll go back to our files
- 5:53:30and first let's create a component for
- 5:53:32the orders page. Inside the pages
- 5:53:35folder, we'll create a new file and
- 5:53:38we'll name it orders page.jsx
- 5:53:43and press enter.
- 5:53:46And inside here we'll create a component
- 5:53:49function
- 5:53:51orders page brackets and curly brackets.
- 5:53:54And we'll return brackets
- 5:53:58and we'll move over the HTML. So we'll
- 5:54:01go back to the starting code folder and
- 5:54:04open orders.html.
- 5:54:08We'll start with the body. So we'll
- 5:54:10scroll down and we can collapse these
- 5:54:13two elements and we'll select the HTML
- 5:54:17and cut
- 5:54:19and inside the orders page inside the
- 5:54:22return we'll paste.
- 5:54:25We'll scroll up
- 5:54:30and collapse these elements
- 5:54:32and we're going to wrap these in a
- 5:54:34fragment less than greater than and
- 5:54:37remove this. and at the end less than
- 5:54:41slashgreater than.
- 5:54:43And to fix the indenting, we can use the
- 5:54:45shortcut control shiftp or commandshiftp
- 5:54:50and search for the command format
- 5:54:52document and press enter.
- 5:54:56Again, we need to rename class to class
- 5:54:59name. So, we'll press control F or
- 5:55:02command F to find code. We'll search for
- 5:55:04class equals. Click here and replace it
- 5:55:07with class name and replace all.
- 5:55:12And finally, at the top, we'll export
- 5:55:15this component.
- 5:55:17Let's save this file.
- 5:55:20Let's go back to orders.html.
- 5:55:25Scroll up and we'll move over this code.
- 5:55:29So, for the title, we're going to move
- 5:55:30it into the component. We'll select it,
- 5:55:34cut, and at the top in orders page,
- 5:55:39we're going to paste it at the top.
- 5:55:43Let's go back to orders.html.
- 5:55:48And these meta and link elements are
- 5:55:50already inside index.html.
- 5:55:53So, we can remove this.
- 5:55:57And finally, we'll move the CSS files
- 5:55:59over. So, general.css CSS was already
- 5:56:02moved into index.css.
- 5:56:05We already moved over header.css up
- 5:56:09here. So, we just need to move the
- 5:56:10orders.css.
- 5:56:12So, inside starting code, we'll go into
- 5:56:15styles/pages
- 5:56:17and we'll move this orders.css file.
- 5:56:23So, we'll move it up here
- 5:56:27beside the orders page component.
- 5:56:30And now we moved over everything inside
- 5:56:33orders.html.
- 5:56:35So we can scroll down and delete this
- 5:56:38file.
- 5:56:42Next, we should rename orders.css
- 5:56:45to match the component name. So we'll
- 5:56:48rightclick and rename
- 5:56:52orders page.css.
- 5:56:57And finally, we'll load the CSS files.
- 5:57:00So, in orders page.jsx
- 5:57:03at the top, we're going to import a
- 5:57:07string. And remember, this requires the
- 5:57:10header and the orders page CSS.
- 5:57:14So, first we'll import dot slash
- 5:57:16header.css.
- 5:57:19And we'll also import
- 5:57:22dot slash orders page.css. CSS and save.
- 5:57:30And now we moved over all the HTML and
- 5:57:33CSS for the orders page.
- 5:57:36So now let's create a route for this
- 5:57:38page. We'll go to app.jsx.
- 5:57:43And first at the top we're going to
- 5:57:46import curly brackets the orders page
- 5:57:53from a string. and we'll locate dot
- 5:57:56slashpages slash orders page and then
- 5:58:01down here we're going to add another
- 5:58:03route. So at the end we'll add less than
- 5:58:07route greater than and we can remove
- 5:58:11this
- 5:58:12and use the self-closing element
- 5:58:15shortcut
- 5:58:17and we'll give it two props. We'll set
- 5:58:19the path equals double quotes and we'll
- 5:58:23put the orders page on the path slash
- 5:58:26orders
- 5:58:28and we'll give it an element to display
- 5:58:31equals curly brackets. And for the
- 5:58:34orders page, we'll display the element
- 5:58:36less than orders page slashgrater than
- 5:58:42and save.
- 5:58:45And now let's view the website. We'll
- 5:58:48open the website
- 5:58:51and on the homepage we're going to click
- 5:58:53this orders link to go to slash orders
- 5:58:58and it displays the orders page. So that
- 5:59:01looks pretty good. And now we finished
- 5:59:03moving the orders page into React.
- 5:59:07So let's go back to VS Code.
- 5:59:10And in the starting code, there's one
- 5:59:14more page to move over, which is the
- 5:59:16tracking page. But I'll leave this as an
- 5:59:19exercise.
- 5:59:20So now let's save these changes into
- 5:59:22Git. Open the Git section.
- 5:59:26In app.jsx, we added a new route for the
- 5:59:29orders page and we moved over the CSS
- 5:59:32and the HTML.
- 5:59:35So we'll type the message move orders
- 5:59:39page to React
- 5:59:42and commit.
- 5:59:50Next, we're going to separate our page
- 5:59:52into smaller components.
- 5:59:54For example, if we look at our website,
- 5:59:58notice that on the orders page, there's
- 6:00:00this header at the top. And if we click
- 6:00:03the top left and go to the homepage, we
- 6:00:06have the same header at the top of the
- 6:00:09page. So, it's a good idea to separate
- 6:00:12the header into a component so we can
- 6:00:14reuse it between these pages. We'll go
- 6:00:18back to VS Code
- 6:00:21and we'll open our files and we're going
- 6:00:24to open homepage.jsx.
- 6:00:28And at the top here, we have the HTML
- 6:00:30for the header. If we collapse it, this
- 6:00:34is all the HTML for the header.
- 6:00:37Let's separate this into a shared
- 6:00:39component.
- 6:00:41Now, because this component is shared
- 6:00:43between multiple pages, we usually put
- 6:00:46shared components in a folder called
- 6:00:48components. So, inside our source
- 6:00:51folder, we're going to click it and
- 6:00:53create a new folder
- 6:00:56and we'll call it components.
- 6:01:01So the components folder is for shared
- 6:01:03components and the pages folder is for
- 6:01:06components that are specific to a page.
- 6:01:10Inside the components folder, we'll
- 6:01:12create a new file.
- 6:01:15And let's name it header.jsx.
- 6:01:21And in here we'll create a component for
- 6:01:23the header using function
- 6:01:26header brackets and curly brackets. And
- 6:01:30we'll return brackets.
- 6:01:34And we'll move over the HTML.
- 6:01:37So we'll open homepage.jsx JSX
- 6:01:41and we're going to select the HTML for
- 6:01:43the header and cut
- 6:01:50inside header.jsx.
- 6:01:52Inside here, we're going to paste.
- 6:01:56And we'll fix the indenting by pressing
- 6:01:58control shiftp or command shiftp and
- 6:02:02search for format document and press
- 6:02:05enter.
- 6:02:08And we'll scroll to the top. And lastly,
- 6:02:12we're going to export this component to
- 6:02:14remove this error.
- 6:02:18Next, we should also move over the CSS
- 6:02:20for the header. Now, luckily the CSS for
- 6:02:24the header has already been separated
- 6:02:26into header.css.
- 6:02:28So, we can just move this over to beside
- 6:02:31the component.
- 6:02:36So, we want to keep the CSS close to the
- 6:02:39component so it's easy to find. So at
- 6:02:42the top of this we can import the CSS
- 6:02:45using import a string and we'll import
- 6:02:49dot slash header.css
- 6:02:53and save.
- 6:02:56So now that we created this header
- 6:02:58component, we can reuse it in the
- 6:03:00homepage and the orders page. We'll open
- 6:03:04homepage.jsx.
- 6:03:06And at the top first, we don't need to
- 6:03:09import the header anymore. It's already
- 6:03:11done by this component. So we can remove
- 6:03:13it. And let's import the component using
- 6:03:17import curly brackets. The header
- 6:03:23from a string.
- 6:03:25And now we need to locate this file. So
- 6:03:28currently we're inside the pages folder.
- 6:03:31So we need to get out of this folder
- 6:03:33using dot dot slash and then go into the
- 6:03:36components folder. components slash
- 6:03:39header.
- 6:03:41And now we'll use this component in the
- 6:03:43HTML. So down here
- 6:03:47we'll insert less than header
- 6:03:50slashgreater than and save.
- 6:03:55So now this HTML is a bit easier to work
- 6:03:58with because we split it up into smaller
- 6:04:01pieces. Let's do the same thing with the
- 6:04:03orders page. We'll open the orders page.
- 6:04:07jsx
- 6:04:08and at the top we don't need to import
- 6:04:10header.css CSS and instead we'll import
- 6:04:14curly brackets the header component from
- 6:04:19a string and we'll locate dot dot slash
- 6:04:23components
- 6:04:25slash header
- 6:04:27and down here instead of this HTML we
- 6:04:30can remove this and just use the
- 6:04:33component less than header slashgrater
- 6:04:38than this helps us reuse and save a lot
- 6:04:41of code. Let's save
- 6:04:45and then view the website.
- 6:04:48The header looks good in the homepage.
- 6:04:50And if we click here to go to the orders
- 6:04:53page, the header also looks good in the
- 6:04:55orders page. So, we're able to reuse the
- 6:04:58header component.
- 6:05:02Next, there's a small issue with the
- 6:05:04header. when we click a link like this
- 6:05:08link in the top left, it actually
- 6:05:11reloads the page when we go to another
- 6:05:13page. So by default, link elements or a
- 6:05:17elements reload the page. Now this made
- 6:05:21sense before when our website had
- 6:05:23multiple HTML files. To go from one file
- 6:05:27to another file, we have to reload the
- 6:05:29page. However, now that we're using
- 6:05:32routing and a single page application,
- 6:05:35we don't have to reload every time, we
- 6:05:38can just use JavaScript to switch
- 6:05:40between pages.
- 6:05:42To do this, React Router provides a
- 6:05:45component called link. So, we'll go back
- 6:05:48to our code
- 6:05:51and we'll open the header component
- 6:05:55and at the top
- 6:05:57we're going to import
- 6:06:00curly brackets
- 6:06:02the link component from
- 6:06:06the package react-rowout.
- 6:06:10So, the link component lets us go to
- 6:06:13another page without reloading.
- 6:06:17To use it, we just have to replace our
- 6:06:20link elements or a elements with this
- 6:06:23link component. So here, let's replace
- 6:06:26this with link and at the end slash
- 6:06:32link.
- 6:06:34And also instead of the href attribute,
- 6:06:37the link component uses a prop called
- 6:06:40to. So this will link to this page.
- 6:06:46Let's scroll down
- 6:06:49and in the header we have two other
- 6:06:51links, the orders link and the cart
- 6:06:54link. So we'll switch both of these as
- 6:06:56well. So here we'll switch it to link
- 6:07:01and slashlink
- 6:07:04and we'll switch the href to two.
- 6:07:08And finally, switch this to link
- 6:07:12and slashlink
- 6:07:14and switch href to two and save.
- 6:07:20And now let's open our website to try it
- 6:07:23out.
- 6:07:25If we click this link to go to the
- 6:07:27orders page, you'll notice it's a little
- 6:07:30bit faster because now it's using
- 6:07:32JavaScript to essentially display a
- 6:07:35different page without reloading.
- 6:07:38So when using React Router, we should
- 6:07:41use the link component instead of the A
- 6:07:44element because the link component lets
- 6:07:47us go to another page without reloading.
- 6:07:51Now let's save these changes into git.
- 6:07:55We'll go to the git section
- 6:07:58and here we split up the header into a
- 6:08:01separate component. And then inside the
- 6:08:04header, we switch to using the link
- 6:08:07component to switch between pages.
- 6:08:10Let's type the message
- 6:08:12split header into a component
- 6:08:17and use link and commit.
- 6:08:27In the next lesson, we'll add a backend
- 6:08:30to this project and make it interactive.
- 6:08:33So, we'll be able to display a list of
- 6:08:35products and add products to a cart and
- 6:08:38create an order. And that's the end of
- 6:08:41this lesson. In this lesson, we started
- 6:08:44the ecommerce project using React and
- 6:08:47Vit. We reviewed Git, which helps us
- 6:08:51track changes in our code. We learned
- 6:08:54routing, which lets us create multiple
- 6:08:57pages in React. We created the homepage,
- 6:09:01the checkout page, and the orders page
- 6:09:03in React. We updated all the links to
- 6:09:06work with routing, and we separated the
- 6:09:10header into a component.
- 6:09:13Here are some exercises to help you
- 6:09:15practice what we learned in this lesson.
- 6:10:04In this lesson, we're going to start
- 6:10:05making our e-commerce project
- 6:10:07interactive and then we'll learn how to
- 6:10:10use a backend with this project.
- 6:10:13By the way, if something in this lesson
- 6:10:15does not work for you, you can check the
- 6:10:18troubleshooting guide in the
- 6:10:19description.
- 6:10:21First, if we look at our website,
- 6:10:25currently we only have three products on
- 6:10:28this website, but in the final project,
- 6:10:32we have a lot more products. So, we're
- 6:10:35going to add all of these products to
- 6:10:37our website. So, let's go to the code
- 6:10:40for the homepage.
- 6:10:42We'll open our code and go to the files
- 6:10:47and we're going to find the pages folder
- 6:10:50and open homepage.jsx.
- 6:10:54So this component represents the
- 6:10:56homepage. And we're going to scroll down
- 6:10:59and find the element with a class
- 6:11:02product-container.
- 6:11:04And we're going to click here to
- 6:11:05collapse this element. And we're going
- 6:11:07to collapse this other product
- 6:11:09container. and this other product
- 6:11:12container as well.
- 6:11:14Each product container represents one
- 6:11:17product on the page. To add more
- 6:11:20products, we could make a copy of this
- 6:11:23code and add each product manually.
- 6:11:26However, that would be very repetitive.
- 6:11:29Instead, we can generate this HTML using
- 6:11:33React. We already learned this technique
- 6:11:36in the chatbot project where we
- 6:11:38generated a list of chat messages on the
- 6:11:41page. So, we're going to review how to
- 6:11:43generate the HTML.
- 6:11:45First, we need to save the data for
- 6:11:48these products in our JavaScript. To
- 6:11:51save time, I already did this. So, if we
- 6:11:53go to our files and we go to the
- 6:11:56starting code folder
- 6:11:58and we open this data folder inside
- 6:12:02products.js, JS I already saved the data
- 6:12:06for the products in this array. So we
- 6:12:09can just use this as our data.
- 6:12:12Each object in the array represents one
- 6:12:15product. So it has a name, an image, and
- 6:12:18a price and some other details. The next
- 6:12:21step is to use this data to generate the
- 6:12:25HTML. We'll go back to the homepage
- 6:12:30and first we're going to import that
- 6:12:32products array. So at the top we're
- 6:12:36going to import curly brackets the
- 6:12:39products array from a string and we're
- 6:12:43going to locate that file. So first we
- 6:12:46need to get out of the pages and source
- 6:12:47folder using dot dot dot slash and then
- 6:12:51we'll find the starting code folder and
- 6:12:54the data folder. starting dashcode slash
- 6:12:58data slash and the products file.
- 6:13:03Next, we're going to loop through this
- 6:13:05products array. So, at the bottom here
- 6:13:09above these product containers, we're
- 6:13:12going to insert using curly brackets the
- 6:13:15products array dot map and brackets. And
- 6:13:19we're going to give map a function
- 6:13:21brackets arrow and curly brackets.
- 6:13:25As a review, do map takes each value in
- 6:13:29an array and transforms it. In this
- 6:13:32parameter, we get each value in this
- 6:13:35array, which is a product.
- 6:13:39So again, a product is just this object
- 6:13:42and it has some details about the
- 6:13:43product inside.
- 6:13:45So, we'll go back
- 6:13:48and we're going to transform this
- 6:13:50product object into this HTML.
- 6:13:54To do that, we're just going to copy
- 6:13:56this HTML
- 6:13:58and inside map, we're going to return
- 6:14:02brackets
- 6:14:04and paste that HTML.
- 6:14:07And to fix the formatting, we're going
- 6:14:09to press control or commandshiftp
- 6:14:13and then search for format document
- 6:14:16and press enter.
- 6:14:19And let's scroll up.
- 6:14:22And remember, whenever we loop through
- 6:14:24an array like this, we need to give each
- 6:14:26element a key prop equals curly
- 6:14:30brackets. So the key helps React keep
- 6:14:33track of changes in this array. Now the
- 6:14:36key has to be unique. So looking at the
- 6:14:39data, each product has a unique ID
- 6:14:43property.
- 6:14:45So we'll go back and we're going to use
- 6:14:47the product. ID as the key. So this
- 6:14:53basically takes each product object and
- 6:14:56transforms it into this HTML.
- 6:14:59So now let's save
- 6:15:02and we'll check the website.
- 6:15:05And as you can see, we took each product
- 6:15:08and transformed it into some HTML.
- 6:15:12Now, obviously, we don't want the
- 6:15:13product to be socks every time. So,
- 6:15:16let's insert the details of each
- 6:15:18product. We'll go back to our code, and
- 6:15:23instead of displaying the socks every
- 6:15:25time, we're going to remove this. and
- 6:15:28insert.
- 6:15:30And each product object has a name
- 6:15:32property. So we'll insert the
- 6:15:35product.name.
- 6:15:37And let's also replace the product
- 6:15:40image. So instead of showing the socks
- 6:15:43image every time, we'll remove this and
- 6:15:46we'll insert the product.
- 6:15:49Property.
- 6:15:51Next, we'll insert the ratings. The way
- 6:15:55this works is that each rating has an
- 6:15:57image. And we just have to multiply the
- 6:16:00rating by 10 to get the image name. So
- 6:16:03for this one, we're going to put open
- 6:16:06curly bracket and close curly bracket
- 6:16:09around this value. And we're going to
- 6:16:11change this to a template string using
- 6:16:14back ticks and back ticks. And then
- 6:16:18instead of 45 every time, we're going to
- 6:16:21insert. And remember for template
- 6:16:23strings to insert we use dollar curly
- 6:16:26brackets. So in here we're going to
- 6:16:28insert the product dot rating dot stars
- 6:16:34and then multiply it by 10. And this
- 6:16:37should give us the ratings image that we
- 6:16:39need. Next we're going to replace the
- 6:16:42rating count. So curly brackets and
- 6:16:45we'll insert the product.
- 6:16:50count.
- 6:16:51Next, we'll insert the price. So,
- 6:16:54instead of this price every time, we'll
- 6:16:57insert the product dot price sets. And
- 6:17:03we usually save money in cents because
- 6:17:06many programming languages have trouble
- 6:17:08calculating decimal numbers. Now, when
- 6:17:11we display it on the website, we want to
- 6:17:13display it as dollars. So to convert
- 6:17:16this back to dollars, we're going to
- 6:17:18divide by 100 since $1 is equal to 100.
- 6:17:24And that's it. We inserted the details
- 6:17:27of each product into this HTML.
- 6:17:30This is the same technique that we used
- 6:17:32in the chatbot project, except our data
- 6:17:35is just a little more complicated. Now,
- 6:17:38let's save
- 6:17:40and check the website.
- 6:17:43And now we have a list of different
- 6:17:45products on the page and we generated
- 6:17:48the HTML for these products. Now there
- 6:17:52is a small problem here which is for
- 6:17:54these socks it's showing the price 10.9
- 6:17:58but usually for price we want to show
- 6:18:0010.90.
- 6:18:02To fix this, we'll go back to our code
- 6:18:06and we're going to scroll down to where
- 6:18:08we insert the price. And to force this
- 6:18:12to show two decimals, we're going to
- 6:18:14wrap this in a open bracket and a closed
- 6:18:18bracket. And then outside of this
- 6:18:20bracket, we'll use the method dot two
- 6:18:23capital Fixed
- 6:18:25and brackets.
- 6:18:27So this converts the number into a
- 6:18:29string and inside these brackets we can
- 6:18:32tell it how many decimals to show. So
- 6:18:35we'll put two
- 6:18:38and save.
- 6:18:40Now if we check the website
- 6:18:43all the prices show two decimals. So
- 6:18:46that's a review of how to generate the
- 6:18:48HTML using some data. Let's go back to
- 6:18:52our code.
- 6:18:54And now that we're generating all this
- 6:18:57HTML, we can scroll to the bottom and
- 6:19:00remove this default HTML that we had
- 6:19:03before and save.
- 6:19:07And now let's save these changes into
- 6:19:09git. We'll go to the git section and at
- 6:19:13the top we'll use a message generate the
- 6:19:17HTML for the home page and commit.
- 6:19:27Now, we're going to take this project to
- 6:19:29the next level and go beyond what we did
- 6:19:32in the chatbot project. We're going to
- 6:19:35add a backend to this project. So, first
- 6:19:38we'll learn what a backend is and why we
- 6:19:41use a backend. So, right now, if we look
- 6:19:45at our files,
- 6:19:47notice that all the products are saved
- 6:19:49in a file on our computer called
- 6:19:52products.js. JS.
- 6:19:54The problem with this is an e-commerce
- 6:19:57website or an online shopping website
- 6:20:00might have thousands of products and it
- 6:20:02does not make sense to save everything
- 6:20:05on our computer or on the customer's
- 6:20:08computer. We might not have enough space
- 6:20:11on our computer. So, what the company or
- 6:20:14the store does is they set up another
- 6:20:17computer in their office or in what we
- 6:20:20call a data center. And this computer
- 6:20:23saves all the thousands of products that
- 6:20:25they offer. And then when we visit the
- 6:20:28website, this computer sends us only the
- 6:20:31products that we need to display. So
- 6:20:34this computer is called the backend.
- 6:20:37It's just another computer that's owned
- 6:20:39by the company and it manages all the
- 6:20:42data.
- 6:20:43Here's another example of why we use a
- 6:20:46backend. Later in this course, we're
- 6:20:48going to add products to a cart. So,
- 6:20:51where do we save this cart? It does not
- 6:20:54make sense to save the cart on the
- 6:20:56customer's computer because if the
- 6:20:58customer uses a different computer to
- 6:21:00visit the website, they would no longer
- 6:21:03have access to their cart. Instead, we
- 6:21:06save the cart in the back end as well.
- 6:21:09When they access the website, the
- 6:21:11backend sends the cart to the website.
- 6:21:14So, the back end also lets us share data
- 6:21:17between computers.
- 6:21:20So again, the backend is another
- 6:21:22computer that manages the data. And this
- 6:21:26is how a React website works in the real
- 6:21:28world. We usually contact a backend to
- 6:21:32get the data. So we're going to practice
- 6:21:34that in this course. We're going to
- 6:21:36learn how to add a backend and then
- 6:21:38we're going to learn how to use the
- 6:21:39backend in React. So you can see how we
- 6:21:42use React in the real world.
- 6:21:47Now that we learned what a backend is
- 6:21:49and why we use a backend, let's add a
- 6:21:52backend to our project. We're going to
- 6:21:55click this link in the description. Go
- 6:21:57to lesson 7 and click this link to open
- 6:22:00the backend code.
- 6:22:03And this is a backend that we can use
- 6:22:06with our project. I called it the
- 6:22:08e-commerce backend AI because this was
- 6:22:11built with AI. And I show you how to do
- 6:22:13that in another video. But for this
- 6:22:16video, I recommend downloading this
- 6:22:18version to make sure it works with a
- 6:22:20video. You can try to recreate the back
- 6:22:23end as an exercise. So, we're going to
- 6:22:25click this code button here and we'll
- 6:22:27click download zip.
- 6:22:31Once that finishes downloading, we're
- 6:22:33going to open the downloads folder. So,
- 6:22:35for me, it's right here. And then we're
- 6:22:37going to unzip this. On Windows, we're
- 6:22:40going to rightclick the file and then
- 6:22:42click extract all.
- 6:22:49And on Mac, you can just doubleclick the
- 6:22:51zip file.
- 6:22:54Then we're going to move this back end
- 6:22:56into our project. So we'll open our
- 6:22:59code.
- 6:23:00And then we're just going to drag this
- 6:23:02folder into our project. But first, I'm
- 6:23:05going to collapse these folders for now.
- 6:23:08And we're going to drag this folder to
- 6:23:10the outside
- 6:23:12and click copy folder
- 6:23:16so that the backend code is right beside
- 6:23:18our website. By the way, we call the
- 6:23:22website or what the user interacts with
- 6:23:24the front end.
- 6:23:27We're also going to rename this folder.
- 6:23:29So, right click and rename.
- 6:23:33and we'll just rename it the e-commerce
- 6:23:35backend to make it easier to understand.
- 6:23:39Next, we're going to start this backend.
- 6:23:43So, we're going to go to our terminal or
- 6:23:45our command line and we'll click here to
- 6:23:48create a new tab. And then in here,
- 6:23:52first we're going to change directory
- 6:23:54into the e-commerce backend folder. So,
- 6:23:57we'll cd ecommerce
- 6:24:00dashbackend.
- 6:24:02So now the command line is running
- 6:24:04inside this folder and the process for
- 6:24:07starting the back end is very similar to
- 6:24:10the website or the front end. So first
- 6:24:13we're going to npm install to install
- 6:24:17all the npm packages that we need
- 6:24:23and then we'll run npm rundev to start
- 6:24:28the back end.
- 6:24:30So, this tells us the back end is
- 6:24:32running on port 3000, which is localhost
- 6:24:36col 3000. To test it, we're going to go
- 6:24:39to our browser,
- 6:24:41and at the top, we're going to create a
- 6:24:43new tab, and we're going to go to
- 6:24:46localhost
- 6:24:48col 3000
- 6:24:51/s.
- 6:24:56And the back end should send back some
- 6:24:59data.
- 6:25:00And this is actually the data for the
- 6:25:03products.
- 6:25:05I can also click this checkbox here to
- 6:25:08show it in an easier to read format. And
- 6:25:10you can see this is the same products
- 6:25:12array that we were using before except
- 6:25:15this is coming from the back end. So the
- 6:25:18way a backend works is when we access
- 6:25:21the backend using a URL, it will send
- 6:25:24back some data. So this is working.
- 6:25:28So now let's go back to our code and
- 6:25:30we'll save these changes into git. Open
- 6:25:33the git section and we'll use the
- 6:25:36message add ecommerce
- 6:25:40backend and commit.
- 6:25:47Next we're going to learn data fetching.
- 6:25:49Data fetching means to get data from the
- 6:25:52back end using our code. So, first we're
- 6:25:56going to go to our files and let's go
- 6:25:59back to the e-commerce project. And
- 6:26:02we're going to open our homepage,
- 6:26:04which is right here.
- 6:26:07And we'll scroll to the top. And one way
- 6:26:10to fetch data or to get data from the
- 6:26:13back end is to use the function fetch
- 6:26:17and brackets.
- 6:26:19So fetch is a built-in function. It's
- 6:26:22provided by JavaScript.
- 6:26:24between the brackets, we're going to
- 6:26:26create a string
- 6:26:28and we're going to put the URL that we
- 6:26:31want to get data from. So, if we go back
- 6:26:34to our browser,
- 6:26:37remember that this URL gave us the
- 6:26:39products data. So, we can just select
- 6:26:42this URL and copy.
- 6:26:46And in our code, we're going to paste it
- 6:26:49into fetch.
- 6:26:52And make sure you have http col double
- 6:26:54slash in front. It doesn't show in the
- 6:26:56browser, but we need this in order for
- 6:26:58our code to work. So if we fetch data
- 6:27:02from this URL, this should give us the
- 6:27:06products data that we saw earlier.
- 6:27:09However, we cannot save this data in a
- 6:27:12variable like this. And that's because
- 6:27:14when we contact a backend, it takes time
- 6:27:18for this code to reach the back end and
- 6:27:20to get a response. So this code does not
- 6:27:24finish right away. It finishes at some
- 6:27:27point in the future. As a review, we
- 6:27:30call this asynchronous code.
- 6:27:33Asynchronous code means code that does
- 6:27:35not finish right away. So how do we
- 6:27:38handle this situation?
- 6:27:40fetch returns a promise and a promise
- 6:27:44lets us wait for asynchronous code to
- 6:27:47finish.
- 6:27:48So a promise has a method called dot
- 6:27:52then and brackets and we give dot then a
- 6:27:56function brackets arrow and curly
- 6:27:59brackets.
- 6:28:00The way this works is that this code
- 6:28:03contacts the backend to get some data
- 6:28:06but it takes some time for the backend
- 6:28:08to respond. So the code is going to keep
- 6:28:11going line by line. Then at some point
- 6:28:15in the future, this is going to finish.
- 6:28:18We're going to get the response from the
- 6:28:19back end. When it finishes in the
- 6:28:22future, it will run the function inside.
- 6:28:26So that's a review of how a promise
- 6:28:29works. So next when fetch gets a
- 6:28:32response from the back end it will save
- 6:28:35it as a parameter in this inner function
- 6:28:38called response.
- 6:28:40By the way when we contact the back end
- 6:28:43using fetch this is called a request.
- 6:28:47When the back end responds with some
- 6:28:49data this is called a response. So this
- 6:28:53parameter contains the response from the
- 6:28:56back end. And inside here, let's just
- 6:28:59console.log
- 6:29:01the response to see what it looks like.
- 6:29:04We'll save
- 6:29:07and then check our website
- 6:29:10and we'll open the tab for our website
- 6:29:13and we're going to open the console.
- 6:29:15Rightclick, inspect,
- 6:29:18and the console.
- 6:29:20And you can see that we got the response
- 6:29:22from the back end. So inside here we
- 6:29:25have some information like the URL that
- 6:29:27we accessed and a status code. 200 means
- 6:29:31it was successful.
- 6:29:33However, notice that we don't have the
- 6:29:36products data in this response. To get
- 6:29:39the products data, we're going to go
- 6:29:41back to our code
- 6:29:44and this response object has a method
- 6:29:47called response.json
- 6:29:50JSON and brackets.
- 6:29:53So,JSON gives us the data that is
- 6:29:57attached to the response. In this case,
- 6:29:59it gives us the products data. Now, the
- 6:30:03tricky part about this is response.json
- 6:30:06is also asynchronous. So, again, we
- 6:30:09can't save this in a variable. Instead,
- 6:30:13this is also a promise. So we have to
- 6:30:16use dot then again and give this another
- 6:30:20function brackets arrow and curly
- 6:30:22brackets
- 6:30:25and then inside this inner function we
- 6:30:28have access to the data that we get from
- 6:30:31the back end. So in this case this is
- 6:30:34the products data.
- 6:30:36So if we console.log log the data and
- 6:30:41save
- 6:30:43and then check the console.
- 6:30:46You'll see that the backend gave us an
- 6:30:48array of products to display on the
- 6:30:51page. So, this is the same thing as the
- 6:30:54products array that we saved in our
- 6:30:56file, but this data is coming from the
- 6:30:59back end.
- 6:31:01Let's go back.
- 6:31:03And that's how we fetch data or get data
- 6:31:07from the back end. We use the built-in
- 6:31:09function fetch and give it the URL of
- 6:31:12the back end and then we use dot then to
- 6:31:16wait for a response.
- 6:31:18Now one shortcut we can use is instead
- 6:31:21of doing dot then inside another dot
- 6:31:24then we can also return the
- 6:31:28response.json JSON and then move this
- 6:31:30dot then to the outside.
- 6:31:34So this code does the same thing as
- 6:31:36before. This waits for this to finish
- 6:31:40and then runs this function. So this is
- 6:31:44just a shortcut that we can use with
- 6:31:46promises.
- 6:31:48Another thing to note is the back end
- 6:31:50can also run on the same computer as the
- 6:31:53website or the front end. So right now
- 6:31:56this back end is actually running on our
- 6:31:58own computer. Locost means our own
- 6:32:02computer. So when we contact the back
- 6:32:04end, we're actually just contacting our
- 6:32:06own computer. We do this during
- 6:32:09development because it's easier. But
- 6:32:11once this website is on the internet,
- 6:32:13the back end will be another computer.
- 6:32:17So now let's save this code
- 6:32:20and we'll save this into git. In the get
- 6:32:23section, we'll use the message fetch
- 6:32:27data from the back end and commit.
- 6:32:35Next, notice that using fetch is still a
- 6:32:38little hard to read. We use a and then
- 6:32:42we use response.json and then another.
- 6:32:46To make this code cleaner, we're going
- 6:32:48to use an npm package called Axios.
- 6:32:52Axios is a cleaner way to make requests
- 6:32:55to the back end. First, we're going to
- 6:32:58go to our command line and we'll open
- 6:33:00the tab for the front end. And here
- 6:33:03we'll press Ctrl C to stop the V server.
- 6:33:07And we're going to npm install Axios and
- 6:33:12type at and we're going to give it a
- 6:33:14version. So, we can click this link in
- 6:33:17the description and then go to lesson 7.
- 6:33:20And this is a version of Axios we're
- 6:33:22going to use. So, again, you should
- 6:33:25always use the version that you see on
- 6:33:27your page.
- 6:33:29So, select the version on your page and
- 6:33:32copy it
- 6:33:34and then paste it in our code and press
- 6:33:37enter to install Axios.
- 6:33:41Once that's installed, we're going to
- 6:33:42run mpm rundev to restart the vit
- 6:33:46server.
- 6:33:48And then to use axios in our code, we're
- 6:33:51going to go to the homepage. And at the
- 6:33:53top, we're going to import axios without
- 6:33:58curly brackets from a string axios.
- 6:34:03And now instead of using fetch, we're
- 6:34:05going to use axios.get.
- 6:34:09So this does the same thing as before
- 6:34:12except the data from the back end will
- 6:34:14be saved directly inside the response.
- 6:34:18So instead of doing all these steps,
- 6:34:21we can just get the data using response.
- 6:34:27So let's console.log this to see if it
- 6:34:29works. So console.log and save.
- 6:34:34And then if we open the console,
- 6:34:37you'll notice it gets the same products
- 6:34:39data as before.
- 6:34:43But if we look at our code, it's a lot
- 6:34:45cleaner because we can just get the data
- 6:34:47directly from the response. So as you
- 6:34:50can see, Axios is a cleaner way to make
- 6:34:53requests to the back end. So now let's
- 6:34:56save this into git. In the get section,
- 6:34:59we'll use a message use Axios to fetch
- 6:35:04the data and commit.
- 6:35:11Now, let's use this data from the back
- 6:35:14end to generate the HTML instead of
- 6:35:17using the products file that we had
- 6:35:19before. But first, we have a problem.
- 6:35:23This code is directly inside the
- 6:35:26homepage component. That means whenever
- 6:35:29the homepage changes or rerenders, it
- 6:35:32will run this code again and send
- 6:35:34another request to the back end.
- 6:35:37However, we just need to load the
- 6:35:39products once on this page. We don't
- 6:35:41need to load the products over and over
- 6:35:44again. To make this code run once, we
- 6:35:47can put it inside a use effect. At the
- 6:35:51top, we're going to import curly
- 6:35:54brackets use effect
- 6:35:58from React.
- 6:36:01And above this code, we're going to do
- 6:36:04use effect brackets and give this a
- 6:36:08function brackets arrow and curly
- 6:36:10brackets.
- 6:36:13Use effect lets us control when some
- 6:36:16code runs. We're going to move this code
- 6:36:20inside the use effect. So we'll cut and
- 6:36:24in here we'll paste.
- 6:36:27And I'll select these lines and press
- 6:36:29tab to reformat.
- 6:36:32By default, use effect runs this code
- 6:36:35every time the component is created or
- 6:36:38updated. But we can control this. At the
- 6:36:41end of this function, we're going to add
- 6:36:44a comma and an empty array. As a review,
- 6:36:48this is called a dependency array. The
- 6:36:52dependency array lets us control when
- 6:36:54use effect runs. If we use an empty
- 6:36:58array, that means this code will only
- 6:37:01run once after the component is created.
- 6:37:05And that's exactly what we need here. We
- 6:37:08just need to load the products once on
- 6:37:10this page. By the way, you might notice
- 6:37:13in the console that use effect runs
- 6:37:15twice instead of once. So, this is
- 6:37:18totally normal and it's caused by using
- 6:37:20strict mode in our app. Strict mode runs
- 6:37:23use effect twice to help us catch bugs
- 6:37:26and it only does this during
- 6:37:28development.
- 6:37:30So now that we have the data, how do we
- 6:37:33use it in this loop down here to
- 6:37:36generate the HTML?
- 6:37:39To do that, we need to save the products
- 6:37:41data inside the state. So at the top,
- 6:37:45we're also going to import use state
- 6:37:49from React.
- 6:37:51And at the top of our component, we're
- 6:37:53going to do const square bracket equals
- 6:37:57use state and brackets.
- 6:38:01As a review, use state gives us an array
- 6:38:04with two values. The first value is the
- 6:38:07name of the data. In this case, we'll
- 6:38:10name it the products. And then we'll
- 6:38:13type a comma. And the second value of
- 6:38:16use state is the updater function. The
- 6:38:19updater function lets us update this
- 6:38:22value and then it will regenerate the
- 6:38:24HTML. We usually name the updater
- 6:38:27function set and then the name of the
- 6:38:30data capital P products.
- 6:38:34And finally between these brackets we're
- 6:38:37going to put the starting value of the
- 6:38:39products. Let's just use an empty array.
- 6:38:43And now after we get the products from
- 6:38:46the back end, we're going to save it
- 6:38:48into the state.
- 6:38:50So instead of console.log, we'll use the
- 6:38:53updater function set products. This will
- 6:38:58save the products data into here. And
- 6:39:01then it's going to use this to generate
- 6:39:04the HTML. So now we're using data from
- 6:39:07the back end to generate the HTML. And
- 6:39:11we don't need to import this file here
- 6:39:13anymore.
- 6:39:15So the only thing to note is that the
- 6:39:17name of the data has to match the name
- 6:39:20that we use down here. So that this code
- 6:39:22is actually using the state.
- 6:39:26Now let's save
- 6:39:28and we'll check the website.
- 6:39:31And everything still works except now
- 6:39:34we're using data from the back end to
- 6:39:36generate this HTML.
- 6:39:39We'll go back to our code
- 6:39:43and we'll save this into git. In the git
- 6:39:46section, we'll create the message use
- 6:39:49data from the back end to generate the
- 6:39:54HTML and commit.
- 6:40:02Next, if we look at the website
- 6:40:05in the homepage, we also need to get the
- 6:40:07cart from the back end because we need
- 6:40:09to display this quantity number up here.
- 6:40:13To do that, we'll go back to our code
- 6:40:17and we're going to make another request
- 6:40:19to the back end to get the cart. So,
- 6:40:22below this request, we're going to use
- 6:40:25axios again. Axios.get get brackets a
- 6:40:30string and we'll give it the URL http
- 6:40:34colhost
- 6:40:38col 3000
- 6:40:40slash API/cart-
- 6:40:43items.
- 6:40:45The way this works is the back end is
- 6:40:48running on http localhost col 3000 and
- 6:40:53after this we add a URL path. Each URL
- 6:40:57path gives us different data. For
- 6:41:00example, the URL path/ API/proucts
- 6:41:04gives us the products data. The URL
- 6:41:07path/ API/cart items gives us the cart
- 6:41:11data. So the URL path can actually be
- 6:41:14anything we want. Whoever creates the
- 6:41:17backend decides what the URL paths will
- 6:41:20be. In the description, you can find a
- 6:41:23documentation page for my backend.
- 6:41:27And this lists all the URL paths that we
- 6:41:30can use. A common naming convention is
- 6:41:33to start the URL path with slash API.
- 6:41:37API means application programming
- 6:41:40interface. Starting with / API makes it
- 6:41:43clear that these URL paths are
- 6:41:46specifically for interacting with the
- 6:41:48backend.
- 6:41:51So we already learned how to use /
- 6:41:53API/roucts.
- 6:41:55Now we're going to learn how to use /
- 6:41:57API/cart items.
- 6:42:01And remember whenever we make a request
- 6:42:03to the back end it's going to take some
- 6:42:05time to get a response. So we need to
- 6:42:08wait for this code to finish using then
- 6:42:13we'll give this a function
- 6:42:15and the response is going to be saved in
- 6:42:17here.
- 6:42:20So when we make a request to /appi/cart
- 6:42:23items, we're going to get the cart in
- 6:42:25the response. So in here, let's
- 6:42:28console.log
- 6:42:30the response data to see what the cart
- 6:42:34looks like. We'll save
- 6:42:37and we'll check the console.
- 6:42:40And you might need to refresh. And the
- 6:42:43back end responded with an array. And
- 6:42:46this array represents the cart that we
- 6:42:49have on the website. So each item in the
- 6:42:52cart contains the product ID that we
- 6:42:55want to buy and the quantity of that
- 6:42:58product. So next we're going to use this
- 6:43:00to calculate the quantity and display it
- 6:43:02on the page. So we'll go back to our
- 6:43:05code and first we need to save this cart
- 6:43:09into state so we can use it to create
- 6:43:11the HTML.
- 6:43:13At the top, we'll create another state
- 6:43:16const brackets equals use state
- 6:43:20brackets.
- 6:43:22And this state will save the cart,
- 6:43:25comma, and the updater function set
- 6:43:28capital C cart.
- 6:43:31And let's set the starting value of the
- 6:43:33cart to an empty array. And we'll scroll
- 6:43:37down. And now once we get the cart from
- 6:43:40the back end, we're going to save it in
- 6:43:43the state. So instead of console.log,
- 6:43:46we're going to use the updater function
- 6:43:48set cart. And now the cart from the back
- 6:43:52end will be saved inside this variable.
- 6:43:55And remember that the quantity for the
- 6:43:57cart is inside the header. So we need to
- 6:44:00pass the cart into the header using a
- 6:44:03prop. So here we'll add a prop. cart
- 6:44:07equals curly brackets and we'll give it
- 6:44:10the cart.
- 6:44:12Let's save this file
- 6:44:14and we'll go into our code and go to
- 6:44:18components header.jsx
- 6:44:20and then inside the header we're going
- 6:44:22to get the cart from the props. To do
- 6:44:25that at the top, we'll type curly
- 6:44:27brackets and the cart. As a reminder,
- 6:44:31this is a shortcut for this code.
- 6:44:34And now we're going to calculate the
- 6:44:36total quantity in this cart. So at the
- 6:44:39top, let's create a variable. Let total
- 6:44:43quantity equals zero to start. And then
- 6:44:47we'll loop through the cart using cart
- 6:44:50for each brackets. And we'll give it a
- 6:44:54function.
- 6:44:58And the inner function will get one
- 6:45:00parameter which is each item in the
- 6:45:02cart. So, we'll call this the cart item.
- 6:45:06And then each cart item has a quantity.
- 6:45:09So, we're going to add that to this
- 6:45:11total quantity up here. We'll do total
- 6:45:15quantity plus equal the cart item.
- 6:45:22And now we can scroll down
- 6:45:25till we see the cart quantity down here.
- 6:45:28And instead of three every time, we're
- 6:45:30going to insert the total quantity
- 6:45:35and save.
- 6:45:37And now if we look at our project
- 6:45:40now, it will display the total quantity
- 6:45:43using data from the back end. So now
- 6:45:46let's go back to our code.
- 6:45:48And if we scroll up, you might see an
- 6:45:51error here for props validation. Again,
- 6:45:55this is an older feature of React. So,
- 6:45:57we can disable this check by going into
- 6:46:00our files and opening eslint.config.js.
- 6:46:04And then we'll scroll down inside this
- 6:46:07rules object. We're going to add a
- 6:46:10string react/prop-
- 6:46:14types colon the string off. So, make
- 6:46:18sure it's inside this rules object. We
- 6:46:22can save.
- 6:46:24And now if we go back to the header, the
- 6:46:26props validation error should be gone.
- 6:46:29So now let's save this code into git.
- 6:46:32We'll open the git section and we'll
- 6:46:34create a message get cart from the back
- 6:46:38end and display quantity and commit.
- 6:46:49Next, if we go back to the homepage, so
- 6:46:52in our files, go to pages and homepage
- 6:46:57and scroll up. Notice that for each
- 6:47:00request, we're sending it to localhost
- 6:47:033000. So, we can actually set up a
- 6:47:05shortcut in Vit. So, we don't have to
- 6:47:08type localhost 3000 every time. So, we
- 6:47:11can actually remove these
- 6:47:16and save.
- 6:47:19And then we're going to go to our files
- 6:47:21and we're going to open vit.config.js.
- 6:47:27And inside here we're going to add the
- 6:47:29property server colon and an object. And
- 6:47:33inside we'll add the property proxy
- 6:47:37colon another object.
- 6:47:39And in here we'll add a string slash API
- 6:47:44colon. And we're going to give this an
- 6:47:47object. And inside we'll add a target
- 6:47:50property colon and the string http colon
- 6:47:55double slash localhost colon 3000.
- 6:48:01So this is called a server proxy
- 6:48:03configuration. This just means that if
- 6:48:06the URL path starts with / API like we
- 6:48:10had it before, the request will
- 6:48:12automatically go to localhost 3000. So
- 6:48:15we don't have to type it every time.
- 6:48:18Another thing we can set up is when we
- 6:48:20load images, we also want to load them
- 6:48:23from the back end. Remember the back end
- 6:48:25should manage the data including the
- 6:48:28product images. So in here we'll also
- 6:48:31add a comma and add a string slash
- 6:48:35images colon and an object
- 6:48:39and we'll give it the same target. So
- 6:48:41we'll select this and copy and in here
- 6:48:45we'll paste
- 6:48:47and save.
- 6:48:50And we need to do one more thing. We're
- 6:48:52going to go inside index.html HTML and
- 6:48:55in the head we're going to add less than
- 6:48:59base slashgreater than and we're going
- 6:49:02to give this an attribute href equals
- 6:49:05double quotes and a slash.
- 6:49:08So this adds slash in front of any
- 6:49:10relative urls. All the image URLs start
- 6:49:14with images and this will convert them
- 6:49:16into slash images so that our redirect
- 6:49:20will work. Let's save
- 6:49:23and we'll go back to the vit config.
- 6:49:27So now when we get images, we're going
- 6:49:29to get them from the back end. And now
- 6:49:32because we changed the vit
- 6:49:33configuration, it's a good idea to stop
- 6:49:36the server with control c and then
- 6:49:38restart the server using mpm rundev.
- 6:49:43And now let's check the website to make
- 6:49:46sure everything is working. So I'll just
- 6:49:48refresh this to make sure. And
- 6:49:50everything still looks good. So now
- 6:49:53we'll go back to our code and we'll save
- 6:49:55these changes into git. Inside git,
- 6:49:59we're going to type the message add
- 6:50:02server proxy config for slash API and
- 6:50:08slash images
- 6:50:11and commit.
- 6:50:17Next, we're going to move on to the
- 6:50:19checkout page. So, let's open the
- 6:50:22website and we'll click the cart icon in
- 6:50:25the top right
- 6:50:27and we're going to work on this page.
- 6:50:29So, on this page, we also need the cart
- 6:50:32to generate this HTML.
- 6:50:34So, we'll go back to our code
- 6:50:38and we'll open our files and we're going
- 6:50:41to open checkout page.jsx.
- 6:50:44And now inside here we could do another
- 6:50:47axios.get/appi/cart
- 6:50:50again. But remember we already loaded
- 6:50:53the cart on the homepage. It does not
- 6:50:56make sense to reload the cart whenever
- 6:50:59we switch pages. We just need to load
- 6:51:02the cart once for the entire website. To
- 6:51:05do that, we're going to go to the
- 6:51:07homepage
- 6:51:09and we're going to share this cart
- 6:51:11between the two pages using the
- 6:51:13technique lifting the state up. As a
- 6:51:16review, this means we move the cart
- 6:51:19state up to an outer component. In this
- 6:51:22case, the outer component is the app
- 6:51:26component here, which contains a
- 6:51:28homepage. So, we're going to lift the
- 6:51:31cart up into the app component. And from
- 6:51:35here, we can share the cart between the
- 6:51:37homepage and the checkout page. So,
- 6:51:40first, let's go into the homepage again.
- 6:51:43We're going to select this cart and cut
- 6:51:48and go back to the app. And we're going
- 6:51:51to lift it up in here by pasting it. And
- 6:51:55we also need the code that loads the
- 6:51:57cart. So in the homepage,
- 6:52:00we're going to select this code and cut
- 6:52:06and save
- 6:52:08inside app.jsx.
- 6:52:11Below this, we're going to paste.
- 6:52:14And remember, we need to put this inside
- 6:52:16a use effect so that it only runs once.
- 6:52:19So use effect brackets, give it a
- 6:52:22function,
- 6:52:24and a empty array at the end. And we're
- 6:52:28going to select it and cut. And inside
- 6:52:31here, paste.
- 6:52:34And I'll select these lines and press
- 6:52:36tab to reformat them. And next, we'll
- 6:52:40need to import this code. So at the top,
- 6:52:43we're going to import curly brackets,
- 6:52:47use state, and use effect
- 6:52:52from React.
- 6:52:55And we'll also import axios. So at the
- 6:52:58top again, we're going to import axios
- 6:53:03from the string axios.
- 6:53:06So now that we have the cart, we're
- 6:53:08going to share it between these two
- 6:53:10pages using a prop. So for the homepage,
- 6:53:13we'll add a prop. Cart equals curly
- 6:53:16brackets and we'll give it the cart. And
- 6:53:20for the checkout page as well, cart
- 6:53:22equals curly brackets. and we'll save
- 6:53:24the cart.
- 6:53:27Let's save this file.
- 6:53:29And we'll go into the homepage.
- 6:53:32And at the top, we need to access the
- 6:53:34cart from the props using curly
- 6:53:36brackets, the cart, and save.
- 6:53:41So, the code should work the same as
- 6:53:42before, except we're just getting the
- 6:53:44cart from the props. Now, let's open our
- 6:53:47website to make sure it works. We're
- 6:53:49going to go to the homepage and refresh.
- 6:53:53And that looks good. We'll go back to
- 6:53:55our code. So now that we lifted the
- 6:53:59state up, we can just load the card once
- 6:54:02for the entire website instead of
- 6:54:04reloading it on every page. We'll open
- 6:54:07the get section and we'll give it the
- 6:54:10message lift cart up and share it
- 6:54:16between pages and commit.
- 6:54:25Next, we're going to use the cart to
- 6:54:28generate the HTML for the checkout page.
- 6:54:31So, first make sure that we pass the
- 6:54:33cart to the checkout page using this
- 6:54:35prop. And now we're going to open the
- 6:54:38checkout page, which is here. And we're
- 6:54:42going to access the cart from the props
- 6:54:44using curly brackets and the cart.
- 6:54:48And I will use this cart to generate the
- 6:54:50HTML down here. Now inside this code,
- 6:54:54we're going to press Ctrl+F to find some
- 6:54:57code. And we're going to search for
- 6:54:59cart- item-container.
- 6:55:03And here we're going to collapse this
- 6:55:05element. And we'll collapse this element
- 6:55:08as well.
- 6:55:10So each of these represents a cart item
- 6:55:13on the page. So let's generate this
- 6:55:16using the cart. Above this we're going
- 6:55:19to insert cart map brackets and we'll
- 6:55:24give it a function brackets arrow and
- 6:55:27curly brackets. And this inner function
- 6:55:30gets one parameter which is the cart
- 6:55:33item. And for each item we're going to
- 6:55:36return brackets some HTML.
- 6:55:40So for each cart item, we want to
- 6:55:42generate this HTML. So let's just select
- 6:55:45it and copy. And inside here, we're
- 6:55:49going to paste.
- 6:55:52And to fix the formatting, we're going
- 6:55:53to press control or commandshiftp
- 6:55:56and run format document.
- 6:56:00Now let's scroll up.
- 6:56:03And remember, when we loop through an
- 6:56:05array, each element needs a key
- 6:56:08property.
- 6:56:11Each cart item has a product ID which is
- 6:56:14unique. So we're going to save the key
- 6:56:16as cart item.
- 6:56:20ID.
- 6:56:21And now instead of showing the same
- 6:56:24product every time, we're going to
- 6:56:26insert the details from each cart item.
- 6:56:29However, there is a problem here.
- 6:56:31Remember that the cart looks like this.
- 6:56:34It doesn't have any product data on it.
- 6:56:38To fix this, we're going to go to the
- 6:56:40app component where we load the cart.
- 6:56:44And at the end of this URL, we can add
- 6:56:47question mark expand equals product.
- 6:56:52So this is called a query parameter. A
- 6:56:55query parameter lets us add additional
- 6:56:58information to our request. When the
- 6:57:00backend receives this query parameter,
- 6:57:03it's going to add product details to the
- 6:57:06cart. So, it's going to expand the cart
- 6:57:09with product details. So, now let's save
- 6:57:12this file
- 6:57:14and we're going to open the website and
- 6:57:17we're going to go to the checkout page
- 6:57:20and we're going to open the console. So,
- 6:57:22we'll rightclick inspect
- 6:57:25and inside the console we're going to
- 6:57:27open the network tab.
- 6:57:30So, I'm going to remove any search that
- 6:57:32I have. And in here, we can see all the
- 6:57:36requests that we make to the back end
- 6:57:38and the responses. So, by the way,
- 6:57:41requests only show up here if the
- 6:57:43console was open before the request. So,
- 6:57:45we may need to refresh
- 6:57:48to get the request to show up in the
- 6:57:50network tab. And now we can see our
- 6:57:52request to cart- items. If you don't see
- 6:57:56it, you may need to search for it up
- 6:57:58here by typing cart. And we have it
- 6:58:01here. So we can open this by clicking it
- 6:58:04and then go into the response tab. And
- 6:58:08here you can see the response from the
- 6:58:10back end. So now you can see that each
- 6:58:12cart item has a property called product.
- 6:58:16And this contains all the product
- 6:58:18details that we need for this page.
- 6:58:22So now let's go back to our code.
- 6:58:26And we're going to go to the checkout
- 6:58:27page.
- 6:58:29And now instead of the socks every time,
- 6:58:32we're going to remove this
- 6:58:35and insert the cart item.
- 6:58:42And we'll also replace the image. So
- 6:58:45we'll remove this and insert the cart
- 6:58:48item.
- 6:58:51image and down here we're going to
- 6:58:54insert the price. Now, if we go to the
- 6:58:57homepage,
- 6:58:59remember that in the homepage, we wrote
- 6:59:02some code here to display the price. So,
- 6:59:05let's actually move this code into a
- 6:59:07function so we can reuse it. Inside our
- 6:59:10files, we're going to click the source
- 6:59:13folder and we'll create a new folder and
- 6:59:16let's call it utils for utilities.
- 6:59:21And inside the utmost folder, we'll
- 6:59:23create a new file and we'll name it
- 6:59:26money.js.
- 6:59:29So this file contains utilities or code
- 6:59:31that helps us work with money. So in
- 6:59:34here, let's create a function and we'll
- 6:59:37name it format money brackets and curly
- 6:59:41brackets.
- 6:59:43And we're going to copy the code from
- 6:59:45the homepage. So, inside the homepage,
- 6:59:48we're going to take all of this code and
- 6:59:50copy it.
- 6:59:53And inside money.js, inside here, we'll
- 6:59:56paste it.
- 6:59:58And now we're going to turn this into a
- 7:00:00string. At the front, we'll type backick
- 7:00:03and at the end a back tick to create a
- 7:00:06template string. And remember in
- 7:00:08template strings to insert something, we
- 7:00:10need dollar curly brackets. And now,
- 7:00:14instead of getting the product.pricent
- 7:00:15price cents. We're going to pass it into
- 7:00:18this function. So, let's name the
- 7:00:20parameter amount cents. And we're going
- 7:00:24to remove this and replace it with
- 7:00:27amount cents. So, this function lets us
- 7:00:31convert any amount cents into this
- 7:00:33format. At the end, let's return this
- 7:00:37value from the function. and we'll
- 7:00:40export it so we can use it in other
- 7:00:42files and save.
- 7:00:46So now we can go back to the homepage
- 7:00:49and at the top we're going to import
- 7:00:53curly brackets the format money function
- 7:00:58that we just created from a string and
- 7:01:01we're going to locate the utils folder.
- 7:01:04So, we'll go out of this folder with dot
- 7:01:06dot slash utils slashmoney.
- 7:01:10And now we'll scroll back down
- 7:01:14and instead of this code, we're going to
- 7:01:17remove it and insert format money
- 7:01:21brackets and we'll give it product.pric
- 7:01:25sets and save.
- 7:01:28And now let's reuse this function in the
- 7:01:30checkout page. So, we'll open the
- 7:01:33checkout page, and at the top, we're
- 7:01:37going to import curly brackets, the
- 7:01:41format money function from a string, and
- 7:01:46we're going to locate this file. And for
- 7:01:49me, I'll go out of this folder using dot
- 7:01:51dot slash utils/money.
- 7:01:55Now, if you did the exercises from the
- 7:01:57previous lesson, this file might be
- 7:01:59inside a folder called checkout. In that
- 7:02:02case, make sure you have two dot dot
- 7:02:04slash in front. And now we're going to
- 7:02:07scroll back down
- 7:02:11and we're going to replace this money
- 7:02:13with curly brackets.
- 7:02:16Format money brackets. And we're going
- 7:02:20to give it the cart item.
- 7:02:24price sets and we'll replace the
- 7:02:27quantity. So, insert
- 7:02:30cart item dot quantity and save.
- 7:02:37And now if we go back to the website,
- 7:02:40now we are generating these cart items
- 7:02:43using data from the back end. And this
- 7:02:46was the default HTML we had before. So
- 7:02:49now we can go back to our code and we
- 7:02:52can scroll down
- 7:02:55and we can remove the default HTML
- 7:02:59and save.
- 7:03:02So now let's save these changes into
- 7:03:04git. We'll open the get section
- 7:03:08and at the top we'll type the message
- 7:03:11generate
- 7:03:12HTML for the cart items and commit.
- 7:03:23Next, we're going to generate the HTML
- 7:03:25for the delivery options. So, we'll go
- 7:03:28back to the checkout page and we'll
- 7:03:31scroll to the top of this component. And
- 7:03:34here we'll load the delivery options
- 7:03:36from the back end. So first let's import
- 7:03:40Axios. At the top we'll import Axios
- 7:03:45from the Axios npm package. And we'll
- 7:03:50also need import curly brackets. Use
- 7:03:54state and use effect to save the
- 7:03:58delivery options. So import it from
- 7:04:00React.
- 7:04:02And now inside the component
- 7:04:05we'll create some state to save the
- 7:04:07delivery options const
- 7:04:10bracket equals use state and brackets
- 7:04:15and we'll name this delivery options
- 7:04:19colon and the updater function set
- 7:04:22delivery options
- 7:04:25and we'll set the starting value to an
- 7:04:27empty array. Next, we'll get the
- 7:04:30delivery options from the back end. So
- 7:04:32here we'll do use effect brackets and
- 7:04:37we'll give it a function brackets arrow
- 7:04:39and curly brackets. And at the end we'll
- 7:04:42give it a comma and an empty dependency
- 7:04:44array so that this code only runs once
- 7:04:47on this page. And inside here we're
- 7:04:50going to call axials.get
- 7:04:52get brackets and the URL to get the
- 7:04:56delivery options is slash API slashd
- 7:05:00delivery-options
- 7:05:03and then we're going to wait for the
- 7:05:04response to come back using then
- 7:05:08brackets and give it a function brackets
- 7:05:11arrow and curly brackets and inside the
- 7:05:14function the response from the back end
- 7:05:16will be saved as the response parameter.
- 7:05:20So once we get this response, we're
- 7:05:22going to save it in the delivery options
- 7:05:24up here using set delivery options
- 7:05:30brackets
- 7:05:31response.
- 7:05:34Now let's save this code and we're going
- 7:05:38to check the network tab to see if it
- 7:05:40works. So, we'll open the console and
- 7:05:43we'll open the network tab. And we might
- 7:05:45need to refresh to make sure we get the
- 7:05:48request and we'll remove this and search
- 7:05:51for delivery options. And we'll click
- 7:05:54one of these and then look at the
- 7:05:57response tab. So, the backend responded
- 7:06:00with three delivery options. Each
- 7:06:03delivery option has the number of days
- 7:06:05it takes to deliver and the price in
- 7:06:08cents. However, if we open our website,
- 7:06:12notice that each delivery option also
- 7:06:14has a estimated delivery time. So, we
- 7:06:17could use this delivery days to
- 7:06:20calculate this date on the front end.
- 7:06:23But remember, it's generally the
- 7:06:25backend's job to manage the data and do
- 7:06:28these calculations.
- 7:06:29So, if we go back to our code,
- 7:06:33this URL path also supports a query
- 7:06:36parameter called expand. So at the end
- 7:06:39we'll add question mark expand equals
- 7:06:43and I called it estimated
- 7:06:47delivery time.
- 7:06:50So when the back end sees this expand
- 7:06:52parameter it will calculate the
- 7:06:54estimated delivery time for us and then
- 7:06:56give it back in our response. If we save
- 7:07:01and check the console
- 7:07:04and refresh
- 7:07:07and open the response and now you'll see
- 7:07:10that each delivery option now has an
- 7:07:12estimated delivery time in milliseconds.
- 7:07:16So now we'll go back to our code and we
- 7:07:19have what we need to display the date.
- 7:07:22As you can see it's generally the
- 7:07:24backend's job to manage the data and to
- 7:07:26do the calculations. And now the front
- 7:07:29end's job is to present that data in an
- 7:07:32easy to read way on the website.
- 7:07:35So now let's use these delivery options
- 7:07:38to generate the HTML. We're going to
- 7:07:41click in this file and press Ctrl+F to
- 7:07:43search for code. And we'll search for
- 7:07:45delivery dash options.
- 7:07:49And inside here we should see an element
- 7:07:51with a class name delivery option. So
- 7:07:54we're going to collapse this element and
- 7:07:57also collapse the other delivery options
- 7:08:00and each of these represents one
- 7:08:02delivery option on the page. So let's
- 7:08:05generate this HTML using React. Above
- 7:08:08this we'll insert and we'll run the code
- 7:08:12delivery options dot map brackets and
- 7:08:17give it a function brackets arrow and
- 7:08:19curly brackets and the parameter will be
- 7:08:22each delivery option
- 7:08:26and here we're going to return brackets
- 7:08:30some HTML for each delivery option. So
- 7:08:33we want to return this HTML. So we can
- 7:08:36select it and copy. And inside here
- 7:08:39we'll paste.
- 7:08:42And we can fix the formatting using
- 7:08:44control or commandshiftp
- 7:08:46and press format document.
- 7:08:50And let's scroll up. And as always, we
- 7:08:53need a key prop whenever we're looping
- 7:08:55through an array. So we'll add key
- 7:08:58equals curly brackets. And each delivery
- 7:09:01option has a unique ID. So we'll just
- 7:09:03set it to the delivery option
- 7:09:07ID.
- 7:09:08Next, we're going to replace these
- 7:09:10values here. So first we'll do the
- 7:09:13delivery date. Currently, we only have
- 7:09:16the delivery time in milliseconds. So
- 7:09:19how do we display it as a date like
- 7:09:21this? To do that, we can use another npm
- 7:09:25package called Dayjs.
- 7:09:28So, we're going to go to the terminal
- 7:09:29and go to the tab that's running our
- 7:09:31front end. And we can press Ctrl C to
- 7:09:34stop the server. And we're going to npm
- 7:09:38install dayjs. And we'll type at to give
- 7:09:42it a version. And we're going to click
- 7:09:44this link in the description to get the
- 7:09:46version of day.js we're going to use.
- 7:09:49And we'll copy that version and paste it
- 7:09:51in here and press enter to install
- 7:09:55day.js.
- 7:09:57And after that we're going to npm rundev
- 7:10:00to restart the server.
- 7:10:04So the dayjs package helps us work with
- 7:10:07dates and it helps us display a date in
- 7:10:10a particular format. To use it, we're
- 7:10:13going to scroll to the top and at the
- 7:10:15top we're going to import dayjs
- 7:10:20from the package day js.
- 7:10:24And now we'll scroll back down or we can
- 7:10:27control F and search for delivery
- 7:10:29options.
- 7:10:32And we'll scroll down here. And now
- 7:10:35above this date, we're going to insert.
- 7:10:38And we're going to run day JS and
- 7:10:42brackets. And we're going to give Day.js
- 7:10:44DJs a time in milliseconds which we
- 7:10:47actually have inside delivery option dot
- 7:10:52estimated
- 7:10:54delivery time ms or milliseconds.
- 7:10:59And now that we wrapped this in dayjs we
- 7:11:02can format it into this format. So after
- 7:11:06this bracket here, we're going to use
- 7:11:08the method dot format and brackets and
- 7:11:12give it a string. To show the time in
- 7:11:15this format, we're going to type D four
- 7:11:18times,
- 7:11:20which is the day of the week. And then
- 7:11:22we'll type a comma to show this comma
- 7:11:25and a space. And we'll type capital M
- 7:11:28four times
- 7:11:30to show the month and a space. and we'll
- 7:11:33type capital D which will show this
- 7:11:36date. And now we can remove this code.
- 7:11:41Next, we're going to replace this price.
- 7:11:44If the price is free, we'll show free
- 7:11:46shipping. Otherwise, we're going to show
- 7:11:48the price dash shipping. So, we're going
- 7:11:51to scroll up to the inside of this loop
- 7:11:54here,
- 7:11:56and we're going to create a variable for
- 7:11:58the price. So, we'll do let price string
- 7:12:03equals a string. And to make it easier,
- 7:12:07we're going to set this to a default of
- 7:12:10free shipping.
- 7:12:13And then we're going to overwrite this
- 7:12:15if we have a price. So, below this,
- 7:12:19we're going to check if brackets the
- 7:12:22delivery option.
- 7:12:26Sense is greater than zero.
- 7:12:29curly brackets. So if it is not free,
- 7:12:32we're going to overwrite this price
- 7:12:36string equals a template string
- 7:12:39and inside we're going to insert and
- 7:12:42remember we have the format money
- 7:12:44function to display the price. So as I
- 7:12:47here we'll call format money and
- 7:12:51brackets and we're going to give it the
- 7:12:54delivery option price sets.
- 7:12:59And at the end we're going to add a dash
- 7:13:02and shipping.
- 7:13:05And now we can scroll back down
- 7:13:08and instead of free shipping every time
- 7:13:10we're going to insert the price string.
- 7:13:15Next, we're going to scroll up a bit and
- 7:13:18there's a few things that we need to
- 7:13:19replace for this input type radio
- 7:13:22element. So, this represents the
- 7:13:25selector and there's a few special
- 7:13:27attributes that we need to replace. The
- 7:13:29first one is the name attribute. So, the
- 7:13:32name groups the selectors together. So,
- 7:13:36each set of selectors should have a
- 7:13:38unique name. This makes sure that for
- 7:13:41each product we can only select one of
- 7:13:43them. So here instead of doing delivery
- 7:13:46option one every time we're going to
- 7:13:48replace this with a product ID because
- 7:13:51each set of selectors should belong to
- 7:13:54one product.
- 7:13:56So in this code we're going to replace
- 7:13:58it using curly brackets
- 7:14:01and change this to a template string
- 7:14:03using a back tick and a back tick and
- 7:14:06instead of one every time we're going to
- 7:14:08insert and we'll insert the product ID.
- 7:14:12Now in this code we actually have access
- 7:14:14to the cart item from above. So inside
- 7:14:17here we can just insert the cart item.
- 7:14:22ID.
- 7:14:24Next we also need to update this checked
- 7:14:26property. So this determines if the
- 7:14:29selector is checked. So we don't want
- 7:14:32the selector to be checked every time.
- 7:14:35Each cart item has a delivery option ID
- 7:14:38and this is the one that should be
- 7:14:40checked. So, inside here, we're going to
- 7:14:43add a new line. And we're going to make
- 7:14:45check equal and insert some code. And
- 7:14:49we're going to compare if this delivery
- 7:14:52option do ID is equal to the cart items.
- 7:15:00Option ID.
- 7:15:02So only the delivery option that matches
- 7:15:05the delivery option ID in the cart will
- 7:15:08be checked. So that's all the things
- 7:15:10that we have to replace in the delivery
- 7:15:12options. So now let's scroll down and we
- 7:15:16can remove this default code
- 7:15:20and save
- 7:15:22and let's check the website.
- 7:15:25And now we're generating these delivery
- 7:15:28options and the correct ones are checked
- 7:15:31and we have the estimated delivery dates
- 7:15:33for each displayed in an easyto read
- 7:15:36format.
- 7:15:37The last thing we need to generate is
- 7:15:39this delivery date up here. So, we'll go
- 7:15:42back to the code
- 7:15:44and we're going to scroll up
- 7:15:47um to the cart item and we're going to
- 7:15:50find the cart item container and this
- 7:15:52delivery date here. So, this date is
- 7:15:55determined by whichever delivery option
- 7:15:57is selected. So, first we need to find
- 7:16:00the delivery option that is selected.
- 7:16:03So inside this loop, we'll create a
- 7:16:06variable const for the selected
- 7:16:10delivery option and equals.
- 7:16:14And to find this, we're going to get the
- 7:16:17delivery options and then use the method
- 7:16:21dot find and brackets.
- 7:16:24And find gets a function. So brackets,
- 7:16:27arrow, and curly brackets.
- 7:16:30And in this inner function, it receives
- 7:16:33each delivery option. So we'll call it
- 7:16:36delivery option.
- 7:16:38And I'll just add a new line here and
- 7:16:41select this code and press tab to format
- 7:16:44it a little bit.
- 7:16:46So the way that find works is it's going
- 7:16:49to loop through each delivery option.
- 7:16:51And the first function here that returns
- 7:16:53true is going to be the result. So
- 7:16:56that's how we find something. And then
- 7:16:57it's going to save it in this variable.
- 7:17:00So inside here we want to return the
- 7:17:04delivery option id that is equal to the
- 7:17:09cart items
- 7:17:12dot delivery option id. So the delivery
- 7:17:16option that matches what is inside the
- 7:17:18cart item will return true and that's
- 7:17:21going to be saved in here. And now we
- 7:17:24can replace this date. So we'll type
- 7:17:27curly brackets and we'll use day js
- 7:17:30brackets and the selected
- 7:17:34delivery option dot estimated delivery
- 7:17:40time ms
- 7:17:43and outside this bracket we'll use a
- 7:17:45method dot format brackets and give it a
- 7:17:49string and to get this date format we'll
- 7:17:52give it d four times a comma and capital
- 7:17:58M four times space and capital D.
- 7:18:03And now we can remove this date here.
- 7:18:07And before we run this code, there is
- 7:18:09one small problem here because remember
- 7:18:11the delivery options start out as empty.
- 7:18:14So if they are empty, we might not find
- 7:18:17the selected delivery option. To fix
- 7:18:20this in front of this code, we're going
- 7:18:22to check if delivery options.length
- 7:18:27is greater than zero and type and and
- 7:18:32as a review. This works like an if
- 7:18:34statement. It will check if delivery
- 7:18:36options.length is greater than zero and
- 7:18:39then only if it's true, it will run the
- 7:18:42rest of the code. So now we can save
- 7:18:46and check the website.
- 7:18:48And now we're generating everything here
- 7:18:51using data from the back end. So let's
- 7:18:54go back to our code
- 7:18:56and we'll save this into git. Inside the
- 7:18:59git section, we'll type the message
- 7:19:03generate the HTML for the delivery
- 7:19:09options
- 7:19:10and commit.
- 7:19:16Next, if we go back to the website,
- 7:19:19we're going to generate the HTML for the
- 7:19:21payment summary here. Now, we could
- 7:19:24calculate all these numbers inside
- 7:19:26React. However, we're going to use the
- 7:19:29back end to calculate these numbers.
- 7:19:32Remember, the backend's job is to manage
- 7:19:34the data and to do the calculations. The
- 7:19:37front end's job is to present the data
- 7:19:40in an easy to read way. So let's go back
- 7:19:43to our code and we'll go back to the
- 7:19:45checkout page and we'll scroll to the
- 7:19:48top and we're going to create a state
- 7:19:51for the payment summary. So here we'll
- 7:19:54type const square brackets equals use
- 7:19:58state and brackets
- 7:20:01and we'll name this payment summary,
- 7:20:06and the updater function set payment
- 7:20:09summary.
- 7:20:11And as a starting value, we're going to
- 7:20:14use null because the payment summary is
- 7:20:17going to be an object. And it's a little
- 7:20:19easier to check if the object is not
- 7:20:21loaded if we set it to null at the
- 7:20:23start.
- 7:20:24Next, we're going to use Axios to get
- 7:20:27the payment summary from the back end.
- 7:20:29So down here, we'll use Axios.get get
- 7:20:35brackets and we'll give it the URL path
- 7:20:38slash API slash payment-
- 7:20:42summary
- 7:20:43and we'll use dot then to wait for a
- 7:20:46response and give it a function
- 7:20:50and inside here we can get the response.
- 7:20:54Once we get the response, we're going to
- 7:20:56use set payment summary brackets
- 7:21:02response data to save the data inside
- 7:21:06here. So now let's save and check the
- 7:21:09response from the back end. So we'll go
- 7:21:11to our website and open the console and
- 7:21:15open the network tab and we'll refresh
- 7:21:17just in case. We can click this to
- 7:21:20remove the search and type payment
- 7:21:24and we get the payment summary. So we
- 7:21:26can click this and you can see that in
- 7:21:29the response the back end calculated all
- 7:21:31the numbers that we need. So now we just
- 7:21:33need to present this on the page.
- 7:21:36Let's go back and we'll press CtrlF in
- 7:21:40this file and we'll search for payment
- 7:21:44dash summary and press enter to find the
- 7:21:48next result. And this section represents
- 7:21:51the payment summary on the page. And now
- 7:21:55we'll replace these numbers with the
- 7:21:56numbers we got from the back end. So
- 7:21:59first we'll replace the total quantity
- 7:22:01here using curly brackets and we'll
- 7:22:04replace it with payment summary dot
- 7:22:07total items.
- 7:22:10And here we'll replace the product cost.
- 7:22:14I'll type a new line to make it a little
- 7:22:16easier to read. And we'll insert.
- 7:22:19And remember for money we need to format
- 7:22:21it using the function format money
- 7:22:25brackets. and we'll get the payment
- 7:22:29summary dot product cost set.
- 7:22:35We'll scroll down and we'll replace this
- 7:22:37next number. So we'll remove it and new
- 7:22:40line and curly brackets and we'll run
- 7:22:43format money again brackets and this one
- 7:22:47is a shipping cost which is payment
- 7:22:50summary dot shipping cost sense.
- 7:22:55Let's keep going. And this is the total
- 7:22:58before tax. So we'll replace this and
- 7:23:02insert format money brackets
- 7:23:06payment summary dot total cost before
- 7:23:12tax cents.
- 7:23:14And we'll scroll down here and replace
- 7:23:17this. insert format money brackets the
- 7:23:23payment summary dot tax sets
- 7:23:27and then down here we're going to
- 7:23:29replace this one and insert format money
- 7:23:35brackets the payment summary dot total
- 7:23:39cost sense
- 7:23:42and finally we only want to display this
- 7:23:45if the payment summary is loaded
- 7:23:48otherwise we don't have this
- 7:23:49information. So above this code,
- 7:23:53we're going to type curly brackets and
- 7:23:56we'll check if payment summary and and
- 7:24:00brackets.
- 7:24:02So this checks if the payment summary
- 7:24:04object exists. If it's null at the
- 7:24:06start, it's not going to run the rest of
- 7:24:08the code. And now we're going to put all
- 7:24:11of this code inside here. So we'll
- 7:24:13select all this code
- 7:24:16up to the place order button and we'll
- 7:24:19cut
- 7:24:22and inside here we're going to paste.
- 7:24:27Now a problem here is that we're
- 7:24:28returning multiple elements inside this
- 7:24:31brackets. So to fix that we need to wrap
- 7:24:33this in a fragment using less than
- 7:24:36greater than and remove this for now.
- 7:24:40And at the end
- 7:24:42after the button we'll do less than
- 7:24:44slashgreater than. So we're returning
- 7:24:47one element inside this brackets. And
- 7:24:50now we'll format our code using control
- 7:24:52or commandshiftp
- 7:24:54and run format document
- 7:24:58and save.
- 7:25:00And that's it. Now we're generating the
- 7:25:03payment summary using data from the back
- 7:25:05end. We'll go to our website. And that
- 7:25:08looks good. So now let's go back to our
- 7:25:11code and we'll save this into git. In
- 7:25:15the git section, we're going to type the
- 7:25:17message generate HTML for the payment
- 7:25:24summary and commit.
- 7:25:31Next, we're going to work on the orders
- 7:25:33page. So, we'll go back to our files
- 7:25:37and we're going to open the orders page.
- 7:25:41So, first of all, the orders page has a
- 7:25:44header component. And remember, the
- 7:25:46header now needs the cart in order to
- 7:25:49display the quantity. So, we need to
- 7:25:51share the cart with the orders page
- 7:25:53first. We'll go to our files and open
- 7:25:57app.jsx.
- 7:26:00And we'll scroll down. And here we'll
- 7:26:02share the cart with the orders page
- 7:26:04using a prop cart equals curly brackets.
- 7:26:08And we'll save the cart and save.
- 7:26:12And now we'll open the orders page
- 7:26:14again. And at the top we're going to
- 7:26:16access the prop using curly brackets and
- 7:26:19cart. And we're going to pass this cart
- 7:26:22into the header using cart equals curly
- 7:26:26brackets and the cart.
- 7:26:29Let's save.
- 7:26:31and we'll open the website and we're
- 7:26:34going to go to the orders page. So,
- 7:26:36first we'll click here to go to the
- 7:26:38homepage and then we'll click here to
- 7:26:41open the orders page and it should be
- 7:26:43working. So, now we're going to generate
- 7:26:46this HTML using data from the back end.
- 7:26:49So, we'll go back to the orders page and
- 7:26:53at the top we're going to import what we
- 7:26:56need. So first we'll import axios from
- 7:27:01Axios and we'll also import curly
- 7:27:04brackets use state and use effect from
- 7:27:10react.
- 7:27:12And now down here first we'll create
- 7:27:15some state to save the orders using
- 7:27:18const square brackets equals use state
- 7:27:22and brackets.
- 7:27:24And we'll call this data the orders,
- 7:27:27comma, and the updater function set
- 7:27:31orders.
- 7:27:33And at the start, we'll set the orders
- 7:27:35to an empty array. And below this, we're
- 7:27:38going to make a request to the back end
- 7:27:41using use effect brackets and a function
- 7:27:46brackets arrow and curly brackets. And
- 7:27:48down here, we'll give it a empty array
- 7:27:51so that it only runs once. And inside
- 7:27:55we're going to use axios.get
- 7:27:58again. So brackets and the URL path for
- 7:28:01the orders is slappi/ord
- 7:28:05orders
- 7:28:06and then we'll do dot then to wait for
- 7:28:09the response from the back end. We'll
- 7:28:12give this a function brackets arrow and
- 7:28:14curly brackets. Inside this parameter is
- 7:28:17the response from the back end. And once
- 7:28:22we get the response, we're going to save
- 7:28:24it inside orders using set orders
- 7:28:28brackets
- 7:28:30response
- 7:28:31data. Let's save.
- 7:28:35And now we can check the network tab to
- 7:28:38see if we got the orders. We'll open the
- 7:28:40console.
- 7:28:42Inside the network tab, we can refresh
- 7:28:46and remove this and search for orders.
- 7:28:50And down here, we'll click this request
- 7:28:53and go to the response tab. And the back
- 7:28:56end gave us the orders that we need to
- 7:28:59display. Now, a problem here is that we
- 7:29:02only have the product ID in the order,
- 7:29:05but we also need to display some product
- 7:29:07details on the website. And again, the
- 7:29:10backend supports an expand parameter for
- 7:29:13this URL path. So at the end here, we'll
- 7:29:16add question mark expand equals
- 7:29:21products. This time, make sure you have
- 7:29:23an s at the end. So now let's save
- 7:29:28and check the network tab again. And
- 7:29:31we'll refresh.
- 7:29:34And now inside the orders request,
- 7:29:36you'll see that each order has the
- 7:29:38product ID, but it also has the other
- 7:29:41data for the product. And this is what
- 7:29:44we need to display on the website.
- 7:29:47So now we'll go into our code and we're
- 7:29:49going to generate the HTML. So first
- 7:29:52we'll press control or command F and
- 7:29:55we're going to search for order dash
- 7:29:58container
- 7:30:00which is down here. And we can collapse
- 7:30:03this code and collapse the other order
- 7:30:06container. Each of these represents an
- 7:30:09order on the website. So, we're going to
- 7:30:12generate this HTML.
- 7:30:14At the top, we'll insert and we'll run
- 7:30:17orders.m map brackets. And we'll give it
- 7:30:21a function.
- 7:30:23And this parameter is going to be each
- 7:30:25order.
- 7:30:28And for each order, we're going to
- 7:30:30return some HTML.
- 7:30:33So, we want each order to be converted
- 7:30:35into this HTML. We're going to select it
- 7:30:38and copy it inside here. We'll paste.
- 7:30:43Press control or command shiftp and run
- 7:30:46format document.
- 7:30:49And we'll scroll back up.
- 7:30:52As always, we need a key prop when we're
- 7:30:54looping through an array. We'll set the
- 7:30:57key equals. And each order has an order
- 7:31:01ID property.
- 7:31:03As usual, we're going to replace the
- 7:31:05values in here. Feel free to pause if
- 7:31:07you want to try it yourself first. So
- 7:31:10for the delivery date, we're going to
- 7:31:13insert and we're going to use dayjs
- 7:31:17brackets and each order has an order dot
- 7:31:21order time ms
- 7:31:24and outside this bracket we use dot
- 7:31:26format brackets and a string. And to
- 7:31:30display the date, we're going to use
- 7:31:32four capital M's for the month and a
- 7:31:35capital D for the date. So now we also
- 7:31:38need to import day.js. So we'll scroll
- 7:31:41to the top and at the top we'll import
- 7:31:45dayjs from a string dayjs.
- 7:31:50And let's also import format money
- 7:31:52because we're going to use it later. So,
- 7:31:54import curly brackets format money from
- 7:31:59a string dot dot slashyouutills/
- 7:32:03money.
- 7:32:05Now, we're going to scroll back down
- 7:32:08and we're going to replace the total
- 7:32:10order cost. So here we're going to
- 7:32:12remove this and curly brackets we're
- 7:32:15going to insert format money brackets
- 7:32:20and give it the order dot total cost
- 7:32:24sense.
- 7:32:26We'll scroll down and here we're going
- 7:32:28to replace this with the order ID. So
- 7:32:31curly brackets order ID.
- 7:32:36Next, inside this element order details
- 7:32:39grid. This displays the list of products
- 7:32:42in the order and we're going to generate
- 7:32:44this using React as well. So above this
- 7:32:49we're going to use curly brackets and
- 7:32:52we're going to get the order products
- 7:32:55array and we're going to loop through it
- 7:32:57using map brackets and give it a
- 7:33:00function
- 7:33:02and save each product
- 7:33:05and for each product we're going to
- 7:33:07return brackets some HTML.
- 7:33:11So if we collapse this code and collapse
- 7:33:14this element and this element and we
- 7:33:17also collapse these.
- 7:33:21So three of these elements represents
- 7:33:23one product. It's a little different
- 7:33:26because we don't have an element that
- 7:33:28contains all of these because we're
- 7:33:30using a grid here, but it works in a
- 7:33:33similar way. We're going to copy this
- 7:33:36code and inside return we're going to
- 7:33:39paste
- 7:33:41and the one difference here is that if
- 7:33:43we scroll up
- 7:33:45we're returning multiple elements from
- 7:33:47this function. So we need to wrap these
- 7:33:49in a fragment. So at the top we'll type
- 7:33:53less than greater than. But then we have
- 7:33:55another problem here which is remember
- 7:33:58when we loop through an array each
- 7:34:00element that we return should have a key
- 7:34:02property and we can't add props to
- 7:34:05fragments like this. To solve this we
- 7:34:08need to use the full fragment syntax. So
- 7:34:11we're going to scroll to the top and for
- 7:34:14React we're going to import using comma
- 7:34:18the fragment component.
- 7:34:21So we'll scroll back down and in here
- 7:34:24we're going to use a full fragment
- 7:34:26syntax
- 7:34:28and remove this. So this does the same
- 7:34:31thing except now we can add a key prop
- 7:34:34to it equals curly brackets and let's
- 7:34:37set the prop to the product ID.
- 7:34:42and we'll scroll to the bottom.
- 7:34:45And before these brackets, remember to
- 7:34:47add the closing fragment tag.
- 7:34:52And finally, we'll press control or
- 7:34:54commandshiftp
- 7:34:56and format document.
- 7:34:58And now we can replace this default code
- 7:35:01that we have down here.
- 7:35:06Finally, we're going to replace each
- 7:35:08product's details. So here we're going
- 7:35:12to replace the name and insert the
- 7:35:14product name. And here let's also
- 7:35:18replace the image
- 7:35:20curly brackets. Replace it with product.
- 7:35:25And actually I made a mistake. I forgot
- 7:35:27that all the product details are saved
- 7:35:29in a property called product. So let's
- 7:35:32actually rename this to order product.
- 7:35:37And then here we're going to do the
- 7:35:39order product dotproduct ID and the
- 7:35:44order product.product.im
- 7:35:46image and the order
- 7:35:49product.product.name.
- 7:35:52And then down here to replace the
- 7:35:54delivery date. This is directly on the
- 7:35:57order product. So we can remove this and
- 7:36:01insert and we'll use dayjs and bracket
- 7:36:05and give it the order product dot
- 7:36:08estimated delivery time ms and outside
- 7:36:13these brackets we'll do dot format
- 7:36:17brackets and a string and we'll use the
- 7:36:20format capital M four times for the
- 7:36:24month and capital D for the date. And
- 7:36:27then down here for the quantity, we're
- 7:36:30going to insert and the quantity is
- 7:36:33directly on the order product dot
- 7:36:36quantity
- 7:36:38and save.
- 7:36:40And now if we scroll down, we can also
- 7:36:44remove the rest of this default HTML
- 7:36:48and save this file. And now let's check
- 7:36:52our website.
- 7:36:54And everything should be working except
- 7:36:56we're generating the HTML for these
- 7:36:58orders using data from the back end. So
- 7:37:01now let's go back and we'll save this
- 7:37:04into git. We'll open the git section
- 7:37:08and we'll give it the message generate
- 7:37:11the HTML for the orders page and commit.
- 7:37:22Next, if we look at our code right now,
- 7:37:25you'll notice we have a lot of code in
- 7:37:27one file. To make our code cleaner, we
- 7:37:30should separate our code into smaller
- 7:37:33components. So, let's go to our files
- 7:37:36and we're going to start with the
- 7:37:38homepage.
- 7:37:40So, inside the homepage, we're showing a
- 7:37:43grid of products. To make the code
- 7:37:46cleaner, we can separate this into its
- 7:37:48own component. So, we'll go to the pages
- 7:37:51folder and we'll click it and we're
- 7:37:54going to create a new file and let's
- 7:37:57call it the products grid.jsx.
- 7:38:04And now we have multiple components that
- 7:38:07are related to the homepage. So, usually
- 7:38:10we organize this into a folder. So,
- 7:38:13inside here, we're going to create a new
- 7:38:15folder and we're going to call it home
- 7:38:19and press enter.
- 7:38:21And we group all the components related
- 7:38:23to the homepage inside here. So, we'll
- 7:38:26move the homepage.jsx.
- 7:38:29And let's update the imports
- 7:38:32and also homepage.css
- 7:38:36and also the products grid.
- 7:38:41And whenever we move files, we need to
- 7:38:43make sure the imports are still correct.
- 7:38:45So in homepage, I'm going to scroll up
- 7:38:48and we should make sure that we have two
- 7:38:51dot dot slashes in front because now
- 7:38:53we're inside an additional folder. So
- 7:38:55that looks good to me. And we're also
- 7:38:58going to go to app.jsx
- 7:39:01and scroll up and make sure that the
- 7:39:04homepage is now inside the home folder.
- 7:39:07So now let's save and check our website
- 7:39:11and go to the homepage to make sure it
- 7:39:14still works. So now let's go back to our
- 7:39:17code and we're going to separate the
- 7:39:19products grid into its own file. So in
- 7:39:22here we'll create the component using
- 7:39:25export function
- 7:39:28products grid brackets and curly
- 7:39:32brackets and we'll return some HTML
- 7:39:36and we're just going to copy the HTML to
- 7:39:38here. So, inside the homepage, we're
- 7:39:41going to scroll down to the products
- 7:39:44grid and we can collapse this element
- 7:39:48and select all this code and copy
- 7:39:52inside the products grid. Inside here,
- 7:39:55we can paste
- 7:39:58and press control or commandshiftp and
- 7:40:01format document.
- 7:40:03Now, we'll scroll up and we're going to
- 7:40:05fix these errors. So first we need
- 7:40:08access to the products. So we can pass
- 7:40:10those in as a prop curly brackets
- 7:40:14products.
- 7:40:15And down here we also need the format
- 7:40:18money function. So at the top we're
- 7:40:22going to import curly brackets format
- 7:40:27money
- 7:40:28from a string and we'll find that utils
- 7:40:31file. So we need to get out of two
- 7:40:33folders this time using dot dot slash
- 7:40:36dot dot slash utils/money
- 7:40:40and save.
- 7:40:42And now inside the homepage at the top
- 7:40:46we're going to import our products grid.
- 7:40:48So import curly brackets
- 7:40:51the products grid
- 7:40:55from a string and it's right beside the
- 7:40:58homepage. So we can just do dot slash
- 7:41:01products grid.
- 7:41:04And then down here we can remove this
- 7:41:06HTML
- 7:41:09and just use our component less than
- 7:41:12products grid slashgreater than and we
- 7:41:16need to give it the products. So we'll
- 7:41:18create a prop products equals curly
- 7:41:22brackets and we'll give it the products.
- 7:41:26So, as you can see, this makes the code
- 7:41:28much cleaner when we separate it into
- 7:41:30smaller components. The last step is at
- 7:41:33the top, we can remove this format money
- 7:41:36function since we're not using it
- 7:41:38anymore. Let's save
- 7:41:41and let's open the website and make sure
- 7:41:44everything still works. So, that looks
- 7:41:47good.
- 7:41:48Next, we're going to do the checkout
- 7:41:50page. So, we'll go back to our code
- 7:41:54and we're going to open the checkout
- 7:41:56page and we're going to scroll to the
- 7:41:59top.
- 7:42:02And I'll leave separating the checkout
- 7:42:04header as an exercise. But we're going
- 7:42:07to scroll down here and we can move the
- 7:42:10order summary section into its own
- 7:42:12component. So, inside the pages folder,
- 7:42:16I'm going to click it and create a new
- 7:42:18folder called checkout.
- 7:42:21Now, you might have already done this if
- 7:42:23you did the exercises from the previous
- 7:42:25lesson, but we're going to use this
- 7:42:27folder checkout. And I'm going to move
- 7:42:29the checkout page inside here.
- 7:42:33And update the imports and also move the
- 7:42:36CSS files.
- 7:42:40And I'll make sure that the imports look
- 7:42:42good. So, in here at the top, make sure
- 7:42:46there's two dot dot slashes in front.
- 7:42:49And inside app.jsx,
- 7:42:52make sure that there is a checkout
- 7:42:53folder in front of this component. So
- 7:42:56I'll go back to the checkout page and
- 7:43:00I'll create a new file. Let's call it
- 7:43:03the order summary.jsx.
- 7:43:09And we're going to create export
- 7:43:12function order summary brackets and
- 7:43:16curly brackets. and we're going to
- 7:43:18return brackets the HTML for the order
- 7:43:23summary. Inside the checkout page, we're
- 7:43:26going to scroll down
- 7:43:29and find the order summary. We can
- 7:43:31collapse it and select this code and
- 7:43:35copy.
- 7:43:36Inside the order summary, we'll paste
- 7:43:40and press control or command shift P and
- 7:43:43format document.
- 7:43:46and we'll scroll to the top and we're
- 7:43:48going to need the delivery options and
- 7:43:50the cart inside here. So, we're going to
- 7:43:53get this from the props using curly
- 7:43:55brackets, the cart, comma, and the
- 7:43:58delivery options.
- 7:44:01And we also need dayjs and the format
- 7:44:04money function. So at the top we'll also
- 7:44:07import dayjs
- 7:44:09from day js and import curly brackets
- 7:44:15format money
- 7:44:18from a string
- 7:44:21and we'll find the utils file which is
- 7:44:24dot dot slash dot dot slash utils
- 7:44:28slashmoney
- 7:44:30and all the errors should now be gone.
- 7:44:33We'll save.
- 7:44:35And inside checkout page, we can remove
- 7:44:38this code.
- 7:44:40And we can use our new component less
- 7:44:42than order summary /grater than. And we
- 7:44:48need to give it the cart equals curly
- 7:44:50brackets and save the cart. And we need
- 7:44:53to give it the delivery options equals
- 7:44:57curly brackets. And give it the delivery
- 7:45:01options. And we need to import this at
- 7:45:04the top. So we'll scroll up and here we
- 7:45:08can import curly brackets the order
- 7:45:12summary
- 7:45:14from a string dot slash order summary.
- 7:45:21And we don't need dayjs anymore.
- 7:45:24So let's save
- 7:45:26and check the website. And everything
- 7:45:29still works.
- 7:45:32We'll go back
- 7:45:33and scroll down.
- 7:45:36And next, we can also move the payment
- 7:45:38summary into its own component. Feel
- 7:45:40free to pause the video if you want to
- 7:45:42try it yourself first. So, inside the
- 7:45:45checkout folder, we'll click here and
- 7:45:47create a new file, payment summary.jsx,
- 7:45:52and we'll create the component export
- 7:45:55function payment summary brackets and
- 7:45:59curly brackets. and we'll return the
- 7:46:02HTML.
- 7:46:03Inside the checkout page, we're going to
- 7:46:06collapse this element and select the
- 7:46:08HTML and copy
- 7:46:11inside the payment summary. We're going
- 7:46:14to paste
- 7:46:15control or command shiftp format
- 7:46:18document
- 7:46:19and we're going to scroll back up and
- 7:46:22we're going to get the payment summary
- 7:46:24from the props. So curly brackets
- 7:46:27payment summary and we need the format
- 7:46:30money function. So up here import curly
- 7:46:34brackets format money from a string dot
- 7:46:39dot slash dot do/youutils/money
- 7:46:43and save.
- 7:46:46In the checkout page, we can remove this
- 7:46:49code and replace it with less than
- 7:46:52payment summary slashgreater than. And
- 7:46:56we need to give this the payment summary
- 7:47:00equals curly brackets and save the
- 7:47:02payment summary.
- 7:47:06And as you can see, our code is much
- 7:47:08cleaner now because we separated it into
- 7:47:10smaller components. So now we need to
- 7:47:13import this. So, we're going to scroll
- 7:47:15to the top and here we'll import curly
- 7:47:19brackets the payment summary
- 7:47:23from a string dot slashpayment
- 7:47:26summary.
- 7:47:28And we don't need format money anymore
- 7:47:32and save.
- 7:47:35Let's go back to the website and
- 7:47:37everything is still working.
- 7:47:40We'll go back to our code
- 7:47:43and we're going to go inside the order
- 7:47:46summary component.
- 7:47:48So in here the code is still a little
- 7:47:50bit too complicated. So what we can do
- 7:47:53is we can find this element with the
- 7:47:56class delivery options and we can
- 7:47:58separate this into its own component as
- 7:48:00well. Feel free to pause the video if
- 7:48:02you want to try this yourself first. So,
- 7:48:05I'm going to go to the checkout folder
- 7:48:07and create a new file and name it
- 7:48:10delivery options.jsx.
- 7:48:15And we'll create a component export
- 7:48:18function delivery options brackets and
- 7:48:22curly brackets and return some HTML. And
- 7:48:26back to the order summary. And here we
- 7:48:29can collapse this element and select it
- 7:48:33and copy.
- 7:48:34Inside delivery options inside here we
- 7:48:38can paste. Control or command shiftp and
- 7:48:42format document.
- 7:48:44And we'll scroll to the top. Here we
- 7:48:47need to get the delivery options using
- 7:48:49the props. So curly brackets delivery
- 7:48:52options.
- 7:48:54And we're going to import format money.
- 7:48:57So up here we're going to import curly
- 7:49:00brackets format money from a string dot
- 7:49:05dot slash dot dot slash utils/money
- 7:49:09and we also need dayjs. So up here we'll
- 7:49:13import dayjs from a string dayjs
- 7:49:19and I think we also need another thing
- 7:49:22here which is the cart item. So we're
- 7:49:24going to get this from the props as
- 7:49:26well.
- 7:49:26So up here we're also going to get the
- 7:49:30cart item and save.
- 7:49:34And now in the order summary we can
- 7:49:37replace all this code with our component
- 7:49:40less than delivery options /grater than.
- 7:49:45And we're going to give it the cart item
- 7:49:48equals curly brackets. save the cart
- 7:49:50item and give it the delivery options
- 7:49:55equals curly brackets save the delivery
- 7:49:58options
- 7:50:00and we also need to import this. So at
- 7:50:03the top
- 7:50:05we can import curly brackets the
- 7:50:09delivery options
- 7:50:12from a string
- 7:50:14dot slash delivery options
- 7:50:18and save
- 7:50:20and let's check our website and make
- 7:50:22sure it still works. So that looks good
- 7:50:25to me. So if we go back to our code, you
- 7:50:29can see that our code is cleaner now
- 7:50:31after we separate this into another
- 7:50:33component. And remember, it's up to us
- 7:50:36how we want to separate our components.
- 7:50:38We can actually keep separating this
- 7:50:40into smaller components if we want. For
- 7:50:43example, we can also have a cart item
- 7:50:46details component or a delivery date
- 7:50:48component, but I'll leave these as
- 7:50:50exercises. I'm also going to leave the
- 7:50:53orders page as an exercise, but we can
- 7:50:56just get started on it. So, here inside
- 7:50:59the pages folder, we're going to create
- 7:51:02a new folder for the orders page. So,
- 7:51:05orders
- 7:51:07and we're going to move this component
- 7:51:09inside here
- 7:51:12and update the imports.
- 7:51:14And we're going to move this inside here
- 7:51:16as well. And just make sure the imports
- 7:51:19look okay. So in here, we'll scroll up.
- 7:51:22Make sure that these two have two dot
- 7:51:25dot slashes in front. That looks good.
- 7:51:28And in app.jsx,
- 7:51:30make sure that the orders page is now
- 7:51:32inside an orders folder. So we'll save
- 7:51:36this and open the website.
- 7:51:39And we're going to click here to go to
- 7:51:41home and click here to go to orders. And
- 7:51:44the orders page should still be working.
- 7:51:47So I'll leave separating this as an
- 7:51:49exercise. We'll go back to our code and
- 7:51:52we'll save this into git. Inside the git
- 7:51:56section, we'll create the message
- 7:51:59separate into smaller components and
- 7:52:04commit.
- 7:52:10Next, we're going to go back to our
- 7:52:12files and we're going to go to our
- 7:52:15homepage.
- 7:52:17and we're going to review async await.
- 7:52:21Async await is a feature of JavaScript
- 7:52:24and it lets us write asynchronous code
- 7:52:26like normal code. Right now we're using
- 7:52:30promises which means we need to use to
- 7:52:33wait for this code to finish and then
- 7:52:36run this function with async await. We
- 7:52:39can actually write this like normal
- 7:52:42code. To do that in front of axios we
- 7:52:45can type await and space. So this is
- 7:52:50going to wait for this code to finish
- 7:52:53and then it's going to move on to the
- 7:52:55next line. And now because we're waiting
- 7:52:58for this code to finish, we can actually
- 7:53:01save this result in a variable. So at
- 7:53:03the front we can save it in const
- 7:53:07response equals await this code.
- 7:53:12And now we don't need to use dot then
- 7:53:14anymore
- 7:53:16and we can remove it.
- 7:53:19However, in order to use a weight, we
- 7:53:22need to be inside an async function. So
- 7:53:25in front of this function, we need to
- 7:53:27add async and space. As you can see,
- 7:53:32async8 lets us write asynchronous code
- 7:53:36like normal code or code that runs line
- 7:53:39by line. And async await makes the code
- 7:53:42easier to understand. So in react it's
- 7:53:46recommended to use async await instead
- 7:53:48of promises when we can. However, in
- 7:53:52React there is a small problem with
- 7:53:54using async await and use effect. As a
- 7:53:58review, when we put async in front of a
- 7:54:01function, this function will now return
- 7:54:04a promise. That's because this function
- 7:54:07has some asynchronous code inside or
- 7:54:10code that takes some time to finish. So
- 7:54:12that means that this entire function is
- 7:54:15going to take some time to finish.
- 7:54:17That's why it has to return a promise.
- 7:54:20Now the problem is the inner function in
- 7:54:22use effect should not return a promise.
- 7:54:26It should only return nothing or a
- 7:54:28cleanup function like this. A cleanup
- 7:54:31function is useful if we want to do some
- 7:54:33cleanup when this component is removed.
- 7:54:36So because this inner function is now
- 7:54:38returning a promise, it's sort of
- 7:54:40breaking the rules of use effect. To
- 7:54:44solve this, we usually create a new
- 7:54:46function inside use effect like const
- 7:54:50get home data equals and create a new
- 7:54:54function brackets arrow and curly
- 7:54:57brackets. And we're going to make this
- 7:54:59function async. And we'll leave this as
- 7:55:03an ordinary function.
- 7:55:06And now we're going to move our code
- 7:55:08inside here. So we'll cut it and paste
- 7:55:11it. And now because we're inside an
- 7:55:13async function, we can now use a weight
- 7:55:16and it will also not mess up use effect.
- 7:55:19At the end, we just need to run this
- 7:55:21function using get home data and
- 7:55:26brackets.
- 7:55:28So this gets the same data as before but
- 7:55:31it uses async await. So remember when
- 7:55:34using async await inside use effect we
- 7:55:37need to create a new function and then
- 7:55:40run the function otherwise it will break
- 7:55:42the rules of use effect. So let's save
- 7:55:47and check the homepage
- 7:55:50and go to the homepage and everything
- 7:55:52still works. So let's go back
- 7:55:56and we'll use async await in the app
- 7:55:59component
- 7:56:01where we get the cart. So here feel free
- 7:56:05to pause if you want to try yourself
- 7:56:07first. We'll create a new function using
- 7:56:10const. Let's call it fetch app data
- 7:56:15equals a function brackets arrow and
- 7:56:18curly brackets.
- 7:56:20And we'll move this code into the
- 7:56:22function. So we'll cut and paste and
- 7:56:26we'll select this and press tab.
- 7:56:29In front of the function we'll use
- 7:56:32async. And now we can use await inside.
- 7:56:36So in front of axios we'll type await to
- 7:56:39wait for this to finish. And then we're
- 7:56:42going to save the result in a variable
- 7:56:44const response equals this result.
- 7:56:49And now we can remove
- 7:56:51and these brackets
- 7:56:56and the code is much cleaner. Finally,
- 7:56:58we're going to run this function fetch
- 7:57:01app data and brackets and save.
- 7:57:06And let's do the same thing in the
- 7:57:08checkout page. So we'll go to the
- 7:57:10checkout page here.
- 7:57:12Feel free to pause if you want to try it
- 7:57:14yourself. const fetch checkout data
- 7:57:19equals brackets arrow and curly brackets
- 7:57:23and we'll move this code inside cut
- 7:57:28and in here paste and select these lines
- 7:57:32and press tab and we're going to turn
- 7:57:35this into an async function so we can
- 7:57:38use await inside. So in front of this
- 7:57:41request, we're going to await.
- 7:57:44And we're going to save it in a variable
- 7:57:46let response
- 7:57:49equals. So we're going to use let
- 7:57:51because we have two requests here and we
- 7:57:53might want to reuse this variable. And
- 7:57:56I'll add a new line to make it a little
- 7:57:58easier to read. And now we can remove.
- 7:58:05And then down here, we'll also use await
- 7:58:08to wait for this to finish. And we're
- 7:58:11going to save the result inside response
- 7:58:14equals.
- 7:58:16And now we can remove then.
- 7:58:21And the last step is to run this
- 7:58:23function. So fetch checkout data and
- 7:58:28brackets and save.
- 7:58:31We'll open the website. Make sure the
- 7:58:34homepage is working and then go to the
- 7:58:36checkout page and make sure this is
- 7:58:38still working. So that looks good to me.
- 7:58:42We'll go back to our code
- 7:58:44and that's async await. It lets us write
- 7:58:47asynchronous code like normal code. I'll
- 7:58:51leave converting the orders page to
- 7:58:53async aait as an exercise. So now we can
- 7:58:56save this into git.
- 7:58:59We'll give it the message convert home
- 7:59:03and checkout
- 7:59:05to async await and commit.
- 7:59:14In the next lesson, we're going to learn
- 7:59:16data mutation, which means instead of
- 7:59:19just getting data from the back end,
- 7:59:21we'll update data in the back end, and
- 7:59:24we'll be able to add products to our
- 7:59:26cart and create orders. And that's the
- 7:59:29end of this lesson. In this lesson, we
- 7:59:32reviewed backend, which manages the data
- 7:59:35for a website. We set up a backend for
- 7:59:38our project. We learned how to fetch
- 7:59:41data or get data from the back end. We
- 7:59:44learned Axios, which is an easier way to
- 7:59:47make requests to the back end. We
- 7:59:49learned how to generate the HTML using
- 7:59:52the data from the back end. We separated
- 7:59:55our pages into smaller components and we
- 7:59:59learned async a weight which lets us
- 8:00:01write asynchronous code like normal
- 8:00:04code. Here are some exercises to help
- 8:00:07you practice what we learned in this
- 8:00:09lesson.
- 8:01:03In this lesson, we're going to learn
- 8:01:05data mutation and we're going to make
- 8:01:07the website interactive. Data mutation
- 8:01:10means we update data in the back end. If
- 8:01:14we open the website
- 8:01:16and I'll close some of these other tabs.
- 8:01:20If we go to the homepage,
- 8:01:23notice that right now we can only view
- 8:01:26the data. If we try to update this data,
- 8:01:29like we try to add to cart, it doesn't
- 8:01:32do anything. So, we're going to make
- 8:01:34this add to cart button work. We'll go
- 8:01:37to our code
- 8:01:39and we're going to find the add to cart
- 8:01:41button. So, I'll open my files and I'm
- 8:01:45going to go to the homepage and we're
- 8:01:48going to open the products grid.
- 8:01:51And we're going to scroll down in this
- 8:01:52file to this add to cart button. The
- 8:01:57first step is to make this button
- 8:01:59interactive. So at the end we're going
- 8:02:02to add an on click attribute equals
- 8:02:05curly brackets and give this a function
- 8:02:08brackets arrow and curly brackets.
- 8:02:12As a review on click is called an event
- 8:02:15handler. When we click this button it
- 8:02:17will run this function.
- 8:02:20Now, inside here, we're going to add the
- 8:02:22product to the cart. So, we could try to
- 8:02:25update the cart here in React, but then
- 8:02:28we have to figure out where to save the
- 8:02:30cart. Instead, in the real world, we use
- 8:02:34the backend to update the cart and to
- 8:02:37save the cart. The advantage of using
- 8:02:39the backend is it will simplify our
- 8:02:42React code and we can share the data in
- 8:02:45the back end. So if we access this
- 8:02:47website from another computer, we can
- 8:02:49access the updated cart using the back
- 8:02:52end.
- 8:02:53So here to use a backend to add a
- 8:02:56product to the cart. We're just going to
- 8:02:58make another request to the back end.
- 8:03:02We're going to go to the top
- 8:03:04and at the top we're going to import
- 8:03:07axios so that we can make another
- 8:03:09request.
- 8:03:12and we'll scroll back down.
- 8:03:15And when we click this button, we're
- 8:03:17going to make a request to the back end
- 8:03:19using axios. And this time we're going
- 8:03:22to use post and brackets.
- 8:03:26So previously we used axios.get
- 8:03:30to get data from the backend. Post is a
- 8:03:34different type of request.
- 8:03:37Is used to create data in the back end.
- 8:03:40In this case, we are adding a product to
- 8:03:43the back end. So, we're creating a new
- 8:03:45cart item. That's why we use post.
- 8:03:49Inside here, we're going to give it a
- 8:03:51URL path slash API/cart-
- 8:03:56items. So, notice this is the same URL
- 8:04:00path that we used to get the cart. But
- 8:04:03when we use post, the backend will
- 8:04:05behave differently. This will create a
- 8:04:08cart item instead of getting the cart
- 8:04:10items.
- 8:04:12Next, we need to tell the backend which
- 8:04:14product to add to the cart. To do that,
- 8:04:17we can give axios.post another value
- 8:04:21which is an object.
- 8:04:24When we use post, we can also send
- 8:04:27information to the back end using an
- 8:04:30object. And this is called the request
- 8:04:32body. Now the back end needs to know two
- 8:04:36things in order to add a product to the
- 8:04:38cart. First, it needs to know the
- 8:04:41product ID that we want to add. In this
- 8:04:46case, this button is for if we scroll
- 8:04:49up, the button is for this product right
- 8:04:53here. So, at the bottom,
- 8:04:57we're going to tell the back end we want
- 8:04:58to add this product
- 8:05:01ID to the cart. And we also need another
- 8:05:05information which is the quantity that
- 8:05:07we want to add. For now, let's just use
- 8:05:10the quantity one. So this code will send
- 8:05:13a request to the back end telling it to
- 8:05:16add this product to the cart with a
- 8:05:19quantity of one.
- 8:05:21So now let's save this file
- 8:05:24and then we'll go to the website
- 8:05:28and we're going to go to this t-shirt
- 8:05:30product and we're going to click add to
- 8:05:32cart.
- 8:05:34And then we're going to refresh the
- 8:05:36page.
- 8:05:38And now if we look at the cart here, we
- 8:05:40have one more product. The quantity is
- 8:05:43four. If we open the cart and we scroll
- 8:05:46to the bottom, notice that it added the
- 8:05:49t-shirt to the cart. So that's how we do
- 8:05:51data mutation or update data in the back
- 8:05:54end. We just send a request to the back
- 8:05:57end and the backend takes care of
- 8:06:00updating and saving the data. So, in the
- 8:06:03real world, we use the back end to
- 8:06:05manage and update the data.
- 8:06:09Next, notice that when we added this
- 8:06:11product to the cart, we had to refresh
- 8:06:14the page to see the new cart. It would
- 8:06:17be better if we could just reload the
- 8:06:19cart and update the page without needing
- 8:06:22to refresh.
- 8:06:24We'll go to our code
- 8:06:26and at the bottom, we're going to go to
- 8:06:29app.jsx.
- 8:06:32So here this code reloads the cart from
- 8:06:36the back end and it updates the state.
- 8:06:40After it updates the state, React will
- 8:06:42update the page. So when we add a
- 8:06:45product to the cart, we just need to
- 8:06:48rerun this code and it will update the
- 8:06:50page and then we don't have to refresh
- 8:06:53the page every time.
- 8:06:55We're going to select this entire
- 8:06:57function and we'll cut.
- 8:07:00And outside the use effect, we're going
- 8:07:02to paste it.
- 8:07:05And now that it's outside the use
- 8:07:07effect, we can share this function with
- 8:07:09our components.
- 8:07:11Now, let's actually rename this function
- 8:07:14to something more accurate like load
- 8:07:17cart. And in here, load cart.
- 8:07:22And now we can share this with our
- 8:07:24components using a prop. So we're going
- 8:07:27to scroll down here and in the homepage
- 8:07:31we're going to give it the prop load
- 8:07:33cart equals curly brackets and we'll
- 8:07:37give it the function load cart and save.
- 8:07:42And now if we go to the homepage
- 8:07:45at the top, we'll get load cart out of
- 8:07:49the props and then give it to the
- 8:07:52products grid component. So remember
- 8:07:55this component contains the add to cart
- 8:07:58button. So in here we'll give it another
- 8:08:01prop load cart equals curly brackets and
- 8:08:05give it the load cart function and save.
- 8:08:10And now inside the products grid
- 8:08:14at the top, we're going to get load cart
- 8:08:19out of the props. And then if we scroll
- 8:08:22back down,
- 8:08:24after we update the cart, we're going to
- 8:08:27call the function load cart.
- 8:08:31So this means we will update the cart in
- 8:08:33the back end and then we'll get the new
- 8:08:36cart and update the page without needing
- 8:08:39to refresh.
- 8:08:41One other thing we have to do here is
- 8:08:43remember axios.post
- 8:08:46contacts the back end. So this is
- 8:08:49asynchronous code. It does not finish
- 8:08:51right away. That means we need to wait
- 8:08:54for this code to finish before going on
- 8:08:57to the next line. To wait for this to
- 8:09:00finish at the front of this code we can
- 8:09:03type await and space. And remember in
- 8:09:07order to use await we need to be inside
- 8:09:10an async function. So we'll add async.
- 8:09:15And also this code is not inside a use
- 8:09:17effect. So we can just use async
- 8:09:20directly here like this.
- 8:09:22After that load cart is also
- 8:09:25asynchronous. So it's a good idea to
- 8:09:28await this code as well to be
- 8:09:31consistent. So now let's save
- 8:09:35and we'll check the website.
- 8:09:38And first we'll go back to the homepage.
- 8:09:42And this time let's click add to cart on
- 8:09:44this toaster. So we'll click add to
- 8:09:47cart.
- 8:09:49And now notice that the quantity here
- 8:09:51updated to five without needing to
- 8:09:54refresh. If we go to the cart and scroll
- 8:09:57down,
- 8:09:59it also updated the cart on the page
- 8:10:02without needing to refresh.
- 8:10:05We'll go back to the homepage
- 8:10:08and we're going to make these quantity
- 8:10:10selectors work. So, this lets us add
- 8:10:13more than one product to the cart at a
- 8:10:15time. We're going to go to our code,
- 8:10:19and first we need to get the quantity in
- 8:10:22the quantity selector. To do that, we're
- 8:10:25going to use a controlled input. We
- 8:10:28learned controlled inputs when creating
- 8:10:30the text box in the chatbot project. So,
- 8:10:33as a review, we'll scroll up
- 8:10:36and we're going to create a state to
- 8:10:38save the quantity. At the top, we're
- 8:10:42going to import curly brackets use state
- 8:10:47from React.
- 8:10:49And in this component,
- 8:10:51we're going to create a state const
- 8:10:54bracket equals use state and brackets.
- 8:11:00And we're going to name the state the
- 8:11:02quantity,
- 8:11:04comma, and the updater function set
- 8:11:07quantity.
- 8:11:09And we'll set the starting value of the
- 8:11:11quantity to one.
- 8:11:13Then we're going to scroll down and
- 8:11:16we're going to find this select element.
- 8:11:19So this element represents the selector
- 8:11:22on the page and we're going to give this
- 8:11:24two props. The first prop is the value
- 8:11:28equals curly brackets and the value is
- 8:11:31the quantity.
- 8:11:33The second prop is on change equals
- 8:11:37curly brackets. And we're going to give
- 8:11:39this a function. So brackets, arrow and
- 8:11:42curly brackets.
- 8:11:44And whenever we use an event handler
- 8:11:46like on click or on change, this
- 8:11:50function gets a parameter called event.
- 8:11:53So event contains details about the
- 8:11:56event. In this case, the event will
- 8:11:58contain the quantity that we selected,
- 8:12:00which is one of these values. To get the
- 8:12:04quantity that we selected, we can use
- 8:12:07event.target.
- 8:12:10value
- 8:12:11and let's save the quantity was selected
- 8:12:14in a variable at the front. We'll create
- 8:12:17const quantity selected equals this
- 8:12:22value.
- 8:12:24Now one thing to be careful of is this
- 8:12:27value is going to be one of these values
- 8:12:30here and these are going to be a string.
- 8:12:32So we need to convert it into a number.
- 8:12:35To do that around this value, we're
- 8:12:38going to use the code capital n number
- 8:12:41open bracket and close bracket.
- 8:12:45So this will convert the string here
- 8:12:48into a number. So now that we have the
- 8:12:51quantity that we selected, let's save it
- 8:12:54into the state using set quantity
- 8:12:59brackets
- 8:13:01quantity selected.
- 8:13:04So this is called a controlled input.
- 8:13:07When we have a value and an onchange
- 8:13:09property when we change the selector,
- 8:13:12it's going to set the quantity in the
- 8:13:14state and then the new state is going to
- 8:13:16be set as the value and then it updates
- 8:13:19the page. So we're basically putting
- 8:13:21react in the middle of this process so
- 8:13:23that we have access to the quantity that
- 8:13:26we selected. And that's a review of how
- 8:13:28controlled inputs work. And let's also
- 8:13:32console.log log the quantity selected to
- 8:13:38see if it works. So, we'll save
- 8:13:42and we'll go to the website
- 8:13:44and we're going to open the console with
- 8:13:46rightclick inspect and the console. And
- 8:13:51now on the website, we're going to click
- 8:13:53the quantity selector here and we'll
- 8:13:56select three.
- 8:13:58And now inside the console, it displays
- 8:14:01the quantity that we selected.
- 8:14:04However, there is a problem here. Notice
- 8:14:07on this website when we change one
- 8:14:10quantity selector, it changes all of
- 8:14:12them. So if I change this one here to
- 8:14:14five, it changes all the quantities. So
- 8:14:18let's fix this. We'll go back to the
- 8:14:20code.
- 8:14:22And if we scroll to the top,
- 8:14:25this is happening because the state here
- 8:14:29is outside of this loop. So that means
- 8:14:32the state is shared across all the
- 8:14:35products. That's why when we change one
- 8:14:38selector, it changes all of them. To
- 8:14:41solve this problem, we need to move this
- 8:14:43state inside this loop so that each
- 8:14:46product has its own quantity. So, we're
- 8:14:50going to select this code and cut.
- 8:14:54And inside this loop, we're going to
- 8:14:57paste.
- 8:15:00However, this code actually breaks the
- 8:15:03rules of React hooks. We can only put
- 8:15:06hooks at the top level of a component.
- 8:15:09We should not put hooks inside a loop
- 8:15:12like this. To fix this problem, we can
- 8:15:15separate each product into its own
- 8:15:18component.
- 8:15:19So, inside the home folder, we're going
- 8:15:22to create a new file and we'll name it
- 8:15:25product.jsx.
- 8:15:29And we'll create a component for each
- 8:15:31product, which is export function
- 8:15:35product brackets and curly brackets. And
- 8:15:39we're going to return the HTML for each
- 8:15:43product. Inside the products grid, we're
- 8:15:47going to collapse this code. And this is
- 8:15:50the code for each product. So, we'll
- 8:15:53select it and copy.
- 8:15:56And inside the product component, we're
- 8:15:59going to paste
- 8:16:02and then control or command shiftp
- 8:16:04format document.
- 8:16:07We'll scroll up
- 8:16:10and we'll go back to the products grid.
- 8:16:13And we also need to copy over the state.
- 8:16:16So we're going to select this copy and
- 8:16:20inside product at the top we're going to
- 8:16:24paste.
- 8:16:26And we'll fix these errors. So first we
- 8:16:29need to import use state at the top. So
- 8:16:33import curly brackets use state from
- 8:16:36react
- 8:16:38and the product we'll get it from the
- 8:16:40props using curly brackets product.
- 8:16:44Scroll down
- 8:16:46we'll need the format money function as
- 8:16:49well as axios and load cart. So first
- 8:16:54we'll import curly brackets format money
- 8:16:59from a string and we'll find dot dot
- 8:17:03slash do/youutils/money.
- 8:17:07At the top we're going to import axios
- 8:17:12from axios
- 8:17:14and we'll get the load cart function
- 8:17:16from the props. So, comma load cart and
- 8:17:20save.
- 8:17:22So, now each product has a separate
- 8:17:25state. It has its own quantity. And this
- 8:17:28is no longer breaking the rules of hooks
- 8:17:31because this state is at the top level
- 8:17:33of this component.
- 8:17:35We'll go back to the products grid.
- 8:17:38And here we can remove this HTML and
- 8:17:42just use our component less than product
- 8:17:46/greater than and we need to give it the
- 8:17:49product
- 8:17:51equals product and also the load cart
- 8:17:54function equals load cart
- 8:17:58and we can remove this state and
- 8:18:01remember when we loop through an array
- 8:18:04we need to give it a key prop. So at the
- 8:18:07front here we'll set a key equals curly
- 8:18:09brackets and we'll set it to the product
- 8:18:13id.
- 8:18:15And finally we need to import this
- 8:18:17component. So we'll scroll up and at the
- 8:18:20top we'll import curly brackets the
- 8:18:24product component from a string and it's
- 8:18:28right beside this file. So, we'll do dot
- 8:18:30slash product and we can remove these
- 8:18:34because they're no longer being used and
- 8:18:37save.
- 8:18:38And since we have the key in this loop
- 8:18:41here, we can also go into the product
- 8:18:44and remove the key in here and save.
- 8:18:50Now, let's open the website.
- 8:18:54And if we change one of these selectors,
- 8:18:57notice that each product can now have
- 8:18:59its own quantity.
- 8:19:02Now that we have this quantity, when we
- 8:19:04add a product to the cart, let's use
- 8:19:07this quantity. We'll go to our code
- 8:19:11and we're going to go inside each
- 8:19:13product and we're going to scroll down
- 8:19:18to the add to cart button here. And
- 8:19:21instead of adding a quantity of one
- 8:19:23every time, we're going to add the
- 8:19:26quantity that is saved in the state
- 8:19:28above. So now let's save
- 8:19:32and we'll try out this code. We'll open
- 8:19:35the website and let's go to this plate
- 8:19:39product and we're going to select a
- 8:19:41quantity of three this time and we'll
- 8:19:44click add to cart.
- 8:19:47And notice that the cart updated and it
- 8:19:50now has a quantity of eight, which is
- 8:19:52three more than before. If we open the
- 8:19:55cart
- 8:19:56and we scroll to the bottom, we now have
- 8:20:00three of these plates in the cart. So
- 8:20:03now the quantity selector is working.
- 8:20:06Let's go back to the homepage.
- 8:20:09And this time, let's try adding a
- 8:20:11product that already exists in the cart.
- 8:20:14So, for the plate product, we're going
- 8:20:16to select the quantity of three again,
- 8:20:20and we're going to click add to cart.
- 8:20:23Notice the cart quantity increased by
- 8:20:25three. And if we open the cart and
- 8:20:29scroll down, instead of adding a new
- 8:20:32plate product, it just increased the
- 8:20:34quantity by three for a total of six. So
- 8:20:38if the product already exists in the
- 8:20:40cart, the backend handles this situation
- 8:20:42for us and just increases the quantity.
- 8:20:46Remember the purpose of the backend is
- 8:20:48to manage the data. By letting the
- 8:20:51backend manage and update the data, we
- 8:20:53can simplify our React code.
- 8:20:57Let's go back to our code
- 8:20:59and we're going to do some cleanup in
- 8:21:01this component.
- 8:21:03First, instead of doing quantity colon
- 8:21:05quantity, we can use the shorthand
- 8:21:08property syntax and just use the
- 8:21:11quantity. It does the same thing as
- 8:21:13before.
- 8:21:14Next, instead of putting our event
- 8:21:16handlers directly in the HTML like this,
- 8:21:20we usually save this in a separate
- 8:21:22function. So, let's select all this code
- 8:21:26and copy.
- 8:21:28And we're going to scroll up.
- 8:21:32And at the top we're going to create a
- 8:21:34function const add to cart equals and
- 8:21:40we'll paste that code.
- 8:21:43So we usually save the function
- 8:21:45separately like this. And then we can
- 8:21:47scroll down.
- 8:21:49We can remove this code and just use the
- 8:21:52function name add to cart. And this
- 8:21:56makes the code easier to read. Next,
- 8:21:59we'll scroll up to the quantity
- 8:22:02selector. We can remove the console.log
- 8:22:07and we'll move this into a separate
- 8:22:09function as well. We'll select this code
- 8:22:12and copy
- 8:22:14and we'll scroll up
- 8:22:17and here we're going to create another
- 8:22:20function const select quantity and make
- 8:22:24it equal and we'll paste that code and
- 8:22:28we'll use this function below. So down
- 8:22:31here,
- 8:22:33instead of putting this function
- 8:22:34directly in the HTML, we use the
- 8:22:37function name select quantity and save.
- 8:22:43And that's it. We created the add to
- 8:22:46cart feature. In the final project,
- 8:22:48there's a small message that pops up
- 8:22:50when we click add to cart, but I'll
- 8:22:52leave this as an exercise. Now, let's
- 8:22:55save this into Git. We'll open the git
- 8:22:58section and let's use the message create
- 8:23:02add to cart feature and commit.
- 8:23:11Before we continue, I want to go back to
- 8:23:13the code that we wrote. So, we're going
- 8:23:15to scroll up in this component and I
- 8:23:18want to explain axios.post
- 8:23:21a little more. As a review, when we send
- 8:23:24a request to the backend, we can set a
- 8:23:27URL path. Based on the URL path, the
- 8:23:31backend can do different things. For
- 8:23:34example, if we use / API/roucts,
- 8:23:38the backend gives us the products. If we
- 8:23:40use / API/cart items, the backend gives
- 8:23:44us the cart.
- 8:23:46In addition to the URL path, every
- 8:23:49request also has a type. In the previous
- 8:23:53lesson when we did axios.get/api/cart
- 8:23:57items, this actually sends the type get
- 8:24:01and the URL path/ API/cart items to the
- 8:24:05back end. So it sends two pieces of
- 8:24:08information to the back end. The type
- 8:24:11and the URL path.
- 8:24:14When we did axios.post/appi/cart
- 8:24:17post/api/cart
- 8:24:19items. This sends the type post and the
- 8:24:23URL path /api/cart
- 8:24:26items to the back end.
- 8:24:28Now when the backend receives our
- 8:24:31request, it looks at both the type and
- 8:24:35the URL path to decide what to do.
- 8:24:39For example, if the backend receives get
- 8:24:43and/api/cart
- 8:24:45items, it will get the cart items and
- 8:24:48return it. If the backend receives post
- 8:24:52and/appi/cart
- 8:24:54items, it will create a new cart item.
- 8:24:58So both the type and the URL path
- 8:25:01together determine what the backend
- 8:25:04does.
- 8:25:06In the description, I have a
- 8:25:08documentation page for my backend,
- 8:25:12and it lists all the different requests
- 8:25:14that my backend supports. Notice that
- 8:25:17each request has a type and a URL path.
- 8:25:22By the way, the type is also called a
- 8:25:24HTTP method.
- 8:25:27When working with a backend, there are
- 8:25:30four common types of requests that we
- 8:25:32use. Get, post, put, and delete. Get
- 8:25:38means we want to get some data. Post
- 8:25:41means we want to create some data. Put
- 8:25:44means we want to update some data and
- 8:25:47delete means we want to delete some
- 8:25:50data.
- 8:25:51So, Axios has a method for each type.
- 8:25:55Axios.get Get sets the type of request
- 8:25:58to get. Axios.post
- 8:26:01sets the type to post. Axios.put sets
- 8:26:05the type to put. And axios.delete
- 8:26:08sets the type to delete. And then
- 8:26:11between the brackets, we put the URL
- 8:26:13path.
- 8:26:16So in our code, this sends a request to
- 8:26:19the back end with the type post and the
- 8:26:22URL path/ API/cart items. So the most
- 8:26:27important thing to remember here is that
- 8:26:29each request has a type and a URL path
- 8:26:33and both of these determine what the
- 8:26:35backend will do.
- 8:26:38In the rest of this lesson, we'll get
- 8:26:40more practice sending different types of
- 8:26:42requests to the backend.
- 8:26:45Next, let's go back to the website.
- 8:26:48And inside the checkout page for each
- 8:26:51product, we have these delivery options.
- 8:26:54So, we're going to add the ability to
- 8:26:57update the delivery option. Because
- 8:27:00we're updating something in the cart,
- 8:27:02we're going to practice using a put
- 8:27:04request.
- 8:27:06First, let's find the code for these
- 8:27:08delivery options. We'll go back to our
- 8:27:10code
- 8:27:12and open our files.
- 8:27:15and we're going to find the pages and
- 8:27:18checkout folder and we're going to open
- 8:27:20delivery options.jsx
- 8:27:25and we'll scroll down
- 8:27:27and here each of these represents one
- 8:27:30delivery option on the page. So we're
- 8:27:33going to put an on click property on
- 8:27:35this element. Now, usually we put the on
- 8:27:38click on the input, but in this case,
- 8:27:41this will make only the radial selector
- 8:27:43clickable. Instead, we want the entire
- 8:27:46delivery option, including the text, to
- 8:27:49be clickable. So, that's why we're going
- 8:27:51to put the on click on the outside
- 8:27:53component.
- 8:27:55So, we're going to add the on click
- 8:27:57attribute equals curly brackets. And
- 8:28:01let's create a function for this. Above
- 8:28:04this, we'll create a function with const
- 8:28:08and let's name it update delivery option
- 8:28:12equals. And let's make it an async
- 8:28:15function brackets arrow and curly
- 8:28:18brackets because we know we're going to
- 8:28:20make a request to the back end to update
- 8:28:22the delivery option.
- 8:28:26Also, we create the function here inside
- 8:28:29of this loop because we need access to
- 8:28:31each delivery option.
- 8:28:34So next to update the delivery option
- 8:28:37for a product, we're going to make a
- 8:28:39request to the back end. We're going to
- 8:28:42scroll to the top and at the top we'll
- 8:28:45import axials from Axios
- 8:28:51and we'll scroll back down
- 8:28:54and inside here according to my
- 8:28:56documentation my backend supports the
- 8:28:59request put/ API/cart items. This lets
- 8:29:04us update the cart. For example, we can
- 8:29:07update the delivery option for a
- 8:29:09product.
- 8:29:11So, we're going to do axios.put
- 8:29:15brackets.
- 8:29:17This sends a request with the type put
- 8:29:19to the back end. And we'll set the URL
- 8:29:22path to / API/cart-ite
- 8:29:26items. And we'll add another slash. And
- 8:29:30we're going to insert the product ID
- 8:29:33that we want to update here at the end
- 8:29:35of the URL path. So in my documentation,
- 8:29:39colon product ID is known as a URL
- 8:29:42parameter. This means my backend wants
- 8:29:44the product ID we want to update inside
- 8:29:47the URL. This is just a naming
- 8:29:50convention that is popular with
- 8:29:52backends. When we update something, we
- 8:29:54usually put the ID of what we want to
- 8:29:57update inside the URL. So here first
- 8:30:01we're going to change this to a template
- 8:30:02string using back ticks
- 8:30:06and then at the end we'll insert dollar
- 8:30:08curly brackets and we're going to insert
- 8:30:10the product ID that we want to update.
- 8:30:14So the product ID is saved inside the
- 8:30:16cart item. So inside here we're going to
- 8:30:20insert the cart item
- 8:30:24product ID.
- 8:30:26Next, we need to tell the backend which
- 8:30:29delivery option we want to update to. To
- 8:30:32do that, we're going to give the backend
- 8:30:34some data. At the end of this, we'll add
- 8:30:37a comma and an object.
- 8:30:41And in my documentation, this section
- 8:30:44tells us what data we can send to the
- 8:30:46back end. Here, we can send the delivery
- 8:30:50option ID that we want to update to.
- 8:30:54So, we're going to give the back end the
- 8:30:57delivery option ID that we want to
- 8:31:01update to. So, if we scroll up here, we
- 8:31:05want to update to this delivery option.
- 8:31:09So, we'll tell the backend to update to
- 8:31:12the delivery option ID.
- 8:31:17And after we update the delivery option,
- 8:31:20we're going to reload the cart and
- 8:31:22update the page.
- 8:31:24So in front of this, let's wait for this
- 8:31:27to finish using await.
- 8:31:30And remember, we created a function for
- 8:31:32reloading the cart called load cart. So
- 8:31:36inside app.jsx,
- 8:31:40we're going to share load cart with the
- 8:31:43checkout page. So down here we'll add a
- 8:31:46prop load cart equals curly brackets
- 8:31:51load cart and save.
- 8:31:55And then inside the checkout page
- 8:31:59at the top we're going to get load cart.
- 8:32:04And now we need to share this with the
- 8:32:06delivery options. Now the delivery
- 8:32:08options are actually inside the order
- 8:32:10summary. So we're going to share it with
- 8:32:12the order summary first.
- 8:32:14We'll scroll down to the order summary
- 8:32:17and here we're going to give it the load
- 8:32:20cart equals curly brackets load cart and
- 8:32:25save
- 8:32:27inside the order summary
- 8:32:30at the top we're going to get from the
- 8:32:33props load cart and then we're going to
- 8:32:36share it with the delivery options. So,
- 8:32:38we'll scroll down to these delivery
- 8:32:41options and we'll give it the prop load
- 8:32:45cart equals curly brackets load cart and
- 8:32:49save.
- 8:32:51And finally, in the delivery options, we
- 8:32:54can get load cart from the props. So,
- 8:32:57comma load cart and scroll down.
- 8:33:02After we update the delivery option,
- 8:33:05we're going to await load cart and
- 8:33:09brackets so that we reload the cart and
- 8:33:12update the page and we don't have to
- 8:33:15refresh to see the updates. Finally,
- 8:33:18we're going to take this function and
- 8:33:20put it in this on click. So, update
- 8:33:23delivery option
- 8:33:26and save.
- 8:33:29Now let's go to the website
- 8:33:31and we'll try changing a delivery
- 8:33:33option.
- 8:33:35And now you can see that it updates when
- 8:33:37we change the delivery option.
- 8:33:40Now one last thing here is if we open
- 8:33:43the console,
- 8:33:45we're going to get this warning message
- 8:33:47saying we provided a check property
- 8:33:49without an onchange handler. So let's
- 8:33:52fix this warning. We're going to go back
- 8:33:55to the code
- 8:33:57and the warning means that for an input
- 8:34:00element, if we have a checked prop, we
- 8:34:03should also have an onchange prop. On
- 8:34:07change lets us change the value of this
- 8:34:09input.
- 8:34:11However, in our situation, we already
- 8:34:14use this on click up here to change the
- 8:34:17value of the input. So, we don't really
- 8:34:19need an onchange property. So in here to
- 8:34:23get rid of that warning, we can just add
- 8:34:25an on change property equals curly
- 8:34:28brackets and give it an empty function.
- 8:34:31And this will just make the warning go
- 8:34:33away. We'll save
- 8:34:37and go back to our website.
- 8:34:39And let's refresh.
- 8:34:42And now if we change the delivery option
- 8:34:45and check the console, we no longer get
- 8:34:48that warning.
- 8:34:51Next, we have another problem on this
- 8:34:53page, which is when we update the
- 8:34:56delivery option. The payment summary
- 8:34:59here is not updating to show the new
- 8:35:02delivery price.
- 8:35:04To fix this, after we update a delivery
- 8:35:06option, we also want to reload the
- 8:35:09payment summary. So, let's find the code
- 8:35:12for reloading the payment summary. We'll
- 8:35:15go to our code
- 8:35:18and we're going to open the checkout
- 8:35:20page
- 8:35:22and we're going to scroll up
- 8:35:25and inside this use effect we have this
- 8:35:29code here that reloads the payment
- 8:35:32summary.
- 8:35:33So we want to rerun this code after
- 8:35:36updating the delivery option. Now we
- 8:35:39could separate this into a function and
- 8:35:41then share it like we did with load
- 8:35:44cart. However, we have an easier option
- 8:35:47here. Remember that in use effect at the
- 8:35:51end we have this dependency array. So
- 8:35:55this array determines when use effect
- 8:35:57runs. Right now it's empty which means
- 8:36:00use effect will only run once. However,
- 8:36:04we can put values inside this array. And
- 8:36:08whenever a value changes, it will rerun
- 8:36:11the use effect. In our situation, we can
- 8:36:15put the value cart inside this
- 8:36:18dependency array.
- 8:36:20And now whenever the cart changes, it
- 8:36:24will rerun the use effect and update the
- 8:36:27payment summary. And that makes sense.
- 8:36:30Every time the cart changes, we do want
- 8:36:32to update the payment summary in case
- 8:36:34the cost of the order changes.
- 8:36:37So in React there are often multiple
- 8:36:40solutions to do something and we should
- 8:36:42try to pick the cleanest solution.
- 8:36:45Also note that this will cause the
- 8:36:47delivery options to be reloaded which is
- 8:36:50not really necessary. So we could
- 8:36:53separate reloading the payment summary
- 8:36:55into another use effect but I'll leave
- 8:36:57this as an exercise.
- 8:37:00So now let's save
- 8:37:03and we'll go to our website.
- 8:37:06And now when we update the delivery
- 8:37:08option, notice that the payment summary
- 8:37:10also updates to show the new delivery
- 8:37:13price. And now this feature is working.
- 8:37:17So now let's save this into git. We'll
- 8:37:20go back to our code and open the git
- 8:37:23section.
- 8:37:25And here we'll use a message create
- 8:37:28update delivery option feature and
- 8:37:33commit.
- 8:37:39Next, we're going to go to our website
- 8:37:42and we're going to work on deleting an
- 8:37:44item from the cart. So, we're going to
- 8:37:47practice making a delete request to the
- 8:37:49back end. First, we're going to find the
- 8:37:52code for this delete link. We'll go to
- 8:37:55our code
- 8:37:57and in our files we're going to find the
- 8:38:00order summary component
- 8:38:03and we're going to scroll down in this
- 8:38:05component and find the delete link. If
- 8:38:09you did the exercises, this might be
- 8:38:11inside a cart item details component.
- 8:38:15Now, for this delete link, we're going
- 8:38:17to add an on click attribute equals
- 8:38:20curly brackets. And we'll give this a
- 8:38:23function to run. Let's call it delete
- 8:38:27cart item. And let's create this
- 8:38:30function above. So, I'm going to scroll
- 8:38:32up
- 8:38:34and I'm going to create the function
- 8:38:36inside this loop because I need access
- 8:38:38to this cart item to delete. So in here
- 8:38:42we'll create the function const delete
- 8:38:46cart item equals a function.
- 8:38:51And let's make this an async function
- 8:38:54because we know we're going to make a
- 8:38:56request to the back end. And first let's
- 8:38:59import axios. So we'll scroll up and at
- 8:39:02the top we'll import axios from a string
- 8:39:07axios.
- 8:39:09And then inside this function,
- 8:39:12my backend can receive a request called
- 8:39:15delete/appi/cart
- 8:39:17items. And this will delete a product
- 8:39:20from the cart. So we'll do axials
- 8:39:23dodelete
- 8:39:25and brackets.
- 8:39:27This sends a request with the type
- 8:39:29delete to the back end. For the URL
- 8:39:32path, we're going to use a template
- 8:39:34string and we'll do / API/cart-
- 8:39:39items slash and we're going to insert
- 8:39:42the product ID in the URL. Again, this
- 8:39:46is just a naming convention that my
- 8:39:47backend uses. So, we're going to insert
- 8:39:51the ID of what we want to delete. In
- 8:39:54this case, we need to give the backend
- 8:39:56the product ID we want to delete. So the
- 8:39:59product ID is saved inside the cart
- 8:40:02item. So we're going to give the backend
- 8:40:04the cart item dot product ID. And that's
- 8:40:10it. When my backend receives a request
- 8:40:13with the type delete and this URL path,
- 8:40:17it's going to delete this product from
- 8:40:18the cart. So after deleting the product,
- 8:40:22let's reload the cart. First, at the
- 8:40:25front of this, we'll type await to wait
- 8:40:28for this code to finish. And then we're
- 8:40:31going to await load the cart and
- 8:40:35brackets.
- 8:40:38So, I have load cart already in the
- 8:40:40order summary. If you did the exercises
- 8:40:43from the previous lesson, you may need
- 8:40:45to pass this function into your
- 8:40:47component using a prop.
- 8:40:50So, now let's scroll down. And that's
- 8:40:52all we need to delete a product from the
- 8:40:55cart and then reload and update the
- 8:40:57page. Let's save.
- 8:41:01We'll go to the website
- 8:41:03and we're going to delete this first
- 8:41:05product.
- 8:41:07And notice it updates the cart and it
- 8:41:09also updates the payment summary. If we
- 8:41:12delete another product, you'll notice it
- 8:41:15updates the cart and updates the payment
- 8:41:17summary. So that's how we make the
- 8:41:19delete link work. I'll leave updating
- 8:41:23the quantity in the cart as an exercise.
- 8:41:26So now let's save this into git. We'll
- 8:41:29open the git section and then here we'll
- 8:41:32use the message create delete cart item
- 8:41:38feature and commit.
- 8:41:45Let's go back to the website.
- 8:41:49And in the checkout page, we're going to
- 8:41:51add the ability to create an order. So,
- 8:41:55first let's find the code for this place
- 8:41:57order button. We'll go back to our code
- 8:42:01and in our files,
- 8:42:04we're going to open the payment summary
- 8:42:06component and we're going to scroll down
- 8:42:09to the bottom to this place order
- 8:42:12button. And to this button, we're going
- 8:42:15to add a prop on click equals curly
- 8:42:19brackets. And we'll give it a function.
- 8:42:22Let's call it create order.
- 8:42:27And now let's scroll up and create this
- 8:42:29function. So up here
- 8:42:34we're going to create const and we'll
- 8:42:37name it create order equals an async
- 8:42:41function brackets arrow and curly
- 8:42:44brackets.
- 8:42:46Again we're going to make a request to
- 8:42:48the back end to create the order. So at
- 8:42:51the top we're going to import axios from
- 8:42:55a string axios
- 8:42:59and in here for my back end we can send
- 8:43:02a request with the type post and the URL
- 8:43:06path /api/orders
- 8:43:08to create an order. So we'll use the
- 8:43:11code axios.post post
- 8:43:15to make a request with the type post and
- 8:43:18then give it the URL path slash API/
- 8:43:23orders.
- 8:43:24And that's all we need. Remember the
- 8:43:27cart is already saved in the back end.
- 8:43:29So the back end can take that cart and
- 8:43:31create an order for us.
- 8:43:34So let's wait for this code to finish
- 8:43:37using a wait.
- 8:43:40And after we create an order, the back
- 8:43:43end will remove everything from our
- 8:43:45cart. So we also need to reload our cart
- 8:43:48here. First we need to get the load cart
- 8:43:51function and that can be found in the
- 8:43:54checkout page.
- 8:43:57So here we get load cart from the props
- 8:43:59and we're going to share this with the
- 8:44:02payment summary. So, we'll scroll down
- 8:44:04and in the payment summary, we'll share
- 8:44:07load cart using a prop load cart equals
- 8:44:11curly brackets load cart and save.
- 8:44:17And then inside the payment summary
- 8:44:20at the top in the props, we'll get comma
- 8:44:24load cart.
- 8:44:27And now after we create an order, we're
- 8:44:30going to await load cart and brackets to
- 8:44:35reload the cart and update the page.
- 8:44:40Finally, after we create an order, let's
- 8:44:42also redirect to the orders page so we
- 8:44:45can see our new order. To do that, React
- 8:44:49Router provides a hook that lets us
- 8:44:51navigate to another page or go to
- 8:44:54another page. At the top, we're going to
- 8:44:58import curly brackets use navigate
- 8:45:03from React-Router.
- 8:45:07And at the top of our component, we're
- 8:45:11going to use const
- 8:45:14equals use navigate and brackets.
- 8:45:19So use navigate gives us a function and
- 8:45:23this function lets us navigate to or go
- 8:45:26to another page in our app. So after we
- 8:45:30create an order and we reload the cart,
- 8:45:33let's navigate to the orders page using
- 8:45:37navigate brackets and a string. And
- 8:45:41we're going to navigate to slash orders.
- 8:45:46Now, navigate is not asynchronous, so it
- 8:45:50finishes immediately and we don't need
- 8:45:52to await this code. Let's save.
- 8:45:57And we'll go to the website and test out
- 8:46:00the place order button.
- 8:46:03So, we could update the delivery option
- 8:46:05to get a shipping cost. And then click
- 8:46:08place order.
- 8:46:10And now you can see that it created a
- 8:46:13new order.
- 8:46:15You'll also see that it updated the
- 8:46:17cart. So, the quantity is now zero. And
- 8:46:20it navigated us to the orders page.
- 8:46:24Once we are on the orders page, there's
- 8:46:26some code at the top of the orders page
- 8:46:29that loads the orders from the back end
- 8:46:31and updates the page. And that's how
- 8:46:34we're able to see our newest order here.
- 8:46:39So, that's how we create an order. I'll
- 8:46:42leave making these add to cart buttons
- 8:46:44work as an exercise.
- 8:46:47So now we finished all the major
- 8:46:49features of our project. We can add
- 8:46:51products to our cart. We can update the
- 8:46:54cart and we can create an order. And
- 8:46:57you'll see that in the real world, we
- 8:46:59use a backend to manage and update all
- 8:47:02the data.
- 8:47:04And in our React code, we just make
- 8:47:07requests to the back end and then update
- 8:47:09the page. The advantage of letting the
- 8:47:12backend manage the data is that it will
- 8:47:15simplify our code a lot. So now let's
- 8:47:18save this into git. Go to the git
- 8:47:21section and at the top we'll type make
- 8:47:26create order button work and commit.
- 8:47:35And now we finished all the major
- 8:47:37features of our project. In the next
- 8:47:40lesson, we'll learn how to create
- 8:47:42automated tests in React to make sure
- 8:47:45our code still works whenever we make
- 8:47:47changes. And that's the end of this
- 8:47:50lesson. In this lesson, we learned data
- 8:47:52mutation, which means to update data in
- 8:47:55the back end. We learned different types
- 8:47:58of requests like get, post, put, and
- 8:48:02delete. We sent a post request to add
- 8:48:06products to the cart and to create an
- 8:48:09order. We sent a put request to update
- 8:48:12the cart. We sent a delete request to
- 8:48:15delete a product from the cart. We used
- 8:48:18the dependency array of use effect to
- 8:48:21update the payment summary whenever the
- 8:48:24cart changes. We learned the use
- 8:48:27navigate hook which lets us navigate to
- 8:48:29another page using our code.
- 8:48:33Thanks for watching so far. If you find
- 8:48:35this course valuable, you can support
- 8:48:37the channel by liking this video,
- 8:48:39leaving a comment, and subscribing.
- 8:48:42You can also check out the premium
- 8:48:44version of this course where you'll get
- 8:48:46all the lessons ads free broken down
- 8:48:48into small videos, as well as additional
- 8:48:51lessons and more projects that are not
- 8:48:53in the public React course. Currently,
- 8:48:56the premium course contains 11 lessons,
- 8:48:58and I'll be adding more lessons in the
- 8:49:00future. You can also get a certificate
- 8:49:03of completion, create comments or
- 8:49:06questions on specific topics, get
- 8:49:08progress tracking, search and course
- 8:49:10notes. You can also check out the
- 8:49:13membership where you can get this course
- 8:49:15ads free separated by each lesson, AI
- 8:49:18videos, backend tutorials, and more.
- 8:49:21Thanks again, and let's continue with
- 8:49:23the course. Here are some exercises to
- 8:49:26help you practice what we learned in
- 8:49:28this lesson.
- 8:50:15In this lesson, we're going to add
- 8:50:17automated tests to our app. Automated
- 8:50:20tests means using code to test our app.
- 8:50:24This is more efficient than manually
- 8:50:26opening the app and clicking around to
- 8:50:29test it.
- 8:50:31To create automated tests in React,
- 8:50:33we're going to use an npm package. The
- 8:50:37two most popular packages for automated
- 8:50:39testing are justest and vest.
- 8:50:43We're going to use VEST because it
- 8:50:45integrates well with Vit, but just and
- 8:50:48Vest are very similar. If you learn one,
- 8:50:51you can easily learn the other. First,
- 8:50:54we're going to install Vest to our
- 8:50:56project. So, we're going to go to our
- 8:50:58command line and open the tab that's
- 8:51:01running the front end. And we'll stop
- 8:51:04the VIT server using C.
- 8:51:07And we're going to npm install
- 8:51:11dash save-dev
- 8:51:16test.
- 8:51:17So, save dev means that vest is only for
- 8:51:21development. This package is not needed
- 8:51:23to run our app or to run our website.
- 8:51:27And we're also going to add at to give
- 8:51:31vest a specific version. So you can
- 8:51:33click this link in the description, go
- 8:51:36to lesson 9, and here you'll find the
- 8:51:39version of Vest that we'll install. So
- 8:51:42we'll copy that version and then paste
- 8:51:44it in here
- 8:51:46and press enter to install VEST.
- 8:51:53Now that we installed Vest, we're ready
- 8:51:55to create our first automated test. So
- 8:51:58there are different types of automated
- 8:52:00tests that we can create. The first type
- 8:52:03is called unit tests. A unit test means
- 8:52:07we test one unit or one piece of the
- 8:52:10code. For example, if we look at our
- 8:52:13files
- 8:52:15and we open our utils folder and open
- 8:52:18money.js, JS.
- 8:52:21If we test a function like format money,
- 8:52:24this would be considered a unit test
- 8:52:27because it's just one piece of the code.
- 8:52:30However, if we test something like the
- 8:52:33product component here, testing
- 8:52:35components like this are not unit tests
- 8:52:39because they use many different pieces
- 8:52:41of code together.
- 8:52:44So, we'll start by creating unit tests
- 8:52:46for format money.
- 8:52:49So, we're going to go to this money.js
- 8:52:52file, and beside this file, we're going
- 8:52:55to create a new file for the tests. And
- 8:52:58we'll name it the same money, but this
- 8:53:01time we'll end with test.js.
- 8:53:07So, the naming convention is to put it
- 8:53:09beside the original file and end it with
- 8:53:13test.js.
- 8:53:15Now inside this test file at the top
- 8:53:18we'll import curly brackets
- 8:53:22it
- 8:53:23from a string v test.
- 8:53:28So it lets us create a test and we'll
- 8:53:32also import
- 8:53:34the format
- 8:53:36money function from a string and import
- 8:53:40it from this file dot / money.
- 8:53:46And now to create our first test, we're
- 8:53:49going to use it and brackets and we'll
- 8:53:52give this a string.
- 8:53:55So this creates a test and the string
- 8:53:58here is the name of the test. So this
- 8:54:01describes what we are testing. So let's
- 8:54:04give it a name like formats1
- 8:54:081999
- 8:54:10cents as $119.99.
- 8:54:16If we run format money with 19.99, it
- 8:54:19should give us $119.99
- 8:54:22as the result. So this name is
- 8:54:25describing what we want to test and a
- 8:54:28naming convention is to make this read
- 8:54:30like English. For example, it formats 1
- 8:54:341999 as 19.99.
- 8:54:37And then we're going to give this a
- 8:54:39function brackets arrow and curly
- 8:54:42brackets.
- 8:54:44And in here we're going to test if
- 8:54:46format money works correctly.
- 8:54:49To do that, we're also going to import
- 8:54:53expect from vest. So expect lets us
- 8:54:58check if the result is correct. So in
- 8:55:01here we're going to run expect and
- 8:55:04brackets.
- 8:55:06And in here we put the code that we want
- 8:55:09to test. For example, let's put the code
- 8:55:12format money brackets and 1 n99.
- 8:55:19And then expect has different methods
- 8:55:21that we can use to check this result.
- 8:55:24One method is called dot to b and
- 8:55:29brackets.
- 8:55:32So in this second bracket we can put the
- 8:55:35expected result of this code. So we said
- 8:55:39that the expected result should be this
- 8:55:41string here. So we'll give it
- 8:55:44dollar19.99.
- 8:55:48So this code tests if this value is
- 8:55:51equal to this value. If they are equal,
- 8:55:55this check has passed. If they are not
- 8:55:58equal, this check has failed.
- 8:56:01And then expect is going to send this
- 8:56:04result to vest. and V test will display
- 8:56:08a summary of what passed and what
- 8:56:10failed.
- 8:56:12So now let's run this test. We're going
- 8:56:15to save this file
- 8:56:17and then in our command line, we're
- 8:56:20going to create a new tab for running
- 8:56:22the tests.
- 8:56:24And first we have to be inside the
- 8:56:26e-commerce project folder. So we'll cd
- 8:56:30ecommerce
- 8:56:32dash project
- 8:56:35and we're going to run npx
- 8:56:39vest.
- 8:56:43So v test is going to search our code
- 8:56:46for all the files that end with test.js
- 8:56:50and runs the tests inside and then it
- 8:56:53displays the result in the command line.
- 8:56:56So this tells us that our format money
- 8:56:59test here has passed.
- 8:57:03Now let's create another test for format
- 8:57:06money. We'll go back to this test file
- 8:57:09and to create another test, we're just
- 8:57:12going to use it again
- 8:57:15and a string. And remember that we had
- 8:57:19some problems with money not showing two
- 8:57:21decimals.
- 8:57:23So let's test if it displays two
- 8:57:28decimals
- 8:57:31and give it a function.
- 8:57:34And now inside here we can run expect
- 8:57:38and brackets
- 8:57:41format money brackets. And let's give it
- 8:57:45a value that might not show two decimals
- 8:57:49like 1 090.
- 8:57:53And then we'll check if this is correct
- 8:57:55using
- 8:57:572 tob and brackets and a string. And the
- 8:58:03expected result of this code should be
- 8:58:05dollar 10.90.
- 8:58:09So we're going to check if this result
- 8:58:11is equal to this result. If we save,
- 8:58:17vest is actually watching our files for
- 8:58:20changes. So when we save it will
- 8:58:22automatically rerun our tests. And now
- 8:58:25you can see we have two tests that
- 8:58:27passed.
- 8:58:30Next inside each test we can have
- 8:58:33multiple checks. So in here let's add
- 8:58:36another check using expect brackets.
- 8:58:41And let's try format money brackets with
- 8:58:46a different value like 100. So we expect
- 8:58:51this to be dollar1.00.
- 8:58:54So we'll do 2v brackets dollar 1.0
- 8:59:03and save.
- 8:59:05Now v test will rerun all of our tests.
- 8:59:08And now you can see that both tests
- 8:59:10still passed. So when we have multiple
- 8:59:13checks like this, all of the checks have
- 8:59:16to pass in order for this test to pass.
- 8:59:20If one of these checks fails, the entire
- 8:59:22test will fail and it will show up here.
- 8:59:25So as a quick example, if I change this
- 8:59:28and save,
- 8:59:30one of these checks failed. So we can
- 8:59:31see that this test has failed. So I'm
- 8:59:35going to add it back and save.
- 8:59:42Next, if we look at our file here,
- 8:59:45notice that these tests are related to
- 8:59:48the format money function. A best
- 8:59:51practice is to group these tests
- 8:59:53together. To do that, at the top, we're
- 8:59:57also going to import describe
- 9:00:01from vest. So, describe groups test
- 9:00:05together. we call a group of tests a
- 9:00:09test suite.
- 9:00:11So above these tests, we're going to use
- 9:00:14the code describe and brackets and we'll
- 9:00:18give this a string
- 9:00:21and this is the name of the test suite
- 9:00:23or the group of tests. So these tests
- 9:00:26are for the format money function and
- 9:00:31we'll give it a function brackets arrow
- 9:00:34and curly brackets
- 9:00:36and we're going to move our test inside.
- 9:00:39So we'll select it and cut and paste
- 9:00:44and save
- 9:00:47and everything still passes
- 9:00:50and we can see the test suite name over
- 9:00:53here. So test suites basically help us
- 9:00:56keep our tests more organized. If we
- 9:00:59want to test a different function inside
- 9:01:01this file, we would create another test
- 9:01:04suite and group our tests in there. So a
- 9:01:07best practice is to always put our tests
- 9:01:10inside a test suite, even if we only
- 9:01:12have one test suite. So now let's save
- 9:01:16this into git. We're going to go to the
- 9:01:19get section
- 9:01:21and let's use the message create unit
- 9:01:25test for format
- 9:01:29money and commit.
- 9:01:36Next, we'll create another type of test
- 9:01:40called an integration test. Integration
- 9:01:43test means we test multiple units or
- 9:01:46pieces of code working together.
- 9:01:50For example, if we look at our files and
- 9:01:53we open the product component,
- 9:01:57testing a component would be considered
- 9:01:59an integration test. Notice this uses
- 9:02:03multiple pieces of code like the format
- 9:02:06money function and the load cart
- 9:02:08function.
- 9:02:10So now let's create tests for this file.
- 9:02:13Beside product.js,
- 9:02:15we're going to create a new file
- 9:02:18and we'll name it product.est.jsx.
- 9:02:28So we end with JSX this time because
- 9:02:31this will let us use component syntax in
- 9:02:34this file like this.
- 9:02:36So, first let's actually copy the
- 9:02:38imports from our money test file because
- 9:02:42we're going to be using these again. So,
- 9:02:44we'll copy
- 9:02:46and in here
- 9:02:49we'll paste,
- 9:02:51but this time we're going to import the
- 9:02:55product component from the dot /rouct
- 9:03:00file.
- 9:03:04And first we're going to create a test
- 9:03:06suite. So we use describe brackets and a
- 9:03:11string. And we're going to be testing
- 9:03:13the product component.
- 9:03:16And give this a function.
- 9:03:20And inside we'll create our first
- 9:03:22integration test using it brackets and a
- 9:03:26string.
- 9:03:28And for components, the first thing we
- 9:03:30can check is if the component is
- 9:03:33displayed correctly. So for the product
- 9:03:36component, we can check if the name is
- 9:03:38correct, the image is correct and so on.
- 9:03:41So we'll name this test it
- 9:03:45displays the product details correctly.
- 9:03:52And we'll give it a function.
- 9:03:56And now inside here, how do we check if
- 9:03:59this component displayed the details
- 9:04:02correctly? So when testing a function,
- 9:04:05we run the function and check the
- 9:04:07result. When testing a component, we
- 9:04:10render the component and check the
- 9:04:13result. So render means to display the
- 9:04:16component on the page. To render the
- 9:04:19component in our test, we need to add
- 9:04:22some more npm packages. So, we're going
- 9:04:25to go to our command line and right now
- 9:04:29vest is watching all of our files for
- 9:04:31changes. So, we're going to stop vest
- 9:04:34using C or you can press Q to quit.
- 9:04:39And now we're going to click this link
- 9:04:41in the description, go to lesson 9, and
- 9:04:44we're going to install all of these npm
- 9:04:47packages that we need. So, we're going
- 9:04:50to select this and copy. And then in our
- 9:04:54command line, we're going to paste this
- 9:04:56command
- 9:04:58and press enter to install all these
- 9:05:01packages.
- 9:05:04After installing, we also need to create
- 9:05:07some config files to test our
- 9:05:09components. So, we'll go to our files
- 9:05:13and we'll scroll down and find
- 9:05:16vit.config.js
- 9:05:18and click it. And we're going to create
- 9:05:20a new file and name this
- 9:05:23vtest.config.js.
- 9:05:30And we'll click this link in the
- 9:05:31description and go to lesson 9. We're
- 9:05:35going to copy this setup code. So we're
- 9:05:38going to select this and copy.
- 9:05:42And then in here we'll paste that code
- 9:05:47and save.
- 9:05:50And we're also going to create another
- 9:05:52file beside this.
- 9:05:54So here create a new file called setup
- 9:05:58capital T tests.js.
- 9:06:04And we'll click the link in the
- 9:06:06description again and go to lesson 9.
- 9:06:09And we're going to copy this code
- 9:06:13and paste it in here
- 9:06:16and save.
- 9:06:19So that's just some setup that we need
- 9:06:21to do. If we look at this config file,
- 9:06:25you don't have to worry too much about
- 9:06:26how this works. This is just some
- 9:06:28standard setup. These two are required
- 9:06:32for component tests or integration tests
- 9:06:34to work. And this basically runs the
- 9:06:38code inside this file before all of our
- 9:06:41tests. So in this file we import just
- 9:06:45DOM and this basically adds a bunch of
- 9:06:49methods to expect that help us test
- 9:06:52components.
- 9:06:55Now that we have the setup, we're going
- 9:06:58to go back to our test
- 9:07:01and at the top we're going to import
- 9:07:05curly brackets render
- 9:07:08from the package at testing-
- 9:07:12library/react.
- 9:07:16So this renders or displays a component
- 9:07:20in a fake web page specifically for
- 9:07:23testing.
- 9:07:25Inside our test, we're going to use the
- 9:07:28render function and brackets
- 9:07:32and we'll tell it to render the product
- 9:07:34component using less than product
- 9:07:38slashgreater than.
- 9:07:41So next the product component has some
- 9:07:44props. So we need to provide these props
- 9:07:47in our test. So in here we're going to
- 9:07:50add the prop product equals curly
- 9:07:54brackets
- 9:07:55and we're going to provide a sample
- 9:07:57product to test with. To do that in our
- 9:08:01files we're going to go to the starting
- 9:08:03code folder and open the data folder and
- 9:08:07open products.js.
- 9:08:10And here we have a bunch of sample
- 9:08:12products that we can use. So, let's just
- 9:08:15select the first product and copy it.
- 9:08:20And then in our test,
- 9:08:23we're going to save the sample product
- 9:08:26in a variable const product equals and
- 9:08:31paste.
- 9:08:34And we're going to take this product and
- 9:08:37save it as the product prop.
- 9:08:41Next, we also need to provide a load
- 9:08:44cart function to this component. So in
- 9:08:47our app, load cart actually contacts the
- 9:08:51backend. However, in our tests, we
- 9:08:54should not contact a real backend
- 9:08:56because the backend might not be
- 9:08:58available during tests. In our tests
- 9:09:01might accidentally update real data. So
- 9:09:05instead, a best practice is to mock this
- 9:09:08function in our tests. Mock means to
- 9:09:12create a fake version of this function.
- 9:09:15To create a mock at the top, we're going
- 9:09:19to import vi from vest.
- 9:09:24And we'll scroll back down and we'll
- 9:09:27create const load cart equals vi.fn. fn
- 9:09:34and brackets.
- 9:09:37So, vi.fn
- 9:09:39basically creates a fake function that
- 9:09:42doesn't do anything. So, this fake
- 9:09:45function is called a mock and we're
- 9:09:48going to give this mock to the
- 9:09:50component. So, here we'll create a prop
- 9:09:53load cart equals curly bracket and our
- 9:09:57mock load cart. And this will prevent
- 9:10:02our component from accidentally
- 9:10:04contacting the back end in our tests.
- 9:10:07And that's it. This will render the
- 9:10:10product component on a fake web page. So
- 9:10:14now, how do we check if this component
- 9:10:16was displayed correctly? We can't see
- 9:10:20the fake web page, but we can check it
- 9:10:22using our code. To do that, we're going
- 9:10:25to scroll up
- 9:10:27and we'll also import the screen from
- 9:10:31testing library/react.
- 9:10:34So screen lets us check the screen or
- 9:10:38the fake web page to see if everything
- 9:10:40was rendered correctly
- 9:10:43in our test. After we render the
- 9:10:46component, we're going to use screen and
- 9:10:50the method
- 9:10:52get by text and brackets.
- 9:10:56So this searches the screen or the fake
- 9:11:00web page for an element with a specific
- 9:11:04text. So we'll give it a string
- 9:11:07and first let's check if the product
- 9:11:09name is displayed correctly. So, we'll
- 9:11:13scroll up and we're going to copy this
- 9:11:16product name.
- 9:11:19And then in here, we're going to paste.
- 9:11:23And this will search the screen or the
- 9:11:26fake web page for an element with this
- 9:11:29text inside.
- 9:11:31So, now we're going to check if this
- 9:11:32element exists. That will tell us if we
- 9:11:35displayed the correct product on the
- 9:11:37page. To do that before this code, we'll
- 9:11:41use expect and open bracket
- 9:11:46and at the end a close bracket and we'll
- 9:11:50use the method dot to be in the document
- 9:11:57and brackets.
- 9:11:59So this method is added by testing
- 9:12:02library/jestdom
- 9:12:04and this checks if this element exists
- 9:12:08on the screen or on the fake web page.
- 9:12:12So now let's save
- 9:12:14and we'll run this test using vest in
- 9:12:18the command line. We'll run npx v test.
- 9:12:27And all of our tests passed.
- 9:12:31Next, let's add some more checks to this
- 9:12:34test. So, let's also check if the price
- 9:12:37was displayed correctly. So, here we'll
- 9:12:40add another expect brackets and we'll
- 9:12:45use screen.get get by text
- 9:12:49and a string and we're going to search
- 9:12:52for the price on the screen. So for the
- 9:12:55sample product, the price should be
- 9:12:58dollar10.90.
- 9:13:01So, we'll scroll down and search for
- 9:13:04dollar 10.90 on the screen. And we
- 9:13:08expect this dot to be in the document
- 9:13:14and brackets
- 9:13:17and save
- 9:13:20and feed will rerun the test and tell us
- 9:13:23that this passed.
- 9:13:25Next, let's check if the product image
- 9:13:28is correct.
- 9:13:29So down here we'll use another expect
- 9:13:33brackets
- 9:13:35screen dot but this time we can't use
- 9:13:39get by text because we're trying to find
- 9:13:42an image. Instead we're going to add a
- 9:13:45test ID to the image element. So in our
- 9:13:49files we'll open the product component
- 9:13:53and we're going to scroll down until we
- 9:13:56find the product image here.
- 9:14:00And for this element, we're going to add
- 9:14:02an attribute data-
- 9:14:05test id equals a string product dash
- 9:14:11image. So this ID lets us find the image
- 9:14:15element in our tests. We could also use
- 9:14:18the class name to find this element, but
- 9:14:21if we change our class name, it will
- 9:14:23break the test. To avoid this, we
- 9:14:25usually use an ID specifically for
- 9:14:28testing like this. So now let's save.
- 9:14:33And in our test, we'll use screen dot
- 9:14:38get by test ID and brackets and a string
- 9:14:44and the test ID product dash image. So
- 9:14:49this will give us the image element in
- 9:14:51the product component.
- 9:14:54Now let's check if this image element
- 9:14:56source attribute is equal to the
- 9:15:00products image up here. So we'll select
- 9:15:04this image and copy
- 9:15:09and then down here
- 9:15:12dot to have attribute
- 9:15:18brackets and we'll check the source
- 9:15:21attribute if it's equal to the string
- 9:15:24and paste
- 9:15:26the image from our sample product
- 9:15:30and Save
- 9:15:33V test will rerun our tests and
- 9:15:35everything passed. So we know we're
- 9:15:37displaying the correct image.
- 9:15:40Next, let's check the ratings image and
- 9:15:42the rating count. Feel free to pause if
- 9:15:45you want to try it yourself first. So
- 9:15:48first we'll do expect
- 9:15:52open bracket the screen dot and we'll
- 9:15:56check the ratings image first.
- 9:15:59So, we'll go to the product component
- 9:16:02and we're going to scroll down until we
- 9:16:04find the image product rating stars and
- 9:16:08we'll give this a data dash test id
- 9:16:12equals
- 9:16:14product rating stars image and save.
- 9:16:21And let's just select this test ID and
- 9:16:24copy.
- 9:16:26And in our test,
- 9:16:28we'll get by test ID brackets a string
- 9:16:35and paste.
- 9:16:38And we expect this dot to have attribute
- 9:16:43brackets the source attribute equals a
- 9:16:47string.
- 9:16:48And we'll go back to the product
- 9:16:50component.
- 9:16:52And let's just copy the source attribute
- 9:16:54here. So we'll select it and copy.
- 9:16:59And inside our test we're going to
- 9:17:02paste.
- 9:17:05Now for our sample product we have a
- 9:17:08rating of
- 9:17:104.5.
- 9:17:12So that means we take 4.5 and multiply
- 9:17:15it by 10. We're going to get the value
- 9:17:1945.
- 9:17:21And this is the rating image that should
- 9:17:23be displayed on the page. So we'll save.
- 9:17:29And v test tells us everything passed.
- 9:17:32Finally, let's check the rating count.
- 9:17:36So we'll expect brackets screen dot. And
- 9:17:41this time we're looking for text. So we
- 9:17:43can just use get by text brackets. And
- 9:17:49for this sample product, the count is
- 9:17:5287.
- 9:17:54So we'll look for the string 87.
- 9:17:58And we expect this to be in the document
- 9:18:06and save.
- 9:18:09Vest will rerun and everything passed.
- 9:18:12So now we tested if the product
- 9:18:15component displayed all the details
- 9:18:17correctly. So let's save this into git.
- 9:18:21We'll open the git section
- 9:18:24and we'll use the message create
- 9:18:27integration test for product component
- 9:18:32and commit.
- 9:18:39Next we're going to test user
- 9:18:41interactions.
- 9:18:42In the product component, we have an add
- 9:18:45to cart button. We can also test
- 9:18:48clicking this add to cart button to see
- 9:18:50if it behaves correctly.
- 9:18:53So in our test file, let's create a new
- 9:18:56test using it brackets and a string. And
- 9:19:02let's name it adds a product to the cart
- 9:19:08and give it a function.
- 9:19:13So first let's render the product
- 9:19:16component again. So we'll scroll up
- 9:19:20and let's just copy all of this code. So
- 9:19:23we'll select it
- 9:19:26to here and copy.
- 9:19:29And then in our second test,
- 9:19:32we're going to paste.
- 9:19:35So this sets up the sample product and
- 9:19:38the mock and then renders the product
- 9:19:40component again. So now that we rendered
- 9:19:43this, how do we click the add to cart
- 9:19:46button in our test?
- 9:19:48Earlier we installed a package called
- 9:19:51testing library/ user event. This
- 9:19:55package lets us simulate events like
- 9:19:58clicking a button. So at the top of our
- 9:20:01test, we're going to scroll up and we'll
- 9:20:05import user event from the package at
- 9:20:11testing- library/
- 9:20:14user-event.
- 9:20:18So this lets us simulate events like a
- 9:20:20click.
- 9:20:22So now we'll scroll back down to our
- 9:20:24test
- 9:20:26and first we need to set up user event
- 9:20:30using const user equals user event dot
- 9:20:35setup and brackets.
- 9:20:39So now we can simulate events using
- 9:20:43user.click.
- 9:20:47So this simulates a click event and
- 9:20:50inside the brackets we're going to tell
- 9:20:52it which element to click. So we want it
- 9:20:55to click the add to cart button. So
- 9:20:58first we need to get the add to cart
- 9:21:00button inside our test. We'll go to our
- 9:21:04files
- 9:21:06and in the product component,
- 9:21:09let's scroll down to the add to cart
- 9:21:12button and we're going to give this a
- 9:21:15data dash test ID equals a string add to
- 9:21:22cart button.
- 9:21:25And let's select this test ID and copy
- 9:21:29and save.
- 9:21:32And then in the product test, we're
- 9:21:35going to get the button using screen dot
- 9:21:40get by test ID brackets
- 9:21:45and a string and we'll paste the test
- 9:21:48ID.
- 9:21:50So this will get the add to cart button
- 9:21:52from the screen.
- 9:21:54So let's save it in a variable const add
- 9:21:58to cart button
- 9:22:01equals
- 9:22:03and we're going to give this to
- 9:22:04user.click
- 9:22:06add to cart button.
- 9:22:11Next when we click this button it takes
- 9:22:14some time for the page to process the
- 9:22:17click. So user.click takes some time to
- 9:22:21finish. That means this is asynchronous
- 9:22:24code and it returns a promise. To wait
- 9:22:28for this to finish at the front, we're
- 9:22:31going to use await.
- 9:22:33And remember to use await we need to be
- 9:22:36inside an async function. So above in
- 9:22:40this function, we're going to make it
- 9:22:43async.
- 9:22:45And we'll scroll back down.
- 9:22:49And now once we click the add to cart
- 9:22:52button, this is going to make a request
- 9:22:54to the back end using axios.post.
- 9:22:58But remember in our tests we should not
- 9:23:01contact a real backend because we might
- 9:23:03accidentally update real data.
- 9:23:07So earlier we mocked load cart and
- 9:23:10replaced it with a fake function.
- 9:23:13Now we're also going to mock axios. So
- 9:23:17we can actually mock an entire npm
- 9:23:19package. To do that at the top,
- 9:23:25we're going to use the code vi
- 9:23:28mock brackets
- 9:23:31and the string axios.
- 9:23:34So this mocks the entire axios package.
- 9:23:38This means that when we are testing,
- 9:23:40when we import from axios, we will get a
- 9:23:43fake version of axios instead.
- 9:23:46So when we click add to cart, it will
- 9:23:49use the fake version of Axios and it
- 9:23:52will do nothing. It will not contact the
- 9:23:54back end. So now what should we test? We
- 9:23:59can still test that our code ran
- 9:24:02axios.post
- 9:24:03and we can test if we gave axios.post
- 9:24:06the correct values.
- 9:24:09To do that at the top, we're also going
- 9:24:12to import Axios from the package Axios.
- 9:24:19And because of vi.mmock,
- 9:24:22this is a fake version of Axios.
- 9:24:25So now we can scroll back down
- 9:24:30and after we click add to cart, we can
- 9:24:34expect brackets axios.post. post.
- 9:24:40If we give a mock to expect, we can
- 9:24:44check if our code ran axios.post
- 9:24:47and if we gave it the correct values
- 9:24:51using the method dot to have been called
- 9:24:57with and brackets.
- 9:25:01So we expect our code to call axios.post
- 9:25:04with /api/cart items. So in here we'll
- 9:25:09check if the first value we gave it was
- 9:25:12slappi/cart-
- 9:25:15items
- 9:25:17and we also expect our code to give
- 9:25:20axios.post an object with a product ID
- 9:25:24and a quantity.
- 9:25:26So in here we can also check if we give
- 9:25:28it an object with a product ID colon and
- 9:25:34we're going to get the product ID of our
- 9:25:37sample product. So here we'll select
- 9:25:40this ID and copy
- 9:25:45and scroll back down
- 9:25:47and create a string and paste the ID
- 9:25:52and we expect the quantity colon to be
- 9:25:56one because we did not change the
- 9:25:59quantity selector.
- 9:26:01So this basically checks that when we
- 9:26:03click add to cart does our code run
- 9:26:06axios.post post and give it these
- 9:26:09values. So even though mocks don't do
- 9:26:12anything, we can still check if we
- 9:26:15called them and we gave them the correct
- 9:26:17values.
- 9:26:19Next, we can also check if our code ran
- 9:26:22load cart. So remember that load cart is
- 9:26:26also a mock. So we can see if our code
- 9:26:29ran load cart. So down here we can also
- 9:26:34expect
- 9:26:36load cart and we don't give load cart
- 9:26:40any values. So we can just use dot2
- 9:26:45have been called and brackets.
- 9:26:49So this just checks if our code called
- 9:26:52load cart and does not check what values
- 9:26:54we gave it. So now let's save
- 9:26:59and v test will run our code and
- 9:27:02everything still passed. So that's how
- 9:27:05we test user interactions. We create
- 9:27:08some mocks and then we check if our code
- 9:27:11ran the mock and gave it the correct
- 9:27:13values. So now let's save this into git.
- 9:27:18In the get section, we'll use a message
- 9:27:21test clicking add to cart button
- 9:27:27and commit.
- 9:27:32Before we move on, we're going to do
- 9:27:34some code cleanup. Notice that in each
- 9:27:38of our tests, we have some setup code at
- 9:27:41the top where we create some sample data
- 9:27:44and mock load cart. So we can actually
- 9:27:47share this setup code between our tests.
- 9:27:51To do that, we can move these two
- 9:27:53variables outside of this it function.
- 9:27:57So we'll select this code
- 9:28:02and cut.
- 9:28:05And outside of this function, we'll
- 9:28:08paste.
- 9:28:11Now that these two variables are outside
- 9:28:14of this function because of scope these
- 9:28:17can be accessed anywhere inside this
- 9:28:20outer describe function. So that means
- 9:28:23it can be accessed in both tests. So in
- 9:28:26the second test
- 9:28:29we can just remove this setup code
- 9:28:32because now it's shared between the two
- 9:28:35tests up here.
- 9:28:38Another recommendation is to recreate
- 9:28:40shared variables before each test
- 9:28:43because if one test modifies a variable,
- 9:28:47that change is going to carry over into
- 9:28:49the next test which can cause bugs.
- 9:28:53So before each test, we're going to
- 9:28:55recreate these variables.
- 9:28:58To do that, we can use before capital E
- 9:29:03each brackets. And we'll give this a
- 9:29:06function brackets arrow and curly
- 9:29:09brackets.
- 9:29:11So before each runs some code before
- 9:29:14each test
- 9:29:16and also we need to import before each.
- 9:29:19So at the top
- 9:29:21we'll import before each from vest.
- 9:29:29So we want to recreate these shared
- 9:29:31variables before each test. So we're
- 9:29:34going to do that in here. Inside we'll
- 9:29:37use product equals
- 9:29:40and let's just select this value and
- 9:29:44cut.
- 9:29:46And in here we'll paste.
- 9:29:51And because we're modifying this
- 9:29:53variable, we need to change from const
- 9:29:55to let.
- 9:29:58And we'll also recreate load cart in
- 9:30:00before each. So here we'll do load cart
- 9:30:04equals vi.fn
- 9:30:07and brackets
- 9:30:10and above we can remove this
- 9:30:13and change this to a let.
- 9:30:17So now before each test we're going to
- 9:30:21recreate the setup code and we'll have a
- 9:30:24fresh set of variables and this will
- 9:30:26make our tests more consistent. So now
- 9:30:29let's save.
- 9:30:32V test will rerun our test and
- 9:30:35everything still passes.
- 9:30:37So before each is known as a test hook.
- 9:30:41There are other test hooks that we can
- 9:30:42use to help us do some setup or some
- 9:30:45cleanup in our tests.
- 9:30:48So now let's save this into git. In the
- 9:30:51get section, we'll use the message use
- 9:30:55before each to remove duplication
- 9:31:01and commit.
- 9:31:07So, we just learned how to create mocks
- 9:31:10that don't do anything. Now, we're going
- 9:31:13to learn how to mock the implementation.
- 9:31:16Mock the implementation means we make
- 9:31:19the mock do whatever we want. To do
- 9:31:22this, we're going to test another
- 9:31:24component. So in our file,
- 9:31:27we're going to go to the homepage
- 9:31:29component.
- 9:31:31And here we're going to create a new
- 9:31:33file
- 9:31:35and name it homepage.est.jsx.
- 9:31:44And let's just copy all the imports from
- 9:31:46the product component test because we'll
- 9:31:49use most of them. So we'll select this
- 9:31:52and copy.
- 9:31:55And in homepage.test
- 9:31:57we're going to paste.
- 9:32:00And this time we'll import homepage
- 9:32:04from dot /home page.
- 9:32:09And as always, we'll start by creating a
- 9:32:11test suite with describe.
- 9:32:15And we'll name this homepage
- 9:32:18component
- 9:32:20and a function.
- 9:32:23And we'll create a test using it
- 9:32:26brackets. And let's call it displays the
- 9:32:31products correctly.
- 9:32:35Brackets, arrow, and curly brackets.
- 9:32:39And now let's render the homepage using
- 9:32:42render bracket less than homepage
- 9:32:47slashgreater than.
- 9:32:50And the homepage requires two props cart
- 9:32:54and load cart. So for the cart equals
- 9:32:58curly brackets. We're not going to test
- 9:33:00the cart right now. So we'll just set it
- 9:33:03as an empty array.
- 9:33:05And for load cart equals curly brackets
- 9:33:10again we'll mock load cart. So above
- 9:33:13this test
- 9:33:15we'll create let node cart and we'll use
- 9:33:20before each brackets
- 9:33:24and a function brackets arrow and curly
- 9:33:27brackets
- 9:33:29and we'll set load cart equals a mock
- 9:33:33function vi.fn Fn and brackets
- 9:33:38and we're going to give this mock to the
- 9:33:41homepage component.
- 9:33:45Next, the homepage component is special.
- 9:33:49If we open the homepage,
- 9:33:52notice at the start, we load products
- 9:33:56from the back end using
- 9:33:58axios.get/appi/roucts.
- 9:34:01Again, in our test, we should not
- 9:34:03contact a real backend. So, we're going
- 9:34:06to mock Axios.
- 9:34:09So, here
- 9:34:11at the top, we'll mock Axios using
- 9:34:14vi.mmock
- 9:34:16brackets and the package axios.
- 9:34:20Now, this just makes axios.get do
- 9:34:23nothing. However, in the homepage, we
- 9:34:27actually need axios.get get to return
- 9:34:30something because we're going to return
- 9:34:32a response and the homepage is going to
- 9:34:35use this response to display the
- 9:34:38products. So here axios.get cannot just
- 9:34:42do nothing. We need to mock the
- 9:34:44implementation which means we're going
- 9:34:47to make axio.get do whatever we want.
- 9:34:51So in our test inside before each
- 9:34:56we're going to do axials.get
- 9:35:00dot mock implementation
- 9:35:05and brackets
- 9:35:07and we're going to give this a function
- 9:35:09brackets arrow and curly brackets.
- 9:35:13So this means that in our code whenever
- 9:35:16we use axios.get get it's going to run
- 9:35:19this fake function instead. So basically
- 9:35:22we can make axios.get do whatever we
- 9:35:25want. That's what mock implementation
- 9:35:28means.
- 9:35:30So remember we can give axios.get some
- 9:35:34values. Those values are going to be
- 9:35:36saved as parameters in here. So the
- 9:35:40first value we give to axios.get is the
- 9:35:43URL path. So in here our fake function
- 9:35:46will also get the URL path.
- 9:35:51Next inside our fake function let's
- 9:35:54check if the URL path is equal to the
- 9:36:00string /
- 9:36:03products.
- 9:36:06So for the URL path /api/proucts,
- 9:36:11usually the backend returns all the
- 9:36:13products in an array. But in our test,
- 9:36:17we don't really need all the products.
- 9:36:19So let's just return two products to
- 9:36:21test with. So in here, we'll just return
- 9:36:27two products. Now, whatever we return
- 9:36:30from this fake function should match
- 9:36:33what axios.get normally returns. So
- 9:36:37axios.get
- 9:36:38normally returns a response with a
- 9:36:42property data. So in our test
- 9:36:46this mock implementation should also
- 9:36:49return an object with a property data.
- 9:36:56And normally
- 9:36:58response data is an array of products.
- 9:37:02So in our test, our data property should
- 9:37:06also be an array.
- 9:37:09So basically in this fake function, we
- 9:37:12should match what axios.get normally
- 9:37:15returns. And this makes sure that the
- 9:37:17rest of the code still works with our
- 9:37:20mock.
- 9:37:21So now in our test,
- 9:37:24let's just return two products to test
- 9:37:26with. to find two sample products. We
- 9:37:30can go back to the starting code folder
- 9:37:33and the data and the products.js.
- 9:37:37And here, let's just collapse the first
- 9:37:39two products and we'll select them and
- 9:37:43copy.
- 9:37:45And inside the homepage test,
- 9:37:48we're going to paste.
- 9:37:51So now in our test, axio.get get will
- 9:37:55return these two products.
- 9:37:58Now there's one more thing we need to do
- 9:38:01which is remember that axios.get
- 9:38:04normally returns a promise that we have
- 9:38:07to await. So in our test we should also
- 9:38:11return a promise. To make this return a
- 9:38:14promise we can wrap this value in
- 9:38:17promise.resolve like this. Or an easier
- 9:38:21way to do this is to make this function
- 9:38:24async.
- 9:38:26So remember that async makes a function
- 9:38:29return a promise.
- 9:38:32Now axios.get will return exactly what
- 9:38:35we want. And that's how we mock the
- 9:38:37implementation which means we make the
- 9:38:40mock do whatever we want.
- 9:38:44Now let's run our tests to make sure
- 9:38:46that this works. We'll save
- 9:38:50and v test will rerun and we actually
- 9:38:53got an error. So if we look at this
- 9:38:57error and we scroll up
- 9:39:01to the details of the error, you'll see
- 9:39:04that we got this error here and it came
- 9:39:07from React Router. And this happens
- 9:39:10because if we look at our homepage
- 9:39:14component,
- 9:39:15remember that the homepage also contains
- 9:39:18a header component. And if we open the
- 9:39:22components folder and the header
- 9:39:24component,
- 9:39:26inside the header component, we have
- 9:39:29these link components.
- 9:39:31In order for a link components to work,
- 9:39:34they need to be inside a router. In
- 9:39:37main.jsx, JSX the entire app is inside a
- 9:39:41router. So that's why link components
- 9:39:44work normally but in our test our
- 9:39:47homepage component is not inside any
- 9:39:50router. So the link component doesn't
- 9:39:53work and it creates an error. To fix
- 9:39:56this we just need to put a router around
- 9:39:59the homepage in our test. So, we'll go
- 9:40:02back to our test
- 9:40:05and first we'll scroll to the top and
- 9:40:08we're going to import a router. So,
- 9:40:10we'll import curly brackets and we're
- 9:40:14going to import the memory router
- 9:40:19from the package react-outer.
- 9:40:23So, the memory router is specifically
- 9:40:26for testing.
- 9:40:28So we'll scroll down
- 9:40:31and when we render the homepage, we're
- 9:40:34going to put it inside a memory router.
- 9:40:37So less than memory router
- 9:40:42and we'll select the homepage and cut.
- 9:40:46And in here we'll paste
- 9:40:49and we'll fix the indenting.
- 9:40:54And now if we save
- 9:40:57the link components are inside a router.
- 9:41:00And now our tests should pass.
- 9:41:04Now let's check if the homepage
- 9:41:06displayed the products correctly. So
- 9:41:09after we render this homepage because we
- 9:41:13mocked Axios to return these two sample
- 9:41:16products, there should be two products
- 9:41:19on the page. Let's add a test ID to
- 9:41:22these elements so we can get them in our
- 9:41:25test. In the product component,
- 9:41:29we're going to scroll up to the top of
- 9:41:33the HTML and we're going to find the
- 9:41:36product container
- 9:41:38and we're going to add the attribute
- 9:41:40data- test id equals a string the
- 9:41:45product dash container
- 9:41:48and save.
- 9:41:51And in our test,
- 9:41:54we're going to look for the test ID
- 9:41:57using screen.get
- 9:42:01all by test ID and brackets and the test
- 9:42:06ID product dash container.
- 9:42:10So we use all here because we're going
- 9:42:12to have multiple products on the page.
- 9:42:15So we're going to get multiple product
- 9:42:17container elements.
- 9:42:19However, there's actually a problem
- 9:42:21here.
- 9:42:23When the homepage is first created, the
- 9:42:26products are empty. So, there will not
- 9:42:29be any products on the page. And we need
- 9:42:32to wait for the homepage to run use
- 9:42:34effect and load the products and then
- 9:42:37update the state. So to wait for
- 9:42:40homepage to finish loading in our test,
- 9:42:45we're going to use a different method
- 9:42:47called
- 9:42:49find all by test ID.
- 9:42:52So find does the same thing as get, but
- 9:42:56it will wait until it finds this
- 9:42:58element.
- 9:43:00So this is useful when our component
- 9:43:02needs to load something and we need to
- 9:43:05wait for it to load. Now, because this
- 9:43:08code is waiting until it finds these
- 9:43:10elements, this code is asynchronous and
- 9:43:14it returns a promise. So, we're going to
- 9:43:16wait for this code to finish using a
- 9:43:19wait. And again, to use a wait, we need
- 9:43:23to be inside an async function.
- 9:43:27So now this will wait until it finds
- 9:43:30these product containers. And then let's
- 9:43:33save these in a variable using const
- 9:43:37product containers equals this result.
- 9:43:43And now we can check this result to see
- 9:43:46if it was displayed correctly. So down
- 9:43:49here
- 9:43:50let's check if we have two product
- 9:43:53containers on the page. So we can expect
- 9:43:57brackets the product containers.length
- 9:44:04dot 2b
- 9:44:062.
- 9:44:08So this gives us an array of elements
- 9:44:11and we're just checking if the length is
- 9:44:13equal to two. If we save,
- 9:44:18v test will rerun. And now this test
- 9:44:21passed. So we know we're displaying the
- 9:44:24correct number of elements on the
- 9:44:26homepage.
- 9:44:28Next, we'll check inside each product
- 9:44:30container to see if they have the
- 9:44:33correct product name. Now, before we
- 9:44:36used screen get by text to search for
- 9:44:39some text on the page. However, this
- 9:44:43searches the entire page. Here we want
- 9:44:47to check specifically the first product
- 9:44:49container to see if it has the correct
- 9:44:52name. To do that we can use a feature
- 9:44:55called within. So we'll scroll up
- 9:45:00and here we're also going to import
- 9:45:03within
- 9:45:05from testing library/react.
- 9:45:08And we'll scroll back down.
- 9:45:12And within lets us find things within or
- 9:45:16inside a specific element. So to find
- 9:45:19things inside the first product
- 9:45:21container, we're going to use within
- 9:45:25brackets the product containers square
- 9:45:30bracket zero.
- 9:45:32This gives us the first product
- 9:45:34container and we're going to search
- 9:45:35inside this container. And then here
- 9:45:39we're going to do dot get by text and
- 9:45:43brackets.
- 9:45:45And for the first container, we're going
- 9:45:47to search for the first product name. If
- 9:45:50we scroll up,
- 9:45:52this is the first product name. So we'll
- 9:45:55select it and copy.
- 9:45:58And down here,
- 9:46:00we're going to paste.
- 9:46:03So again, this searches inside the first
- 9:46:06product container for this text. And
- 9:46:09I'll add a new line to make it a little
- 9:46:11easier to read. And now we expect this
- 9:46:14to exist. So here we'll type expect open
- 9:46:19bracket
- 9:46:21and just going to reformat this code a
- 9:46:24little bit and at the end close bracket
- 9:46:27and use
- 9:46:292B in the document and brackets.
- 9:46:35Now let's save
- 9:46:38and v test will tell us that this
- 9:46:40passed. So, we know we're displaying the
- 9:46:43correct product in the first product
- 9:46:45container. Now, let's check the second
- 9:46:48product container. We'll select this
- 9:46:50code and copy.
- 9:46:53And down here, paste.
- 9:46:57And we'll change the zero to a one. This
- 9:47:00gets us the second product container.
- 9:47:03And we're going to get the second
- 9:47:04product name. So up here, we're going to
- 9:47:08select this name and copy
- 9:47:12and scroll back down.
- 9:47:16And here we're going to paste
- 9:47:19and save
- 9:47:23and the test passed. So now we know that
- 9:47:26the second product container is
- 9:47:28displaying the correct product. Now we
- 9:47:31could check all the other details of the
- 9:47:33product as an exercise. However, we
- 9:47:36don't have to do it because remember
- 9:47:39that all the product details have
- 9:47:41already been tested in the product
- 9:47:44component test.
- 9:47:47So instead for the homepage component,
- 9:47:50we focus on testing things that are
- 9:47:52specific to the homepage such as
- 9:47:55rendering multiple products and loading
- 9:47:57products using Axios.
- 9:48:00So that's how we test a more complicated
- 9:48:02component like the homepage. We need to
- 9:48:05mock the implementation of axios.get
- 9:48:09and we may need to wrap the component
- 9:48:11inside a router. I'll leave adding more
- 9:48:15tests for the homepage as an exercise.
- 9:48:18So now let's save to git. We'll open the
- 9:48:21git section and we'll use the message
- 9:48:24create integration test for home page
- 9:48:31and commit.
- 9:48:36So now whenever we make changes to our
- 9:48:39code we can run these automated tests to
- 9:48:42make sure that format money the product
- 9:48:45component and the homepage are still
- 9:48:47correct. Now, usually we create tests
- 9:48:50for all of our components, but I'll
- 9:48:53leave that as exercises.
- 9:48:56In the next lesson, we'll learn
- 9:48:57deploying, which means how to put our
- 9:49:00React app on the internet.
- 9:49:04And that's the end of this lesson. In
- 9:49:06this lesson, we learned how to create
- 9:49:08automated tests with VEST. We learned
- 9:49:11how to create unit tests, which test one
- 9:49:14piece of the code. We learned it,
- 9:49:18expect, and describe, which lets us
- 9:49:20create a test, check the results, and
- 9:49:23group the tests together into a test
- 9:49:26suite. We learned how to create
- 9:49:28integration tests, which test multiple
- 9:49:31pieces of code working together. We
- 9:49:34learned how to test a component using
- 9:49:36render and screen. We learned how to
- 9:49:39create a mock, which is a fake version
- 9:49:42of a function or a package. And we learn
- 9:49:45how to mock the implementation, which
- 9:49:47means we make a mock do whatever we
- 9:49:50want.
- 9:49:52Here are some exercises to help you
- 9:49:54practice what we learned in this lesson.
- 9:50:35In this lesson, we're going to learn
- 9:50:37deploying, which means how to put a
- 9:50:40React app on the internet. So, there are
- 9:50:43many services we can use to deploy. In
- 9:50:46this lesson, we're going to use AWS or
- 9:50:49Amazon Web Services. AWS is the biggest
- 9:50:53service for putting websites on the
- 9:50:55internet. Most big tech companies use
- 9:50:58AWS, so it's the most useful to learn
- 9:51:00for your career.
- 9:51:03So AWS stands for Amazon Web Services.
- 9:51:07It's a collection of services that help
- 9:51:10us put stuff on the internet. And AWS
- 9:51:13has a free tier where we can use many of
- 9:51:16their services for free using some
- 9:51:18credits.
- 9:51:20So, let's click this link in the
- 9:51:22description. Go to lesson 10.
- 9:51:26And here we have a link to AWS.
- 9:51:30Now, before we begin, AWS might change
- 9:51:33their website design in the future. If
- 9:51:35you have any issues, please check this
- 9:51:37AWS troubleshooting guide here.
- 9:51:42Now, let's get started with AWS.
- 9:51:45We'll click this link to open AWS.
- 9:51:49And the free tier is free for new
- 9:51:51accounts. So, we're going to create a
- 9:51:54new AWS account. If you don't see this,
- 9:51:57you can also click sign in
- 9:52:00and then scroll down and click create a
- 9:52:03new AWS account.
- 9:52:07Before we continue, because AWS is the
- 9:52:10biggest web service, it has to follow a
- 9:52:12lot of security and compliance
- 9:52:14regulations. So, even though it has a
- 9:52:16free tier, it will ask you for a credit
- 9:52:19card, billing address, and phone number
- 9:52:21when signing up. If you're unable to
- 9:52:23provide this information, please check
- 9:52:25the AWS troubleshooting guide in the
- 9:52:28description.
- 9:52:30However, I still recommend at least
- 9:52:32watching this tutorial to see how AWS
- 9:52:35works since it's the most useful for
- 9:52:37your career. So, here we'll enter our
- 9:52:41email address for logging in. So, for
- 9:52:44me, I'll use this email
- 9:52:50and then we'll go through the rest of
- 9:52:52the sign up process.
- 9:53:05After signing up, it might take a few
- 9:53:07minutes for the account to be activated.
- 9:53:10So, we just need to give it a bit of
- 9:53:11time.
- 9:53:14Now that we created a new AWS account,
- 9:53:17one thing to note is when signing into
- 9:53:20AWS,
- 9:53:22if you see an account ID and I am here,
- 9:53:26we're going to click this sign in with
- 9:53:28root email button.
- 9:53:31And then here we can sign in with the
- 9:53:33email and password we created earlier.
- 9:53:37After signing in, you can see the
- 9:53:39details of the free tier here or by
- 9:53:42clicking here.
- 9:53:53After we sign in, we're going to land on
- 9:53:55this AWS dashboard. So, as I said, AWS
- 9:53:59is a collection of services that help us
- 9:54:02put stuff on the internet. So, here we
- 9:54:06can see all the services that are part
- 9:54:08of AWS. And there's a lot of services.
- 9:54:12At the top in the search bar, we can
- 9:54:15search for a service. So, we're going to
- 9:54:17start with the most basic service called
- 9:54:20EC2.
- 9:54:22And click here to open it.
- 9:54:27EC2 stands for Elastic Compute Cloud. It
- 9:54:31basically lets us rent a computer from
- 9:54:34AWS.
- 9:54:35Earlier in the course, I mentioned that
- 9:54:38many websites use a backend to manage
- 9:54:40the data and the backend is another
- 9:54:43computer on the internet. Now, we could
- 9:54:46buy a physical computer ourselves and
- 9:54:49use that as the back end, but then we
- 9:54:52have to keep this computer running 24
- 9:54:54hours a day and put it in our office
- 9:54:56somewhere. So this is a lot of work and
- 9:55:00what most companies do is they just rent
- 9:55:02a computer from AWS and AWS will manage
- 9:55:07the computer and keep it running 24
- 9:55:09hours a day and this saves a lot of
- 9:55:12work. So EC2 basically lets us rent a
- 9:55:16computer from AWS. This is called
- 9:55:19infrastructure as a service or IAS. This
- 9:55:23means that AWS manages the
- 9:55:25infrastructure or the computers for us.
- 9:55:30So to rent a computer in here, we can
- 9:55:32click the instances section and then
- 9:55:36click launch instance. By the way, one
- 9:55:39instance is one EC2 computer.
- 9:55:43However, we're not going to use this
- 9:55:45because there's still some problems with
- 9:55:47this process. EC2 just gives us a
- 9:55:51computer. We still need to install
- 9:55:53NodeJS on this computer. We still need
- 9:55:56to connect this computer to the
- 9:55:57internet. And we need to upload our code
- 9:56:00to this computer. So even though AWS
- 9:56:04manages the computer, there's still a
- 9:56:06lot of manual work we have to do. To
- 9:56:09solve this problem, AWS provides another
- 9:56:12service.
- 9:56:14At the top, we're going to search for
- 9:56:17elastic
- 9:56:19beantock
- 9:56:21and click here to open this service.
- 9:56:25You can think of elastic beantock as a
- 9:56:28layer on top of EC2. It uses EC2 to rent
- 9:56:32a computer, but it also installs all the
- 9:56:35software we need on this computer and it
- 9:56:38connects the computer to the internet.
- 9:56:40So, Elastic Beantock also does all the
- 9:56:43software setup for us. All we have to do
- 9:56:47is just upload our code to elastic
- 9:56:49beantock and it will just work. This is
- 9:56:53called a platform as a service or a
- 9:56:56path. This manages the infrastructure
- 9:56:59and the software for us. If you want to
- 9:57:02learn more about these concepts, you can
- 9:57:04check out my backend overview video in
- 9:57:07the description.
- 9:57:10Now that we have an overview of AWS and
- 9:57:13some of the services we can use, let's
- 9:57:15learn how to use Elastic Beantock. Now,
- 9:57:18Amazon might change the design of these
- 9:57:21pages in the future. If you run into any
- 9:57:23issues, make sure to check the AWS
- 9:57:26troubleshooting guide in the
- 9:57:27description.
- 9:57:29We're going to start by creating an
- 9:57:31application. Or we can click here, click
- 9:57:35environments,
- 9:57:37and create environment.
- 9:57:40And on this page, we're going to select
- 9:57:43a web server environment. And we're
- 9:57:46going to give a name to our application.
- 9:57:48So let's just use e-commerce-
- 9:57:52project-
- 9:57:54react.
- 9:57:56And we can use the default values for
- 9:57:58this section. And we'll scroll down. And
- 9:58:02for the platform section, we're going to
- 9:58:04choose the technology we want to use
- 9:58:06here. For us, we're going to use NodeJS.
- 9:58:12And down here, we'll just use the sample
- 9:58:15application. We can upload our own code
- 9:58:18later.
- 9:58:19And we'll scroll down. And here we'll
- 9:58:22choose single instance. If you see free
- 9:58:25tier allegible, pick that one.
- 9:58:27Otherwise, just pick the single
- 9:58:29instance.
- 9:58:31And then here we'll click next.
- 9:58:35And here we need to create a role. A
- 9:58:38role gives elastic beantock permission
- 9:58:41to use other services in AWS.
- 9:58:45So in here, if you already have a role
- 9:58:48for elastic beantock, you can use that.
- 9:58:51Otherwise, we'll click create ro.
- 9:58:57And we should be able to use the default
- 9:58:59values that they set here. And we'll
- 9:59:02click next.
- 9:59:04And then click next.
- 9:59:07And at the bottom, we'll click create
- 9:59:10roll.
- 9:59:12And then we'll go back to Elastic
- 9:59:14Beantock. And we'll click this button to
- 9:59:17refresh.
- 9:59:19And we'll select the RO that we just
- 9:59:21created.
- 9:59:23For this one, we also need to create a
- 9:59:24role. So, we'll click here.
- 9:59:28And then we can also use the default
- 9:59:30values that it gives. Click next
- 9:59:33and next.
- 9:59:35And at the bottom, create RO.
- 9:59:41And we'll go back and refresh
- 9:59:45and select the RO that we just created.
- 9:59:48And we can keep this as blank. And we'll
- 9:59:51click next.
- 9:59:54For the networking and database
- 9:59:56settings, we can just use the default
- 9:59:58values here. So, we'll scroll down and
- 10:00:01click next.
- 10:00:04And for the instance settings, we're
- 10:00:07going to scroll down and find the
- 10:00:09section called capacity.
- 10:00:11And down here, we're going to scroll
- 10:00:13down
- 10:00:18and we're going to find a section like
- 10:00:20this called instance types. And it has a
- 10:00:23drop down here. So, this lets us choose
- 10:00:26the size of the computer that we want to
- 10:00:28rent from EC2.
- 10:00:31So, in the description, we're going to
- 10:00:33click this link and go to lesson 10 and
- 10:00:37then follow the instructions here to
- 10:00:39select the correct instance types.
- 10:00:43So, now we'll scroll down and click next
- 10:00:49and we can leave monitoring and logging
- 10:00:51settings as the default. So, we'll
- 10:00:54scroll down
- 10:00:58and click next.
- 10:01:02And finally, we'll scroll to the bottom
- 10:01:05and click create.
- 10:01:10So now, Elastic Beantock is going to
- 10:01:13rent a computer for us using EC2. It
- 10:01:17will also install all the software that
- 10:01:19we need on this computer like NodeJS and
- 10:01:22npm. And it will do all the
- 10:01:25configuration to connect this computer
- 10:01:27to the internet.
- 10:01:30So, this will take a few minutes to
- 10:01:31complete. So, we need to wait for this
- 10:01:34to finish.
- 10:01:35And you might need to refresh the page
- 10:01:38to check the progress.
- 10:01:43Once that's done, it should look
- 10:01:45something like this. And we can click
- 10:01:47this URL here or click here to open this
- 10:01:52website.
- 10:01:55And now we should see a sample NodeJS
- 10:01:58application on this page. If something
- 10:02:01goes wrong and this doesn't work, please
- 10:02:03check the AWS troubleshooting guide in
- 10:02:05the description.
- 10:02:09Now that we set up Elastic Beantock,
- 10:02:11we're ready to put our code on the
- 10:02:13internet. We'll go back to Elastic
- 10:02:16Beantock.
- 10:02:18And our code has two parts, the back end
- 10:02:21and the front end. We put the back end
- 10:02:24on the internet first and then we put
- 10:02:27the front end. To put the back end on
- 10:02:30the internet, we just need to upload our
- 10:02:33backend code to elastic beantock. So
- 10:02:36we'll click upload and deploy.
- 10:02:40And here we're going to upload our
- 10:02:43backend code. However, we need to upload
- 10:02:46the code as a zip file. So we'll go back
- 10:02:49to VS Code.
- 10:02:52And I'll collapse this for now. And I
- 10:02:56actually set up the e-commerce backend
- 10:02:59so that it works with elastic beantock.
- 10:03:02So this backend has a command that will
- 10:03:06save all the code in a zip file. So
- 10:03:09we're going to go to the command line
- 10:03:12and we're going to create a new tab.
- 10:03:15And in here we're going to cd into
- 10:03:19e-commerce backend.
- 10:03:23and we'll run the command npm run zip.
- 10:03:29So this will save all the backend code
- 10:03:32into a zip file which is right here. If
- 10:03:36I open this zip file,
- 10:03:38you'll see that it just contains all of
- 10:03:40the backend code.
- 10:03:43So now we just need to upload this zip
- 10:03:46file to Elastic Beantock. We'll go back
- 10:03:49to the browser
- 10:03:51and we'll click this to choose a file.
- 10:03:55And for me, my code is on the desktop
- 10:03:59inside the React course folder inside
- 10:04:02e-commerce backend.
- 10:04:04And if I scroll down, I'll see this
- 10:04:06e-commerce backend one, which is a zip
- 10:04:09file. So, I'll click this and open.
- 10:04:14Wait for it to upload and then click
- 10:04:16deploy.
- 10:04:21So now elastic beantock will take the
- 10:04:24backend code and put it on the internet.
- 10:04:27So we'll wait for a few minutes for this
- 10:04:29to complete.
- 10:04:33Once that's done, we're going to click
- 10:04:35this URL again.
- 10:04:38And now instead of the sample
- 10:04:40application, it should say index not
- 10:04:43found, which is normal. This is how I
- 10:04:45designed my back end.
- 10:04:47Now in the URL, we're going to click up
- 10:04:50here and go to the end. We're going to
- 10:04:53enter the URL path slash API slash
- 10:04:57products and press enter.
- 10:05:01So this will give us the products data
- 10:05:04that we were using in our project on our
- 10:05:07computer. So this is the same data that
- 10:05:10we saw before except now our backend is
- 10:05:13on the internet.
- 10:05:17Now that our back end is on the
- 10:05:19internet, let's put our front end on the
- 10:05:21internet as well. For the front end, we
- 10:05:24also need to prepare our code. So, we'll
- 10:05:28go back to VS Code.
- 10:05:31And I'll collapse this folder for now.
- 10:05:34And if we look at the front-end code,
- 10:05:37notice that in here we use JSX code.
- 10:05:42However, remember that the browser does
- 10:05:44not understand JSX. It only understands
- 10:05:47normal JavaScript. So, we need to
- 10:05:50convert this code into normal JavaScript
- 10:05:54first. To do that, VIT has a command for
- 10:05:57this. Inside the command line, we'll
- 10:06:00create a new tab.
- 10:06:03And this time we'll cd into
- 10:06:06ecommerce
- 10:06:08dash project.
- 10:06:12And vit has a command called npm run
- 10:06:16build.
- 10:06:18This is a shortcut for vit build.
- 10:06:22So we'll press enter.
- 10:06:27And this basically converts all of our
- 10:06:29JSX code into normal JavaScript and
- 10:06:33saves the code in a folder called dist.
- 10:06:36So inside here we have an index.html
- 10:06:40file and this is the HTML for our React
- 10:06:43website. And inside the assets folder we
- 10:06:47have this JavaScript file. So this is
- 10:06:51all of our React code but converted into
- 10:06:54normal JavaScript. You'll also notice
- 10:06:57that Vit combined all of our code into
- 10:06:59one file and it also minified or
- 10:07:02compressed the code. So, it shortened
- 10:07:05the variable names and removed the extra
- 10:07:07spaces. This is because when we open a
- 10:07:11website, our browser needs to download
- 10:07:13all the front-end code and run it. By
- 10:07:17combining and compressing the code, it
- 10:07:19makes our website load faster. And Vit
- 10:07:22did the same thing with the CSS.
- 10:07:25You can see that it combined all of our
- 10:07:27CSS files together and compressed it. So
- 10:07:30now our front end is optimized and it's
- 10:07:33ready to be put on the internet.
- 10:07:36So now when we open a website, we need
- 10:07:39to download this React code from
- 10:07:42somewhere. So where do we put this
- 10:07:44front-end code? So we could put this in
- 10:07:47another service like GitHub pages and
- 10:07:50then when we visit the website our
- 10:07:52browser will download this front-end
- 10:07:54code from there.
- 10:07:56However, notice in our current setup
- 10:07:59that we already have this backend
- 10:08:01computer that's running in elastic
- 10:08:04beantock.
- 10:08:05So we can just reuse this backend
- 10:08:07computer. we can put our React code into
- 10:08:11the backend computer and our browser
- 10:08:13will download the React code from the
- 10:08:15backend computer. So, this is a common
- 10:08:18setup and it simplifies our setup
- 10:08:21because now we only need to set up one
- 10:08:24computer. So, to do this step, all we
- 10:08:27need to do is to take this disc folder
- 10:08:30and then drag it into the e-commerce
- 10:08:32back end like this.
- 10:08:36And I added some code in the e-commerce
- 10:08:39back end so that it works with this disc
- 10:08:41folder. So now in the command line,
- 10:08:45we'll go back to the tab that's running
- 10:08:47in the back end and we're going to
- 10:08:49create the zip file again. So we'll do
- 10:08:52npm runzip.
- 10:08:59So this will create another zip file but
- 10:09:02with our front-end code inside.
- 10:09:05And now we just need to upload this to
- 10:09:08Elastic Beantock. We'll go back to the
- 10:09:11browser
- 10:09:13and open Elastic Beantock and we'll
- 10:09:16click upload and deploy.
- 10:09:19And we'll choose a file.
- 10:09:22And this time we'll choose the second
- 10:09:25file here
- 10:09:29and click deploy.
- 10:09:35And we'll wait a few minutes for this to
- 10:09:37complete.
- 10:09:40Once that's finished, we're going to
- 10:09:42click this URL again.
- 10:09:45And now we should have our React app on
- 10:09:48the internet. So, let's try adding some
- 10:09:51stuff to the cart to make sure it works.
- 10:09:53I'll add a toaster and two plates.
- 10:09:58And now, if I look at the cart, it
- 10:10:00updated. If I open it, we have our new
- 10:10:03products in the cart and we can update
- 10:10:06them like usual.
- 10:10:08And if we click create order,
- 10:10:12it works. So everything is working the
- 10:10:15same as on our computer, but this React
- 10:10:18app is now on the internet. If we go
- 10:10:21back to the homepage,
- 10:10:23I'm going to give you a overview of how
- 10:10:26this works. So visually, this is what's
- 10:10:28happening. First we put our back end on
- 10:10:31the internet. So the back end is running
- 10:10:32on a separate computer and then we put
- 10:10:35our front end code in the backend
- 10:10:37computer and I set up some code in the
- 10:10:39back end so that when we visit the
- 10:10:41website using the URL path slash the
- 10:10:45backend sends back the react code to our
- 10:10:48computer. So now we have the react code
- 10:10:50running on our computer and it
- 10:10:53communicates with the backend running on
- 10:10:55another computer. So, this is how a
- 10:10:58realworld React app works. When we're
- 10:11:01developing, the front end and the back
- 10:11:04end run on the same computer because
- 10:11:06it's easier for development.
- 10:11:09But when we put the React app on the
- 10:11:11internet, the front-end code runs on our
- 10:11:14computer and the backend code runs on
- 10:11:16another computer.
- 10:11:20Next, we can add a shortcut to Vit to
- 10:11:23make this process a little easier. We'll
- 10:11:26go back to VS Code
- 10:11:29and we'll go to the e-commerce project
- 10:11:32folder which is here. So remember that
- 10:11:36we created a disc folder and then
- 10:11:38manually dragged it into e-commerce
- 10:11:41backend. We can configure VIT to put the
- 10:11:45disc folder in e-commerce backend
- 10:11:47directly.
- 10:11:49We're going to scroll down
- 10:11:51to vit.config.js.
- 10:11:56And in this configuration at the bottom
- 10:12:00we're going to add a property called
- 10:12:02build colon curly brackets. And in here
- 10:12:07we'll use a property out capital D dur
- 10:12:12colon and a string.
- 10:12:14So this tells vit where to put the dist
- 10:12:17folder.
- 10:12:18So we want to put it inside the
- 10:12:20e-commerce backend folder. So we'll give
- 10:12:23it dot dot slash to go out of e-commerce
- 10:12:26project and then into e-commerce
- 10:12:30dashbackend
- 10:12:32slashdist
- 10:12:34and save.
- 10:12:37So now if we go into the back end and we
- 10:12:40rightclick and delete this disc folder
- 10:12:46and then run npm run build. So in the
- 10:12:49command line we'll open the tab that's
- 10:12:52running in the e-commerce project and
- 10:12:54then run npm run build and press enter.
- 10:13:02It will create the disc folder directly
- 10:13:04in here. So directly in the e-commerce
- 10:13:07back end. So this just saves a step in
- 10:13:10our process. We can also automate this
- 10:13:13process further but I'll leave that as
- 10:13:15an exercise.
- 10:13:17So now we'll save this change into git.
- 10:13:20We'll go to the git section
- 10:13:24and we'll use the message save build
- 10:13:28result in ecommerce dashbackend
- 10:13:33and commit.
- 10:13:38Before we continue, I want to discuss
- 10:13:41how to deploy a react app without a
- 10:13:44backend.
- 10:13:46So notice that so far to put a React app
- 10:13:49on the internet, we needed a backend.
- 10:13:52But what if we don't know how to write
- 10:13:54backend code yet? What if we just know
- 10:13:57React? So if a React app does not have a
- 10:14:01backend, we can actually just put the
- 10:14:03code on the internet directly.
- 10:14:07For example, in our code here, we're
- 10:14:10going to go back to our chatbot project
- 10:14:13from earlier. And notice that this
- 10:14:16project does not use a backend. It only
- 10:14:19has React code. So to deploy this
- 10:14:23project, we're going to use npm run
- 10:14:25build again. So in our command line,
- 10:14:28we'll create a new tab
- 10:14:30and we'll cd into the chatbot-
- 10:14:34project folder.
- 10:14:37And if we need to, we can npm install
- 10:14:44and then npm run build.
- 10:14:49And this will convert all the React code
- 10:14:51into normal JavaScript and save it in a
- 10:14:54disc folder again. So notice that inside
- 10:14:58the disc folder, we actually just have
- 10:15:00normal HTML, CSS, and JavaScript code.
- 10:15:04So this is actually just an HTML website
- 10:15:08and we can actually upload this code to
- 10:15:10any service that puts an HTML website on
- 10:15:13the internet. For example, here I use a
- 10:15:17service called GitHub pages and I can
- 10:15:19just upload this code to GitHub pages
- 10:15:22and it will put it on the internet. No
- 10:15:24backend knowledge required.
- 10:15:27To learn more, you can actually follow
- 10:15:29the same steps as my video, how to put a
- 10:15:31HTML website on the internet. However,
- 10:15:35make sure you do the exercises in this
- 10:15:37lesson first since there's still some
- 10:15:39small configuration changes that we need
- 10:15:42to make for this to work. So, basically,
- 10:15:45if we just have a front end, we can just
- 10:15:48upload this code directly to the
- 10:15:50internet using something like GitHub
- 10:15:52pages. But if our project has a backend
- 10:15:55as well, then we need to use a more
- 10:15:58complicated service like AWS to put the
- 10:16:01back end and the front end on the
- 10:16:04internet.
- 10:16:06In the rest of this lesson, we're going
- 10:16:09to improve our AWS setup to make it more
- 10:16:12real world and professional.
- 10:16:15So, we'll go back to the browser
- 10:16:17and we'll open AWS again. And I'm
- 10:16:21actually going to close some of these
- 10:16:24other tabs because we don't need them.
- 10:16:27And inside Elastic Beantock in this
- 10:16:30project, we're going to go to the
- 10:16:32configuration section.
- 10:16:36And we're going to scroll down and find
- 10:16:38the configuration for instance traffic.
- 10:16:41If you don't see this, you can check the
- 10:16:43AWS troubleshooting guide in the
- 10:16:45description.
- 10:16:47So here we'll click edit.
- 10:16:51and we're going to scroll until we find
- 10:16:54the capacity section. And here we'll
- 10:16:58look for something like environment type
- 10:17:00and a drop down that says single
- 10:17:02instance. So we're going to change this
- 10:17:04to load balanced.
- 10:17:08So what this does is it adds another
- 10:17:11computer in front of our backend
- 10:17:13computer. This is called a load
- 10:17:15balancer. So what a load balancer does
- 10:17:18is if we have a really busy website, one
- 10:17:21computer might not be enough to handle
- 10:17:24all the requests. So we can actually run
- 10:17:27the back end on multiple computers and
- 10:17:29this load balancer will distribute the
- 10:17:32requests across these computers. This
- 10:17:34allows our website to handle much more
- 10:17:37users.
- 10:17:39So here we can decide how many back-end
- 10:17:43computers to run. Since we're just
- 10:17:45testing, we'll set this to a minimum of
- 10:17:47one and a maximum of one.
- 10:17:52And then we'll scroll down
- 10:17:56and click apply.
- 10:18:02And now Elastic Beantock will set up a
- 10:18:04load balancer computer in front of our
- 10:18:07back end. And this will take a few
- 10:18:09minutes to complete.
- 10:18:12After it finishes, we're going to go
- 10:18:14back to the configuration again
- 10:18:19and we'll scroll down to the instance
- 10:18:21traffic settings and just make sure that
- 10:18:24min is one and max is one. If it's not
- 10:18:27one, we can click edit
- 10:18:31and then scroll back down and then
- 10:18:34change it to one. And we'll scroll down
- 10:18:38and click apply again.
- 10:18:47So when that finishes, I'll just check
- 10:18:49it again.
- 10:18:52And now it's one and one.
- 10:18:57So now our website setup looks like
- 10:19:00this. We have three computers, the front
- 10:19:03end, the back end, and the load
- 10:19:05balancer. This is closer to a real world
- 10:19:08setup. Again, you can watch my video on
- 10:19:12backend overview to learn more about
- 10:19:14these concepts.
- 10:19:17Next, currently all the data for our
- 10:19:20React app like the products, the cart,
- 10:19:22and the orders are saved in a database
- 10:19:26and the database is saved in a file.
- 10:19:30However, when we put our React app on
- 10:19:32the internet, we usually put the
- 10:19:35database on a separate computer and this
- 10:19:38lets us share the data between backend
- 10:19:41computers.
- 10:19:43So, inside AWS, we're going to go to the
- 10:19:47configuration again
- 10:19:50and we'll scroll down to the database
- 10:19:53configuration and we'll click edit.
- 10:19:58And inside the database section, we're
- 10:20:01going to scroll down
- 10:20:04and find database and we'll click enable
- 10:20:07database.
- 10:20:10Elastic Beantock will automatically
- 10:20:12create a new computer that's running a
- 10:20:15database and it will connect this
- 10:20:17database to our back end.
- 10:20:20So, we'll scroll down and look at these
- 10:20:23options. And here we can select the
- 10:20:26database that we want to use. So the
- 10:20:28e-commerce back end works with the
- 10:20:30database MySQL. So we're going to use
- 10:20:33that.
- 10:20:35And then here we're going to look at the
- 10:20:38instance class setting. So this is the
- 10:20:41size of the computer running the
- 10:20:43database.
- 10:20:45In the description, we'll click this
- 10:20:47link, go to lesson 10, and then here
- 10:20:51we'll follow the instructions to select
- 10:20:53the correct instance class for the
- 10:20:55database.
- 10:21:03And finally, we'll set a password.
- 10:21:08And now we'll scroll down to the bottom
- 10:21:11and click apply.
- 10:21:16So AWS will set up a separate computer
- 10:21:19to run the database. And I already added
- 10:21:22some code in my e-commerce back end to
- 10:21:24connect to this database. So it should
- 10:21:27just work. If you have issues, please
- 10:21:30check the AWS troubleshooting guide.
- 10:21:35After that finishes, we can open the
- 10:21:38website again
- 10:21:40and it still works. If we add something
- 10:21:43to the cart, notice that the database
- 10:21:46still works. And if we open the orders
- 10:21:49page,
- 10:21:51remember that earlier I created a new
- 10:21:53order.
- 10:21:55And now the orders are back to the
- 10:21:57default values.
- 10:21:59So this means that we're using the new
- 10:22:01MySQL database that we set up. And now
- 10:22:05our AWS setup looks like this. We have a
- 10:22:08load balancer, a backend, and it
- 10:22:10connects to a database on another
- 10:22:13computer. And this is getting even
- 10:22:15closer to a real world setup.
- 10:22:20Next, if we go back to AWS,
- 10:22:24notice that to open our website, we use
- 10:22:27this default URL, which is really long.
- 10:22:31So, now we're going to set up a domain
- 10:22:32name to make our website easier to
- 10:22:35access. A domain name is like myite.com
- 10:22:39or youtube.com. It's a short name that
- 10:22:43makes it easy to access our website.
- 10:22:46So instead of putting this long URL in
- 10:22:48the browser, we can just type myite.com
- 10:22:52and it will open this website. So I'll
- 10:22:55show you how to set up a domain name
- 10:22:57with AWS.
- 10:22:59First, we need to purchase a domain
- 10:23:02name. Most domain names cost around $10
- 10:23:05or $20 per year, so this section is
- 10:23:08optional. If you already have a domain
- 10:23:11name that you purchased before and you
- 10:23:12want to use, feel free to skip to the
- 10:23:15next section. Otherwise, I'm going to
- 10:23:17show you how to purchase your own domain
- 10:23:20name. There are many services we can use
- 10:23:22to purchase domain names. I use a
- 10:23:25service called namecheep for all my
- 10:23:27websites like superimple.dev.
- 10:23:30In the description, we'll click this
- 10:23:32link, go to lesson 10, and click here to
- 10:23:36go to namecheep. So, this is an
- 10:23:38affiliate link. If you decide to
- 10:23:40purchase a domain name using this link,
- 10:23:43part of your purchase will help support
- 10:23:45this channel. Thank you so much.
- 10:23:50Once we're on NameCheep, we're going to
- 10:23:52search for a domain name that we want.
- 10:23:54If you see a search box here, you can
- 10:23:57search for it. Otherwise, we can go to
- 10:23:59domains and domain name search.
- 10:24:03and we'll scroll down here
- 10:24:05and search for a domain name. So, domain
- 10:24:08names are unique. Once they are taken,
- 10:24:11like youtube.com, we can't purchase it
- 10:24:14anymore. If you don't know what domain
- 10:24:17name you want, my recommendation is to
- 10:24:20try to register your first and last
- 10:24:22name. This might be useful for a
- 10:24:24personal website. So, for me, it's Simon
- 10:24:28Bao. and we need to give it an ending
- 10:24:32like
- 10:24:35do stands for commercial. It's the most
- 10:24:37popular ending for domain names. Now,
- 10:24:40you could pick another ending that you
- 10:24:42want, but be careful because some domain
- 10:24:44endings don't work with normal browsers.
- 10:24:48If you're not sure which ending you
- 10:24:49want, I recommend using com or.net just
- 10:24:52to be safe because they are the most
- 10:24:54popular.
- 10:24:56So now I'm going to click search to see
- 10:24:59if this domain has already been taken.
- 10:25:03So it looks like this one has already
- 10:25:05been taken. So I'll just add something
- 10:25:07to it like Simon Balenge or Engineer.
- 10:25:12And this one is available. So you might
- 10:25:15need to modify your domain name to find
- 10:25:17one that works for you.
- 10:25:19So, we'll add this to the cart
- 10:25:24and then open the cart.
- 10:25:29If you want to autorenew this domain
- 10:25:31name, you can click here and then we'll
- 10:25:34click confirm order.
- 10:25:37And if you don't have a name account,
- 10:25:39you need to create an account first.
- 10:25:47And after filling out all the
- 10:25:48information, we'll purchase the domain.
- 10:25:55And that's it. Now we have a domain name
- 10:25:58and we own a little piece of the
- 10:26:00internet.
- 10:26:02Once we have a domain name, we're going
- 10:26:04to set up the domain name in AWS.
- 10:26:08So, we'll go back to AWS
- 10:26:11and at the top we're going to search for
- 10:26:14the service route 53
- 10:26:18and we're going to write click this and
- 10:26:21open in a new tab.
- 10:26:26So, route 53 lets us manage our domain
- 10:26:29names in AWS. We can click get started
- 10:26:34and click create hosted zones
- 10:26:37and then click get started.
- 10:26:40If you don't see that, we can also click
- 10:26:43this menu here,
- 10:26:45click hosted zones
- 10:26:47and then click create hosted zone.
- 10:26:52A hosted zone lets us connect a domain
- 10:26:55name to our app. So currently a hosted
- 10:26:58zone costs about 1.6 6 cents per day. So
- 10:27:01you can use it for free for a few hours.
- 10:27:05So here we're going to enter the domain
- 10:27:07name that we have. So for me it's simon
- 10:27:12balenge.com
- 10:27:16and scroll down and we'll leave it as a
- 10:27:18public and then click create hosted
- 10:27:21zone.
- 10:27:24And now inside here, we're going to
- 10:27:27scroll down and click create record.
- 10:27:31So this sets where the domain name will
- 10:27:34go. We're going to leave the subdomain
- 10:27:36as blank and we'll create an A record
- 10:27:40and we're going to check alias.
- 10:27:43And here we can select which AWS service
- 10:27:47we want to connect to.
- 10:27:49So for us, we're using elastic beantock
- 10:27:55and we're going to choose the region. If
- 10:27:57we go back to the tab that's running
- 10:27:59elastic beantock, we can see the region
- 10:28:03up here. So for me, I'm using the region
- 10:28:05Ohio.
- 10:28:07So go back to route 53 and I'll search
- 10:28:10for the region Ohio and select it. And
- 10:28:14we should see our app here. So I'll
- 10:28:17select that.
- 10:28:20and then scroll down and click create
- 10:28:22records.
- 10:28:26And finally, we need to tell NameCheep
- 10:28:28or the company that you use for your
- 10:28:30domain name to redirect to AWS.
- 10:28:35So inside NameCheep or your domain
- 10:28:38company, we're going to go to the
- 10:28:41settings
- 10:28:44and we're going to find the domain. So
- 10:28:46in namecheep it's in domain list
- 10:28:50and here you may need to verify your
- 10:28:53email. So we'll click this and check
- 10:28:55your email for the verification. So this
- 10:28:58might take a while. So we just have to
- 10:29:00wait until the verification is complete.
- 10:29:04And we may need to log out and log back
- 10:29:07in to our NameCheep account for the
- 10:29:09verification to update.
- 10:29:14Once verification is complete, we should
- 10:29:16be able to go back to the domain list,
- 10:29:19find our domain, and open the settings
- 10:29:22for the domain.
- 10:29:25And in the settings, we're going to look
- 10:29:28for a section that's called name
- 10:29:30servers. If you're having trouble
- 10:29:32finding this in namecheep or your domain
- 10:29:35company, please check the AWS
- 10:29:37troubleshooting guide in the
- 10:29:39description.
- 10:29:40And then in here, we're going to click
- 10:29:42this dropown
- 10:29:44and we're going to set custom DNS
- 10:29:47because we're going to use AWS for this.
- 10:29:51So now we need to enter some name
- 10:29:53servers. To find these, we're going to
- 10:29:56go back to AWS route 53.
- 10:30:00And I need to zoom out a little bit to
- 10:30:03see this. Inside the hosted zone, we
- 10:30:07have some records here. and we're going
- 10:30:09to look for the record NS or name
- 10:30:12server.
- 10:30:13So in the NS record, we should see these
- 10:30:17four URLs here. So we just need to copy
- 10:30:20these over to namecheep or your domain
- 10:30:23company. So we'll select this and copy.
- 10:30:27And then inside NameCheep
- 10:30:30and here we'll paste.
- 10:30:33And we'll go back and select the second
- 10:30:37name server.
- 10:30:38copy
- 10:30:40and paste.
- 10:30:42If we need to, we can add two more name
- 10:30:45servers.
- 10:30:47And here we'll select this and copy.
- 10:30:52And here we'll paste.
- 10:30:55And we'll select the last one. Copy.
- 10:31:00And here paste. And then save.
- 10:31:07So what this does is when we open our
- 10:31:09domain name in the browser, NameCheep is
- 10:31:12going to redirect to AWS and then AWS
- 10:31:16will redirect to Elastic Beantock or to
- 10:31:19our app.
- 10:31:21So this setting will take a while to
- 10:31:23update. Sometimes it finishes in just 10
- 10:31:26minutes, but it could take up to 24 to
- 10:31:2948 hours. So we just have to be patient.
- 10:31:33So once we waited for a while, we can
- 10:31:36try opening a new tab.
- 10:31:39And in here we'll type http
- 10:31:43colon double slash and our domain name.
- 10:31:46So for me it's simon baenge.com
- 10:31:51and press enter.
- 10:31:55And this will redirect to our app. So
- 10:31:58now we're able to access our React app
- 10:32:00using a short domain name that's easy to
- 10:32:03remember.
- 10:32:06Next, I'm going to explain why we enter
- 10:32:09HTTP in front and why we see this not
- 10:32:12secure warning here. So this means that
- 10:32:16our website is not using encryption.
- 10:32:19This means that any data we send to this
- 10:32:21website can be intercepted by a hacker.
- 10:32:25To enable encryption on this website and
- 10:32:28make it secure, we need to set up an SSL
- 10:32:31certificate, which is a special file
- 10:32:33that enables encryption.
- 10:32:36To get an SSL certificate, we can use
- 10:32:39AWS.
- 10:32:41So, we'll go back to AWS Route 53 and
- 10:32:45then at the top we'll search for another
- 10:32:48service called certificate
- 10:32:51manager
- 10:32:53and click here
- 10:32:58and we can request a certificate. If you
- 10:33:01don't see it, you can also go in here
- 10:33:03and click request certificate.
- 10:33:07We'll use the default value and click
- 10:33:10next.
- 10:33:12And here we're going to enter our domain
- 10:33:14name. Make sure to enter your domain
- 10:33:16name.
- 10:33:18And we'll scroll down and make sure DNS
- 10:33:21validation is selected.
- 10:33:24And scroll down and click request.
- 10:33:28And then we'll wait for this to finish
- 10:33:30loading.
- 10:33:34And now AWS needs to confirm that we own
- 10:33:38the domain name. That's why it says
- 10:33:40pending validation.
- 10:33:42To do that, we can just click this
- 10:33:44create records in Route 53 button. If
- 10:33:47you don't see this, please check the AWS
- 10:33:50troubleshooting guide.
- 10:33:53And it should show up here. And we'll
- 10:33:56click create records.
- 10:33:59And we'll wait for this to finish. We
- 10:34:02may need to refresh a few times until it
- 10:34:06says it was successful.
- 10:34:08And now that we have an SSL certificate,
- 10:34:12we can use this in elastic beantock. So
- 10:34:15we'll go back to elastic beantock
- 10:34:18and then in the configurations here,
- 10:34:23we're going to scroll down to the
- 10:34:26instance traffic settings and click
- 10:34:29edit.
- 10:34:31And we're going to scroll down
- 10:34:38until we find this listeners section.
- 10:34:42And here we'll click add listener.
- 10:34:45For the port we're going to use 443.
- 10:34:49And for the protocol we're going to use
- 10:34:52HTTPS.
- 10:34:55HTTPS means we're going to have a secure
- 10:34:57connection to our website. And this
- 10:35:00usually uses port 443.
- 10:35:03And here we're going to choose the SSL
- 10:35:05certificate that we just created.
- 10:35:09And we should be able to leave the SSL
- 10:35:11policy blank and click save.
- 10:35:16If that didn't work, please check the
- 10:35:18AWS troubleshooting guide. So this
- 10:35:21basically adds an SSL certificate to the
- 10:35:24load balancer. And now any data sent to
- 10:35:27our website will be encrypted.
- 10:35:30So, we're going to scroll down and click
- 10:35:32apply.
- 10:35:38And again, this will take a few minutes
- 10:35:40to complete. So, we just have to wait
- 10:35:42for it.
- 10:35:46Once that's complete, I'll close some of
- 10:35:49these other tabs
- 10:35:52and I'll create a new tab.
- 10:35:55And in the URL, we're going to type
- 10:35:58https
- 10:36:00colon double slash. So this tells the
- 10:36:03browser to use the secure connection to
- 10:36:05our website
- 10:36:07and then we'll type our domain name. For
- 10:36:10me, it's simonbowenge.com
- 10:36:14and press enter.
- 10:36:16So now the not secure warning is gone
- 10:36:19and we're using a secure connection to
- 10:36:22our website that uses encryption.
- 10:36:25Also now if we just type our domain name
- 10:36:28in the URL without HTTPS in front and
- 10:36:32press enter
- 10:36:35it will default to using the secure
- 10:36:37version. So that's how we set up an SSL
- 10:36:40certificate and HTTPS in AWS.
- 10:36:44And now we have a professional and
- 10:36:47realworld setup for our React app.
- 10:36:51In this section, I'll show you how to
- 10:36:53delete everything we created in AWS.
- 10:36:57However, I recommend to do the exercises
- 10:36:59of this lesson first to get more
- 10:37:01practice with AWS and then do this
- 10:37:04deletion.
- 10:37:06Also, I try to keep everything we
- 10:37:08created in the free tier. The only
- 10:37:11exception is the hosted zone in Route
- 10:37:1353. You can use it for free for a few
- 10:37:16hours. So you could keep running these
- 10:37:18services and practice AWS if you want to
- 10:37:22check your AWS bill. We can go to the
- 10:37:25profile and click billing and cost
- 10:37:28management.
- 10:37:29And now after you finish the exercises,
- 10:37:32I'll show you how to delete everything
- 10:37:34just in case. So in elastic beantock, we
- 10:37:38can click this actions dropown and click
- 10:37:41terminate environment.
- 10:37:44And in here we need to type the name of
- 10:37:46the environment which is here
- 10:37:53and click terminate.
- 10:37:59Once that's finished, we can go up here
- 10:38:02and go to route 53.
- 10:38:07And in this menu, we can go to hosted
- 10:38:10zones.
- 10:38:12and we'll open the hosted zone.
- 10:38:15And inside here, we're going to select
- 10:38:18the A record and the CNAME record and
- 10:38:23delete these records.
- 10:38:28And then at the top, we can delete the
- 10:38:31hosted zone.
- 10:38:33And in here, we'll type delete and click
- 10:38:37delete.
- 10:38:40Once that's finished, at the top, we'll
- 10:38:42go to certificate manager.
- 10:38:48And in this menu, we're going to click
- 10:38:50list certificates.
- 10:38:53And we'll select the certificate and
- 10:38:55click delete.
- 10:38:58Type delete and click delete.
- 10:39:05You can also delete your entire account
- 10:39:08if you want by clicking your profile and
- 10:39:11clicking account
- 10:39:16and then close account. Although you
- 10:39:19lose access to the free tier if you do
- 10:39:21this. I usually keep my account open for
- 10:39:23a while to practice with the free tier.
- 10:39:28So now we put our React app on the
- 10:39:31internet and we created a professional
- 10:39:34real world setup and that's the end of
- 10:39:37this lesson. In this lesson we learned
- 10:39:40AWS or Amazon Web Services which is a
- 10:39:44collection of services that help us put
- 10:39:46stuff on the internet. We learned EC2
- 10:39:49which lets us rent a computer from AWS
- 10:39:52and is known as infrastructure as a
- 10:39:55service or IAS. We learned Elastic
- 10:39:59Beantock which manages EC2 and sets up
- 10:40:02all the software that we need. This is
- 10:40:05also known as a platform as a service or
- 10:40:08path. We put our backend on the
- 10:40:11internet. We learned how to build and
- 10:40:14optimize the front end and put it on the
- 10:40:16internet. We learned how to deploy React
- 10:40:19without a back end. We added a load
- 10:40:22balancer and database to create a more
- 10:40:25realworld setup. And we learned how to
- 10:40:28set up a domain name, SSL certificate,
- 10:40:31and HTTPS for our website.
- 10:40:36Here are some exercises to help you
- 10:40:38practice what we learned in this lesson.
- 10:41:11In this lesson, we're going to learn
- 10:41:13some of the updates in the new version
- 10:41:15of React, and we'll learn how to use
- 10:41:17TypeScript with React.
- 10:41:20On the React website, there's a page
- 10:41:22that lists the updates in the new
- 10:41:25version of React. You can find a link to
- 10:41:28this page in the description.
- 10:41:30So there's a lot of changes here and
- 10:41:33many of these changes are related to
- 10:41:35using React in a framework which is
- 10:41:38basically a layer on top of React.
- 10:41:41For now I'm just going to go over the
- 10:41:43changes that are related to this course.
- 10:41:48The first change is called support for
- 10:41:50document metadata. So this means that we
- 10:41:53can put elements that normally go inside
- 10:41:55the head section like the title, link
- 10:41:58and meta elements directly inside our
- 10:42:01component like this. So this lets us
- 10:42:04change the head section when we render
- 10:42:06this component. For example, in our
- 10:42:09project, we used a title element to
- 10:42:12change the text in the tab when we
- 10:42:14switch pages. The way this works is when
- 10:42:17this component renders, React will put
- 10:42:20this title inside the head section and
- 10:42:23this will change the text in the tab.
- 10:42:26Before React 19, we had to use an npm
- 10:42:29package like React helmet to do this.
- 10:42:33After React 19, we can just put these
- 10:42:36elements directly in our components. So,
- 10:42:38it's easier.
- 10:42:41Another change is called ref as a prop.
- 10:42:44Earlier in this course, we learned about
- 10:42:46refs and we can use a ref to get access
- 10:42:49to an HTML element directly.
- 10:42:53We can also give a ref to a component.
- 10:42:56For example, if we want to get an HTML
- 10:42:59element inside this component.
- 10:43:02Before React 19, ref was a special prop.
- 10:43:06We can't access it inside this
- 10:43:08component. We had to use a feature
- 10:43:10called forward ref.
- 10:43:13After React 19, ref is now a normal
- 10:43:16prop. We can access it like any other
- 10:43:19prop and we don't need to know about
- 10:43:21forward ref anymore, so it's easier.
- 10:43:25The next change is the introduction of
- 10:43:27the React compiler. The React compiler
- 10:43:31optimizes our React code. For example,
- 10:43:35let's say we had a component like this.
- 10:43:37We have a counter and two other
- 10:43:39components inside. When we click the
- 10:43:42counter, this element will update, but
- 10:43:46it will also recreate these other two
- 10:43:49components. Even though clicking the
- 10:43:51counter does not affect the other two
- 10:43:53components.
- 10:43:55That's because by default, when a
- 10:43:57component updates, it will also update
- 10:44:00or recreate all the components inside.
- 10:44:03And this may cause performance issues.
- 10:44:07To avoid this issue before React
- 10:44:10compiler, we had to use three features.
- 10:44:13Use memo, use callback, and memo. And
- 10:44:17this prevents a component from being
- 10:44:19recreated unless one of the props
- 10:44:22changed.
- 10:44:24So don't worry if you don't understand
- 10:44:25this code because after the React
- 10:44:28compiler, the compiler will just add
- 10:44:30these optimizations automatically.
- 10:44:34And now we can just write our code as
- 10:44:36normal and we don't need to worry about
- 10:44:39use memo, use callback or memo anymore
- 10:44:42in most cases.
- 10:44:45So again, the React compiler optimizes
- 10:44:48our React code. All we have to do is to
- 10:44:52set up the React compiler and we will
- 10:44:54get these optimizations. We don't have
- 10:44:57to change any of our code.
- 10:45:02Now, let's learn how to set up the React
- 10:45:04compiler in our project. In the
- 10:45:07description, we'll click this link, go
- 10:45:09to lesson 11, and here we have the
- 10:45:12instructions for setting up the React
- 10:45:14compiler. I put the setup instructions
- 10:45:17here because this process might change
- 10:45:20in the future, and it's not easy to
- 10:45:22update the video. So, follow the
- 10:45:24instructions that you see on your page.
- 10:45:27For me, the first step is to install the
- 10:45:30React compiler npm package. So, I'm
- 10:45:33going to select this command and copy
- 10:45:36it. And then in my project,
- 10:45:40I'm going to go to the command line and
- 10:45:42create a new tab. And I'll close these
- 10:45:45other tabs so that I can start fresh.
- 10:45:49And first, I need to make sure I'm
- 10:45:50running inside e-commerce-pro.
- 10:45:53So, cd ecommerce-
- 10:45:57project
- 10:45:59and then I'll paste the command
- 10:46:02and press enter to install the react
- 10:46:05compiler npm package.
- 10:46:09And then I'll go back and the next
- 10:46:11instruction is to copy this react
- 10:46:14config. So, I'll select this config and
- 10:46:17copy it.
- 10:46:19And then I need to update vit.config.js.
- 10:46:22js and put that react config in here. So
- 10:46:26I'll go to the project
- 10:46:28and open vit. doconfig.js.
- 10:46:32And at the top I see this react config.
- 10:46:35So I'm just going to replace it and
- 10:46:39paste
- 10:46:41and save.
- 10:46:43And that's the setup. Again, the
- 10:46:45instructions might be different when you
- 10:46:47watch this video. So follow the
- 10:46:49instructions on your page.
- 10:46:53Next, let's check if the React compiler
- 10:46:55is working. So, we'll go back to the
- 10:46:58instructions
- 10:47:00and down here we have some instructions
- 10:47:02for checking if React compiler is
- 10:47:05working. Again, follow the instructions
- 10:47:07on your page because they might have
- 10:47:09changed. So, first I'll start the back
- 10:47:12end and the front end using mpm rundev.
- 10:47:16I'll go back to VS Code
- 10:47:18and in the command line I'll create a
- 10:47:21new tab
- 10:47:23and I'm going to go into the backend
- 10:47:25folder using cd ecommerce-backend
- 10:47:31and run mpm rundev.
- 10:47:36And I'll go to the front end tab
- 10:47:39and I'll also run mpm rundev.
- 10:47:45So now both of them are running. I'll go
- 10:47:48back to the instructions
- 10:47:50and the next step is to install the
- 10:47:53React DevTools Chrome extension. So
- 10:47:56we'll click here.
- 10:47:58So this is an extension made
- 10:48:00specifically for React. It has a bunch
- 10:48:02of features that help us with React
- 10:48:04development.
- 10:48:06So here we're going to add this
- 10:48:08extension to our browser.
- 10:48:13And once that's finished, I'll go back
- 10:48:15to the instructions.
- 10:48:19And the next step is in your project,
- 10:48:21open the console and open the components
- 10:48:23tab. So, in VS Code, we're going to open
- 10:48:28this project in the browser
- 10:48:33and I'm going to rightclick
- 10:48:35and inspect.
- 10:48:38And in the console, we're going to click
- 10:48:40here. And we should have a new
- 10:48:43components tab.
- 10:48:46So, we'll click this.
- 10:48:48If it gets stuck, you can refresh.
- 10:48:53And this shows us all the React
- 10:48:55components that are on our page. If we
- 10:48:58scroll down and if we look at our
- 10:49:01components, they should have a badge
- 10:49:03beside them called memo. This means that
- 10:49:06the React compiler is working and it's
- 10:49:09optimizing our React components.
- 10:49:13Now, let's go back and look at some more
- 10:49:15changes in the new version of React.
- 10:49:19The next change is removed prop types
- 10:49:22and default props. So prop types were an
- 10:49:25older feature of React. It basically
- 10:49:28lets us check the type of a prop.
- 10:49:32For example, here we can check if the
- 10:49:35prop text is a string. If we run this
- 10:49:38app and text is not a string, it would
- 10:49:41show a warning in the console.
- 10:49:45So now we use a different tool called
- 10:49:47TypeScript which is better at this type
- 10:49:50of type checking. So React removed the
- 10:49:53prop types feature in favor of
- 10:49:55TypeScript.
- 10:49:57React also removed default props. If a
- 10:50:00prop is undefined, this feature lets us
- 10:50:03set a default value. So now we can do
- 10:50:07this with normal JavaScript. So we don't
- 10:50:09need this React feature anymore.
- 10:50:14Now that we learned that propt types was
- 10:50:16replaced by TypeScript, let's learn how
- 10:50:19to set up TypeScript with React, we'll
- 10:50:22go back to VS Code.
- 10:50:25And to set up TypeScript with React, we
- 10:50:28can use the create vit command. So in
- 10:50:32the command line, we'll create a new tab
- 10:50:36and make sure this is running in the
- 10:50:38React course folder. And we're going to
- 10:50:41run the command npx
- 10:50:44create-ash vit. And we'll type at to
- 10:50:48give it a specific version. And we'll
- 10:50:51click this link in the description. Go
- 10:50:53to lesson 11.
- 10:50:55And we'll use this version of vit. So
- 10:50:58copy the version that you see on your
- 10:51:00page.
- 10:51:01And here we'll paste
- 10:51:04and press enter.
- 10:51:08If it asks to install the package, press
- 10:51:10Y and press enter.
- 10:51:14And as a review, create VIT creates a
- 10:51:17new project. So let's name this project
- 10:51:20ecommerce-
- 10:51:23project-ts
- 10:51:26or TypeScript and press enter.
- 10:51:30For the framework,
- 10:51:32we're going to press down and choose
- 10:51:34React.
- 10:51:37And for the variant, we're going to
- 10:51:39choose TypeScript.
- 10:51:43So this will create a new React project
- 10:51:46that uses TypeScript.
- 10:51:48If we look at the code inside source,
- 10:51:52notice that it uses tsx files instead of
- 10:51:55JSX.
- 10:51:56TSX means this code uses TypeScript
- 10:52:00while JSX means it uses JavaScript.
- 10:52:04So now to run this project, we're going
- 10:52:06to follow the instructions that we see.
- 10:52:08So for me, I'll cd into e-commerce
- 10:52:13dash project-ts
- 10:52:17and run npm install
- 10:52:21to install all the packages that we
- 10:52:23need.
- 10:52:26And then I'm going to open the tab
- 10:52:27that's running the front end. And I'll
- 10:52:30press Ctrl C to stop the front end so we
- 10:52:34don't have any issues. And then in the
- 10:52:36TypeScript tab, we're going to run mpm
- 10:52:40rundev to start this project.
- 10:52:45And now we'll open this in the browser.
- 10:52:48So I already have it open. So I'm going
- 10:52:50to go in here and open this tab and
- 10:52:53refresh.
- 10:52:56Now we have a new React project. But
- 10:52:59this project uses TypeScript instead of
- 10:53:01JavaScript.
- 10:53:04And now I'll give you an intro to
- 10:53:06TypeScript.
- 10:53:08Let's go back to our code.
- 10:53:11And inside the TypeScript project, we're
- 10:53:14going to open app.tsx.
- 10:53:18So first of all, what is Typescript?
- 10:53:21Typescript is just JavaScript with extra
- 10:53:24features.
- 10:53:27So notice that this TypeScript code is
- 10:53:30the same as JavaScript code. The syntax
- 10:53:33is the same and the features are the
- 10:53:36same. For example, in here
- 10:53:40in Typescript, we can create a variable
- 10:53:43the same way as JavaScript using const
- 10:53:47message equals the string hello.
- 10:53:51And then we can console.log log message.
- 10:53:56So as you can see, TypeScript is just
- 10:53:59JavaScript.
- 10:54:00However, TypeScript gives us some extra
- 10:54:03features on top of JavaScript. The main
- 10:54:06feature is we can add types to our
- 10:54:09variables.
- 10:54:11So what are types? Remember that
- 10:54:13JavaScript has different types of values
- 10:54:16like numbers, strings, and objects.
- 10:54:20In Typescript, we can set what type of
- 10:54:23value is inside a variable.
- 10:54:27For example, in this variable, we have a
- 10:54:30string. So, after the variable name, we
- 10:54:34can type colon space and string.
- 10:54:39So, this tells TypeScript that this
- 10:54:41variable contains a string.
- 10:54:45So now below this if I type message and
- 10:54:50then dot TypeScript knows that this is a
- 10:54:54string. So now it's able to suggest all
- 10:54:57the features of a string like two
- 10:55:01lowercase
- 10:55:03and brackets.
- 10:55:06So using TypeScript our computer can
- 10:55:08give us better autocomplete suggestions.
- 10:55:12Next, if I type message
- 10:55:16to fixed and brackets,
- 10:55:20two fixed only works with numbers.
- 10:55:24Because TypeScript knows that this is a
- 10:55:26string, it's going to give us an error
- 10:55:29saying that too does not work on
- 10:55:31strings.
- 10:55:34So, this feature is called type
- 10:55:36checking. TypeScript checks if this code
- 10:55:40works with this type of value and this
- 10:55:43helps us avoid errors. In JavaScript,
- 10:55:46there is no type checking. So if I want
- 10:55:49to put this code on my website,
- 10:55:51JavaScript will just let me do it and it
- 10:55:53will crash my website. But in
- 10:55:56Typescript, if I want to put this code
- 10:55:58on my website, I would save this code
- 10:56:02and then in my command line, press
- 10:56:04control C to stop the server and run npm
- 10:56:08run build.
- 10:56:12Notice that TypeScript actually blocks
- 10:56:14us from building this code and putting
- 10:56:17it on our website because there's an
- 10:56:19error with the types.
- 10:56:21So, as you can see, TypeScript gives us
- 10:56:24type checking, which helps us avoid a
- 10:56:27lot of these coding errors and helps us
- 10:56:29keep our code safer.
- 10:56:32Another feature of TypeScript is type
- 10:56:35inference. Type inference means
- 10:56:38TypeScript can figure out the type
- 10:56:40automatically.
- 10:56:42For example, in this code, this variable
- 10:56:46obviously contains a string. So we
- 10:56:49actually don't need to set the type
- 10:56:51manually here. If we delete this type,
- 10:56:56TypeScript will infer or figure out that
- 10:56:59this is a string. So I can remove it and
- 10:57:02save
- 10:57:04and the error check still works because
- 10:57:06TypeScript knows that this is a string.
- 10:57:09Another example of type inference is up
- 10:57:12here in use state. So this count
- 10:57:16variable contains the number zero.
- 10:57:19TypeScript will infer or figure out that
- 10:57:22count is a number.
- 10:57:25So if we do count to two lowercase and
- 10:57:30brackets, it's going to give us an error
- 10:57:33because two lowercase only works with
- 10:57:35strings and TypeScript figured out that
- 10:57:38this is a number. So, as you can see,
- 10:57:41TypeScript is just JavaScript, but it
- 10:57:44adds extra features like type checking
- 10:57:47and type inference to our code, and this
- 10:57:50helps us find and avoid errors. So, now
- 10:57:53let's save this code. And we'll save
- 10:57:56these changes into git. In the git
- 10:57:59section
- 10:58:01at the top, we're going to use the
- 10:58:03message create new React TypeScript
- 10:58:09project
- 10:58:10and press commit.
- 10:58:16So, we just learned how to create a new
- 10:58:19React project that uses TypeScript.
- 10:58:22Now, let's learn how to convert an
- 10:58:24existing project, like our e-commerce
- 10:58:27project, to Typescript. To do that,
- 10:58:30we're just going to move all of our
- 10:58:32changes over to the new TypeScript
- 10:58:35project.
- 10:58:36Before we begin, if you have any issues
- 10:58:39during this process, please check the
- 10:58:41troubleshooting guide in the description
- 10:58:44since things might change depending on
- 10:58:46the version of VIT.
- 10:58:50To make this easier in the e-commerce
- 10:58:52project, we're going to delete the node
- 10:58:54modules folder for now.
- 10:59:01And then we'll make a copy of this
- 10:59:03folder. So we'll rightclick,
- 10:59:06copy,
- 10:59:08and we'll collapse these folders and
- 10:59:11click the outside and rightclick and
- 10:59:15paste.
- 10:59:17So, this just helps us keep track of
- 10:59:19progress when we're moving the changes
- 10:59:21over. So, we're going to start from top
- 10:59:24to bottom. First, we'll look at the
- 10:59:26public folder. So, in here, the change
- 10:59:30we made is we added this images folder.
- 10:59:33So, we're going to take the public
- 10:59:35folder and we're going to drag it to the
- 10:59:38TypeScript project
- 10:59:40and click move
- 10:59:42and click replace.
- 10:59:46Next, let's go into the source folder
- 10:59:50and we're going to move our changes over
- 10:59:52one by one. So, first in the assets
- 10:59:55folder,
- 10:59:56we didn't make any changes to this
- 10:59:58folder, so we're not going to move it
- 11:00:00over and we can just delete this folder.
- 11:00:06As a reminder, we're not trying to move
- 11:00:09all of our code over. We want to start
- 11:00:12from a default TypeScript project and
- 11:00:14then only move our changes over to the
- 11:00:17new project.
- 11:00:19So let's continue and look at the
- 11:00:21components folder. So this folder is
- 11:00:24completely new. So we're going to move
- 11:00:26this entire folder over to the
- 11:00:28TypeScript project inside the source
- 11:00:31folder
- 11:00:34and click move.
- 11:00:37And if it asks to update the imports,
- 11:00:39we're going to click no because we're
- 11:00:41going to move everything over.
- 11:00:44Next, we're going to go back up and the
- 11:00:48pages folder is new. So, we're going to
- 11:00:50drag this folder into the TypeScript
- 11:00:53project inside source
- 11:00:56and click move
- 11:00:58and update the imports. Click no.
- 11:01:03Next, we'll scroll up and the utils
- 11:01:06folder is new. So, we're going to select
- 11:01:08this folder and drag it into the
- 11:01:10TypeScript folder inside source. So,
- 11:01:14move and click no.
- 11:01:18And next, we'll keep going one by one.
- 11:01:22For app.css,
- 11:01:24we made it empty. So, we're going to
- 11:01:26move this over. So, drag it into source.
- 11:01:31Click move and click replace
- 11:01:35and scroll back up for app.jsx.
- 11:01:40We made a bunch of changes in here. So,
- 11:01:42we're going to move this over. So, drag
- 11:01:45it into source
- 11:01:48and click move.
- 11:01:50However, notice in the TypeScript
- 11:01:53project, the file is called app.tsx.
- 11:01:57So, we're going to replace this code
- 11:01:58with our file. So, first we're going to
- 11:02:01select this and delete app.tsx.
- 11:02:07And then we'll rightclick app.jsx
- 11:02:10and rename
- 11:02:12and rename it to tsx.
- 11:02:17Now, we'll scroll back up and continue
- 11:02:20moving our changes.
- 11:02:22So, index.css,
- 11:02:24we completely changed this file. So,
- 11:02:27we're going to move it into the
- 11:02:29TypeScript project into source.
- 11:02:33Click move and click replace
- 11:02:37and scroll back up. And finally, in
- 11:02:41main.jsx,
- 11:02:42we just added the router. So, let's move
- 11:02:45this file as well. We'll select it and
- 11:02:48drag it into source
- 11:02:51and move.
- 11:02:53And again in Typescript the file is
- 11:02:55called main.tsx.
- 11:02:58We'll select it and delete.
- 11:03:02And we're going to rename our file.
- 11:03:06Right click, rename
- 11:03:08main.tsx,
- 11:03:11and press enter.
- 11:03:14Update the imports. Select no.
- 11:03:18We'll scroll back up and we moved all of
- 11:03:21the code from the source folder so we
- 11:03:24can delete it.
- 11:03:27Next, we don't need to move the starting
- 11:03:30code folder because this was just for
- 11:03:32practice when we were setting up this
- 11:03:34project. So, we can just delete this
- 11:03:37folder.
- 11:03:41Now, let's move all the files over to
- 11:03:44the TypeScript project. So in git
- 11:03:47ignore, we didn't make any changes to
- 11:03:50this file, so we don't need to move it
- 11:03:52over and we can delete it.
- 11:03:57In eslint.config.js,
- 11:04:00we added one change to this file at the
- 11:04:02bottom, which is we added this rule to
- 11:04:06disable prop types. So we'll select this
- 11:04:08code and copy.
- 11:04:12And in the TypeScript project, we're
- 11:04:15going to open eslint.config.
- 11:04:18And at the bottom in this rules object,
- 11:04:22we're going to paste and move that
- 11:04:24change over and save.
- 11:04:28So again, we don't want to move all of
- 11:04:30our code over. We just want to move the
- 11:04:32changes over because a lot of this code
- 11:04:35is specific to TypeScript.
- 11:04:38So, we'll scroll back up here and we can
- 11:04:41delete this file because we moved it
- 11:04:44over.
- 11:04:46Next, let's keep going in here. For
- 11:04:50index.html,
- 11:04:52we changed a few parts of this code. So,
- 11:04:55we're going to move these changes over
- 11:04:58to help us compare this code with the
- 11:05:00TypeScript version. I'm going to scroll
- 11:05:03down
- 11:05:04and take this index.html HTML and then
- 11:05:07move it at the bottom here so that I can
- 11:05:10compare these side by side.
- 11:05:13So first the first three lines here we
- 11:05:16did not make any changes so we can skip
- 11:05:19them.
- 11:05:20And then here the next three lines we
- 11:05:24also did not make any changes so we can
- 11:05:26skip them. And here we added a base
- 11:05:30element. So we're going to select this
- 11:05:32and copy. And in the TypeScript version,
- 11:05:36we're going to paste that change.
- 11:05:39And here we also deleted the title. So
- 11:05:42in the TypeScript version, we're also
- 11:05:44going to delete the title.
- 11:05:47And here we added a font called Roboto.
- 11:05:51So we're going to move this change by
- 11:05:53selecting it and copy.
- 11:05:56And in here we'll paste the font. And if
- 11:06:01we need to fix the formatting, we can
- 11:06:03select this and press shift tab to
- 11:06:06remove some indents
- 11:06:10and I'll scroll down.
- 11:06:14And for the rest of the code, we did not
- 11:06:16make any changes. So now we moved all
- 11:06:19the changes over. I'm going to save this
- 11:06:22file and close this for now.
- 11:06:26And then in the copy, we can delete
- 11:06:28index.html.
- 11:06:32And next, we don't need to move packaged
- 11:06:35lock.json over because this will be
- 11:06:38autogenerated when we npm install. So,
- 11:06:41we can just delete this file
- 11:06:46and I'll scroll back up
- 11:06:51and we'll move over package.json.
- 11:06:55In this file, we didn't change anything
- 11:06:57at the top, but we did add some
- 11:07:00dependencies and dev dependencies. So,
- 11:07:02we're going to move these over. We'll
- 11:07:05select the dependencies and copy.
- 11:07:09And then in the TypeScript project, open
- 11:07:12package.json.
- 11:07:15And in these dependencies,
- 11:07:18we'll type comma and paste.
- 11:07:21And then we'll remove any duplicates.
- 11:07:24So, we'll remove React and React DOM and
- 11:07:30save.
- 11:07:32And we'll scroll back up
- 11:07:35and open package.json.
- 11:07:37And we'll do the same thing with the dev
- 11:07:39dependencies. So, we'll select all these
- 11:07:42dependencies
- 11:07:44and copy.
- 11:07:46And in the TypeScript project
- 11:07:49package.json JSON in the dev
- 11:07:51dependencies at the bottom we'll type
- 11:07:54comma and paste
- 11:07:58and we'll remove all these duplicates.
- 11:08:01So I'll remove eslint
- 11:08:05and I'll remove these duplicates
- 11:08:08and these duplicates
- 11:08:11and these duplicates
- 11:08:15and remove this
- 11:08:18until there are no more duplicates
- 11:08:20remaining.
- 11:08:22And now we'll save.
- 11:08:24And then we're going to run npm install
- 11:08:26to make sure we successfully install all
- 11:08:29these packages. In the command line,
- 11:08:32we'll go to the tab for the TypeScript
- 11:08:34project and we'll run npm install.
- 11:08:41And now we can scroll up and delete
- 11:08:44package.json.
- 11:08:48And we'll keep going for readme.md. We
- 11:08:52didn't change this file. So we can
- 11:08:54delete it.
- 11:08:59And this file setup test.js is new. So
- 11:09:03we'll move this over to the TypeScript
- 11:09:05project.
- 11:09:07And we'll scroll up and we're going to
- 11:09:10move over vit.config.js.
- 11:09:12So in here we added a bunch of
- 11:09:14configurations.
- 11:09:16So in the TypeScript project
- 11:09:19we're going to drag vit.config.ts ts at
- 11:09:23the bottom
- 11:09:25and we're going to move our changes
- 11:09:27over. So, inside the config,
- 11:09:32we added some stuff for the React
- 11:09:34config. So, we're going to select this
- 11:09:37React config and copy
- 11:09:40and then select this code, remove it,
- 11:09:44and paste.
- 11:09:46And we'll keep going. So, all of these
- 11:09:49configs are new. So, we can just select
- 11:09:52this code
- 11:09:54and then copy.
- 11:09:57And inside the TypeScript version, we're
- 11:10:00going to paste
- 11:10:03and save.
- 11:10:06So, now we moved all of our changes over
- 11:10:08to the new project. And we can delete
- 11:10:11this file.
- 11:10:15And I'll close this for now.
- 11:10:17And finally, vtest.config.js.
- 11:10:20JS is new. So, we can drag it into the
- 11:10:23project.
- 11:10:26And now we moved all of our changes over
- 11:10:29into the TypeScript project. And I can
- 11:10:32delete this copy.
- 11:10:38Next, we're going to go into the
- 11:10:39TypeScript project and we're going to
- 11:10:42fix the errors. First, we'll open
- 11:10:45main.tsx.
- 11:10:47And in here, instead of importing from
- 11:10:50app.jsx,
- 11:10:51the file is now app.tsx.
- 11:10:55So we're going to update that.
- 11:10:58Also, we have an error here. So this
- 11:11:01code tries to get an element from the
- 11:11:03page using the ID root. However, this
- 11:11:07element might not exist. So TypeScript
- 11:11:10gives us an error telling us this value
- 11:11:13might be null. However, if we check our
- 11:11:17index.html
- 11:11:20and scroll down, we have an element here
- 11:11:23with the ID of root. So, we know that
- 11:11:26this element definitely exists.
- 11:11:29In main.tsx,
- 11:11:31to tell Typescript that this value
- 11:11:34definitely exists. At the end, we're
- 11:11:37going to add exclamation and save.
- 11:11:42So this tells Typescript that this value
- 11:11:45definitely exists and it will not be
- 11:11:48null. And now the error is gone.
- 11:11:52Next we'll open app.tsx.
- 11:11:57And up here we get some errors when we
- 11:11:59are trying to import these files. The
- 11:12:02reason we get these errors is because
- 11:12:04these files are still.jsx
- 11:12:08files or JavaScript files. So, we could
- 11:12:11rename all of our files totsx,
- 11:12:15but then Typescript will start doing
- 11:12:16type checking in these files and we may
- 11:12:19get a bunch of errors.
- 11:12:22Instead,
- 11:12:23in our files, we can scroll down to
- 11:12:27tsconfig.app.json.
- 11:12:31And inside here,
- 11:12:34we're going to add a setting double
- 11:12:36quotes allow capital J S
- 11:12:40colon true and comma
- 11:12:44and save.
- 11:12:47So this setting lets us mix TypeScript
- 11:12:50and JavaScript files in our project. So
- 11:12:53now in app.tsx, tsx.
- 11:12:56Our TypeScript code can also import from
- 11:12:59JavaScript files and it won't cause an
- 11:13:02error. So the allowJS setting lets us
- 11:13:06gradually convert a JavaScript project
- 11:13:08to TypeScript, one file at a time.
- 11:13:12Now that we fixed the errors, let's run
- 11:13:15this project again to see if our
- 11:13:17e-commerce project works with
- 11:13:18TypeScript. in the command line. Make
- 11:13:22sure you're in e-commerce project ts and
- 11:13:25we're going to npm rundev
- 11:13:28and press enter.
- 11:13:32And now if we open this URL in the
- 11:13:34browser
- 11:13:36and refresh
- 11:13:39and now you can see that our e-commerce
- 11:13:42project is working with TypeScript. If
- 11:13:45we try some of the features like add to
- 11:13:47cart, you'll see that everything still
- 11:13:50works. If this does not work for you,
- 11:13:53you can check the troubleshooting guide
- 11:13:54in the description.
- 11:13:57Now, let's go back to our code
- 11:14:00and let's rename our folders so that we
- 11:14:03use TypeScript by default. We're going
- 11:14:06to scroll up and we're going to rename
- 11:14:08e-commerce project. Right click, rename
- 11:14:13to e-commerce project dashjs
- 11:14:16for JavaScript.
- 11:14:18And then we're going to rename
- 11:14:20e-commerce project-ts
- 11:14:23to e-commerce project.
- 11:14:28So now by default our project will be
- 11:14:31using TypeScript.
- 11:14:33And just to make sure, we're going to go
- 11:14:34to the command line and stop the V
- 11:14:37server. And we're going to delete all
- 11:14:39our tabs. So here we'll also control C
- 11:14:42to stop the back end and delete all the
- 11:14:45tabs. And then we'll reopen the command
- 11:14:48line using the terminal menu and new
- 11:14:51terminal.
- 11:14:54And we're going to restart the back end
- 11:14:56using cd ecommerce-backend
- 11:15:01mpm rundev
- 11:15:05and create a new tab
- 11:15:10and we'll run the front end cd
- 11:15:12ecommerce- project
- 11:15:15and npm rundev.
- 11:15:20And in the browser, we'll refresh
- 11:15:25and our TypeScript project still works.
- 11:15:28We'll go to VS Code. And finally, we're
- 11:15:32going to organize these folders a little
- 11:15:34bit. So, right now, we're no longer
- 11:15:36using the e-commerce project JavaScript
- 11:15:39version or the React basics folder. So,
- 11:15:42let's click outside and we'll click here
- 11:15:44to create a new folder. and let's call
- 11:15:47it old dash projects.
- 11:15:51And then we'll drag the JavaScript
- 11:15:53version into here.
- 11:15:57And we'll move React Basics into here.
- 11:16:04So now our folders are a little bit more
- 11:16:06organized.
- 11:16:09So now let's save this into Git. We'll
- 11:16:11open the git section and at the top
- 11:16:15we'll use the message convert e-commerce
- 11:16:20project to type script and commit.
- 11:16:30Next we're going to learn an intro to
- 11:16:32TypeScript with React. First, we'll go
- 11:16:36into our files
- 11:16:38and in the project, we're going to go to
- 11:16:41source the utils folder and we're going
- 11:16:45to look at money.js.
- 11:16:48Currently, this is a JavaScript file.
- 11:16:51That means there is no type checking in
- 11:16:54this code. So, we're going to convert
- 11:16:56this to TypeScript by right-clicking
- 11:17:00and rename.
- 11:17:02And we'll rename it to money.t. ts or
- 11:17:06typescript.
- 11:17:09So this converts the file into
- 11:17:11typescript and now it will enable type
- 11:17:14checking and now we will get an error
- 11:17:16here.
- 11:17:18So this error says amount sent
- 11:17:21implicitly has an any type. So what this
- 11:17:25means is that earlier we learned the
- 11:17:27feature type inference which means
- 11:17:29typescript can figure out the type
- 11:17:32automatically.
- 11:17:34However, in this situation, TypeScript
- 11:17:37does not have enough information to
- 11:17:40figure out the type of amount sets. So,
- 11:17:43we have to set the type manually.
- 11:17:46To set the type of a parameter after
- 11:17:50this parameter, we're going to type
- 11:17:52colon space and then the type of this
- 11:17:56value. So, in this case, amount sent
- 11:17:59should be a number. So, we're going to
- 11:18:01type number and save.
- 11:18:06And now that TypeScript knows what type
- 11:18:09of value this is, the error will go
- 11:18:11away. And now we successfully converted
- 11:18:14this file to TypeScript.
- 11:18:17Let's do another example. We're going to
- 11:18:19go to the components folder and we're
- 11:18:22going to convert header.jsx into
- 11:18:25TypeScript. So we're going to rename
- 11:18:27this file
- 11:18:29to header.tsx.
- 11:18:33So tsx is just a TypeScript file, but
- 11:18:37the X means we can write HTML inside
- 11:18:40this file. So it's just like JSX except
- 11:18:44it uses TypeScript.
- 11:18:47So now this converts the file to
- 11:18:48TypeScript and it enables type checking
- 11:18:51and we get some errors here.
- 11:18:54If we look at this error,
- 11:18:57we get a similar error. Cart implicitly
- 11:19:00has an any type. So again, this means
- 11:19:03that TypeScript does not have enough
- 11:19:06information to figure out the type of
- 11:19:08cart automatically. So we have to set
- 11:19:11the type manually.
- 11:19:14We might try to add the type after cart
- 11:19:16like this. However, this is not how we
- 11:19:19add a type to this code because remember
- 11:19:23this code is actually a shortcut for
- 11:19:26this code. This is called the
- 11:19:28dstructuring shortcut.
- 11:19:31So this whole thing is the parameter.
- 11:19:35So we need to add the type here
- 11:19:39after the parameter.
- 11:19:42So this code is a shortcut for props
- 11:19:45which is an object. To tell Typescript
- 11:19:48that this is an object here we're going
- 11:19:51to type curly brackets
- 11:19:54and then inside here we can also tell
- 11:19:57TypeScript what properties are in this
- 11:20:00object. So in this object we have a cart
- 11:20:04property. So in the type we're also
- 11:20:06going to add a cart property. And
- 11:20:10remember that the cart is an array like
- 11:20:13this.
- 11:20:15So we're also going to tell Typescript
- 11:20:17that this cart is an array using colon
- 11:20:20and square bracket semicolon.
- 11:20:24So again, this code tells Typescript
- 11:20:27that this value is an object with a
- 11:20:30property called cart and the cart is an
- 11:20:33array.
- 11:20:35Next, we can also tell Typescript what
- 11:20:38is inside this array. In front of the
- 11:20:41array, we can type curly brackets to
- 11:20:44tell Typescript that this is an array of
- 11:20:47objects.
- 11:20:50And inside this object, we can also tell
- 11:20:52TypeScript what properties each object
- 11:20:55has. So inside our cart array, each
- 11:20:59object has a product ID and this product
- 11:21:03ID is a string. So we can tell that to
- 11:21:06TypeScript using colon string semicolon
- 11:21:12and each object also has a quantity and
- 11:21:15this is a number. So we can tell that to
- 11:21:18Typescript using colon number semicolon
- 11:21:23and each object has a delivery option ID
- 11:21:28which is a string. So colon string
- 11:21:31semicolon.
- 11:21:34So using this code we can tell
- 11:21:36Typescript exactly what type of value
- 11:21:39this is. It's an object with a property
- 11:21:42called cart. And the cart is an array.
- 11:21:46And each value in the array has these
- 11:21:49three properties.
- 11:21:51So now TypeScript can actually look at
- 11:21:53the rest of our code and check if each
- 11:21:56value in the array has a quantity
- 11:21:59and if we accidentally misspell quantity
- 11:22:02here like this. TypeScript can use this
- 11:22:06type here to check if we made a mistake.
- 11:22:09So this helps us avoid a lot of mistakes
- 11:22:11when we're coding. So, let's add that
- 11:22:14back and save.
- 11:22:20Next, we're going to organize this code
- 11:22:22a little bit. So, putting a complex type
- 11:22:26directly in the code like this is a
- 11:22:29little hard to read. So, we can actually
- 11:22:32take this type and then save it
- 11:22:34separately.
- 11:22:36above this component we can use type
- 11:22:40space and then give this a name.
- 11:22:44So here this value is the props for the
- 11:22:47header component. So we'll name this
- 11:22:50header props and we'll type equal and
- 11:22:55we're going to move this type up here.
- 11:22:57So we'll select this type and cut.
- 11:23:01And then in here we're going to paste.
- 11:23:07So this is called a type alias and it
- 11:23:10works just like a variable but for
- 11:23:12types. So we can save a type in here and
- 11:23:17then we can use this type later in our
- 11:23:19code. For example, down here, we can
- 11:23:23just use this type header props
- 11:23:28and it will substitute this type into
- 11:23:31our code just like a variable.
- 11:23:35And now our code is much cleaner. So
- 11:23:38that's an intro to how to use TypeScript
- 11:23:40with React. We basically add types to
- 11:23:44React features like props.
- 11:23:48So now let's save
- 11:23:50and we'll save these changes into git.
- 11:23:53In the git section
- 11:23:56at the top we'll use the message convert
- 11:24:00money utils and header to type script
- 11:24:07and commit.
- 11:24:12Finally, we're going to learn how to use
- 11:24:14AI or artificial intelligence with
- 11:24:18React. After we learn React, we can use
- 11:24:21AI to help us write React code faster.
- 11:24:25The easiest way to get started with AI
- 11:24:28is to use GitHub Copilot, which is built
- 11:24:31into VS Code. Before we begin, AI tools
- 11:24:35are changing very quickly. If something
- 11:24:38in this section is not working, you can
- 11:24:40check the troubleshooting guide in the
- 11:24:42description.
- 11:24:44To set up GitHub Copilot, we're going to
- 11:24:47install the GitHub Copilot extension.
- 11:24:50So, in the extension section, we need to
- 11:24:53make sure we have the GitHub Copilot
- 11:24:56extension installed. If it's not
- 11:24:58installed, we can search up here for
- 11:25:02GitHub Copilot
- 11:25:06and install this extension.
- 11:25:12Once we have that installed,
- 11:25:14we're going to click this C-pilot icon
- 11:25:17up here and we'll need to log in to our
- 11:25:20GitHub account. So, we'll click sign in
- 11:25:25and then we need to log in to GitHub. If
- 11:25:28you don't have a GitHub account, you can
- 11:25:30create one by clicking here or going to
- 11:25:33github.com.
- 11:25:34So, I'm going to log into my GitHub
- 11:25:37account
- 11:25:39and then I'll click continue
- 11:25:43and I'll click here to go back to VS
- 11:25:46Code.
- 11:25:48Once we finish the setup, we can start
- 11:25:50using some features of AI. The first
- 11:25:54feature is autocomplete.
- 11:25:57So, let's say that we're in the header
- 11:26:00and we'll close this window for now.
- 11:26:04And let's scroll down to the HTML
- 11:26:08and we're going to go to the right
- 11:26:10section.
- 11:26:11Let's say that inside the header, we
- 11:26:13want to add a link to a login page. So
- 11:26:17here we can just start typing in this
- 11:26:20code
- 11:26:22and the AI will give us some
- 11:26:24autocomplete suggestions. To accept
- 11:26:28these suggestions, we can press tab.
- 11:26:31So this creates a new link and some
- 11:26:33text. And I'll press enter.
- 11:26:37And it gives another suggestion. So I
- 11:26:39press tab.
- 11:26:41And this creates the closing tag for the
- 11:26:43link.
- 11:26:45So you notice that the autocomplete is
- 11:26:47very smart because the AI can read and
- 11:26:51understand the rest of our code and it
- 11:26:54can suggest code that follows the same
- 11:26:56patterns as the rest of our code. For
- 11:26:58example, here it creates a link element
- 11:27:02and it gave it a class name that is
- 11:27:04similar to the other class names.
- 11:27:07However, when using AI, we may need to
- 11:27:09go in and modify things to make sure we
- 11:27:12get exactly what we want. So, here the
- 11:27:16AI created a link to the homepage, but
- 11:27:19what we actually want is a link to a
- 11:27:21login page. So, I can click here and
- 11:27:24modify the URL path to slash login.
- 11:27:29And I can also change the text to login.
- 11:27:34And I'll change this to login. And I
- 11:27:37don't really have an icon for the login
- 11:27:39page. So we can delete it.
- 11:27:42And I'll change this to login.
- 11:27:45So using AI, I'm able to write this code
- 11:27:48a lot faster.
- 11:27:50Another feature of AI is we can give the
- 11:27:53AI instructions.
- 11:27:55For example, let's remove this code for
- 11:27:58now.
- 11:28:00And we can click this copilot icon.
- 11:28:04and it will open this chat window. And
- 11:28:07inside here we can just tell the AI what
- 11:28:11we want to do. For example, in here we
- 11:28:14can say in the header
- 11:28:18add a link to the login page and make
- 11:28:24sure in this dropdown you have agent or
- 11:28:28edit selected
- 11:28:30and we'll press enter.
- 11:28:33and the AI will make edits to our code.
- 11:28:38So here you can see that it added a
- 11:28:40login link like before. So we can press
- 11:28:44keep to keep these changes or undo if we
- 11:28:47don't like these changes. So this looks
- 11:28:49good to me. So I'm going to press keep.
- 11:28:54And again I'll go in here and modify it
- 11:28:56a bit. So here it added the login link
- 11:29:00after the orders link but I want to put
- 11:29:02this before the orders. So I can just
- 11:29:05select this code and cut
- 11:29:09and at the top
- 11:29:12paste that code.
- 11:29:16So, as you can see, by using AI, we can
- 11:29:19write React code faster. But you'll
- 11:29:22notice that we still need to understand
- 11:29:24the code to check if it's what we want
- 11:29:27and to modify the code if we need to.
- 11:29:30So, it's important to learn how to code
- 11:29:33first and then we can use AI to help us
- 11:29:36code faster. So now I'll go into the git
- 11:29:40section and I'm actually going to undo
- 11:29:43these changes because this was just an
- 11:29:46example.
- 11:29:50And that's the end of this lesson. In
- 11:29:52this lesson, we learned some of the
- 11:29:54updates in the new version of React. We
- 11:29:58learned how to set up the React compiler
- 11:30:00to automatically optimize our React
- 11:30:03code. We set up a new Typescript React
- 11:30:07project using create vit. We did an
- 11:30:10intro to TypeScript and learned basic
- 11:30:13features like type checking and type
- 11:30:15inference. We converted our e-commerce
- 11:30:18project to TypeScript.
- 11:30:21We learned how to use TypeScript with
- 11:30:23React. And we learned how to use AI with
- 11:30:27React to help us write code faster. Here
- 11:30:32are some exercises to help you practice
- 11:30:34what we learned in this lesson.
- 11:31:09Good job on finishing this React
- 11:31:11tutorial. If you want to continue
- 11:31:13learning with me, you can check out the
- 11:31:15premium version of this course where I
- 11:31:17will be adding extra lessons and
- 11:31:19projects in the future. Currently, the
- 11:31:22premium version has 11 lessons, but I'll
- 11:31:24be adding more lessons in the future.
- 11:31:27You can also check out the membership
- 11:31:29for extra content on AI and backend.
- 11:31:33As the next step after learning React, I
- 11:31:36recommend moving to the back end and
- 11:31:38learning the command line and NodeJS.
- 11:31:41I'll be putting my work in progress on
- 11:31:43these technologies inside the membership
- 11:31:45first, so be on the lookout for that.
- 11:31:49Thanks so much for watching and taking
- 11:31:50this course with me and I'll see you in
- 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.