text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
So something special
about arrow notation | 2,356.37 | 2.91 |
is that it has an implicit return,
which means we don't actually | 2,359.28 | 3.48 |
have to write-- so we could have
written the same thing as this, | 2,362.76 | 9.142 |
as a function that takes props. | 2,371.902 | 4.945 |
And what does it do? | 2,376.847 | 0.833 |
It returns this. | 2,377.68 | 0.75 |
So this actually does
the exact same thing. | 2,391.37 | 2.529 |
So we're saying, app
two is a function that | 2,393.899 | 1.791 |
takes a single argument called props. | 2,395.69 | 1.78 |
And what we do is we return this, and
so arrow notation shorthand is just | 2,397.47 | 6.2 |
you have your arguments in arrow. | 2,403.67 | 2.19 |
And then if you don't have braces,
which referred to it like a code block, | 2,405.86 | 4.56 |
it just returns whatever's next. | 2,410.42 | 1.65 |
And so we're saying, return this. | 2,412.07 | 2.22 |
What is this? | 2,414.29 | 0.93 |
Well, it's this div and H2. | 2,415.22 | 2.32 |
We're going to wrap it in a parentheses
so we know that it's just one value. | 2,417.54 | 4.91 |
Does that make sense? | 2,422.45 | 1.47 |
A great question. | 2,423.92 | 2.56 |
Do you guys see how these are the same? | 2,426.48 | 2.45 |
So this is great, but we don't really
have all that much power yet, right? | 2,433.56 | 3.71 |
If we wanted to go ahead
and change these props, | 2,437.27 | 2.25 |
we still have to drop down to
this raw JavaScript over here. | 2,439.52 | 4.72 |
So next we'll see exactly how we
create apps that are stateful. | 2,444.24 | 7.597 |
What does that mean? | 2,451.837 | 0.833 |
Well, there's this
notion of state in React, | 2,452.67 | 2.95 |
and state is basically an
internally managed configuration | 2,455.62 | 3.37 |
for any component. | 2,458.99 | 3.36 |
And so now components become classes,
and this .state is a property on that | 2,462.35 | 4.365 |
component's instance. | 2,466.715 | 0.875 |
So how do we update the state? | 2,470.66 | 1.25 |
Well, there's a method
called this.setState, | 2,471.91 | 5.04 |
which is implemented in this thing
called a React.Component, which | 2,476.95 | 2.82 |
we have to extend in order to
have access to that method. | 2,479.77 | 4.86 |
And this goes ahead
and changes that value. | 2,484.63 | 4.38 |
So you can pass an object to be merged
or a function of the previous state. | 2,489.01 | 3.64 |
And so if we pass
this.setState, an object, | 2,492.65 | 2.53 |
it will go ahead and merge that
in with the existing state. | 2,495.18 | 2.5 |
So if we pass it in an
updater function, it's | 2,497.68 | 2.97 |
basically a function that gets run
when we want to change the state. | 2,500.65 | 3.51 |
And the set state calls are
batched and run asynchronously. | 2,504.16 | 5.65 |
And of course, any change in
state will also cause a re-render, | 2,509.81 | 2.79 |
because it would be silly if
we were to change the state | 2,512.6 | 2.43 |
but not reflect that in the UI. | 2,515.03 | 3.68 |
And so how might we go about
representing state over here? | 2,518.71 | 3.86 |
So first, let me copy this
so that we can save it. | 2,522.57 | 4 |
So let's go ahead, and rather
than having an app be a function, | 2,544.28 | 3.81 |
let's actually have it be a class. | 2,548.09 | 2.152 |
So we can do class app, and we
want to extend React.Component. | 2,553.61 | 7.38 |
And within that, we want to have
this method called render, which is | 2,567.9 | 4.68 |
automatically invoked on a re-render. | 2,572.58 | 4.58 |
Within render, we want to return this. | 2,581.16 | 3.37 |
Cool. | 2,604.82 | 0.5 |
So the way to now write
this is rather than having | 2,607.84 | 2.82 |
app be a function that takes
props and returns something, | 2,610.66 | 2.895 |
we're actually writing a class for app. | 2,613.555 | 2.055 |
And so as we talked about
earlier, classes have instances. | 2,615.61 | 2.79 |
And React knows that when
you want to render something | 2,618.4 | 3.3 |
like this, if it's a class, go ahead
and create a new instance of that | 2,621.7 | 3.6 |
and pass in these as props. | 2,625.3 | 3.78 |
And notice how we don't ever
take the props anywhere. | 2,629.08 | 2.85 |
That's because when we
extend React Component, | 2,631.93 | 3 |
React Component, that base class,
goes ahead and attaches the props | 2,634.93 | 5.4 |
to the instance. | 2,640.33 | 0.99 |
And so in order to get at them, rather
than doing props does something, | 2,641.32 | 3.24 |
we do this dot props dot count. | 2,644.56 | 2.78 |
So again, the props that come in,
in the way that React.Component | 2,647.34 | 3.73 |
is implemented, it
automatically takes the props | 2,651.07 | 2.19 |
and attaches it to that
instance of the class. | 2,653.26 | 3.13 |
And so in order for us to get
them in the render method, | 2,656.39 | 2.72 |
we do this dot props dot count. | 2,659.11 | 3.93 |
Does that make sense so far,
going from a function to a class? | 2,663.04 | 5.13 |
We'll talk about this in
depth the next lecture. | 2,668.17 | 4.24 |
And so we talked about
this thing called state, | 2,672.41 | 2.63 |
and how do we actually
go ahead and use that? | 2,675.04 | 3.03 |
Well, when we want to create
our state, we actually | 2,678.07 | 2.634 |
do that in the constructor method. | 2,680.704 | 1.416 |
And so the first thing that we want
to do in our constructor method | 2,687.63 | 3.2 |
is actually called a
super, which means allow | 2,690.83 | 5.61 |
React.Component to do
stuff with the props | 2,696.44 | 2.34 |
that it would have done otherwise. | 2,698.78 | 2.51 |
And now go ahead and
do what we want to do. | 2,701.29 | 2.244 |
What do we want to do? | 2,703.534 | 0.916 |
Well, we want to initialize
this thing called state. | 2,704.45 | 2.166 |
Cool. | 2,718.83 | 0.5 |
So now we have this thing
called state, and how are we | 2,719.33 | 2.85 |
going to go ahead and update it? | 2,722.18 | 1.98 |
Well, maybe we should have something
called increase count, which | 2,724.16 | 6.9 |
is a method on this instance. | 2,731.06 | 2.962 |
And let's go ahead and
increase the count here. | 2,734.022 | 1.958 |
So how might I do that? | 2,735.98 | 1.5 |
Well, I should call this dot set state
and pass in count is this dot state dot | 2,737.48 | 8.25 |
count plus 1. | 2,745.73 | 3.09 |
And so now we have a method on
this instance called increase count | 2,748.82 | 3.09 |
that we can call. | 2,751.91 | 0.81 |
And it should, in theory,
increase that count. | 2,752.72 | 3.95 |
And so rather than referencing the
props down here, let's reference state. | 2,756.67 | 3.154 |
And now it should be
0, and it's just going | 2,762.67 | 2.24 |
to stay at 0, because
we're not doing anything. | 2,764.91 | 3.052 |
So we can go ahead and
get rid of this interval. | 2,767.962 | 2 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.