text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
and we'll see later today how we get that going.
81.91
4.62
And so how exactly does this work?
86.53
1.99
How are we allowed to write JavaScript and have it run on mobile devices?
88.52
4.85
So first, the JavaScript is bundled.
93.37
1.5
So, just like in React, what happens are a couple of different steps
94.87
4.17
where your JavaScript is transpilaged so going from ES6, ES7, ES-next,
99.04
5.61
down to ES5 code, and it's also minified.
104.65
3.65
And so throughout the process you go from a bunch of different files
108.3
2.86
all to one big JavaScript bundle.
111.16
3.81
This runs on your phone.
114.97
1.327
There are separate threads for UI layout and JavaScript.
116.297
2.333
And so, as we saw, if we're running JavaScript in the browser
118.63
2.73
and that locks up, then nothing works.
121.36
2.899
But in React Native there are actually separate threads
124.259
2.291
for the UI layout and JavaScript.
126.55
1.95
And these different threads communicate asynchronously through a bridge.
131.95
5.44
And so the JavaScript thread will request
137.39
2.18
UI elements to be shown and then, like I alluded to earlier,
139.57
4.45
the JavaScript thread can be blocked and the UI still works.
144.02
2.92
And so what do I mean by this bridge?
146.94
3.86
And so what ends up happening is there are separate threads.
150.8
3.14
So there's one thread for the UI and there's one thread for JavaScript.
153.94
5.275
And so say we're writing React and we have something like a button
162.54
7.43
that we want to be shown.
169.97
1.97
Basically what happens is there's this bridge which will
171.94
7.56
communicate between these two threads.
179.5
1.59
And so the JavaScript will basically say,
181.09
2.11
hey UI thread I want a view or a button.
183.2
4.49
And then UI will basically say, OK here it is a button.
194.4
4.197
And then if I, as a user, go ahead and press
198.597
1.833
that the UI will actually send over to the JavaScript,
200.43
4.427
oh the button was pressed.
204.857
1.083
And so this bridge here is asynchronous, so
214.32
2.64
if something happens on the JavaScript and the JavaScript is actually
216.96
2.94
clogged and blocking, the UI thread can still do its thing
219.9
3.18
and I'll show you an example of that in a second.
223.08
2.62
And, conversely, when you touch something on the UI
225.7
4.25
the JavaScript doesn't know until the bridge basically says,
229.95
3.73
hey, this button was pressed and the UI can talk to the JavaScript that way.
233.68
4.46
And so let's see some example where we can lock up that JavaScript thread
238.14
4.83
and not have the UI lock up.
242.97
3.24
And so here we are in the Snack, snack.expo.io,
246.21
5.49
and this allows us to run React Native and have it all happen in the browser.
251.7
4.92
And so a few lectures ago we wrote this blocking method that basically locks up
256.62
6.404
the JavaScript thread.
263.024
0.916
So we can go ahead and do that again.
263.94
1.61
So say we have a method called block JavaScript.
265.55
4.33
And what that does is you can say, we're going to block and then do this thing,
269.88
9.4
so while date.now so we can say, cons done is five seconds from now.
279.28
9.68
And we can say, while it has not been that much time just do nothing.
293.9
5.31
And then when that's done we can say, OK we're done.
302.904
2.166
And so down here, let's actually create a scroll view--
310.78
3.07
we'll see what that is in a second--
313.85
1.5
and a button.
320.239
0.541
And we actually don't care about any of all that stuff.
328.999
2.291
And so, when this button is clicked, we're
341.691
1.749
going to go ahead and block that JavaScript.
343.44
1.9
All right.
356.61
0.98
So everybody following?
361.52
1
So, so far we have a scroll view which is basically just a view that
362.52
4.83
can scroll back and forth.
367.35
2.08
We have a button that says block JavaScript, and when it's clicked,
369.43
3.95
it will run this function that blocks the JavaScript.
373.38
3.375
And so watch what happens when I click this button.
376.755
3.255
Small bug.
391.24
0.99
All right here's a chance.
398.437
1.083
Who can spot the bug?
399.52
0.874
Oh, on press.
407
2.71
So on press, it will actually block the JavaScript.
409.71
2.46
And you see how it still is scrolling, but that button is locked up.
412.17
4.26
And then only after five seconds have passed do the logs come through
416.43
4.68
and then the button returns to its normal state.
421.11
3.15
But notice how when it was stuck and locked,
424.26
2.302
when the JavaScript thread was locked, we still
426.562
1.958
had the ability to scroll back and forth.
428.52
2.85
That's because all of the JavaScript is controlled on one thread
431.37
3.12
whereas on the other thread all of the UI elements still worked as intended.
434.49
4.9
And so even though the JavaScript thread was locked up here,
439.39
2.51
it was in this while loop doing nothing, the UI elements
441.9
4.05
were still able to scroll back and forth.
445.95
2.86
And so it's not as big of a deal if you lock up
448.81
3.59
the JavaScript thread in React Native but it's still a big deal
452.4
3.45
because none of your event handlers will fire.
455.85
3.87
So any questions about how React Native works, how these UI and JavaScript
462.75
5.45
threads run separately in the bridge?
468.2
3
Great.
471.2
1.86
And so what are some of the differences between React Native and React Web
473.06
3.15
which we've been talking about thus far?
476.21
2.157
So there are differences in base components.
478.367
1.833
So as you saw in my quick example, things like scroll view or capital
480.2
3.63
button do not exist in web.
483.83
2.205
Things like style, the way that you style elements,
486.035
2.125
is slightly different in React Native and you don't actually
488.16
3.23
have browser APIs anymore.
491.39
2.79
So things like CSS, animations, canvas, SVG, things like that
494.18
4.17
don't actually exist in React Native.
498.35
2.467
But there are actually things that have been polyfilled.
500.817
2.333
So polyfilled is a term that people use to mean some methods or functions that
503.15
6.9
might exist in one environment do not necessarily exist in all environments.
510.05
3.97
And by polyfilling these we can actually implement them such
514.02
3.92