00:00:00
react is a JavaScript library full of
00:00:02
fancy terms like reconciliation
00:00:04
composition and error boundaries what do
00:00:07
all these terms actually mean let's
00:00:09
start from the beginning with components
00:00:11
components are the building blocks of
00:00:13
every react app they allow us to make
00:00:15
all the visible parts of our
00:00:16
applications like buttons inputs or even
00:00:19
entire Pages just like Legos you can use
00:00:22
components as many times as you want
00:00:25
every react component is a JavaScript
00:00:27
function that returns markup but since
00:00:29
react is is a JavaScript library react
00:00:31
components don't return HTML markup they
00:00:34
actually return something called jsx
00:00:37
which is Javascript in Disguise jsx is
00:00:40
optional but the alternative way to make
00:00:42
user interfaces is with the function
00:00:44
create element which gets annoying to
00:00:46
write pretty fast so everyone just uses
00:00:49
jsx since jsx is Javascript you can't
00:00:52
write attributes like you would in HTML
00:00:55
you have to write them in the camel case
00:00:57
style that means HTML attributes like
00:01:00
class become class name unlike HTML
00:01:04
which is static and unchanging the
00:01:06
benefit of using react is that you can
00:01:09
use Dynamic JavaScript values in your
00:01:11
jsx if you have data you can display it
00:01:14
in your jsx using curly braces curly
00:01:17
braces accept values like strings and
00:01:19
numbers directly you can use them to
00:01:21
make your attributes Dynamic and you can
00:01:24
style react elements using a JavaScript
00:01:27
object within the curly braces since
00:01:29
JavaScript functions can only return one
00:01:31
thing in react you can only return one
00:01:34
parent element from a component so you
00:01:36
can't do this without getting a big
00:01:38
error we could fix this by wrapping
00:01:40
these components in a div but maybe you
00:01:43
don't want to add another element to the
00:01:44
page instead you can use an empty
00:01:47
component called a react fragment okay
00:01:50
but what if I want to pass data into
00:01:52
another component for that you use
00:01:54
something called props to make a prop
00:01:56
create a name on the component you want
00:01:58
to pass data to and set it equal to some
00:02:01
value and that's it you can then use
00:02:03
that prop in the component you passed it
00:02:05
to props refers to properties on an
00:02:08
object which is what you get in the
00:02:10
parameters of each component to use the
00:02:12
prop take it from the object like a
00:02:14
normal JavaScript property think of them
00:02:16
like custom attributes you can add to
00:02:18
any component so wait can you pass
00:02:21
anything as a prop yes you can you can
00:02:24
even pass other components as props
00:02:26
using the children prop if you make
00:02:28
opening and closing tags for a component
00:02:31
you can pass other components in between
00:02:33
them these pass components are called
00:02:35
children and you can access them on the
00:02:38
children promp of the parent component
00:02:40
and it's great for something called
00:02:42
composition which is about organizing
00:02:44
our react components in the most optimal
00:02:46
way the children prop is really useful
00:02:48
for creating layout components when you
00:02:50
want your children to have the same
00:02:52
common layout the key prop is another
00:02:55
built-in prop to react and no unlike the
00:02:58
name implies it doesn't unlock anything
00:03:00
interesting the key prop is used so
00:03:03
react can tell one component apart from
00:03:05
another usually when you're creating a
00:03:08
list with the map function a key is just
00:03:11
a unique string or number to identify a
00:03:13
component you'll usually know when you
00:03:15
need to add a key because react will
00:03:17
warn you in the console fortunately if
00:03:20
you don't have a unique value for each
00:03:22
item you can always use the current
00:03:24
index from the map function but how does
00:03:27
react take all my amazing code and make
00:03:30
it display something in the browser that
00:03:32
process is called rendering react does
00:03:35
this for us but it's important to know
00:03:37
how it works because sometimes we can do
00:03:40
a bad thing and cause it to infinitely
00:03:42
reender which crashes our app the way
00:03:45
react knows how and when to render our
00:03:48
application is using something called
00:03:50
the virtual Dom also known as the vdom
00:03:54
okay but what does Dom mean Dom stands
00:03:56
for document object model which is what
00:03:59
every browser uses to model all the HTML
00:04:02
elements on a web page and when you draw
00:04:05
it out it kind of looks like a tree
00:04:07
here's the complete rendering process in
00:04:10
react if the state of our react app
00:04:12
changes then react updates the virtual
00:04:14
Dom which is quicker to update than the
00:04:16
real Dom then react uses a process
00:04:19
called diffing to compare the updated
00:04:22
virtual Dom to a previous version to see
00:04:24
what's changed once it sees what's
00:04:27
different react uses a process called
00:04:29
reconcil iation to update the real Dom
00:04:32
with the changes that it found whenever
00:04:34
someone uses our app tons of events take
00:04:37
place like clicks Mouse movements and
00:04:39
key presses many of which we need to
00:04:42
detect event handling is how we take
00:04:44
those user events and do something with
00:04:46
them react has a lot of built-in events
00:04:48
such as onclick onchange and onsubmit
00:04:51
these three events are ones you'll
00:04:53
probably use the most if we want to
00:04:55
alert users when a button is clicked we
00:04:58
would add the onclick prop to the button
00:05:00
and connect it to a function that would
00:05:03
show that
00:05:04
alert to manage data in our react apps
00:05:07
we need to use State not that kind of
00:05:09
state though state is like a snapshot
00:05:12
from a camera it's a picture of our app
00:05:14
at any given time to manage State we
00:05:17
also can't use JavaScript variables they
00:05:19
don't cause our app to render instead we
00:05:22
use special functions like use State and
00:05:24
use reducer use State takes an argument
00:05:28
that serves as the starting value value
00:05:30
of the state variable which is likes in
00:05:32
this example and returns an array
00:05:36
containing the state variable and a
00:05:38
function to update that state using our
00:05:41
button example we could also update the
00:05:43
number of times the button's been
00:05:45
clicked with the update function set
00:05:47
clicks and display it in the button with
00:05:51
the state variable
00:05:53
likes controlled components use State
00:05:55
values to have more predictable Behavior
00:05:59
here's an example of a controlled
00:06:00
component where the value typed into the
00:06:03
input is being put into State and
00:06:05
controlled by the state variable value
00:06:09
here's how it works the user types and
00:06:11
set value puts what the user typed into
00:06:13
State the state value is then updated
00:06:17
and finally the input uses that updated
00:06:20
State as its value controlled components
00:06:23
are a great pattern to use because if we
00:06:25
want to change the component's behavior
00:06:27
we just need to change the state that
00:06:29
controls it UST state is an example of a
00:06:32
react hook which allow us to hook into
00:06:35
features such as state within function
00:06:37
components there are five main types of
00:06:40
hooks State hooks like use State and use
00:06:42
reducer help you manage state within
00:06:44
react components context hooks such as
00:06:47
use context let you Ed data pass through
00:06:50
react context ref hooks such as use ref
00:06:54
let you reference things like HTML
00:06:56
elements effect hooks like use effect
00:06:59
let you connect with external systems
00:07:01
like browser apis and performance hooks
00:07:04
like use memo and use callback which can
00:07:07
improve performance by preventing
00:07:08
unnecessary work you'll use all of these
00:07:11
hooks at some point but the majority of
00:07:13
the time you'll likely use just three
00:07:15
hooks in your react components use State
00:07:18
use effect and use ref when you think of
00:07:21
the word purity you might think of
00:07:23
something like purified water Purity is
00:07:26
a term used to describe how react
00:07:28
components should work work but this
00:07:30
type of Purity is more like how
00:07:32
mathematical formulas are pure pure
00:07:34
react components mean that the same
00:07:36
input should always return the same
00:07:38
output to keep a react component pure
00:07:41
they should only return their jsx and
00:07:45
not change any objects or variables that
00:07:47
existed before rendering the cup
00:07:49
component in this example is impure
00:07:52
because it changes the variable count
00:07:54
during render which exists outside the
00:07:57
component this leads to the jsx have
00:07:59
having the wrong output when it is used
00:08:01
more than once to help make sure we
00:08:04
don't run into errors like this we can
00:08:06
use something called strict mode strict
00:08:08
mode is a special component which tells
00:08:10
us about mistakes as we develop our
00:08:12
react apps it's really convenient
00:08:14
because it's just a component we usually
00:08:16
wrap around our app component and it'll
00:08:19
tell us when we really shouldn't do
00:08:21
something but what if we need to do
00:08:23
something outside our react app your app
00:08:26
might need to talk with the browser API
00:08:28
or make a request to a server if you do
00:08:31
have an external system you're going to
00:08:33
need a way to step outside of react
00:08:36
effects are code that reach outside of
00:08:38
our react application usually effects
00:08:41
also known as side effects can be done
00:08:43
within event handlers for example to
00:08:45
make an HTTP request when you submit a
00:08:48
form or click on a button if you can't
00:08:51
run your effects within an event handler
00:08:53
then you can run them using the use
00:08:55
effect hook for example a common pattern
00:08:58
is to fetch dat data when components
00:09:00
first load with the use effect hook like
00:09:04
effects sometimes you want to step
00:09:05
outside react and work directly with the
00:09:08
Dom to reference an actual Dom element
00:09:11
you can use what's called a ref you can
00:09:13
create a ref with the Ed ref hook and to
00:09:16
get access to a Dom element use the ref
00:09:19
prop on any react element for some tasks
00:09:22
such as focusing an input it's much
00:09:24
easier to reference the actual Dom
00:09:26
element instead of attempting to do it
00:09:28
the react way
00:09:30
context is a powerful way to pass prop
00:09:32
data through your apps components most
00:09:35
react apps have tons of nested
00:09:37
components to get data down multiple
00:09:39
levels involves passing the same props
00:09:42
through components that don't actually
00:09:44
need it context lets us jump through the
00:09:47
component tree and use data at any level
00:09:49
without making props to use context you
00:09:52
first create context in a parent
00:09:55
component then wrap your parent
00:09:57
component in a special context component
00:09:59
called a context provider put the data
00:10:02
you want to pass down on the provider
00:10:05
and finally access that data in any
00:10:07
child component with the used context
00:10:10
hook portals on the other hand are kind
00:10:13
of like context but for components
00:10:15
portals let you move react components
00:10:17
into any HTML element you select portals
00:10:21
are great for components that can't be
00:10:22
displayed properly because of their
00:10:24
parents component styles for example for
00:10:26
displaying modals drop-down menus and
00:10:29
tool tips to create a portal just use
00:10:32
the create portal function pass your
00:10:34
component to it and choose the HTML
00:10:37
element where you'd like your react
00:10:39
component to appear suspense is a
00:10:41
special component that helps you handle
00:10:43
loading a component or its data suspense
00:10:46
is helpful for components that take some
00:10:48
time to fetch data it provides a better
00:10:51
user experience to show a fallback
00:10:53
component like a loading spinner until
00:10:55
the data is available instead of nothing
00:10:58
suspense is also useful if you're lazily
00:11:00
loading a component which lets us load a
00:11:03
component only when it's needed since
00:11:06
react apps are all JavaScript errors
00:11:08
that happen during rendering can totally
00:11:10
break your app airor boundaries are
00:11:12
components that let you catch app
00:11:14
breaking errors and show a fallback
00:11:16
component to tell the user about what
00:11:18
happened for example our app will crash
00:11:21
if we run this code because it throws an
00:11:23
error when there's no user to prevent
00:11:25
our app from crashing we'll first add an
00:11:28
error boundary to display a fallback
00:11:30
component with a more helpful error
00:11:32
message to be displayed to the user now
00:11:35
if you really want to dive deep into
00:11:37
react I've put together a complete boot
00:11:40
camp to help you master every one of
00:11:41
these Concepts from front to back you
00:11:44
can get started now at react boot camp.
00:11:47
I hope you learned a lot in this video
00:11:49
and I'll see you in the next one