text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
Well, we want to merge this dot state dot count plus 1.
3,173.99
4.11
And then we also want to merge that same thing again.
3,181.95
2.41
And so what happens when you merge these three things together?
3,189.195
2.625
Well, these two get merged together, and it actually turns into this.
3,194.44
3.32
Well, you go left to right, so count gets merged,
3,203
5.201
gets replaced by this dot state dot count,
3,208.201
1.749
which then gets replaced by this dot state dot count plus 1.
3,209.95
4.75
And so that is actually just 0 plus 1, which is why we end up getting a 1
3,214.7
6.46
here.
3,221.16
0.665
Does that make sense?
3,221.825
0.875
And so we can prove that it runs asynchronously by logging.
3,226.78
2.76
So what do we expect this dot state dot count to be at line 21?
3,235.14
5.618
AUDIENCE: [INAUDIBLE]
3,240.758
4.872
JORDAN HAYASHI: It actually logs 0, because when you call this dot set
3,245.63
5.57
state, all it does is add it to that queue
3,251.2
2.93
that we know it's eventually going to execute.
3,254.13
1.96
Same with this dot set state.
3,256.09
1.208
It gets added to a queue as well.
3,257.298
2.342
And so when we get to line 21 here, what has actually executed?
3,259.64
3.53
Well, nothing.
3,263.17
1.41
And so this dot state dot count still has a value of 0,
3,264.58
2.97
and then only after that do those batch set states run.
3,267.55
4.5
So what if we actually did care about the previous count before?
3,272.05
6.63
Say we wanted to add 1 to the previous count,
3,278.68
2.52
but we actually really wanted that state to exist beforehand.
3,281.2
5.6
Well, we can pass it what's called an updater function,
3,286.8
2.3
and that's a function that takes the previous state
3,289.1
4.74
and returns some new state.
3,293.84
7.12
In this case, we want it to be the previous state, dot count plus 1.
3,300.96
5.15
So now we have two set states, where rather
3,317.11
2.384
than just passing an object to emerge, we're saying,
3,319.494
2.166
actually run this function.
3,321.66
1.87
This function takes the old state and returns
3,323.53
2.42
a new object, which is where the count is the previous state's count plus 1.
3,325.95
6.006
And so now when it batches it, it says, oh, we actually
3,331.956
2.754
need to run this function twice.
3,334.71
1.67
And so now when we click, it actually goes up by two.
3,336.38
5.82
So any questions on React, props, or state thus far?
3,349.06
4.542
Cool.
3,358.01
0.5
Let's go ahead and take a quick break.
3,358.51
1.38
And then when we come back, we can play with React a little bit more.
3,359.89
2.875
Hello, and welcome back.
3,365.32
1.1
So before the break, we were talking about React and props and state.
3,366.42
3.88
And now with all three, we can actually go ahead and start
3,370.3
2.76
building pretty powerful apps.
3,373.06
3.66
And so for homework, for the project zero,
3,376.72
2.43
you guys have been working on a to do app, which you've
3,379.15
2.455
been writing in all vanilla JavaScript.
3,381.605
1.625
And today for the rest of the class, we're
3,383.23
2.01
going to go ahead and together implement that in all React.
3,385.24
2.87
So what are some strategies you may go around doing
3,395.44
3.12
your thing in vanilla JavaScript?
3,398.56
3.03
Well, say we had to dos created as list items, where within each list item,
3,401.59
6.12
we have input, which has a checkbox.
3,407.71
2.339
Maybe you're doing the challenge and you want to take care of the deletes
3,410.049
3.041
as well.
3,413.09
0.795
And then, of course, you're going to have some span
3,413.885
2.125
or some way of displaying text.
3,416.01
3
And so maybe have a couple different functions.
3,419.01
2.39
One is to create a to do.
3,421.4
1.89
One is to delete a to do.
3,423.29
1.339
And what might you do in those functions in order to create those to dos?
3,424.629
3.041
Well, first, maybe you'll get the text.
3,430.4
3.761
Then what?
3,434.161
0.499
Maybe go ahead and using document dot create element,
3,434.66
3.08
maybe you want to create a list item.
3,437.74
2.9
Then what?
3,440.64
0.5
You'll probably have to create the input.
3,441.14
2.145
Then probably create a button.
3,446.05
2.67
Create the span.
3,448.72
2.25
And maybe at the end, you hook those all up together
3,450.97
2.58
and append those to the list.
3,453.55
3.65
And so how might you take care of delete?
3,460.21
2.11
Well, find the to do.
3,462.32
2.34
Maybe delete that.
3,468.17
3.071
Then what?
3,471.241
0.499
Make sure to update the counts.
3,471.74
1.31
And maybe we have to do that over here as well.
3,475.342
1.958
So you see how we're doing this in a very imperative manner.
3,482
4.632
Using JavaScript, we tell the browser exactly what we want to do.
3,486.632
2.708
Well, we know that our to dos are shaped like this.
3,489.34
2.32
So first go ahead and get the text.
3,491.66
1.48
Maybe create that [INAUDIBLE],, create the checkbox,
3,493.14
2.3
create all these other things.
3,495.44
1.92
Append them to each other, and maybe append them to the list.
3,497.36
3.07
And so we go ahead, and we created a new to do, but we had to do a lot of steps
3,500.43
4.22
in order to get there.
3,504.65
1.55
And so what might be an easier way to do this?
3,506.2
4.67
Well, in to do one, maybe we wanted to abstract out the creation of the
3,510.87
6.7
to do itself.
3,517.57
1.12
So maybe in that new create to do function,
3,518.69
2.267
we go ahead and make the list item, make the input, make the button,
3,520.957
2.833
make the span.
3,523.79
0.87
Hook those all up together.
3,524.66
1.639
But that's one, little, discrete part of the UI, right?
3,526.299
2.291
We're going to start to componentize these things.
3,528.59
2.34
Maybe abstract out a function for creating the to do, and then in the new
3,530.93
3.48
to do, we can still get the text, update the counts, and append to the list.
3,534.41
4.09
But rather than doing all of this work in the new to do function,
3,538.5
3.65
maybe we just invoke create to do.
3,542.15
2.04