background top icon
background center wave icon
background filled rhombus icon
background two lines icon
background stroke rhombus icon

Download "React Full Course for free ⚛️ (2024)"

input logo icon
Table of contents
|

Table of contents

0:00
1..React tutorial for beginners ⚛️
20:26
2..card components
32:24
3..add CSS styles
40:40
4..props
52:49
5..conditional rendering
1:03:04
6..render lists
1:29:43
7..click events
1:42:04
8..useState() hook
1:58:36
9..onChange event handler
2:13:16
10..color picker app
2:23:31
11..updater functions
2:30:45
12..update OBJECTS in state
2:39:55
13..update ARRAYS in state
2:48:55
14..update ARRAY of OBJECTS in state
3:01:42
15..to-do list app
3:24:17
16..useEffect() hook
3:44:08
17..digital clock app
4:00:08
18..useContext() hook 🧗‍♂️
4:11:44
19..useRef() hook 🗳️
4:23:01
20..stopwatch app
Video tags
|

Video tags

react js
react tutorial
learn react
react course
reactjs tutorial
react crash course
react js tutorial
reactjs course
react tutorial for beginners
react.js
reactjs tutorial for beginners
reactJS
react
learn reactjs
tutorial
javascript
crash course
web development
learn react js
reactjs
course
Subtitles
|

Subtitles

subtitles menu arrow
  • ruRussian
