text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
And so how are we going to change that
value of 0 to be something like 1? | 2,778.17 | 5.87 |
We have to invoke that
method, and so let's go ahead | 2,784.04 | 2.49 |
and do that when we click a button. | 2,786.53 | 2.35 |
So let's have something like
div here, and within that div, | 2,788.88 | 5.834 |
let's create a button. | 2,794.714 | 0.916 |
And when we click that button,
let's go ahead and invoke this thing | 2,800.72 | 5.184 |
called increase count. | 2,805.904 | 0.916 |
And let's call it increase. | 2,810.73 | 1.21 |
So now if we click this button,
what do we expect to happen? | 2,829.57 | 3.12 |
So notice how we pass
this thing called unclick. | 2,838.27 | 2.584 |
What's the value of unclick? | 2,840.854 | 1.166 |
Or what function should we invoke? | 2,842.02 | 1.98 |
Well, we should invoke this dot
increase count, which should be this. | 2,844 | 7.02 |
We'll see what it is in a second. | 2,851.02 | 2.23 |
And then that should call
this dot set state, which | 2,853.25 | 3.11 |
should, in theory, increase count. | 2,856.36 | 1.98 |
So what happens? | 2,858.34 | 1.55 |
Uh-oh! | 2,859.89 | 0.76 |
Cannot read property
set state of undefined. | 2,860.65 | 2.26 |
So there's a bug here. | 2,862.91 | 1.56 |
Can anybody spot the bug? | 2,864.47 | 2.9 |
Cannot read property
set state of undefined. | 2,867.37 | 4.14 |
It even tells us exactly where that was. | 2,871.51 | 3.42 |
So we're seeing this
dot set state, which | 2,874.93 | 2.34 |
I told you is a method that we can use. | 2,877.27 | 3.53 |
But it's saying, we cannot
read a property of undefined, | 2,880.8 | 2.87 |
which implies that
this is undefined here. | 2,883.67 | 3.27 |
Yeah? | 2,886.94 | 0.65 |
What do you think is going on here? | 2,887.59 | 1.458 |
AUDIENCE: [INAUDIBLE] | 2,889.048 | 3.492 |
JORDAN HAYASHI: Yeah. | 2,892.54 | 1.05 |
So we have to bind
that function to this. | 2,893.59 | 2.32 |
So we talked about this and binding
this towards the end of last lecture. | 2,895.91 | 3.65 |
And we talked about how this is bound
on whatever object it's invoked on. | 2,899.56 | 7.61 |
And so right now, this isn't
invoked on the class, right? | 2,907.17 | 3.76 |
It's invoked when you click that button. | 2,910.93 | 2.55 |
And so we're going to have to bind this
to the value that we want it to be. | 2,913.48 | 4.566 |
And so towards the end of the lecture,
we talked about different ways | 2,918.046 | 2.874 |
to bind this. | 2,920.92 | 0.75 |
Can anybody remember those? | 2,921.67 | 1.23 |
Yeah? | 2,928.02 | 0.5 |
AUDIENCE: Bind method. | 2,928.52 | 1.32 |
JORDAN HAYASHI: Yeah. | 2,929.84 | 0.874 |
There's this thing called bind,
which is a method on all functions. | 2,930.714 | 3.436 |
And similar to bind
there is call and apply. | 2,934.15 | 3.55 |
And we also talked about a
different way of writing functions, | 2,937.7 | 4.47 |
which actually lexically binds this. | 2,942.17 | 3.84 |
Do you guys remember how that was? | 2,946.01 | 3.39 |
Remember, if you use arrow notation,
it automatically binds this for you. | 2,949.4 | 5.1 |
And so you can solve this problem
in all three different ways. | 2,954.5 | 3.27 |
And so down here, if we
wanted to do dot bind, | 2,957.77 | 2.64 |
we could do dot bind to this,
which, since we're invoking it | 2,960.41 | 4.63 |
on the render method, which is a
method that's invoked on the class, | 2,965.04 | 4.58 |
this is bound correctly. | 2,969.62 | 1.68 |
And so when we increase
it, it will increase. | 2,971.3 | 3.67 |
We could also use-- | 2,974.97 | 1.6 |
well, call and apply
don't really work here. | 2,976.57 | 2.2 |
But we could actually
use arrow notation. | 2,978.77 | 3.24 |
So we can say, let's have
an arrow notation down here, | 2,982.01 | 9.73 |
which creates a new, anonymous function. | 2,991.74 | 4.2 |
And what does that
anonymous function do? | 2,995.94 | 1.72 |
Well, all it does is
invoke increase count. | 2,997.66 | 3.22 |
And so since this we wrote
as an ES6 arrow notation, | 3,000.88 | 4.51 |
it automatically binds this
to be what we want it to be. | 3,005.39 | 3.922 |
Does that makes sense? | 3,013.56 | 2.48 |
There are a couple other
places that you could do this. | 3,016.04 | 3 |
We could also do it in
the constructor method. | 3,019.04 | 2.19 |
You could do this dot increase count
equals this dot increase count dot bind | 3,021.23 | 6.72 |
this. | 3,027.95 | 2.04 |
Or lastly, you could also-- | 3,029.99 | 2.51 |
and then down here, you'd just
have to do this dot increase count. | 3,032.5 | 3.22 |
Or alternatively, you could actually
just define this in the constructor. | 3,035.72 | 5.982 |
And so that would be the other way. | 3,051.83 | 1.75 |
So you could say, this dot increase
count equals an arrow notation in this. | 3,053.58 | 6.75 |
But let's stick with this way for now. | 3,060.33 | 8.385 |
Cool. | 3,068.715 | 0.746 |
So now we have a button that works,
that increases as we expected. | 3,069.461 | 2.749 |
So what if instead of increasing it
by 1, we wanted to increase it by 2? | 3,075.13 | 4.32 |
Well, of course, you could just do this. | 3,079.45 | 2.98 |
Let's say we want to do
it by sending state twice. | 3,082.43 | 3.65 |
So what do we expect to happen here? | 3,095.6 | 1.5 |
We expect it to set state to be count
as this dot state dot count plus 1 is 1. | 3,101.43 | 5.22 |
And then [? it does ?] set count
to be 1 plus 1 to be 2, right? | 3,106.65 | 5.34 |
But we see it's only going up by 1. | 3,111.99 | 4.07 |
Why might that be? | 3,116.06 | 3.1 |
Well, set state calls are actually
batched and run asynchronously, | 3,119.16 | 4.772 |
which means React is smart enough to
know that if this dot set state is | 3,123.932 | 2.958 |
getting called a bunch
of times in a row, | 3,126.89 | 1.86 |
rather than immediately doing that,
maybe it would be better to just batch | 3,128.75 | 3.87 |
them and do it all in one go. | 3,132.62 | 2.88 |
And like I said, if you pass an
object to this dot set state, | 3,135.5 | 3.17 |
it will just merge it
into the new state. | 3,138.67 | 2.43 |
And so what is actually
happening when you | 3,141.1 | 2.05 |
call this dot set state twice in a row? | 3,143.15 | 3.6 |
Well, they get batched together. | 3,146.75 | 1.81 |
And so it says, OK, now
we know we need to merge | 3,148.56 | 5.6 |
into the old state these two objects. | 3,154.16 | 4.29 |
And so it's basically saying, merge
the current state-- in this case, | 3,158.45 | 7.47 |
it's count is 0. | 3,165.92 | 6.529 |
And then what are we merging into it? | 3,172.449 | 1.541 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.