[ { "text": "[MUSIC PLAYING]", "start": 0.0, "duration": 2.495 }, { "text": "SPEAKER 1: Hello and welcome\nfor Lecture 3 in React Native.", "start": 16.48, "duration": 3.844 }, { "text": "So previous lecture we talked\nabout a bunch of different topics;", "start": 20.324, "duration": 2.666 }, { "text": "one being classes and how ES6\nallows you to write classes.", "start": 22.99, "duration": 4.23 }, { "text": "React, we talked about\nthe library by Facebook", "start": 27.22, "duration": 2.73 }, { "text": "that allows you to write\ndeclarative programming.", "start": 29.95, "duration": 2.309 }, { "text": "We talked about what imperative\nand declarative programming were", "start": 32.259, "duration": 3.721 }, { "text": "and how React allows you\nto write declaratively.", "start": 35.98, "duration": 2.4 }, { "text": "We talked about props, which\nare basically an object that", "start": 38.38, "duration": 2.55 }, { "text": "are passed down to elements.", "start": 40.93, "duration": 1.71 }, { "text": "We talked about state, which is a\nmanagement system for allowing you", "start": 42.64, "duration": 6.09 }, { "text": "to track state in a class component.", "start": 48.73, "duration": 3.06 }, { "text": "We implemented the to do app in\nReact and then we teased at something", "start": 51.79, "duration": 3.57 }, { "text": "called React Native.", "start": 55.36, "duration": 1.71 }, { "text": "And so this lecture we'll be\ntalking a lot about React Native.", "start": 57.07, "duration": 2.73 }, { "text": "React Native is a framework\nthat relies on React Core.", "start": 59.8, "duration": 3.0 }, { "text": "And so a lot of the paradigms that apply\nto React also apply to React Native.", "start": 62.8, "duration": 4.79 }, { "text": "And it allows us to build mobile\napps using only JavaScript.", "start": 67.59, "duration": 3.76 }, { "text": "And so, as the React team like\nto say when this was released,", "start": 71.35, "duration": 4.14 }, { "text": "you can learn once and write everywhere.", "start": 75.49, "duration": 4.11 }, { "text": "And so React Native\nsupports iOS and Android,", "start": 79.6, "duration": 2.31 }, { "text": "and we'll see later today\nhow we get that going.", "start": 81.91, "duration": 4.62 }, { "text": "And so how exactly does this work?", "start": 86.53, "duration": 1.99 }, { "text": "How are we allowed to write JavaScript\nand have it run on mobile devices?", "start": 88.52, "duration": 4.85 }, { "text": "So first, the JavaScript is bundled.", "start": 93.37, "duration": 1.5 }, { "text": "So, just like in React, what happens\nare a couple of different steps", "start": 94.87, "duration": 4.17 }, { "text": "where your JavaScript is transpilaged\nso going from ES6, ES7, ES-next,", "start": 99.04, "duration": 5.61 }, { "text": "down to ES5 code, and\nit's also minified.", "start": 104.65, "duration": 3.65 }, { "text": "And so throughout the process you\ngo from a bunch of different files", "start": 108.3, "duration": 2.86 }, { "text": "all to one big JavaScript bundle.", "start": 111.16, "duration": 3.81 }, { "text": "This runs on your phone.", "start": 114.97, "duration": 1.327 }, { "text": "There are separate threads\nfor UI layout and JavaScript.", "start": 116.297, "duration": 2.333 }, { "text": "And so, as we saw, if we're\nrunning JavaScript in the browser", "start": 118.63, "duration": 2.73 }, { "text": "and that locks up, then nothing works.", "start": 121.36, "duration": 2.899 }, { "text": "But in React Native there\nare actually separate threads", "start": 124.259, "duration": 2.291 }, { "text": "for the UI layout and JavaScript.", "start": 126.55, "duration": 1.95 }, { "text": "And these different threads communicate\nasynchronously through a bridge.", "start": 131.95, "duration": 5.44 }, { "text": "And so the JavaScript\nthread will request", "start": 137.39, "duration": 2.18 }, { "text": "UI elements to be shown and\nthen, like I alluded to earlier,", "start": 139.57, "duration": 4.45 }, { "text": "the JavaScript thread can be\nblocked and the UI still works.", "start": 144.02, "duration": 2.92 }, { "text": "And so what do I mean by this bridge?", "start": 146.94, "duration": 3.86 }, { "text": "And so what ends up happening\nis there are separate threads.", "start": 150.8, "duration": 3.14 }, { "text": "So there's one thread for the UI and\nthere's one thread for JavaScript.", "start": 153.94, "duration": 5.275 }, { "text": "And so say we're writing React and\nwe have something like a button", "start": 162.54, "duration": 7.43 }, { "text": "that we want to be shown.", "start": 169.97, "duration": 1.97 }, { "text": "Basically what happens is\nthere's this bridge which will", "start": 171.94, "duration": 7.56 }, { "text": "communicate between these two threads.", "start": 179.5, "duration": 1.59 }, { "text": "And so the JavaScript\nwill basically say,", "start": 181.09, "duration": 2.11 }, { "text": "hey UI thread I want a view or a button.", "start": 183.2, "duration": 4.49 }, { "text": "And then UI will basically\nsay, OK here it is a button.", "start": 194.4, "duration": 4.197 }, { "text": "And then if I, as a\nuser, go ahead and press", "start": 198.597, "duration": 1.833 }, { "text": "that the UI will actually\nsend over to the JavaScript,", "start": 200.43, "duration": 4.427 }, { "text": "oh the button was pressed.", "start": 204.857, "duration": 1.083 }, { "text": "And so this bridge here\nis asynchronous, so", "start": 214.32, "duration": 2.64 }, { "text": "if something happens on the JavaScript\nand the JavaScript is actually", "start": 216.96, "duration": 2.94 }, { "text": "clogged and blocking, the UI\nthread can still do its thing", "start": 219.9, "duration": 3.18 }, { "text": "and I'll show you an\nexample of that in a second.", "start": 223.08, "duration": 2.62 }, { "text": "And, conversely, when you\ntouch something on the UI", "start": 225.7, "duration": 4.25 }, { "text": "the JavaScript doesn't know\nuntil the bridge basically says,", "start": 229.95, "duration": 3.73 }, { "text": "hey, this button was pressed and the\nUI can talk to the JavaScript that way.", "start": 233.68, "duration": 4.46 }, { "text": "And so let's see some example where\nwe can lock up that JavaScript thread", "start": 238.14, "duration": 4.83 }, { "text": "and not have the UI lock up.", "start": 242.97, "duration": 3.24 }, { "text": "And so here we are in\nthe Snack, snack.expo.io,", "start": 246.21, "duration": 5.49 }, { "text": "and this allows us to run React Native\nand have it all happen in the browser.", "start": 251.7, "duration": 4.92 }, { "text": "And so a few lectures ago we wrote this\nblocking method that basically locks up", "start": 256.62, "duration": 6.404 }, { "text": "the JavaScript thread.", "start": 263.024, "duration": 0.916 }, { "text": "So we can go ahead and do that again.", "start": 263.94, "duration": 1.61 }, { "text": "So say we have a method\ncalled block JavaScript.", "start": 265.55, "duration": 4.33 }, { "text": "And what that does is you can say, we're\ngoing to block and then do this thing,", "start": 269.88, "duration": 9.4 }, { "text": "so while date.now so we can say,\ncons done is five seconds from now.", "start": 279.28, "duration": 9.68 }, { "text": "And we can say, while it has not\nbeen that much time just do nothing.", "start": 293.9, "duration": 5.31 }, { "text": "And then when that's done\nwe can say, OK we're done.", "start": 302.904, "duration": 2.166 }, { "text": "And so down here, let's\nactually create a scroll view--", "start": 310.78, "duration": 3.07 }, { "text": "we'll see what that is in a second--", "start": 313.85, "duration": 1.5 }, { "text": "and a button.", "start": 320.239, "duration": 0.541 }, { "text": "And we actually don't care\nabout any of all that stuff.", "start": 328.999, "duration": 2.291 }, { "text": "And so, when this\nbutton is clicked, we're", "start": 341.691, "duration": 1.749 }, { "text": "going to go ahead and\nblock that JavaScript.", "start": 343.44, "duration": 1.9 }, { "text": "All right.", "start": 356.61, "duration": 0.98 }, { "text": "So everybody following?", "start": 361.52, "duration": 1.0 }, { "text": "So, so far we have a scroll view\nwhich is basically just a view that", "start": 362.52, "duration": 4.83 }, { "text": "can scroll back and forth.", "start": 367.35, "duration": 2.08 }, { "text": "We have a button that says block\nJavaScript, and when it's clicked,", "start": 369.43, "duration": 3.95 }, { "text": "it will run this function\nthat blocks the JavaScript.", "start": 373.38, "duration": 3.375 }, { "text": "And so watch what happens\nwhen I click this button.", "start": 376.755, "duration": 3.255 }, { "text": "Small bug.", "start": 391.24, "duration": 0.99 }, { "text": "All right here's a chance.", "start": 398.437, "duration": 1.083 }, { "text": "Who can spot the bug?", "start": 399.52, "duration": 0.874 }, { "text": "Oh, on press.", "start": 407.0, "duration": 2.71 }, { "text": "So on press, it will actually\nblock the JavaScript.", "start": 409.71, "duration": 2.46 }, { "text": "And you see how it still is scrolling,\nbut that button is locked up.", "start": 412.17, "duration": 4.26 }, { "text": "And then only after five seconds\nhave passed do the logs come through", "start": 416.43, "duration": 4.68 }, { "text": "and then the button returns\nto its normal state.", "start": 421.11, "duration": 3.15 }, { "text": "But notice how when it\nwas stuck and locked,", "start": 424.26, "duration": 2.302 }, { "text": "when the JavaScript thread\nwas locked, we still", "start": 426.562, "duration": 1.958 }, { "text": "had the ability to\nscroll back and forth.", "start": 428.52, "duration": 2.85 }, { "text": "That's because all of the JavaScript\nis controlled on one thread", "start": 431.37, "duration": 3.12 }, { "text": "whereas on the other thread all of the\nUI elements still worked as intended.", "start": 434.49, "duration": 4.9 }, { "text": "And so even though the JavaScript\nthread was locked up here,", "start": 439.39, "duration": 2.51 }, { "text": "it was in this while loop\ndoing nothing, the UI elements", "start": 441.9, "duration": 4.05 }, { "text": "were still able to\nscroll back and forth.", "start": 445.95, "duration": 2.86 }, { "text": "And so it's not as big\nof a deal if you lock up", "start": 448.81, "duration": 3.59 }, { "text": "the JavaScript thread in React\nNative but it's still a big deal", "start": 452.4, "duration": 3.45 }, { "text": "because none of your\nevent handlers will fire.", "start": 455.85, "duration": 3.87 }, { "text": "So any questions about how React Native\nworks, how these UI and JavaScript", "start": 462.75, "duration": 5.45 }, { "text": "threads run separately in the bridge?", "start": 468.2, "duration": 3.0 }, { "text": "Great.", "start": 471.2, "duration": 1.86 }, { "text": "And so what are some of the differences\nbetween React Native and React Web", "start": 473.06, "duration": 3.15 }, { "text": "which we've been talking about thus far?", "start": 476.21, "duration": 2.157 }, { "text": "So there are differences\nin base components.", "start": 478.367, "duration": 1.833 }, { "text": "So as you saw in my quick example,\nthings like scroll view or capital", "start": 480.2, "duration": 3.63 }, { "text": "button do not exist in web.", "start": 483.83, "duration": 2.205 }, { "text": "Things like style, the way\nthat you style elements,", "start": 486.035, "duration": 2.125 }, { "text": "is slightly different in React\nNative and you don't actually", "start": 488.16, "duration": 3.23 }, { "text": "have browser APIs anymore.", "start": 491.39, "duration": 2.79 }, { "text": "So things like CSS, animations,\ncanvas, SVG, things like that", "start": 494.18, "duration": 4.17 }, { "text": "don't actually exist in React Native.", "start": 498.35, "duration": 2.467 }, { "text": "But there are actually things\nthat have been polyfilled.", "start": 500.817, "duration": 2.333 }, { "text": "So polyfilled is a term that people use\nto mean some methods or functions that", "start": 503.15, "duration": 6.9 }, { "text": "might exist in one environment do not\nnecessarily exist in all environments.", "start": 510.05, "duration": 3.97 }, { "text": "And by polyfilling these we can\nactually implement them such", "start": 514.02, "duration": 3.92 }, { "text": "that all environments\nwill actually have these.", "start": 517.94, "duration": 2.03 }, { "text": "And so fetch is something that's\nnot supported by all browsers,", "start": 519.97, "duration": 4.199 }, { "text": "but by polyfilling it\nyou can actually include", "start": 524.169, "duration": 2.891 }, { "text": "code that will implement fetch\nif it doesn't exist already.", "start": 527.06, "duration": 2.88 }, { "text": "And so things like fetch timers like\nset interval or console or console.log,", "start": 529.94, "duration": 6.24 }, { "text": "console.warn, and stuff like\nthat have been polyfilled so", "start": 536.18, "duration": 3.995 }, { "text": "that they work in React Native as well.", "start": 540.175, "duration": 4.144 }, { "text": "And also the way that\nyou handle navigation", "start": 544.319, "duration": 1.791 }, { "text": "is slightly different in\nReact Native, and we'll", "start": 546.11, "duration": 1.999 }, { "text": "talk about that in a future lecture.", "start": 548.109, "duration": 1.993 }, { "text": "And so I said that React Native base\ncomponents are slightly different.", "start": 550.102, "duration": 2.958 }, { "text": "And so what do I mean there?", "start": 553.06, "duration": 2.81 }, { "text": "So in React Web we had access to\nthings like div or span or P image,", "start": 555.87, "duration": 4.82 }, { "text": "and we can just declare those globally.", "start": 560.69, "duration": 2.84 }, { "text": "But in React Native we actually have to\nimport from the React Native library,", "start": 563.53, "duration": 4.6 }, { "text": "and we'll see how to do\nthat in a little bit.", "start": 568.13, "duration": 2.64 }, { "text": "And so divs no longer exist and\nwhat we use instead are Views.", "start": 570.77, "duration": 2.84 }, { "text": "So View with a capital V is basically\na cross-platform, just blank E-Y slate.", "start": 573.61, "duration": 7.58 }, { "text": "So basically the same thing as a div.", "start": 581.19, "duration": 3.17 }, { "text": "There's no such thing\nas span or P anymore", "start": 584.36, "duration": 2.31 }, { "text": "and so instead we use this text.", "start": 586.67, "duration": 1.887 }, { "text": "And so what's unique in React\nNative is that all text actually", "start": 588.557, "duration": 2.583 }, { "text": "must be wrapped by this text tag.", "start": 591.14, "duration": 3.69 }, { "text": "As you saw in this previous\nexample lower case button", "start": 594.83, "duration": 2.52 }, { "text": "doesn't exist anymore.", "start": 597.35, "duration": 1.45 }, { "text": "Instead, we use capitol Button\nwith a slightly different API.", "start": 598.8, "duration": 2.81 }, { "text": "And so from React Web, if you\nwant to attach a handler to that", "start": 601.61, "duration": 3.66 }, { "text": "you do on click.", "start": 605.27, "duration": 1.2 }, { "text": "Whereas, in React Native, you\ndo on press which was actually", "start": 606.47, "duration": 3.78 }, { "text": "the bug in the code earlier.", "start": 610.25, "duration": 2.85 }, { "text": "And lastly we have these things\ncalled scroll views or lists which", "start": 613.1, "duration": 3.87 }, { "text": "don't really exist in web world, but\nthey do exist a lot in React Native,", "start": 616.97, "duration": 5.82 }, { "text": "and we'll be talking about\nthose in the future as well.", "start": 622.79, "duration": 2.852 }, { "text": "Of course there are many,\nmany other components", "start": 625.642, "duration": 1.958 }, { "text": "and, if you want to explore them,\nthe documentation is really good.", "start": 627.6, "duration": 3.84 }, { "text": "Cool, so let's actually\ntake that example", "start": 634.52, "duration": 2.04 }, { "text": "that we wrote last lecture,\nthe to do app, and actually", "start": 636.56, "duration": 3.36 }, { "text": "translate it into React Native.", "start": 639.92, "duration": 2.12 }, { "text": "And so I have here this implementation,\nwhich is exactly the code", "start": 642.04, "duration": 6.82 }, { "text": "that we wrote in the previous lecture.", "start": 648.86, "duration": 1.624 }, { "text": "And what we're going to do is\ncopy and paste that into the Snack", "start": 650.484, "duration": 2.666 }, { "text": "that we saw earlier and go ahead and\ntranslate that into React Native.", "start": 653.15, "duration": 3.18 }, { "text": "So this is just the command\nto copy it and let's actually", "start": 661.67, "duration": 4.74 }, { "text": "paste that into here.", "start": 666.41, "duration": 3.15 }, { "text": "So of course there's going to be many\nerrors just because this is React Web", "start": 669.56, "duration": 3.27 }, { "text": "and we're trying to run\nthis in React Native.", "start": 672.83, "duration": 2.22 }, { "text": "And so let's go ahead and\ntry to fix those errors.", "start": 675.05, "duration": 3.27 }, { "text": "And so first we see stuff like\nLI, input, button, and those", "start": 678.32, "duration": 4.71 }, { "text": "don't exist in React Native.", "start": 683.03, "duration": 1.32 }, { "text": "And so we'll have to first\nreplace those React Web", "start": 684.35, "duration": 3.45 }, { "text": "components with React Native ones.", "start": 687.8, "duration": 3.85 }, { "text": "And so this rendering\ndoes not exist anymore.", "start": 691.65, "duration": 2.255 }, { "text": "And so let's first do import\nsome stuff from React Native.", "start": 693.905, "duration": 6.375 }, { "text": "And we'll be talking about imports\nand exports a little bit later,", "start": 700.28, "duration": 3.82 }, { "text": "but just bear with me for now.", "start": 704.1, "duration": 1.73 }, { "text": "And so some things that we're\ngoing to need are stuff like View,", "start": 705.83, "duration": 3.16 }, { "text": "we'll need a Button and we'll need text.", "start": 708.99, "duration": 2.72 }, { "text": "And we'll need some scrolling\nviews and then maybe", "start": 715.88, "duration": 4.71 }, { "text": "some more stuff in a little bit.", "start": 720.59, "duration": 2.07 }, { "text": "So first let's work on\nthat quick to do component.", "start": 722.66, "duration": 3.36 }, { "text": "And so we have a list item here,\ninstead let's actually use a View.", "start": 726.02, "duration": 3.15 }, { "text": "Let's actually get rid of this input for\nnow, and we'll add that in a little bit", "start": 734.2, "duration": 4.35 }, { "text": "later.", "start": 738.55, "duration": 1.05 }, { "text": "But how are we going to change this\nlowercase button to React Native?", "start": 739.6, "duration": 4.336 }, { "text": "Well first we need to replace it\nwith the capital Button, which", "start": 743.936, "duration": 2.624 }, { "text": "is React Natives version of the button.", "start": 746.56, "duration": 2.174 }, { "text": "It no longer has an on click\nproperty and so instead we'll", "start": 748.734, "duration": 2.416 }, { "text": "pass an on press prop and then we don't\nactually wrap the content any more", "start": 751.15, "duration": 6.69 }, { "text": "instead we pass a title prop.", "start": 757.84, "duration": 2.48 }, { "text": "And so that button's done.", "start": 763.026, "duration": 1.874 }, { "text": "What are we going to do for span?", "start": 764.9, "duration": 1.73 }, { "text": "Anyone?", "start": 766.63, "duration": 1.536 }, { "text": "AUDIENCE: [INAUDIBLE]", "start": 768.166, "duration": 0.874 }, { "text": "SPEAKER 1: Yeah, we'll\nuse a text instead.", "start": 769.04, "duration": 1.749 }, { "text": "And now our to do is done.", "start": 773.41, "duration": 2.73 }, { "text": "So now let's start looking\ninto this app component.", "start": 776.14, "duration": 3.201 }, { "text": "So first let's get rid of this render,\nwhich does not exist in React Native,", "start": 782.17, "duration": 5.94 }, { "text": "and let's start working our way\nthrough this big return function here.", "start": 788.11, "duration": 3.48 }, { "text": "So first we have a div.", "start": 791.59, "duration": 1.23 }, { "text": "Instead of a div let's\ngo ahead and use a View.", "start": 792.82, "duration": 2.25 }, { "text": "And then we have a to do count.", "start": 798.639, "duration": 1.291 }, { "text": "So what are we going to\nuse instead of this div?", "start": 799.93, "duration": 3.27 }, { "text": "We can't use a View\notherwise an error will", "start": 803.2, "duration": 1.86 }, { "text": "be thrown because remember the\nonly way that we can include text", "start": 805.06, "duration": 3.63 }, { "text": "in React Native is by wrapping it in\nthis text component and same thing", "start": 808.69, "duration": 5.921 }, { "text": "with this.", "start": 814.611, "duration": 0.499 }, { "text": "All right, we see another button\nand we've seen a button before,", "start": 817.924, "duration": 2.666 }, { "text": "so all we have to do is replace\nthat lowercase b with a capital B,", "start": 820.59, "duration": 3.5 }, { "text": "change on click to on press and\nchange the content to be title.", "start": 824.09, "duration": 4.495 }, { "text": "And then we're done there.", "start": 835.007, "duration": 1.083 }, { "text": "All right, UL, unordered lists.", "start": 841.39, "duration": 2.29 }, { "text": "So how might we handle this\nunordered list in React Native?", "start": 843.68, "duration": 5.35 }, { "text": "So lists, ULs and ordered\nlists don't actually", "start": 849.03, "duration": 4.15 }, { "text": "exist in React Native, and the\nway that we handled those instead", "start": 853.18, "duration": 2.89 }, { "text": "are by using the scrolling\ncomponents because we don't know", "start": 856.07, "duration": 6.815 }, { "text": "how long that list is going\nto get and so we better", "start": 862.885, "duration": 2.125 }, { "text": "assume it's going to get pretty long\nand be able to scroll through them", "start": 865.01, "duration": 2.07 }, { "text": "if we needed.", "start": 867.08, "duration": 1.31 }, { "text": "And so unordered lists we\nreplace with scroll view", "start": 868.39, "duration": 4.18 }, { "text": "and now we have what\nwe were looking for.", "start": 872.57, "duration": 3.01 }, { "text": "So all of the React Native components,\nthe React components sorry,", "start": 875.58, "duration": 3.88 }, { "text": "we've changed to React Native\ncomponents and now we have--", "start": 879.46, "duration": 3.388 }, { "text": "Oops, something's breaking still.", "start": 882.848, "duration": 2.322 }, { "text": "Prompt is another one\nof those browser APIs", "start": 890.42, "duration": 3.9 }, { "text": "that just does not exist in\nthe React Native world which", "start": 894.32, "duration": 2.7 }, { "text": "caused our code to crash.", "start": 897.02, "duration": 1.39 }, { "text": "And so rather than using\none of these browser", "start": 898.41, "duration": 2.96 }, { "text": "APIs let's just replace it with\nsome hard coded text for now,", "start": 901.37, "duration": 3.21 }, { "text": "maybe something like to do, number,\nand then whatever it's ID is.", "start": 904.58, "duration": 8.15 }, { "text": "And rather than\nincrementing ID down there", "start": 916.511, "duration": 1.749 }, { "text": "let's actually increment it up here.", "start": 918.26, "duration": 1.5 }, { "text": "And so now it works.", "start": 922.51, "duration": 1.15 }, { "text": "So we have to do number\none, to do number two", "start": 923.66, "duration": 2.42 }, { "text": "and you can see how we add those.", "start": 926.08, "duration": 3.31 }, { "text": "And so we'll fix the\nstyle in the future,", "start": 929.39, "duration": 2.1 }, { "text": "but basically does\nanybody have questions", "start": 931.49, "duration": 2.64 }, { "text": "on going from React Web to React Native?", "start": 934.13, "duration": 3.39 }, { "text": "What we did there was just replace\nall the React Web components", "start": 937.52, "duration": 3.36 }, { "text": "with the React Native components.", "start": 940.88, "duration": 1.89 }, { "text": "And it is almost as easy as command\neffing and replacing like that.", "start": 942.77, "duration": 4.52 }, { "text": "Great.", "start": 951.291, "duration": 0.499 }, { "text": "And so how are you going to go\nabout styling those components?", "start": 951.79, "duration": 3.27 }, { "text": "So in React Web the way\nwe did that was just", "start": 955.06, "duration": 2.9 }, { "text": "by adding a class name and then\nstyling in CSS, but in React Native", "start": 957.96, "duration": 3.89 }, { "text": "we don't have this concept of CSS.", "start": 961.85, "duration": 3.492 }, { "text": "So the way that React\nNative handles that", "start": 965.342, "duration": 1.708 }, { "text": "is by actually using JavaScript objects\nfor styling and what that gives us", "start": 967.05, "duration": 3.33 }, { "text": "is the ability to use dynamic styles.", "start": 970.38, "duration": 4.11 }, { "text": "Object keys in these objects\nare based on CSS properties,", "start": 974.49, "duration": 3.35 }, { "text": "so we have stuff like margin top,\nmargin bottom, margin padding.", "start": 977.84, "duration": 5.726 }, { "text": "And the layout system\nthat we used is Flexbox", "start": 983.566, "duration": 1.874 }, { "text": "and so, if you're familiar\nwith the Flexbox system in web,", "start": 985.44, "duration": 2.416 }, { "text": "it's almost exactly the\nsame in React Native.", "start": 987.856, "duration": 3.104 }, { "text": "One of the key differences\nis that rather than", "start": 990.96, "duration": 3.06 }, { "text": "defaulting to row we default to\nlaying out things in columns.", "start": 994.02, "duration": 7.48 }, { "text": "So, in React Web, we have this\nconcept of pixels or percentages,", "start": 1001.5, "duration": 3.52 }, { "text": "but in React Native we actually\nuse unit-less numbers for length.", "start": 1005.02, "duration": 4.36 }, { "text": "Which is good because there\nare so many different devices", "start": 1009.38, "duration": 3.48 }, { "text": "that this runs on with\ndifferent pixel densities", "start": 1012.86, "duration": 2.19 }, { "text": "that having a unit-less number allows\nus to abstract that pixel density out.", "start": 1015.05, "duration": 4.18 }, { "text": "The style prop, so the way that\nyou style a given component", "start": 1022.52, "duration": 3.72 }, { "text": "is by assigning that JavaScript\nobject to a style prop", "start": 1026.24, "duration": 3.89 }, { "text": "and it can actually\ntake an array of styles.", "start": 1030.13, "duration": 1.87 }, { "text": "And so, if you wanted to have a bunch\nof different class names in React Web", "start": 1032.0, "duration": 3.78 }, { "text": "you'd just start adding those\nwith a space in between.", "start": 1035.78, "duration": 3.6 }, { "text": "But in React Native you handled\nthat by passing an array of styles.", "start": 1039.38, "duration": 3.075 }, { "text": "So let's go ahead and add styles\nto this app that we have here.", "start": 1045.109, "duration": 7.121 }, { "text": "So first, let's go ahead\nand style this view.", "start": 1052.23, "duration": 2.24 }, { "text": "And so right now, as you see, each\nto do has a Delete button and a", "start": 1054.47, "duration": 6.27 }, { "text": "to do right under it, whereas in web\nwe had the Delete button next to that", "start": 1060.74, "duration": 4.24 }, { "text": "to do.", "start": 1064.98, "duration": 0.5 }, { "text": "And so let's try to\nfigure out exactly how we", "start": 1065.48, "duration": 2.32 }, { "text": "can get that Delete button over\nhere and the to do to be next to it.", "start": 1067.8, "duration": 4.7 }, { "text": "And so does anybody have any ideas\nabout how we might want to do that?", "start": 1072.5, "duration": 4.59 }, { "text": "We know we're going to\nhave to pass a style prop,", "start": 1077.09, "duration": 2.43 }, { "text": "and we know that it's\ngoing to be an object.", "start": 1079.52, "duration": 3.45 }, { "text": "And so this looks weird to\nhave those double curlies", "start": 1082.97, "duration": 2.46 }, { "text": "but the outer curlies\nmean, hey here comes", "start": 1085.43, "duration": 2.7 }, { "text": "some JavaScript and the inner\ncurlies are just an object", "start": 1088.13, "duration": 3.06 }, { "text": "literal like we've seen before.", "start": 1091.19, "duration": 2.88 }, { "text": "And so how might we get\nthose to dos to be in a row?", "start": 1094.07, "duration": 3.61 }, { "text": "So we saw before that React Native uses\nFlexbox in order to handle it's layout.", "start": 1100.35, "duration": 6.81 }, { "text": "And so we can just say, hey we want\nthe flex direction, rather than being", "start": 1107.16, "duration": 6.63 }, { "text": "column by default, let's set it to row.", "start": 1113.79, "duration": 2.818 }, { "text": "And so now we have the Delete\nbutton next to the to do.", "start": 1119.42, "duration": 5.19 }, { "text": "But it's annoying me a little bit how\nthat button is slightly below the text.", "start": 1124.61, "duration": 5.77 }, { "text": "And so how might we get those\nitems to be aligned to center?", "start": 1130.38, "duration": 5.36 }, { "text": "Well in Flexbox we have this\nthing called align items", "start": 1135.74, "duration": 6.12 }, { "text": "and then you just say, hey\nalign those to be center.", "start": 1141.86, "duration": 3.66 }, { "text": "Then all of a sudden we\nhave it aligned center.", "start": 1145.52, "duration": 2.27 }, { "text": "And just confirming that\ndelete button does indeed work.", "start": 1151.55, "duration": 3.841 }, { "text": "Great.", "start": 1155.391, "duration": 0.499 }, { "text": "Anybody notice any other\nbad style bugs over here?", "start": 1155.89, "duration": 3.45 }, { "text": "So look at this.", "start": 1161.89, "duration": 3.25 }, { "text": "So most phones will have some\nnav bar, and that nav bar", "start": 1165.14, "duration": 5.72 }, { "text": "will have stuff like if you're\nconnected to Wi-Fi, the time and etc.", "start": 1170.86, "duration": 3.87 }, { "text": "But that first view is actually\ngoing all the way to the top.", "start": 1174.73, "duration": 3.95 }, { "text": "And so how might we get all this\ncontent to be slightly moved down?", "start": 1178.68, "duration": 3.46 }, { "text": "So how would we do that in Web?", "start": 1186.29, "duration": 2.75 }, { "text": "AUDIENCE: Add a margin.", "start": 1189.04, "duration": 1.98 }, { "text": "SPEAKER 1: Yeah we would add a\nmargin or some padding or something", "start": 1191.02, "duration": 2.75 }, { "text": "to move the content from\nthe top slightly down.", "start": 1193.77, "duration": 4.09 }, { "text": "And so we could do that\nin React Native as well.", "start": 1197.86, "duration": 3.68 }, { "text": "So you can say with this view, let's\nactually put a padding on the top", "start": 1201.54, "duration": 7.57 }, { "text": "and let's just say something like\n50 which is somewhat arbitrary.", "start": 1209.11, "duration": 4.741 }, { "text": "And now we've gone ahead and\nmoved that down slightly.", "start": 1213.851, "duration": 2.249 }, { "text": "But what if we wanted to move it down\nexactly the same amount as the status", "start": 1219.46, "duration": 6.08 }, { "text": "bar?", "start": 1225.54, "duration": 0.5 }, { "text": "Well there's this great\ntool called Expo which", "start": 1228.96, "duration": 2.85 }, { "text": "we'll talk about in length later today.", "start": 1231.81, "duration": 1.84 }, { "text": "They're also the people\nwho designed Snack.", "start": 1233.65, "duration": 2.3 }, { "text": "And they actually give you\nthis thing called constant", "start": 1235.95, "duration": 2.61 }, { "text": "so we can do import constants from Expo.", "start": 1238.56, "duration": 3.515 }, { "text": "And we can actually say,\nhey padding top rather", "start": 1245.56, "duration": 2.2 }, { "text": "than assigning an arbitrary value of\n50 let's do constants.statusbarheight", "start": 1247.76, "duration": 8.76 }, { "text": "and that will actually give you\nthat exact status bar height", "start": 1256.52, "duration": 4.05 }, { "text": "even if you're on different phones.", "start": 1260.57, "duration": 2.174 }, { "text": "And so, again, we'll\ntalk about Expo and we'll", "start": 1262.744, "duration": 1.916 }, { "text": "talk about importing and\nexporting later today.", "start": 1264.66, "duration": 3.17 }, { "text": "But just FYI there's this thing called\nconstants.statusbarheight which will", "start": 1267.83, "duration": 3.99 }, { "text": "give us exactly that status bar height.", "start": 1271.82, "duration": 2.67 }, { "text": "And now we have a to do app\nthat doesn't look half bad.", "start": 1274.49, "duration": 3.14 }, { "text": "Cool.", "start": 1277.63, "duration": 0.5 }, { "text": "So there's actually this\nthing called style sheet", "start": 1278.13, "duration": 2.27 }, { "text": "which is part of React Native which has\nsome optimizations for these styles.", "start": 1280.4, "duration": 5.91 }, { "text": "And so we talked about earlier how the\nway that your JavaScript communicates", "start": 1286.31, "duration": 4.11 }, { "text": "with the UI throughout\nis through this bridge.", "start": 1290.42, "duration": 3.65 }, { "text": "And so that means every single\ntime you want a view with the style", "start": 1294.07, "duration": 3.46 }, { "text": "you need to send those style\nattributes over the bridge.", "start": 1297.53, "duration": 2.7 }, { "text": "There's actually a way\nthat you can optimize this.", "start": 1300.23, "duration": 2.13 }, { "text": "And so the Facebook team\ncreated this thing called", "start": 1302.36, "duration": 2.083 }, { "text": "style sheet which does this for you.", "start": 1304.443, "duration": 1.967 }, { "text": "So it's basically the same thing\nas creating objects for style,", "start": 1306.41, "duration": 2.67 }, { "text": "but as an additional optimization that\nrather than sending this style object", "start": 1309.08, "duration": 5.04 }, { "text": "over the bridge we can only send IDs.", "start": 1314.12, "duration": 2.55 }, { "text": "And so say we have these objects,\nrather than passing the full object", "start": 1316.67, "duration": 6.78 }, { "text": "every time you can just\nsay, hey this object,", "start": 1323.45, "duration": 1.925 }, { "text": "let's assign it this\narbitrary ID of one.", "start": 1325.375, "duration": 2.215 }, { "text": "And so every time we say, hey\nthis should have a style of one,", "start": 1327.59, "duration": 4.72 }, { "text": "the UI thread will know, oh, I\nknow exactly what style that means.", "start": 1332.31, "duration": 4.11 }, { "text": "And so to do that, all we have to do\nis import this thing called style sheet", "start": 1336.42, "duration": 6.26 }, { "text": "from React Native and we can do this.", "start": 1342.68, "duration": 1.77 }, { "text": "And so we're saying we're declaring our\nstyles as a constant outside of our app", "start": 1351.83, "duration": 5.42 }, { "text": "and we're saying, hey\ncreate this style sheet", "start": 1357.25, "duration": 2.64 }, { "text": "and we're going to pass into\nyou an object where the keys map", "start": 1359.89, "duration": 2.55 }, { "text": "with how we're going to use this later.", "start": 1362.44, "duration": 3.676 }, { "text": "And so we can go ahead\nand abstract this out.", "start": 1366.116, "duration": 1.874 }, { "text": "And so we can say, this\nhere we're actually", "start": 1367.99, "duration": 4.38 }, { "text": "going to call our to do container.", "start": 1372.37, "duration": 3.995 }, { "text": "And our to do container will just say,\nall right flex in the row direction", "start": 1380.085, "duration": 7.165 }, { "text": "and align your items to be centered.", "start": 1387.25, "duration": 2.922 }, { "text": "And then down here\nfor style we just say,", "start": 1390.172, "duration": 1.708 }, { "text": "oh, we want to use styles.todocontainer.", "start": 1391.88, "duration": 1.73 }, { "text": "And let's actually lowercase\nthis for convention.", "start": 1397.42, "duration": 5.04 }, { "text": "And as you can see those\nstyles still get applied.", "start": 1402.46, "duration": 3.86 }, { "text": "And why else might this pattern be good?", "start": 1406.32, "duration": 3.34 }, { "text": "Does anybody see something\nbetter about this pattern?", "start": 1409.66, "duration": 2.37 }, { "text": "AUDIENCE: It's reusable.", "start": 1415.229, "duration": 1.831 }, { "text": "SPEAKER 1: Yeah exactly, it's reusable.", "start": 1417.06, "duration": 3.21 }, { "text": "And so say we wanted to\nhave something else that", "start": 1420.27, "duration": 2.94 }, { "text": "had very similar styles\nto to do container", "start": 1423.21, "duration": 5.49 }, { "text": "we can actually use that again.", "start": 1428.7, "duration": 1.299 }, { "text": "And then if we wanted to change\nboth of them at the same time", "start": 1429.999, "duration": 2.541 }, { "text": "we could do it by using\nthis abstracted out object.", "start": 1432.54, "duration": 2.94 }, { "text": "And so let's do the app container\nand set that equal to this object.", "start": 1435.48, "duration": 7.66 }, { "text": "And then down here for this view\nlet's do styles.appcontainer.", "start": 1451.27, "duration": 3.75 }, { "text": "And so there's actually an\neven better reason to do this", "start": 1458.61, "duration": 4.99 }, { "text": "and it's because every\nsingle time we rendered,", "start": 1463.6, "duration": 2.29 }, { "text": "we used to be building a\nnew style object to pass.", "start": 1465.89, "duration": 2.87 }, { "text": "And now we're just using\nthat same reference", "start": 1468.76, "duration": 2.13 }, { "text": "to that object that we created\noutside of this component.", "start": 1470.89, "duration": 3.81 }, { "text": "And so that's just an additional\noptimization that, using this pattern,", "start": 1474.7, "duration": 4.99 }, { "text": "creating the styles outside of\nthe component allows us to do.", "start": 1479.69, "duration": 3.14 }, { "text": "Cool.", "start": 1485.56, "duration": 2.19 }, { "text": "Any questions on styles and styling?", "start": 1487.75, "duration": 3.68 }, { "text": "Great.", "start": 1491.43, "duration": 0.5 }, { "text": "So let's talk about event handling.", "start": 1494.69, "duration": 2.53 }, { "text": "So unlike Web not every component\nhas every single interaction.", "start": 1497.22, "duration": 3.71 }, { "text": "And so in Web, if we had a div, we\ncan assign an onClick to that div", "start": 1500.93, "duration": 3.24 }, { "text": "or if we add a list item or a list,\nanything we can assign an onClick to.", "start": 1504.17, "duration": 4.23 }, { "text": "But unlike Web there are only a few\ntouchable components in React Native", "start": 1508.4, "duration": 3.48 }, { "text": "and those are a button,\nwhich we've used before.", "start": 1511.88, "duration": 3.469 }, { "text": "These three things called touchable\nopacity, touchable highlight", "start": 1515.349, "duration": 2.666 }, { "text": "and touchable without\nfeedback, which are just", "start": 1518.015, "duration": 4.436 }, { "text": "three basic components that have\nslightly different reactions when", "start": 1522.451, "duration": 2.749 }, { "text": "you touch them.", "start": 1525.2, "duration": 1.292 }, { "text": "An then lastly, this thing\ncalled touchable native feedback", "start": 1526.492, "duration": 2.458 }, { "text": "which is this native component\nthat you can only use on Android.", "start": 1528.95, "duration": 4.09 }, { "text": "And so in Web, when you\nhad an event handler,", "start": 1536.33, "duration": 3.36 }, { "text": "that handler would receive\nthat event as an argument.", "start": 1539.69, "duration": 2.87 }, { "text": "But that's not necessarily true\nfor all React Native handlers.", "start": 1542.56, "duration": 5.88 }, { "text": "And so in order just to\nfind out how those work", "start": 1548.44, "duration": 2.57 }, { "text": "you basically have to\nconsult the documentation.", "start": 1551.01, "duration": 2.57 }, { "text": "The documentation for React\nNative is really good.", "start": 1553.58, "duration": 2.14 }, { "text": "I recommend that you\nperuse them just to see", "start": 1555.72, "duration": 2.03 }, { "text": "what components are available\nto you and those component APIs.", "start": 1557.75, "duration": 3.92 }, { "text": "And basically most of the\nstuff that you want to do", "start": 1561.67, "duration": 3.25 }, { "text": "is already pre-built for you.", "start": 1564.92, "duration": 1.742 }, { "text": "And so, even though\nthere's no such thing", "start": 1566.662, "duration": 1.708 }, { "text": "as a checkbox component in React Native,\nthere is actually a way to do that.", "start": 1568.37, "duration": 4.82 }, { "text": "And so let's go ahead\nand add this thing which", "start": 1573.19, "duration": 2.65 }, { "text": "is similar to a checkbox\nin that it's a Boolean flag", "start": 1575.84, "duration": 4.47 }, { "text": "but it's not really exactly\nstylistically the same.", "start": 1580.31, "duration": 3.462 }, { "text": "And so let's add this\nthing called a switch.", "start": 1583.772, "duration": 1.833 }, { "text": "As a switch you'll see what\nit looks like in a moment", "start": 1589.082, "duration": 3.428 }, { "text": "but it's basically just a Boolean flag\nthat we're going to use in this example", "start": 1592.51, "duration": 3.487 }, { "text": "rather than using a checkbox.", "start": 1595.997, "duration": 1.208 }, { "text": "So before what we had here was an\ninput of type checkbox and instead", "start": 1600.94, "duration": 4.04 }, { "text": "we're going to use a switch.", "start": 1604.98, "duration": 2.26 }, { "text": "And the value of that is going to be\nwhether the props.TODO is checked.", "start": 1607.24, "duration": 5.21 }, { "text": "And so now when we add\na TODO we see the switch", "start": 1616.7, "duration": 2.4 }, { "text": "that can be flipped back and forth.", "start": 1619.1, "duration": 2.41 }, { "text": "But if you notice, every\ntime I try to flip it on it", "start": 1621.51, "duration": 2.81 }, { "text": "immediately turns back off.", "start": 1624.32, "duration": 3.15 }, { "text": "Can anybody spot why that's happening?", "start": 1627.47, "duration": 2.7 }, { "text": "So we have this value set to props.TODO\nchecked, and down here do we ever", "start": 1634.97, "duration": 7.25 }, { "text": "update that value?", "start": 1642.22, "duration": 0.934 }, { "text": "No we don't.", "start": 1645.76, "duration": 0.81 }, { "text": "So we have this thing called Toggle\nTODO which we implemented last week,", "start": 1646.57, "duration": 3.93 }, { "text": "which, given some TODOs ID will\nflip that Boolean checked flag.", "start": 1650.5, "duration": 4.93 }, { "text": "But we never actually hook\nthat up for this example.", "start": 1655.43, "duration": 4.53 }, { "text": "So in the TODO component here\nwe're passing down an onToggle prop", "start": 1659.96, "duration": 4.97 }, { "text": "but we never actually use it up here.", "start": 1664.93, "duration": 2.52 }, { "text": "And so here we should actually\nhave the switch when we do on--", "start": 1667.45, "duration": 7.65 }, { "text": "what's it called-- on value change.", "start": 1675.1, "duration": 3.02 }, { "text": "And I only know this because\nthat's what the documentation says.", "start": 1678.12, "duration": 4.06 }, { "text": "And so when that value changed,\nI should run props.onToggle,", "start": 1682.18, "duration": 6.18 }, { "text": "which is the name of the\nprop that we're passing down", "start": 1688.36, "duration": 3.03 }, { "text": "from this parent component.", "start": 1691.39, "duration": 1.95 }, { "text": "And so now when we\nclick TODO it updates.", "start": 1693.34, "duration": 4.25 }, { "text": "And as you see right here the unchecked\nTODO count still updates as expected.", "start": 1697.59, "duration": 5.664 }, { "text": "And so now if we create a bunch\nwe can see those numbers changing.", "start": 1703.254, "duration": 3.986 }, { "text": "We can see the checking and\ntoggling works as expected.", "start": 1707.24, "duration": 2.75 }, { "text": "We see if we delete this\none the TODO count goes down", "start": 1709.99, "duration": 2.43 }, { "text": "but the uncheck count does not\ngo down since it was checked.", "start": 1712.42, "duration": 2.704 }, { "text": "If we delete something that isn't\nchecked, both of them go down.", "start": 1715.124, "duration": 2.666 }, { "text": "And so we have the same behavior as\nwe did in our React Web application.", "start": 1717.79, "duration": 5.917 }, { "text": "Any questions there?", "start": 1728.58, "duration": 1.18 }, { "text": "So we have this being a scroll view.", "start": 1735.2, "duration": 2.73 }, { "text": "But, if you notice, the\nscroll view gets cut off early", "start": 1737.93, "duration": 2.77 }, { "text": "and as we add TODOs it grows.", "start": 1740.7, "duration": 4.23 }, { "text": "But if we add a bunch of\nTODOs it grows with it.", "start": 1744.93, "duration": 11.59 }, { "text": "But say we actually wanted the scroll\nview to reach all the way to the bottom", "start": 1756.52, "duration": 6.54 }, { "text": "no matter how many TODOs were there.", "start": 1763.06, "duration": 2.25 }, { "text": "Does anybody know how we\nmay go about doing that?", "start": 1765.31, "duration": 3.37 }, { "text": "The hint being that in order\nto control layout what we use", "start": 1768.68, "duration": 2.687 }, { "text": "is this thing called Flexbox.", "start": 1771.367, "duration": 1.208 }, { "text": "So by default components\nwill grow to however big", "start": 1775.66, "duration": 4.83 }, { "text": "they need to be in order\nto fit their children.", "start": 1780.49, "duration": 3.48 }, { "text": "But we have a way of saying, hey fill\nas much space as you possibly can.", "start": 1783.97, "duration": 4.69 }, { "text": "And the way to do that\nis saying flex: 1.", "start": 1788.66, "duration": 3.65 }, { "text": "And so we want the app container\nto have a flex value of 1", "start": 1792.31, "duration": 3.94 }, { "text": "so that the app container\nfills up all possible space.", "start": 1796.25, "duration": 4.4 }, { "text": "And so we could do something like\napp container let's do flex: 1.", "start": 1800.65, "duration": 4.59 }, { "text": "And then maybe for the TODOs,\nthe scroll view here, maybe we", "start": 1805.24, "duration": 5.67 }, { "text": "want that to fill as well.", "start": 1810.91, "duration": 1.97 }, { "text": "And so we could go\ndown to the scroll view", "start": 1812.88, "duration": 2.74 }, { "text": "and say the style is\ngoing to be flex: 1.", "start": 1815.62, "duration": 5.61 }, { "text": "But that isn't great design.", "start": 1821.23, "duration": 2.61 }, { "text": "What if we wanted instead to just have\na style called Fill, which will just", "start": 1823.84, "duration": 5.43 }, { "text": "fill whatever space is available.", "start": 1829.27, "duration": 2.49 }, { "text": "And so that would be a\ngood abstraction to have.", "start": 1831.76, "duration": 2.772 }, { "text": "So here we have a TODO container,\nwe have an app container,", "start": 1834.532, "duration": 2.458 }, { "text": "and let's actually create this style\ncalled Fill, which we'll just flex: 1.", "start": 1836.99, "duration": 4.37 }, { "text": "And so now we can say hey, we\nwant this scroll view to fill", "start": 1845.8, "duration": 9.34 }, { "text": "and we also want our\napp container to fill.", "start": 1855.14, "duration": 5.37 }, { "text": "But instead of adding\nthat to app container,", "start": 1860.51, "duration": 3.23 }, { "text": "what would be a better way to do it?", "start": 1863.74, "duration": 1.78 }, { "text": "Well, we can actually\napply both those styles.", "start": 1865.52, "duration": 1.98 }, { "text": "Does anybody remember how we could\ndo that, apply multiple styles", "start": 1867.5, "duration": 3.09 }, { "text": "to the same component?", "start": 1870.59, "duration": 1.666 }, { "text": "So in React Web we would actually\njust give it multiple classes.", "start": 1875.68, "duration": 4.49 }, { "text": "What is the analog in React Native?", "start": 1880.17, "duration": 1.64 }, { "text": "We can actually just pass an array.", "start": 1884.88, "duration": 2.18 }, { "text": "And so say first apply\nstyles.Appcontainer and then apply", "start": 1887.06, "duration": 3.58 }, { "text": "styles.fill.", "start": 1890.64, "duration": 0.75 }, { "text": "And now, if we add a\nbunch of TODOs we can", "start": 1894.63, "duration": 4.23 }, { "text": "see that it fills the available space.", "start": 1898.86, "duration": 2.4 }, { "text": "And then if we delete TODOs such that\nit doesn't fill the available space", "start": 1901.26, "duration": 3.96 }, { "text": "we see that it's not getting\ncut off at the bottom", "start": 1905.22, "duration": 3.06 }, { "text": "because it's filling all the way\ndown to the bottom of this container.", "start": 1908.28, "duration": 5.64 }, { "text": "So any questions on event\nhandling, styling or moving", "start": 1913.92, "duration": 4.92 }, { "text": "React Web to React Native components?", "start": 1918.84, "duration": 2.355 }, { "text": "No.", "start": 1923.925, "duration": 1.301 }, { "text": "So we've been talking about\nthis thing called components,", "start": 1925.226, "duration": 2.374 }, { "text": "but we haven't really dived too\ndeeply into what that really means.", "start": 1927.6, "duration": 4.5 }, { "text": "In the past few weeks, we've\ntalked about how components", "start": 1932.1, "duration": 2.61 }, { "text": "return a node, how they\nrepresent a discrete piece of UI,", "start": 1934.71, "duration": 3.08 }, { "text": "how all components should act like pure\nfunctions with respect to their props.", "start": 1937.79, "duration": 4.01 }, { "text": "But that's really where we\nstopped talking about components.", "start": 1941.8, "duration": 2.69 }, { "text": "And so this week, we're going to\ndive more deeply into components", "start": 1944.49, "duration": 2.73 }, { "text": "and what components actually are.", "start": 1947.22, "duration": 2.797 }, { "text": "And so there are actually\ntwo types of components.", "start": 1950.017, "duration": 2.083 }, { "text": "And we've actually seen\nboth of them already.", "start": 1952.1, "duration": 2.05 }, { "text": "So first is this thing called a\nstateless functional component.", "start": 1954.15, "duration": 2.92 }, { "text": "You might see it abbreviated as SFC or\nif you're reading blog posts online,", "start": 1957.07, "duration": 3.38 }, { "text": "some people call them pure\nfunctional components.", "start": 1960.45, "duration": 2.95 }, { "text": "And what those are\nbasically just functions.", "start": 1963.4, "duration": 3.65 }, { "text": "So something like this\nTODO that we've created", "start": 1967.05, "duration": 3.06 }, { "text": "is just a function that takes\nin props and returns some node.", "start": 1970.11, "duration": 3.72 }, { "text": "It has no concept of state.", "start": 1973.83, "duration": 2.676 }, { "text": "And so that's why it's called a\nstateless functional component.", "start": 1976.506, "duration": 2.624 }, { "text": "It's just a function with no state.", "start": 1979.13, "duration": 2.41 }, { "text": "And the second is a\nreact.component which", "start": 1984.09, "duration": 1.86 }, { "text": "we've been extending from but we\nhaven't really talked about it", "start": 1985.95, "duration": 3.21 }, { "text": "too far in depth.", "start": 1989.16, "duration": 1.91 }, { "text": "And so first let's talk about\nstateless functional components.", "start": 1991.07, "duration": 3.041 }, { "text": "So this is the simplest\ntype of component.", "start": 1999.03, "duration": 2.21 }, { "text": "You should use this when\nyou don't need any state.", "start": 2001.24, "duration": 3.24 }, { "text": "And what it is is a function that\ntakes props and returns a node.", "start": 2004.48, "duration": 4.04 }, { "text": "And it shouldn't do anything other\nthan taking props and return a node.", "start": 2008.52, "duration": 3.34 }, { "text": "It should be what's\ncalled a pure function.", "start": 2011.86, "duration": 1.999 }, { "text": "In other words, it should not have\nany side effects, like setting a value", "start": 2013.859, "duration": 3.041 }, { "text": "or pushing to an array, updating\nan object, something like that", "start": 2016.9, "duration": 4.86 }, { "text": "because it should just take\nin props and return the value.", "start": 2021.76, "duration": 4.2 }, { "text": "If you do stuff other than that you\nmight create some bugs or even worse", "start": 2025.96, "duration": 5.4 }, { "text": "crash your app or something like that.", "start": 2031.36, "duration": 3.264 }, { "text": "Then, any changes to the\nprops that you passed", "start": 2034.624, "duration": 1.916 }, { "text": "to a stateless functional\ncomponent will automatically", "start": 2036.54, "duration": 2.68 }, { "text": "cause that function to be re-invoked.", "start": 2039.22, "duration": 2.65 }, { "text": "And then after it re-invokes\nthe function and returns nodes,", "start": 2041.87, "duration": 3.2 }, { "text": "React will do its thing and compare\nnodes to what it has in its virtual dom", "start": 2045.07, "duration": 4.407 }, { "text": "and then go ahead and\nreplace what's needed.", "start": 2049.477, "duration": 1.833 }, { "text": "And so on the other side of the coin,\nwe have what's called a React.component.", "start": 2054.06, "duration": 3.966 }, { "text": "This is something that's actually\nprovided by the React library", "start": 2058.026, "duration": 2.624 }, { "text": "and implemented for you.", "start": 2060.65, "duration": 1.68 }, { "text": "And it's an abstract class that can be\nextended to behave however you want.", "start": 2062.33, "duration": 3.82 }, { "text": "And so, in our example\nhere and examples prior,", "start": 2066.15, "duration": 3.299 }, { "text": "we've been doing this thing where we\ncreate a class called whatever we want", "start": 2069.449, "duration": 3.421 }, { "text": "and we're actually extending this\nthing called a React component.", "start": 2072.87, "duration": 4.53 }, { "text": "And so what is a React.component?", "start": 2077.4, "duration": 3.981 }, { "text": "Well these things have\nadditional features", "start": 2081.381, "duration": 1.749 }, { "text": "that stateless functional\ncomponents do not.", "start": 2083.13, "duration": 2.46 }, { "text": "One of those, of course, is that they\nhave instances so they're a class.", "start": 2085.59, "duration": 3.247 }, { "text": "And so when you invoke that\nclass it returns an instance", "start": 2088.837, "duration": 2.333 }, { "text": "and that instance will persist\nthroughout the lifetime of this class.", "start": 2091.17, "duration": 5.719 }, { "text": "As suggested by the name,\nit maintains its own state", "start": 2096.889, "duration": 3.341 }, { "text": "and so stateless components do not,\nwhereas these React components do.", "start": 2100.23, "duration": 4.62 }, { "text": "They have this concept of\nstate, and we talked about state", "start": 2104.85, "duration": 2.58 }, { "text": "in depth last lecture.", "start": 2107.43, "duration": 0.96 }, { "text": "One thing we didn't\ntalk about last lecture", "start": 2111.399, "duration": 1.791 }, { "text": "is this thing called a lifecycle,\nit's lifecycle methods.", "start": 2113.19, "duration": 3.395 }, { "text": "And so these are similar\nto hooks or event handlers.", "start": 2116.585, "duration": 2.595 }, { "text": "And so we've used the event handlers\nbefore in both React Web and React", "start": 2119.18, "duration": 4.17 }, { "text": "Native.", "start": 2123.35, "duration": 1.29 }, { "text": "And these things are actually\nautomatically invoked for you.", "start": 2124.64, "duration": 2.56 }, { "text": "You don't have to worry about\nexactly the implementation details", "start": 2127.2, "duration": 3.65 }, { "text": "or when to invoke your own functions.", "start": 2130.85, "duration": 1.9 }, { "text": "That's actually something\nthat's done automatically.", "start": 2132.75, "duration": 4.13 }, { "text": "And so unlike stateless\nfunctional components,", "start": 2136.88, "duration": 2.43 }, { "text": "which just take props and return a\nnode, a React components render function", "start": 2139.31, "duration": 4.68 }, { "text": "actually becomes a function of\nthe props and also any class", "start": 2143.99, "duration": 3.06 }, { "text": "properties that exist.", "start": 2147.05, "duration": 1.06 }, { "text": "And so if you remember back to last\nlecture we talked about classes", "start": 2148.11, "duration": 3.44 }, { "text": "and how when you create\na class instance,", "start": 2151.55, "duration": 1.96 }, { "text": "you might attach to it some properties.", "start": 2153.51, "duration": 2.1 }, { "text": "These properties can be\nvalues anywhere from functions", "start": 2155.61, "duration": 3.23 }, { "text": "to just primitives, objects.", "start": 2158.84, "duration": 3.78 }, { "text": "And so when you create a\nclass component instance,", "start": 2162.62, "duration": 3.19 }, { "text": "you can actually use all of those\nclass properties in that render method.", "start": 2165.81, "duration": 5.85 }, { "text": "And so we saw that over here\nwhen we created this addTODO", "start": 2171.66, "duration": 7.04 }, { "text": "where within the render we referenced\nthis.addTODO in this button component", "start": 2178.7, "duration": 6.99 }, { "text": "here.", "start": 2185.69, "duration": 1.401 }, { "text": "And so, as you see, this is a\nclass property which we actually", "start": 2187.091, "duration": 3.249 }, { "text": "used in our render method.", "start": 2190.34, "duration": 1.56 }, { "text": "And so this render method is\nactually a function of both props", "start": 2191.9, "duration": 4.29 }, { "text": "and any class properties like its\nstate or these methods that we defined.", "start": 2196.19, "duration": 5.33 }, { "text": "Cool.", "start": 2204.72, "duration": 0.5 }, { "text": "So I talked about this thing\ncalled a lifecycle method,", "start": 2205.22, "duration": 2.74 }, { "text": "but what actually is\na component lifecycle.", "start": 2207.96, "duration": 3.08 }, { "text": "And so a component lifecycle can\nactually be represented by this graph.", "start": 2211.04, "duration": 3.89 }, { "text": "And so first a component will\nmount and so some lifecycle hooks", "start": 2214.93, "duration": 4.48 }, { "text": "get called there, but that's\nbasically that constructor where", "start": 2219.41, "duration": 4.06 }, { "text": "the class instance gets created and\nmaybe its state gets instantiated.", "start": 2223.47, "duration": 5.48 }, { "text": "And then what it does is it renders.", "start": 2228.95, "duration": 2.4 }, { "text": "It will just put you to the page.", "start": 2231.35, "duration": 3.03 }, { "text": "And then every time we call set\nstate or get new props we actually", "start": 2234.38, "duration": 3.21 }, { "text": "enter what's called an update cycle.", "start": 2237.59, "duration": 1.78 }, { "text": "And so when you receive new props,\nthe component needs to update.", "start": 2239.37, "duration": 2.97 }, { "text": "It needs to re-render.", "start": 2242.34, "duration": 1.44 }, { "text": "And so part of its lifecycle is\nactually updating over and over.", "start": 2243.78, "duration": 3.69 }, { "text": "And this happens any time new props\nget received because it wouldn't really", "start": 2247.47, "duration": 3.53 }, { "text": "make sense if we had\na component that when", "start": 2251.0, "duration": 2.13 }, { "text": "it received new props nothing changed.", "start": 2253.13, "duration": 2.61 }, { "text": "But also this update cycle happens\nevery single time the state changes.", "start": 2255.74, "duration": 3.88 }, { "text": "So if you call this .setstate, you\nupdate state and presumably you have", "start": 2259.62, "duration": 4.19 }, { "text": "something in the UI that also updates.", "start": 2263.81, "duration": 1.74 }, { "text": "And so this update\ncycle will happen again.", "start": 2265.55, "duration": 2.04 }, { "text": "And every single time you update state,\nreceive new props this update cycle", "start": 2267.59, "duration": 3.43 }, { "text": "will fire again.", "start": 2271.02, "duration": 1.67 }, { "text": "And then, when it's time for\nthat component to disappear,", "start": 2272.69, "duration": 2.96 }, { "text": "it enters what's called\nthe unmount stage where", "start": 2275.65, "duration": 3.91 }, { "text": "you have the chance to\nclean up anything that you", "start": 2279.56, "duration": 3.0 }, { "text": "may have created during that lifecycle.", "start": 2282.56, "duration": 4.44 }, { "text": "And so what actually does\nthat mean in practice?", "start": 2287.0, "duration": 3.652 }, { "text": "So I said that there's\nthis thing called mount,", "start": 2290.652, "duration": 1.958 }, { "text": "which is basically\njust a series of steps", "start": 2292.61, "duration": 2.04 }, { "text": "that happens when a component\nfirst gets mounted and rendered.", "start": 2294.65, "duration": 3.189 }, { "text": "And so the first thing that happens\nis the constructor gets called.", "start": 2297.839, "duration": 2.791 }, { "text": "And so, as we saw a few lectures\nago when we talked about classes,", "start": 2300.63, "duration": 3.35 }, { "text": "the constructor is where we have\na chance to add class properties", "start": 2303.98, "duration": 4.11 }, { "text": "or add anything to that\ninstance that we need.", "start": 2308.09, "duration": 3.3 }, { "text": "And so here we might want to\ndo stuff like initialize state", "start": 2311.39, "duration": 2.52 }, { "text": "or maybe add some other class\nproperties like bound methods et cetera.", "start": 2313.91, "duration": 5.52 }, { "text": "Next what happens is we render, which\nis just the meat of the component.", "start": 2319.43, "duration": 5.33 }, { "text": "The main goal of any\ncomponent is to show you why.", "start": 2324.76, "duration": 4.24 }, { "text": "And so, in this render method,\nthat's exactly what happens.", "start": 2329.0, "duration": 4.08 }, { "text": "You take any properties that you have,\nany class properties that you have,", "start": 2333.08, "duration": 4.66 }, { "text": "and then you end up returning a node.", "start": 2337.74, "duration": 3.209 }, { "text": "And then after that this hook,\nthat we haven't seen yet, gets", "start": 2340.949, "duration": 2.541 }, { "text": "called a component div mount.", "start": 2343.49, "duration": 1.646 }, { "text": "And this is the chance\nfor you to do anything", "start": 2345.136, "duration": 1.874 }, { "text": "that you wanted to do that\ndidn't really matter for render.", "start": 2347.01, "duration": 4.285 }, { "text": "And so if you have asynched actions,\nlike standing network or passes.", "start": 2351.295, "duration": 2.875 }, { "text": "If you want to add timers\nand stuff like that this", "start": 2354.17, "duration": 3.57 }, { "text": "is exactly where you should do that.", "start": 2357.74, "duration": 3.31 }, { "text": "And then maybe you'll need to\nupdate the state accordingly.", "start": 2361.05, "duration": 3.32 }, { "text": "And so if you actually\nsee that state here", "start": 2364.37, "duration": 2.01 }, { "text": "this will cause a re-render\nwithout updating the UI.", "start": 2366.38, "duration": 2.36 }, { "text": "And so all of this will happen\nbefore the UI re-renders.", "start": 2368.74, "duration": 3.57 }, { "text": "And so let's play with\nthat a little bit.", "start": 2372.31, "duration": 2.89 }, { "text": "So first let's copy this,\nthat way you can keep it.", "start": 2375.2, "duration": 3.81 }, { "text": "So let's clear all of this and then\nplay with mounting a little bit.", "start": 2389.01, "duration": 8.76 }, { "text": "And so in your next project what\nyou're going to do is you're", "start": 2397.77, "duration": 3.27 }, { "text": "going to be implementing\nwhat's called a Pomodoro timer.", "start": 2401.04, "duration": 3.76 }, { "text": "And so for those of you who\nare aware of what those are--", "start": 2404.8, "duration": 3.47 }, { "text": "basically what it is is\na timer that allows you", "start": 2408.27, "duration": 3.06 }, { "text": "to switch between two amounts of time.", "start": 2411.33, "duration": 3.069 }, { "text": "And so there's this thing called\nthe Pomodoro Technique which", "start": 2414.399, "duration": 2.541 }, { "text": "is used for studying where you study or\nwork very hard for some amount of time", "start": 2416.94, "duration": 4.187 }, { "text": "and then you take a short break.", "start": 2421.127, "duration": 1.333 }, { "text": "And then you work hard for\nsome short amount of time", "start": 2422.46, "duration": 2.166 }, { "text": "and then you take a short break.", "start": 2424.626, "duration": 1.605 }, { "text": "And so what you're going to be\ncreating in your next project", "start": 2426.231, "duration": 2.499 }, { "text": "is actually a timer that\nallows you to do that.", "start": 2428.73, "duration": 2.46 }, { "text": "It will automatically track for\nyou however long you want to work.", "start": 2431.19, "duration": 4.79 }, { "text": "And it will also allow\nyou to set some time", "start": 2435.98, "duration": 2.227 }, { "text": "that you want to take a break\nin between those working blocks.", "start": 2438.207, "duration": 2.583 }, { "text": "And it will do that.", "start": 2440.79, "duration": 0.833 }, { "text": "So when you start the timer it will\ncount down until you're done working.", "start": 2441.623, "duration": 3.787 }, { "text": "It will let you know,\nhey time for a break.", "start": 2445.41, "duration": 2.28 }, { "text": "Then it will switch over\nto that break timer,", "start": 2447.69, "duration": 1.98 }, { "text": "run down until that ends up and\nsays, oh now it's time to work,", "start": 2449.67, "duration": 3.12 }, { "text": "and it will cycle through that.", "start": 2452.79, "duration": 1.82 }, { "text": "And so in order to do\nthat, you might need", "start": 2454.61, "duration": 1.75 }, { "text": "to know how to work things like timers.", "start": 2456.36, "duration": 2.85 }, { "text": "And so these methods may be a chance\nfor you to set up those timers.", "start": 2459.21, "duration": 5.41 }, { "text": "And so let's go ahead and\ndo an example doing that.", "start": 2464.62, "duration": 3.08 }, { "text": "So first let's get rid\nof all of this stuff.", "start": 2467.7, "duration": 6.22 }, { "text": "So in this example we\nno longer need TODOs.", "start": 2473.92, "duration": 4.27 }, { "text": "And in our render lets\nactually just have some count.", "start": 2478.19, "duration": 4.88 }, { "text": "Those have a View.", "start": 2483.07, "duration": 0.75 }, { "text": "And inside here let's have some text.", "start": 2488.62, "duration": 1.96 }, { "text": "And that text is just\ngoing to show some count.", "start": 2496.27, "duration": 2.1 }, { "text": "And let's initialize that count to 0.", "start": 2505.04, "duration": 1.67 }, { "text": "So, as soon as I get rid of all this, we\nhave a very simple app and all it does", "start": 2515.55, "duration": 5.31 }, { "text": "is show a count of 0.", "start": 2520.86, "duration": 1.07 }, { "text": "You see it up in this\nleft hand corner here.", "start": 2521.93, "duration": 2.49 }, { "text": "And let's go ahead and\nactually center this.", "start": 2524.42, "duration": 1.88 }, { "text": "So let's do styles, and let's\ncreate a new style sheet.", "start": 2526.3, "duration": 6.74 }, { "text": "Let's call this app container and\nwhatever we want to do in here.", "start": 2537.42, "duration": 3.91 }, { "text": "So let's have it grow as much as we can.", "start": 2541.33, "duration": 2.719 }, { "text": "Let's have it center its items.", "start": 2544.049, "duration": 1.291 }, { "text": "And let's have it align its items\nthe other direction as well.", "start": 2549.69, "duration": 4.95 }, { "text": "And down here let's actually pass\nthis in as styles.appcontainer.", "start": 2562.24, "duration": 4.76 }, { "text": "So now we see this\nnumber 0 is very small.", "start": 2573.0, "duration": 1.97 }, { "text": "Let's go ahead and make\nit a little bit bigger.", "start": 2574.97, "duration": 2.73 }, { "text": "And so let's have count and\nlet's give it a big font", "start": 2577.7, "duration": 4.65 }, { "text": "size of like 48 or something.", "start": 2582.35, "duration": 3.669 }, { "text": "So now-- style--", "start": 2592.87, "duration": 4.94 }, { "text": "we have a big number\nand all it does is 0.", "start": 2597.81, "duration": 2.16 }, { "text": "And so how are we going\nto get it to count?", "start": 2599.97, "duration": 2.43 }, { "text": "Well presumably we should\nbe setting our state", "start": 2602.4, "duration": 2.25 }, { "text": "and just have that state increase.", "start": 2604.65, "duration": 4.05 }, { "text": "And so if we want to repeat something\nwhat might we use to achieve that?", "start": 2608.7, "duration": 5.69 }, { "text": "So do something at some given interval.", "start": 2618.21, "duration": 3.81 }, { "text": "We can actually use that set interval.", "start": 2622.02, "duration": 2.83 }, { "text": "And so let's first implement this count.", "start": 2624.85, "duration": 2.2 }, { "text": "So let's have this\nthing called increment.", "start": 2627.05, "duration": 2.1 }, { "text": "Is this too small?", "start": 2629.15, "duration": 3.44 }, { "text": "Let's have this thing called ink.", "start": 2632.59, "duration": 2.05 }, { "text": "And what that does is it does\nthis.setstate, takes a previous state", "start": 2634.64, "duration": 8.81 }, { "text": "and returns the count to be 1\ngreater than the previous state.", "start": 2643.45, "duration": 4.74 }, { "text": "And so now we've implemented this\nthing called increment where we", "start": 2653.45, "duration": 4.77 }, { "text": "take the states count and increment it.", "start": 2658.22, "duration": 3.25 }, { "text": "And so how might we get this increment\ncall to happen every once in a while?", "start": 2661.47, "duration": 6.497 }, { "text": "Well presumably we\nshould use a thing called", "start": 2667.967, "duration": 1.833 }, { "text": "set interval, which we'll call\na function every n milliseconds.", "start": 2669.8, "duration": 4.35 }, { "text": "But how are we going\nto get that to happen?", "start": 2674.15, "duration": 3.219 }, { "text": "Well we should do this after\nthe component finishes mounting.", "start": 2677.369, "duration": 2.541 }, { "text": "We should set up some sort of timer.", "start": 2679.91, "duration": 2.33 }, { "text": "And so how do we know or\nhow do we get a function", "start": 2682.24, "duration": 2.26 }, { "text": "to execute after\ncomponents finish mounting?", "start": 2684.5, "duration": 4.73 }, { "text": "That's actually where this\nlifecycle method comes in.", "start": 2689.23, "duration": 2.519 }, { "text": "And so there's this\nlifecycle method called", "start": 2691.749, "duration": 1.791 }, { "text": "componentDidMount which\nautomatically gets", "start": 2693.54, "duration": 2.54 }, { "text": "run for after the component mounts.", "start": 2696.08, "duration": 2.63 }, { "text": "And so we can actually set\nup our timer tap in there.", "start": 2698.71, "duration": 3.38 }, { "text": "And so we can do componentDidMount\nand inside of it we can do setInterval", "start": 2702.09, "duration": 8.42 }, { "text": "and that's called\nthis.Increment every second.", "start": 2710.51, "duration": 5.58 }, { "text": "And so there's going\nto be a small bug here.", "start": 2716.09, "duration": 4.56 }, { "text": "Does anybody know what the bug is?", "start": 2720.65, "duration": 3.25 }, { "text": "So every 1,000 milliseconds this\nthing called this.Increment is getting", "start": 2727.08, "duration": 3.81 }, { "text": "called.", "start": 2730.89, "duration": 1.0 }, { "text": "And this.Increment is\ninvoking this.Setstate.", "start": 2731.89, "duration": 3.42 }, { "text": "Does anybody see how\nthat might be a problem?", "start": 2737.98, "duration": 3.206 }, { "text": "I'll give you a hint.", "start": 2747.15, "duration": 0.99 }, { "text": "It has something to do with this.", "start": 2748.14, "duration": 4.52 }, { "text": "And so when this gets executed this\nmight not mean what this is here.", "start": 2752.66, "duration": 4.83 }, { "text": "And so this is a common bug in React.", "start": 2757.49, "duration": 1.94 }, { "text": "Oftentimes, when you say\nthis here, what it means", "start": 2759.43, "duration": 4.59 }, { "text": "lexically is not\nnecessarily what it means", "start": 2764.02, "duration": 2.13 }, { "text": "when it actually eventually gets run.", "start": 2766.15, "duration": 2.16 }, { "text": "And so how did we go about fixing\nthis bug in previous lectures?", "start": 2768.31, "duration": 7.06 }, { "text": "We have to somehow get this to be\nbound to what we want it to be.", "start": 2775.37, "duration": 3.81 }, { "text": "And in case we want it\nto be bound lexically.", "start": 2779.18, "duration": 4.27 }, { "text": "So a few different ways to do that.", "start": 2783.45, "duration": 1.93 }, { "text": "One way was creating a new function\nhere in line that does this.", "start": 2785.38, "duration": 4.22 }, { "text": "And I'm actually not sure that\nthat works in this example", "start": 2793.0, "duration": 5.39 }, { "text": "because this isn't necessarily set here.", "start": 2798.39, "duration": 3.18 }, { "text": "The way we've generally handled\nthis is by actually binding it", "start": 2801.57, "duration": 7.12 }, { "text": "at creation time.", "start": 2808.69, "duration": 2.54 }, { "text": "And so there's a\nshorthand whereby we can", "start": 2811.23, "duration": 10.79 }, { "text": "say when we create this\nincrement function automatically", "start": 2822.02, "duration": 4.5 }, { "text": "bind it to this class.", "start": 2826.52, "duration": 2.01 }, { "text": "And so this is actually a new\naddition to the ECMAScript standard", "start": 2828.53, "duration": 6.33 }, { "text": "where this is called\nclass properties whereby", "start": 2834.86, "duration": 2.34 }, { "text": "inline as we're declaring\nthis class we can also", "start": 2837.2, "duration": 2.97 }, { "text": "create properties that should\nbe added during the constructor.", "start": 2840.17, "duration": 3.7 }, { "text": "And so this is syntactically\nthe same as doing this.", "start": 2843.87, "duration": 6.41 }, { "text": "Having the increment function\ndefined as we did before.", "start": 2850.28, "duration": 9.79 }, { "text": "And in the constructor\nwhen this is created", "start": 2860.07, "duration": 2.02 }, { "text": "doing this.Increment equals\nthis.Increment.bind whatever", "start": 2862.09, "duration": 9.4 }, { "text": "the this context we want to be.", "start": 2871.49, "duration": 2.29 }, { "text": "And in this case it's this.", "start": 2873.78, "duration": 3.06 }, { "text": "Or in other words, it's\nthe same as just doing", "start": 2876.84, "duration": 2.16 }, { "text": "this.Increment is equal to the anonymous\nfunction that we defined down there.", "start": 2879.0, "duration": 4.01 }, { "text": "Whatever.", "start": 2887.03, "duration": 2.58 }, { "text": "And so that's just, rather than having\nto write everything in the constructor,", "start": 2889.61, "duration": 5.56 }, { "text": "we can just use this\nshorthand down here which", "start": 2895.17, "duration": 7.68 }, { "text": "is just generally the preferred\nway because it's easier to read.", "start": 2902.85, "duration": 6.164 }, { "text": "And now we've gone ahead and\ncreated this timer that runs.", "start": 2909.014, "duration": 2.416 }, { "text": "And so you can see that\nthe numbers are going up.", "start": 2911.43, "duration": 3.277 }, { "text": "And the reason that this is happening\nis because we created this increment", "start": 2914.707, "duration": 3.083 }, { "text": "function.", "start": 2917.79, "duration": 0.75 }, { "text": "We correctly bound it to the\nthis that we wanted to bind it to", "start": 2918.54, "duration": 3.177 }, { "text": "and then we said, hey,\ncomponent when you're", "start": 2921.717, "duration": 1.833 }, { "text": "done mounting set up this timer, set up\non an interval of 1,000 milliseconds.", "start": 2923.55, "duration": 5.32 }, { "text": "So every second call\nthis increment function.", "start": 2928.87, "duration": 2.522 }, { "text": "And what does that\nincrement function do?", "start": 2931.392, "duration": 1.708 }, { "text": "Well it updates the state to be\nthe previous states count plus 1", "start": 2933.1, "duration": 3.5 }, { "text": "and then down in the render\nwe render this.state.count.", "start": 2936.6, "duration": 3.3 }, { "text": "And so you see every second the\nstate gets updated to a new number,", "start": 2939.9, "duration": 4.17 }, { "text": "and the new number is shown there.", "start": 2944.07, "duration": 2.46 }, { "text": "And so we never had to manually say\nhey, run this code when you mount.", "start": 2946.53, "duration": 9.15 }, { "text": "We just created this method called\ncomponent.DidMount and React handles", "start": 2955.68, "duration": 5.28 }, { "text": "automatically invoking that for\nyou after the component mounted.", "start": 2960.96, "duration": 2.82 }, { "text": "And it's the same as the constructor.", "start": 2967.59, "duration": 2.08 }, { "text": "We never had to manually\ninvoke the constructor.", "start": 2969.67, "duration": 2.08 }, { "text": "It just gets called automatically when\na class instance is getting created?", "start": 2971.75, "duration": 3.65 }, { "text": "Any questions on the mount cycle or\nthe mount processor I should say?", "start": 2978.22, "duration": 5.25 }, { "text": "Great so now let's talk\nabout the update cycle.", "start": 2991.01, "duration": 4.23 }, { "text": "And so, just like in\nthe mounting process,", "start": 2995.24, "duration": 2.17 }, { "text": "there is a bunch of lifecycle hooks\nthat got called for you automatically.", "start": 2997.41, "duration": 3.35 }, { "text": "There are also a bunch\nof lifecycle hooks", "start": 3000.76, "duration": 1.708 }, { "text": "that get called automatically every\nsingle time we want to re-render.", "start": 3002.468, "duration": 4.762 }, { "text": "And so the first thing\nthat happens is component", "start": 3007.23, "duration": 2.38 }, { "text": "will receive props which\ntakes the next props.", "start": 3009.61, "duration": 3.54 }, { "text": "And so say you had\nsomething in your state", "start": 3013.15, "duration": 2.1 }, { "text": "that really depended on\nwhat the props were set to,", "start": 3015.25, "duration": 3.744 }, { "text": "you can actually use this function\nto update any of those state fields", "start": 3018.994, "duration": 2.916 }, { "text": "that rely on the props.", "start": 3021.91, "duration": 1.51 }, { "text": "And you do that by\ncalling this .setstate.", "start": 3023.42, "duration": 2.9 }, { "text": "Next is this thing called\nshouldComponentUpdate,", "start": 3026.32, "duration": 3.15 }, { "text": "which takes the next\nprops and the next date,", "start": 3029.47, "duration": 2.94 }, { "text": "and here you can compare change\nvalues and decide whether or not", "start": 3032.41, "duration": 3.54 }, { "text": "you want that component to render.", "start": 3035.95, "duration": 1.47 }, { "text": "And you can actually stop\nthe update cycle here.", "start": 3037.42, "duration": 4.14 }, { "text": "And so this is a good optimization.", "start": 3044.26, "duration": 2.1 }, { "text": "So say you have a very\ncomplicated component", "start": 3046.36, "duration": 2.14 }, { "text": "that takes a really long time\nto render you don't necessarily", "start": 3048.5, "duration": 3.54 }, { "text": "want it to render every\nsingle time you get a new prop", "start": 3052.04, "duration": 3.18 }, { "text": "because it might be that the\nnew prop doesn't actually", "start": 3055.22, "duration": 2.25 }, { "text": "change anything that's shown.", "start": 3057.47, "duration": 2.04 }, { "text": "And so you could use this\nmethod to stop it early.", "start": 3059.51, "duration": 4.02 }, { "text": "But that adds a lot of\ncomplexity to your app", "start": 3063.53, "duration": 1.89 }, { "text": "and there's almost always\na premature optimization.", "start": 3065.42, "duration": 3.45 }, { "text": "The next happens render, we\nknow exactly what happens there.", "start": 3068.87, "duration": 4.02 }, { "text": "And last we have this thing\ncalled ComponentDidUpdate,", "start": 3072.89, "duration": 3.06 }, { "text": "whereby you can do anything\nthat isn't needed for the UI,", "start": 3075.95, "duration": 3.06 }, { "text": "like network requests,\nwhich is basically", "start": 3079.01, "duration": 2.25 }, { "text": "the analog for ComponentDidMount.", "start": 3081.26, "duration": 2.69 }, { "text": "And so let's see an\nexample for this update.", "start": 3083.95, "duration": 3.17 }, { "text": "So say rather than just\nrendering this text we actually", "start": 3087.12, "duration": 8.78 }, { "text": "pass this count to another function.", "start": 3095.9, "duration": 3.16 }, { "text": "So let's have this thing called a\ncount, which takes as a propped account.", "start": 3099.06, "duration": 10.29 }, { "text": "And then let's create\nthis class called count.", "start": 3114.47, "duration": 3.05 }, { "text": "And then here, let's first\njust render the text.", "start": 3122.61, "duration": 3.315 }, { "text": "And so now we're back to where\nwe started, where we basically", "start": 3132.16, "duration": 10.8 }, { "text": "have some text that gets rendered\nbased on this.PropsAccount,", "start": 3142.96, "duration": 4.73 }, { "text": "and let's style it to be larger.", "start": 3147.69, "duration": 2.87 }, { "text": "And so now we're basically back\nto exactly where we started.", "start": 3158.201, "duration": 2.499 }, { "text": "We have this app which\nautomatically increments", "start": 3160.7, "duration": 4.7 }, { "text": "and then it passes whatever its\nstate count is as the count prop", "start": 3165.4, "duration": 3.51 }, { "text": "to this other class that we call count.", "start": 3168.91, "duration": 4.22 }, { "text": "And then this component\nwe basically just take", "start": 3173.13, "duration": 2.35 }, { "text": "that prop and render that text.", "start": 3175.48, "duration": 4.11 }, { "text": "But say we actually only wanted\nto update on odd numbers.", "start": 3179.59, "duration": 5.94 }, { "text": "So say we want to create a new--", "start": 3185.53, "duration": 4.1 }, { "text": "or say we want to have this\nbe called CountEvennumbers.", "start": 3189.63, "duration": 5.39 }, { "text": "And so, in this example\nhere, how might we say,", "start": 3203.29, "duration": 4.79 }, { "text": "hey don't actually update\nunless your number is even?", "start": 3208.08, "duration": 5.97 }, { "text": "Right now every single time it\nreceives a new prop it's updating.", "start": 3214.05, "duration": 5.02 }, { "text": "But say we only wanted to count even\nnumbers, what's some strategy that we", "start": 3219.07, "duration": 4.07 }, { "text": "may use in order to skip the\nrendering for odd numbers", "start": 3223.14, "duration": 3.33 }, { "text": "and only render on even numbers?", "start": 3226.47, "duration": 1.59 }, { "text": "AUDIENCE: You could use\n[INAUDIBLE] before an update.", "start": 3234.78, "duration": 3.85 }, { "text": "SPEAKER 1: Yeah, so we have this\nthing called shouldComponentUpdate,", "start": 3238.63, "duration": 3.38 }, { "text": "which takes the next props.", "start": 3242.01, "duration": 1.17 }, { "text": "And so we have the ability\nto look at the next props", "start": 3243.18, "duration": 2.19 }, { "text": "and decide whether or\nnot we want to update,", "start": 3245.37, "duration": 2.77 }, { "text": "and if we return false from this\nfunction, the update cycle is over.", "start": 3248.14, "duration": 3.74 }, { "text": "It won't render.", "start": 3251.88, "duration": 2.2 }, { "text": "And so if in this function we wanted\nto check if that count were odd", "start": 3254.08, "duration": 6.26 }, { "text": "we can abort early.", "start": 3260.34, "duration": 2.05 }, { "text": "And so let's actually do that.", "start": 3262.39, "duration": 1.61 }, { "text": "So let's have this method\ncalled shouldComponentUpdate", "start": 3264.0, "duration": 5.784 }, { "text": "and first let's just return false.", "start": 3269.784, "duration": 1.416 }, { "text": "Now what's going to happen here?", "start": 3273.857, "duration": 1.333 }, { "text": "Nothing.", "start": 3277.91, "duration": 0.88 }, { "text": "We can see that it's receiving stuff.", "start": 3278.79, "duration": 1.74 }, { "text": "So see down here in the logs\nwe're receiving this thing called", "start": 3288.46, "duration": 4.94 }, { "text": "updating every second.", "start": 3293.4, "duration": 2.83 }, { "text": "But the UI is just stuck\nat 0, it never updates", "start": 3296.23, "duration": 2.592 }, { "text": "and that's because we're saying\nhey, should the component update?", "start": 3298.822, "duration": 2.708 }, { "text": "No we shouldn't.", "start": 3301.53, "duration": 1.59 }, { "text": "And so React is saying,\nOK, then we're not", "start": 3303.12, "duration": 1.98 }, { "text": "going to call this render method\nand nothing changes over here.", "start": 3305.1, "duration": 3.92 }, { "text": "And so how may we change this is such\nthat it only counts the even numbers?", "start": 3309.02, "duration": 5.65 }, { "text": "Well we can say return, check to see if\nthe next Props.count is divisible by 2.", "start": 3319.74, "duration": 16.17 }, { "text": "And, if it returns true, that\nmeans we're an odd number.", "start": 3335.91, "duration": 11.81 }, { "text": "But what we want is whether\nwe're an even number.", "start": 3347.72, "duration": 2.89 }, { "text": "And so we can actually just say,\noh, return the inverse of this.", "start": 3350.61, "duration": 3.104 }, { "text": "And now it's only going to\ncount those even numbers.", "start": 3353.714, "duration": 2.166 }, { "text": "Does that make sense?", "start": 3361.456, "duration": 0.874 }, { "text": "And so say we wanted to do\nsomething like send a request", "start": 3366.4, "duration": 4.075 }, { "text": "or server every single\ntime this UI updated,", "start": 3370.475, "duration": 5.675 }, { "text": "we wouldn't really want to\ndo that in the render cycle,", "start": 3376.15, "duration": 2.31 }, { "text": "we wouldn't really want to do\nit in shouldComponentUpdate.", "start": 3378.46, "duration": 4.6 }, { "text": "We can actually do that\nin ComponentDidUpdate.", "start": 3383.06, "duration": 2.198 }, { "text": "And so we can actually do for\nnow let's just console.log.", "start": 3388.32, "duration": 3.18 }, { "text": "We can say, hey, we updated it\nand we created a new number.", "start": 3397.13, "duration": 4.63 }, { "text": "And as you see, it's only getting\ncalled for those even numbers.", "start": 3401.76, "duration": 4.4 }, { "text": "Because first shouldComponentUpdate\ngets called and then", "start": 3406.16, "duration": 3.81 }, { "text": "it renders and then\nComponentDidUpdate gets called.", "start": 3409.97, "duration": 3.05 }, { "text": "And, since we exit early\non all odd numbers,", "start": 3413.02, "duration": 3.1 }, { "text": "only the even numbers will reach the\nrender cycle and ComponentDidUpdate.", "start": 3416.12, "duration": 4.29 }, { "text": "And so what you see logged\nare only those even numbers.", "start": 3420.41, "duration": 2.49 }, { "text": "So does this concept of updating\nand methods that automatically", "start": 3430.21, "duration": 5.61 }, { "text": "get invoked makes sense to everyone?", "start": 3435.82, "duration": 3.324 }, { "text": "AUDIENCE: Could you only\ndo this kind of stuff", "start": 3439.144, "duration": 2.39 }, { "text": "with [INAUDIBLE] and not\nthe purely functional ones?", "start": 3441.534, "duration": 2.56 }, { "text": "SPEAKER 1: Yeah.", "start": 3444.094, "duration": 0.666 }, { "text": "So the question was, do these\nmethods exist on the functional ones?", "start": 3444.76, "duration": 3.93 }, { "text": "And they don't because, say\nthis functional components", "start": 3448.69, "duration": 4.2 }, { "text": "are literally just functions.", "start": 3452.89, "duration": 2.37 }, { "text": "They take props and they return a node.", "start": 3455.26, "duration": 2.07 }, { "text": "And so since they're functions,\nall they do are get invoked.", "start": 3457.33, "duration": 3.24 }, { "text": "You pass them props and\nthen you get a node back.", "start": 3460.57, "duration": 2.452 }, { "text": "And in order to have\nthese methods you actually", "start": 3463.022, "duration": 1.958 }, { "text": "have to have this\nconcept of an instance.", "start": 3464.98, "duration": 5.67 }, { "text": "And so React components have\ninstances, and these instances", "start": 3470.65, "duration": 3.15 }, { "text": "are tracked throughout their lifecycle.", "start": 3473.8, "duration": 1.92 }, { "text": "And since these are class\ninstances React well", "start": 3475.72, "duration": 4.59 }, { "text": "go ahead and invoke\nthose methods for you.", "start": 3480.31, "duration": 3.54 }, { "text": "And so the stateless\nfunctional components", "start": 3483.85, "duration": 2.254 }, { "text": "are just functions that\nget invoked and do not", "start": 3486.104, "duration": 1.916 }, { "text": "have these class methods, whereas\nthe React components not only have", "start": 3488.02, "duration": 3.96 }, { "text": "these methods but the methods also\nget invoked automatically for you.", "start": 3491.98, "duration": 3.14 }, { "text": "But yeah great question.", "start": 3495.12, "duration": 1.0 }, { "text": "So that's the update cycle.", "start": 3500.36, "duration": 2.43 }, { "text": "And then lastly we have what's\ncalled the unmount cycle.", "start": 3502.79, "duration": 3.566 }, { "text": "There's only one thing that\nhappens in the unmount cycle.", "start": 3506.356, "duration": 2.374 }, { "text": "You have componentWillUnmount which\ngives you a chance to clean up.", "start": 3508.73, "duration": 4.42 }, { "text": "What do I mean by clean up?", "start": 3513.15, "duration": 1.13 }, { "text": "Well, you can remove any event listeners\nif you had them in React, mostly Web.", "start": 3514.28, "duration": 5.91 }, { "text": "You can invalidate any network\nrequests that you have out.", "start": 3520.19, "duration": 2.67 }, { "text": "Or lastly you should clear\nany timeouts or intervals.", "start": 3522.86, "duration": 3.0 }, { "text": "And so there's actually some bugs that\nyou can create if you're not careful.", "start": 3525.86, "duration": 6.1 }, { "text": "So let's actually revert\nthis back to being", "start": 3531.96, "duration": 8.86 }, { "text": "just an app that shows that count.", "start": 3540.82, "duration": 6.43 }, { "text": "So now we have again that\nnumber being incremented.", "start": 3555.81, "duration": 3.61 }, { "text": "And this class called app keeps track\nof its own number and is updating.", "start": 3567.75, "duration": 6.52 }, { "text": "But say, let's actually\ncall this counter", "start": 3574.27, "duration": 5.03 }, { "text": "and create this thing called app.", "start": 3579.3, "duration": 2.1 }, { "text": "And in this, let's actually\njust show the component.", "start": 3590.59, "duration": 2.752 }, { "text": "So now we have basically\nthe same exact thing.", "start": 3615.49, "duration": 2.03 }, { "text": "So we have this thing\ncalled class app which", "start": 3617.52, "duration": 2.87 }, { "text": "just returns an instance of counter.", "start": 3620.39, "duration": 2.54 }, { "text": "And counter is what we've\nbeen working on thus far.", "start": 3622.93, "duration": 2.44 }, { "text": "It creates an interval and we'll\nkeep track of that interval", "start": 3625.37, "duration": 2.64 }, { "text": "and keep counting up.", "start": 3628.01, "duration": 2.21 }, { "text": "But say we actually have this button\nthat will toggle whether or not", "start": 3630.22, "duration": 3.79 }, { "text": "this counter is shown.", "start": 3634.01, "duration": 1.14 }, { "text": "And so let's have this\nthing called state", "start": 3635.15, "duration": 13.81 }, { "text": "and let's have this\nflag called show counter", "start": 3648.96, "duration": 3.037 }, { "text": "and let's initialize it to true.", "start": 3651.997, "duration": 1.333 }, { "text": "And then down here we\ncan do something like,", "start": 3657.27, "duration": 4.8 }, { "text": "if you should show the\ncounter then return this.", "start": 3662.07, "duration": 9.25 }, { "text": "Else let's just return an empty view.", "start": 3676.96, "duration": 2.356 }, { "text": "And so now this is basically\nthe exact same thing", "start": 3687.75, "duration": 2.76 }, { "text": "because we have no way to change whether\nor not we want to show the counter.", "start": 3690.51, "duration": 4.65 }, { "text": "But say we did, so let's\ncreate toggleCounter.", "start": 3695.16, "duration": 2.61 }, { "text": "So show counter.", "start": 3708.04, "duration": 2.17 }, { "text": "It should be the opposite of what\nthe previous state show counter was.", "start": 3710.21, "duration": 3.07 }, { "text": "And let's also have a button\nthat will toggle this counter.", "start": 3716.14, "duration": 17.34 }, { "text": "And so now we have a button\nwhere if we click it,", "start": 3743.37, "duration": 3.582 }, { "text": "we can just toggle whether\nor not that counter shows.", "start": 3746.952, "duration": 2.208 }, { "text": "And there's actually\na pretty bad bug here.", "start": 3761.53, "duration": 2.726 }, { "text": "And that is that when we\ntoggled this counter--", "start": 3767.61, "duration": 3.512 }, { "text": "oops, we should return this--", "start": 3775.59, "duration": 3.68 }, { "text": "when you toggle that counter we\nnever actually tell this component,", "start": 3779.27, "duration": 6.18 }, { "text": "hey, stop incrementing.", "start": 3785.45, "duration": 3.372 }, { "text": "It's actually going\nto keep incrementing.", "start": 3788.822, "duration": 1.708 }, { "text": "And so we can show that if\nwe have here, if we console", "start": 3790.53, "duration": 2.94 }, { "text": "that log we see that it's\nincrementing, and if we toggle it,", "start": 3793.47, "duration": 10.66 }, { "text": "it's still logging.", "start": 3804.13, "duration": 1.47 }, { "text": "So counters now gone.", "start": 3805.6, "duration": 2.29 }, { "text": "So there's no counter here, it's\ngone but it keeps saying increment.", "start": 3807.89, "duration": 6.32 }, { "text": "And, if we show it again, now look\nat how fast it's incrementing.", "start": 3814.21, "duration": 10.15 }, { "text": "It's two every second because\nthe first counter created", "start": 3824.36, "duration": 5.17 }, { "text": "an incrementing function every 1,000\nmilliseconds and the second time", "start": 3829.53, "duration": 4.2 }, { "text": "we created one it also did.", "start": 3833.73, "duration": 2.94 }, { "text": "And so what if we did this again.", "start": 3836.67, "duration": 1.59 }, { "text": "Toggle and then toggle again.", "start": 3838.26, "duration": 3.42 }, { "text": "Now it's going up three every second.", "start": 3841.68, "duration": 1.9 }, { "text": "And if we did this a bunch of times,\nyou see it's going pretty crazy now.", "start": 3843.58, "duration": 4.67 }, { "text": "It's like five every second.", "start": 3848.25, "duration": 2.072 }, { "text": "And so that's not good.", "start": 3850.322, "duration": 0.958 }, { "text": "That's definitely not good at all.", "start": 3851.28, "duration": 1.99 }, { "text": "Say you had a super complex app\nand hundreds of timers getting set", "start": 3853.27, "duration": 4.55 }, { "text": "and you kept toggling them.", "start": 3857.82, "duration": 1.53 }, { "text": "Eventually you're going to have a\nbig problem where a bunch of things", "start": 3859.35, "duration": 3.03 }, { "text": "are getting called and nothing\nshould be getting called.", "start": 3862.38, "duration": 2.91 }, { "text": "And so that's like a memory leak and\nit could cause your app to crash.", "start": 3865.29, "duration": 4.35 }, { "text": "And so how might we\ngo about solving this?", "start": 3869.64, "duration": 3.61 }, { "text": "So, as we alluded to, there's\nthis thing called an unmount cycle", "start": 3873.25, "duration": 3.11 }, { "text": "where we have a chance to clean up.", "start": 3876.36, "duration": 2.01 }, { "text": "We can get rid of any\ntimers that we've created.", "start": 3878.37, "duration": 5.16 }, { "text": "And so you can actually do that here.", "start": 3883.53, "duration": 3.0 }, { "text": "And so, rather than just\nsetting the interval,", "start": 3886.53, "duration": 2.22 }, { "text": "we should actually track that\nfunction that we created.", "start": 3888.75, "duration": 3.06 }, { "text": "And so you can do something like\nthis.Interval equals setInterval.", "start": 3891.81, "duration": 6.18 }, { "text": "And so setInterval actually returns like\nan ID of the interval that you created.", "start": 3897.99, "duration": 5.63 }, { "text": "And so now we're tracking exactly\nthe interval that we created.", "start": 3903.62, "duration": 4.94 }, { "text": "And we can go ahead and right\nbefore this component dies,", "start": 3908.56, "duration": 4.07 }, { "text": "so component will unmount, we\nshould actually clean that up.", "start": 3912.63, "duration": 6.84 }, { "text": "We can actually do clearInterval\nand we pass in that ID.", "start": 3919.47, "duration": 5.445 }, { "text": "And so now you can see--", "start": 3929.36, "duration": 5.07 }, { "text": "oops, things did not get updated.", "start": 3934.43, "duration": 3.375 }, { "text": "Let's actually copy all\nthis and refresh the page.", "start": 3937.805, "duration": 7.617 }, { "text": "And so when this loads,\nyou'll see that it will still", "start": 3952.06, "duration": 4.5 }, { "text": "log when it's getting shown.", "start": 3956.56, "duration": 3.49 }, { "text": "And so as this number is incrementing we\ncan see it saying, oh I'm incrementing.", "start": 3960.05, "duration": 5.42 }, { "text": "But when we toggle it that\nnumber no longer goes up.", "start": 3965.47, "duration": 7.41 }, { "text": "And then, if we toggle it again,\nit's going to continue incrementing.", "start": 3972.88, "duration": 4.33 }, { "text": "And, if we cancel that,\nthat incrementing function", "start": 3977.21, "duration": 3.74 }, { "text": "is no longer getting called.", "start": 3980.95, "duration": 1.76 }, { "text": "And that's because, over here,\nwhen the component was created,", "start": 3982.71, "duration": 5.3 }, { "text": "so when the component mounts, we set\nthe interval for this button increment", "start": 3988.01, "duration": 5.2 }, { "text": "to happen every 1,000 seconds.", "start": 3993.21, "duration": 2.75 }, { "text": "But then later when we are no\nlonger returning that counter so", "start": 3995.96, "duration": 8.72 }, { "text": "the instance gets unmounted.", "start": 4004.68, "duration": 1.5 }, { "text": "It's no longer part of the React tree.", "start": 4006.18, "duration": 1.67 }, { "text": "Before it gets unmounted, we say\nclear that interval and it clears.", "start": 4013.89, "duration": 7.21 }, { "text": "And then when it remounts\nit creates a new interval.", "start": 4021.1, "duration": 3.38 }, { "text": "And when it gets unmounted again it\ngoes ahead and clears that interval.", "start": 4024.48, "duration": 4.47 }, { "text": "And so now we no longer\nhave any memory leaks.", "start": 4028.95, "duration": 1.98 }, { "text": "So does that whole lifecycle\nprocess make sense to people?", "start": 4034.76, "duration": 3.59 }, { "text": "Do you have any questions\nabout these methods", "start": 4038.35, "duration": 2.94 }, { "text": "or why we might want to use them?", "start": 4041.29, "duration": 5.78 }, { "text": "Cool.", "start": 4047.07, "duration": 0.5 }, { "text": "Let's take a quick five\nminute break and then", "start": 4047.57, "duration": 2.36 }, { "text": "after this, we'll go ahead and\nstart writing some React Native.", "start": 4049.93, "duration": 2.88 }, { "text": "Hello and welcome back.", "start": 4052.81, "duration": 1.38 }, { "text": "So if you are following Snack some\npeople had some questions about style", "start": 4054.19, "duration": 5.16 }, { "text": "and whether or not people\nin industry chose to bundle", "start": 4059.35, "duration": 2.79 }, { "text": "this style with each component.", "start": 4062.14, "duration": 2.46 }, { "text": "And what I tend to\nsee mostly in industry", "start": 4064.6, "duration": 2.67 }, { "text": "is that people do indeed tend to\ninclude the styles with each component", "start": 4067.27, "duration": 4.5 }, { "text": "because part of React's\ncomponent mindset", "start": 4071.77, "duration": 2.64 }, { "text": "is that each component\nshould be a standalone thing", "start": 4074.41, "duration": 2.55 }, { "text": "and it should include with it any event\nhandlers or JavaScript and any style", "start": 4076.96, "duration": 5.25 }, { "text": "as well.", "start": 4082.21, "duration": 1.25 }, { "text": "But there are many compelling\nreasons to actually remove", "start": 4083.46, "duration": 3.25 }, { "text": "the style outside of that component.", "start": 4086.71, "duration": 1.95 }, { "text": "And one big compelling reason would\nbe something like color themes.", "start": 4088.66, "duration": 3.51 }, { "text": "So say you had some color theme where\nyou really liked crimson in your app", "start": 4092.17, "duration": 5.91 }, { "text": "and everywhere you were using crimson.", "start": 4098.08, "duration": 2.37 }, { "text": "And then say suddenly you wanted\nto use something like blue.", "start": 4100.45, "duration": 5.4 }, { "text": "If you had everything\nhard coded to crimson", "start": 4105.85, "duration": 2.339 }, { "text": "it might be a bit of a pain to go\nthrough and find every instance", "start": 4108.189, "duration": 3.21 }, { "text": "of that crimson and change it to blue.", "start": 4111.399, "duration": 1.991 }, { "text": "But say you actually abstracted\nout some value like primary color", "start": 4113.39, "duration": 5.299 }, { "text": "and instead used that in\nall of your components.", "start": 4118.689, "duration": 2.669 }, { "text": "Then if you wanted to change the\nprimary color from crimson to blue", "start": 4121.358, "duration": 2.791 }, { "text": "it would be very easy.", "start": 4124.149, "duration": 0.916 }, { "text": "You would just do it in\none file and be done.", "start": 4125.065, "duration": 2.265 }, { "text": "So there are definitely compelling\nreasons to remove the styles out", "start": 4127.33, "duration": 3.87 }, { "text": "of a particular component.", "start": 4131.2, "duration": 2.61 }, { "text": "And since React Native uses\nJavaScript's objects to style components", "start": 4133.81, "duration": 4.169 }, { "text": "it allows you to do stuff like\ncreate variables and import them in.", "start": 4137.979, "duration": 4.541 }, { "text": "And so are there reasons\nto take the style sheets", "start": 4142.52, "duration": 2.75 }, { "text": "outside of any particular component.", "start": 4145.27, "duration": 2.549 }, { "text": "But generally if you\nhave a choice it's easier", "start": 4147.819, "duration": 2.101 }, { "text": "to just leave it in there\nfor organization sake.", "start": 4149.92, "duration": 4.05 }, { "text": "Cool.", "start": 4153.97, "duration": 1.36 }, { "text": "And so up until now we've been writing\na lot of React Native in this browser", "start": 4155.33, "duration": 6.59 }, { "text": "environment.", "start": 4161.92, "duration": 0.78 }, { "text": "And so let's actually figure out\nand talk about how we actually", "start": 4162.7, "duration": 3.6 }, { "text": "write React Native if you were to\nstart your own personal project", "start": 4166.3, "duration": 4.019 }, { "text": "or assignment for the course.", "start": 4170.319, "duration": 3.16 }, { "text": "So I've been talking about this\ncompany called Expo quite a bit.", "start": 4173.479, "duration": 3.21 }, { "text": "And what they do is they create\na very fast way to build an app.", "start": 4176.689, "duration": 3.46 }, { "text": "They provide a lot of\ntooling around React", "start": 4180.149, "duration": 1.75 }, { "text": "Native and the React native community.", "start": 4181.899, "duration": 3.302 }, { "text": "So there are a suite of tools to\naccelerate the React Native development", "start": 4185.201, "duration": 2.999 }, { "text": "process.", "start": 4188.2, "duration": 1.047 }, { "text": "And so one that we've\nbeen using quite a lot", "start": 4189.247, "duration": 1.833 }, { "text": "is this thing called Snack,\nwhich is this website here.", "start": 4191.08, "duration": 3.81 }, { "text": "We've used it this course,\na lot this lecture and last.", "start": 4194.89, "duration": 5.58 }, { "text": "And what that allows us to do is\nrun React Native in the browser.", "start": 4200.47, "duration": 3.06 }, { "text": "So it automatically\nbundles any libraries", "start": 4203.53, "duration": 2.49 }, { "text": "that we need like React\nNative, React, and then", "start": 4206.02, "duration": 3.75 }, { "text": "it also runs on a phone\nembedded into the browser.", "start": 4209.77, "duration": 2.55 }, { "text": "They also give us a tool\ncalled the XDE which", "start": 4216.1, "duration": 3.03 }, { "text": "is a GUI, graphical user interface,\nto serve share and publish any Expo", "start": 4219.13, "duration": 4.8 }, { "text": "projects that you have.", "start": 4223.93, "duration": 2.31 }, { "text": "They also give us a command\nline interface to do", "start": 4226.24, "duration": 2.04 }, { "text": "the same things, to be a command line.", "start": 4228.28, "duration": 3.52 }, { "text": "They give us the Expo client which,\nif you saw my email from earlier,", "start": 4231.8, "duration": 4.25 }, { "text": "is an iPhone app that you can\ninstall and actually run stuff", "start": 4236.05, "duration": 4.29 }, { "text": "that you write in Snack or\nstuff that you write and manage", "start": 4240.34, "duration": 4.29 }, { "text": "by the XDE or the CLI to actually\nrun it on your phone while developing", "start": 4244.63, "duration": 5.31 }, { "text": "and before you publish.", "start": 4249.94, "duration": 2.61 }, { "text": "And lastly they give us an\nSDK, software development kit,", "start": 4252.55, "duration": 3.09 }, { "text": "which bundles and exposes\ncross-platform libraries and APIs.", "start": 4255.64, "duration": 3.93 }, { "text": "And so included in that are maps, stuff\nfor like icons and images and SVGs", "start": 4259.57, "duration": 8.01 }, { "text": "and maybe animations.", "start": 4267.58, "duration": 1.56 }, { "text": "They also give us handy constants.", "start": 4269.14, "duration": 1.56 }, { "text": "So the Expo that we imported into\nthis app is actually the SDK,", "start": 4270.7, "duration": 5.94 }, { "text": "and it gave us a constant\nlike, we didn't use it here", "start": 4276.64, "duration": 4.32 }, { "text": "but, the height of the status bar.", "start": 4280.96, "duration": 3.72 }, { "text": "And so they give us a\nlot of helpful things", "start": 4284.68, "duration": 2.49 }, { "text": "to really get our developing\nprocess moving forward.", "start": 4287.17, "duration": 3.86 }, { "text": "And so let's take a look at\nthis thing called the XDE.", "start": 4291.03, "duration": 3.76 }, { "text": "So I have it here.", "start": 4294.79, "duration": 1.44 }, { "text": "It's a little bit small but\nbasically what this is is it", "start": 4300.07, "duration": 3.9 }, { "text": "allows us to create and run projects.", "start": 4303.97, "duration": 2.38 }, { "text": "And so I actually already created\nthis project before the class,", "start": 4306.35, "duration": 5.04 }, { "text": "but when you do create your project\nit allows you to choose a template", "start": 4311.39, "duration": 4.16 }, { "text": "and basically create the directory\nand all of its dependencies", "start": 4315.55, "duration": 5.01 }, { "text": "on your computer.", "start": 4320.56, "duration": 1.23 }, { "text": "And so, if you started with the blank\nlike I did and created my new project,", "start": 4321.79, "duration": 4.53 }, { "text": "it gives you a chance to\nsave it wherever you'd like.", "start": 4326.32, "duration": 7.2 }, { "text": "But I already went and did that\nprocess and created my new project.", "start": 4333.52, "duration": 2.97 }, { "text": "And so, if you want to\nfollow along at home,", "start": 4336.49, "duration": 2.07 }, { "text": "you just do create new project,\nblank, save it wherever you like", "start": 4338.56, "duration": 3.96 }, { "text": "and then you end up right here.", "start": 4342.52, "duration": 3.76 }, { "text": "And basically what this is, is\nthis is running a bunch of things", "start": 4346.28, "duration": 2.81 }, { "text": "behind the scenes including React Native\npackager which handles the bundling", "start": 4349.09, "duration": 3.81 }, { "text": "and whatnot of your app and any\nlogs if you want to console log,", "start": 4352.9, "duration": 5.37 }, { "text": "will also appear in this XDE.", "start": 4358.27, "duration": 2.31 }, { "text": "And so what it's doing is it's\nactually looking in that directory", "start": 4360.58, "duration": 2.97 }, { "text": "that it created on your computer.", "start": 4363.55, "duration": 2.278 }, { "text": "And so if you create it and then CD into\nthe correct directory on your computer", "start": 4374.11, "duration": 3.78 }, { "text": "you end up right here.", "start": 4377.89, "duration": 1.08 }, { "text": "And so when you create\na new file you see", "start": 4381.942, "duration": 1.708 }, { "text": "this thing called app.json\nwhich is basically", "start": 4383.65, "duration": 5.45 }, { "text": "a bunch of metadata that's\ncreated about your project and is", "start": 4389.1, "duration": 2.5 }, { "text": "how Expo tracks your app.", "start": 4391.6, "duration": 3.21 }, { "text": "And then you see this\nthing called app.js.", "start": 4394.81, "duration": 1.75 }, { "text": "And that is basically the bare bones\nReact Native app that you start with.", "start": 4400.48, "duration": 6.92 }, { "text": "And when you run it, if you open it\nup on a device, you see it run here.", "start": 4407.4, "duration": 8.106 }, { "text": "And so this says, open up app.js\nto start working on your app.", "start": 4418.86, "duration": 3.82 }, { "text": "And if I were to change the text here\nand save, we can see it re-bundle.", "start": 4422.68, "duration": 12.88 }, { "text": "It's currently building\nthat new JavaScript bundle", "start": 4435.56, "duration": 2.13 }, { "text": "and then you see here, live on the\nphone, that you did update that text.", "start": 4437.69, "duration": 5.58 }, { "text": "And so, if you want to start\nworking on your project earlier", "start": 4445.89, "duration": 4.31 }, { "text": "or work on any personal projects of\nyour own, you're welcome to use Expo.", "start": 4450.2, "duration": 3.48 }, { "text": "I actually encourage you\nto use Expo because they", "start": 4453.68, "duration": 2.79 }, { "text": "take a lot of annoying\nand difficult parts", "start": 4456.47, "duration": 2.79 }, { "text": "about creating projects\nand publishing them", "start": 4459.26, "duration": 1.847 }, { "text": "and do all the hard work for you\nso that all you have to worry about", "start": 4461.107, "duration": 2.833 }, { "text": "is writing the JavaScript here.", "start": 4463.94, "duration": 1.291 }, { "text": "Cool.", "start": 4470.33, "duration": 0.99 }, { "text": "And so we've been doing this thing a\nlot in this lecture and previous ones", "start": 4471.32, "duration": 4.089 }, { "text": "called importing and exporting.", "start": 4475.409, "duration": 1.291 }, { "text": "And we haven't actually\ntalked about what is.", "start": 4476.7, "duration": 2.65 }, { "text": "And so we have talked\na lot about components", "start": 4479.35, "duration": 3.1 }, { "text": "and how we should break up components.", "start": 4482.45, "duration": 2.372 }, { "text": "And it's actually also highly encouraged\nthat you break up the components", "start": 4484.822, "duration": 3.968 }, { "text": "into their own files.", "start": 4488.79, "duration": 1.49 }, { "text": "And so we've seen that components\nare great for simplifying your code", "start": 4490.28, "duration": 3.234 }, { "text": "and we can actually split\nthem into their own files.", "start": 4493.514, "duration": 2.166 }, { "text": "And then what this allows us to do is\nhelp organize the project even more.", "start": 4495.68, "duration": 5.19 }, { "text": "Rather than having everything all\nin one big super long file, which", "start": 4500.87, "duration": 3.75 }, { "text": "might get very unwieldy, we\ncan actually organize things", "start": 4504.62, "duration": 2.46 }, { "text": "into their own separate files\nor even separate folders", "start": 4507.08, "duration": 3.57 }, { "text": "so it's easy to find whatever\ncode you need to update.", "start": 4510.65, "duration": 3.89 }, { "text": "Then what we do is from the\nfile that we create we actually", "start": 4514.54, "duration": 3.37 }, { "text": "export the component that we want.", "start": 4517.91, "duration": 2.89 }, { "text": "And then we can then import it in to\nanother file if we want to use it.", "start": 4520.8, "duration": 4.82 }, { "text": "And there are a couple different\nways to import and export.", "start": 4527.732, "duration": 2.458 }, { "text": "There's this thing\ncalled a default export,", "start": 4530.19, "duration": 2.12 }, { "text": "and there's this thing called\na named import and export.", "start": 4532.31, "duration": 2.88 }, { "text": "And so what I was typing a\nlot earlier is you see me type", "start": 4535.19, "duration": 4.57 }, { "text": "this thing called export\ndefault class app.", "start": 4539.76, "duration": 2.62 }, { "text": "And that's me saying, hey\nfrom this file here I actually", "start": 4542.38, "duration": 3.43 }, { "text": "want to export this app.", "start": 4545.81, "duration": 1.56 }, { "text": "And it should be the default export.", "start": 4547.37, "duration": 2.99 }, { "text": "And so let's take a look\nat what that looks here.", "start": 4550.36, "duration": 2.23 }, { "text": "And so in app.js in a brand\nnew directory I went and--", "start": 4552.59, "duration": 3.99 }, { "text": "the boiler plate code says\nexport this default class app.", "start": 4559.19, "duration": 3.0 }, { "text": "But say I want to in a new file.", "start": 4562.19, "duration": 2.05 }, { "text": "So let me create a new file called\nlet's just call it count.js.", "start": 4564.24, "duration": 4.09 }, { "text": "From here let's first import from\nReact, which is something that we", "start": 4570.95, "duration": 6.72 }, { "text": "have to do in every single file.", "start": 4577.67, "duration": 2.28 }, { "text": "And let's create this\ncomponent called Count.", "start": 4579.95, "duration": 2.91 }, { "text": "So let's do const Count\nequals props and then", "start": 4582.86, "duration": 4.2 }, { "text": "let's return this thing called a view.", "start": 4587.06, "duration": 2.79 }, { "text": "And then have props.count show.", "start": 4589.85, "duration": 5.214 }, { "text": "Actually, let's have this be text.", "start": 4595.064, "duration": 1.416 }, { "text": "So this won't actually work because\nwe're using this component called", "start": 4603.81, "duration": 4.53 }, { "text": "Text here which doesn't exist.", "start": 4608.34, "duration": 3.15 }, { "text": "So Text here, capital T\ncapital Text, is a component", "start": 4611.49, "duration": 4.68 }, { "text": "but this file has no idea what\nText is, what capital Text is.", "start": 4616.17, "duration": 5.33 }, { "text": "It's a component.", "start": 4621.5, "duration": 1.64 }, { "text": "We know it's a component because\nit's in JSX and capitalized", "start": 4623.14, "duration": 3.17 }, { "text": "but nowhere in this file do we\ndeclare this thing called Text.", "start": 4626.31, "duration": 3.19 }, { "text": "And so we actually have to import it\nfrom the library called React Native.", "start": 4629.5, "duration": 4.49 }, { "text": "And so you've seen me\nbefore in the example", "start": 4645.2, "duration": 3.33 }, { "text": "files do this thing called\nimport Text from React Native.", "start": 4648.53, "duration": 2.71 }, { "text": "And what that the actually does is it\ngoes into that React Native API code", "start": 4651.24, "duration": 5.0 }, { "text": "and gives you access to the variable\nthat they're exporting called Text.", "start": 4656.24, "duration": 3.79 }, { "text": "And then when we go\nahead and use it here", "start": 4660.03, "duration": 3.95 }, { "text": "the JavaScript knows that\nthis variable context exists.", "start": 4663.98, "duration": 3.25 }, { "text": "And so how are we going to get this\ncomponent called Count into our app?", "start": 4667.23, "duration": 7.43 }, { "text": "So say I wanted to hear a display\ncount and passed an account of 0.", "start": 4677.19, "duration": 8.61 }, { "text": "So if I save this and\ntry to run it, it's", "start": 4689.01, "duration": 2.37 }, { "text": "going to error because this\nvariable called Count doesn't exist,", "start": 4691.38, "duration": 5.29 }, { "text": "which makes sense right?", "start": 4696.67, "duration": 2.06 }, { "text": "In this app.js file we haven't\ncreated a variable called Count.", "start": 4698.73, "duration": 4.59 }, { "text": "We actually created it in a\nseparate file called count.js.", "start": 4703.32, "duration": 5.79 }, { "text": "And so as I alluded to, you can\nactually export a component from a file", "start": 4709.11, "duration": 3.6 }, { "text": "and import it into a new file.", "start": 4712.71, "duration": 2.03 }, { "text": "And so we can actually, over here,\njust use this term called export.", "start": 4714.74, "duration": 4.285 }, { "text": "And if we go ahead and\nsave this then now we", "start": 4723.05, "duration": 2.62 }, { "text": "can try to import this\nthing called Count from--", "start": 4725.67, "duration": 5.115 }, { "text": "where is it?", "start": 4734.19, "duration": 0.636 }, { "text": "Well, it's in the current\ndirectory, so ./Count.js.", "start": 4734.826, "duration": 2.124 }, { "text": "And so now if we save that we\ncan see that it compiles and we", "start": 4741.21, "duration": 5.85 }, { "text": "see that 0 show up.", "start": 4747.06, "duration": 3.39 }, { "text": "And so let's make it a little bit\nbigger so that's easier to see.", "start": 4750.45, "duration": 3.74 }, { "text": "And so how are we going to do that?", "start": 4754.19, "duration": 1.49 }, { "text": "Let's create this constant called\nstyles and let's have stylesheet.create.", "start": 4755.68, "duration": 5.2 }, { "text": "And let's just have a font size of 72.", "start": 4765.51, "duration": 5.24 }, { "text": "And then here let's just do style is\nthat Styles object that we created.", "start": 4775.2, "duration": 9.931 }, { "text": "And if we save this and run\nit what's going to happen?", "start": 4785.131, "duration": 2.249 }, { "text": "It's going to error.", "start": 4787.38, "duration": 1.832 }, { "text": "And why is it erroring?", "start": 4789.212, "duration": 0.958 }, { "text": "Well it says cannot find variable called\nstylesheet, which makes sense right?", "start": 4790.17, "duration": 4.04 }, { "text": "In this file here nowhere is there\na variable called stylesheet.", "start": 4794.21, "duration": 5.027 }, { "text": "And so we actually also have to\nimport that from React Native.", "start": 4799.237, "duration": 2.583 }, { "text": "And so if we run that and\nsave it, lo and behold,", "start": 4806.19, "duration": 2.53 }, { "text": "it rhymes and the style is applied.", "start": 4808.72, "duration": 2.29 }, { "text": "We see a big number 0 there.", "start": 4811.01, "duration": 3.41 }, { "text": "And so that is called a named export.", "start": 4814.42, "duration": 2.13 }, { "text": "We exported a named\nvariable called Count.", "start": 4816.55, "duration": 4.56 }, { "text": "And then in here in brackets we say\nimport all of the variables named Count", "start": 4821.11, "duration": 6.29 }, { "text": "that we exported from ./Count.js.", "start": 4827.4, "duration": 4.48 }, { "text": "And so that is an example\nof a named export.", "start": 4831.88, "duration": 1.89 }, { "text": "And say we wanted to export another\nnamed variable we can do export const--", "start": 4833.77, "duration": 8.048 }, { "text": "let's just call it num, and\nlet's have it be the number 50.", "start": 4844.954, "duration": 6.936 }, { "text": "And we can, over here, do import\nCount and also num count.js.", "start": 4851.89, "duration": 5.91 }, { "text": "And then rather than passing\nin 0 we can just pass in num.", "start": 4857.8, "duration": 4.58 }, { "text": "And as we see over here\nnow it's the number 50.", "start": 4862.38, "duration": 4.13 }, { "text": "Why is that?", "start": 4866.51, "duration": 0.69 }, { "text": "Well in Count.js we\nexported two variables.", "start": 4867.2, "duration": 5.0 }, { "text": "One we called Count,\nso capital Count here.", "start": 4872.2, "duration": 3.75 }, { "text": "We have a const called Count\nand we're exporting that.", "start": 4875.95, "duration": 2.65 }, { "text": "And so it's a named variable\nthat's getting exported.", "start": 4878.6, "duration": 3.09 }, { "text": "Over here we have a constant called\nnum, and we're exporting that,", "start": 4881.69, "duration": 3.42 }, { "text": "so there's also a named variable\ncalled num that's getting exported.", "start": 4885.11, "duration": 3.3 }, { "text": "Then over here in our\napp file we actually", "start": 4888.41, "duration": 2.4 }, { "text": "import to this thing\ncalled Count and num.", "start": 4890.81, "duration": 2.589 }, { "text": "And so the names do\nmatter because it needs", "start": 4893.399, "duration": 1.791 }, { "text": "to know exactly what we're importing in.", "start": 4895.19, "duration": 2.884 }, { "text": "And then we go ahead and use those\nand they show up as expected.", "start": 4898.074, "duration": 2.666 }, { "text": "And so those are called named\nimports and named exports", "start": 4903.57, "duration": 3.32 }, { "text": "but there's also this thing\ncalled a default export.", "start": 4906.89, "duration": 3.0 }, { "text": "And so you see here we have an\nadditional term called export default", "start": 4909.89, "duration": 5.43 }, { "text": "this value.", "start": 4915.32, "duration": 1.51 }, { "text": "And so in our Count file\nover here let's get rid of--", "start": 4916.83, "duration": 3.55 }, { "text": "or let's not actually\nget rid of anything", "start": 4920.38, "duration": 1.75 }, { "text": "and let's actually do export\ndefault. And then just", "start": 4922.13, "duration": 5.42 }, { "text": "give it a value to export.", "start": 4927.55, "duration": 2.94 }, { "text": "So export default this\nfunction that we're creating", "start": 4930.49, "duration": 3.6 }, { "text": "and if we want to make this more\nobvious what's happening here", "start": 4934.09, "duration": 3.03 }, { "text": "we can actually do\nconst Count equals this.", "start": 4937.12, "duration": 7.26 }, { "text": "And then we can do export\ndefault Count, which", "start": 4944.38, "duration": 3.45 }, { "text": "is saying set the default\nexport of this file to be Count.", "start": 4947.83, "duration": 3.7 }, { "text": "And then how are we going to import it?", "start": 4953.705, "duration": 1.625 }, { "text": "Well, rather than\nimporting these name things", "start": 4955.33, "duration": 4.32 }, { "text": "we just say we want to import\nthis thing from Count.js.", "start": 4959.65, "duration": 5.19 }, { "text": "And since we've dropped\nthose brackets we", "start": 4964.84, "duration": 2.52 }, { "text": "say just import whatever\nis default exporting.", "start": 4967.36, "duration": 3.14 }, { "text": "And we're going to call it here Count.", "start": 4970.5, "duration": 2.62 }, { "text": "And so if you notice it works--", "start": 4973.12, "duration": 3.42 }, { "text": "oops as long as I save all\nthe files but I never--", "start": 4976.54, "duration": 9.09 }, { "text": "let's change this back to 0.", "start": 4985.63, "duration": 1.17 }, { "text": "And so now as you notice it is\nshowing that 0 value as expected.", "start": 4990.8, "duration": 3.485 }, { "text": "But let's actually call\nthis something else.", "start": 4997.29, "duration": 2.68 }, { "text": "So let's call this custom Count.", "start": 4999.97, "duration": 2.98 }, { "text": "So even though over here we're\nexporting a variable called", "start": 5009.98, "duration": 4.92 }, { "text": "Count, since we're are using\nit as a default export,", "start": 5014.9, "duration": 4.89 }, { "text": "we can import the default export in\nthis file and call it whatever you want.", "start": 5019.79, "duration": 5.54 }, { "text": "And so we can call it custom Count over\nhere and it still works as expected.", "start": 5025.33, "duration": 5.06 }, { "text": "And so the difference between named\nand default exports are that named, 1,", "start": 5030.39, "duration": 4.67 }, { "text": "you can export multiple\nof named exports and also", "start": 5035.06, "duration": 5.4 }, { "text": "import to multiple named\nexports from a different file.", "start": 5040.46, "duration": 3.96 }, { "text": "And they do have to be named exactly\nas they are in the other files.", "start": 5044.42, "duration": 5.68 }, { "text": "But when you export\nsomething default you're", "start": 5050.1, "duration": 2.15 }, { "text": "limited to only exporting\none which makes sense.", "start": 5052.25, "duration": 2.52 }, { "text": "If you exported multiple defaults you\nwouldn't know which one to import.", "start": 5054.77, "duration": 4.317 }, { "text": "And then over here, you\ncan call it whatever", "start": 5059.087, "duration": 1.833 }, { "text": "you want because it doesn't matter,\nit's just the default export.", "start": 5060.92, "duration": 5.16 }, { "text": "And so that's how you're\ngoing to go about starting", "start": 5066.08, "duration": 2.37 }, { "text": "to remove components from one long\nfile and move them into separate files.", "start": 5068.45, "duration": 7.09 }, { "text": "And so you can also actually import the\nexport default and also named exports.", "start": 5075.54, "duration": 10.37 }, { "text": "So here we are exporting a named export\nand we're also having a default export.", "start": 5090.33, "duration": 7.11 }, { "text": "And in here we're importing the default\nand we're also importing a named export", "start": 5097.44, "duration": 7.9 }, { "text": "and, as you see, that works as well.", "start": 5105.34, "duration": 2.44 }, { "text": "And so over here you see that we\nare importing React from React which", "start": 5110.72, "duration": 5.2 }, { "text": "is whatever its default export is.", "start": 5115.92, "duration": 3.57 }, { "text": "We could also import one\nof its named exports,", "start": 5119.49, "duration": 5.67 }, { "text": "and rather than extending\na React.component", "start": 5125.16, "duration": 2.84 }, { "text": "we can just extend\ncomponent here because we", "start": 5128.0, "duration": 4.3 }, { "text": "went ahead and imported the named\nexport called component from React.", "start": 5132.3, "duration": 4.74 }, { "text": "And so that will also work as well.", "start": 5137.04, "duration": 2.14 }, { "text": "So any questions on\nimporting and exporting", "start": 5143.89, "duration": 2.72 }, { "text": "and how you would go about\nbreaking up particular components", "start": 5146.61, "duration": 3.66 }, { "text": "into separate files?", "start": 5150.27, "duration": 3.04 }, { "text": "Yeah.", "start": 5153.31, "duration": 1.0 }, { "text": "AUDIENCE: You were able to access\nthe components with React.components.", "start": 5154.31, "duration": 6.5 }, { "text": "Is that because the component is\n[INAUDIBLE] named export and the value", "start": 5160.81, "duration": 6.5 }, { "text": "in the React.components?", "start": 5167.31, "duration": 2.0 }, { "text": "SPEAKER 1: Yes exactly.", "start": 5169.31, "duration": 1.0 }, { "text": "So the question was, I was\nable to access component", "start": 5170.31, "duration": 3.83 }, { "text": "as React.component and\nseparately also as component", "start": 5174.14, "duration": 7.86 }, { "text": "and that's because this thing\ncalled component is actually", "start": 5182.0, "duration": 3.69 }, { "text": "a property on this React.", "start": 5185.69, "duration": 4.56 }, { "text": "And so what React exports is\nboth a default export, which", "start": 5190.25, "duration": 4.11 }, { "text": "is this massive thing where one of\nits object keys is called a component", "start": 5194.36, "duration": 7.29 }, { "text": "and has the value of the component\nand it's also separately exporting", "start": 5201.65, "duration": 4.08 }, { "text": "a named export called component.", "start": 5205.73, "duration": 1.86 }, { "text": "And so we can go ahead and either\nimport the default export and do that", "start": 5207.59, "duration": 4.27 }, { "text": ".component or we can import the\nnamed component and just use that.", "start": 5211.86, "duration": 3.61 }, { "text": "So you're absolutely correct.", "start": 5215.47, "duration": 1.92 }, { "text": "Any other questions on\nimporting and exporting?", "start": 5221.23, "duration": 2.99 }, { "text": "Cool.", "start": 5227.93, "duration": 0.5 }, { "text": "So when we go ahead and\nimport and export components", "start": 5228.43, "duration": 4.42 }, { "text": "they can start to get more and more\ncomplex as we pass down more props.", "start": 5232.85, "duration": 5.26 }, { "text": "And so how might we go about\nkeeping track of all of these props?", "start": 5238.11, "duration": 4.22 }, { "text": "Well React actually has a\nway to help you with that,", "start": 5242.33, "duration": 3.09 }, { "text": "and it's called prop types.", "start": 5245.42, "duration": 2.22 }, { "text": "And so React can actually validate\nthe types of a given components props", "start": 5247.64, "duration": 3.9 }, { "text": "at runtime which is great.", "start": 5251.54, "duration": 2.59 }, { "text": "It's a development tool that\nallows you as a developer", "start": 5254.13, "duration": 3.09 }, { "text": "to ensure that you're\npassing the props that you", "start": 5257.22, "duration": 2.0 }, { "text": "think you're actually passing down.", "start": 5259.22, "duration": 1.458 }, { "text": "And so say you're passing\ndown some prop that's a number", "start": 5260.678, "duration": 3.972 }, { "text": "but you actually want it as a string.", "start": 5264.65, "duration": 2.1 }, { "text": "It's very easy, as your project scales\nup, to kind of get those mixed up", "start": 5266.75, "duration": 5.512 }, { "text": "and maybe you pass down\na string when you meant", "start": 5272.262, "duration": 1.958 }, { "text": "to pass down a number or vise versa.", "start": 5274.22, "duration": 3.31 }, { "text": "And so this thing called PropTypes\nallows you to keep track of that.", "start": 5277.53, "duration": 4.31 }, { "text": "And React will actually do this\nautomatically in development mode.", "start": 5281.84, "duration": 4.75 }, { "text": "And this is also great because\nit helps document your APIs.", "start": 5286.59, "duration": 2.78 }, { "text": "And so I have this very simple\ncomponent over here called Count", "start": 5289.37, "duration": 8.19 }, { "text": "and it takes a single prop called Count.", "start": 5297.56, "duration": 2.16 }, { "text": "But say it also took\nthree other props and say", "start": 5299.72, "duration": 2.825 }, { "text": "it also had 10 other components\nthat also took their own props.", "start": 5302.545, "duration": 5.365 }, { "text": "It might get kind of hard to\nremember that as I scale the project.", "start": 5307.91, "duration": 4.11 }, { "text": "And say you're somebody\nlike Facebook who", "start": 5312.02, "duration": 1.8 }, { "text": "have something like 30,000\ndifferent components in production", "start": 5313.82, "duration": 3.69 }, { "text": "it would be very painful to\nnot have those documented", "start": 5317.51, "duration": 2.21 }, { "text": "and have to go and read the code to\nknow exactly what props you should", "start": 5319.72, "duration": 2.98 }, { "text": "be passing.", "start": 5322.7, "duration": 1.45 }, { "text": "And so by declaring this\nthing called PropTypes", "start": 5324.15, "duration": 3.95 }, { "text": "you actually can help self\ndocument all of these APIs.", "start": 5328.1, "duration": 3.59 }, { "text": "And what's nice is this only\nruns in development mode", "start": 5331.69, "duration": 2.23 }, { "text": "and so you don't have to\nworry about it slowing down", "start": 5333.92, "duration": 2.166 }, { "text": "your production in production mode.", "start": 5336.086, "duration": 3.604 }, { "text": "And so how might we go about doing that?", "start": 5339.69, "duration": 2.45 }, { "text": "There's actually a\npackage called PropTypes.", "start": 5342.14, "duration": 2.07 }, { "text": "It used to be bundled as\npart of React but they've", "start": 5344.21, "duration": 2.91 }, { "text": "since split it into its own\nmodule though it's still", "start": 5347.12, "duration": 5.73 }, { "text": "maintained by Facebook.", "start": 5352.85, "duration": 1.62 }, { "text": "So we can go ahead and do this\nthing called import PropTypes", "start": 5354.47, "duration": 4.47 }, { "text": "from a package called prop-types.", "start": 5358.94, "duration": 1.95 }, { "text": "And so now we have access to\nthis variable called PropTypes", "start": 5364.97, "duration": 2.61 }, { "text": "where importing the default export\nof whatever this library called", "start": 5367.58, "duration": 6.36 }, { "text": "PropTypes is.", "start": 5373.94, "duration": 1.63 }, { "text": "And so now we can go ahead\nand use that in our file.", "start": 5375.57, "duration": 2.21 }, { "text": "And so let's go ahead and do Count.", "start": 5377.78, "duration": 4.2 }, { "text": "And so we created this\nvariable called Count,", "start": 5381.98, "duration": 3.25 }, { "text": "which is a function of a\nstateless functional component.", "start": 5385.23, "duration": 3.78 }, { "text": "And we can actually attached to\nit a property called propTypes.", "start": 5389.01, "duration": 6.48 }, { "text": "Note that it's a lower case p here.", "start": 5395.49, "duration": 2.3 }, { "text": "And I can set it equal to an\nobject and the keys of this object", "start": 5401.05, "duration": 3.33 }, { "text": "will map to whatever\nprops that I expect.", "start": 5404.38, "duration": 3.31 }, { "text": "And so in this case we expect\na single prop called Count.", "start": 5407.69, "duration": 3.9 }, { "text": "And so I can say Count should\nbe of what type, well PropTypes.", "start": 5411.59, "duration": 4.8 }, { "text": "So capital P here is referring to\nthe library that we just imported.", "start": 5416.39, "duration": 4.09 }, { "text": "And we can say we want a\nnumber, and it's required.", "start": 5420.48, "duration": 4.24 }, { "text": "And so now we see this warning.", "start": 5428.14, "duration": 5.49 }, { "text": "We see, hey, there's a\nfailed prop type here.", "start": 5433.63, "duration": 4.77 }, { "text": "The prop count is marked as\nrequired in this component", "start": 5438.4, "duration": 3.54 }, { "text": "called Count but it's values undefined.", "start": 5441.94, "duration": 2.49 }, { "text": "Why is it undefined?", "start": 5444.43, "duration": 1.69 }, { "text": "Well because we're importing\nthis thing called number", "start": 5446.12, "duration": 3.71 }, { "text": "and we're passing this thing\ncalled number over here.", "start": 5449.83, "duration": 2.31 }, { "text": "But we're not actually exporting\nanything called number in this module.", "start": 5452.14, "duration": 4.51 }, { "text": "And say we actually remove\nthat and passed a 0 here.", "start": 5456.65, "duration": 7.34 }, { "text": "Now it'll run as expected.", "start": 5466.54, "duration": 1.71 }, { "text": "There are no warnings thrown.", "start": 5468.25, "duration": 2.91 }, { "text": "And now let's try simulating a mistake\nwhere rather than passing the number 0", "start": 5471.16, "duration": 5.22 }, { "text": "we go ahead and pass the string 0.", "start": 5476.38, "duration": 4.32 }, { "text": "The app will still work because we're\njust displaying whatever we pass.", "start": 5480.7, "duration": 4.47 }, { "text": "But if you notice we\nhave a warning here.", "start": 5485.17, "duration": 4.11 }, { "text": "Warning: Failed prop type.", "start": 5489.28, "duration": 1.29 }, { "text": "Invalid prop count of type\nstring supplied to count.", "start": 5490.57, "duration": 3.24 }, { "text": "We were actually expecting a number.", "start": 5493.81, "duration": 1.825 }, { "text": "And so that's actually\nReact's built-in prop type", "start": 5495.635, "duration": 2.675 }, { "text": "system which is checking against the\ntypes that we're passing down here.", "start": 5498.31, "duration": 4.08 }, { "text": "We say, hey we're expecting\nthis thing as a number", "start": 5502.39, "duration": 2.92 }, { "text": "but since we pass a string it will throw\na warning for us just to let us know,", "start": 5505.31, "duration": 3.635 }, { "text": "oh by the way I don't know\nif you know this or not,", "start": 5508.945, "duration": 2.125 }, { "text": "but you're passing the wrong type down.", "start": 5511.07, "duration": 2.25 }, { "text": "And so we can use this thing\ncalled prop types in order", "start": 5513.32, "duration": 2.39 }, { "text": "to validate the props that were passed.", "start": 5515.71, "duration": 5.162 }, { "text": "And so in stateless\nfunctional components", "start": 5520.872, "duration": 1.708 }, { "text": "we actually just add this as a property.", "start": 5522.58, "duration": 3.33 }, { "text": "But say this were actually\na class component.", "start": 5525.91, "duration": 3.81 }, { "text": "So let's do class Count,\nextends React.component.", "start": 5529.72, "duration": 12.96 }, { "text": "And then we have a render\nwhich will just return this.", "start": 5542.68, "duration": 5.357 }, { "text": "And let's fix some styling\nso it's easier to read.", "start": 5553.35, "duration": 2.16 }, { "text": "So now we have a class component and\nas long as there are no syntax errors--", "start": 5560.5, "duration": 7.76 }, { "text": "There's a syntax error\nsomewhere that I don't see.", "start": 5571.06, "duration": 6.16 }, { "text": "Does anybody see the syntax error?", "start": 5577.22, "duration": 1.51 }, { "text": "Can anybody beat me to it?", "start": 5578.73, "duration": 1.693 }, { "text": "AUDIENCE: Is that last a\nparentheses or is that a bracket?", "start": 5580.423, "duration": 3.099 }, { "text": "SPEAKER 1: This here is a parentheses.", "start": 5583.522, "duration": 1.583 }, { "text": "Oh, yep.", "start": 5585.105, "duration": 2.595 }, { "text": "Nice catch.", "start": 5587.7, "duration": 0.745 }, { "text": "I owe you some candy.", "start": 5588.445, "duration": 0.875 }, { "text": "So now if we go ahead\nand reload this, it", "start": 5592.08, "duration": 3.3 }, { "text": "can't find the called props\nbecause class component", "start": 5595.38, "duration": 6.51 }, { "text": "is stored as this.props.", "start": 5601.89, "duration": 2.13 }, { "text": "And now we can go ahead and\nsee that it is as expected.", "start": 5604.02, "duration": 3.789 }, { "text": "We still have that\nfailed prop type message", "start": 5607.809, "duration": 1.791 }, { "text": "because we're passing a string rather\nthan a number but it's working.", "start": 5609.6, "duration": 4.38 }, { "text": "And so we can go ahead and\nso we create the class here.", "start": 5613.98, "duration": 3.7 }, { "text": "And then we go ahead and do\nCount.propTypes here which works.", "start": 5617.68, "duration": 3.84 }, { "text": "But generally the way that you\nsee the convention is actually", "start": 5621.52, "duration": 4.52 }, { "text": "to use this thing\ncalled a static method.", "start": 5626.04, "duration": 3.55 }, { "text": "And so there's actually a static keyword\nwhere you can do static propTypes", "start": 5629.59, "duration": 5.35 }, { "text": "equals that object.", "start": 5634.94, "duration": 1.69 }, { "text": "So functionally exactly the same, but\nthe convention is to use this thing", "start": 5642.09, "duration": 5.27 }, { "text": "called a static method\nor a static property", "start": 5647.36, "duration": 5.88 }, { "text": "because it's just the way that\nthe new class index works.", "start": 5653.24, "duration": 5.53 }, { "text": "And so it's functionally the same\nas doing count.propTypes down here,", "start": 5658.77, "duration": 4.57 }, { "text": "but the convention is just\nto use the newer syntax.", "start": 5663.34, "duration": 2.125 }, { "text": "So any questions on PropTypes?", "start": 5668.49, "duration": 3.38 }, { "text": "Does everybody see the\nutility of using them?", "start": 5671.87, "duration": 2.85 }, { "text": "They've definitely saved me\nmultiple times on personal projects.", "start": 5674.72, "duration": 2.865 }, { "text": "And so now, if we've passed the\ncorrect props, there are no warnings.", "start": 5683.83, "duration": 6.22 }, { "text": "Great.", "start": 5690.05, "duration": 0.5 }, { "text": "And the last concept I wanted to touch\non is just how the heck do I read docs.", "start": 5690.55, "duration": 5.515 }, { "text": "Because I can't possibly teach\nyou every single component", "start": 5696.065, "duration": 2.375 }, { "text": "that React Native offers.", "start": 5698.44, "duration": 1.74 }, { "text": "But they do offer a\nlot of great components", "start": 5700.18, "duration": 2.34 }, { "text": "that you may want to use in either\nproject for this class or maybe", "start": 5702.52, "duration": 3.48 }, { "text": "a personal project\nthat you're working on.", "start": 5706.0, "duration": 1.98 }, { "text": "And so here's basically\nthe steps that I go through", "start": 5707.98, "duration": 3.15 }, { "text": "in order to figure out what I should\nuse when working on my own project.", "start": 5711.13, "duration": 4.09 }, { "text": "So first have a goal in mind.", "start": 5715.22, "duration": 1.832 }, { "text": "You need to know what problem\nyou're trying to solve.", "start": 5717.052, "duration": 2.208 }, { "text": "Otherwise reading a bunch\nabout what's offered", "start": 5719.26, "duration": 2.31 }, { "text": "doesn't really mean anything to you.", "start": 5721.57, "duration": 1.86 }, { "text": "And so say you were doing\nsomething like the TODO app", "start": 5723.43, "duration": 3.09 }, { "text": "that we did before the first\nthing that you'll notice", "start": 5726.52, "duration": 3.6 }, { "text": "is that there is no input\ntype checkbox in React Native.", "start": 5730.12, "duration": 3.7 }, { "text": "And so you have to figure\nout what is my goal here?", "start": 5733.82, "duration": 2.57 }, { "text": "Well I want to replace\nwhat used to be a checkbox.", "start": 5736.39, "duration": 4.91 }, { "text": "And so your goal in that\nexample would be all right", "start": 5741.3, "duration": 2.77 }, { "text": "I need some component that\nbasically just renders a Boolean.", "start": 5744.07, "duration": 4.62 }, { "text": "Then just see what the\nlibrary or framework offers.", "start": 5748.69, "duration": 2.72 }, { "text": "And so the way I did that was I\njust browsed through the docs.", "start": 5751.41, "duration": 3.77 }, { "text": "So they're linked in this presentation.", "start": 5755.18, "duration": 2.87 }, { "text": "I'll go ahead and link them in\nthe Resources tab on the website.", "start": 5758.05, "duration": 3.51 }, { "text": "But just see exactly what the library\nhas to offer, see what they have,", "start": 5761.56, "duration": 4.755 }, { "text": "then find something that\nsolves your problem.", "start": 5766.315, "duration": 1.875 }, { "text": "There may be multiple things\nthat solve your problem", "start": 5768.19, "duration": 2.19 }, { "text": "but just try to find the thing\nthat best solves your problem.", "start": 5770.38, "duration": 2.88 }, { "text": "And so in my case for that\nparticular Boolean switch", "start": 5773.26, "duration": 3.09 }, { "text": "I saw that they had something\ncalled a switch that", "start": 5776.35, "duration": 2.49 }, { "text": "just renders a Boolean flag that you\ncan just tap and then configure it.", "start": 5778.84, "duration": 8.43 }, { "text": "And so the docs will\ntell you exactly what", "start": 5787.27, "duration": 2.73 }, { "text": "the API is, how you can configure\nit, what properties you have to set.", "start": 5790.0, "duration": 3.42 }, { "text": "And so for switch we had the value\nwhich was either true or false.", "start": 5793.42, "duration": 4.37 }, { "text": "And we had on value change which was, I\nbelieve, the name of the prop that gets", "start": 5797.79, "duration": 4.9 }, { "text": "called every single time\nyou change that value.", "start": 5802.69, "duration": 3.31 }, { "text": "And so it just fit that API\nto whatever you're using.", "start": 5806.0, "duration": 6.83 }, { "text": "And then, of course, if you\nhave trouble even then, then", "start": 5812.83, "duration": 3.06 }, { "text": "you can turn to the community.", "start": 5815.89, "duration": 1.83 }, { "text": "A great thing about React,\nReact Native is the community.", "start": 5817.72, "duration": 3.584 }, { "text": "Lots and lots of people use\nReact, lots and lots of people", "start": 5821.304, "duration": 2.416 }, { "text": "use React Native and a\nlot of them are experts", "start": 5823.72, "duration": 3.942 }, { "text": "who were once beginners themselves.", "start": 5827.662, "duration": 1.458 }, { "text": "And everybody remembers when they\nwere first learning React and React", "start": 5829.12, "duration": 3.23 }, { "text": "Native there's just a\nlot around and there's", "start": 5832.35, "duration": 2.38 }, { "text": "a lot of stuff in the ecosystem.", "start": 5834.73, "duration": 2.04 }, { "text": "And the community's really great in\nhelping each other learn what's around", "start": 5836.77, "duration": 5.13 }, { "text": "and how to best solve their problems.", "start": 5841.9, "duration": 1.612 }, { "text": "And so just ask the question.", "start": 5843.512, "duration": 1.208 }, { "text": "You can ask it in Slack\nif you're part of a class.", "start": 5844.72, "duration": 2.76 }, { "text": "You can ask it on something like\nStack Overflow or just Google around.", "start": 5847.48, "duration": 4.299 }, { "text": "And so if you run into a\nproblem odds are somebody else", "start": 5851.779, "duration": 2.291 }, { "text": "has already run into the problem and\nodds are somebody better than you,", "start": 5854.07, "duration": 4.93 }, { "text": "better than I have solved that problem.", "start": 5859.0, "duration": 2.47 }, { "text": "And you can go ahead\nand use their solution.", "start": 5861.47, "duration": 3.89 }, { "text": "And so any time you get stuck\nfeel free to either browse", "start": 5865.36, "duration": 4.44 }, { "text": "the documentation yourself or reach\nout to the staff with any questions.", "start": 5869.8, "duration": 5.82 }, { "text": "And so, yeah, that's all\nI have for you today.", "start": 5875.62, "duration": 3.49 }, { "text": "Good luck starting your project.", "start": 5879.11, "duration": 1.61 }, { "text": "It will be released before next Monday.", "start": 5880.72, "duration": 3.32 } ]