Download
00:00:00
hey what's going on everybody it's your
00:00:01
bro hope you're doing well and in
00:00:03
today's video I'm going to show you
00:00:04
everything you need to know to get
00:00:05
started working with reactjs so why
00:00:08
don't you go ahead and sit back relax
00:00:10
and enjoy the
00:00:12
show react is a JavaScript library not
00:00:16
framework that's used to easily build
00:00:18
and arrange user interfaces for web
00:00:21
applications react focuses on building a
00:00:23
web application using these things
00:00:25
called components a component is a
00:00:27
self-contained section of code that
00:00:29
functions as as a reusable building
00:00:31
block think of Legos you could say a
00:00:32
Lego is similar to react component each
00:00:35
Lego represents a section of reusable
00:00:37
JavaScript and HTML code we can build a
00:00:39
diarama or other complicated structure
00:00:42
from these Legos if we use enough of
00:00:44
them and in the correct places react
00:00:46
uses a syntax extension of JavaScript
00:00:48
known as jsx meaning JavaScript XML jsx
00:00:52
allows you to write HTML like code
00:00:55
within your JavaScript files react also
00:00:57
utilizes a virtual Dom it's a
00:00:59
lightweight version of the real Dom of a
00:01:01
web page we can keep track of any
00:01:03
changes made to the virtual Dom and only
00:01:05
apply that specific change to the real
00:01:07
Dom without needing to refresh the
00:01:09
entire web page only that specific
00:01:11
section this reduces rendering
00:01:13
performance overhead before we do begin
00:01:15
you will need to know JavaScript since
00:01:17
react is a JavaScript library everything
00:01:19
up to arrays classes objects and es6
00:01:22
feature such as Arrow functions will be
00:01:24
necessary HTML will also be mandatory
00:01:27
since our react components involve
00:01:29
rendering HTML elements and applying CSS
00:01:32
I do have full free courses on my
00:01:33
YouTube channel on these topics if you
00:01:35
need to catch up or you need a refresher
00:01:37
so that's a quick overview of react
00:01:38
we'll move on to the installation
00:01:40
instructions next we're going to
00:01:42
download nodejs from this URL nodejs.org
00:01:46
node.js is a backend JavaScript runtime
00:01:49
environment it executes JavaScript code
00:01:51
outside of a web browser we're mostly
00:01:53
interested in the node package manager
00:01:55
that comes with it let's download the
00:01:58
latest version of node.js
00:02:01
open it when the download is
00:02:06
complete select next read the license
00:02:09
agreement yes I did read it that fast
00:02:12
next select a destination folder I'll
00:02:15
keep it as
00:02:16
is you can define a custom setup but
00:02:19
that's outside of the scope of this
00:02:21
tutorial series we'll press next we'll
00:02:24
skip tools for Native modules press next
00:02:28
and install
00:02:32
then
00:02:33
finish you'll also need a text editor I
00:02:36
recommend vs code since you've made it
00:02:39
this far in my web development series
00:02:41
I'm assuming you already have one
00:02:42
downloaded which you can get from this
00:02:44
site code dovisual studio.com and select
00:02:46
the correct download for your operating
00:02:48
system within VSS code I'm going to
00:02:51
create a project folder let's go to
00:02:53
explore open folder just for convenience
00:02:57
I'll place my project folder on my
00:02:59
desktop
00:03:00
I'll create a new folder I'll name mine
00:03:04
website then we will select this
00:03:07
folder we're going to need to open up
00:03:09
command prompt if you're using Windows
00:03:11
or terminal if you're on Mac you can
00:03:13
also do this within vs code go to the
00:03:15
hamburger menu up top terminal new
00:03:21
terminal we within our website folder we
00:03:24
will type the command npm meaning node
00:03:27
package
00:03:28
manager
00:03:30
create vit at latest meaning latest
00:03:34
version V's a development server it's a
00:03:38
modern replacement to create react app
00:03:40
which is now
00:03:42
outdated hit
00:03:44
enter we'll need a name for this project
00:03:47
a common naming convention for a user's
00:03:49
first react project is my react app
00:03:54
that's a common name that you'll see
00:03:56
enter select a framework using the arrow
00:03:59
keys we can select whatever framework we
00:04:01
would like to use we will select react
00:04:04
hit enter are we going to use typescript
00:04:07
or JavaScript we'll stick with plain
00:04:10
JavaScript all right here are the last
00:04:12
few commands and they are given to us
00:04:15
we'll change the directory to our react
00:04:18
app CD meaning change directory my react
00:04:23
app and that's the name of the folder
00:04:25
where our project is
00:04:27
located npm install that is the second
00:04:35
command the last command is npm runev to
00:04:39
start the development server npm
00:04:43
runev all right so our web server is
00:04:45
found at this address let's copy it then
00:04:48
open up a web browser let's paste that
00:04:51
address and here we are within our
00:04:53
development server this is a sample
00:04:56
project that's given to us it even
00:04:58
includes this little counter
00:05:01
hey this is future bro I was editing
00:05:03
this video and I totally forgot to
00:05:05
mention how you can restart the server
00:05:07
if you close out of it all you got to do
00:05:09
is open up a terminal let's go to
00:05:10
terminal new terminal we have to be sure
00:05:13
that we're within our application folder
00:05:15
I'm going to change my directory by
00:05:17
typing CD the name of the folder my
00:05:21
react
00:05:23
app then npm run Dev to start the
00:05:27
development
00:05:28
server
00:05:30
and let's refresh that yep it seems like
00:05:33
it's working so yes I forgot to mention
00:05:35
that that is all I'm quickly going to
00:05:37
cover the project structure of our react
00:05:39
application we'll begin with the node
00:05:41
modules folder this folder contains
00:05:44
external libraries and packages that our
00:05:46
project relies on a few of which are
00:05:49
build tools utility libraries routing
00:05:51
libraries just to name a few we won't be
00:05:55
spending any time directly in this
00:05:56
folder but you should still be aware
00:05:58
what it's for if it just contains
00:06:00
external libraries and packages that our
00:06:02
project relies on next we have the
00:06:04
public folder the public folder contains
00:06:07
any public assets one of which in here
00:06:10
is a vit image it should be this image
00:06:12
right here this folder can contain
00:06:15
public fonts images videos they're not
00:06:18
bundled during the final output they're
00:06:20
typically available via a URL just to
00:06:23
demonstrate I'm going to delete this
00:06:25
image file and see what
00:06:27
happens yep and that logo is gone so my
00:06:31
vit logo was a public asset via a URL
00:06:35
and let's put that
00:06:36
back then we have the source folder
00:06:39
we'll spend 99% of our time within the
00:06:42
source folder we have an assets folder
00:06:44
that can contain images and videos the
00:06:47
assets folder is very similar to the
00:06:49
public folder but files within the
00:06:51
assets folder are bundled during the
00:06:53
final output public assets are not and
00:06:55
they're generally available via a URL my
00:06:59
re act image is within the assets folder
00:07:02
currently we have a main jsx file
00:07:05
remember that jsx is Javascript XML in a
00:07:08
way this functions as our main
00:07:10
Javascript file react works with
00:07:13
components we're adding a single
00:07:14
component an app component which we are
00:07:18
importing so let's take a look at our
00:07:20
app component the app component in this
00:07:23
example you could say is the root
00:07:24
component we have two different
00:07:26
stylesheets a stylesheet for app and we
00:07:30
have an index CSS stylesheet this is the
00:07:32
main CSS file for our application we're
00:07:35
importing this file from our main jsx
00:07:38
file right here so that's the source
00:07:40
folder then we have an index HTML file
00:07:43
this is the main entry point into our
00:07:45
program within the body of our document
00:07:48
we have a development with an ID of root
00:07:51
as well as a script to our main jsx file
00:07:54
a few other things you should know about
00:07:56
is this package Json file Json file
00:07:59
files are structured in key value pairs
00:08:01
this file contains metadata about our
00:08:03
project such as the project name the
00:08:06
version number what build we're using
00:08:08
we're using vit and the react version
00:08:11
number just to name a few all right
00:08:14
everybody so in just a moment we will be
00:08:15
creating our first react component we
00:08:18
already do have one component that is
00:08:20
app right now this is serving as a
00:08:22
sample project for us then we are
00:08:24
importing app to main jsx this is our
00:08:28
main J s x file think of it as the main
00:08:30
JavaScript file we are creating a root
00:08:33
element and including our app component
00:08:37
within our app component we do have a
00:08:39
function delete everything within this
00:08:45
function that should eliminate our
00:08:47
sample
00:08:49
project and we no longer need these
00:08:52
Imports we can delete them
00:08:54
too but you do want to keep this export
00:08:57
statement components can have dedicated
00:08:59
stylesheets which you can see here for
00:09:01
app CSS we are going to delete this
00:09:04
because we no longer need
00:09:06
it we do have an app component that's
00:09:08
going to serve as the root we will add
00:09:10
other components to our app component
00:09:12
our app component ties them all together
00:09:15
to create a new component go to your
00:09:16
Source folder we're going to rightclick
00:09:18
then go to new file we will create a
00:09:21
component for a header the extension is
00:09:25
jsx because it's a JavaScript XML file
00:09:30
we will be working with function based
00:09:33
components we will create a function
00:09:36
with the name of header do pay attention
00:09:39
to the
00:09:40
capitalization then we would like to
00:09:42
export this component so we can import
00:09:44
it elsewhere at the end of this file
00:09:47
type
00:09:48
export default the name of the
00:09:52
component header within our header
00:09:55
function we can write a combination of
00:09:57
JavaScript and HTML
00:09:59
all we're going to do within this
00:10:00
function is return something return
00:10:04
parentheses semicolon within the return
00:10:07
statement you can write pure HTML I will
00:10:10
create a header
00:10:11
element so react components they're only
00:10:14
capable of returning a single element
00:10:17
but you can place children elements
00:10:19
within within our header element I will
00:10:21
create an H1 element with text of my
00:10:27
website just to be sure everything is
00:10:29
working let's save I'm holding contrl s
00:10:32
on Windows we will go to our app
00:10:34
component then import the header
00:10:36
component So within app we will type the
00:10:40
statement import the name of the
00:10:43
component
00:10:45
header from a location that location is
00:10:49
going to be do SL header.
00:10:54
jsx we can now use this header component
00:10:57
within our app component we will also be
00:10:59
returning something so we will need a
00:11:02
return statement return parentheses
00:11:04
semicolon what are we going to return I
00:11:08
would like to use my header component so
00:11:10
to add a component we need a set of
00:11:13
angle brackets then type the name of the
00:11:15
component
00:11:18
header like this they're a pair of two
00:11:22
and the second one is closed that's why
00:11:24
we have a forward slash preceding it we
00:11:26
now have a header component you can
00:11:29
shorten the syntax as well take your
00:11:32
component end it with the forward slash
00:11:35
that also works too it's a shorthand way
00:11:37
of writing the same thing let's head
00:11:39
back to our header component We'll add a
00:11:41
few more
00:11:42
elements after our H1 element let's
00:11:45
create a navigation
00:11:47
element then within the navigation
00:11:49
element we'll create an unordered
00:11:52
list within the unordered list we'll
00:11:54
create a few list
00:11:57
items the first will be
00:12:01
home these are list items you would
00:12:03
typically see for a navigation bar
00:12:07
home
00:12:11
about
00:12:15
services and
00:12:17
contact we now have our unordered list
00:12:21
I'll enclose our list items within
00:12:23
anchor tags to make them
00:12:25
hyperlinks a hre equals
00:12:29
I don't want these to go anywhere I'm
00:12:30
just going to leave these empty I'll add
00:12:33
a pound
00:12:34
sign and then be sure to close these
00:12:37
anchor
00:12:38
tags we'll place our text within the
00:12:40
anchor tag so they are now hyperlinks
00:12:43
okay let's do that for the other list
00:12:53
items there we are then after my
00:12:56
navigation element I will add a
00:12:58
horizontal rule to make it look
00:13:00
nice okay there is our header component
00:13:04
we no longer need to work with this
00:13:05
header component we do have some styling
00:13:08
already applied those Styles can be
00:13:09
found within index.css if you would like
00:13:13
to start fresh and eliminate all of
00:13:14
these Styles you can delete everything
00:13:16
from here then you do have HTML with no
00:13:20
CSS in a future video I'll show you
00:13:22
various ways in which we can apply CSS
00:13:24
to a react component but for now we'll
00:13:26
just keep this the same our head element
00:13:29
is now complete we will create a new
00:13:31
element for a footer again go to our
00:13:34
source folder right click go to new
00:13:37
file
00:13:38
footer.
00:13:41
jsx we are creating a function based
00:13:44
component it's a function that's going
00:13:47
to return a footer
00:13:50
component then at the end of our script
00:13:53
we will export default the name of the
00:13:56
component footer so that we we can
00:13:58
import it elsewhere our footer is going
00:14:01
to return something so we need a return
00:14:04
statement then within the set of
00:14:06
parentheses we can return a single
00:14:08
element we will return a footer
00:14:13
element but remember you can place
00:14:15
children elements within an element it
00:14:17
still only counts as one then so what do
00:14:20
we want to do for our
00:14:21
footer I'll include a paragraph
00:14:27
element
00:14:29
within the paragraph element I'm going
00:14:30
to add a copyright symbol to add a
00:14:32
copyright Mark Type ampersand copy semi
00:14:36
call
00:14:37
in then afterwards I'll add your website
00:14:43
name all right now we need to import
00:14:46
this footer going back to our app
00:14:49
component we will
00:14:51
import
00:14:53
footer from footer.
00:14:57
jsx then I would like to include a
00:15:00
footer component after the
00:15:03
header now pay attention to
00:15:06
this we're returning two
00:15:09
components we have this warning we only
00:15:12
have the capability of returning a
00:15:14
single component that's how our return
00:15:16
statements are designed jsx elements
00:15:18
must be wrapped in an enclosing tag do
00:15:21
you want a jsx fragment so since we only
00:15:25
have the capability of returning a
00:15:26
single enclosing tag we're we're going
00:15:28
to enclose our components with what's
00:15:30
known as a
00:15:32
fragment it's just an empty set of ankle
00:15:35
brackets so we will enclose all
00:15:37
components within a
00:15:40
fragment and that should work we have
00:15:42
our footer component following our
00:15:44
header component we have our
00:15:47
header and our footer let's make a few
00:15:50
more changes to our
00:15:52
footer I'm going to zoom out a little
00:15:55
bit within our return statement we can
00:15:57
insert some Javas script to insert some
00:16:00
JavaScript you need a set of curly
00:16:02
braces I will create a new date object
00:16:06
this is some JavaScript code
00:16:09
new date parentheses I would just like
00:16:13
the year I will follow this object with
00:16:16
get full year method let's save and take
00:16:20
a look so I'm filming this video near
00:16:23
the end of 2023 your web application
00:16:26
should return the current year when ever
00:16:28
you're watching this all right our
00:16:30
footer component is now
00:16:32
done I'm going to create a component for
00:16:35
a unordered list to sandwich between our
00:16:37
header and our footer I'll create a
00:16:40
component for food we will go to our
00:16:42
source folder create a new file this
00:16:46
component will be food.
00:16:50
jsx again we are creating a function
00:16:53
based component with the name of
00:16:57
food then we will export this component
00:16:59
when we're done export default
00:17:03
food be sure to add a return
00:17:07
statement before we add anything to the
00:17:09
return statement I will create some
00:17:11
JavaScript variables so normally if
00:17:14
you're adding JavaScript code within the
00:17:16
return statement you need a set of curly
00:17:18
braces but outside of the return
00:17:20
statement you don't I will create two
00:17:23
JavaScript constants const food one pick
00:17:28
some food for me I will pick an
00:17:31
orange let's make that
00:17:34
Capital all right then let's create a
00:17:36
second food
00:17:38
variable food 2 for me will be a
00:17:42
banana within the return statement we
00:17:44
can utilize these
00:17:46
constants what do we want to return
00:17:49
exactly I will return an unordered
00:17:54
list we'll have a few list
00:17:57
items three to be
00:18:00
exact my first list item I'll just add
00:18:03
some text I'll add an
00:18:05
apple just to be sure that everything is
00:18:08
working let's import this food component
00:18:10
from our app
00:18:12
component again we need an import
00:18:14
statement import food from food. jsx we
00:18:20
can now use the food component we will
00:18:23
add that after the header we will add
00:18:27
food then here is our first list item an
00:18:30
apple okay let's head back to our food
00:18:33
component for our second list item I
00:18:35
will add food one that's our constant
00:18:38
now pay attention to this if I type food
00:18:40
one well we're literally printing the
00:18:43
text food one since we're within our
00:18:45
return statement I would like to include
00:18:47
some JavaScript code we're including a
00:18:49
JavaScript variable so I need to enclose
00:18:53
this variable within a set of curly
00:18:55
braces because we're within the return
00:18:57
statement and now now my second list
00:18:59
item is an orange then let's include
00:19:02
food 2 but I'm going to add one more
00:19:04
thing food 2 however I'm going to use
00:19:08
the builtin 2 uppercase method just to
00:19:12
demonstrate that we can use JavaScript
00:19:14
functions all right and there is our
00:19:16
unordered list we have an apple orange
00:19:18
and a
00:19:19
banana so these components are now
00:19:21
complete and the cool thing about react
00:19:23
as well is that we can keep on reusing
00:19:25
these components if we would like I
00:19:27
would like a couple food components so I
00:19:29
will just paste another component we now
00:19:31
have two food components or even three
00:19:34
or
00:19:35
four you can rearrange these components
00:19:38
however you would like by rearranging
00:19:40
them we can change the feel and style of
00:19:42
our web application for demonstration
00:19:44
purposes I will put our footer on the
00:19:46
top that doesn't make any sense but I
00:19:48
just want to demonstrate so our footer
00:19:51
is now on the top followed by the header
00:19:53
then the food component or maybe I would
00:19:56
like the food component on top
00:20:00
we now have our food component footer
00:20:03
component then our header
00:20:06
component all right everybody that is an
00:20:08
introduction to react react is all about
00:20:11
reusing components a component is a
00:20:14
small section of code that can include
00:20:16
JavaScript and HTML your function will
00:20:19
return that code and make it reusable
00:20:21
and that is your introduction to
00:20:25
react hey what's going on everybody and
00:20:28
today's video I'm going to get a little
00:20:29
more in depth with creating components
00:20:31
and by the end of this topic we will be
00:20:33
creating our own card component so sit
00:20:36
back relax and enjoy the
00:20:39
show in today's topic I'm going to get a
00:20:41
little more in depth with react
00:20:43
components as we've discussed in the
00:20:45
previous topic a component is a section
00:20:47
of reusable jsx code it's HTML like code
00:20:51
that can contain JavaScript I thought
00:20:53
today we could create some card
00:20:54
components card components are fairly
00:20:56
popular with react app applications
00:20:59
typically they involve a picture a title
00:21:01
and a description you can use cards for
00:21:03
all sorts of things in the previous
00:21:05
topic we did delete all of the contents
00:21:07
within our app function let's go ahead
00:21:09
and do that now if we haven't already
00:21:11
and we'll delete these Imports we will
00:21:13
not need them for this lesson in the
00:21:15
previous topic we did have a stylesheet
00:21:17
for our app component we can delete this
00:21:19
too we are now ready to begin within our
00:21:23
source folder we will create a new file
00:21:26
the name of the component will be card
00:21:28
this is a jsx file jsx meaning
00:21:31
JavaScript XML our components will be
00:21:34
function based function type the name of
00:21:37
the component card parenthesis then
00:21:40
curly
00:21:42
braces we will be exporting this
00:21:44
component so that we can import it
00:21:46
elsewhere so type export default the
00:21:49
name of the component
00:21:51
card within our card function we will be
00:21:54
returning some HTML like code
00:21:59
we'll begin with the div
00:22:03
element we will be applying some CSS
00:22:06
Styles we will need to set the
00:22:08
class however with jsx class is a
00:22:12
reserved keyword to set an elements
00:22:14
class we have to instead use class name
00:22:17
do pay attention to the capitalization
00:22:19
the N is
00:22:20
uppercase okay we will set this div
00:22:23
section equal to
00:22:26
card so cards typically have an image
00:22:29
we'll include an image element we'll set
00:22:32
the source
00:22:34
later they usually have a title we'll
00:22:36
use an H2 element and a description
00:22:39
we'll include a
00:22:40
paragraph for our H2 title why don't you
00:22:43
go ahead and type in your name I'll type
00:22:45
in my YouTube channel name but feel free
00:22:47
to use your own
00:22:49
name then let's add a description for
00:22:52
the description of Year card type in
00:22:54
what you do for a living or if you're a
00:22:56
student what are you studying for me
00:22:59
personally I make YouTube
00:23:03
videos and add a hobby of viers I make
00:23:07
YouTube videos and play video
00:23:12
games so far so good let's import our
00:23:15
card component just to be sure that it's
00:23:17
working within our app component I will
00:23:20
at the top import the name of the
00:23:22
component card from a file location
00:23:27
these files are right next to each other
00:23:29
so I will
00:23:31
type SLC card.
00:23:36
jsx within our app component we will
00:23:39
return within the return statement list
00:23:42
the component we would like to return we
00:23:45
will return our card component to
00:23:47
include a component within angle
00:23:49
brackets type the name of the
00:23:51
component these are typically in
00:23:54
pairs you need an opening tag and a
00:23:56
closing tag and there's our card so far
00:23:59
we'll be applying some CSS styling
00:24:02
momentarily there is a shortcut in place
00:24:04
of using an opening and closing
00:24:06
tag take the opening tag end it with a
00:24:10
forward slash that's also valid let's
00:24:13
head back to our card component we'll
00:24:15
include an
00:24:16
image within our opening image tag it is
00:24:19
good practice to set the alt attribute
00:24:22
just in case this image can't be
00:24:23
displayed or if somebody is using a
00:24:25
screen reader the alternate text will be
00:24:28
displayed or read out loud will include
00:24:30
a profile picture for us let's set the
00:24:33
alternative text to be profile
00:24:37
picture we're going to set the source
00:24:42
attribute now I recommend pausing the
00:24:44
video and finding a profile picture of
00:24:46
yourself from you know Facebook or
00:24:48
Twitter or whatever you use for social
00:24:50
media pick a smaller picture something
00:24:53
around 200 x 200 pixels would be decent
00:24:56
if you don't want to follow along you
00:24:58
can include a placeholder image just for
00:25:00
this lesson which you can get from this
00:25:02
site this will be an external URL https
00:25:06
colon to slashes Via
00:25:11
placeholder doc1 15 that will give you a
00:25:15
placeholder image otherwise if you have
00:25:18
an image you would like to use I'm using
00:25:20
the profile picture from my YouTube
00:25:21
channel all we got to do is place this
00:25:24
image within our assets
00:25:26
folder
00:25:29
from our card class we will import that
00:25:33
image
00:25:35
import then we need a name for this
00:25:38
import I'll name my import profile pick
00:25:44
from a
00:25:46
location so we need to navigate to the
00:25:49
assets directory then get our
00:25:53
image slash the assets directory slash
00:25:57
the the name of the image so mine is
00:25:59
profile and it's a JPEG yours is likely
00:26:02
going to be different do pay attention
00:26:03
to
00:26:04
that profile.
00:26:07
jpeg for the source
00:26:11
attribute I will set the SQL to a set of
00:26:13
curly braces then we will include our
00:26:16
profile pick that we're
00:26:19
importing there's my profile picture
00:26:22
depending on the dimensions of your
00:26:24
profile picture it might be fairly big
00:26:26
you may want to choose a smaller image
00:26:28
or otherwise within our CSS file we can
00:26:31
change the dimensions of the picture
00:26:33
okay let's head to our CSS stylesheet we
00:26:36
already do have some Global CSS
00:26:38
properties already applied but I'd like
00:26:40
to start fresh let's delete everything
00:26:42
within the CSS stylesheet that should
00:26:45
eliminate all CSS within our web
00:26:47
page we will select the name of the
00:26:50
class card remember that with jsx we use
00:26:54
class name not
00:26:56
class with within our index CSS
00:26:58
stylesheet let's select our card class
00:27:01
I'm going to go through these CSS
00:27:03
properties real quick we should already
00:27:05
have some experience with CSS okay let's
00:27:07
add a border border one pixel
00:27:12
solid I'll set this to be
00:27:14
black I do like using hsl values I'll
00:27:18
pick a light gray
00:27:20
color let's set the lightness to about
00:27:22
80 for the
00:27:25
Border I'll set the Border radi to be 10
00:27:28
pixels to round the corners the corners
00:27:31
should now be rounded as you can see
00:27:33
here I'll add a box
00:27:36
Shadow the first value is for the
00:27:39
horizontal offset the second is for the
00:27:41
vertical offset the third value is for
00:27:44
the blur
00:27:46
radius I'm going to pick a lighter color
00:27:48
though so I'm going to select black
00:27:51
change this to an hsl
00:27:54
value but I'm going to change the
00:27:56
transparency to be
00:28:00
10% so 0.1 that is not bad
00:28:04
looking okay let's add a little bit of
00:28:07
padding 20
00:28:09
pixels padding is found between the
00:28:12
elements and the border so if I were to
00:28:15
increase this we have more padding but
00:28:17
let's stick with
00:28:19
20 I'll add some
00:28:21
margin margin is for the space outside
00:28:24
of the Border let's go with 10 pixels
00:28:28
I'll text align Center to Center the
00:28:30
elements within text align
00:28:33
Center I'll set a Max width for the
00:28:37
card to be 250
00:28:40
pixels then I will set the display to be
00:28:44
an inline
00:28:46
Block in case we have more cards they'll
00:28:49
at least flow within our container if we
00:28:50
have
00:28:51
more because normally divs are Block
00:28:54
Level elements okay so let's work on the
00:28:56
card image
00:28:57
next we'll head back to our card
00:29:00
component
00:29:01
class we'll add a class name to the
00:29:04
image element class name
00:29:10
equals let's say card image go back to
00:29:14
our CSS stylesheet we are accessing our
00:29:18
cards class then access the card image
00:29:21
class
00:29:23
within I will set a Max width I will set
00:29:26
a Max WI width to be 60% of the width
00:29:30
available I will set the height to be
00:29:33
Auto to automatically resize if you
00:29:37
would like a circular image you can set
00:29:39
the Border radius to be
00:29:43
50% I'll add a little bit of margin on
00:29:45
the bottom with margin bottom 10
00:29:51
pixels all right let's work on our title
00:29:53
next going back to our card
00:29:56
component let's add a class
00:30:00
name the class name will be
00:30:03
card-
00:30:05
title going back to our CSS
00:30:09
stylesheet we will select our card class
00:30:12
then select the card title
00:30:16
class with my card title I will set a
00:30:18
font family to be pick a font I like
00:30:22
Ariel I'll set a backup of s serif in
00:30:26
case Ariel can't be displayed for some
00:30:28
reason I'll set margin to be zero to
00:30:32
remove any
00:30:34
margin I'll set the text color to we'll
00:30:37
set the color property to be pick a
00:30:40
color again I'm using hsl
00:30:43
values I'll set the lightness to be
00:30:47
20% now let's change our card text going
00:30:50
back to our card
00:30:52
component within our paragraph element
00:30:54
we will set the class name to equal
00:30:59
card-
00:31:01
text going to our CSS
00:31:03
stylesheet we will access the card class
00:31:07
then access the card- text
00:31:11
class I'll set the font family to be
00:31:13
aerial as
00:31:15
well and I will change the
00:31:18
color I'll set the lightness to be 40%
00:31:21
again I'm using hsl
00:31:23
values uh maybe
00:31:25
30 I would say that looks decent enough
00:31:29
we have now created and styled a card
00:31:32
component within our app component we
00:31:34
can reuse the card component however if
00:31:37
I were to add
00:31:38
another Well jsx elements must be
00:31:41
wrapped in an enclosing tag our return
00:31:43
statements are only designed to return a
00:31:46
single element you can enclose all
00:31:48
elements within what is known as a
00:31:50
fragment it's an empty set of angle
00:31:54
brackets then technically we're only
00:31:56
returning a single El element we now
00:31:58
have two cards however they are
00:32:00
identical though and we can use this
00:32:02
card component whenever we want I can
00:32:04
include four if I would like all right
00:32:07
everybody so that is us making a card
00:32:09
component in the next video I'll discuss
00:32:11
props where each component can have
00:32:13
different values because we don't want
00:32:15
all of our components to look the same
00:32:17
and that was some react practice by
00:32:19
making a card
00:32:24
component yo what's going on everybody
00:32:27
so in today's video I'm going to show
00:32:28
you a few ways in which we can style
00:32:30
react components with CSS this topic
00:32:33
doesn't include external Frameworks such
00:32:35
as Tailwind or pre-processors such as
00:32:38
SAS I'm going to cover a few of the more
00:32:40
basic techniques including external
00:32:43
modules and inline CSS styling we will
00:32:46
need a component to work with let's
00:32:47
begin with a button component let's go
00:32:49
to our source folder new file button.
00:32:54
jsx we'll create a function based comp
00:32:57
component because we like function based
00:32:59
components be sure to export it because
00:33:02
I sometimes forget to do that so let's
00:33:04
do it now export default the name of the
00:33:08
component then we will
00:33:10
return a single button
00:33:16
element with text that says click me
00:33:20
normally when setting a CSS class you
00:33:22
use the keyword class however in jsx
00:33:25
that's a reserved keyword
00:33:27
we'll instead use class name with a
00:33:30
capital N I'll set my class name to be
00:33:35
button all right then we just need to
00:33:38
import this button component going back
00:33:40
to our app component that's the parent
00:33:43
we will import button from its
00:33:48
location SL button. jsx and now we have
00:33:53
a button component to work
00:33:55
with there's my button let's begin with
00:33:58
external CSS styling we should already
00:34:00
be a little familiar with it already
00:34:02
heading to our index CSS stylesheet we
00:34:05
can apply some Global Styles we will
00:34:07
select the class name of button dot to
00:34:10
select a class button let's apply the
00:34:14
following CSS properties but feel free
00:34:16
to make any changes or alterations I
00:34:19
will begin with a background color I've
00:34:21
already pre-picked a color personally I
00:34:23
like using hsl
00:34:25
values 200 for the Hue 100% for the
00:34:31
saturation 50% for the lightness that
00:34:34
will give you a light blue color for the
00:34:36
font color that would be the color
00:34:38
property I will set that to be
00:34:41
white for
00:34:43
padding let's say 10 pixels by 20
00:34:48
pixels border radius to round the
00:34:50
corners I will set that to be five
00:34:54
pixels border none to remove the
00:34:58
Border then let's change our cursor to
00:35:01
be a pointer when we hover over the
00:35:04
button that's not a bad looking button I
00:35:07
am zoomed in to like 300% just for this
00:35:10
demonstration all right we have utilized
00:35:12
an external CSS stylesheet they're easy
00:35:15
to use for simple projects such as
00:35:17
making a YouTube video tutorial it gives
00:35:20
you flexibility with media queries and
00:35:21
pseudo selectors you can use external
00:35:24
stylesheets to apply Global Styles
00:35:26
throughout your application however
00:35:28
using external stylesheets can lead to
00:35:30
naming conflicts especially when you
00:35:32
have more components and classes to work
00:35:33
with if you have a large web application
00:35:36
you'll likely have all sorts of
00:35:37
different buttons you would need a
00:35:39
strong naming convention and good
00:35:41
organization two buttons you may create
00:35:44
might have the same class Name by
00:35:45
mistake and with large applications it
00:35:47
might be difficult to keep track of all
00:35:49
the different class names moving on to
00:35:51
Method two let's cut all of these
00:35:53
properties save we're going to create a
00:35:56
module now that's the second method
00:35:59
we'll create a dedicated CSS stylesheet
00:36:02
specifically for each component what you
00:36:05
may see some developers do is they'll
00:36:07
create a new folder specifically for
00:36:09
their component let's create a new
00:36:12
folder which we will name button the
00:36:14
name of our
00:36:15
component let's move our button
00:36:17
component to the button folder let's
00:36:20
move it be sure to update any Imports as
00:36:23
well my app component is now importing
00:36:26
the button component from the button
00:36:29
directory within our button folder we'll
00:36:32
create a new file this will be a CSS
00:36:34
stylesheet which we will name the name
00:36:37
of the component in this case
00:36:39
button. module.
00:36:43
CSS let's paste all of those properties
00:36:45
that we cut
00:36:46
before now from our button component we
00:36:50
will need to import this module at the
00:36:53
top we will import styles
00:36:57
from the location of that
00:36:59
module
00:37:01
SL
00:37:03
button. module.
00:37:06
CSS for our class name we will use a set
00:37:09
of curly braces to use a dynamic
00:37:13
value our class name will equal our
00:37:16
import of styles dot the name of the
00:37:19
class
00:37:23
button and there's our button again the
00:37:26
thing about modules is that it avoids
00:37:28
naming conflicts because a unique class
00:37:30
is going to be generated for you via a
00:37:32
hashing algorithm if I were to right
00:37:34
click on this button and go to
00:37:37
inspect this class name is unique it's
00:37:40
generated via a hash so with modules we
00:37:43
don't have to worry about naming
00:37:45
conflicts modules are also convenient if
00:37:47
each component has its own unique style
00:37:50
however a few of the downsides though
00:37:52
with using modules is that it does
00:37:54
require additional setup and Global
00:37:56
Style aren't applied easily you would
00:37:58
have to import them from elsewhere and
00:38:00
that's a whole thing let's remove our
00:38:02
button component from the button
00:38:03
directory let's put it back in
00:38:06
source and delete our button
00:38:10
directory going to our button component
00:38:13
then remove the import as well we don't
00:38:15
need that anymore now we're going to use
00:38:17
inline
00:38:19
Styles what you see some people do is
00:38:21
they'll create a JavaScript object named
00:38:25
styles Styles equals a set of curly
00:38:28
braces for an object let's paste all of
00:38:31
those CSS properties we're within jsx
00:38:34
code right now we can't use any dashes
00:38:37
we'll switch to a camel case naming
00:38:43
convention all values Should Be
00:38:55
Strings each property should be comma
00:39:03
separated let's remove our class
00:39:06
name we will set the style attribute to
00:39:10
equal use a set of curly braces to
00:39:13
insert a dynamic value we will insert
00:39:16
our Styles object that contains all of
00:39:18
these CSS properties and now we have
00:39:21
done inline CSS styling inline CSS
00:39:24
styling is convenient and easy easy to
00:39:27
understand it prevents Global style
00:39:28
conflicts because we're not working with
00:39:30
class names it's great for isolated
00:39:32
components with minimal styling such as
00:39:35
a like button or a subscribe button
00:39:37
however inline styling can be
00:39:39
increasingly Less maintainable in large
00:39:41
applications it reduces the readability
00:39:44
of your components especially if you
00:39:45
have a lot of CSS properties it's not
00:39:48
great for any complex styling such as
00:39:50
responsive CSS it tends to be better for
00:39:52
components with minimal styling all
00:39:55
right everybody so those those are three
00:39:56
different ways in which we can style
00:39:58
react components with CSS external which
00:40:01
is great for Global Styles or small
00:40:03
projects modules which is preferred for
00:40:06
individual components that have their
00:40:08
own unique Styles and inline which tends
00:40:11
to be good for any small components with
00:40:13
minimal styling use what's best for your
00:40:15
project or your own personal preference
00:40:17
or whatever your team prefers if you're
00:40:19
working on a project together there's no
00:40:21
one siiz fits-all approach unfortunately
00:40:23
myself I prefer to use modules but
00:40:25
throughout the series I'm going to be
00:40:27
using a lot of external CSS just because
00:40:29
our projects are really small and well
00:40:31
everybody those are a few ways in which
00:40:33
you can style react components with
00:40:39
CSS hello everybody so today I got to
00:40:42
explain props props short for properties
00:40:45
they are readon properties that are
00:40:47
shared between components a parent
00:40:50
component can send data to a child
00:40:52
component when you include a component
00:40:54
within a parent you can send that that
00:40:56
child component key value pairs in the
00:40:59
last video we made cards but all of our
00:41:01
cards were identical using props we can
00:41:04
send a child component individual values
00:41:07
so that they're all unique let's go over
00:41:09
an example we're within our app
00:41:11
component I will create a new
00:41:13
component let's go to our source folder
00:41:16
go to new file we will create a
00:41:18
component for students to display
00:41:20
student data student.
00:41:25
jsx we need to create a function
00:41:27
function student parenthesis curly
00:41:30
braces then don't forget to export
00:41:33
default the name of the component
00:41:35
student in order for this component to
00:41:38
accept props this function needs a props
00:41:41
parameter props is going to be a
00:41:43
JavaScript object so let's add our
00:41:46
return statement we'll include a few
00:41:48
HTML
00:41:50
elements we'll start with a
00:41:52
div within our div I will include a
00:41:55
paragraph element
00:41:57
within our paragraph We'll add the text
00:41:59
Name colon space we'll include a set of
00:42:03
curly braces to inject some JavaScript
00:42:06
let's take our props this is an object
00:42:09
access the name property however we
00:42:12
still need to pass in a value for the
00:42:14
key name so heading back to our app
00:42:17
component we need to import the student
00:42:19
component let's do so at the top import
00:42:23
student from the location where the
00:42:26
student file is
00:42:29
found/ student.
00:42:33
jsx let's include a student component
00:42:36
within our app
00:42:38
component
00:42:40
student now if I would like to send some
00:42:43
data to my student component student is
00:42:45
going to be the child in this case app
00:42:47
is the parent we'll need to list key
00:42:50
value pairs the key will be name equals
00:42:54
some value for the value associated with
00:42:57
the name key let's say
00:43:00
SpongeBob when I save and update
00:43:02
everything we have the name
00:43:05
SpongeBob when we send key value pairs
00:43:07
to a component they're all stored within
00:43:10
this props object to get the value
00:43:12
associated with the key you type props
00:43:15
dot the name of the key that will give
00:43:18
you the associated value on my web page
00:43:20
I'm going to zoom in a little bit just
00:43:21
so you can see it that's good for now
00:43:24
heading back to our app component
00:43:26
let's send another key value pair let's
00:43:28
send an age if you're going to store
00:43:31
some data as a value if it's not a
00:43:33
string literal you got to enclose it
00:43:35
within curly braces let's say Spongebob
00:43:38
his age is 30 we now have access to this
00:43:41
age key heading back to our student
00:43:44
component let's create a new
00:43:46
paragraph age colon space to include
00:43:50
some JavaScript we got to use a set of
00:43:52
curly braces we are accessing the props
00:43:55
object do access the key of age
00:44:00
SpongeBob's age is
00:44:01
30 okay let's send is student is
00:44:04
SpongeBob a student we'll create a key
00:44:07
of is student equals add a set of cly
00:44:12
braces is student will be a Boolean true
00:44:15
or false SpongeBob is in boting school
00:44:17
currently he is a student let's save
00:44:21
heading back to our student component
00:44:24
I'll include a new paragraph
00:44:27
student colon space curly
00:44:31
braces props do is student SpongeBob is
00:44:36
a student that is true however our
00:44:39
Boolean is not displaying so with a
00:44:42
Boolean I recommend using the trary
00:44:45
operator if is student within props is
00:44:49
true then we'll return a string literal
00:44:52
of yes otherwise we'll return a string
00:44:56
literal of
00:44:57
no let's update everything yes SpongeBob
00:45:01
is a student you can place complex
00:45:04
JavaScript code within your HTML code
00:45:06
booleans don't display directly to the
00:45:08
Dom you typically find them used within
00:45:11
tary operators or some sort of
00:45:12
conditional if some value is true return
00:45:16
or do this if not do something else now
00:45:19
if I were to pass in a Boolean value of
00:45:22
false well that will change to no
00:45:25
SpongeBob is not a student let's change
00:45:28
that back to
00:45:29
true now before I include a few more
00:45:31
components I'm going to do a little bit
00:45:32
of CSS styling just so that all of this
00:45:35
is going to be more readable I'm going
00:45:37
to zoom out to 100% where it was
00:45:40
originally within my student component
00:45:43
within the enclosing div element I will
00:45:46
set a class name remember that with jsx
00:45:49
you have to use class name not class
00:45:51
because class is a reserved
00:45:54
keyword let's name this class
00:45:57
student then heading to our CSS
00:45:59
stylesheet index.css feel free to clear
00:46:02
out anything within here I will select
00:46:05
the class of
00:46:07
student I'll change the font family to
00:46:11
be aiel I'll add a backup of s serif in
00:46:15
case aiel can't display for some
00:46:18
reason let's change the font size to 2
00:46:22
em for 200% I'll add a little bit of
00:46:25
padding
00:46:26
10
00:46:28
pixels I'll add a
00:46:30
border border one pixel
00:46:33
solid pick a color I like hsl
00:46:37
values I'll set the transparency down a
00:46:40
little bit to like
00:46:42
80% let's go with like 50%
00:46:45
lightness I'll remove any margin around
00:46:48
the paragraph elements we are accessing
00:46:50
the class student then access any
00:46:53
paragraph
00:46:54
elements margin
00:46:56
zero okay that's not too bad so this is
00:47:00
a student component to display some
00:47:02
student data we'll reuse the student
00:47:04
component but pass in different data
00:47:06
each
00:47:07
time we'll create a new student
00:47:12
component this Student's name will be
00:47:17
Patrick Patrick's age Patrick will be
00:47:22
42 is Patrick a student is student
00:47:26
I will set that to be false let's update
00:47:29
our web application and there is
00:47:31
Patrick's data name Patrick age 42
00:47:35
student no let's add a few
00:47:39
more
00:47:41
student name
00:47:44
equals
00:47:46
Squidward Squidward's age will be
00:47:51
50 is
00:47:54
student Squidward will not be a student
00:47:56
that will be
00:47:58
false then let's add Sandy she'll be the
00:48:01
last
00:48:04
student student
00:48:07
name equals
00:48:10
Sandy sy's
00:48:12
age will be
00:48:16
27 is Sandy a student she will be that
00:48:20
will be true there we are all right we
00:48:23
have created four student components
00:48:26
each has different values depending on
00:48:27
what we pass into props we're going to
00:48:30
move on to prop types prop types prop
00:48:34
types are a mechanism that ensures that
00:48:36
the past value is of the correct data
00:48:38
type for example within props we have an
00:48:41
age we would like to ensure that the age
00:48:44
value passed in is a number not a string
00:48:47
or a Boolean if it isn't then react can
00:48:50
emit a warning within our console mostly
00:48:52
for debugging purposes if you're working
00:48:55
with props it is good practice to also
00:48:57
use prop types so here's how to create
00:49:00
prop
00:49:01
types after our student function we'll
00:49:04
type the name of the component
00:49:07
student. prop
00:49:10
types equals curly
00:49:13
braces now we do have to import prop
00:49:16
types it's found within the node modules
00:49:19
folder where is it this should be an
00:49:22
alphabetical order there it is prop
00:49:24
types
00:49:28
at the top of our jsx file we will
00:49:30
import prop
00:49:32
types
00:49:34
from prop types
00:49:38
prop-types within our prop types I would
00:49:41
like to ensure that any name passed in
00:49:44
is going to be a string if it's not
00:49:46
we'll issue a warning within our
00:49:48
console Name colon
00:49:51
space prop
00:49:53
types. string
00:49:56
let's do this with age age will be a
00:49:58
number age colon
00:50:00
space prop types.
00:50:05
number then is student is student will
00:50:08
be a Boolean is student colon space prop
00:50:13
types dob for
00:50:16
Boolean all right and those are our prop
00:50:18
types heading back to our app component
00:50:21
for SpongeBob's age if I don't pass in a
00:50:23
number let's say I pass in a string a
00:50:25
string of 30 well this won't prevent the
00:50:28
program from running but it will issue a
00:50:31
warning within our console if we
00:50:33
rightclick go to inspect go to
00:50:37
console we can see a warning failed prop
00:50:40
type invalid prop age of type string
00:50:44
supplied to student expected number and
00:50:47
here's a location so this is good for
00:50:49
debugging it won't stop the application
00:50:52
from running just issue a warning it is
00:50:55
good practice ice to include prop types
00:50:56
if you're accepting props then lastly
00:50:59
we're going to discuss default
00:51:02
props default props are default values
00:51:05
for props in case they are not passed
00:51:08
from the parent
00:51:09
component let's say we create a new
00:51:11
student component but we don't pass in a
00:51:13
name we can add a default value such as
00:51:16
guest just in case that value isn't
00:51:18
passed in one way or
00:51:21
another okay let's add default props
00:51:24
down here
00:51:29
student. default
00:51:32
props
00:51:34
equals set of curly braces for the name
00:51:38
the default value will be
00:51:42
guest age will be
00:51:46
zero is
00:51:48
student will be
00:51:51
false to test this let's go to our app
00:51:54
component
00:51:56
we'll create a new student component but
00:51:58
don't pass in any props we'll create an
00:52:01
empty student component with no
00:52:04
props that appears to work the name is
00:52:06
guessed the age is zero student is set
00:52:09
to know so if I were to include a name
00:52:12
let's say name equals
00:52:16
Larry well Larry's name is going to be
00:52:18
set we didn't include any key value
00:52:21
pairs for age or is student we ended up
00:52:23
using the default values zero and no all
00:52:27
right everybody so those are props they
00:52:29
are readon properties that are shared
00:52:31
between components a parent component
00:52:34
can send data to a child component when
00:52:37
you include a component you can send
00:52:39
that component key value pairs which
00:52:41
allows you to reuse that component but
00:52:43
have unique data and those are props
00:52:45
with
00:52:48
react hey sup everybody in today's video
00:52:51
I'm going to give you an introduction to
00:52:54
conditional rendering in react
00:52:56
conditional rendering allows you to
00:52:57
control what gets rendered in your
00:52:59
application based on certain conditions
00:53:01
by utilizing conditional rendering we
00:53:03
can show hide or change components
00:53:06
here's an example we'll create a user
00:53:09
greeting
00:53:10
component let's go to our source folder
00:53:13
new file user greeting and this will be
00:53:18
a jsx
00:53:19
file I will be using function based
00:53:22
components function the name of the
00:53:24
component
00:53:25
user
00:53:26
greeting we will have one parameter
00:53:30
props we learned about props in the last
00:53:32
video before I forget I'm going to
00:53:35
export default the component name just
00:53:39
so that we can import it later heading
00:53:41
back to our app component we will return
00:53:44
a user greeing
00:53:47
component user greeting by using props
00:53:51
we'll send two key value pairs to our
00:53:53
user greeing component
00:53:55
is logged in this will be a Boolean
00:53:59
value we'll check to see if we're logged
00:54:01
in or not and a username username equals
00:54:07
this is a string so we do not need a
00:54:09
placeholder type in your name or
00:54:12
username then we do need to import user
00:54:16
greeting import user greeting from a
00:54:22
location. slash user
00:54:26
greeting.
00:54:31
jsx let's save everything head to our
00:54:34
user greeting component we have these
00:54:36
two key value pairs that are stored
00:54:38
within our props object I'll use an if
00:54:41
statement to check to see if we're
00:54:42
logged in or not if props is an object
00:54:47
we will access props followed by dot is
00:54:52
logged
00:54:53
in is this true or not not if it is then
00:54:57
let's return an H2 element with text of
00:55:02
Welcome I'd like to include some
00:55:04
JavaScript I'll use curly
00:55:06
braces let's take props dot our
00:55:11
username let's see if this
00:55:13
works welcome bro code or whatever your
00:55:16
name
00:55:17
is so what if we're not logged in I will
00:55:20
set is logged in to be
00:55:23
false well nothing appears to happen
00:55:26
going back to our user greeting let's
00:55:29
add an else
00:55:30
statement else we will return an H2
00:55:34
element with text of please log in to
00:55:41
continue please log in to continue
00:55:44
within our function based component we
00:55:46
can check some logic using an if
00:55:48
statement if some condition is true
00:55:50
return this if not return something else
00:55:54
now Technic technically you don't need
00:55:55
the else statement you could just say
00:55:58
return because when you return you exit
00:56:01
this would work
00:56:03
too as you can see I don't think it's as
00:56:06
explicit as using an lse statement
00:56:09
another option is to use the tary
00:56:11
operator which we learned in
00:56:13
JavaScript we will
00:56:17
return check props do is logged in add a
00:56:22
question mark is this true or is it
00:56:24
false
00:56:25
if it's true then we'll return an H2
00:56:27
element that says
00:56:30
welcome I'll add some
00:56:32
JavaScript props do
00:56:36
username I'm going to close this menu
00:56:38
just so we can see if this condition is
00:56:41
true return this otherwise add a colon
00:56:45
we'll return something
00:56:48
else we'll add an H2
00:56:50
element please log
00:56:53
into
00:56:56
continue this statement is pretty
00:56:58
lengthy I'm going to put each part of
00:57:00
the statement on a new line just so we
00:57:02
can read it
00:57:06
easily if is logged in is true return
00:57:10
this H2 element if not return this H2
00:57:14
element currently we're logged in that
00:57:17
is true if I set this to be
00:57:20
false well then we get that login prompt
00:57:23
please log in to continue you'll more
00:57:25
commonly see people use tary operators
00:57:28
rather than IFL statements just because
00:57:29
they're more concise let's apply some
00:57:32
CSS
00:57:33
styling going back to our app component
00:57:35
we'll set is logged in to be
00:57:38
true We'll add a class name to each of
00:57:40
these H2 components class name equals
00:57:45
the first H2 element if our condition is
00:57:47
true we'll have a class name of Welcome
00:57:51
Dash
00:57:53
message
00:57:55
our second H2 element will have a class
00:57:57
name of login D
00:58:01
prompt going to our CSS stylesheet let's
00:58:05
select the class of Welcome
00:58:07
message I'll apply the following CSS
00:58:10
properties let's set the font size to be
00:58:13
2.5
00:58:16
em I'll change the background
00:58:20
color I like using hsl
00:58:23
values
00:58:27
I'll go with this shade of
00:58:29
green let's change the font color to be
00:58:33
white I'll add a little bit of padding
00:58:36
10
00:58:38
pixels I'll add a border radius around
00:58:41
the
00:58:44
corners then set margin to be
00:58:48
zero here is our user greeting component
00:58:52
if the user is logged
00:58:53
in let's style the login prompt class
00:58:58
next do login prompt really I'll just
00:59:01
copy these properties but we'll change
00:59:03
the background
00:59:04
color let's change the background color
00:59:07
to be
00:59:08
red all right let's set is logged in to
00:59:11
be
00:59:12
false then our user greeing component
00:59:14
should have those different CSS
00:59:18
properties please log in to continue if
00:59:21
I set that to be true well then we're
00:59:24
logged in welcome your
00:59:27
username within our user greeting this
00:59:29
tary operator is very verbose here's
00:59:33
another way in which we can use the tary
00:59:35
operator in this
00:59:36
situation let's create two constants
00:59:39
const the first will be welcome
00:59:42
message be sure not to include any
00:59:44
dashes
00:59:46
though separately we'll store these H2
00:59:50
elements I'm getting a copy of this
00:59:52
first H2
00:59:53
element we'll store that within our
00:59:55
welcome
00:59:57
message just so we can read it I'm going
00:59:59
to place each line on a new
01:00:05
line then let's create another
01:00:09
constant const login
01:00:12
prompt be sure to eliminate any dashes
01:00:15
we will be using a camel case naming
01:00:18
convention take our second H2 cut
01:00:23
it paste
01:00:29
it and there we go I find that a lot
01:00:31
easier to
01:00:33
read so we're going to take our
01:00:35
condition is logged in if that's true
01:00:39
we'll return our constant our welcome
01:00:42
message otherwise we'll return our other
01:00:45
constant our login prompt and that will
01:00:48
work still the same let's set that to be
01:00:50
false please log to continue set it back
01:00:53
to be true welcome your username I find
01:00:57
this syntax a lot easier to read now it
01:00:59
is good practice if you're accepting
01:01:01
props to set up prop types just in case
01:01:04
the values that are passed in are not of
01:01:06
the correct data type you'll at least
01:01:08
get a warning for debugging purposes
01:01:10
it's a good habit that we should develop
01:01:12
we learned about prop types in the last
01:01:14
video so at the top let's
01:01:17
import prop types from
01:01:23
prop-types after our function take our
01:01:26
user greeing component do propop types
01:01:32
equals take our keys is logged in the
01:01:36
value with this Associated key should be
01:01:39
a Boolean prop that's capital T types
01:01:44
dob then a string for
01:01:46
username user
01:01:49
name prop types. string all right the
01:01:54
last thing we're going to do is set up
01:01:55
default props what if somebody's logged
01:01:57
in but they don't have a username we'll
01:02:00
set a default for the username as guest
01:02:03
so after our prop types we'll set up
01:02:05
some default props again take user
01:02:08
greeting that's the name of our
01:02:10
component. default
01:02:13
props
01:02:15
equals is logged in will be
01:02:20
false and
01:02:22
username will be G
01:02:25
if there's no username passed in all
01:02:28
right going back to our app component if
01:02:30
a user is logged in but there's no
01:02:33
username let's eliminate that we'll get
01:02:36
the welcome message of Welcome guest now
01:02:39
if a user is not logged
01:02:41
in regardless we don't display the
01:02:44
username all right everybody so that's
01:02:46
conditional rendering you can check some
01:02:48
sort of condition based on if that
01:02:50
condition is true or false we can show
01:02:53
hide or change components there's many
01:02:55
different ways in which you can do that
01:02:57
and well everybody that is an
01:02:58
introduction to conditional rendering in
01:03:03
react hey everybody I have a huge video
01:03:06
for you today today we're going to talk
01:03:08
about rendering lists in reactjs this
01:03:11
can be pretty complex we'll go through
01:03:12
it step by step what we'll do in this
01:03:14
tutorial is create a new component to
01:03:16
hold our list let's go to our source
01:03:19
folder create a new file I'll name this
01:03:21
component list this will be a Js x file
01:03:26
to finish creating this component this
01:03:28
will be a function based
01:03:30
component with the name of list then be
01:03:34
sure to export it export default
01:03:39
list we'll start with something very
01:03:42
simple we'll create an array of fruit
01:03:44
this will be const fruits equals think
01:03:48
of some fruit for me I'll pick an
01:03:51
apple
01:03:53
Orange
01:03:56
banana coconut and a
01:04:01
pineapple let me show you what would
01:04:03
happen if we were to return our array
01:04:06
within our list component we will
01:04:08
return our array of fruits but we will
01:04:12
need to import it going back to the app
01:04:15
component we will import our list
01:04:18
component import list from a location .
01:04:25
SL list.
01:04:28
jsx we need a return statement to be
01:04:30
sure you have one we will return a list
01:04:38
component and here is our list let me
01:04:40
zoom
01:04:41
in it's all one big string Apple orange
01:04:45
banana coconut pineapple heading back to
01:04:47
our list component we need to convert
01:04:49
our array of strings into an array of
01:04:52
list item elements we can use the
01:04:54
built-in map method to do that we'll
01:04:57
create a new array of list items the map
01:05:00
method is going to return a new array
01:05:02
after running it through a function
01:05:04
const list items equals take our
01:05:08
original array a fruits use the built-in
01:05:12
map method then we'll either pass in a
01:05:15
callback a function expression or an
01:05:18
arrow function we'll use Arrow functions
01:05:20
because I like Arrow functions for every
01:05:23
fruit in fruits Arrow meaning do this
01:05:28
we'll create a new list item
01:05:32
element that has the text of our fruit
01:05:36
the array item we're not going to return
01:05:38
our fruits array we're going to return
01:05:40
an unordered
01:05:43
list we're going to insert some
01:05:46
JavaScript with curly braces will
01:05:48
include our list items it's an array of
01:05:52
list items and here's our array whoops
01:05:55
but I forgot to enclose fruit with curly
01:06:00
braces my mistake they much better or if
01:06:05
you prefer we can turn this unordered
01:06:07
list into an ordered list with a pair of
01:06:10
O
01:06:11
tags
01:06:13
o there they're all numbered that's how
01:06:15
to render a simple array if you're
01:06:17
working with an array of strings and
01:06:19
you'd like to sort this array beforehand
01:06:21
you can use the sort method fruit do
01:06:24
sort the sort method will sort an array
01:06:27
in place however this doesn't work with
01:06:29
numbers because we're sorting lexico
01:06:32
graphically we would treat numbers and
01:06:34
symbols as characters more on that later
01:06:37
let's go over example two we'll convert
01:06:40
our array of strings into an array of
01:06:42
objects each object will have a name
01:06:45
property and calories calories per
01:06:48
serving so let's enclose all of our
01:06:50
elements within a set of curly braces to
01:06:52
make them objects
01:07:00
I'm going to place each of these objects
01:07:02
on a new line just so that I can read it
01:07:04
more
01:07:07
easily each of these objects will have a
01:07:09
name property the name property will be
01:07:12
set to the original string for our array
01:07:15
the first object will have a name of
01:07:17
Apple let's add calories too calories
01:07:19
per
01:07:21
serving I did a quick Google search of
01:07:23
some the calories per serving for these
01:07:25
fruits I don't know how accurate these
01:07:27
are but they seemed right our first
01:07:29
object has a name of Apple calories is
01:07:32
set to
01:07:34
95 name orange
01:07:38
calories
01:07:41
45 name
01:07:45
banana calories
01:07:49
105 name
01:07:52
coconut
01:07:56
coconut
01:07:58
calories
01:08:01
159 name
01:08:04
pineapple
01:08:06
calories
01:08:09
37 all right we now have an array of
01:08:12
objects there's a few changes we're
01:08:14
going to make if I run this again our
01:08:17
list isn't rendering I need to display
01:08:19
the name property of each fruit fruit.
01:08:23
name property
01:08:25
we have one issue behind the scenes I'm
01:08:27
going to right click go to inspect then
01:08:30
go to
01:08:36
console warning each child in a list
01:08:39
should have a unique key prop react
01:08:42
wants us to assign a key to each list
01:08:44
item each key should be unique in my
01:08:47
array of objects each fruit has a unique
01:08:49
name I could use that we will set the
01:08:53
key attribute to equal include some
01:08:56
JavaScript fruit. name that warning
01:09:00
should go
01:09:04
away which it did if there's a
01:09:08
possibility that two objects can share
01:09:09
the same name you'll want to use some
01:09:12
sort of unique ID in a real world
01:09:14
scenario if you're pulling data from a
01:09:16
database each row of data is going to
01:09:18
have some sort of unique ID so we're
01:09:20
going to mimic that let's add a new
01:09:23
property for
01:09:24
id id will be one for Apple these will
01:09:28
be in ascending order orange will have
01:09:31
an ID of two banana is
01:09:34
three coconut is four pineapple is
01:09:39
five we'll set the key to be each
01:09:43
ID this would be much better than using
01:09:46
the name you can have a naming conflict
01:09:48
if two objects share the same name like
01:09:51
if these were people you could have two
01:09:52
people named Bob
01:09:54
that warning should be
01:09:57
gone which it is react would like a
01:10:00
unique key for each item just so it can
01:10:02
more easily keep track of items being
01:10:04
inserted and removed along with each
01:10:07
element I'm going to display the
01:10:09
calories next to each
01:10:10
element so we'll make a few changes I'm
01:10:13
going to put these HTML elements on a
01:10:15
new line for
01:10:16
readability after the fruit's name I'm
01:10:19
going to add a
01:10:20
colon a non-breaking space character and
01:10:24
Pand nbsp for space oh then not a
01:10:29
semicolon forgot about
01:10:31
that then we'll insert some
01:10:34
JavaScript fruit. calories for the
01:10:37
calories of each
01:10:38
fruit I'll make it bold I'll enclose our
01:10:42
calories with a pair of bold tags which
01:10:44
is just
01:10:51
B now we're going to sort the items in
01:10:53
our list we'll do that before the map
01:10:56
method I'm going to sort our array of
01:10:59
objects by their name property we'll
01:11:02
take
01:11:03
fruits use the sort method the sort
01:11:06
method will sort an array in place we'll
01:11:09
write a custom sorting function we have
01:11:11
two parameters A and B A for the first
01:11:15
item B for the second then we iterate
01:11:18
over the entire
01:11:19
array we need an arrow meaning do
01:11:22
something to lexicographically sort
01:11:25
string properties within an array we can
01:11:27
use the following method we'll take the
01:11:29
name property of a use the built-in
01:11:33
local or local some people
01:11:36
say compare method I misspelled
01:11:39
that yeah I can't spell
01:11:42
compare to b.n
01:11:45
name that should sort our aray of
01:11:47
objects by their name property for
01:11:50
reverse order let me turn this line into
01:11:53
a comment
01:11:54
this is
01:11:58
alphabetical for reverse alphabetical
01:12:02
order we just have to replace a with B
01:12:06
and B with a there pineapple orange
01:12:09
coconut banana apple with apple being at
01:12:11
the
01:12:12
bottom let's sort by
01:12:15
calories fruits. sort this one's easier
01:12:19
again we have our parameters a comma B
01:12:23
Arrow
01:12:24
the calories of a a.
01:12:26
calories minus b.
01:12:30
calories that one's easy we have
01:12:32
pineapple at the top followed by orange
01:12:34
apple banana
01:12:35
coconut this is in numeric
01:12:39
order for reverse numeric
01:12:45
order reverse
01:12:47
numeric or descending you could say the
01:12:51
calories of B minus the calories of a
01:12:54
now we have coconut at the top with
01:12:56
pineapple at the bottom with the least
01:12:57
amount of
01:12:59
calories in this next section I'm going
01:13:01
to demonstrate to you how we can filter
01:13:03
objects by a certain criteria we'll
01:13:05
filter anything that's greater than 100
01:13:07
calories we'll create a new array of
01:13:09
fruit that has low
01:13:11
calories we'll create a new array const
01:13:14
low cal
01:13:17
fruit equals take our original array of
01:13:20
fruits use the built-in fil
01:13:25
method we'll have one parameter of fruit
01:13:29
examine each fruit in our array of
01:13:32
fruits Arrow then a condition here's the
01:13:36
criteria check the calories property of
01:13:39
our
01:13:41
fruit if it's less than 100 filter it
01:13:45
and add it to a new array instead of
01:13:48
displaying our array of fruits let's
01:13:50
display our new array of low calorie
01:13:52
fruit oh that should be plural local
01:13:55
fruits when we create our array of list
01:13:57
items replace fruits with local fruits
01:14:01
and any instance of fruit with lcal
01:14:04
fruit
01:14:06
singular so we have three instances of
01:14:08
fruit let's replace
01:14:11
those let me clean this up a
01:14:14
little feel free to pause the video if
01:14:16
you need to write this
01:14:18
down we have three fruits that are low
01:14:20
calories the calories is less than
01:14:23
100 let's find any high calorie
01:14:28
fruits we can just copy this line of
01:14:30
code paste it but change the condition
01:14:34
examine the calories of each fruit
01:14:36
filter it out if the calories are
01:14:38
greater than or equal to
01:14:40
100 the name of this array will be high
01:14:43
Cal
01:14:45
fruits replace low cal fruits with high
01:14:48
Cal
01:14:49
fruits replace low cal fruit with high
01:14:53
Cal fruit
01:14:55
singular and do that for the other
01:14:57
instances as
01:15:00
well there we are we have two fruits
01:15:02
that are high in calories bananas and
01:15:05
coconuts that is US using the filter
01:15:07
method to filter out list items let's
01:15:10
replace High Cal fruits with fruits high
01:15:13
Cal fruit with fruit to display our
01:15:16
original
01:15:22
array for the next part of this lesson
01:15:24
we're going to transform this list
01:15:26
component so it's reusable with
01:15:28
different lists currently the way that
01:15:30
we set this up is that each list
01:15:31
component that we create has its own
01:15:33
list of fruit so we're going to make
01:15:36
some
01:15:37
changes let's cut our list of
01:15:41
fruits going to the parent component of
01:15:44
app we'll paste our list of fruits then
01:15:47
pass it as props to our list
01:15:52
component
01:15:54
with our list component we will have a
01:15:57
key of items equals insert some
01:16:02
JavaScript our list of fruits let's also
01:16:06
add a category
01:16:07
key category equals for my category
01:16:12
let's say fruits eventually we'll add an
01:16:15
H3 heading we're now going to send all
01:16:18
of this data to the list component but
01:16:21
we have to set up props with in the list
01:16:24
function we have a parameter of
01:16:28
props we'll access the items of props to
01:16:31
get our
01:16:33
fruit we'll create const item list
01:16:38
equals not to be confused with list
01:16:41
items list items is what we get after we
01:16:44
map
01:16:45
it item list equals props do items and
01:16:50
remember that items is our fruits array
01:16:54
replace fruits with item
01:16:57
list replace fruit with
01:17:00
item do that here here and
01:17:04
here there's our list
01:17:07
again if you're going to sort or use the
01:17:09
filter method be sure it's item list not
01:17:12
fruits because now list is a reusable
01:17:15
component I'll add our Category 2 that's
01:17:18
stored within props const category
01:17:22
equals
01:17:23
props do
01:17:27
category currently we're returning a
01:17:29
single ordered list I'll also include an
01:17:31
H3
01:17:35
element include some JavaScript we need
01:17:37
curly braces add our
01:17:40
category however with our return
01:17:42
statement we can only return a single
01:17:44
element or many elements that are
01:17:46
wrapped within a fragment let's create a
01:17:49
fragment that will enclose all of our
01:17:52
markup
01:18:00
there we
01:18:01
are going back to our app component
01:18:04
let's create a new list just to make
01:18:06
sure that our list component is
01:18:09
reusable let's copy our array of
01:18:13
fruits paste
01:18:15
it we'll create an array of
01:18:21
vegetables the IDS will be 6 7 8 9
01:18:27
10 the name of my first item will be
01:18:31
potatoes calories 110 per
01:18:34
serving
01:18:36
celery calories will be
01:18:41
15 carrots calories
01:18:46
25 corn calories
01:18:51
63 broccoli
01:18:55
calories
01:18:57
50 now we should be able to create a new
01:18:59
list
01:19:01
component within our return statement
01:19:03
we're going to need a fragment
01:19:10
again we'll return a second list item
01:19:14
component the items will be vegetables
01:19:18
the category will be a string of
01:19:21
vegetables Bo boom there we go here's
01:19:24
our second list
01:19:25
component so our list component is now
01:19:28
reusable we can pass in many different
01:19:30
types of lists to make this look better
01:19:33
let's apply some CSS styling we'll have
01:19:36
to set a class name
01:19:38
first our H3 element will have a class
01:19:41
name equal
01:19:43
to list-
01:19:47
category our ordered list will have a
01:19:50
class name of list items
01:19:56
all right going to our CSS stylesheet
01:19:59
index.css let's work on our list
01:20:02
category
01:20:03
first use dot to select a class list
01:20:08
category I'm just going to run through
01:20:09
these real quick I'm going to make sure
01:20:12
I'm zoomed in to 100 because I was
01:20:14
zoomed in
01:20:15
beforehand font size 2.5
01:20:20
em font weight will be
01:20:26
gold pick a
01:20:28
color I'm going to stick with hsl values
01:20:32
I'll set the lightness to be
01:20:35
20% I'll add a little bit of margin on
01:20:37
the
01:20:40
bottom 10
01:20:43
pixels text align
01:20:50
Center I'll add a border
01:20:53
three pixel
01:20:56
solid I'll add a border radius to round
01:20:59
the
01:21:05
corners and a background
01:21:09
color pick a color again I'll use
01:21:15
hsl this one I pre-picked already
01:21:19
185
01:21:21
100% 50% % that's not a bad looking
01:21:25
color pick whatever color you would like
01:21:27
let's work on our list item
01:21:29
elements we need to select the class of
01:21:32
list
01:21:33
items do list items select each list
01:21:38
item within this
01:21:40
class I'll increase the font size to be
01:21:43
2
01:21:46
em I'll remove the list style but you
01:21:49
don't have
01:21:50
to list Style
01:21:53
will be
01:21:54
none pick a color again I'm using hsl
01:22:01
values I'll set the lightness to be
01:22:06
25% text align
01:22:11
Center and margin will be
01:22:15
zero all right now when I hover over one
01:22:18
of these list items I'll apply a hover
01:22:21
effect
01:22:23
take our class list items take each list
01:22:25
item access the hover sudo
01:22:28
class I'll change the brightness the
01:22:32
color will be something a little bit
01:22:34
brighter I'll set the lightness to be
01:22:37
45% and then the cursor will be a
01:22:41
pointer yeah not bad that's how we can
01:22:44
apply some CSS styling to list
01:22:46
components all right heading back to our
01:22:48
app component what if we have an empty
01:22:51
list for example with our fruits I'm
01:22:53
going to cut
01:22:55
it we can use conditional rendering to
01:22:58
render our list only if there's elements
01:23:02
let's put those back we're going to
01:23:04
write some JavaScript we need some curly
01:23:06
braces let's take our array of fruits
01:23:10
access the length
01:23:12
property is it greater than
01:23:15
zero question mark if it is we'll return
01:23:19
our list
01:23:21
component
01:23:23
colon if it's false we can return null
01:23:27
let's do that with our vegetables
01:23:29
too take our array of vegetables access
01:23:33
the length property is it greater than
01:23:36
zero tary operator if it is return our
01:23:40
list
01:23:42
component if not we can return
01:23:45
null if one of these lists doesn't have
01:23:47
any elements we won't render that
01:23:51
component
01:23:56
there is a short hand to the tary
01:23:58
operator we don't need to write or else
01:24:00
return null what we'll use instead of
01:24:03
the tary operator is the and logical
01:24:05
operator we can effectively shortcircuit
01:24:08
the first
01:24:09
expression then we don't need colon null
01:24:13
it's a little less
01:24:17
code our list component will always be
01:24:20
true because it exists
01:24:23
however the first expression might not
01:24:25
be so if this condition is false we
01:24:29
don't render this if it is true we will
01:24:32
render this let's try that again let's
01:24:35
remove these
01:24:37
elements we don't display the fruits
01:24:39
list
01:24:40
component and we will not display the
01:24:43
vegetable list
01:24:44
component this is known as short
01:24:47
circuiting one thing to consider what if
01:24:50
our category is missing let's remove our
01:24:53
category from
01:24:54
fruits well I would like to add some
01:24:56
sort of placeholder here or what if one
01:24:59
of these arrays was
01:25:01
missing well nothing displays not even
01:25:04
vegetables we should add some default
01:25:06
props in case one of these properties is
01:25:08
missing so going back to our list
01:25:11
component before we export it let's set
01:25:14
up some default props take our component
01:25:17
name
01:25:18
list. default props equals
01:25:22
within a set of curly braces let's set
01:25:25
our
01:25:26
category category
01:25:29
property to be category as a
01:25:34
placeholder if for some reason these
01:25:36
components don't have a
01:25:38
category We'll add a placeholder of
01:25:40
category which at least looks better if
01:25:43
we're missing an array that's a bigger
01:25:45
problem let me go to inspect then
01:25:51
console
01:25:53
we're trying to map something that's
01:25:55
undefined we have no array to work with
01:25:58
so nothing is rendering so as a backup
01:26:01
within default props let's set our items
01:26:05
to be an empty
01:26:06
array items will be an empty
01:26:11
array if one of these arrays was missing
01:26:13
for some
01:26:15
reason at least the category is
01:26:17
displayed as well as the subsequent
01:26:19
components at least something will
01:26:21
display
01:26:23
okay then lastly as good practice if
01:26:25
we're accepting props we should set up
01:26:28
prop types I'll walk you through it if
01:26:30
you're just joining us with prop types
01:26:33
if the incorrect data type is passed
01:26:35
into props when we debug it'll give us a
01:26:38
warning to use prop types we have to
01:26:41
import it at the top of our list
01:26:43
component we will
01:26:45
import prop
01:26:47
types
01:26:49
from prop
01:26:51
types
01:26:54
take our component of
01:26:56
list
01:26:58
list. prop
01:27:02
types equals set of curly braces for our
01:27:07
category this will be a
01:27:09
string category colon
01:27:12
space prop types.
01:27:17
string add a comma for another line okay
01:27:21
this is where it's going to get tricky
01:27:23
we have an array of
01:27:25
objects we'll access the items
01:27:28
property items colon space prop types
01:27:34
I'm going to move down a little
01:27:37
bit dot we have an
01:27:40
array
01:27:41
array of we have an array of
01:27:45
objects prop
01:27:47
types. shape method we have to define
01:27:51
the shape of each object object each
01:27:53
object is going to have its own data
01:27:55
types we have a number string then
01:27:59
number we're defining an object we need
01:28:01
a set of curly
01:28:02
braces ID
01:28:05
colon prop types do
01:28:09
number comma for another
01:28:12
property I'll put the next one on a new
01:28:14
line for
01:28:18
readability we have a name property
01:28:20
which will be a string
01:28:22
prop types.
01:28:27
string then
01:28:31
calories prop types.
01:28:35
number and that's
01:28:38
it let's head back to our app
01:28:41
component our prop types should raise a
01:28:43
warning if some of our data is of the
01:28:45
wrong data type for example let's say
01:28:48
that calories is now the string Apple
01:28:52
maybe somebody mistyped it
01:28:54
twice you can see that right here it
01:28:56
changed let's go to
01:28:58
inspect
01:29:01
console and here's our warning because
01:29:03
we have prop type set up invalid prop
01:29:06
items index zero calories of type
01:29:10
string if we didn't have prop type setup
01:29:12
we wouldn't receive that warning this
01:29:14
may go unnoticed it is good practice if
01:29:16
you're accepting props to also set up
01:29:18
prop types it's a little more
01:29:20
complicated if you have an array of
01:29:22
objects but here are the steps all right
01:29:24
everybody I know this was a massive
01:29:26
topic thank you all for watching feel
01:29:28
free to take some time to study and work
01:29:30
with this before moving on to the next
01:29:32
topic if you would like we did Cover a
01:29:34
lot of material and well those are
01:29:36
various ways in which we can render
01:29:38
lists in
01:29:43
react hey uh so today I'm going to
01:29:45
explain click events and how to handle
01:29:47
them in react a click event is an
01:29:49
interaction when a user clicks on a
01:29:52
specific element we can respond to
01:29:54
clicks by passing a call back to the
01:29:56
onclick event handler in this example
01:29:59
we'll create a button let's create a
01:30:02
button component go to new
01:30:04
file button.
01:30:08
jsx we will use a function base
01:30:11
component function
01:30:14
button then be sure to export it export
01:30:17
default
01:30:19
button going back to our parent
01:30:22
component of app we will need to import
01:30:24
our button import button from its
01:30:29
location button.
01:30:31
jsx then we will create one button
01:30:36
component going to our button component
01:30:40
we will return a single button
01:30:44
element the button will have text of
01:30:47
Click
01:30:49
me hey for fun I'm just going to add it
01:30:51
an emoji but you don't have
01:30:57
to and I will zoom in so you can see it
01:31:01
many HTML elements have an onclick event
01:31:05
handler we can set the SQL to a
01:31:07
JavaScript callback but we need a
01:31:09
function to work with So within our
01:31:12
button function we can write an inner
01:31:15
function const handle click I will
01:31:20
assign this equal to either a function
01:31:22
expression or an arrow function I like
01:31:25
Arrow functions so I'm going to stick
01:31:26
with an arrow function when we click on
01:31:29
the button what do we want to do let's
01:31:32
console.log the word
01:31:36
ouch so we have our function we will set
01:31:39
the onclick event handler equal to a
01:31:42
callback a call back to this function
01:31:45
when we click on the button do this if I
01:31:48
went to my console inspect console
01:31:52
Let me refresh this if I click on the
01:31:55
button we execute this code we will
01:31:58
output the word ouch every time I click
01:32:01
the button if your function has parameters
01:32:04
there's one change we'll need to make
01:32:06
let's create a second function const
01:32:10
handle Click
01:32:13
2 this function will have a parameter of
01:32:16
name we'll use an arrow function we will
01:32:20
console.log
01:32:22
I'll use a template string let's display
01:32:25
the
01:32:26
name stop clicking
01:32:30
me for the onclick event handler we will
01:32:33
set a call back of handle click to but
01:32:36
we have parameters that means we need to
01:32:38
send a matching number of arguments I
01:32:41
will send my first name but feel free to
01:32:43
use your name I didn't click on the
01:32:45
button yet I'm going to go to inspect
01:32:49
console uh Let me refresh all this
01:32:52
I didn't click on the button yet but
01:32:54
we've already called that function if
01:32:56
you add a set of parentheses after a
01:32:58
call back you'll invoke it right away so
01:33:01
we don't want to do that if we have
01:33:03
arguments we need to send to a function
01:33:05
we could wrap this call back within a
01:33:07
function expression or an arrow function
01:33:10
let's use an arrow function when we
01:33:12
click on the button do this that will
01:33:15
prevent us from calling this function
01:33:17
right away so let's refresh
01:33:20
everything then when I click on the
01:33:22
button then we execute this code bro
01:33:25
stop clicking me bro stop clicking me
01:33:28
bro stop clicking me so that's how to
01:33:30
send arguments to a
01:33:32
function now in this next example We'll
01:33:34
add some
01:33:36
conditions let's set the onclick
01:33:38
attribute to be a call back to handle
01:33:40
click if you have more than one line of
01:33:43
code for your arrow function you'll need
01:33:44
to use a set of curly
01:33:47
braces let's add a count variable let
01:33:51
count equal
01:33:53
Z with our handle click function let's
01:33:57
have one parameter a name what would we
01:34:00
like to do let's check to see if our
01:34:04
count
01:34:05
variable is less than three if it is
01:34:09
let's increase count by
01:34:11
one then
01:34:15
console.log I'll use a template
01:34:18
string let's add our name parameter
01:34:22
you clicked me add a
01:34:25
placeholder
01:34:27
count time or
01:34:30
times else if the user clicked me more
01:34:34
than three times let's console. log a
01:34:38
different
01:34:39
message let's
01:34:41
say add a placeholder name stop clicking
01:34:49
me with the onclick event Handler we
01:34:52
have a parameter that means we have to
01:34:54
wrap this call back within a function
01:34:56
expression or an arrow function let's
01:34:58
use an arrow function for
01:35:02
Simplicity we'll pass in a first name
01:35:05
pass in your own first
01:35:06
name so let's save and refresh
01:35:09
everything go to inspect
01:35:12
console when I click on the button once
01:35:15
it's going to display bro you clicked me
01:35:17
one time bro you clicked me two times
01:35:20
bro you clicked me three times after the
01:35:23
third time we'll display a different
01:35:24
message bro stop clicking me bro stop
01:35:27
clicking me bro stop clicking
01:35:30
Me So within your functions you can
01:35:32
write some
01:35:33
conditions now I need to explain the
01:35:36
event
01:35:38
parameter let's recreate that handle
01:35:40
click
01:35:41
function const handle click equals with
01:35:47
click events were automatically provided
01:35:49
with an event argument it's an object
01:35:52
that describes the event that occurred
01:35:54
but as a parameter people usually
01:35:56
shorten the event parameter to be simply
01:35:59
e let's print our
01:36:02
event
01:36:04
console.log e our event we'll need to
01:36:08
set the onclick event handler we have
01:36:10
one parameter so we need to wrap this
01:36:13
within an arrow
01:36:15
function handle click for the arrow
01:36:18
function we'll have e for the parameter
01:36:21
and E for an argument for the handle
01:36:23
click
01:36:28
function let's click on the button we're
01:36:31
now outputting the event its type is
01:36:34
synthetic base event and it has all of
01:36:37
these properties and
01:36:39
methods such as where you clicked on the
01:36:42
screen there's a timestamp and a Target
01:36:45
by utilizing this event object that
01:36:48
gives us many different possibilities
01:36:50
for this demonstration let's change the
01:36:52
text content of the
01:36:54
button so there should be a Target
01:36:56
property here I am going to change the
01:37:00
text content that should be a
01:37:03
property yep there it
01:37:07
is so what we'll do after clicking on
01:37:10
the button we will access that event
01:37:13
object follow this with DOT that's the
01:37:16
property
01:37:17
accessor we're selecting the inner
01:37:20
target object
01:37:22
then selecting the text content property
01:37:26
let's set that equal to be
01:37:29
ouch so now when you click on the button
01:37:32
the text of the button should change to
01:37:35
ouch I'm going to add an emoji
01:37:38
too there we go okay let's try this
01:37:41
again this will be fun click me
01:37:45
ouch there's also the on doubleclick
01:37:48
event handler instead of onclick you'll
01:37:50
say say on double click so when we click
01:37:54
on the button once now nothing
01:37:57
happens but once I double click that's
01:38:00
when we'll execute this function now I'm
01:38:02
going to double click
01:38:05
ouch if you need to handle a double
01:38:07
click you just have to use the on double
01:38:10
click event
01:38:11
handler all right let's move on from
01:38:13
using a button this time let's use an
01:38:15
image in my assets folder I do have my
01:38:18
profile picture from my YouTube channel
01:38:21
find a profile picture of yourself or a
01:38:23
picture you
01:38:24
like we're going to create a new
01:38:26
component let's go to our source folder
01:38:29
I will name this component profile
01:38:32
picture.
01:38:35
jsx we will create a function with the
01:38:38
name of profile
01:38:42
picture then be sure to export it export
01:38:47
default profile
01:38:49
picture then going back back to our app
01:38:52
component we will import our profile
01:38:55
picture component from its
01:38:58
location profile picture.
01:39:02
jsx let's include our profile picture
01:39:05
component instead of our
01:39:10
button all right so within our profile
01:39:12
picture component I will store a URL
01:39:15
within a constant constant image
01:39:20
URL I will list a relative file
01:39:23
path so my image is within my assets
01:39:27
folder my file location but it might be
01:39:30
different for you mine is going to
01:39:33
be SL Source SL assets slash the name of
01:39:39
the image including the extension mine
01:39:42
is profile.
01:39:44
jpeg we will return an image on
01:39:49
lint I AMG for an image
01:39:52
element I will set the source equal to
01:39:55
some JavaScript I need to set a curly
01:39:58
braces I will set it equal to my image
01:40:02
URL and let's see if that worked yes it
01:40:05
did but I'm going to zoom out to like
01:40:09
150 my image is
01:40:12
small let's add a handle click function
01:40:15
const handle click equals an arrow
01:40:19
function to test it let's console.log
01:40:23
the
01:40:24
word
01:40:26
ouch then we'll need to set the onclick
01:40:29
event handler within our
01:40:31
image on click equals a callback to
01:40:36
handle
01:40:37
click so let's save and refresh
01:40:40
everything let's go to inspect console
01:40:43
when you click on your image it should
01:40:45
display the word ouch every time you
01:40:47
click
01:40:49
it let's utilize the event object that's
01:40:52
generated we have one parameter e for
01:40:55
our event that means we'll have to
01:40:57
change the call back to Be an Arrow
01:41:00
function e for the parameter
01:41:04
Arrow handle click we have one argument
01:41:08
of e so what should we do now when we
01:41:12
click on the image let's hide the image
01:41:16
we'll need to access that event
01:41:18
object access the target object that's
01:41:21
found within access its style then the
01:41:25
display
01:41:27
property we will set the display to be
01:41:29
none when we click on it so then if you
01:41:32
were to click on your image it should
01:41:35
disappear so by accessing the event
01:41:37
object that gives us a load of different
01:41:40
possibilities for what we can do all
01:41:42
right everybody so that is an
01:41:43
introduction to handling click events in
01:41:46
react a click event is an interaction
01:41:49
when a user clicks on a specific element
01:41:51
we can respond to clicks by passing a
01:41:53
call back to the onclick event handler
01:41:56
and well everybody that is an
01:41:57
introduction to click events in
01:42:03
react hey what's going on everybody in
01:42:06
today's video I'm going to explain the
01:42:07
usate react hook and near the end of
01:42:09
this video we're going to create a
01:42:11
reactive counter program so sit back
01:42:13
relax and enjoy the show I haven't
01:42:16
explained what react hooks are yet a
01:42:18
react Hook is a special function that
01:42:21
allows functional components to use
01:42:23
react features without writing class
01:42:25
components this was a change made in
01:42:27
react version 16.8 basically we no
01:42:29
longer need to write class components we
01:42:31
can write function-based components that
01:42:34
use react hooks to use react features
01:42:36
there are many react hooks if a function
01:42:39
begins with use it's probably a react
01:42:41
hook use State use effect use context
01:42:43
use reducer use callback and more UST
01:42:46
state is the most widely used UST state
01:42:49
is a react hook that allows the the
01:42:50
creation of a stateful variable and a
01:42:53
Setter function to update its value in
01:42:55
the virtual Dom basically by using the
01:42:58
usate hook we can create not just a
01:43:00
variable but a stateful variable when
01:43:03
you update this variable that change
01:43:05
will be reflected in the virtual domum
01:43:07
normal variables don't when you use the
01:43:09
UST State hook you're given a variable
01:43:11
and a Setter function specifically for
01:43:13
that variable so what we'll do in this
01:43:15
example is create a new component going
01:43:17
to our source folder we're going to
01:43:19
create a new file we'll name this
01:43:21
component my
01:43:24
component we use react hooks in function
01:43:27
based components so make sure you're not
01:43:29
writing a class
01:43:31
component with this component we will
01:43:34
export it export default my component
01:43:38
then be sure to import this because I'm
01:43:40
probably going to forget if we don't do
01:43:41
it now so we will import my
01:43:46
component from its
01:43:49
location slash my component and this is
01:43:53
a jsx file let's return one my
01:43:59
component and we are ready to begin in
01:44:02
order to use a react hook we need to
01:44:04
import it at the top of this component
01:44:06
we will import the react Library however
01:44:10
we don't need the entire react Library
01:44:13
we can use object destructuring to
01:44:15
extract individual variables or
01:44:17
functions I would just like the UST
01:44:19
State function we don't need the entire
01:44:22
react library from its location react we
01:44:27
now have access to this UST State
01:44:29
function using the UST State function
01:44:31
we'll create a stateful variable and a
01:44:34
Setter function to update that variable
01:44:36
so let's declare const we're going to
01:44:39
use a set of straight brackets for array
01:44:41
destructuring equals use State
01:44:46
function the use State function returns
01:44:49
an array with two ele elements a
01:44:51
variable and a Setter function we're
01:44:54
going to use array destructuring to
01:44:56
destructure these two elements we'll
01:44:58
create a stateful variable for name then
01:45:00
we're given a Setter function
01:45:02
specifically for this variable a common
01:45:04
naming convention is to type set than
01:45:07
the variable name with camel case naming
01:45:10
convention and that's it if we ever need
01:45:13
to change the value of the stateful
01:45:15
variable we have to do so with this
01:45:17
Setter it's a function at the bottom
01:45:19
we're going to return
01:45:21
return a div
01:45:24
element within this div element we'll
01:45:27
create a paragraph element and a button
01:45:30
we'll begin with a paragraph element
01:45:32
that has text of name and I'm going to
01:45:35
zoom in a little bit I will insert some
01:45:38
JavaScript using curly braces let's
01:45:40
display our name following our paragraph
01:45:44
element let's include a button the
01:45:46
button will have an onclick attribute
01:45:49
equal to a JavaScript function so we
01:45:52
need a set of curly braces to embed that
01:45:54
let's create a function to
01:45:57
update name for the text of the button
01:46:02
let's say set
01:46:04
name all right now we just need to
01:46:07
declare this function I'll use an arrow
01:46:09
function const update name equals Arrow
01:46:16
function so what would we like to do
01:46:19
okay let's attempt to set our name equal
01:46:23
to type your name or some other name I'm
01:46:25
just going to type in
01:46:27
SpongeBob if I click on this button we
01:46:30
should update our name right well that
01:46:33
doesn't appear to
01:46:35
work so if I were to
01:46:38
console.log my name
01:46:41
variable then attempt to update it using
01:46:43
this button if I were to go to my
01:46:46
console hold on I'm going to use let so
01:46:49
we can update it if I attempt to change
01:46:52
this name of the
01:46:54
variable it does so within our
01:46:56
console but it doesn't update in react
01:46:59
the virtual Dom is still displaying the
01:47:01
previous state so if I would like to
01:47:03
display any changes I will want to use
01:47:06
that Setter instead of setting our name
01:47:08
equal to a new value I'm going to change
01:47:10
this back to be a
01:47:12
constant we will use the setter function
01:47:16
and pass in a new value so let me type
01:47:19
in a new value
01:47:22
and that should work we have updated our
01:47:24
name when you use the setter function of
01:47:26
a stateful variable it will trigger a
01:47:28
render of the virtual Dom normal
01:47:31
variables don't that's why the UST State
01:47:33
react Hook is useful we can create a
01:47:36
stateful variable when the stateful
01:47:38
variable gets updated with its Setter
01:47:40
function it triggers a rerender of the
01:47:42
virtual Dom with the usate function you
01:47:45
can pass in an initial state currently
01:47:47
we're not passing in anything for the
01:47:49
initial state State I will set this to
01:47:51
be
01:47:52
guested when I refresh everything and
01:47:54
start over the initial state is guessed
01:47:57
whatever value you pass in to the UST
01:47:59
State hook then I can set my name again
01:48:02
to something else now we're going to
01:48:04
create an age variable and increment
01:48:07
it const we're going to use array to
01:48:09
structuring we need a stateful variable
01:48:13
like age and a Setter function for that
01:48:16
age set age equals use
01:48:21
state if you would like an initial value
01:48:24
you can place that within the UST State
01:48:25
function I'll set the initial value of
01:48:28
AG to be
01:48:30
zero I'm going to copy this paragraph
01:48:33
and this
01:48:35
button change name to be
01:48:39
age we'll create a function to increment
01:48:43
age the text on the button will
01:48:46
be increment age now we just need this
01:48:51
function
01:48:53
const increment age I'll use an arrow
01:49:00
function to increment our age we will
01:49:02
use the set age
01:49:06
function let's take our age +
01:49:10
one so our initial value for our age is
01:49:13
zero but every time I click on the
01:49:15
button we will update the value of that
01:49:18
variable so every time I click the
01:49:19
button button we're increasing our age
01:49:21
by
01:49:22
one or even a different number this time
01:49:25
I'll increase our age by two on each
01:49:27
button click so we start at zero then
01:49:30
we'll increment by two 2 4 6 8 10 now
01:49:34
this time we'll create a Boolean
01:49:36
variable and toggle it between being
01:49:38
true and false using the US state hook
01:49:42
we will create const is
01:49:45
employed the setter function will be set
01:49:49
is
01:49:51
employed equals the use State
01:49:56
hook I would like an initial value a
01:50:00
false let's create a paragraph element
01:50:02
and a
01:50:04
button I will set the text of the
01:50:06
paragraph to be is
01:50:10
employed instead of displaying a Boolean
01:50:13
directly let's use the tary operator for
01:50:16
conditional rendering is employed if
01:50:19
that is true will display yes otherwise
01:50:24
no when we click on the button let's
01:50:27
create a function
01:50:28
to
01:50:30
toggle toggle employed status that's
01:50:35
kind of a Long Function name all right
01:50:38
let's create a function const toggle
01:50:41
employed status equals an arrow
01:50:46
function we will use the setter function
01:50:49
set is employed
01:50:51
pass in a new value let's switch this
01:50:53
value from being false to true and true
01:50:55
to false every time we click the button
01:50:57
since this is a Boolean we can use the
01:51:00
not logical operator to reverse it so
01:51:03
let's say not is
01:51:06
employed and let's see if this works is
01:51:08
employed no oh let's change the text on
01:51:11
the button
01:51:13
too toggle
01:51:17
status there we go all right when we
01:51:20
click on the button we can toggle this
01:51:21
Boolean from being true to false and
01:51:23
false to true and this should happen
01:51:26
every time I click the
01:51:28
button as a project what we're going to
01:51:30
do now is create a counter program so
01:51:33
let's close out of my
01:51:35
component we'll create a new component
01:51:37
for a counter component counter.
01:51:41
jsx this will be a function base
01:51:45
component
01:51:46
function
01:51:48
counter then be sure to export it export
01:51:53
default
01:51:55
counter going back to our app component
01:51:58
we will import our counter component
01:52:01
from its location counter. jsx then we
01:52:05
will include one counter component and
01:52:09
that's it in order for us to use the US
01:52:12
state hook we have to import it from the
01:52:14
react Library import react we'll use
01:52:17
object destructuring just to get the UST
01:52:20
State hook and nothing else from its
01:52:23
location of
01:52:25
react all we need is one variable a
01:52:29
counter let's say const we'll use array
01:52:32
destructuring create a stateful variable
01:52:35
of count and a Setter function for that
01:52:38
count set count equals the use State
01:52:44
hook would we like an initial value for
01:52:46
count we would like the initial value to
01:52:49
be zero
01:52:50
we'll create a few functions to
01:52:52
increment decrement and reset the
01:52:55
counter const
01:52:57
increment equals I'll use an arrow
01:53:02
function to update the count to
01:53:04
increment it we will use the set count
01:53:07
function the value we'll pass in is
01:53:10
Count +
01:53:12
one then let's do this for
01:53:16
decrement
01:53:18
const decrement
01:53:20
count minus one then
01:53:24
reset const reset for set count we'll
01:53:29
pass in zero to reset the
01:53:31
count now we're going to return some
01:53:35
elements we'll also style this with CSS
01:53:39
let's begin with the div
01:53:41
element I'm going to scroll down a
01:53:42
little
01:53:46
bit my div element will have a class
01:53:49
name name equal to
01:53:53
counter-
01:53:55
container I will create a paragraph
01:53:57
element with a class
01:54:00
name equal to count display to display
01:54:05
the number for the text of the paragraph I
01:54:08
will insert some JavaScript and display
01:54:11
our count
01:54:13
variable we'll create three buttons for
01:54:16
the first we'll create a button element
01:54:19
this button will have a class
01:54:22
name of
01:54:24
counter-
01:54:26
button the onclick attribute will be set
01:54:29
equal to a JavaScript function this
01:54:32
first button will be the decrement
01:54:34
button for the unclick attribute we will
01:54:36
set this equal to the decrement function
01:54:39
for the text on the button we'll say
01:54:45
decrement and there's our first button
01:54:48
so let's copy this button paste it the
01:54:52
second button will be for reset the text
01:54:55
will be
01:54:57
reset then the third button will be
01:55:00
increment onclick will be the increment
01:55:03
function the text will be
01:55:06
increment all right and that's all that
01:55:08
we need let's check it for functionality
01:55:10
to be sure that everything works we can
01:55:12
increment this number we can decrement
01:55:16
it and we can reset it so for the icing
01:55:20
on the cake let's style it with
01:55:22
CSS going to our index CSS stylesheet
01:55:26
we'll apply the following CSS let's
01:55:29
select our counter
01:55:32
container do counter container I will
01:55:36
text align
01:55:39
Center change the font
01:55:43
family I will pick a s serif font of
01:55:46
Arial with a backup of s serif
01:55:50
next we'll select the count
01:55:53
display select the class of count
01:55:56
display that would be this number will
01:55:59
increase the font size to something
01:56:00
massive like 10 em RM works too oh okay
01:56:05
that's a little too big but I am zoomed
01:56:06
in actually you know what that's perfect
01:56:09
I will set the margin top to be zero to
01:56:13
close this Gap and I will set the margin
01:56:17
on the bottom to be 50
01:56:20
pixels then let's work on the counter
01:56:23
buttons
01:56:25
next select the class of counter button
01:56:29
I will set the width to be 150
01:56:34
pixels the height to be 50
01:56:39
pixels the font
01:56:42
size to be 1.5
01:56:46
em I will set the font weight to be
01:56:51
bold set the margin to
01:56:54
be 0 pixels by 5
01:56:57
pixels this would be for the left and
01:56:59
right of the
01:57:01
buttons change the background
01:57:05
color pick a color that you
01:57:08
like I'm going to use hsl values
01:57:13
though that's pretty good I will set the
01:57:17
color of the font to be white
01:57:20
remove the
01:57:21
border border
01:57:24
none set the Border radius to round the
01:57:29
corners five
01:57:32
pixels and change our cursor to be a
01:57:36
pointer if we hover over the button when
01:57:39
we hover over the button I'm going to
01:57:40
change the background color of the
01:57:42
button so with the counter button class
01:57:45
with the counter button class we will
01:57:47
select the hover sudo class
01:57:50
we'll take our background color decrease
01:57:53
the lightness to be 10%
01:57:55
darker there we are and that is all the
01:57:58
CSS styling that we need we have our
01:58:00
counter program we can increment the
01:58:03
counter we can decrement it and we can
01:58:06
reset
01:58:07
it all right everybody so that is the US
01:58:10
state react hook it allows the creation
01:58:12
of a stateful variable and a Setter
01:58:14
function to update its value in the
01:58:17
virtual Dom when you include the UST
01:58:19
State hook you're given an array of two
01:58:21
elements we use array destructuring to
01:58:24
create a stateful variable and a Setter
01:58:26
function to update that variable and
01:58:29
well everybody that is the UST State
01:58:31
hook in
01:58:36
react yo yo yo yo yo what's going on
01:58:38
people so today I got to explain the
01:58:40
onchange event handler in react onchange
01:58:44
is an event handler used primarily with
01:58:47
form elements including but not limited
01:58:49
lied to input text area select and radio
01:58:53
buttons onchange triggers a function
01:58:56
every time the value of the input
01:58:58
changes so let me give you an example we
01:59:00
will be using the UST State hook so we
01:59:03
need to import it import react I would
01:59:07
like the use State Hook from
01:59:11
react using the use State hook we're
01:59:14
going to create a name variable a
01:59:15
stateful variable const name and we need
01:59:20
a Setter function for this variable set
01:59:23
name equals the use State hook you can
01:59:28
set an initial value I'll set mine to be
01:59:30
an empty string I will be returning a
01:59:33
div element to enclose all of our
01:59:37
markup within my div element I will
01:59:39
create an input
01:59:43
element and here's my input element I
01:59:47
will set the value of the input element
01:59:50
to be some JavaScript my stateful name
01:59:54
variable then I will set the
01:59:56
onchange event
01:59:58
handler to be a reference to a
02:00:01
JavaScript function which we still need
02:00:03
to create this will be a call back let's
02:00:07
create a function to handle a name
02:00:09
change it's going to be similar to
02:00:11
handle
02:00:12
click function handle name change we
02:00:18
have one parameter an event that's going
02:00:21
to be provided to us when we change the
02:00:24
value of this input element I will
02:00:26
invoke our set name
02:00:29
function we will set our name variable
02:00:32
to be access our event
02:00:35
object access its Target then get the
02:00:39
value property onchange is going to be
02:00:42
set equal to a call back to this
02:00:44
function when we type something or
02:00:46
somehow change the value within here
02:00:48
call the this function and then
02:00:51
afterwards I will include a paragraph
02:00:53
element that has the text of name and
02:00:57
include some JavaScript I will include
02:01:00
my name variable so what should happen
02:01:03
is when I type in a name it will update
02:01:06
in real
02:01:10
time with use State I can set the
02:01:13
initial value to be something too I'll
02:01:15
set my initial value to be
02:01:18
guested
02:01:21
the onchange event handler triggers a
02:01:24
function every time the value of the
02:01:26
input
02:01:27
changes right now we have an initial
02:01:29
value if I start deleting this my name
02:01:31
is going to
02:01:32
update this time we'll create an input
02:01:35
element for a number instead of text I
02:01:37
will create a stateful variable for a
02:01:40
quantity as if we're ordering something
02:01:43
quantity and we need a Setter function
02:01:47
set quantity
02:01:50
equals the use State
02:01:53
hook would we like an initial value not
02:01:57
yet but we'll get to that later I will
02:01:59
create a function to handle quantity
02:02:05
change it's the same formula as before
02:02:08
we will be provided with an event so
02:02:11
let's write a parameter for that we will
02:02:13
use the setter function for the
02:02:15
quantity set quantity
02:02:20
we'll pass in Access our event access
02:02:23
its Target then access its
02:02:26
value then we need some HTML elements
02:02:30
let me scroll down I will create an
02:02:33
input
02:02:36
element I will set the value of this
02:02:39
element to be our
02:02:43
quantity then I will set the onchange
02:02:46
event handler to be a call back to a job
02:02:48
JavaScript function handle quantity
02:02:52
change I will set the type of this
02:02:54
element to be a
02:02:56
number type
02:02:58
equals
02:03:01
number afterwards I will add a paragraph
02:03:05
with our
02:03:06
quantity and display our quantity
02:03:11
variable so now we have arrows to select
02:03:15
a number I will update the quantity to
02:03:18
be one two 3 4
02:03:21
five I'll set an initial value of one
02:03:25
let's pretend that we're ordering
02:03:27
something we're going to assume that the
02:03:29
user wants to buy at least one of
02:03:30
something that's how you can use
02:03:32
onchange to reflect a number that's
02:03:34
changing within an input
02:03:36
element all right now we're going to
02:03:38
create a text area a user is going to be
02:03:41
buying something we'll have a user leave
02:03:43
a comment I will create const comment
02:03:50
set comment equals use
02:03:55
State I'll set the initial state of my
02:03:57
comment to be an empty
02:03:59
string then I'll create a function to
02:04:02
handle comment
02:04:04
change
02:04:06
function handle comment
02:04:10
change we have one parameter and event
02:04:14
we will use our Setter for our comment
02:04:17
then pass in Access our event access its
02:04:20
Target then access its
02:04:23
value then we need our HTML
02:04:26
elements we will create a text area
02:04:31
element I will set the value to be our
02:04:36
comment
02:04:37
variable then set the onchange event
02:04:40
handler to
02:04:42
be a call back to handle comment
02:04:45
change I'm also going to add a
02:04:47
placeholder for some placeholder
02:04:50
text
02:04:52
placeholder
02:04:53
equals in my specific example this text
02:04:57
area will be for specific delivery
02:04:59
instructions if somebody's shopping on
02:05:02
your site and they order something they
02:05:04
might want to provide additional
02:05:05
delivery instructions leave package on
02:05:08
the back porch ring the doorbell after
02:05:10
the delivery is complete stuff like that
02:05:13
so for my placeholder I'll type in enter
02:05:17
delivery
02:05:20
instructions and there's my placeholder
02:05:22
text then just to see if this updates
02:05:25
I'll add a paragraph
02:05:27
element with my
02:05:30
comment then insert my comment
02:05:33
variable all right let's see if this
02:05:36
works
02:05:38
leave package on front porch and avoid
02:05:45
the killer
02:05:47
Chihuahua
02:05:49
that's how we can use onchange with a
02:05:51
text area all right the next one's going
02:05:53
to be a little more
02:05:55
complicated we're going to use a select
02:05:57
element for a drop- down menu we'll
02:05:59
create a drop- down menu for a payment
02:06:01
is somebody going to pay with a Visa a
02:06:03
MasterCard a gift card
02:06:07
const payment set payment this will be a
02:06:12
payment type we'll use use
02:06:15
State I will set the initial value to be
02:06:18
an empty
02:06:19
string then we need a Handler
02:06:23
function function
02:06:26
handle payment
02:06:30
change we have one parameter and
02:06:33
event then we'll use that Setter
02:06:35
function for the
02:06:37
payment set
02:06:39
payment event access its Target then the
02:06:46
value our select element has as a pair
02:06:48
of
02:06:50
tags we'll Place option elements within
02:06:53
our select element the opening select
02:06:56
element is going to have a value equal
02:06:59
to our payment
02:07:02
variable I will set the onchange event
02:07:05
handler equal to a call back to the
02:07:09
handle payment change function we have
02:07:11
some options within our select
02:07:16
element the text on my first stop option
02:07:18
will be
02:07:20
select an
02:07:23
option so now my drop- down menu has
02:07:25
select an option for this option's value
02:07:29
I will set it to be an empty string this
02:07:32
will be the
02:07:33
default let's create another
02:07:38
option the text on this option element
02:07:40
will be a Visa if somebody's paying with
02:07:42
a Visa
02:07:43
card the value will be a string of Visa
02:07:49
besides the defaults we do have an
02:07:51
option for Visa then
02:07:57
MasterCard Master
02:07:59
Card the value will be a string of
02:08:04
MasterCard and then a gift
02:08:09
card gift card the value will be gift
02:08:14
card so besides our default we have Visa
02:08:18
MasterCard and gift
02:08:20
card after our select element I'll
02:08:23
create a
02:08:24
paragraph the text on the paragraph will
02:08:26
be payment then I'll display our payment
02:08:29
stateful
02:08:32
variable when I select Visa our payment
02:08:35
is going to update with
02:08:36
Visa Mastercard and gift
02:08:40
card with use State I can even set an
02:08:43
initial value I could set the initial
02:08:45
value to be for example
02:08:47
Visa
02:08:49
Master
02:08:50
Card or a gift
02:08:53
card but I'll keep it as an empty string
02:08:55
so we get that default text of Select an
02:08:58
option the last form element I'll cover
02:09:01
is radio buttons and these are going to
02:09:03
be the most complicated we'll create a
02:09:05
pair of two radio buttons for a delivery
02:09:07
method either pickup or a delivery does
02:09:11
somebody want to come into a store to
02:09:12
pick up our package or is a driver going
02:09:14
to take it to their
02:09:15
residence I will create comp
02:09:20
let's say
02:09:21
shipping for the shipping method and a
02:09:24
Setter function for set shipping equals
02:09:29
use State for the time being I will set
02:09:33
the initial state to be an empty
02:09:35
string we'll create a Handler
02:09:38
function function handle shipping
02:09:43
change we have one parameter of
02:09:46
event I will use that Setter function of
02:09:49
set
02:09:52
shipping access our event argument
02:09:56
access its Target access its
02:09:59
value within our HTML
02:10:02
markup we're going to create a pair of
02:10:04
radio buttons we'll create a label we
02:10:07
need a pair of label
02:10:09
elements the text on the first label
02:10:11
will be pickup are we going to pick up
02:10:14
our package from a store we'll create a
02:10:16
second label I'll just copy what we have
02:10:19
paste it the second label will be for a
02:10:24
delivery within my label I'll create an
02:10:27
input
02:10:31
element I will set the type to equal
02:10:36
radio I will set the value of this radio
02:10:39
button to
02:10:41
be
02:10:46
pickup I'll place the these additional
02:10:48
properties on a new line just for
02:10:51
readability I will set the checked
02:10:53
attribute equal to a JavaScript
02:10:56
expression let's check to see if our
02:10:59
shipping stateful variable is strictly
02:11:03
equal to our value of
02:11:06
pickup this will evaluate to be true or
02:11:10
false if this expression is
02:11:13
true then we'll trigger our onchange
02:11:16
vent Handler and set it equal to a
02:11:19
JavaScript function it's going to be a
02:11:22
call back to handle shipping
02:11:26
change let's copy this input element
02:11:29
then within our second label paste
02:11:32
it but we'll change our value to be pick
02:11:35
up to
02:11:38
delivery within our checked property
02:11:40
check to see if shipping is strictly
02:11:43
equal to
02:11:44
delivery if this expression is true then
02:11:47
checked will be true through this will
02:11:49
trigger the onchange event
02:11:51
handler just to reflect the change I'm
02:11:53
going to add a paragraph
02:11:55
element with text of
02:11:58
shipping I will insert some
02:12:01
JavaScript I will insert our shipping
02:12:03
stateful
02:12:05
variable I'm going to add a break after
02:12:07
our first label just for readability I
02:12:10
think that looks better if I were to
02:12:12
select pickup the shipping method
02:12:14
changes to pickup we're going to pick up
02:12:16
our delivery from a nearby store
02:12:19
if I change it to delivery the shipping
02:12:21
method changes to delivery with the US
02:12:24
state hook we can set an initial State
02:12:28
let's assume that if somebody is
02:12:29
ordering something from our site they
02:12:31
will want it delivered I will set the
02:12:34
initial state to be
02:12:36
delivery and that did update but if
02:12:39
somebody does want to pick it up they
02:12:41
can easily just change that by selecting
02:12:43
this radio button all right everybody so
02:12:45
that is the onchange event handler it's
02:12:47
used primarily with form elements
02:12:50
including but not limited to input text
02:12:53
area select and radio buttons onchange
02:12:57
triggers a function every time the value
02:12:59
of the input changes so for example if I
02:13:02
were to type in my name into this input
02:13:04
element that change is immediately going
02:13:06
to be reflected in real time and well
02:13:09
everybody that is the onchange vent
02:13:10
Handler in
02:13:15
react hey what's going on everybody in
02:13:17
today video we're going to create a
02:13:19
Color Picker program using react this
02:13:21
serves as more of an exercise now that
02:13:23
we know how the UST State Hook and the
02:13:25
onchange event handlers work so let's
02:13:28
get started all right let's begin
02:13:29
everybody within our source folder we'll
02:13:31
create a new file for a Color Picker
02:13:35
component this will be a jsx file we
02:13:38
will use function based components
02:13:41
function Color Picker there are no
02:13:45
parameters then let's be sure to export
02:13:47
this export default Color
02:13:51
Picker we need to return something for
02:13:54
the time being I'm going to return a
02:13:56
fragment just so that everything works
02:13:59
going back to our app component we will
02:14:02
import our Color Picker component from
02:14:06
its
02:14:09
location SLC colorpicker
02:14:13
djsx then let's include one Color Picker
02:14:16
component and that is all we need with
02:14:19
the app component heading to the Color
02:14:21
Picker component we will need to import
02:14:23
the UST State hook import from react
02:14:27
we'll use object destructuring just to
02:14:30
get the UST State Hook from
02:14:34
react within our Color Picker function
02:14:37
at the top we'll use the UST State hook
02:14:40
to create a stateful color
02:14:42
variable const we're going to use array
02:14:45
D structuring and create a color and a
02:14:48
Setter function for that color we will
02:14:51
use the use State
02:14:53
hook for the color if you would like to
02:14:56
set an initial color you can do that I
02:14:58
recommend using hexadecimal values to
02:15:01
keep it simple for example if you would
02:15:03
like white that would be
02:15:05
6fs personally I'm a fan of using hsl
02:15:08
values for Hue saturation and lightness
02:15:11
but that can get kind of complicated you
02:15:14
would need to pass in a JavaScript
02:15:15
object with those three properties we
02:15:17
stick with heximal values just to keep
02:15:19
it
02:15:20
simple then we'll create our HTML
02:15:22
elements So within the return statement
02:15:25
I will create a div
02:15:30
element this div element will have a
02:15:32
class
02:15:34
name of Color Picker
02:15:39
container with react when using CSS I
02:15:42
like to use a hyphenated naming
02:15:43
convention this development will contain
02:15:46
everything we need for our Color Picker
02:15:48
component within this St elment I will
02:15:50
create an H1 element for a heading The
02:15:53
Heading will be Color
02:15:56
Picker after this H1 element I will
02:15:59
create a nested div
02:16:01
element this nested div element will
02:16:03
have a class name equal to
02:16:08
color- display this is what's going to
02:16:11
change color this
02:16:13
development now I will set the style
02:16:15
attribute equal to some Java script if I
02:16:19
attempt to set the background
02:16:21
color to our color
02:16:25
variable well with any CSS properties in
02:16:27
JavaScript we need to enclose them
02:16:29
within an object so we will use a double
02:16:32
set of curly braces and that would work
02:16:35
if you're embedding some JavaScript and
02:16:37
then you're changing some CSS properties
02:16:40
you'll need to enclose those CSS
02:16:41
properties within a JavaScript object
02:16:44
within this Nesta div element we'll
02:16:46
create a paragraph element ment that has
02:16:49
text of selected
02:16:52
color embed some JavaScript include our
02:16:56
stateful color
02:16:58
variable after this development I will
02:17:01
create a
02:17:02
label this label will have text of
02:17:06
select a
02:17:09
color I'll create an input
02:17:12
element
02:17:16
input
02:17:18
the type attribute is going to be
02:17:20
color we'll have an input element for a
02:17:24
color the default is black I'll set the
02:17:28
value equal
02:17:29
to our state full color
02:17:33
variable then to change the color I will
02:17:36
set the onchange event handler equal to
02:17:40
a call back to a
02:17:42
function we will handle color change but
02:17:47
we still need to to Define this function
02:17:49
that's the last step in this
02:17:51
component so we will create a function
02:17:54
to handle color
02:17:56
change there will be one parameter an
02:18:00
event all we're going to do is use our
02:18:02
Setter function for our color whichever
02:18:06
color we
02:18:07
select that's going to be its value
02:18:09
either RGB hsl or
02:18:13
hexadecimal we will access this
02:18:16
event Target
02:18:18
access its value set the color to
02:18:21
whatever that value is so if I set the
02:18:24
color to be I don't know blue well that
02:18:29
color is going to change to be blue and
02:18:32
that is everything we need for this
02:18:33
component we haven't applied any CSS yet
02:18:35
so it's not very impressive although it
02:18:38
does have functionality so be sure to
02:18:40
save everything we will go to our CSS
02:18:44
stylesheet let's select the body of our
02:18:46
document I will set the font family to
02:18:50
be aial with a backup of s
02:18:54
serif I will take our class of Color
02:18:59
Picker
02:19:01
container this is what contains
02:19:04
everything this div
02:19:07
element take our Color Picker container
02:19:10
I will use
02:19:11
flexbox because I like
02:19:13
flexbox display
02:19:16
Flex Flex
02:19:18
direction will be a column right now
02:19:20
it's a
02:19:22
row and
02:19:24
align items in the
02:19:27
center much
02:19:29
better let's take our H1 element that
02:19:31
would be this heading we are selecting
02:19:35
H1 let's add some margin of 50
02:19:40
pixels and increase the font size to 3
02:19:46
r
02:19:48
let's select our color display that
02:19:49
would be this element right
02:19:51
here that has a class of color-
02:19:57
display I will set a width of 300
02:20:02
pixels a height of 300
02:20:06
pixels I'll use flex box for all the
02:20:08
elements within display
02:20:13
Flex
02:20:14
justify
02:20:16
content
02:20:17
in the
02:20:19
center then
02:20:21
align items
02:20:24
Center I'll add a border around this
02:20:28
element border 5 pixel
02:20:33
solid I'll use hsl for the
02:20:37
color let's pick something light gray
02:20:41
I'll set the lightness to be
02:20:43
80% then I'll round the corners with
02:20:45
border
02:20:46
radius
02:20:48
border
02:20:49
radius 25
02:20:52
pixels I'll add a little bit of margin
02:20:54
to the bottom margin bottom 25
02:20:58
pixels so when we select a color it's
02:21:02
going to
02:21:04
change I'm going to add a transition
02:21:06
effect so the transition is a little
02:21:09
more smooth I will set the transition
02:21:14
property after a quarter of a second 0 2
02:21:17
5 Seconds we will ease this
02:21:20
transition so this animation should be
02:21:23
more
02:21:30
smooth let's select this paragraph
02:21:32
element and style it take our color
02:21:35
display
02:21:36
class access the paragraph element
02:21:39
within for the color I'll use hsl
02:21:42
values 0
02:21:46
0% then
02:21:48
20% this will make the text a dark gray
02:21:51
color although it's kind of small you
02:21:53
can't really see
02:21:54
it I'll increase the font
02:21:56
size to 2
02:22:00
RM then text align
02:22:04
Center let's work on the text of the
02:22:07
label we're going to select our label
02:22:10
increase the font size to 1.5
02:22:14
RM let's set the font weight to be
02:22:20
bold and add a little bit of margin to
02:22:22
the
02:22:23
bottom margin bottom 10
02:22:27
pixels the last thing we need to do is
02:22:29
change this input element so it's a
02:22:31
little bigger I will select our input
02:22:34
element but I don't want to select all
02:22:36
input elements I'll use an attribute
02:22:38
selector select all input elements that
02:22:40
have a type of
02:22:43
color so that'll be just this
02:22:46
one I will set a width of 75
02:22:51
pixels a height of 50
02:22:55
pixels some padding of 5
02:23:00
pixels I'll set a border radius to round
02:23:02
the
02:23:03
corners of 10
02:23:06
pixels then set the border to be 3
02:23:09
pixels solid and for the color I'll use
02:23:13
hsl zero for the Hue zero for the
02:23:16
saturation
02:23:18
80% for the lightness that should give
02:23:20
us a thick gray border all right
02:23:22
everybody so that is a Color Picker
02:23:24
program you can create as an exercise
02:23:26
for
02:23:30
react yo what's going on everybody so in
02:23:33
today's video I got to explain updator
02:23:35
functions in react an updator function
02:23:38
is a function passed as an argument to
02:23:41
set State usually for example I have a
02:23:44
Setter function for a year if I need to
02:23:46
increment my year by one I would just
02:23:48
add plus one to year this does in fact
02:23:51
work but a better practice would be to
02:23:54
pass in an updator function as an
02:23:57
argument usually this is represented as
02:24:00
an arrow function by passing in an
02:24:03
updater function this allows for safe
02:24:05
updates based on the previous state you
02:24:08
typically see updator functions used
02:24:10
with multiple State updates and
02:24:12
asynchronous functions it is a good
02:24:15
practice to use updator functions when
02:24:17
ever possible so in this basic example
02:24:20
I've made a counter program we can
02:24:22
increment count decrement count and
02:24:25
reset count within each of these
02:24:28
functions I'm taking my state variable
02:24:30
of count adding + one minus one or set
02:24:34
and count to equal zero in the case of
02:24:36
reset I don't want to increment once I
02:24:39
would like to increment twice I will
02:24:42
take set count update the state again
02:24:45
using count plus one this is different
02:24:47
for me adding two to count I want to
02:24:50
increment count by one twice I will set
02:24:53
the state of count twice count + one
02:24:56
then count plus one if I attempt to run
02:24:59
this I will increment count twice with
02:25:02
every button click however our count
02:25:05
only increases by one during each button
02:25:07
click I'm very persistent let's try and
02:25:10
set count again because you know the
02:25:13
first two times didn't work during each
02:25:16
button click
02:25:17
I would like to increment count by one
02:25:19
three times but we're still only
02:25:21
updating by one still here's the reason
02:25:23
why we're using the current state of
02:25:26
count to calculate the next state after
02:25:29
using a Setter function this doesn't
02:25:31
trigger an update react batches together
02:25:34
updates for performance reasons you
02:25:36
would imagine that after these functions
02:25:38
we would
02:25:42
update but that's not necessarily the
02:25:45
case using react we batch together the
02:25:48
State updates for performance reasons in
02:25:50
reality it's going to look something
02:25:52
like
02:25:53
this we're taking count which is zero
02:25:56
adding one to it count didn't update yet
02:25:59
it's still zero add one to it take count
02:26:02
which is still zero because we didn't
02:26:03
update add one to it in reality what
02:26:06
we're doing is setting count to be one
02:26:09
three separate times and then updating
02:26:12
this would be similar to me hitting the
02:26:13
refresh button on my web browser a bunch
02:26:15
of times
02:26:17
we've sent a bunch of commands to
02:26:18
refresh the web browser but we may only
02:26:21
end up refreshing once not each time I
02:26:23
click the button that's because our web
02:26:25
browser has its own event Loop cycle
02:26:28
that's a similar way of thinking about
02:26:29
it if for some reason you need to use
02:26:32
multiple State updates you'll want to
02:26:34
use an updator function here's
02:26:37
how we're going to write a function our
02:26:40
function has one
02:26:41
parameter count in this case Arrow then
02:26:45
do this let's write an arrow function
02:26:48
for each set
02:26:53
count so this will work however you'll
02:26:56
want to rename count to be something
02:26:58
else we're going to be working with the
02:27:00
previous state of count not the current
02:27:03
state we're using the previous state of
02:27:05
count to calculate the next state
02:27:08
according to the react documents you'll
02:27:10
want to rename count as either something
02:27:12
like previous
02:27:15
count
02:27:20
such as this or you can take the first
02:27:23
letter of the state variable in this
02:27:25
case
02:27:30
C I find taking the first letter of the
02:27:33
state variable much more easier to read
02:27:35
it's less ver Bose so this will work
02:27:37
then when I press the increment button
02:27:40
we're updating the state three separate
02:27:42
times C represents the previous count
02:27:46
not the current count with our updator
02:27:48
function we're taking the pending state
02:27:51
to calculate the next state not the
02:27:54
current state we don't update until
02:27:57
later when we pass in an updator
02:27:59
function we're adding all of these
02:28:01
updator functions to a q a q is a type
02:28:04
of data structure a q is similar to a
02:28:07
bunch of people waiting in line it's
02:28:09
first come first serve the first person
02:28:11
in line is going to be helped first so
02:28:13
during the next render it will call all
02:28:16
of these functions in order rather than
02:28:18
batch them all together when using
02:28:21
multiple State updates you'll want to be
02:28:23
sure to use updator functions it is good
02:28:25
practice whenever possible to use
02:28:26
updator functions just for consistency
02:28:29
even if you're only going to update the
02:28:31
state
02:28:34
once it would still be good practice to
02:28:36
use an updator function it will help
02:28:38
future proof your code in case you ever
02:28:40
need to update the state
02:28:43
again so now let's take our decrement
02:28:46
function currently we're updating the
02:28:48
current state of count not the previous
02:28:50
state let's begin by setting up a
02:28:52
parameter of count Arrow then do this
02:28:57
but we'll want to be sure to rename
02:28:58
count just to show that we're working
02:29:00
with the previous state not the current
02:29:02
state let's rename count as the first
02:29:05
letter of the state variable which is c
02:29:08
c arrow take C minus 1 let's do this two
02:29:12
additional times now when we increment
02:29:15
we will increment 3 separate times and
02:29:18
we can decrement three separate times
02:29:21
now last we have
02:29:22
reset with the setter function we're not
02:29:25
dependent on the previous state all
02:29:27
we're doing is setting count to be zero
02:29:30
if I was going to use an updator
02:29:31
function it would look like this C Arrow
02:29:35
C equals z we really don't care about
02:29:38
the previous state this would still
02:29:41
technically work we don't really need
02:29:43
our count for anything we're setting c
02:29:45
equal to zero because by passing in zero
02:29:48
we're already setting it to be zero in
02:29:50
this specific example an updator
02:29:52
function wouldn't be necessary because
02:29:54
we don't need the previous state all
02:29:56
right everybody so those are updator
02:29:57
functions it's a function passed as an
02:30:00
argument to set State usually when
02:30:04
updating a state variable you'll pass in
02:30:06
an arrow function if we were working
02:30:08
with a state variable of year I would
02:30:10
pass in an arrow function that looked
02:30:11
like this we have a parameter of Y Arrow
02:30:15
let's take y + 1 if I was incrementing
02:30:19
our year by one in this case by using
02:30:21
updator functions they allow for safe
02:30:23
updates based on the previous state not
02:30:26
the current state this allows for
02:30:28
multiple State updates and is used with
02:30:31
asynchronous functions it's a good
02:30:33
practice to use updator functions
02:30:34
whenever possible we will be using
02:30:36
updator functions in the future and well
02:30:38
everybody those are updator functions in
02:30:44
react hey everybody so in today's video
02:30:47
I'm going to show you how we can update
02:30:49
the state of objects be sure that you're
02:30:51
importing the UST State Hook from react
02:30:53
so we'll Begin by creating a constant
02:30:56
use array D structuring our object will
02:30:59
be a state variable of car then we need
02:31:02
a Setter variable for this car set car
02:31:06
equals the use State hook you can set
02:31:10
the initial state with you state so what
02:31:12
we're going to do in this lesson is
02:31:14
create a JavaScript object we're going
02:31:16
to create your favorite car with three
02:31:18
properties a
02:31:20
year a make who manufactured the car and
02:31:24
a model so pick a car that you like I
02:31:28
like a
02:31:31
2024
02:31:33
Ford
02:31:35
Mustang if you would like although not
02:31:37
necessary you can put each of these
02:31:39
properties on a new line for
02:31:42
readability personally I find that
02:31:44
easier to read so car is going to be an
02:31:47
object with these three properties but
02:31:50
later on we're going to update these
02:31:51
properties with a few text boxes within
02:31:54
our return statement we will create a
02:31:56
development to wrap
02:31:59
everything I will create a paragraph
02:32:01
with some
02:32:02
text your favorite car is so we have
02:32:07
that text currently We'll add our cars
02:32:11
year followed by The Cars
02:32:15
make then the cars
02:32:19
model so for me my paragraph States your
02:32:23
favorite car is a 2024 Ford Mustang
02:32:28
after our paragraph element we're going
02:32:30
to create three input elements one for
02:32:33
year make and model so let's do that I
02:32:36
will create an input
02:32:39
element the type of this input element
02:32:42
will be number for the year so we should
02:32:45
have some arrows on this input element
02:32:47
to increment or decrement I will set the
02:32:50
initial value to equal the year of our
02:32:55
car car
02:32:57
doe I'll add a break
02:33:03
afterwards let's copy this input element
02:33:05
paste it twice the second input element
02:33:08
is going to have a type of text for a
02:33:12
string the initial value will be the
02:33:15
make of our car
02:33:16
in my case Ford the third input element
02:33:19
will have type text the value will be
02:33:23
model I have a 2024 Ford Mustang when I
02:33:27
interact with these input elements I
02:33:29
would like to change the property of my
02:33:31
object we'll need to use the onchange
02:33:34
event handler the onchange event handler
02:33:36
is going to accept a call back to some
02:33:39
functions function handle year change
02:33:44
there will be one parameter
02:33:46
an
02:33:47
event the event object is going to be
02:33:50
provided to us through the onchange
02:33:51
event handler when we add that in in a
02:33:54
moment let's copy this function we'll
02:33:56
fill it in
02:33:58
later then we will have a function to
02:34:01
handle make
02:34:02
change then handle model
02:34:05
change we have our three functions to
02:34:08
change the year the make and the model
02:34:10
properties of our object with our input
02:34:13
elements we will set the on change event
02:34:16
handler equal to a call back a call back
02:34:20
to handle year
02:34:21
change so let's set the onchange event
02:34:24
handler to the other two input
02:34:27
elements onchange handle make change
02:34:32
onchange handle model
02:34:36
change the last thing we got to do is
02:34:38
finish filling in our functions we'll
02:34:40
begin with handle year change if I want
02:34:43
to change the state of my car object I
02:34:46
will use the setter function that's
02:34:48
provided to us with use
02:34:50
State set car set of
02:34:53
parenthesis we will be using an updator
02:34:55
function but there's something I want to
02:34:57
explain first we're going to set our car
02:34:59
to be a JavaScript object if I were to
02:35:02
take the year and set it equal to
02:35:05
2025 this is what happens if I interact
02:35:07
with this input element let's say I
02:35:10
decrease this input element well the
02:35:12
value changes to 2025 that worked but
02:35:16
the make and the model of our car
02:35:17
disappeared these properties no longer
02:35:19
exist what we've ended up doing is
02:35:22
replacing the initial object that has
02:35:25
these three properties with a new object
02:35:27
that has this one property of year we
02:35:31
need some way to retain the make and
02:35:32
model of our car the previous properties
02:35:34
that we're not updating that's why we're
02:35:37
going to precede this property to be
02:35:39
changed with the spread operator of our
02:35:41
car when we update the state of our car
02:35:44
we're going to be creating a new new
02:35:46
object spread all of the current
02:35:48
properties of our car and add a year
02:35:51
after using the spread operator it's
02:35:53
going to look like
02:36:00
this we'll have a year make model and
02:36:03
year again if you have an object with
02:36:05
two properties with the same name you'll
02:36:07
end up using the later one and
02:36:09
disregarding the first JavaScript
02:36:11
doesn't allow for duplicate Keys that's
02:36:14
why we're going to be using the spread
02:36:15
operator on our car object than updating
02:36:18
the year that should allow us to retain
02:36:20
the previous properties if I were to
02:36:22
interact with the year we'll retain the
02:36:25
previous properties they won't change
02:36:28
currently the setter is only set to
02:36:30
change the year to 2025 we're going to
02:36:32
access the event object provided to us
02:36:34
from the onchange event handler behind
02:36:36
the scenes I will change the year to be
02:36:39
access our event object access its
02:36:42
Target access its
02:36:44
value when I interact with this input
02:36:47
element the year property is going to
02:36:49
change
02:36:50
accordingly but a better practice with
02:36:52
use state would be to use an updator
02:36:54
function which we learned about in the
02:36:56
last video we don't want to modify the
02:36:59
current state of car but rather make a
02:37:01
reference to the previous state this
02:37:03
allows for safe updates we're going to
02:37:05
turn this object to be part of an arrow
02:37:07
function so let's create our Arrow
02:37:09
function we will take our car Arrow do
02:37:13
this so with an arrow function if you
02:37:16
have a set of curly braces after the
02:37:17
arrow function JavaScript thinks you're
02:37:20
trying to write a multi-line statement
02:37:22
but that's not the case we're trying to
02:37:23
create a JavaScript object what you
02:37:26
could do is surround your set of curly
02:37:27
braces with a set of
02:37:29
parentheses that will allow you to
02:37:31
create an object within an arrow
02:37:33
function since we're working with the
02:37:35
previous state of car and not the
02:37:37
current state we would want to name this
02:37:39
to something else one common naming
02:37:40
convention is to take the first letter
02:37:43
of the state variable and use that to
02:37:45
indicate that this is the previous state
02:37:48
so c will be our parameter we will use
02:37:51
the spread operator on the previous
02:37:53
state of car represented as
02:37:55
C and that should work this allows for
02:37:58
safe updates especially if you're
02:38:00
updating the state of your car more than
02:38:02
one time within a function see my video
02:38:05
on updator functions if you'd like to
02:38:06
learn more about that all right then we
02:38:08
have to handle make change we will use
02:38:12
our setter set car we're going to be
02:38:15
replacing the initial object with a new
02:38:17
object we will use the spread operator
02:38:20
on our
02:38:21
car access the make set it equal to
02:38:25
access our event parameter that's
02:38:27
provided to us access its Target access
02:38:31
its value we'll upgrade the statement so
02:38:33
that it's an updater function let's take
02:38:35
our car Arrow do
02:38:40
this then we will rename car as C to
02:38:43
indicate that we're working with the
02:38:45
previous state of car not the current
02:38:48
state I can change my year and I can
02:38:51
change the
02:38:52
make and that should
02:38:54
update we have one last function to work
02:38:57
with handle model change again set
02:39:01
car we're creating a JavaScript object
02:39:05
use the spread operator on our card to
02:39:07
spread the properties so we don't lose
02:39:09
them update the model with our access
02:39:13
our event access its Target access its
02:39:16
value then we'll change this to be an
02:39:18
updator function we have parameter of
02:39:21
car Arrow do this since we're creating a
02:39:24
JavaScript object within an arrow
02:39:26
function we need to surround it with a
02:39:28
set of
02:39:29
parentheses and then rename car as C to
02:39:33
indicate we're working with the previous
02:39:34
state and that should work I will change
02:39:38
the
02:39:39
year the
02:39:41
make and the
02:39:44
model
02:39:46
all right everybody so that is how we
02:39:47
can update the state of an object using
02:39:54
react hey what's going on everybody in
02:39:56
today's video I'm going to show you how
02:39:58
we can update the state of an array
02:39:59
using react all right everybody here's
02:40:02
how we can update the state of an array
02:40:03
using react be sure to import the UST
02:40:05
State Hook from react we'll Begin by
02:40:08
creating a constant use array D
02:40:10
structuring the name of our array will
02:40:13
be Foods because I'm hungry like you ual
02:40:16
we'll need a Setter function for the
02:40:17
state variable set Foods equals use
02:40:23
State we will set the initial state to
02:40:26
be an empty array if you would like to
02:40:29
include some initial elements you can
02:40:30
put that within here such as an
02:40:33
apple an orange and a
02:40:38
banana we'll create two functions a
02:40:40
function to add an element to this array
02:40:43
and another function to remove an
02:40:44
element we'll create a function
02:40:47
to handle add
02:40:51
food there are no
02:40:53
parameters and we'll declare a function
02:40:56
to handle remove
02:40:59
food we'll fill these in later within
02:41:02
our return statement let's wrap
02:41:04
everything within a div
02:41:08
element I will create an H2
02:41:13
heading that says list of
02:41:17
food we'll create an unordered list with
02:41:20
a pair of UL tags within my unordered
02:41:23
list we will embed some JavaScript code
02:41:26
let's take our array of
02:41:28
foods use the built-in map method the
02:41:31
map method will return a new array what
02:41:33
would we like to do with all of these
02:41:35
elements of our array well we're
02:41:37
provided with an element which we will
02:41:39
name as food and an index these are the
02:41:43
parameters that we'll receive
02:41:44
automatically
02:41:45
we'll write an arrow function to do
02:41:47
something I will create a list item
02:41:50
element we have three already because
02:41:53
our array initially has three elements
02:41:55
an apple orange and a banana if I were
02:41:57
to go to my
02:41:59
console react wants us to add a key for
02:42:02
each list item so it can keep track of
02:42:05
them so with the opening list item tag I
02:42:08
will set the key attribute to equal our
02:42:12
index within each list item I I will add
02:42:16
the current food element and let's see
02:42:19
if this works yes it does Apple orange
02:42:22
banana if I were to change the initial
02:42:25
values of this array within new state
02:42:27
that should be
02:42:28
reflected after the unordered list we'll
02:42:31
create an input element to enter in the
02:42:32
names of some
02:42:34
food I will create an input
02:42:37
element the type the type will be text
02:42:41
because we're adding a
02:42:42
string I will set the ID equal to be
02:42:47
food
02:42:48
input I'll add a placeholder too I will
02:42:52
set the placeholder attribute to be
02:42:55
enter food
02:42:59
name after this input element let's
02:43:02
create a
02:43:04
button the button will have text of add
02:43:09
food with this button I will set the
02:43:12
onclick event handler equal to to a call
02:43:16
back to handle ad food let's work on the
02:43:19
handle ad food function we need to get
02:43:21
the value found within this input
02:43:23
element if I were to type coconut then
02:43:26
press the button I need to get this
02:43:28
value but first we'll select this input
02:43:30
element to access it I will create a
02:43:33
constant of new food new food will be a
02:43:37
string it's going to contain this value
02:43:40
I will access this element this input
02:43:43
element document. get element by ID the
02:43:48
ID that I'm getting is food
02:43:51
input I would just like the value not
02:43:54
the entire
02:43:55
element so if I were to type in coconut
02:43:58
press the button new food is going to be
02:44:00
a string of coconut then after pressing
02:44:03
the button I'm going to reset this input
02:44:06
element so it clears like this I can do
02:44:09
that by copying this line of code where
02:44:11
we access the value set it equal to be
02:44:13
an empty string so if I were to type in
02:44:16
something it should reset after hitting
02:44:19
the button which it does let's try a
02:44:22
mango and that resets too so we have the
02:44:26
string that contains our new
02:44:28
food we'll use our Setter to update our
02:44:34
array if I were to pass in a new array
02:44:37
then add new
02:44:39
food this is what happens I will attempt
02:44:42
to add a coconut add food our initial
02:44:45
values of Apple orange and banana
02:44:47
they're all gone we've replaced the
02:44:50
initial array with the new one that
02:44:52
contains only one string element our new
02:44:54
food that we're trying to add we need to
02:44:57
copy over the previous elements of this
02:45:00
initial state within this new array when
02:45:02
setting the state we will use the spread
02:45:05
operator and access our array of
02:45:08
foods the spread operator when used on
02:45:10
an array will spread all of these
02:45:12
elements into separate values it'll look
02:45:15
something like
02:45:20
this we're replacing the initial state
02:45:22
of this array with a new one so we need
02:45:25
to copy the old elements over so this
02:45:28
does work I will attempt to add a
02:45:30
coconut and a main
02:45:33
go however it would be best practice if
02:45:36
we were to use an updator function we
02:45:38
don't want to directly work with the
02:45:40
current state of a state variable rather
02:45:43
the previous state I will will upgrade
02:45:45
the statement into an updator function
02:45:48
we will take Foods Arrow do this you
02:45:52
don't want to use the same name as the
02:45:54
current state variable a common naming
02:45:56
convention is to take the first letter
02:45:58
of your state variable and use that as a
02:46:01
parameter foods will become f for the
02:46:04
previous state we will use the spread
02:46:06
operator on the previous state of foods
02:46:10
and that should work we have an apple
02:46:12
orange banana we can add a coconut
02:46:16
or a
02:46:18
Maino now we're going to complete the
02:46:20
handle remove food function there's one
02:46:23
or two things I need to change make sure
02:46:25
we use camel case naming convention I
02:46:28
accidentally made the r lowercase
02:46:30
there's going to be one parameter an
02:46:32
index number because we'll be working
02:46:35
with the index numbers of these list
02:46:37
item
02:46:38
elements within our unordered list we'll
02:46:41
be working with the opening list item
02:46:42
tag just for readability I'm going to
02:46:45
put this on a new
02:46:49
line like so within the opening list
02:46:52
item tag we will set the onclick event
02:46:55
handler equal to a callback a call back
02:46:58
to the handle remove food
02:47:01
function this function needs a parameter
02:47:04
in order for us to pass in a parameter
02:47:05
to the handle remove food callback we
02:47:08
need to convert this to an arrow
02:47:10
function we'll create an arrow with no
02:47:12
parameters Arrow do this
02:47:16
within the map method we're provided
02:47:17
with an index number we'll be passing in
02:47:19
that index number to this function so we
02:47:22
can work with it within the handle
02:47:24
remove food function we will use the
02:47:27
setter for our
02:47:28
Foods within the setter we'll use the
02:47:31
filter method we will take our Foods use
02:47:35
the built-in filter method of
02:47:37
arrays with the filter method of an
02:47:39
array we are
02:47:41
provided with an element and an index
02:47:44
number for that element however we do
02:47:47
have a naming conflict we've already
02:47:49
stated that the parameter of this
02:47:51
function is going to be index just to
02:47:53
differentiate it I'm going to rename
02:47:55
index as I so within the filter function
02:47:58
we will use an arrow function to do
02:48:00
something we're going to filter all
02:48:03
elements where the current index
02:48:06
represented as I does not equal the
02:48:09
index that we
02:48:10
receive so if you take a look our
02:48:13
element parameter is provided to us but
02:48:15
we're not using it you can see that it's
02:48:17
kind of grayed out we're going to
02:48:19
replace this with an underscore using an
02:48:21
underscore for a parameter is a
02:48:23
convention to indicate that the
02:48:25
parameter being passed to the function
02:48:27
is ignored so with parameters if you see
02:48:29
an underscore that means ignore it all
02:48:32
right and this should
02:48:33
work I will add a
02:48:36
coconut a
02:48:38
mango then I can click on one of these
02:48:40
list items to remove it let's remove our
02:48:43
banana our Apple
02:48:45
and the mango all right everybody so
02:48:48
that is how we can update the state of
02:48:49
arrays using
02:48:54
react hey everybody in today's video I'm
02:48:57
going to show you how we can update the
02:48:59
state of an array of objects if you're
02:49:01
able to comprehend this lesson and
02:49:02
you've watched previous topics we'll be
02:49:04
ready to make a few projects and react
02:49:07
so let's get started like we've done in
02:49:09
Prior lessons be sure to import the UST
02:49:11
State hook in this topic we're not going
02:49:14
to create one state variable but
02:49:17
four const cars cars is going to be an
02:49:21
array of objects then we need a Setter
02:49:24
for our array of cars equals use
02:49:29
state I'm going to set the initial state
02:49:32
to be an empty
02:49:33
array then we will create a state
02:49:36
variable for the cars year and a Setter
02:49:40
for car
02:49:42
year use state
02:49:46
if we're going to be adding car objects
02:49:48
to make it easier for me I could set the
02:49:50
initial state to be a year like 2024
02:49:54
something that's kind of recent but I
02:49:56
think it would be a lot cooler if we use
02:49:59
JavaScript to get the current year right
02:50:01
now I'm filming this video in December
02:50:03
2023 for the initial state I'm going to
02:50:06
create a new date object then get the
02:50:10
full year method for me this is going to
02:50:14
turn
02:50:15
2023 but depending on when you're
02:50:17
watching this video it might be later
02:50:20
we'll create a state variable for our
02:50:22
cars make who manufactured the
02:50:25
car set car make equals use State I will
02:50:32
set the initial state to be an empty
02:50:34
string then we need the model I'll just
02:50:37
copy this because I'm lazy change make
02:50:39
to be
02:50:41
model set car model
02:50:45
the initial State for the model will be
02:50:46
a string all right now we're going to
02:50:49
declare all the functions we'll need
02:50:51
throughout this
02:50:52
application we'll create a function to
02:50:54
add a car remove a car change the year
02:50:57
change the make change the model five
02:51:00
functions total we'll declare a function
02:51:04
to handle add
02:51:09
car then a function to handle remove car
02:51:14
are there is one parameter an
02:51:18
index what is the index of the object
02:51:21
that we're removing from this
02:51:23
array we need a Handler for year make
02:51:26
and
02:51:27
model function
02:51:29
handle year
02:51:32
change there is one parameter an event
02:51:35
that will be provided to us through the
02:51:37
onchange event handler we need a
02:51:40
function to handle make change handle
02:51:44
make
02:51:46
change and handle model
02:51:49
change so we should have five functions
02:51:52
total add car remove car change the year
02:51:56
change the make change the
02:51:58
model let's create our HTML elements So
02:52:02
within our return statement I will
02:52:04
create a div to enclose
02:52:07
everything let's create an H2 element
02:52:10
for a heading list of car
02:52:16
objects afterwards I will create an
02:52:18
unordered
02:52:19
list we will populate this unordered
02:52:22
list
02:52:25
later we will create three input
02:52:27
elements for the year the make and the
02:52:30
model of our car that we're trying to
02:52:31
add we will begin with input that has a
02:52:36
type of
02:52:39
number I will set the value to equal
02:52:44
this state of car year and for me that's
02:52:49
2023 that's because I'm creating a date
02:52:51
object of the current date and time then
02:52:54
returning the year depending on what
02:52:56
year you're watching this it might be
02:52:58
different I will set the on change event
02:53:01
handler equal to a call back to handle
02:53:05
year
02:53:06
change I'll add a break
02:53:11
afterwards okay let's copy this input
02:53:14
element including the break the type of
02:53:16
the second input element will be
02:53:18
text the value will be the cars make who
02:53:23
made the car the onchange event handler
02:53:25
will be a call back to handle make
02:53:28
change we should add a placeholder to
02:53:30
tell the user what we want them to type
02:53:32
in it's not very apparent I'll put this
02:53:35
on a new line for readability I will set
02:53:37
the placeholder attribute equal
02:53:40
to enter car make
02:53:45
let's copy this element paste it for the
02:53:48
third input element the value will be
02:53:51
car model the onchange event handler
02:53:54
will be a call back to handle model
02:53:57
change the placeholder will be enter car
02:54:01
model then we'll create a button to
02:54:03
submit our car
02:54:05
object
02:54:07
button the text on the button will be
02:54:10
add
02:54:11
car we will set the on click event
02:54:15
handler equal to handle
02:54:21
Adar all right now we have to fill in
02:54:23
these
02:54:24
functions we'll begin with handle year
02:54:26
change because it's easy we will use the
02:54:29
setter for set car
02:54:32
Year we will pass in Access our event
02:54:36
object that's provided to us access its
02:54:39
Target access its
02:54:41
value then we have to do this with make
02:54:43
and
02:54:45
model set car
02:54:47
make access the value of the target of
02:54:50
the event pass it as an
02:54:53
argument set car
02:54:56
model again take our event access its
02:54:59
Target access its value so now we should
02:55:03
be able to update these values let's try
02:55:10
it when I click on the button I would
02:55:13
like to add this object to my array of
02:55:16
objects within our handle Adar function
02:55:19
we have a few things to write it's not
02:55:21
as complicated as what you think it
02:55:22
might be using these State variables of
02:55:25
year making model we will create a new
02:55:27
car const new car equals a JavaScript
02:55:33
object with these properties the year
02:55:36
will equal the current state of car
02:55:40
year the make will equal the current
02:55:43
state of of car make the model will
02:55:46
equal the current state of car model
02:55:50
then just for readability I'm going to
02:55:51
put all these properties on a new line
02:55:53
not necessary but I find it easier to
02:55:55
read we will use the setter for our
02:55:58
array of car
02:56:01
objects we will use an updator function
02:56:04
we will take
02:56:05
cars Arrow create a new array use the
02:56:10
spread operator on cars we need to copy
02:56:14
the previous elements over to the new
02:56:16
array we don't want to lose them then we
02:56:18
will add a new element our new car
02:56:22
object so we don't want to use the
02:56:24
current state of cars but rather the
02:56:27
previous state with this updator
02:56:29
function a common naming convention for
02:56:31
your parameter is to use the first
02:56:33
letter of that state variable let's
02:56:36
rename cars as
02:56:38
C so what we're going to do now is go to
02:56:42
our unordered list we need we need to
02:56:44
populate it with list items I will embed
02:56:46
some JavaScript take our array of cars
02:56:50
use the built-in map method to return a
02:56:52
new array we're provided with two
02:56:55
arguments an element and an index with
02:56:58
the map method but I'm going to rename
02:57:01
element as car so it's more
02:57:03
understandable what would we like to do
02:57:06
I'll use an arrow function we will
02:57:09
return a list item element and for
02:57:12
readability I'm just going to place
02:57:13
these on a new
02:57:14
line react wants us to add a key to each
02:57:18
list item element so let's take care of
02:57:20
that I will set the key of each list
02:57:23
item element to be our index that's
02:57:26
provided to us within each list item
02:57:29
element I will display the cars year
02:57:33
followed by The Cars make and the cars
02:57:38
model all right let's see if this works
02:57:42
let's add a 2023 Ford Mustang and that
02:57:47
worked how about a
02:57:49
2024
02:57:51
Chevrolet
02:57:53
Corvette and a
02:57:56
2022 Dodge
02:58:01
Challenger one thing I'm going to add to
02:58:03
the handle Adar function after
02:58:06
submitting a car I would like to reset
02:58:08
these
02:58:09
values at the end of this function let's
02:58:12
use the setters
02:58:14
I will set the car year when we're done
02:58:16
with it to be the current year we'll
02:58:19
create a new date object get the F year
02:58:22
update the state to whatever value is
02:58:24
returned we'll use the setter for the
02:58:27
car make and reset it to be an empty
02:58:29
string same thing with the car
02:58:36
model all right let's see if this resets
02:58:40
2023 Ford Mustang and it
02:58:44
does
02:58:47
2025
02:58:49
Chevrolet
02:58:52
Camaro and that does reset you can see
02:58:55
that my year went back to 2023 even
02:58:58
though I adjusted it to 2025 the make
02:59:00
and model were both replaced with empty
02:59:02
strings now I would like to remove a car
02:59:05
when I click on one of these list item
02:59:07
elements we'll make one change to the
02:59:10
opening list item tag I will set the
02:59:14
unclick event handler to a call back to
02:59:18
handle remove
02:59:21
car we do have to pass in an argument
02:59:23
though to this function we have to pass
02:59:26
in an index what is the index of the car
02:59:29
we're trying to
02:59:30
remove if I attempt to pass in an
02:59:33
argument we will call this function
02:59:35
right away we will convert this call
02:59:37
back to an arrow
02:59:39
function that has no
02:59:42
parameters
02:59:44
So within the handle remove car function
02:59:47
there's not a lot to write we will use
02:59:50
the setter for our
02:59:54
cars we'll use an updator function we
02:59:57
don't want to use cars but we'll use the
02:59:59
first letter to indicate we're working
03:00:00
with the previous state
03:00:03
arrow take the previous state of our
03:00:05
array of cars represented as C use the
03:00:08
built-in filter method we will filter
03:00:11
any cars that don't meet a certain
03:00:12
criteria the filter method provides us
03:00:16
with an element and an index but we do
03:00:19
have a naming conflict we already have a
03:00:21
parameter with the name of index let's
03:00:23
rename the index of each element within
03:00:26
our array as
03:00:28
I then do this with an arrow function
03:00:31
we'll write our condition check to see
03:00:34
if the index of the current element I is
03:00:38
not strictly equal
03:00:40
to the index we would like to remove
03:00:44
we'll filter out that element when we
03:00:46
create a new array so we're not working
03:00:48
with the current element even though
03:00:50
it's provided to us so what people do as
03:00:53
a convention if you have a parameter
03:00:54
you're not using people will change the
03:00:56
name of that parameter to be an
03:00:58
underscore that tells other people to
03:01:00
ignore this parameter and that should
03:01:02
work let's run and test everything let's
03:01:05
add a 2023 forward Mustang but feel free
03:01:08
to pick some different
03:01:10
cars
03:01:11
2024 shev roll
03:01:17
Corvette
03:01:20
2022
03:01:21
Dodge
03:01:23
Challenger and I should be able to
03:01:24
remove a car when I click on it that
03:01:26
list item element our Corvette is gone
03:01:30
our Mustang and our challenger all right
03:01:33
everybody so that is how to create an
03:01:35
array of objects and update their state
03:01:37
using
03:01:41
react hey everybody in today's video I'm
03:01:44
going to walk you through creating a
03:01:45
to-do list app using react so sit back
03:01:48
relax and enjoy the
03:01:52
show all right let's do this everybody
03:01:54
we'll need to create a new jsx file
03:01:56
going to our source folder we will
03:01:58
rightclick new file this component will
03:02:00
be named too list.
03:02:04
jsx this will be a function-based
03:02:07
component function to do list no
03:02:11
parameters then let's be sure to export
03:02:13
it because because if I don't do it now
03:02:14
I'm going to forget export default to do
03:02:18
list we will be using the use State hook
03:02:21
we should import that
03:02:24
import react use object D structuring I
03:02:28
would like just the use State Hook from
03:02:31
its location of
03:02:33
react we do need to return something for
03:02:36
now we're just going to return a
03:02:39
fragment going back to our app component
03:02:41
we need to import our to-do list
03:02:43
component
03:02:44
component import to-do list from its
03:02:48
relative file path SL too list.
03:02:53
jsx we will return one to-do list
03:02:57
component and we are ready to move on
03:03:00
within our to-do list component we will
03:03:02
have two State variables const tasks
03:03:06
tasks will be an array of strings we
03:03:08
need a Setter function for tasks set
03:03:11
tasks equals the use State hook the
03:03:16
initial state of tasks is going to be an
03:03:18
empty array but eventually we're going

