text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
oops, we should return this--
3,775.59
3.68
when you toggle that counter we never actually tell this component,
3,779.27
6.18
hey, stop incrementing.
3,785.45
3.372
It's actually going to keep incrementing.
3,788.822
1.708
And so we can show that if we have here, if we console
3,790.53
2.94
that log we see that it's incrementing, and if we toggle it,
3,793.47
10.66
it's still logging.
3,804.13
1.47
So counters now gone.
3,805.6
2.29
So there's no counter here, it's gone but it keeps saying increment.
3,807.89
6.32
And, if we show it again, now look at how fast it's incrementing.
3,814.21
10.15
It's two every second because the first counter created
3,824.36
5.17
an incrementing function every 1,000 milliseconds and the second time
3,829.53
4.2
we created one it also did.
3,833.73
2.94
And so what if we did this again.
3,836.67
1.59
Toggle and then toggle again.
3,838.26
3.42
Now it's going up three every second.
3,841.68
1.9
And if we did this a bunch of times, you see it's going pretty crazy now.
3,843.58
4.67
It's like five every second.
3,848.25
2.072
And so that's not good.
3,850.322
0.958
That's definitely not good at all.
3,851.28
1.99
Say you had a super complex app and hundreds of timers getting set
3,853.27
4.55
and you kept toggling them.
3,857.82
1.53
Eventually you're going to have a big problem where a bunch of things
3,859.35
3.03
are getting called and nothing should be getting called.
3,862.38
2.91
And so that's like a memory leak and it could cause your app to crash.
3,865.29
4.35
And so how might we go about solving this?
3,869.64
3.61
So, as we alluded to, there's this thing called an unmount cycle
3,873.25
3.11
where we have a chance to clean up.
3,876.36
2.01
We can get rid of any timers that we've created.
3,878.37
5.16
And so you can actually do that here.
3,883.53
3
And so, rather than just setting the interval,
3,886.53
2.22
we should actually track that function that we created.
3,888.75
3.06
And so you can do something like this.Interval equals setInterval.
3,891.81
6.18
And so setInterval actually returns like an ID of the interval that you created.
3,897.99
5.63
And so now we're tracking exactly the interval that we created.
3,903.62
4.94
And we can go ahead and right before this component dies,
3,908.56
4.07
so component will unmount, we should actually clean that up.
3,912.63
6.84
We can actually do clearInterval and we pass in that ID.
3,919.47
5.445
And so now you can see--
3,929.36
5.07
oops, things did not get updated.
3,934.43
3.375
Let's actually copy all this and refresh the page.
3,937.805
7.617
And so when this loads, you'll see that it will still
3,952.06
4.5
log when it's getting shown.
3,956.56
3.49
And so as this number is incrementing we can see it saying, oh I'm incrementing.
3,960.05
5.42
But when we toggle it that number no longer goes up.
3,965.47
7.41
And then, if we toggle it again, it's going to continue incrementing.
3,972.88
4.33
And, if we cancel that, that incrementing function
3,977.21
3.74
is no longer getting called.
3,980.95
1.76
And that's because, over here, when the component was created,
3,982.71
5.3
so when the component mounts, we set the interval for this button increment
3,988.01
5.2
to happen every 1,000 seconds.
3,993.21
2.75
But then later when we are no longer returning that counter so
3,995.96
8.72
the instance gets unmounted.
4,004.68
1.5
It's no longer part of the React tree.
4,006.18
1.67
Before it gets unmounted, we say clear that interval and it clears.
4,013.89
7.21
And then when it remounts it creates a new interval.
4,021.1
3.38
And when it gets unmounted again it goes ahead and clears that interval.
4,024.48
4.47
And so now we no longer have any memory leaks.
4,028.95
1.98
So does that whole lifecycle process make sense to people?
4,034.76
3.59
Do you have any questions about these methods
4,038.35
2.94
or why we might want to use them?
4,041.29
5.78
Cool.
4,047.07
0.5
Let's take a quick five minute break and then
4,047.57
2.36
after this, we'll go ahead and start writing some React Native.
4,049.93
2.88
Hello and welcome back.
4,052.81
1.38
So if you are following Snack some people had some questions about style
4,054.19
5.16
and whether or not people in industry chose to bundle
4,059.35
2.79
this style with each component.
4,062.14
2.46
And what I tend to see mostly in industry
4,064.6
2.67
is that people do indeed tend to include the styles with each component
4,067.27
4.5
because part of React's component mindset
4,071.77
2.64
is that each component should be a standalone thing
4,074.41
2.55
and it should include with it any event handlers or JavaScript and any style
4,076.96
5.25
as well.
4,082.21
1.25
But there are many compelling reasons to actually remove
4,083.46
3.25
the style outside of that component.
4,086.71
1.95
And one big compelling reason would be something like color themes.
4,088.66
3.51
So say you had some color theme where you really liked crimson in your app
4,092.17
5.91
and everywhere you were using crimson.
4,098.08
2.37
And then say suddenly you wanted to use something like blue.
4,100.45
5.4
If you had everything hard coded to crimson
4,105.85
2.339
it might be a bit of a pain to go through and find every instance
4,108.189
3.21
of that crimson and change it to blue.
4,111.399
1.991
But say you actually abstracted out some value like primary color
4,113.39
5.299
and instead used that in all of your components.
4,118.689
2.669
Then if you wanted to change the primary color from crimson to blue
4,121.358
2.791
it would be very easy.
4,124.149
0.916
You would just do it in one file and be done.
4,125.065
2.265
So there are definitely compelling reasons to remove the styles out
4,127.33
3.87
of a particular component.
4,131.2
2.61
And since React Native uses JavaScript's objects to style components
4,133.81
4.169
it allows you to do stuff like create variables and import them in.
4,137.979
4.541
And so are there reasons to take the style sheets
4,142.52
2.75
outside of any particular component.
4,145.27
2.549
But generally if you have a choice it's easier
4,147.819
2.101
to just leave it in there for organization sake.
4,149.92
4.05
Cool.
4,153.97
1.36
And so up until now we've been writing a lot of React Native in this browser
4,155.33
6.59
environment.
4,161.92
0.78
And so let's actually figure out and talk about how we actually
4,162.7
3.6