text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
The way we've generally handled this is by actually binding it
2,801.57
7.12
at creation time.
2,808.69
2.54
And so there's a shorthand whereby we can
2,811.23
10.79
say when we create this increment function automatically
2,822.02
4.5
bind it to this class.
2,826.52
2.01
And so this is actually a new addition to the ECMAScript standard
2,828.53
6.33
where this is called class properties whereby
2,834.86
2.34
inline as we're declaring this class we can also
2,837.2
2.97
create properties that should be added during the constructor.
2,840.17
3.7
And so this is syntactically the same as doing this.
2,843.87
6.41
Having the increment function defined as we did before.
2,850.28
9.79
And in the constructor when this is created
2,860.07
2.02
doing this.Increment equals this.Increment.bind whatever
2,862.09
9.4
the this context we want to be.
2,871.49
2.29
And in this case it's this.
2,873.78
3.06
Or in other words, it's the same as just doing
2,876.84
2.16
this.Increment is equal to the anonymous function that we defined down there.
2,879
4.01
Whatever.
2,887.03
2.58
And so that's just, rather than having to write everything in the constructor,
2,889.61
5.56
we can just use this shorthand down here which
2,895.17
7.68
is just generally the preferred way because it's easier to read.
2,902.85
6.164
And now we've gone ahead and created this timer that runs.
2,909.014
2.416
And so you can see that the numbers are going up.
2,911.43
3.277
And the reason that this is happening is because we created this increment
2,914.707
3.083
function.
2,917.79
0.75
We correctly bound it to the this that we wanted to bind it to
2,918.54
3.177
and then we said, hey, component when you're
2,921.717
1.833
done mounting set up this timer, set up on an interval of 1,000 milliseconds.
2,923.55
5.32
So every second call this increment function.
2,928.87
2.522
And what does that increment function do?
2,931.392
1.708
Well it updates the state to be the previous states count plus 1
2,933.1
3.5
and then down in the render we render this.state.count.
2,936.6
3.3
And so you see every second the state gets updated to a new number,
2,939.9
4.17
and the new number is shown there.
2,944.07
2.46
And so we never had to manually say hey, run this code when you mount.
2,946.53
9.15
We just created this method called component.DidMount and React handles
2,955.68
5.28
automatically invoking that for you after the component mounted.
2,960.96
2.82
And it's the same as the constructor.
2,967.59
2.08
We never had to manually invoke the constructor.
2,969.67
2.08
It just gets called automatically when a class instance is getting created?
2,971.75
3.65
Any questions on the mount cycle or the mount processor I should say?
2,978.22
5.25
Great so now let's talk about the update cycle.
2,991.01
4.23
And so, just like in the mounting process,
2,995.24
2.17
there is a bunch of lifecycle hooks that got called for you automatically.
2,997.41
3.35
There are also a bunch of lifecycle hooks
3,000.76
1.708
that get called automatically every single time we want to re-render.
3,002.468
4.762
And so the first thing that happens is component
3,007.23
2.38
will receive props which takes the next props.
3,009.61
3.54
And so say you had something in your state
3,013.15
2.1
that really depended on what the props were set to,
3,015.25
3.744
you can actually use this function to update any of those state fields
3,018.994
2.916
that rely on the props.
3,021.91
1.51
And you do that by calling this .setstate.
3,023.42
2.9
Next is this thing called shouldComponentUpdate,
3,026.32
3.15
which takes the next props and the next date,
3,029.47
2.94
and here you can compare change values and decide whether or not
3,032.41
3.54
you want that component to render.
3,035.95
1.47
And you can actually stop the update cycle here.
3,037.42
4.14
And so this is a good optimization.
3,044.26
2.1
So say you have a very complicated component
3,046.36
2.14
that takes a really long time to render you don't necessarily
3,048.5
3.54
want it to render every single time you get a new prop
3,052.04
3.18
because it might be that the new prop doesn't actually
3,055.22
2.25
change anything that's shown.
3,057.47
2.04
And so you could use this method to stop it early.
3,059.51
4.02
But that adds a lot of complexity to your app
3,063.53
1.89
and there's almost always a premature optimization.
3,065.42
3.45
The next happens render, we know exactly what happens there.
3,068.87
4.02
And last we have this thing called ComponentDidUpdate,
3,072.89
3.06
whereby you can do anything that isn't needed for the UI,
3,075.95
3.06
like network requests, which is basically
3,079.01
2.25
the analog for ComponentDidMount.
3,081.26
2.69
And so let's see an example for this update.
3,083.95
3.17
So say rather than just rendering this text we actually
3,087.12
8.78
pass this count to another function.
3,095.9
3.16
So let's have this thing called a count, which takes as a propped account.
3,099.06
10.29
And then let's create this class called count.
3,114.47
3.05
And then here, let's first just render the text.
3,122.61
3.315
And so now we're back to where we started, where we basically
3,132.16
10.8
have some text that gets rendered based on this.PropsAccount,
3,142.96
4.73
and let's style it to be larger.
3,147.69
2.87
And so now we're basically back to exactly where we started.
3,158.201
2.499
We have this app which automatically increments
3,160.7
4.7
and then it passes whatever its state count is as the count prop
3,165.4
3.51
to this other class that we call count.
3,168.91
4.22
And then this component we basically just take
3,173.13
2.35
that prop and render that text.
3,175.48
4.11
But say we actually only wanted to update on odd numbers.
3,179.59
5.94
So say we want to create a new--
3,185.53
4.1
or say we want to have this be called CountEvennumbers.
3,189.63
5.39
And so, in this example here, how might we say,
3,203.29
4.79
hey don't actually update unless your number is even?
3,208.08
5.97
Right now every single time it receives a new prop it's updating.
3,214.05
5.02
But say we only wanted to count even numbers, what's some strategy that we
3,219.07
4.07
may use in order to skip the rendering for odd numbers
3,223.14
3.33
and only render on even numbers?
3,226.47
1.59
AUDIENCE: You could use [INAUDIBLE] before an update.
3,234.78
3.85
SPEAKER 1: Yeah, so we have this thing called shouldComponentUpdate,
3,238.63
3.38
which takes the next props.
3,242.01
1.17
And so we have the ability to look at the next props
3,243.18
2.19