Description:

This is a beginners React JS course that should be enough to get you started using the React Library. There are still many more hooks and concepts that are beyond the scope of the beginner level. This video should at least give you a solid foundation. 1. 00:00:00 React tutorial for beginners ⚛️ 2. 00:20:26 card components 🃏 3. 00:32:24 add CSS styles 🎨 4. 00:40:40 props📧 5. 00:52:49 conditional rendering ❓ 6. 01:03:04 render lists 📃 7. 01:29:43 click events 👆 8. 01:42:04 useState() hook 🎣 9. 01:58:36 onChange event handler 🚦 10. 02:13:16 color picker app 🖌 11. 02:23:31 updater functions 🔄 12. 02:30:45 update OBJECTS in state 🚗 13. 02:39:55 update ARRAYS in state 🍎 14. 02:48:55 update ARRAY of OBJECTS in state 🚘 15. 03:01:42 to-do list app ☝ 16. 03:24:17 useEffect() hook 🌟 17. 03:44:08 digital clock app 🕒 18. 04:00:08 useContext() hook 🧗‍♂️ 19. 04:11:44 useRef() hook 🗳️ 20. 04:23:01 stopwatch app ⏱ Copyright Disclaimer: This video is the intellectual property of Bro Code. All rights reserved. No part of this video may be reproduced, distributed, or transmitted in any form or by any means, including but not limited to recording, uploading, or other electronic or mechanical methods, without my written permission, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law.

