text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
and decide whether or not we want to update,
3,245.37
2.77
and if we return false from this function, the update cycle is over.
3,248.14
3.74
It won't render.
3,251.88
2.2
And so if in this function we wanted to check if that count were odd
3,254.08
6.26
we can abort early.
3,260.34
2.05
And so let's actually do that.
3,262.39
1.61
So let's have this method called shouldComponentUpdate
3,264
5.784
and first let's just return false.
3,269.784
1.416
Now what's going to happen here?
3,273.857
1.333
Nothing.
3,277.91
0.88
We can see that it's receiving stuff.
3,278.79
1.74
So see down here in the logs we're receiving this thing called
3,288.46
4.94
updating every second.
3,293.4
2.83
But the UI is just stuck at 0, it never updates
3,296.23
2.592
and that's because we're saying hey, should the component update?
3,298.822
2.708
No we shouldn't.
3,301.53
1.59
And so React is saying, OK, then we're not
3,303.12
1.98
going to call this render method and nothing changes over here.
3,305.1
3.92
And so how may we change this is such that it only counts the even numbers?
3,309.02
5.65
Well we can say return, check to see if the next Props.count is divisible by 2.
3,319.74
16.17
And, if it returns true, that means we're an odd number.
3,335.91
11.81
But what we want is whether we're an even number.
3,347.72
2.89
And so we can actually just say, oh, return the inverse of this.
3,350.61
3.104
And now it's only going to count those even numbers.
3,353.714
2.166
Does that make sense?
3,361.456
0.874
And so say we wanted to do something like send a request
3,366.4
4.075
or server every single time this UI updated,
3,370.475
5.675
we wouldn't really want to do that in the render cycle,
3,376.15
2.31
we wouldn't really want to do it in shouldComponentUpdate.
3,378.46
4.6
We can actually do that in ComponentDidUpdate.
3,383.06
2.198
And so we can actually do for now let's just console.log.
3,388.32
3.18
We can say, hey, we updated it and we created a new number.
3,397.13
4.63
And as you see, it's only getting called for those even numbers.
3,401.76
4.4
Because first shouldComponentUpdate gets called and then
3,406.16
3.81
it renders and then ComponentDidUpdate gets called.
3,409.97
3.05
And, since we exit early on all odd numbers,
3,413.02
3.1
only the even numbers will reach the render cycle and ComponentDidUpdate.
3,416.12
4.29
And so what you see logged are only those even numbers.
3,420.41
2.49
So does this concept of updating and methods that automatically
3,430.21
5.61
get invoked makes sense to everyone?
3,435.82
3.324
AUDIENCE: Could you only do this kind of stuff
3,439.144
2.39
with [INAUDIBLE] and not the purely functional ones?
3,441.534
2.56
SPEAKER 1: Yeah.
3,444.094
0.666
So the question was, do these methods exist on the functional ones?
3,444.76
3.93
And they don't because, say this functional components
3,448.69
4.2
are literally just functions.
3,452.89
2.37
They take props and they return a node.
3,455.26
2.07
And so since they're functions, all they do are get invoked.
3,457.33
3.24
You pass them props and then you get a node back.
3,460.57
2.452
And in order to have these methods you actually
3,463.022
1.958
have to have this concept of an instance.
3,464.98
5.67
And so React components have instances, and these instances
3,470.65
3.15
are tracked throughout their lifecycle.
3,473.8
1.92
And since these are class instances React well
3,475.72
4.59
go ahead and invoke those methods for you.
3,480.31
3.54
And so the stateless functional components
3,483.85
2.254
are just functions that get invoked and do not
3,486.104
1.916
have these class methods, whereas the React components not only have
3,488.02
3.96
these methods but the methods also get invoked automatically for you.
3,491.98
3.14
But yeah great question.
3,495.12
1
So that's the update cycle.
3,500.36
2.43
And then lastly we have what's called the unmount cycle.
3,502.79
3.566
There's only one thing that happens in the unmount cycle.
3,506.356
2.374
You have componentWillUnmount which gives you a chance to clean up.
3,508.73
4.42
What do I mean by clean up?
3,513.15
1.13
Well, you can remove any event listeners if you had them in React, mostly Web.
3,514.28
5.91
You can invalidate any network requests that you have out.
3,520.19
2.67
Or lastly you should clear any timeouts or intervals.
3,522.86
3
And so there's actually some bugs that you can create if you're not careful.
3,525.86
6.1
So let's actually revert this back to being
3,531.96
8.86
just an app that shows that count.
3,540.82
6.43
So now we have again that number being incremented.
3,555.81
3.61
And this class called app keeps track of its own number and is updating.
3,567.75
6.52
But say, let's actually call this counter
3,574.27
5.03
and create this thing called app.
3,579.3
2.1
And in this, let's actually just show the component.
3,590.59
2.752
So now we have basically the same exact thing.
3,615.49
2.03
So we have this thing called class app which
3,617.52
2.87
just returns an instance of counter.
3,620.39
2.54
And counter is what we've been working on thus far.
3,622.93
2.44
It creates an interval and we'll keep track of that interval
3,625.37
2.64
and keep counting up.
3,628.01
2.21
But say we actually have this button that will toggle whether or not
3,630.22
3.79
this counter is shown.
3,634.01
1.14
And so let's have this thing called state
3,635.15
13.81
and let's have this flag called show counter
3,648.96
3.037
and let's initialize it to true.
3,651.997
1.333
And then down here we can do something like,
3,657.27
4.8
if you should show the counter then return this.
3,662.07
9.25
Else let's just return an empty view.
3,676.96
2.356
And so now this is basically the exact same thing
3,687.75
2.76
because we have no way to change whether or not we want to show the counter.
3,690.51
4.65
But say we did, so let's create toggleCounter.
3,695.16
2.61
So show counter.
3,708.04
2.17
It should be the opposite of what the previous state show counter was.
3,710.21
3.07
And let's also have a button that will toggle this counter.
3,716.14
17.34
And so now we have a button where if we click it,
3,743.37
3.582
we can just toggle whether or not that counter shows.
3,746.952
2.208
And there's actually a pretty bad bug here.
3,761.53
2.726
And that is that when we toggled this counter--
3,767.61
3.512