Preparing download options

popular icon
Popular
hd icon
HD video
audio icon
Only sound
total icon
All
* — If the video is playing in a new tab, go to it, then right-click on the video and select "Save video as..."
** — Link intended for online playback in specialized players

Questions about downloading video

mobile menu iconHow can I download "React Full Course for free ⚛️ (2024)" video?mobile menu icon

  • http://unidownloader.com/ website is the best way to download a video or a separate audio track if you want to do without installing programs and extensions.

  • The UDL Helper extension is a convenient button that is seamlessly integrated into YouTube, Instagram and OK.ru sites for fast content download.

  • UDL Client program (for Windows) is the most powerful solution that supports more than 900 websites, social networks and video hosting sites, as well as any video quality that is available in the source.

  • UDL Lite is a really convenient way to access a website from your mobile device. With its help, you can easily download videos directly to your smartphone.

mobile menu iconWhich format of "React Full Course for free ⚛️ (2024)" video should I choose?mobile menu icon

  • The best quality formats are FullHD (1080p), 2K (1440p), 4K (2160p) and 8K (4320p). The higher the resolution of your screen, the higher the video quality should be. However, there are other factors to consider: download speed, amount of free space, and device performance during playback.

mobile menu iconWhy does my computer freeze when loading a "React Full Course for free ⚛️ (2024)" video?mobile menu icon

  • The browser/computer should not freeze completely! If this happens, please report it with a link to the video. Sometimes videos cannot be downloaded directly in a suitable format, so we have added the ability to convert the file to the desired format. In some cases, this process may actively use computer resources.

mobile menu iconHow can I download "React Full Course for free ⚛️ (2024)" video to my phone?mobile menu icon

  • You can download a video to your smartphone using the website or the PWA application UDL Lite. It is also possible to send a download link via QR code using the UDL Helper extension.

mobile menu iconHow can I download an audio track (music) to MP3 "React Full Course for free ⚛️ (2024)"?mobile menu icon

  • The most convenient way is to use the UDL Client program, which supports converting video to MP3 format. In some cases, MP3 can also be downloaded through the UDL Helper extension.

mobile menu iconHow can I save a frame from a video "React Full Course for free ⚛️ (2024)"?mobile menu icon

  • This feature is available in the UDL Helper extension. Make sure that "Show the video snapshot button" is checked in the settings. A camera icon should appear in the lower right corner of the player to the left of the "Settings" icon. When you click on it, the current frame from the video will be saved to your computer in JPEG format.

mobile menu iconWhat's the price of all this stuff?mobile menu icon

  • It costs nothing. Our services are absolutely free for all users. There are no PRO subscriptions, no restrictions on the number or maximum length of downloaded videos.