CS50-raw / 7O43VDOlQ_o.json
cutehusky's picture
Upload folder using huggingface_hub
0791e78 verified
[
{
"text": "[MUSIC PLAYING]",
"start": 0.0,
"duration": 2.958
},
{
"text": "JORDAN HAYASHI: Hello, and\nwelcome for Lecture Two--",
"start": 17.27,
"duration": 2.37
},
{
"text": "React, Props, and State.",
"start": 19.64,
"duration": 2.4
},
{
"text": "So last week, we talked about a bunch\nof different topics, one being ES6",
"start": 22.04,
"duration": 3.72
},
{
"text": "and beyond and the syntax\nthat comes with each of those.",
"start": 25.76,
"duration": 2.69
},
{
"text": "We talked about closures,\nthe process by which",
"start": 28.45,
"duration": 2.62
},
{
"text": "a function can reference variables\ndeclared in a parent function.",
"start": 31.07,
"duration": 5.13
},
{
"text": "We talked about IIFEs, Immediately\nInvoked Function Expressions.",
"start": 36.2,
"duration": 3.68
},
{
"text": "We talked about using functions\nas first-class citizens.",
"start": 39.88,
"duration": 3.675
},
{
"text": "We talked about the execution\nstack and event loop,",
"start": 43.555,
"duration": 2.125
},
{
"text": "and how JavaScript actually\nexecutes in browsers.",
"start": 45.68,
"duration": 2.82
},
{
"text": "We talked about callbacks, promises, and\nasync/await, all of the different ways",
"start": 48.5,
"duration": 3.3
},
{
"text": "to handle asynchronous actions.",
"start": 51.8,
"duration": 1.89
},
{
"text": "And last, we talked about this\nand the way that this is bound.",
"start": 53.69,
"duration": 3.96
},
{
"text": "This week, we're going to\nstart with classes, which",
"start": 57.65,
"duration": 3.23
},
{
"text": "is a syntax I was introduced in ES6.",
"start": 60.88,
"duration": 3.639
},
{
"text": "It simplifies the defining of complex\nobjects that have their own prototypes.",
"start": 64.519,
"duration": 4.967
},
{
"text": "And with that, you have\ntwo different things.",
"start": 69.486,
"duration": 1.874
},
{
"text": "You have classes and instances.",
"start": 71.36,
"duration": 1.53
},
{
"text": "Classes is basically an abstract\nthing that you can declare,",
"start": 72.89,
"duration": 3.73
},
{
"text": "basically saying, hey, by\nthe way, any of these objects",
"start": 76.62,
"duration": 2.78
},
{
"text": "that you create will have these\nmethods associated with them.",
"start": 79.4,
"duration": 3.03
},
{
"text": "Or they might have these things\nattached to them that you can use.",
"start": 82.43,
"duration": 4.14
},
{
"text": "And then when you actually\nturn an abstract class",
"start": 86.57,
"duration": 2.64
},
{
"text": "into an instance of that object,\nthat is called an instance.",
"start": 89.21,
"duration": 4.42
},
{
"text": "An example of that would be the\ndifference between the date,",
"start": 93.63,
"duration": 6.12
},
{
"text": "which is a function-- it's a class--",
"start": 99.75,
"duration": 2.66
},
{
"text": "or a new date.",
"start": 102.41,
"duration": 1.23
},
{
"text": "So if you do something like\nconst d equals new date,",
"start": 103.64,
"duration": 4.54
},
{
"text": "then now you have a date object itself.",
"start": 108.18,
"duration": 3.38
},
{
"text": "And so Date with a capital D\nwould be a class in that case,",
"start": 111.56,
"duration": 3.137
},
{
"text": "and the lowercase D\nwould be an instance.",
"start": 114.697,
"duration": 1.708
},
{
"text": "These instances have things attached\nto them, like methods and properties,",
"start": 120.61,
"duration": 3.54
},
{
"text": "and the classes have\nthings like static methods.",
"start": 124.15,
"duration": 2.4
},
{
"text": "And so methods are\nbasically anything that's",
"start": 126.55,
"duration": 2.07
},
{
"text": "a function that can be invoked\non any of the instances.",
"start": 128.62,
"duration": 4.12
},
{
"text": "You can think of that as a\nfunction on the classes prototype.",
"start": 132.74,
"duration": 6.24
},
{
"text": "A static method, on the\nother hand, is basically",
"start": 138.98,
"duration": 2.6
},
{
"text": "a method that doesn't really care about\nthe particular instance of a class.",
"start": 141.58,
"duration": 3.3
},
{
"text": "Instead, it cares about all instances of\nthe class, so something like Date.now,",
"start": 144.88,
"duration": 10.14
},
{
"text": "where you don't really care about\na specific instance of a date",
"start": 155.02,
"duration": 3.78
},
{
"text": "if you just want to get the time.",
"start": 158.8,
"duration": 1.92
},
{
"text": "Whereas something like\nturning a date to a string--",
"start": 160.72,
"duration": 2.62
},
{
"text": "in this class, d.toString--",
"start": 163.34,
"duration": 3.625
},
{
"text": "in that case, you do care\nabout the particular date",
"start": 166.965,
"duration": 2.125
},
{
"text": "object that you're working on.",
"start": 169.09,
"duration": 1.47
},
{
"text": "And so when you do\ncapital Date.now, that's",
"start": 170.56,
"duration": 2.91
},
{
"text": "considered a static method,\nsince it's attached to the class.",
"start": 173.47,
"duration": 2.97
},
{
"text": "And if you do something like date--",
"start": 176.44,
"duration": 2.52
},
{
"text": "lowercase d-- dot toString, that really\nmatters which instance you're attached",
"start": 178.96,
"duration": 4.38
},
{
"text": "to, and therefore, that's a method.",
"start": 183.34,
"duration": 2.71
},
{
"text": "Lastly, we have properties,\nwhich are like methods.",
"start": 186.05,
"duration": 3.86
},
{
"text": "But rather than being functions,\nthey're actually just values,",
"start": 189.91,
"duration": 3.68
},
{
"text": "and they are associated with a\nparticular instance of a class.",
"start": 193.59,
"duration": 4.55
},
{
"text": "And so with classes come\na few different keywords.",
"start": 198.14,
"duration": 2.27
},
{
"text": "We have new, which you saw me type\nover here, which is basically saying,",
"start": 200.41,
"duration": 3.91
},
{
"text": "hey, give me an instance\nof this particular class.",
"start": 204.32,
"duration": 2.99
},
{
"text": "You invoke the class like an object, in\ncase you want to pass anything into it.",
"start": 207.31,
"duration": 3.32
},
{
"text": "And so say we want to do new\nconst d2 equals new date,",
"start": 210.63,
"duration": 5.362
},
{
"text": "and we actually want\nto pass in some number.",
"start": 215.992,
"duration": 1.833
},
{
"text": "That gives us a new date\nfrom a very long time ago.",
"start": 220.69,
"duration": 3.93
},
{
"text": "So d.toString.",
"start": 224.62,
"duration": 1.29
},
{
"text": "Oh, d2.toString.",
"start": 228.784,
"duration": 1.596
},
{
"text": "Since we passed in the number 1,\n2, 3, 4, that's basically saying,",
"start": 233.8,
"duration": 3.78
},
{
"text": "give me a date that's 1,234 milliseconds\nafter date 0, which is back in 1969.",
"start": 237.58,
"duration": 13.23
},
{
"text": "So a constructor is\nbasically something that you",
"start": 250.81,
"duration": 2.0
},
{
"text": "define within a class that says,\nhey, when you create a new class,",
"start": 252.81,
"duration": 3.719
},
{
"text": "invoke this method such that you\ncreate a new instance of a class.",
"start": 256.529,
"duration": 5.911
},
{
"text": "So let's actually practice\nthis a little bit.",
"start": 262.44,
"duration": 2.46
},
{
"text": "So is everybody here familiar with\na data structure called a set?",
"start": 264.9,
"duration": 3.075
},
{
"text": "So basically what a\nset is, is it's a list,",
"start": 273.85,
"duration": 3.12
},
{
"text": "a data structure that supports things\nlike add, delete, and inclusion,",
"start": 276.97,
"duration": 4.5
},
{
"text": "where you cannot have multiple\nthings of the same value.",
"start": 281.47,
"duration": 3.62
},
{
"text": "Or in other words, it's\na list of unique values.",
"start": 285.09,
"duration": 2.84
},
{
"text": "And the methods that\nit should support are",
"start": 287.93,
"duration": 2.51
},
{
"text": "add, which is basically\nadd to this list;",
"start": 290.44,
"duration": 2.46
},
{
"text": "delete, which is basically get\nrid of something from this;",
"start": 292.9,
"duration": 3.06
},
{
"text": "or inclusion, which is saying, hey,\ndoes this list have a particular value?",
"start": 295.96,
"duration": 5.97
},
{
"text": "And it should also have the\nability to get its size.",
"start": 301.93,
"duration": 2.54
},
{
"text": "And so down at the bottom of\nthe file, I defined a few tests",
"start": 304.47,
"duration": 4.0
},
{
"text": "that we're going to run after we\nimplement this, such as line 7 here,",
"start": 308.47,
"duration": 4.65
},
{
"text": "we have const s gets a new\nset with an array from 1 to 5,",
"start": 313.12,
"duration": 4.17
},
{
"text": "which is basically saying, give me a\nnew set with five values, 1, 2, 3, 4,",
"start": 317.29,
"duration": 3.21
},
{
"text": "and 5.",
"start": 320.5,
"duration": 1.09
},
{
"text": "We're going to try to\ndo S.add1, and so we're",
"start": 321.59,
"duration": 2.21
},
{
"text": "going to try to add 1\nthree times to the set.",
"start": 323.8,
"duration": 3.37
},
{
"text": "And when we do S.size, we want it\nactually to only have five members,",
"start": 327.17,
"duration": 4.31
},
{
"text": "because you shouldn't be\nable to add 1 multiple times.",
"start": 331.48,
"duration": 4.17
},
{
"text": "Down here, we do S.add6,\nand then we try S.has6,",
"start": 335.65,
"duration": 5.07
},
{
"text": "and it should contain the number 6.",
"start": 340.72,
"duration": 2.67
},
{
"text": "That thing should not be there.",
"start": 343.39,
"duration": 3.18
},
{
"text": "We try to see the size of it, and\nit should have added another member.",
"start": 346.57,
"duration": 3.752
},
{
"text": "And then down here, we tried\nto delete that and do a couple",
"start": 350.322,
"duration": 2.458
},
{
"text": "of associated checks with that.",
"start": 352.78,
"duration": 1.85
},
{
"text": "And so how are we going to go\nabout implementing this class?",
"start": 354.63,
"duration": 3.22
},
{
"text": "So as per the slide over here, we\nuse a method called constructor",
"start": 361.22,
"duration": 3.8
},
{
"text": "in order to go ahead and construct\nan instance of this class.",
"start": 365.02,
"duration": 4.18
},
{
"text": "And so within the class, we should\nhave a method called constructor,",
"start": 369.2,
"duration": 6.31
},
{
"text": "which takes a single argument.",
"start": 375.51,
"duration": 3.55
},
{
"text": "It should take an array or some\nsort of list to be created with.",
"start": 379.06,
"duration": 4.324
},
{
"text": "And what are we going to do?",
"start": 383.384,
"duration": 1.166
},
{
"text": "Well, we should attach to\nthe instance the array.",
"start": 384.55,
"duration": 6.01
},
{
"text": "And that's basically saying,\nwhen I'm constructed,",
"start": 390.56,
"duration": 2.38
},
{
"text": "I expect to have one argument,\nwhich is just an array.",
"start": 392.94,
"duration": 3.0
},
{
"text": "And the only thing I'm going to\ndo when creating this set object",
"start": 395.94,
"duration": 3.87
},
{
"text": "is just store a reference to that array.",
"start": 399.81,
"duration": 3.33
},
{
"text": "By doing this.arr = arr.",
"start": 403.14,
"duration": 1.68
},
{
"text": "And so in this case, the\nthis keyword is referring",
"start": 407.33,
"duration": 2.71
},
{
"text": "to the instance of the object.",
"start": 410.04,
"duration": 4.36
},
{
"text": "Cool, so let's try adding a couple\nof different methods to this.",
"start": 414.4,
"duration": 3.44
},
{
"text": "So we should be able to support\nadd, which should take a value.",
"start": 417.84,
"duration": 5.88
},
{
"text": "We should be able to support\ndelete, which also takes a value.",
"start": 423.72,
"duration": 5.39
},
{
"text": "And we should have something called\nhas, which checks for inclusion.",
"start": 429.11,
"duration": 6.62
},
{
"text": "So how might we do something?",
"start": 435.73,
"duration": 3.2
},
{
"text": "How might we add to this class?",
"start": 438.93,
"duration": 1.594
},
{
"text": "Does anybody have any ideas?",
"start": 440.524,
"duration": 1.166
},
{
"text": "AUDIENCE: [INAUDIBLE]",
"start": 446.78,
"duration": 3.76
},
{
"text": "JORDAN HAYASHI: Exactly.",
"start": 450.54,
"duration": 1.0
},
{
"text": "So we should use something\nlike push to add to the array.",
"start": 451.54,
"duration": 3.6
},
{
"text": "But before we do that, we should make\nsure that that number does not already",
"start": 455.14,
"duration": 4.35
},
{
"text": "exist.",
"start": 459.49,
"duration": 0.85
},
{
"text": "And so maybe we should implement the\nhas method first, which is a great idea.",
"start": 460.34,
"duration": 4.626
},
{
"text": "Let's go ahead and do that.",
"start": 464.966,
"duration": 1.124
},
{
"text": "So how might we do has?",
"start": 466.09,
"duration": 3.39
},
{
"text": "Well, it turns out on\nthe array prototype,",
"start": 469.48,
"duration": 2.19
},
{
"text": "we already have something\ncalled includes, which tells us",
"start": 471.67,
"duration": 3.222
},
{
"text": "if an array includes a value,\nso we can just do that.",
"start": 474.892,
"duration": 2.208
},
{
"text": "We can do return this.arr.includes(val).",
"start": 477.1,
"duration": 2.98
},
{
"text": "And so now that we have that,\nhow might we take care of add?",
"start": 486.23,
"duration": 2.68
},
{
"text": "We should say, oh, well, if this does\nnot have the value already, add to it.",
"start": 492.43,
"duration": 8.3
},
{
"text": "And so here I use this.has.",
"start": 506.08,
"duration": 3.15
},
{
"text": "So this here is referring\nto the instance of the set,",
"start": 509.23,
"duration": 5.37
},
{
"text": "and so this.has is\nreferring to this method",
"start": 514.6,
"duration": 2.459
},
{
"text": "down here, on this particular instance.",
"start": 517.059,
"duration": 3.411
},
{
"text": "And then when I do this.arr, this still\nrefers to the instance of this set.",
"start": 520.47,
"duration": 5.78
},
{
"text": "And so we're just getting at\nthis array property that we have,",
"start": 526.25,
"duration": 3.53
},
{
"text": "and we're pushing that\nvalue to that array.",
"start": 529.78,
"duration": 3.25
},
{
"text": "Cool.",
"start": 533.03,
"duration": 0.5
},
{
"text": "So how might we go about this delete?",
"start": 533.53,
"duration": 2.475
},
{
"text": "AUDIENCE: [INAUDIBLE] has value.",
"start": 540.718,
"duration": 2.119
},
{
"text": "JORDAN HAYASHI: Yeah, we can\ncheck if we have the value,",
"start": 542.837,
"duration": 2.333
},
{
"text": "but it doesn't really\nmatter all that much.",
"start": 545.17,
"duration": 2.27
},
{
"text": "A quick and easy way would just be\ndoing this.arr = this.arr.filter",
"start": 547.44,
"duration": 6.691
},
{
"text": "and then we can just\nfilter by the values.",
"start": 554.131,
"duration": 1.749
},
{
"text": "So we could say, oh, we\nwant for every x in here,",
"start": 555.88,
"duration": 4.17
},
{
"text": "we want the x's that\ndon't equal this value.",
"start": 560.05,
"duration": 2.56
},
{
"text": "Cool.",
"start": 568.14,
"duration": 0.5
},
{
"text": "And so we can go ahead and\nrun this to see if it works.",
"start": 568.64,
"duration": 2.39
},
{
"text": "Oops.",
"start": 578.945,
"duration": 0.5
},
{
"text": "While I edit, I should flip these.",
"start": 585.88,
"duration": 1.77
},
{
"text": "And we go ahead.",
"start": 593.96,
"duration": 1.739
},
{
"text": "S should have five members and actually\nhas-- ooh, we forgot to influence size,",
"start": 595.699,
"duration": 3.291
},
{
"text": "actually.",
"start": 598.99,
"duration": 0.5
},
{
"text": "And so we took care\nof all of the methods,",
"start": 601.9,
"duration": 2.19
},
{
"text": "but we didn't include the size.",
"start": 604.09,
"duration": 3.45
},
{
"text": "And so we should be able to\nreturn the size of this set.",
"start": 607.54,
"duration": 3.84
},
{
"text": "How might we do that?",
"start": 611.38,
"duration": 1.05
},
{
"text": "Well, this is interesting.",
"start": 615.247,
"duration": 1.083
},
{
"text": "So we should be able to get at this\nvalue by doing the instance.size.",
"start": 616.33,
"duration": 6.9
},
{
"text": "And JavaScript actually\nhas a convenient way.",
"start": 623.23,
"duration": 2.17
},
{
"text": "We can do get size,\nwhich is saying, when",
"start": 625.4,
"duration": 8.26
},
{
"text": "somebody tries to get at the\nvalue or the property.size,",
"start": 633.66,
"duration": 4.2
},
{
"text": "actually run this function.",
"start": 637.86,
"duration": 2.41
},
{
"text": "So this is just syntax\nfor that shortcut.",
"start": 640.27,
"duration": 4.94
},
{
"text": "So how might we implement size?",
"start": 645.21,
"duration": 1.81
},
{
"text": "AUDIENCE: [INAUDIBLE]",
"start": 650.713,
"duration": 2.397
},
{
"text": "JORDAN HAYASHI: Yeah, just\nreturn this.arr.length.",
"start": 653.11,
"duration": 3.09
},
{
"text": "So now we can run this, and we\nsee I should have five members,",
"start": 661.13,
"duration": 5.589
},
{
"text": "and it actually has five.",
"start": 666.719,
"duration": 1.041
},
{
"text": "So that's good.",
"start": 667.76,
"duration": 1.41
},
{
"text": "S should contain 5.",
"start": 669.17,
"duration": 1.03
},
{
"text": "That's true.",
"start": 670.2,
"duration": 0.5
},
{
"text": "That works.",
"start": 670.7,
"duration": 1.06
},
{
"text": "S should contain six.",
"start": 671.76,
"duration": 0.95
},
{
"text": "It's true.",
"start": 672.71,
"duration": 0.93
},
{
"text": "S should have six members, and\nactually has six, so we're good still.",
"start": 673.64,
"duration": 3.2
},
{
"text": "S should no longer contain six.",
"start": 676.84,
"duration": 1.54
},
{
"text": "That also returns true.",
"start": 678.38,
"duration": 1.53
},
{
"text": "And lastly, S should have five members,\nand actually does indeed have five.",
"start": 679.91,
"duration": 5.34
},
{
"text": "So does anybody have any questions with\nour implementation of set as a class?",
"start": 685.25,
"duration": 4.39
},
{
"text": "Great.",
"start": 693.47,
"duration": 0.5
},
{
"text": "So it turns out JavaScript\nactually already has a set class,",
"start": 693.97,
"duration": 7.1
},
{
"text": "and it works exactly as we implemented.",
"start": 701.07,
"duration": 2.49
},
{
"text": "But say we actually wanted to use\nthe native implementation of set",
"start": 703.56,
"duration": 3.18
},
{
"text": "and actually add some stuff to it.",
"start": 706.74,
"duration": 2.91
},
{
"text": "So that's where we use these other\nkeywords, called extends and super.",
"start": 709.65,
"duration": 3.7
},
{
"text": "So extends is the\nJavaScript way of saying,",
"start": 713.35,
"duration": 2.31
},
{
"text": "hey, I want to start with a base\nclass and actually add to it.",
"start": 715.66,
"duration": 2.66
},
{
"text": "Extend this class.",
"start": 718.32,
"duration": 1.83
},
{
"text": "And super, as we'll see in a second,\nis when we're writing that class,",
"start": 720.15,
"duration": 3.016
},
{
"text": "so we can refer to the original\nclass using this keyword.",
"start": 723.166,
"duration": 2.374
},
{
"text": "And so in this example\ncalled my set, we're",
"start": 730.49,
"duration": 2.27
},
{
"text": "going ahead and extending that set\nwith a bunch of different things.",
"start": 732.76,
"duration": 3.197
},
{
"text": "And so here you see constructor.",
"start": 735.957,
"duration": 1.333
},
{
"text": "It still takes an array.",
"start": 737.29,
"duration": 1.08
},
{
"text": "And the first thing that we do\nis we invoke super on that array.",
"start": 738.37,
"duration": 3.87
},
{
"text": "So this is basically saying,\nhey, we're extending a set.",
"start": 742.24,
"duration": 5.769
},
{
"text": "So when you do the constructor,\nthe first thing you should do",
"start": 748.009,
"duration": 2.541
},
{
"text": "is actually run the\noriginal set's constructor.",
"start": 750.55,
"duration": 3.69
},
{
"text": "And then let's also keep\ntrack of this.originalarray",
"start": 754.24,
"duration": 2.23
},
{
"text": "is the array that's passed in.",
"start": 756.47,
"duration": 1.46
},
{
"text": "And so we'll use that later to\nuse this thing called reset.",
"start": 757.93,
"duration": 4.68
},
{
"text": "And so say we wanted to every single\ntime we added a value to this set,",
"start": 762.61,
"duration": 3.78
},
{
"text": "we also wanted to log, hey,\nwe added a value to this set.",
"start": 766.39,
"duration": 4.35
},
{
"text": "So we can just start writing\nthis method called add.",
"start": 770.74,
"duration": 3.29
},
{
"text": "It takes a value just\nlike any other example.",
"start": 774.03,
"duration": 2.114
},
{
"text": "But instead of implementing\nadd ourself, we're",
"start": 776.144,
"duration": 1.916
},
{
"text": "just going to use the native\nimplementation of add.",
"start": 778.06,
"duration": 2.25
},
{
"text": "And so that's where we use super.add.",
"start": 780.31,
"duration": 1.6
},
{
"text": "So super, again, refers to the\nclass that we're extending.",
"start": 781.91,
"duration": 4.57
},
{
"text": "And so when we invoke\nsuper.add, it goes ahead",
"start": 786.48,
"duration": 2.35
},
{
"text": "and does that using the\nnative implementation.",
"start": 788.83,
"duration": 2.106
},
{
"text": "And then since we're going to extend\nit with some additional logging.",
"start": 790.936,
"duration": 2.874
},
{
"text": "We're just going to log, hey,\nwe added this val to the set.",
"start": 793.81,
"duration": 3.09
},
{
"text": "And if you're not familiar with\nthis, if you use the backticks,",
"start": 796.9,
"duration": 3.48
},
{
"text": "you can go ahead and add\nvariables in line, in the string,",
"start": 800.38,
"duration": 2.85
},
{
"text": "and it'll go ahead and\nsubstitute those in.",
"start": 803.23,
"duration": 3.28
},
{
"text": "And so you see we added a\ncouple of other methods here.",
"start": 806.51,
"duration": 2.36
},
{
"text": "We have to array, which is basically\nsaying, hey, I actually want the array,",
"start": 808.87,
"duration": 3.48
},
{
"text": "and not the set.",
"start": 812.35,
"duration": 0.7
},
{
"text": "And so we can just\nreturn Array.from (this).",
"start": 813.05,
"duration": 2.41
},
{
"text": "We're passing in the entire instance.",
"start": 815.46,
"duration": 2.86
},
{
"text": "And lastly, we have a\nreset, which is saying, hey,",
"start": 818.32,
"duration": 3.27
},
{
"text": "I want the original set\nthat I had, or at least",
"start": 821.59,
"duration": 4.71
},
{
"text": "a new set with equivalent value.",
"start": 826.3,
"duration": 3.06
},
{
"text": "So you can return a new my set.",
"start": 829.36,
"duration": 1.38
},
{
"text": "So notice you are referencing\nmy set inside that class.",
"start": 830.74,
"duration": 3.72
},
{
"text": "We want a new one, and\nwe're going to pass in,",
"start": 834.46,
"duration": 2.22
},
{
"text": "as the array here, the original array.",
"start": 836.68,
"duration": 2.12
},
{
"text": "So this is an example of us extending\na class that already exists.",
"start": 841.65,
"duration": 4.6
},
{
"text": "And as you see, if we want to\nreference methods on that original,",
"start": 846.25,
"duration": 4.38
},
{
"text": "we just use that super keyword.",
"start": 850.63,
"duration": 3.48
},
{
"text": "So any questions on sets,\nhow we define them--",
"start": 854.11,
"duration": 3.09
},
{
"text": "or sorry, classes and how we\ndefine them, how we extend them?",
"start": 857.2,
"duration": 4.2
},
{
"text": "So why might this be useful?",
"start": 861.4,
"duration": 3.82
},
{
"text": "So as you guys are\ndoing on your project,",
"start": 865.22,
"duration": 3.5
},
{
"text": "you're keeping track of\nthese things called to dos.",
"start": 868.72,
"duration": 3.07
},
{
"text": "What if we actually\nhad a class for to do?",
"start": 871.79,
"duration": 3.59
},
{
"text": "And when you invoke this constructor\non some configuration object,",
"start": 875.38,
"duration": 4.419
},
{
"text": "what if it pulls out the text\nand whether it's checked or not",
"start": 879.799,
"duration": 2.541
},
{
"text": "and stores it as part of\n[? its ?] class instance?",
"start": 882.34,
"duration": 2.732
},
{
"text": "And say we want to\nrender it to the page.",
"start": 885.072,
"duration": 1.708
},
{
"text": "What if we could just\nreturn some HTML like this?",
"start": 886.78,
"duration": 2.67
},
{
"text": "It would be quite handy, right?",
"start": 891.699,
"duration": 1.291
},
{
"text": "And that actually is\nour next topic, react.",
"start": 892.99,
"duration": 5.46
},
{
"text": "So react is a JavaScript\nlibrary, and it allows",
"start": 898.45,
"duration": 2.52
},
{
"text": "us to write declarative views that will\nreact to changes in data automatically.",
"start": 900.97,
"duration": 4.716
},
{
"text": "It allows us to abstract complex\nproblems into smaller components,",
"start": 905.686,
"duration": 3.754
},
{
"text": "and it allows us to write simple\ncode that still perform it.",
"start": 909.44,
"duration": 3.29
},
{
"text": "And so I use this word in the\nfirst bullet, declarative.",
"start": 912.73,
"duration": 2.91
},
{
"text": "So what the heck does that mean?",
"start": 915.64,
"duration": 2.5
},
{
"text": "So in CS50, we learned a paradigm\nof coding called imperative.",
"start": 918.14,
"duration": 5.93
},
{
"text": "And today we're going to talk\nabout declarative coding.",
"start": 924.07,
"duration": 3.15
},
{
"text": "So the difference in\nimperative and declarative",
"start": 927.22,
"duration": 2.58
},
{
"text": "is like asking the difference\nbetween how you do something",
"start": 929.8,
"duration": 3.33
},
{
"text": "and actually what you want out of it.",
"start": 933.13,
"duration": 3.21
},
{
"text": "So imperative programming\noutlines a series of steps",
"start": 936.34,
"duration": 3.27
},
{
"text": "to get to what you want,\nwhereas declarative,",
"start": 939.61,
"duration": 2.94
},
{
"text": "you just say what you want.",
"start": 942.55,
"duration": 2.2
},
{
"text": "And it's just an implementation\ndetail on how to get it.",
"start": 944.75,
"duration": 4.07
},
{
"text": "And so we've learned a few different\nlanguages through CS50 in this course.",
"start": 948.82,
"duration": 3.57
},
{
"text": "A couple that come to mind\nare HTML and JavaScript.",
"start": 952.39,
"duration": 4.08
},
{
"text": "So in HTML, do we tell\nthe browser exactly how",
"start": 956.47,
"duration": 3.48
},
{
"text": "we want to render all of these things?",
"start": 959.95,
"duration": 3.24
},
{
"text": "Do we tell it exactly how we\nwant the DOM to be constructed?",
"start": 963.19,
"duration": 4.26
},
{
"text": "No, we just tell it what we want.",
"start": 967.45,
"duration": 2.61
},
{
"text": "And so HTML is considered a declarative\nlanguage, because you just say,",
"start": 970.06,
"duration": 3.9
},
{
"text": "hey, I want this.",
"start": 973.96,
"duration": 0.81
},
{
"text": "And browsers are in charge of\njust giving you what you want.",
"start": 974.77,
"duration": 5.69
},
{
"text": "Rather, with JavaScript, as you'll\nsee in your first project, when",
"start": 980.46,
"duration": 2.95
},
{
"text": "you want to do anything to\nthe DOM with JavaScript,",
"start": 983.41,
"duration": 2.4
},
{
"text": "you tell it, oh, first\nget me a new element.",
"start": 985.81,
"duration": 3.57
},
{
"text": "Call it a div.",
"start": 989.38,
"duration": 1.41
},
{
"text": "Then do this.",
"start": 990.79,
"duration": 1.5
},
{
"text": "Then maybe append it to the tree.",
"start": 992.29,
"duration": 1.65
},
{
"text": "Then maybe add a class to it.",
"start": 993.94,
"duration": 1.208
},
{
"text": "Maybe give it some inner HTML.",
"start": 995.148,
"duration": 2.362
},
{
"text": "And so you're telling it exactly\nwhat you want and how to do it.",
"start": 997.51,
"duration": 2.75
},
{
"text": "And so that is the more\nimperative way of programming.",
"start": 1000.26,
"duration": 4.018
},
{
"text": "So let's take this into an example.",
"start": 1007.35,
"duration": 1.9
},
{
"text": "Say we had a classical\nguitar here, and say we",
"start": 1009.25,
"duration": 2.95
},
{
"text": "wanted to actually create this guitar.",
"start": 1012.2,
"duration": 3.75
},
{
"text": "So in an imperative way,\nhow would you describe that?",
"start": 1015.95,
"duration": 3.43
},
{
"text": "Well, you would say, oh,\nI need a head over here.",
"start": 1019.38,
"duration": 3.41
},
{
"text": "I need to add some pegs to it.",
"start": 1022.79,
"duration": 1.26
},
{
"text": "Maybe I want the neck.",
"start": 1024.05,
"duration": 1.47
},
{
"text": "Maybe I add some frets to that.",
"start": 1025.52,
"duration": 2.085
},
{
"text": "Oh, I need to create the body and attach\nthem all and then maybe return that.",
"start": 1027.605,
"duration": 4.415
},
{
"text": "And what would be a more declarative\nway of creating the guitar?",
"start": 1032.02,
"duration": 3.16
},
{
"text": "You just say, I want a guitar.",
"start": 1037.96,
"duration": 1.8
},
{
"text": "Maybe tune the strings to this.",
"start": 1039.76,
"duration": 1.319
},
{
"text": "And so an example in pseudo\ncode would be like this.",
"start": 1041.079,
"duration": 8.091
},
{
"text": "So say we have a guitar, and\nsay we have some function",
"start": 1049.17,
"duration": 2.92
},
{
"text": "called create element, similar to\nwhat we have in the document in HTML.",
"start": 1052.09,
"duration": 5.86
},
{
"text": "And say we know exactly\nwhat strings we want.",
"start": 1057.95,
"duration": 2.3
},
{
"text": "How might we go about\ncreating this guitar?",
"start": 1060.25,
"duration": 3.45
},
{
"text": "Well, first we might want to do\nsomething like let's create a head.",
"start": 1063.7,
"duration": 3.24
},
{
"text": "Again, telling whoever's\nlistening exactly what we want.",
"start": 1073.15,
"duration": 4.7
},
{
"text": "And then maybe for 6 pegs, maybe we\nwant to start adding pegs to that head.",
"start": 1077.85,
"duration": 11.63
},
{
"text": "And so now we, in a very terse\nmanner, have a head with six pegs.",
"start": 1101.27,
"duration": 6.31
},
{
"text": "And so we might do the\nexact same thing with neck.",
"start": 1107.58,
"duration": 3.11
},
{
"text": "And maybe we want to add\nsomething like 19 frets.",
"start": 1126.46,
"duration": 2.775
},
{
"text": "And you can see how this gets a little\nbit annoying to write it, right?",
"start": 1133.67,
"duration": 4.65
},
{
"text": "And maybe we do the\nsame thing with body,",
"start": 1138.32,
"duration": 2.61
},
{
"text": "but we have some sort of for\neach loop with the strings,",
"start": 1140.93,
"duration": 9.39
},
{
"text": "and we pass in the tone that we want.",
"start": 1150.32,
"duration": 3.881
},
{
"text": "And then we go ahead and create a\nstring and tune it to that tone.",
"start": 1154.201,
"duration": 2.749
},
{
"text": "And then we go ahead and\nadd that to the body.",
"start": 1165.604,
"duration": 1.916
},
{
"text": "And you see how this gets very\nstep-by-step, exactly what we",
"start": 1177.93,
"duration": 4.371
},
{
"text": "want to do to create a guitar.",
"start": 1182.301,
"duration": 1.249
},
{
"text": "And this is exactly what we've been\ndoing thus far for writing to the DOM.",
"start": 1188.19,
"duration": 4.11
},
{
"text": "And so how might we do this\nin a more declarative manner?",
"start": 1195.265,
"duration": 2.375
},
{
"text": "Well, we would just\nsay, give me a guitar.",
"start": 1201.38,
"duration": 3.995
},
{
"text": "Give me a string.",
"start": 1208.58,
"duration": 1.57
},
{
"text": "Maybe I want it to be tuned to\nthe first note in the string.",
"start": 1210.15,
"duration": 7.43
},
{
"text": "And maybe copy that a few times.",
"start": 1221.382,
"duration": 1.333
},
{
"text": "And there we go.",
"start": 1229.04,
"duration": 2.4
},
{
"text": "A better way to do this would be\nrather than hard coding these,",
"start": 1231.44,
"duration": 3.57
},
{
"text": "maybe we just do strings.map, and\nfor each note, we stick it in there.",
"start": 1235.01,
"duration": 11.47
},
{
"text": "So it looks like there's\na little bug here.",
"start": 1249.559,
"duration": 1.791
},
{
"text": "I used string even though I declared\nthe variable called strings.",
"start": 1251.35,
"duration": 5.97
},
{
"text": "So if we map over the\narray called strings,",
"start": 1257.32,
"duration": 2.73
},
{
"text": "and for each note return a\nstring where the note is note,",
"start": 1260.05,
"duration": 3.45
},
{
"text": "then we have declaratively\nwritten a guitar.",
"start": 1263.5,
"duration": 3.386
},
{
"text": "Does this make sense?",
"start": 1266.886,
"duration": 0.874
},
{
"text": "So a great thing about react\nis that the way that you code",
"start": 1275.58,
"duration": 2.76
},
{
"text": "is in a very, very declarative manner.",
"start": 1278.34,
"duration": 3.9
},
{
"text": "The browser APIs aren't\nsuper fun to work with.",
"start": 1282.24,
"duration": 2.43
},
{
"text": "You get to work with them a bit\nin project zero, but react just",
"start": 1284.67,
"duration": 3.96
},
{
"text": "allows us to write exactly what we want.",
"start": 1288.63,
"duration": 1.68
},
{
"text": "And the library will actually take\ncare of the DOM manipulation for us.",
"start": 1290.31,
"duration": 3.92
},
{
"text": "And so what does that really look like?",
"start": 1294.23,
"duration": 1.75
},
{
"text": "So say we wanted to create a slide here.",
"start": 1303.97,
"duration": 3.825
},
{
"text": "Say we wanted to use the\nnative DOM manipulation",
"start": 1307.795,
"duration": 4.685
},
{
"text": "API in order to create the slide here.",
"start": 1312.48,
"duration": 2.41
},
{
"text": "So we might have a slide\nelement that we created",
"start": 1314.89,
"duration": 3.14
},
{
"text": "and say we wanted to add a H1 to that.",
"start": 1318.03,
"duration": 2.66
},
{
"text": "Add a title to it.",
"start": 1320.69,
"duration": 0.76
},
{
"text": "How might we do it?",
"start": 1321.45,
"duration": 1.26
},
{
"text": "Well, we'd have to do something like\nconst title = document.createElement.",
"start": 1322.71,
"duration": 4.514
},
{
"text": "Get an H1.",
"start": 1331.18,
"duration": 2.43
},
{
"text": "And then start adding to that.",
"start": 1333.61,
"duration": 1.25
},
{
"text": "We can do title.innerHTML\nis equal to the SLIDE.title.",
"start": 1334.86,
"duration": 5.23
},
{
"text": "And you see how this starts getting\nto like what we've been doing earlier,",
"start": 1344.19,
"duration": 3.36
},
{
"text": "where you say exactly what you want,\nbut it might take you a long time to do.",
"start": 1347.55,
"duration": 5.501
},
{
"text": "So in react land, this\nis actually a lot easier.",
"start": 1353.051,
"duration": 1.999
},
{
"text": "So how might we do this if we were\ndoing this completely declaratively?",
"start": 1358.84,
"duration": 4.4
},
{
"text": "Well, we just say exactly what we want.",
"start": 1363.24,
"duration": 7.57
},
{
"text": "We want a slide.",
"start": 1370.81,
"duration": 2.09
},
{
"text": "Maybe it has a title, where the\ntitle is equal to the slide's title.",
"start": 1372.9,
"duration": 5.565
},
{
"text": "Maybe we have some bullets.",
"start": 1381.27,
"duration": 2.619
},
{
"text": "Or we can just map through the\nbullets that we have up there.",
"start": 1383.889,
"duration": 2.541
},
{
"text": "We can do SLIDE.bullets.map.",
"start": 1386.43,
"duration": 2.01
},
{
"text": "And we can say for every bullet,\njust give me a list item.",
"start": 1392.41,
"duration": 5.75
},
{
"text": "And maybe we should wrap those\nlist items in unordered list.",
"start": 1409.451,
"duration": 2.499
},
{
"text": "And maybe instead of using\nthis, we can do an H1 here.",
"start": 1416.17,
"duration": 2.25
},
{
"text": "So see how this is a\nlot more declarative?",
"start": 1431.07,
"duration": 2.32
},
{
"text": "Makes sense, right?",
"start": 1441.73,
"duration": 1.17
},
{
"text": "It just makes sense.",
"start": 1442.9,
"duration": 1.51
},
{
"text": "It's easier to read, and\nit's easier to maintain.",
"start": 1444.41,
"duration": 3.02
},
{
"text": "So another great thing about react\nis it's very easily componentized.",
"start": 1450.552,
"duration": 4.475
},
{
"text": "What do I mean by componentized?",
"start": 1455.027,
"duration": 1.333
},
{
"text": "Well, it's a process by which you break\na very complex problem into a bunch",
"start": 1456.36,
"duration": 3.48
},
{
"text": "of different, discrete components.",
"start": 1459.84,
"duration": 2.746
},
{
"text": "You can reuse these components,\nwhich is great for consistency.",
"start": 1462.586,
"duration": 2.624
},
{
"text": "So say you had a bunch\nof slides, and you wanted",
"start": 1465.21,
"duration": 2.31
},
{
"text": "to just change how every title looked.",
"start": 1467.52,
"duration": 2.735
},
{
"text": "Well, if you did this using\nnative DOM manipulation,",
"start": 1470.255,
"duration": 2.995
},
{
"text": "that might be a bunch of different\nlines of code that you have to change.",
"start": 1473.25,
"duration": 3.41
},
{
"text": "But if you did this\nusing React components,",
"start": 1476.66,
"duration": 2.787
},
{
"text": "it might just be one line\nthat you have to change,",
"start": 1479.447,
"duration": 2.083
},
{
"text": "and then it's applied to every\nsingle slide that you have.",
"start": 1481.53,
"duration": 2.922
},
{
"text": "It's also great for iteration\nspeed, because then you",
"start": 1484.452,
"duration": 2.208
},
{
"text": "can just reuse the\ncomponents over and over,",
"start": 1486.66,
"duration": 1.833
},
{
"text": "rather than having to cut and\npaste code all over the place.",
"start": 1488.493,
"duration": 3.438
},
{
"text": "Another great thing\nabout these components",
"start": 1491.931,
"duration": 1.749
},
{
"text": "is that React's declarative nature makes\nit very easy to customize components.",
"start": 1493.68,
"duration": 4.74
},
{
"text": "So say we had the last three slides\nand wanted to write them in HTML.",
"start": 1498.42,
"duration": 3.61
},
{
"text": "It might look like\nsomething like this, where",
"start": 1505.065,
"duration": 1.875
},
{
"text": "you have a div, which represents\na slide where the titles react.",
"start": 1506.94,
"duration": 5.72
},
{
"text": "You notice these are the same\nthree bullets as previously.",
"start": 1512.66,
"duration": 2.8
},
{
"text": "We have the declarative slide, where\nwe have the same bullets as previously.",
"start": 1515.46,
"duration": 5.015
},
{
"text": "And say we wanted to change this.",
"start": 1520.475,
"duration": 1.375
},
{
"text": "Say we wanted to make all of the\ntitles have a slightly different style.",
"start": 1521.85,
"duration": 5.28
},
{
"text": "Well, how might we do that?",
"start": 1527.13,
"duration": 1.71
},
{
"text": "Well, we'd have to edit this line.",
"start": 1528.84,
"duration": 1.77
},
{
"text": "We'd have to go edit this line.",
"start": 1530.61,
"duration": 1.86
},
{
"text": "Maybe down there, there's another\ntitle that we have to change.",
"start": 1532.47,
"duration": 2.73
},
{
"text": "Or what if we wanted to even change the\nstructure of how we represent bullets?",
"start": 1535.2,
"duration": 4.47
},
{
"text": "Maybe rather than using\nan unordered list,",
"start": 1539.67,
"duration": 2.72
},
{
"text": "we might use an unordered list with\na div that wraps the list items.",
"start": 1542.39,
"duration": 4.43
},
{
"text": "Well, in order to do that, that would\nbe a lot of code you have to change.",
"start": 1546.82,
"duration": 3.83
},
{
"text": "But imagine that we had broken this up\ninto a bunch of different components.",
"start": 1550.65,
"duration": 3.66
},
{
"text": "Where might it make\nsense to break it up?",
"start": 1554.31,
"duration": 2.99
},
{
"text": "Well, you see us repeating code\nthat looks pretty much the same",
"start": 1557.3,
"duration": 4.042
},
{
"text": "over here, right?",
"start": 1561.342,
"duration": 0.708
},
{
"text": "So maybe that should be broken\nup into a separate component.",
"start": 1562.05,
"duration": 3.3
},
{
"text": "So how might we go about doing that?",
"start": 1565.35,
"duration": 1.65
},
{
"text": "Well, first we have to extract the\ninformation, and so in this array here,",
"start": 1571.05,
"duration": 4.61
},
{
"text": "we have the slides where\nI've basically just ripped",
"start": 1575.66,
"duration": 3.27
},
{
"text": "out the information for each slide.",
"start": 1578.93,
"duration": 4.96
},
{
"text": "And so now let's go ahead\nand implement that slideshow.",
"start": 1583.89,
"duration": 3.652
},
{
"text": "And so in order to do\nthat, we might have",
"start": 1587.542,
"duration": 1.708
},
{
"text": "something like where we have a\nslide, which might take an array,",
"start": 1589.25,
"duration": 10.52
},
{
"text": "or it might take an object.",
"start": 1599.77,
"duration": 1.26
},
{
"text": "Let's call it a slide.",
"start": 1601.03,
"duration": 2.916
},
{
"text": "And what will it return?",
"start": 1603.946,
"duration": 2.454
},
{
"text": "Well, it should return a div.",
"start": 1606.4,
"duration": 3.597
},
{
"text": "Maybe inside it we have that H1.",
"start": 1609.997,
"duration": 1.333
},
{
"text": "Then maybe we have that unordered list.",
"start": 1615.16,
"duration": 1.64
},
{
"text": "And then go ahead and close that div.",
"start": 1619.359,
"duration": 1.541
},
{
"text": "So what goes in the H1 here?",
"start": 1626.26,
"duration": 2.37
},
{
"text": "Well, maybe we should\nhave that slide's title.",
"start": 1628.63,
"duration": 5.31
},
{
"text": "And what about generating those bullets?",
"start": 1633.94,
"duration": 2.1
},
{
"text": "Well, like we did in\nthe previous example,",
"start": 1636.04,
"duration": 1.75
},
{
"text": "we might want to do the\nslide.bullets and map [? overflows ?]",
"start": 1637.79,
"duration": 4.01
},
{
"text": "to create those list items.",
"start": 1641.8,
"duration": 2.16
},
{
"text": "So for each bullet, just go\nahead and create a list item",
"start": 1643.96,
"duration": 6.196
},
{
"text": "where you have that bullet.",
"start": 1650.156,
"duration": 1.124
},
{
"text": "And so now we declared\nexactly what a slide is.",
"start": 1657.25,
"duration": 2.48
},
{
"text": "Well, a slide takes in\nan object representing",
"start": 1659.73,
"duration": 2.79
},
{
"text": "the slide in the same shape\nof the object up here.",
"start": 1662.52,
"duration": 6.03
},
{
"text": "And then it just returns the same\nHTML that we had in the other example.",
"start": 1668.55,
"duration": 3.81
},
{
"text": "It's a div that wraps\nin H1, where we stick",
"start": 1672.36,
"duration": 2.04
},
{
"text": "in the title, an unordered\nlist that has some list",
"start": 1674.4,
"duration": 3.06
},
{
"text": "items that represent those bullets.",
"start": 1677.46,
"duration": 2.409
},
{
"text": "And now how might we\ncreate that slideshow?",
"start": 1679.869,
"duration": 1.791
},
{
"text": "Well, maybe we just do for\neach slot in the slides,",
"start": 1688.45,
"duration": 7.08
},
{
"text": "maybe we have an outer-wrapping\ndiv that maps through the slides.",
"start": 1695.53,
"duration": 7.44
},
{
"text": "And for each slide, we actually\njust use the slide down there.",
"start": 1705.79,
"duration": 5.23
},
{
"text": "So now we're going to get ahead\nand use React and generate",
"start": 1724.29,
"duration": 5.36
},
{
"text": "basically the same HTML as\nwe had in the previous one.",
"start": 1729.65,
"duration": 3.12
},
{
"text": "But now say we wanted to change\nthe styling of the title.",
"start": 1732.77,
"duration": 3.33
},
{
"text": "How would we do that?",
"start": 1736.1,
"duration": 1.48
},
{
"text": "Well, that's just right here.",
"start": 1737.58,
"duration": 2.33
},
{
"text": "What about if we wanted to\nchange the structure of how",
"start": 1739.91,
"duration": 2.25
},
{
"text": "we created those bullets?",
"start": 1742.16,
"duration": 2.64
},
{
"text": "In the previous example, if we wanted\nto wrap the list items in a div,",
"start": 1744.8,
"duration": 3.469
},
{
"text": "it would be many, many lines of code.",
"start": 1748.269,
"duration": 1.541
},
{
"text": "But here, it's just\nyou stick a div here,",
"start": 1749.81,
"duration": 2.79
},
{
"text": "because this is basically an\nabstract component that says,",
"start": 1752.6,
"duration": 3.18
},
{
"text": "if we wanted to create a slide,\nhere's exactly how we do it.",
"start": 1755.78,
"duration": 2.52
},
{
"text": "Just make sure to pass me a\nslide of the correct shape.",
"start": 1758.3,
"duration": 4.2
},
{
"text": "And so the React paradigm is\nto take a very complex problem",
"start": 1762.5,
"duration": 3.3
},
{
"text": "and break it down into\nsmall chunks like this",
"start": 1765.8,
"duration": 2.28
},
{
"text": "that each solve a discrete\nproblem in that UI.",
"start": 1768.08,
"duration": 4.29
},
{
"text": "Any questions so far?",
"start": 1772.37,
"duration": 1.23
},
{
"text": "Cool.",
"start": 1781.3,
"duration": 0.8
},
{
"text": "So another great thing is\nthat React is very performant.",
"start": 1782.1,
"duration": 3.29
},
{
"text": "We just write what we\nwant, and React will",
"start": 1785.39,
"duration": 1.75
},
{
"text": "do the hard work of\nplaying with the DOM,",
"start": 1787.14,
"duration": 2.67
},
{
"text": "or changing things so\nthat what we have written",
"start": 1789.81,
"duration": 3.33
},
{
"text": "matches with what we\nhave showing on the page.",
"start": 1793.14,
"duration": 3.34
},
{
"text": "And so the way it does this is through\nan algorithm called reconciliation.",
"start": 1796.48,
"duration": 7.1
},
{
"text": "And this is the process by which\nReact syncs any changes in app state",
"start": 1803.58,
"duration": 3.06
},
{
"text": "to the DOM.",
"start": 1806.64,
"duration": 1.92
},
{
"text": "So what's great about\nReact is that it actually",
"start": 1808.56,
"duration": 2.64
},
{
"text": "maintains what's called a virtual DOM.",
"start": 1811.2,
"duration": 2.67
},
{
"text": "And so the slow thing\nabout playing with the DOM",
"start": 1813.87,
"duration": 3.3
},
{
"text": "is that anytime you destroy elements\nor create new elements, that",
"start": 1817.17,
"duration": 3.45
},
{
"text": "takes a relatively long amount of time.",
"start": 1820.62,
"duration": 3.33
},
{
"text": "And so the way that React\nsays, maybe we should do this",
"start": 1823.95,
"duration": 3.36
},
{
"text": "better by rather than\nchanging stuff in the DOM",
"start": 1827.31,
"duration": 2.779
},
{
"text": "every time we want to change something,\nwhat if we just stored everything",
"start": 1830.089,
"duration": 3.041
},
{
"text": "in memory?",
"start": 1833.13,
"duration": 1.009
},
{
"text": "And then with any\nchanges, we can just say,",
"start": 1834.139,
"duration": 1.791
},
{
"text": "hey, how does this differ\nfrom what's shown on the page?",
"start": 1835.93,
"duration": 2.53
},
{
"text": "And only change what's necessary.",
"start": 1838.46,
"duration": 2.03
},
{
"text": "And so that's what's done in\nthe reconciliation process.",
"start": 1840.49,
"duration": 3.03
},
{
"text": "So first, anytime your\ndata changes, React",
"start": 1843.52,
"duration": 3.11
},
{
"text": "will reconstruct that virtual DOM.",
"start": 1846.63,
"duration": 3.12
},
{
"text": "Then it will diff that DOM against\nwhat's actually there in the real DOM.",
"start": 1849.75,
"duration": 6.12
},
{
"text": "And it'll make only the\nchanges that are needed.",
"start": 1855.87,
"duration": 2.784
},
{
"text": "Of course, there is an asterisk\nthat goes along with that,",
"start": 1858.654,
"duration": 2.416
},
{
"text": "but in your mental model,\nyou can just think of it",
"start": 1861.07,
"duration": 2.083
},
{
"text": "as React will just only make\nthe changes as necessary.",
"start": 1863.153,
"duration": 5.377
},
{
"text": "So how the heck do we write React?",
"start": 1868.53,
"duration": 3.0
},
{
"text": "Well, there's this\nthing called JSX, which",
"start": 1871.53,
"duration": 2.46
},
{
"text": "is short for JavaScript and XML, which\nis basically just an XML-like syntax",
"start": 1873.99,
"duration": 4.44
},
{
"text": "extension of JavaScript.",
"start": 1878.43,
"duration": 2.349
},
{
"text": "It's great because it just\ntranspiles directly to JavaScript.",
"start": 1880.779,
"duration": 2.541
},
{
"text": "So as we talked about\nin an earlier lecture,",
"start": 1883.32,
"duration": 2.7
},
{
"text": "the paradigm now is to rather than just\nwriting JavaScript by hand, using ES5",
"start": 1886.02,
"duration": 4.26
},
{
"text": "directly, we can write\nin these other languages",
"start": 1890.28,
"duration": 2.31
},
{
"text": "that just transpile back to JavaScript.",
"start": 1892.59,
"duration": 2.64
},
{
"text": "So JSX is an extension to that\nJavaScript syntax, which will just",
"start": 1895.23,
"duration": 3.63
},
{
"text": "compile back down into JavaScript.",
"start": 1898.86,
"duration": 2.7
},
{
"text": "And so tags defined in JSX, XML looks\nexactly like HTML with the tags.",
"start": 1904.99,
"duration": 6.06
},
{
"text": "Lowercase tags are treated\nas HTML or SVG tags,",
"start": 1911.05,
"duration": 3.69
},
{
"text": "whereas things uppercase are\ntreated as our custom components.",
"start": 1914.74,
"duration": 4.136
},
{
"text": "And what the heck is a component?",
"start": 1918.876,
"duration": 1.374
},
{
"text": "Well, a component is just a function,\na function that returns a node.",
"start": 1920.25,
"duration": 3.387
},
{
"text": "What's a node?",
"start": 1923.637,
"duration": 0.583
},
{
"text": "It's something that React can render.",
"start": 1924.22,
"duration": 2.04
},
{
"text": "For example, like a div or a\nspan or a string or a number.",
"start": 1926.26,
"duration": 3.99
},
{
"text": "And components will receive\nan object of properties",
"start": 1933.31,
"duration": 3.51
},
{
"text": "that are passed to this element.",
"start": 1936.82,
"duration": 2.25
},
{
"text": "So going back to the example\nthat we were doing before,",
"start": 1939.07,
"duration": 3.15
},
{
"text": "see how one, we declared a\nconst called slide down here.",
"start": 1942.22,
"duration": 5.24
},
{
"text": "And notice how I\nuppercased that S. That was",
"start": 1947.46,
"duration": 2.35
},
{
"text": "fully intentional, because in\nJSX, a tag with a capital S gets",
"start": 1949.81,
"duration": 5.22
},
{
"text": "invoked like a function.",
"start": 1955.03,
"duration": 1.44
},
{
"text": "And what does it get invoked with?",
"start": 1956.47,
"duration": 1.68
},
{
"text": "Well, anything you\npass it as properties.",
"start": 1958.15,
"duration": 2.189
},
{
"text": "So if you remember properties\nfrom HTML, you just",
"start": 1960.339,
"duration": 2.041
},
{
"text": "pass them in line with\nthe key and a value pair.",
"start": 1962.38,
"duration": 3.57
},
{
"text": "In React, you do the exact\nsame thing, and it actually",
"start": 1965.95,
"duration": 2.25
},
{
"text": "gets passed as an object to the\ncomponent that you described.",
"start": 1968.2,
"duration": 4.549
},
{
"text": "And so now to talk about props.",
"start": 1977.8,
"duration": 1.544
},
{
"text": "Well, props are just past as\nan object through a component",
"start": 1979.344,
"duration": 2.416
},
{
"text": "and used to compute the return node.",
"start": 1981.76,
"duration": 1.5
},
{
"text": "So basically, the object that we were\njust referring to in this example,",
"start": 1983.26,
"duration": 3.84
},
{
"text": "we called it a slide.",
"start": 1987.1,
"duration": 2.08
},
{
"text": "In React convention, we always call\nthat props, short for properties,",
"start": 1989.18,
"duration": 3.41
},
{
"text": "like those HTML properties.",
"start": 1992.59,
"duration": 4.26
},
{
"text": "And any change to these props will cause\na recomputation of that return node,",
"start": 1996.85,
"duration": 4.35
},
{
"text": "or in React vocab, a re-render.",
"start": 2001.2,
"duration": 5.2
},
{
"text": "And so unlike in HTML, these\ncan be any JavaScript value.",
"start": 2006.4,
"duration": 2.97
},
{
"text": "So if you remember back to HTML,\nwhen you declare a property,",
"start": 2009.37,
"duration": 3.37
},
{
"text": "it has to be a string.",
"start": 2012.74,
"duration": 1.27
},
{
"text": "It might be JavaScript,\nlike a JavaScript function,",
"start": 2014.01,
"duration": 4.44
},
{
"text": "but really it's just a string.",
"start": 2018.45,
"duration": 2.05
},
{
"text": "And so you can't pass any complex\nobjects or a date, object, classes,",
"start": 2020.5,
"duration": 5.75
},
{
"text": "instances, and stuff like that.",
"start": 2026.25,
"duration": 2.07
},
{
"text": "But in React, we can pass anything.",
"start": 2028.32,
"duration": 1.62
},
{
"text": "So notice here, slide is an\nobject as declared in the array",
"start": 2029.94,
"duration": 5.864
},
{
"text": "that we have up there.",
"start": 2035.804,
"duration": 0.916
},
{
"text": "And we're just passing\nthe object over there.",
"start": 2036.72,
"duration": 2.88
},
{
"text": "And the way that we pass\nvalues in JavaScript",
"start": 2039.6,
"duration": 2.22
},
{
"text": "is we wrap them in single\ncurlies, which means,",
"start": 2041.82,
"duration": 3.53
},
{
"text": "hey, execute this as a JavaScript.",
"start": 2045.35,
"duration": 2.55
},
{
"text": "Does that makes sense?",
"start": 2051.594,
"duration": 0.916
},
{
"text": "So let's actually use\na live example of this.",
"start": 2056.88,
"duration": 3.5
},
{
"text": "So there's this website\ncalled codesandbox.io,",
"start": 2060.38,
"duration": 2.58
},
{
"text": "which is linked on the\nwebsite in the Resources tab,",
"start": 2062.96,
"duration": 4.44
},
{
"text": "and this allows us to write React\nand actually render it live.",
"start": 2067.4,
"duration": 4.5
},
{
"text": "And so can everybody read this?",
"start": 2071.9,
"duration": 5.16
},
{
"text": "So basically, what's on the left\nover there is code that gets executed",
"start": 2081.0,
"duration": 3.719
},
{
"text": "and runs live on the right side here.",
"start": 2084.719,
"duration": 1.661
},
{
"text": "So if we started editing this, you see\nit applied directly on the right side",
"start": 2086.38,
"duration": 6.85
},
{
"text": "there.",
"start": 2093.23,
"duration": 2.32
},
{
"text": "And so you see we have\na const called app,",
"start": 2095.55,
"duration": 2.862
},
{
"text": "which doesn't care\nabout any arguments it",
"start": 2098.412,
"duration": 1.708
},
{
"text": "receives and just renders\nthe static HTML here.",
"start": 2100.12,
"duration": 6.12
},
{
"text": "So what if we actually\nwanted to pass it a prop?",
"start": 2106.24,
"duration": 2.38
},
{
"text": "So say we passed it something like\na count and passed it the number 1.",
"start": 2108.62,
"duration": 7.43
},
{
"text": "Again, we use curly braces there to\nrepresent this is JavaScript coming,",
"start": 2116.05,
"duration": 5.07
},
{
"text": "and the JavaScript is just the number 1.",
"start": 2121.12,
"duration": 3.06
},
{
"text": "So how might we go ahead\nand render that in the app?",
"start": 2124.18,
"duration": 4.23
},
{
"text": "Well, first, we need to actually\npay attention to the object",
"start": 2128.41,
"duration": 4.016
},
{
"text": "that we receive here.",
"start": 2132.426,
"duration": 0.874
},
{
"text": "And second, we can just\ndo it directly in line.",
"start": 2136.86,
"duration": 3.96
},
{
"text": "And so we use those curly\nbraces to represent,",
"start": 2140.82,
"duration": 3.9
},
{
"text": "hey, here comes some JavaScript.",
"start": 2144.72,
"duration": 2.13
},
{
"text": "And we can just use\nprops.count, and now we'll",
"start": 2146.85,
"duration": 5.23
},
{
"text": "go ahead and grab the prop with\nthe key [? call ?] account.",
"start": 2152.08,
"duration": 7.08
},
{
"text": "And so I mentioned that as props\nchange, React gets re-rendered.",
"start": 2159.16,
"duration": 3.57
},
{
"text": "So how might we do that?",
"start": 2162.73,
"duration": 1.0
},
{
"text": "Well, we can just wrap this in a set\ntimeout, or a set interval, rather.",
"start": 2166.81,
"duration": 4.72
},
{
"text": "And every 1,000 milliseconds\nor 1,000 milliseconds,",
"start": 2175.53,
"duration": 5.07
},
{
"text": "let's go ahead and pass it not\njust one, but the count plus plus,",
"start": 2180.6,
"duration": 7.08
},
{
"text": "where count starts at 0.",
"start": 2187.68,
"duration": 5.13
},
{
"text": "And every time this is\ninvoked, it increments.",
"start": 2192.81,
"duration": 6.86
},
{
"text": "And so down here, we're\nsaying, sudden interval,",
"start": 2202.96,
"duration": 2.2
},
{
"text": "which means run some\nfunction every n seconds.",
"start": 2205.16,
"duration": 2.42
},
{
"text": "We set n to equal 1,000\nmilliseconds, or one second there.",
"start": 2207.58,
"duration": 4.57
},
{
"text": "And every time we want\nto render this function",
"start": 2212.15,
"duration": 2.66
},
{
"text": "called app with a count of one\nmore than it was last time.",
"start": 2214.81,
"duration": 5.091
},
{
"text": "And so we start at 0,\nand every second you",
"start": 2219.901,
"duration": 1.749
},
{
"text": "see we pass a new prop\ncount incremented.",
"start": 2221.65,
"duration": 5.91
},
{
"text": "And even though we're not really doing\nanything actively in the app here,",
"start": 2227.56,
"duration": 4.77
},
{
"text": "we wrote this in a very\ndeclarative nature, right?",
"start": 2232.33,
"duration": 2.36
},
{
"text": "We said, for any props that you give\nme, just render a div with an H2 where",
"start": 2234.69,
"duration": 6.43
},
{
"text": "the H2 is the value of the props.",
"start": 2241.12,
"duration": 1.807
},
{
"text": "See how that's very declarative.",
"start": 2242.927,
"duration": 1.333
},
{
"text": "We don't say, oh, first\nyou have to grab the count,",
"start": 2244.26,
"duration": 3.749
},
{
"text": "then check to see if it is\ndifferent than the last one,",
"start": 2248.009,
"duration": 2.291
},
{
"text": "and then append this to the DOM.",
"start": 2250.3,
"duration": 1.333
},
{
"text": "We just say, hey, this is what we want.",
"start": 2251.633,
"duration": 1.637
},
{
"text": "And React will give it to us.",
"start": 2253.27,
"duration": 3.12
},
{
"text": "Any questions on React\nor props thus far?",
"start": 2256.39,
"duration": 4.71
},
{
"text": "Yeah?",
"start": 2261.1,
"duration": 0.695
},
{
"text": "AUDIENCE: [INAUDIBLE]",
"start": 2261.795,
"duration": 3.805
},
{
"text": "JORDAN HAYASHI: Yes, of course.",
"start": 2265.6,
"duration": 1.71
},
{
"text": "So set interval requires two arguments,\nwhere the first one is a function.",
"start": 2267.31,
"duration": 15.0
},
{
"text": "So the question was can you go\nover the arrow notation here?",
"start": 2282.31,
"duration": 5.29
},
{
"text": "And so set interval\nexpects two arguments.",
"start": 2287.6,
"duration": 2.91
},
{
"text": "One is a function that it\nwill invoke with no arguments,",
"start": 2290.51,
"duration": 3.98
},
{
"text": "and so I'm just declaring an anonymous\nfunction here with no arguments.",
"start": 2294.49,
"duration": 3.63
},
{
"text": "So I'm saying, here is a\nfunction that takes no arguments,",
"start": 2298.12,
"duration": 3.085
},
{
"text": "and what should I do?",
"start": 2301.205,
"duration": 0.875
},
{
"text": "Well, I should render this.",
"start": 2302.08,
"duration": 1.71
},
{
"text": "I could have also written this as\nthis, and it would have functionally",
"start": 2303.79,
"duration": 12.297
},
{
"text": "been the exact same.",
"start": 2316.087,
"duration": 0.833
},
{
"text": "Does that makes sense?",
"start": 2328.614,
"duration": 0.916
},
{
"text": "Any other questions on React or props?",
"start": 2332.27,
"duration": 2.7
},
{
"text": "Yeah?",
"start": 2334.97,
"duration": 0.593
},
{
"text": "AUDIENCE: So in line 10 [INAUDIBLE]?",
"start": 2335.563,
"duration": 2.465
},
{
"text": "JORDAN HAYASHI: So in line 10, we\nhave braces after the arrow here.",
"start": 2344.93,
"duration": 5.72
},
{
"text": "So that's just saying, interpret\nall of this as one statement.",
"start": 2350.65,
"duration": 5.72
},
{
"text": "So something special\nabout arrow notation",
"start": 2356.37,
"duration": 2.91
},
{
"text": "is that it has an implicit return,\nwhich means we don't actually",
"start": 2359.28,
"duration": 3.48
},
{
"text": "have to write-- so we could have\nwritten the same thing as this,",
"start": 2362.76,
"duration": 9.142
},
{
"text": "as a function that takes props.",
"start": 2371.902,
"duration": 4.945
},
{
"text": "And what does it do?",
"start": 2376.847,
"duration": 0.833
},
{
"text": "It returns this.",
"start": 2377.68,
"duration": 0.75
},
{
"text": "So this actually does\nthe exact same thing.",
"start": 2391.37,
"duration": 2.529
},
{
"text": "So we're saying, app\ntwo is a function that",
"start": 2393.899,
"duration": 1.791
},
{
"text": "takes a single argument called props.",
"start": 2395.69,
"duration": 1.78
},
{
"text": "And what we do is we return this, and\nso arrow notation shorthand is just",
"start": 2397.47,
"duration": 6.2
},
{
"text": "you have your arguments in arrow.",
"start": 2403.67,
"duration": 2.19
},
{
"text": "And then if you don't have braces,\nwhich referred to it like a code block,",
"start": 2405.86,
"duration": 4.56
},
{
"text": "it just returns whatever's next.",
"start": 2410.42,
"duration": 1.65
},
{
"text": "And so we're saying, return this.",
"start": 2412.07,
"duration": 2.22
},
{
"text": "What is this?",
"start": 2414.29,
"duration": 0.93
},
{
"text": "Well, it's this div and H2.",
"start": 2415.22,
"duration": 2.32
},
{
"text": "We're going to wrap it in a parentheses\nso we know that it's just one value.",
"start": 2417.54,
"duration": 4.91
},
{
"text": "Does that make sense?",
"start": 2422.45,
"duration": 1.47
},
{
"text": "A great question.",
"start": 2423.92,
"duration": 2.56
},
{
"text": "Do you guys see how these are the same?",
"start": 2426.48,
"duration": 2.45
},
{
"text": "So this is great, but we don't really\nhave all that much power yet, right?",
"start": 2433.56,
"duration": 3.71
},
{
"text": "If we wanted to go ahead\nand change these props,",
"start": 2437.27,
"duration": 2.25
},
{
"text": "we still have to drop down to\nthis raw JavaScript over here.",
"start": 2439.52,
"duration": 4.72
},
{
"text": "So next we'll see exactly how we\ncreate apps that are stateful.",
"start": 2444.24,
"duration": 7.597
},
{
"text": "What does that mean?",
"start": 2451.837,
"duration": 0.833
},
{
"text": "Well, there's this\nnotion of state in React,",
"start": 2452.67,
"duration": 2.95
},
{
"text": "and state is basically an\ninternally managed configuration",
"start": 2455.62,
"duration": 3.37
},
{
"text": "for any component.",
"start": 2458.99,
"duration": 3.36
},
{
"text": "And so now components become classes,\nand this .state is a property on that",
"start": 2462.35,
"duration": 4.365
},
{
"text": "component's instance.",
"start": 2466.715,
"duration": 0.875
},
{
"text": "So how do we update the state?",
"start": 2470.66,
"duration": 1.25
},
{
"text": "Well, there's a method\ncalled this.setState,",
"start": 2471.91,
"duration": 5.04
},
{
"text": "which is implemented in this thing\ncalled a React.Component, which",
"start": 2476.95,
"duration": 2.82
},
{
"text": "we have to extend in order to\nhave access to that method.",
"start": 2479.77,
"duration": 4.86
},
{
"text": "And this goes ahead\nand changes that value.",
"start": 2484.63,
"duration": 4.38
},
{
"text": "So you can pass an object to be merged\nor a function of the previous state.",
"start": 2489.01,
"duration": 3.64
},
{
"text": "And so if we pass\nthis.setState, an object,",
"start": 2492.65,
"duration": 2.53
},
{
"text": "it will go ahead and merge that\nin with the existing state.",
"start": 2495.18,
"duration": 2.5
},
{
"text": "So if we pass it in an\nupdater function, it's",
"start": 2497.68,
"duration": 2.97
},
{
"text": "basically a function that gets run\nwhen we want to change the state.",
"start": 2500.65,
"duration": 3.51
},
{
"text": "And the set state calls are\nbatched and run asynchronously.",
"start": 2504.16,
"duration": 5.65
},
{
"text": "And of course, any change in\nstate will also cause a re-render,",
"start": 2509.81,
"duration": 2.79
},
{
"text": "because it would be silly if\nwe were to change the state",
"start": 2512.6,
"duration": 2.43
},
{
"text": "but not reflect that in the UI.",
"start": 2515.03,
"duration": 3.68
},
{
"text": "And so how might we go about\nrepresenting state over here?",
"start": 2518.71,
"duration": 3.86
},
{
"text": "So first, let me copy this\nso that we can save it.",
"start": 2522.57,
"duration": 4.0
},
{
"text": "So let's go ahead, and rather\nthan having an app be a function,",
"start": 2544.28,
"duration": 3.81
},
{
"text": "let's actually have it be a class.",
"start": 2548.09,
"duration": 2.152
},
{
"text": "So we can do class app, and we\nwant to extend React.Component.",
"start": 2553.61,
"duration": 7.38
},
{
"text": "And within that, we want to have\nthis method called render, which is",
"start": 2567.9,
"duration": 4.68
},
{
"text": "automatically invoked on a re-render.",
"start": 2572.58,
"duration": 4.58
},
{
"text": "Within render, we want to return this.",
"start": 2581.16,
"duration": 3.37
},
{
"text": "Cool.",
"start": 2604.82,
"duration": 0.5
},
{
"text": "So the way to now write\nthis is rather than having",
"start": 2607.84,
"duration": 2.82
},
{
"text": "app be a function that takes\nprops and returns something,",
"start": 2610.66,
"duration": 2.895
},
{
"text": "we're actually writing a class for app.",
"start": 2613.555,
"duration": 2.055
},
{
"text": "And so as we talked about\nearlier, classes have instances.",
"start": 2615.61,
"duration": 2.79
},
{
"text": "And React knows that when\nyou want to render something",
"start": 2618.4,
"duration": 3.3
},
{
"text": "like this, if it's a class, go ahead\nand create a new instance of that",
"start": 2621.7,
"duration": 3.6
},
{
"text": "and pass in these as props.",
"start": 2625.3,
"duration": 3.78
},
{
"text": "And notice how we don't ever\ntake the props anywhere.",
"start": 2629.08,
"duration": 2.85
},
{
"text": "That's because when we\nextend React Component,",
"start": 2631.93,
"duration": 3.0
},
{
"text": "React Component, that base class,\ngoes ahead and attaches the props",
"start": 2634.93,
"duration": 5.4
},
{
"text": "to the instance.",
"start": 2640.33,
"duration": 0.99
},
{
"text": "And so in order to get at them, rather\nthan doing props does something,",
"start": 2641.32,
"duration": 3.24
},
{
"text": "we do this dot props dot count.",
"start": 2644.56,
"duration": 2.78
},
{
"text": "So again, the props that come in,\nin the way that React.Component",
"start": 2647.34,
"duration": 3.73
},
{
"text": "is implemented, it\nautomatically takes the props",
"start": 2651.07,
"duration": 2.19
},
{
"text": "and attaches it to that\ninstance of the class.",
"start": 2653.26,
"duration": 3.13
},
{
"text": "And so in order for us to get\nthem in the render method,",
"start": 2656.39,
"duration": 2.72
},
{
"text": "we do this dot props dot count.",
"start": 2659.11,
"duration": 3.93
},
{
"text": "Does that make sense so far,\ngoing from a function to a class?",
"start": 2663.04,
"duration": 5.13
},
{
"text": "We'll talk about this in\ndepth the next lecture.",
"start": 2668.17,
"duration": 4.24
},
{
"text": "And so we talked about\nthis thing called state,",
"start": 2672.41,
"duration": 2.63
},
{
"text": "and how do we actually\ngo ahead and use that?",
"start": 2675.04,
"duration": 3.03
},
{
"text": "Well, when we want to create\nour state, we actually",
"start": 2678.07,
"duration": 2.634
},
{
"text": "do that in the constructor method.",
"start": 2680.704,
"duration": 1.416
},
{
"text": "And so the first thing that we want\nto do in our constructor method",
"start": 2687.63,
"duration": 3.2
},
{
"text": "is actually called a\nsuper, which means allow",
"start": 2690.83,
"duration": 5.61
},
{
"text": "React.Component to do\nstuff with the props",
"start": 2696.44,
"duration": 2.34
},
{
"text": "that it would have done otherwise.",
"start": 2698.78,
"duration": 2.51
},
{
"text": "And now go ahead and\ndo what we want to do.",
"start": 2701.29,
"duration": 2.244
},
{
"text": "What do we want to do?",
"start": 2703.534,
"duration": 0.916
},
{
"text": "Well, we want to initialize\nthis thing called state.",
"start": 2704.45,
"duration": 2.166
},
{
"text": "Cool.",
"start": 2718.83,
"duration": 0.5
},
{
"text": "So now we have this thing\ncalled state, and how are we",
"start": 2719.33,
"duration": 2.85
},
{
"text": "going to go ahead and update it?",
"start": 2722.18,
"duration": 1.98
},
{
"text": "Well, maybe we should have something\ncalled increase count, which",
"start": 2724.16,
"duration": 6.9
},
{
"text": "is a method on this instance.",
"start": 2731.06,
"duration": 2.962
},
{
"text": "And let's go ahead and\nincrease the count here.",
"start": 2734.022,
"duration": 1.958
},
{
"text": "So how might I do that?",
"start": 2735.98,
"duration": 1.5
},
{
"text": "Well, I should call this dot set state\nand pass in count is this dot state dot",
"start": 2737.48,
"duration": 8.25
},
{
"text": "count plus 1.",
"start": 2745.73,
"duration": 3.09
},
{
"text": "And so now we have a method on\nthis instance called increase count",
"start": 2748.82,
"duration": 3.09
},
{
"text": "that we can call.",
"start": 2751.91,
"duration": 0.81
},
{
"text": "And it should, in theory,\nincrease that count.",
"start": 2752.72,
"duration": 3.95
},
{
"text": "And so rather than referencing the\nprops down here, let's reference state.",
"start": 2756.67,
"duration": 3.154
},
{
"text": "And now it should be\n0, and it's just going",
"start": 2762.67,
"duration": 2.24
},
{
"text": "to stay at 0, because\nwe're not doing anything.",
"start": 2764.91,
"duration": 3.052
},
{
"text": "So we can go ahead and\nget rid of this interval.",
"start": 2767.962,
"duration": 2.0
},
{
"text": "And so how are we going to change that\nvalue of 0 to be something like 1?",
"start": 2778.17,
"duration": 5.87
},
{
"text": "We have to invoke that\nmethod, and so let's go ahead",
"start": 2784.04,
"duration": 2.49
},
{
"text": "and do that when we click a button.",
"start": 2786.53,
"duration": 2.35
},
{
"text": "So let's have something like\ndiv here, and within that div,",
"start": 2788.88,
"duration": 5.834
},
{
"text": "let's create a button.",
"start": 2794.714,
"duration": 0.916
},
{
"text": "And when we click that button,\nlet's go ahead and invoke this thing",
"start": 2800.72,
"duration": 5.184
},
{
"text": "called increase count.",
"start": 2805.904,
"duration": 0.916
},
{
"text": "And let's call it increase.",
"start": 2810.73,
"duration": 1.21
},
{
"text": "So now if we click this button,\nwhat do we expect to happen?",
"start": 2829.57,
"duration": 3.12
},
{
"text": "So notice how we pass\nthis thing called unclick.",
"start": 2838.27,
"duration": 2.584
},
{
"text": "What's the value of unclick?",
"start": 2840.854,
"duration": 1.166
},
{
"text": "Or what function should we invoke?",
"start": 2842.02,
"duration": 1.98
},
{
"text": "Well, we should invoke this dot\nincrease count, which should be this.",
"start": 2844.0,
"duration": 7.02
},
{
"text": "We'll see what it is in a second.",
"start": 2851.02,
"duration": 2.23
},
{
"text": "And then that should call\nthis dot set state, which",
"start": 2853.25,
"duration": 3.11
},
{
"text": "should, in theory, increase count.",
"start": 2856.36,
"duration": 1.98
},
{
"text": "So what happens?",
"start": 2858.34,
"duration": 1.55
},
{
"text": "Uh-oh!",
"start": 2859.89,
"duration": 0.76
},
{
"text": "Cannot read property\nset state of undefined.",
"start": 2860.65,
"duration": 2.26
},
{
"text": "So there's a bug here.",
"start": 2862.91,
"duration": 1.56
},
{
"text": "Can anybody spot the bug?",
"start": 2864.47,
"duration": 2.9
},
{
"text": "Cannot read property\nset state of undefined.",
"start": 2867.37,
"duration": 4.14
},
{
"text": "It even tells us exactly where that was.",
"start": 2871.51,
"duration": 3.42
},
{
"text": "So we're seeing this\ndot set state, which",
"start": 2874.93,
"duration": 2.34
},
{
"text": "I told you is a method that we can use.",
"start": 2877.27,
"duration": 3.53
},
{
"text": "But it's saying, we cannot\nread a property of undefined,",
"start": 2880.8,
"duration": 2.87
},
{
"text": "which implies that\nthis is undefined here.",
"start": 2883.67,
"duration": 3.27
},
{
"text": "Yeah?",
"start": 2886.94,
"duration": 0.65
},
{
"text": "What do you think is going on here?",
"start": 2887.59,
"duration": 1.458
},
{
"text": "AUDIENCE: [INAUDIBLE]",
"start": 2889.048,
"duration": 3.492
},
{
"text": "JORDAN HAYASHI: Yeah.",
"start": 2892.54,
"duration": 1.05
},
{
"text": "So we have to bind\nthat function to this.",
"start": 2893.59,
"duration": 2.32
},
{
"text": "So we talked about this and binding\nthis towards the end of last lecture.",
"start": 2895.91,
"duration": 3.65
},
{
"text": "And we talked about how this is bound\non whatever object it's invoked on.",
"start": 2899.56,
"duration": 7.61
},
{
"text": "And so right now, this isn't\ninvoked on the class, right?",
"start": 2907.17,
"duration": 3.76
},
{
"text": "It's invoked when you click that button.",
"start": 2910.93,
"duration": 2.55
},
{
"text": "And so we're going to have to bind this\nto the value that we want it to be.",
"start": 2913.48,
"duration": 4.566
},
{
"text": "And so towards the end of the lecture,\nwe talked about different ways",
"start": 2918.046,
"duration": 2.874
},
{
"text": "to bind this.",
"start": 2920.92,
"duration": 0.75
},
{
"text": "Can anybody remember those?",
"start": 2921.67,
"duration": 1.23
},
{
"text": "Yeah?",
"start": 2928.02,
"duration": 0.5
},
{
"text": "AUDIENCE: Bind method.",
"start": 2928.52,
"duration": 1.32
},
{
"text": "JORDAN HAYASHI: Yeah.",
"start": 2929.84,
"duration": 0.874
},
{
"text": "There's this thing called bind,\nwhich is a method on all functions.",
"start": 2930.714,
"duration": 3.436
},
{
"text": "And similar to bind\nthere is call and apply.",
"start": 2934.15,
"duration": 3.55
},
{
"text": "And we also talked about a\ndifferent way of writing functions,",
"start": 2937.7,
"duration": 4.47
},
{
"text": "which actually lexically binds this.",
"start": 2942.17,
"duration": 3.84
},
{
"text": "Do you guys remember how that was?",
"start": 2946.01,
"duration": 3.39
},
{
"text": "Remember, if you use arrow notation,\nit automatically binds this for you.",
"start": 2949.4,
"duration": 5.1
},
{
"text": "And so you can solve this problem\nin all three different ways.",
"start": 2954.5,
"duration": 3.27
},
{
"text": "And so down here, if we\nwanted to do dot bind,",
"start": 2957.77,
"duration": 2.64
},
{
"text": "we could do dot bind to this,\nwhich, since we're invoking it",
"start": 2960.41,
"duration": 4.63
},
{
"text": "on the render method, which is a\nmethod that's invoked on the class,",
"start": 2965.04,
"duration": 4.58
},
{
"text": "this is bound correctly.",
"start": 2969.62,
"duration": 1.68
},
{
"text": "And so when we increase\nit, it will increase.",
"start": 2971.3,
"duration": 3.67
},
{
"text": "We could also use--",
"start": 2974.97,
"duration": 1.6
},
{
"text": "well, call and apply\ndon't really work here.",
"start": 2976.57,
"duration": 2.2
},
{
"text": "But we could actually\nuse arrow notation.",
"start": 2978.77,
"duration": 3.24
},
{
"text": "So we can say, let's have\nan arrow notation down here,",
"start": 2982.01,
"duration": 9.73
},
{
"text": "which creates a new, anonymous function.",
"start": 2991.74,
"duration": 4.2
},
{
"text": "And what does that\nanonymous function do?",
"start": 2995.94,
"duration": 1.72
},
{
"text": "Well, all it does is\ninvoke increase count.",
"start": 2997.66,
"duration": 3.22
},
{
"text": "And so since this we wrote\nas an ES6 arrow notation,",
"start": 3000.88,
"duration": 4.51
},
{
"text": "it automatically binds this\nto be what we want it to be.",
"start": 3005.39,
"duration": 3.922
},
{
"text": "Does that makes sense?",
"start": 3013.56,
"duration": 2.48
},
{
"text": "There are a couple other\nplaces that you could do this.",
"start": 3016.04,
"duration": 3.0
},
{
"text": "We could also do it in\nthe constructor method.",
"start": 3019.04,
"duration": 2.19
},
{
"text": "You could do this dot increase count\nequals this dot increase count dot bind",
"start": 3021.23,
"duration": 6.72
},
{
"text": "this.",
"start": 3027.95,
"duration": 2.04
},
{
"text": "Or lastly, you could also--",
"start": 3029.99,
"duration": 2.51
},
{
"text": "and then down here, you'd just\nhave to do this dot increase count.",
"start": 3032.5,
"duration": 3.22
},
{
"text": "Or alternatively, you could actually\njust define this in the constructor.",
"start": 3035.72,
"duration": 5.982
},
{
"text": "And so that would be the other way.",
"start": 3051.83,
"duration": 1.75
},
{
"text": "So you could say, this dot increase\ncount equals an arrow notation in this.",
"start": 3053.58,
"duration": 6.75
},
{
"text": "But let's stick with this way for now.",
"start": 3060.33,
"duration": 8.385
},
{
"text": "Cool.",
"start": 3068.715,
"duration": 0.746
},
{
"text": "So now we have a button that works,\nthat increases as we expected.",
"start": 3069.461,
"duration": 2.749
},
{
"text": "So what if instead of increasing it\nby 1, we wanted to increase it by 2?",
"start": 3075.13,
"duration": 4.32
},
{
"text": "Well, of course, you could just do this.",
"start": 3079.45,
"duration": 2.98
},
{
"text": "Let's say we want to do\nit by sending state twice.",
"start": 3082.43,
"duration": 3.65
},
{
"text": "So what do we expect to happen here?",
"start": 3095.6,
"duration": 1.5
},
{
"text": "We expect it to set state to be count\nas this dot state dot count plus 1 is 1.",
"start": 3101.43,
"duration": 5.22
},
{
"text": "And then [? it does ?] set count\nto be 1 plus 1 to be 2, right?",
"start": 3106.65,
"duration": 5.34
},
{
"text": "But we see it's only going up by 1.",
"start": 3111.99,
"duration": 4.07
},
{
"text": "Why might that be?",
"start": 3116.06,
"duration": 3.1
},
{
"text": "Well, set state calls are actually\nbatched and run asynchronously,",
"start": 3119.16,
"duration": 4.772
},
{
"text": "which means React is smart enough to\nknow that if this dot set state is",
"start": 3123.932,
"duration": 2.958
},
{
"text": "getting called a bunch\nof times in a row,",
"start": 3126.89,
"duration": 1.86
},
{
"text": "rather than immediately doing that,\nmaybe it would be better to just batch",
"start": 3128.75,
"duration": 3.87
},
{
"text": "them and do it all in one go.",
"start": 3132.62,
"duration": 2.88
},
{
"text": "And like I said, if you pass an\nobject to this dot set state,",
"start": 3135.5,
"duration": 3.17
},
{
"text": "it will just merge it\ninto the new state.",
"start": 3138.67,
"duration": 2.43
},
{
"text": "And so what is actually\nhappening when you",
"start": 3141.1,
"duration": 2.05
},
{
"text": "call this dot set state twice in a row?",
"start": 3143.15,
"duration": 3.6
},
{
"text": "Well, they get batched together.",
"start": 3146.75,
"duration": 1.81
},
{
"text": "And so it says, OK, now\nwe know we need to merge",
"start": 3148.56,
"duration": 5.6
},
{
"text": "into the old state these two objects.",
"start": 3154.16,
"duration": 4.29
},
{
"text": "And so it's basically saying, merge\nthe current state-- in this case,",
"start": 3158.45,
"duration": 7.47
},
{
"text": "it's count is 0.",
"start": 3165.92,
"duration": 6.529
},
{
"text": "And then what are we merging into it?",
"start": 3172.449,
"duration": 1.541
},
{
"text": "Well, we want to merge this\ndot state dot count plus 1.",
"start": 3173.99,
"duration": 4.11
},
{
"text": "And then we also want to\nmerge that same thing again.",
"start": 3181.95,
"duration": 2.41
},
{
"text": "And so what happens when you\nmerge these three things together?",
"start": 3189.195,
"duration": 2.625
},
{
"text": "Well, these two get merged together,\nand it actually turns into this.",
"start": 3194.44,
"duration": 3.32
},
{
"text": "Well, you go left to right,\nso count gets merged,",
"start": 3203.0,
"duration": 5.201
},
{
"text": "gets replaced by this\ndot state dot count,",
"start": 3208.201,
"duration": 1.749
},
{
"text": "which then gets replaced by\nthis dot state dot count plus 1.",
"start": 3209.95,
"duration": 4.75
},
{
"text": "And so that is actually just 0 plus\n1, which is why we end up getting a 1",
"start": 3214.7,
"duration": 6.46
},
{
"text": "here.",
"start": 3221.16,
"duration": 0.665
},
{
"text": "Does that make sense?",
"start": 3221.825,
"duration": 0.875
},
{
"text": "And so we can prove that it\nruns asynchronously by logging.",
"start": 3226.78,
"duration": 2.76
},
{
"text": "So what do we expect this dot\nstate dot count to be at line 21?",
"start": 3235.14,
"duration": 5.618
},
{
"text": "AUDIENCE: [INAUDIBLE]",
"start": 3240.758,
"duration": 4.872
},
{
"text": "JORDAN HAYASHI: It actually logs 0,\nbecause when you call this dot set",
"start": 3245.63,
"duration": 5.57
},
{
"text": "state, all it does is\nadd it to that queue",
"start": 3251.2,
"duration": 2.93
},
{
"text": "that we know it's\neventually going to execute.",
"start": 3254.13,
"duration": 1.96
},
{
"text": "Same with this dot set state.",
"start": 3256.09,
"duration": 1.208
},
{
"text": "It gets added to a queue as well.",
"start": 3257.298,
"duration": 2.342
},
{
"text": "And so when we get to line 21\nhere, what has actually executed?",
"start": 3259.64,
"duration": 3.53
},
{
"text": "Well, nothing.",
"start": 3263.17,
"duration": 1.41
},
{
"text": "And so this dot state dot\ncount still has a value of 0,",
"start": 3264.58,
"duration": 2.97
},
{
"text": "and then only after that do\nthose batch set states run.",
"start": 3267.55,
"duration": 4.5
},
{
"text": "So what if we actually did care\nabout the previous count before?",
"start": 3272.05,
"duration": 6.63
},
{
"text": "Say we wanted to add 1\nto the previous count,",
"start": 3278.68,
"duration": 2.52
},
{
"text": "but we actually really wanted\nthat state to exist beforehand.",
"start": 3281.2,
"duration": 5.6
},
{
"text": "Well, we can pass it what's\ncalled an updater function,",
"start": 3286.8,
"duration": 2.3
},
{
"text": "and that's a function that\ntakes the previous state",
"start": 3289.1,
"duration": 4.74
},
{
"text": "and returns some new state.",
"start": 3293.84,
"duration": 7.12
},
{
"text": "In this case, we want it to be the\nprevious state, dot count plus 1.",
"start": 3300.96,
"duration": 5.15
},
{
"text": "So now we have two set\nstates, where rather",
"start": 3317.11,
"duration": 2.384
},
{
"text": "than just passing an object\nto emerge, we're saying,",
"start": 3319.494,
"duration": 2.166
},
{
"text": "actually run this function.",
"start": 3321.66,
"duration": 1.87
},
{
"text": "This function takes the\nold state and returns",
"start": 3323.53,
"duration": 2.42
},
{
"text": "a new object, which is where the count\nis the previous state's count plus 1.",
"start": 3325.95,
"duration": 6.006
},
{
"text": "And so now when it batches\nit, it says, oh, we actually",
"start": 3331.956,
"duration": 2.754
},
{
"text": "need to run this function twice.",
"start": 3334.71,
"duration": 1.67
},
{
"text": "And so now when we click,\nit actually goes up by two.",
"start": 3336.38,
"duration": 5.82
},
{
"text": "So any questions on React,\nprops, or state thus far?",
"start": 3349.06,
"duration": 4.542
},
{
"text": "Cool.",
"start": 3358.01,
"duration": 0.5
},
{
"text": "Let's go ahead and take a quick break.",
"start": 3358.51,
"duration": 1.38
},
{
"text": "And then when we come back, we can\nplay with React a little bit more.",
"start": 3359.89,
"duration": 2.875
},
{
"text": "Hello, and welcome back.",
"start": 3365.32,
"duration": 1.1
},
{
"text": "So before the break, we were talking\nabout React and props and state.",
"start": 3366.42,
"duration": 3.88
},
{
"text": "And now with all three, we can\nactually go ahead and start",
"start": 3370.3,
"duration": 2.76
},
{
"text": "building pretty powerful apps.",
"start": 3373.06,
"duration": 3.66
},
{
"text": "And so for homework,\nfor the project zero,",
"start": 3376.72,
"duration": 2.43
},
{
"text": "you guys have been working\non a to do app, which you've",
"start": 3379.15,
"duration": 2.455
},
{
"text": "been writing in all vanilla JavaScript.",
"start": 3381.605,
"duration": 1.625
},
{
"text": "And today for the rest\nof the class, we're",
"start": 3383.23,
"duration": 2.01
},
{
"text": "going to go ahead and together\nimplement that in all React.",
"start": 3385.24,
"duration": 2.87
},
{
"text": "So what are some strategies\nyou may go around doing",
"start": 3395.44,
"duration": 3.12
},
{
"text": "your thing in vanilla JavaScript?",
"start": 3398.56,
"duration": 3.03
},
{
"text": "Well, say we had to dos created as list\nitems, where within each list item,",
"start": 3401.59,
"duration": 6.12
},
{
"text": "we have input, which has a checkbox.",
"start": 3407.71,
"duration": 2.339
},
{
"text": "Maybe you're doing the challenge and\nyou want to take care of the deletes",
"start": 3410.049,
"duration": 3.041
},
{
"text": "as well.",
"start": 3413.09,
"duration": 0.795
},
{
"text": "And then, of course, you're\ngoing to have some span",
"start": 3413.885,
"duration": 2.125
},
{
"text": "or some way of displaying text.",
"start": 3416.01,
"duration": 3.0
},
{
"text": "And so maybe have a couple\ndifferent functions.",
"start": 3419.01,
"duration": 2.39
},
{
"text": "One is to create a to do.",
"start": 3421.4,
"duration": 1.89
},
{
"text": "One is to delete a to do.",
"start": 3423.29,
"duration": 1.339
},
{
"text": "And what might you do in those functions\nin order to create those to dos?",
"start": 3424.629,
"duration": 3.041
},
{
"text": "Well, first, maybe you'll get the text.",
"start": 3430.4,
"duration": 3.761
},
{
"text": "Then what?",
"start": 3434.161,
"duration": 0.499
},
{
"text": "Maybe go ahead and using\ndocument dot create element,",
"start": 3434.66,
"duration": 3.08
},
{
"text": "maybe you want to create a list item.",
"start": 3437.74,
"duration": 2.9
},
{
"text": "Then what?",
"start": 3440.64,
"duration": 0.5
},
{
"text": "You'll probably have\nto create the input.",
"start": 3441.14,
"duration": 2.145
},
{
"text": "Then probably create a button.",
"start": 3446.05,
"duration": 2.67
},
{
"text": "Create the span.",
"start": 3448.72,
"duration": 2.25
},
{
"text": "And maybe at the end, you\nhook those all up together",
"start": 3450.97,
"duration": 2.58
},
{
"text": "and append those to the list.",
"start": 3453.55,
"duration": 3.65
},
{
"text": "And so how might you\ntake care of delete?",
"start": 3460.21,
"duration": 2.11
},
{
"text": "Well, find the to do.",
"start": 3462.32,
"duration": 2.34
},
{
"text": "Maybe delete that.",
"start": 3468.17,
"duration": 3.071
},
{
"text": "Then what?",
"start": 3471.241,
"duration": 0.499
},
{
"text": "Make sure to update the counts.",
"start": 3471.74,
"duration": 1.31
},
{
"text": "And maybe we have to do\nthat over here as well.",
"start": 3475.342,
"duration": 1.958
},
{
"text": "So you see how we're doing this\nin a very imperative manner.",
"start": 3482.0,
"duration": 4.632
},
{
"text": "Using JavaScript, we tell the\nbrowser exactly what we want to do.",
"start": 3486.632,
"duration": 2.708
},
{
"text": "Well, we know that our to\ndos are shaped like this.",
"start": 3489.34,
"duration": 2.32
},
{
"text": "So first go ahead and get the text.",
"start": 3491.66,
"duration": 1.48
},
{
"text": "Maybe create that [INAUDIBLE],,\ncreate the checkbox,",
"start": 3493.14,
"duration": 2.3
},
{
"text": "create all these other things.",
"start": 3495.44,
"duration": 1.92
},
{
"text": "Append them to each other, and\nmaybe append them to the list.",
"start": 3497.36,
"duration": 3.07
},
{
"text": "And so we go ahead, and we created a new\nto do, but we had to do a lot of steps",
"start": 3500.43,
"duration": 4.22
},
{
"text": "in order to get there.",
"start": 3504.65,
"duration": 1.55
},
{
"text": "And so what might be an\neasier way to do this?",
"start": 3506.2,
"duration": 4.67
},
{
"text": "Well, in to do one, maybe we wanted\nto abstract out the creation of the",
"start": 3510.87,
"duration": 6.7
},
{
"text": "to do itself.",
"start": 3517.57,
"duration": 1.12
},
{
"text": "So maybe in that new\ncreate to do function,",
"start": 3518.69,
"duration": 2.267
},
{
"text": "we go ahead and make the list item,\nmake the input, make the button,",
"start": 3520.957,
"duration": 2.833
},
{
"text": "make the span.",
"start": 3523.79,
"duration": 0.87
},
{
"text": "Hook those all up together.",
"start": 3524.66,
"duration": 1.639
},
{
"text": "But that's one, little,\ndiscrete part of the UI, right?",
"start": 3526.299,
"duration": 2.291
},
{
"text": "We're going to start to\ncomponentize these things.",
"start": 3528.59,
"duration": 2.34
},
{
"text": "Maybe abstract out a function for\ncreating the to do, and then in the new",
"start": 3530.93,
"duration": 3.48
},
{
"text": "to do, we can still get the text, update\nthe counts, and append to the list.",
"start": 3534.41,
"duration": 4.09
},
{
"text": "But rather than doing all of this\nwork in the new to do function,",
"start": 3538.5,
"duration": 3.65
},
{
"text": "maybe we just invoke create to do.",
"start": 3542.15,
"duration": 2.04
},
{
"text": "And so we're starting\nto break out something",
"start": 3544.19,
"duration": 2.34
},
{
"text": "that was pretty complicated into\nnow two separate steps, where",
"start": 3546.53,
"duration": 3.27
},
{
"text": "each step is relatively simple.",
"start": 3549.8,
"duration": 3.07
},
{
"text": "But still in each step, we have\nto go about all of these steps",
"start": 3552.87,
"duration": 3.08
},
{
"text": "in order to get what we want.",
"start": 3555.95,
"duration": 5.11
},
{
"text": "So what might be a\nbetter way to do this?",
"start": 3561.06,
"duration": 5.03
},
{
"text": "Well, rather than going through\nthe steps, where we say, oh, hey,",
"start": 3566.09,
"duration": 4.59
},
{
"text": "get me a new element called a list\nitem and input button in a span,",
"start": 3570.68,
"duration": 3.6
},
{
"text": "maybe we wanted just to write that\nall somewhat declaratively ourselves.",
"start": 3574.28,
"duration": 4.03
},
{
"text": "And so we can actually do\nsomething called innerHTML,",
"start": 3578.31,
"duration": 3.65
},
{
"text": "so say we created a list item.",
"start": 3581.96,
"duration": 1.65
},
{
"text": "And maybe we want to take\nadvantage of JavaScript's ability",
"start": 3593.35,
"duration": 3.85
},
{
"text": "to do in-line string\nconcatenation in order",
"start": 3597.2,
"duration": 3.99
},
{
"text": "to create some HTML directly here.",
"start": 3601.19,
"duration": 2.89
},
{
"text": "So we could do something\nlike li.innerHTML,",
"start": 3604.08,
"duration": 6.85
},
{
"text": "and we can start actually\nputting this directly in here.",
"start": 3610.93,
"duration": 3.04
},
{
"text": "And so this will actually, in\na more declarative manner, say,",
"start": 3627.31,
"duration": 2.82
},
{
"text": "hey, I want an input,\na button in the span,",
"start": 3630.13,
"duration": 2.58
},
{
"text": "and just stick this in the\ninnerHTML of that list item.",
"start": 3632.71,
"duration": 3.87
},
{
"text": "And so it's starting to\nlook somewhat like something",
"start": 3636.58,
"duration": 2.91
},
{
"text": "we've seen before, right?",
"start": 3639.49,
"duration": 1.62
},
{
"text": "It's starting to represent\nmore of a React component.",
"start": 3641.11,
"duration": 5.28
},
{
"text": "Do you guys see how that is starting\nto look a lot like what we were",
"start": 3646.39,
"duration": 3.93
},
{
"text": "doing before, using React components?",
"start": 3650.32,
"duration": 1.98
},
{
"text": "But let's stick with vanilla JavaScript\nstill and take this a step further",
"start": 3656.62,
"duration": 6.69
},
{
"text": "and actually store the to dos in memory.",
"start": 3663.31,
"duration": 1.88
},
{
"text": "And so what have we been\npreviously using to store to dos?",
"start": 3665.19,
"duration": 3.68
},
{
"text": "Well, we've just been\nadding them to the DOM,",
"start": 3668.87,
"duration": 3.18
},
{
"text": "and then using the DOM as a way to\nremember exactly what our to dos are.",
"start": 3672.05,
"duration": 3.76
},
{
"text": "But what if we actually wanted to\nrather than storing it on the DOM,",
"start": 3675.81,
"duration": 2.96
},
{
"text": "actually store that in\nJavaScript, in some sort of data",
"start": 3678.77,
"duration": 2.34
},
{
"text": "structure in our JavaScripted memory?",
"start": 3681.11,
"duration": 4.2
},
{
"text": "Well, that might be easier\nto do stuff like delete on.",
"start": 3685.31,
"duration": 3.7
},
{
"text": "But how are we going\nto get that to the DOM?",
"start": 3689.01,
"duration": 2.465
},
{
"text": "Well, we're going to actually\nhave to write a method ourselves.",
"start": 3691.475,
"duration": 2.625
},
{
"text": "And so maybe we have to dos\nstored in memory as an array.",
"start": 3694.1,
"duration": 5.044
},
{
"text": "Maybe we have something\ncalled a render to do,",
"start": 3699.144,
"duration": 1.916
},
{
"text": "which will actually\nrender a single to do.",
"start": 3701.06,
"duration": 2.08
},
{
"text": "And by render, I mean turn that\nto do, an object in memory,",
"start": 3703.14,
"duration": 4.13
},
{
"text": "into actual HTML, something\nthat the browser can display.",
"start": 3707.27,
"duration": 3.816
},
{
"text": "And so that takes care of\nrendering a single to do,",
"start": 3711.086,
"duration": 2.124
},
{
"text": "but how are we going to\nrender that entire to do list?",
"start": 3713.21,
"duration": 2.249
},
{
"text": "Well, maybe we want to clear the list\nand actually map over the to dos.",
"start": 3715.459,
"duration": 7.711
},
{
"text": "So we do to dos dot map.",
"start": 3723.17,
"duration": 1.23
},
{
"text": "What are we mapping?",
"start": 3724.4,
"duration": 0.833
},
{
"text": "Well, let's actually render\neach of those to dos.",
"start": 3725.233,
"duration": 2.537
},
{
"text": "And for each of the to\ndos that we rendered,",
"start": 3727.77,
"duration": 2.14
},
{
"text": "go ahead and append that to the list.",
"start": 3729.91,
"duration": 3.89
},
{
"text": "And so now we went from\nstoring to dos in memory,",
"start": 3733.8,
"duration": 3.32
},
{
"text": "creating this concept of rendering,\nwhere we turn a single JavaScript",
"start": 3737.12,
"duration": 3.99
},
{
"text": "object into an actual thing\nthat the browser can display.",
"start": 3741.11,
"duration": 4.83
},
{
"text": "And then we have this\nconcept called render, which",
"start": 3745.94,
"duration": 2.22
},
{
"text": "actually renders all of our to dos.",
"start": 3748.16,
"duration": 2.85
},
{
"text": "And so we go from what used to be\na very imperative way of writing",
"start": 3751.01,
"duration": 2.88
},
{
"text": "JavaScript, and going from having\na single to do to appending that",
"start": 3753.89,
"duration": 4.2
},
{
"text": "to the DOM.",
"start": 3758.09,
"duration": 1.12
},
{
"text": "So now we're a more declarative way.",
"start": 3759.21,
"duration": 1.88
},
{
"text": "We're declaring that a\nbunch of to dos may exist.",
"start": 3761.09,
"duration": 2.94
},
{
"text": "We're declaring that, hey, here's\na way to render a single to do",
"start": 3764.03,
"duration": 3.78
},
{
"text": "and writing a way to turn that\nlist into a full rendered page.",
"start": 3767.81,
"duration": 7.654
},
{
"text": "And so now when we want\nto add a to do, rather",
"start": 3775.464,
"duration": 1.916
},
{
"text": "than doing a bunch of DOM\nmanipulation, maybe we actually",
"start": 3777.38,
"duration": 3.12
},
{
"text": "want to just do something like a\nnew to do and push that to the list.",
"start": 3780.5,
"duration": 5.316
},
{
"text": "Well, once you push it to the list,\nnow we have to manually say, hey,",
"start": 3785.816,
"duration": 2.874
},
{
"text": "we updated the list.",
"start": 3788.69,
"duration": 0.87
},
{
"text": "Let's go ahead and\nrender that to the page.",
"start": 3789.56,
"duration": 3.48
},
{
"text": "And so now if we're storing\neverything in memory,",
"start": 3793.04,
"duration": 2.4
},
{
"text": "it becomes a lot easier\nto remove things.",
"start": 3795.44,
"duration": 2.86
},
{
"text": "And so how are we going\nto remove the to dos?",
"start": 3798.3,
"duration": 2.19
},
{
"text": "Well, we have to figure out\nexactly how to do the to dos,",
"start": 3800.49,
"duration": 2.374
},
{
"text": "and this is just an\nimplementation detail.",
"start": 3802.864,
"duration": 1.826
},
{
"text": "But we can actually reset to\ndos to be a filtered list.",
"start": 3804.69,
"duration": 2.67
},
{
"text": "So just filter them by excluding that to\ndo that we didn't want to be included.",
"start": 3807.36,
"duration": 7.65
},
{
"text": "And then we, again, since\nwe changed everything,",
"start": 3815.01,
"duration": 2.21
},
{
"text": "now we're going to have to go\nand manually kick off a render.",
"start": 3817.22,
"duration": 3.0
},
{
"text": "And so now we're writing JavaScript in\nmore of a declarative paradigm, more",
"start": 3820.22,
"duration": 3.42
},
{
"text": "like what we saw in React earlier.",
"start": 3823.64,
"duration": 2.52
},
{
"text": "But we're still implementing stuff like\nrendering and rendering a single to do",
"start": 3826.16,
"duration": 4.8
},
{
"text": "and kicking off these things manually.",
"start": 3830.96,
"duration": 2.61
},
{
"text": "And let's actually see how this looks\nif we were all to do it in React,",
"start": 3833.57,
"duration": 3.75
},
{
"text": "where React actually handles a lot\nof the data rendering to the screen,",
"start": 3837.32,
"duration": 4.17
},
{
"text": "using a library rather than stuff\nthat we have to write manually.",
"start": 3841.49,
"duration": 3.06
},
{
"text": "So let's actually do this from scratch.",
"start": 3848.036,
"duration": 2.734
},
{
"text": "First let me save this.",
"start": 3850.77,
"duration": 1.4
},
{
"text": "So let's go ahead and actually, from\nscratch, implement our to do list",
"start": 3873.79,
"duration": 5.48
},
{
"text": "all in React.",
"start": 3879.27,
"duration": 1.5
},
{
"text": "So of course, we're going to\nhave to store stuff in state.",
"start": 3880.77,
"duration": 2.67
},
{
"text": "So rather than having app be a function,\nwhat might we want to do instead?",
"start": 3883.44,
"duration": 4.999
},
{
"text": "Have it be a class.",
"start": 3888.439,
"duration": 0.791
},
{
"text": "And just like in the example\nI was showing earlier,",
"start": 3895.896,
"duration": 2.124
},
{
"text": "we have a concept of rendering that app.",
"start": 3898.02,
"duration": 2.002
},
{
"text": "React components actually\ndo this automatically",
"start": 3900.022,
"duration": 1.958
},
{
"text": "for you in this render method.",
"start": 3901.98,
"duration": 2.28
},
{
"text": "And so let's go ahead and\nimplement that rendering method.",
"start": 3918.054,
"duration": 2.416
},
{
"text": "And so returning this.",
"start": 3932.98,
"duration": 3.46
},
{
"text": "And now we have a blank to do list.",
"start": 3942.745,
"duration": 7.475
},
{
"text": "So now we have a class called app.",
"start": 3950.22,
"duration": 2.19
},
{
"text": "It's extending React\ncomponent, which gives us what?",
"start": 3952.41,
"duration": 4.5
},
{
"text": "It allows us to have state,\nand it gives us access",
"start": 3956.91,
"duration": 2.49
},
{
"text": "to this thing called this dot set\nstate in order to update the state.",
"start": 3959.4,
"duration": 3.39
},
{
"text": "And this render method goes ahead\nand just renders a blank div",
"start": 3962.79,
"duration": 3.33
},
{
"text": "with a blank UL.",
"start": 3966.12,
"duration": 1.285
},
{
"text": "Yeah?",
"start": 3967.405,
"duration": 0.499
},
{
"text": "AUDIENCE: In the previous\nversion of render,",
"start": 3967.904,
"duration": 1.868
},
{
"text": "why was it returning a [INAUDIBLE]?",
"start": 3969.772,
"duration": 3.597
},
{
"text": "JORDAN HAYASHI: Oh.",
"start": 3973.369,
"duration": 0.791
},
{
"text": "So the question was in my\nexample to do app three,",
"start": 3974.16,
"duration": 7.71
},
{
"text": "why was render up here returning false?",
"start": 3981.87,
"duration": 4.23
},
{
"text": "So we'll talk about handling\nevents in a little bit.",
"start": 3986.1,
"duration": 4.24
},
{
"text": "But basically, browsers actually\nall have a way to handle events.",
"start": 3990.34,
"duration": 4.49
},
{
"text": "And so if you do something\nlike click a Submit button,",
"start": 3994.83,
"duration": 2.49
},
{
"text": "it will automatically go ahead\nand submit a form for you",
"start": 3997.32,
"duration": 3.57
},
{
"text": "and refresh the page.",
"start": 4000.89,
"duration": 1.78
},
{
"text": "And so a lot of times, when we actually\nwant to handle that all in JavaScript,",
"start": 4002.67,
"duration": 3.8
},
{
"text": "we have to have a way to disable\nthe automatic page refresh.",
"start": 4006.47,
"duration": 3.394
},
{
"text": "And so there are a couple\ndifferent ways to do that.",
"start": 4009.864,
"duration": 2.166
},
{
"text": "You can do something like event\ndot stop propagation or event dot--",
"start": 4012.03,
"duration": 3.285
},
{
"text": "I forget the exact method\nname, but it's something",
"start": 4018.637,
"duration": 2.083
},
{
"text": "like stop this event from happening.",
"start": 4020.72,
"duration": 1.71
},
{
"text": "Prevent default. Event\ndot prevent default.",
"start": 4022.43,
"duration": 2.36
},
{
"text": "And a shorthand for doing\nboth of those is just",
"start": 4024.79,
"duration": 2.02
},
{
"text": "returning false out of\nwhatever handler they do.",
"start": 4026.81,
"duration": 2.94
},
{
"text": "And so when I do return\nfalse from this render, that",
"start": 4029.75,
"duration": 3.687
},
{
"text": "means if I ever want\nto do something, like I",
"start": 4033.437,
"duration": 1.833
},
{
"text": "add to do, and hook it up to a button\nthat might automatically submit a form,",
"start": 4035.27,
"duration": 4.77
},
{
"text": "I can return false from that\ninstead and automatically",
"start": 4040.04,
"duration": 3.3
},
{
"text": "prevent that from happening.",
"start": 4043.34,
"duration": 1.69
},
{
"text": "And so this is just some shorthand\nfor rather than having submit buttons",
"start": 4045.03,
"duration": 5.15
},
{
"text": "automatically refresh a\npage, it just blocks that",
"start": 4050.18,
"duration": 3.21
},
{
"text": "and lets me handle it all in JavaScript.",
"start": 4053.39,
"duration": 2.132
},
{
"text": "Cool.",
"start": 4061.295,
"duration": 0.5
},
{
"text": "So back to this example in React.",
"start": 4061.795,
"duration": 3.775
},
{
"text": "So we're up to this point where\nwe have a class called app.",
"start": 4065.57,
"duration": 3.66
},
{
"text": "Right now, we have no state associated\nwith it, and all we're doing",
"start": 4069.23,
"duration": 3.15
},
{
"text": "is rendering this empty list.",
"start": 4072.38,
"duration": 1.84
},
{
"text": "And so how might we go about\nimplementing these features",
"start": 4074.22,
"duration": 2.9
},
{
"text": "that we have in our to do project?",
"start": 4077.12,
"duration": 3.141
},
{
"text": "Well, first, we're going to\nwant to start tracking something",
"start": 4080.261,
"duration": 2.499
},
{
"text": "in [? our ?] state, right?",
"start": 4082.76,
"duration": 1.083
},
{
"text": "Maybe we want to track the to dos.",
"start": 4083.843,
"duration": 3.697
},
{
"text": "And so how do we go ahead and\ncreate something called state?",
"start": 4087.54,
"duration": 3.7
},
{
"text": "Well, first, we want to\ninvoke the constructor, which",
"start": 4091.24,
"duration": 3.61
},
{
"text": "for now takes no arguments, since we're\nnot actually passing any props to app.",
"start": 4094.85,
"duration": 4.83
},
{
"text": "First thing we do in the\nconstructor is always",
"start": 4099.68,
"duration": 2.07
},
{
"text": "called the super, which is invoking\nthe constructor on React.Component,",
"start": 4101.75,
"duration": 6.09
},
{
"text": "which gives us things\nlike this dot set state.",
"start": 4107.84,
"duration": 2.519
},
{
"text": "And then now we have the\nopportunity to initialize our state,",
"start": 4110.359,
"duration": 3.271
},
{
"text": "so we can go ahead and do\nthis.state equals whatever.",
"start": 4113.63,
"duration": 5.16
},
{
"text": "And so what might we\nwant to store in state?",
"start": 4118.79,
"duration": 2.071
},
{
"text": "AUDIENCE: To do list.",
"start": 4120.861,
"duration": 1.398
},
{
"text": "JORDAN HAYASHI: Yeah, the to do list.",
"start": 4122.259,
"duration": 1.541
},
{
"text": "Is there anything else\nthat we should store?",
"start": 4123.8,
"duration": 1.833
},
{
"text": "Nothing I can think of\noff the top of the head.",
"start": 4127.87,
"duration": 2.581
},
{
"text": "But thankfully, if we want to add\nsomething later, it's very easy to do.",
"start": 4130.451,
"duration": 2.999
},
{
"text": "And so let's just do to dos, and\nlet's store this all on an empty list.",
"start": 4133.45,
"duration": 6.9
},
{
"text": "Cool.",
"start": 4140.35,
"duration": 0.5
},
{
"text": "So now we have this concept of state.",
"start": 4140.85,
"duration": 2.41
},
{
"text": "We now have this concept of to\ndos, which is stored in our state,",
"start": 4143.26,
"duration": 3.059
},
{
"text": "but we're not doing anything with it.",
"start": 4146.319,
"duration": 1.87
},
{
"text": "And so let's actually render\nthose to dos to the screen.",
"start": 4148.189,
"duration": 3.321
},
{
"text": "So we might want to do it\nwithin the unordered list.",
"start": 4151.51,
"duration": 3.36
},
{
"text": "Let's have something like\nthis dot state dot to dos.",
"start": 4154.87,
"duration": 3.42
},
{
"text": "And again, we wrap things\nin single curly braces",
"start": 4158.29,
"duration": 3.189
},
{
"text": "when we want to execute\nthem as JavaScript.",
"start": 4161.479,
"duration": 2.5
},
{
"text": "And so we have this dot state dot\nto dos, which is an empty array.",
"start": 4163.979,
"duration": 3.791
},
{
"text": "But we can't just take for granted\nthat it's always an empty array.",
"start": 4167.77,
"duration": 3.36
},
{
"text": "The point of writing declarative code\nis that we want this to hold true,",
"start": 4171.13,
"duration": 4.14
},
{
"text": "no matter what the state is.",
"start": 4175.27,
"duration": 2.07
},
{
"text": "And so we just assume that this is\nfull of to dos, and so we can go ahead",
"start": 4177.34,
"duration": 3.479
},
{
"text": "and map over that and render each to do.",
"start": 4180.819,
"duration": 2.011
},
{
"text": "So for each to do, let's\nactually create a to do.",
"start": 4186.52,
"duration": 2.85
},
{
"text": "All right, so I'm mapping out\nthe to dos, and for each to do,",
"start": 4198.18,
"duration": 3.24
},
{
"text": "I'm creating this tag\ncalled uppercase to do.",
"start": 4201.42,
"duration": 4.29
},
{
"text": "So who remembers what\nuppercase tags mean?",
"start": 4205.71,
"duration": 5.26
},
{
"text": "AUDIENCE: It's a React component.",
"start": 4210.97,
"duration": 1.79
},
{
"text": "JORDAN HAYASHI: Yeah,\nit's a React component.",
"start": 4212.76,
"duration": 2.79
},
{
"text": "And we now have to describe something\ncalled a React component called capital",
"start": 4215.55,
"duration": 4.14
},
{
"text": "to do.",
"start": 4219.69,
"duration": 0.69
},
{
"text": "And so let's go ahead and do that.",
"start": 4220.38,
"duration": 1.416
},
{
"text": "And so just like the examples\nthat I showed you earlier,",
"start": 4224.1,
"duration": 2.72
},
{
"text": "where we abstracted out the\nconcept of creating a to do,",
"start": 4226.82,
"duration": 2.357
},
{
"text": "we can do that in React as well.",
"start": 4229.177,
"duration": 1.333
},
{
"text": "So let's have this thing called a to do.",
"start": 4230.51,
"duration": 2.36
},
{
"text": "And what it does is it takes some props.",
"start": 4232.87,
"duration": 2.83
},
{
"text": "We can call this variable\nwhatever we want,",
"start": 4235.7,
"duration": 2.19
},
{
"text": "but the React convention\nis to call it props.",
"start": 4237.89,
"duration": 2.057
},
{
"text": "And what are we going to return?",
"start": 4239.947,
"duration": 1.333
},
{
"text": "Well, let's just return an\nunordered list, or a list item.",
"start": 4241.28,
"duration": 4.89
},
{
"text": "What might we have in that list item?",
"start": 4246.17,
"duration": 1.56
},
{
"text": "Well, we had an input of type checkbox.",
"start": 4250.36,
"duration": 3.16
},
{
"text": "We had a button that\nallowed us to delete.",
"start": 4256.96,
"duration": 3.72
},
{
"text": "And last but not least,\nwe had some text.",
"start": 4263.44,
"duration": 3.165
},
{
"text": "And what was in that text?",
"start": 4266.605,
"duration": 1.605
},
{
"text": "Well, probably just props, dots--",
"start": 4268.21,
"duration": 2.46
},
{
"text": "we can call it the text for the to do.",
"start": 4270.67,
"duration": 2.37
},
{
"text": "Cool.",
"start": 4278.581,
"duration": 0.499
},
{
"text": "So now we declaratively just said,\nhey, every time we want a to do,",
"start": 4279.08,
"duration": 4.006
},
{
"text": "this is what it looks like.",
"start": 4283.086,
"duration": 1.124
},
{
"text": "It's a list item, and\ninside it, we have an input.",
"start": 4284.21,
"duration": 2.284
},
{
"text": "It's going to be a checkbox.",
"start": 4286.494,
"duration": 1.166
},
{
"text": "We're going to have a\nbutton called delete.",
"start": 4287.66,
"duration": 1.43
},
{
"text": "It doesn't actually do anything for now.",
"start": 4289.09,
"duration": 1.99
},
{
"text": "And we also have a span, and\nwhat's that value of the span?",
"start": 4291.08,
"duration": 3.29
},
{
"text": "Well, just look at my\nprops and grab the text.",
"start": 4294.37,
"duration": 2.35
},
{
"text": "OK, so now what do we have to do?",
"start": 4307.39,
"duration": 1.5
},
{
"text": "Maybe we should add a way to add to dos.",
"start": 4308.89,
"duration": 3.49
},
{
"text": "So let's create a\nmethod called add to do.",
"start": 4312.38,
"duration": 5.205
},
{
"text": "And what should we do first?",
"start": 4317.585,
"duration": 1.475
},
{
"text": "Maybe we should get some text.",
"start": 4319.06,
"duration": 2.53
},
{
"text": "So there are many\ndifferent ways to do this.",
"start": 4321.59,
"duration": 6.01
},
{
"text": "Let's just for now prompt.",
"start": 4327.6,
"duration": 1.408
},
{
"text": "And then what do we\nwant to do with that?",
"start": 4340.48,
"duration": 2.34
},
{
"text": "We have text.",
"start": 4342.82,
"duration": 2.34
},
{
"text": "What should we actually do with it?",
"start": 4345.16,
"duration": 2.0
},
{
"text": "AUDIENCE: Add it to the to do list.",
"start": 4351.66,
"duration": 2.0
},
{
"text": "JORDAN HAYASHI: Yeah, we should\nadd it to our to do list.",
"start": 4353.66,
"duration": 1.77
},
{
"text": "And how are we going to do that?",
"start": 4355.43,
"duration": 1.333
},
{
"text": "We can just do this dot state\nequals something, right?",
"start": 4359.22,
"duration": 2.54
},
{
"text": "No.",
"start": 4364.28,
"duration": 0.51
},
{
"text": "There's only one way\nto update state, right?",
"start": 4364.79,
"duration": 1.833
},
{
"text": "We should do this dot set state.",
"start": 4366.623,
"duration": 2.711
},
{
"text": "And what are we doing?",
"start": 4369.334,
"duration": 0.916
},
{
"text": "Well, we have the to do equal\nthis dot state dot to dos.",
"start": 4370.25,
"duration": 11.13
},
{
"text": "And then additional thing.",
"start": 4381.38,
"duration": 2.22
},
{
"text": "And so the way that\nwe've added to an array",
"start": 4383.6,
"duration": 3.12
},
{
"text": "thus far is by doing\nthis sum array dot push.",
"start": 4386.72,
"duration": 3.87
},
{
"text": "What that does is it\nactually mutates the array.",
"start": 4390.59,
"duration": 3.06
},
{
"text": "And so the way that React\nknows that it should update",
"start": 4393.65,
"duration": 4.11
},
{
"text": "is it sees if the values that\nare passed to it are different.",
"start": 4397.76,
"duration": 4.69
},
{
"text": "And as we learned a couple\nlectures ago, all objects",
"start": 4402.45,
"duration": 2.666
},
{
"text": "are actually stored by reference.",
"start": 4405.116,
"duration": 1.374
},
{
"text": "And so when we go ahead\nand mutate an object,",
"start": 4406.49,
"duration": 3.297
},
{
"text": "the reference doesn't actually change.",
"start": 4409.787,
"duration": 1.583
},
{
"text": "And so the React paradigm is\nrather than mutating these props,",
"start": 4411.37,
"duration": 3.7
},
{
"text": "we should actually create new ones.",
"start": 4415.07,
"duration": 2.71
},
{
"text": "And so a quick way to\ncreate a new array is",
"start": 4417.78,
"duration": 5.45
},
{
"text": "by using this dot dot\ndot notation, which",
"start": 4423.23,
"duration": 1.92
},
{
"text": "means given an array, if\nyou dot dot dot array,",
"start": 4425.15,
"duration": 5.28
},
{
"text": "it just pulls out all of\nthose values of the array.",
"start": 4430.43,
"duration": 3.24
},
{
"text": "And so when we do something like\nbrackets dot dot dot some array,",
"start": 4433.67,
"duration": 5.43
},
{
"text": "it pulls out all the values of that\narray and puts it into a new array.",
"start": 4439.1,
"duration": 3.6
},
{
"text": "And so this is a quick\nway of cloning an array.",
"start": 4442.7,
"duration": 2.741
},
{
"text": "And so now the array reference is\nchanged, but the values in there",
"start": 4445.441,
"duration": 2.749
},
{
"text": "are all the same.",
"start": 4448.19,
"duration": 2.2
},
{
"text": "And so now we've cloned the array,\nand now we should add a new to do.",
"start": 4450.39,
"duration": 6.11
},
{
"text": "And so let's call our to dos\nobjects that have a text field,",
"start": 4456.5,
"duration": 6.88
},
{
"text": "and that can be the text.",
"start": 4463.38,
"duration": 1.422
},
{
"text": "So there's a small bug\nin our program now.",
"start": 4469.89,
"duration": 1.95
},
{
"text": "Well, one, we don't\nknow how to add a list.",
"start": 4471.84,
"duration": 3.0
},
{
"text": "Add to that.",
"start": 4474.84,
"duration": 1.33
},
{
"text": "So let's go ahead and create\na button that can add a to do.",
"start": 4476.17,
"duration": 8.862
},
{
"text": "So now you have a button,\nbut it's not actually",
"start": 4485.032,
"duration": 1.958
},
{
"text": "doing anything when we click it.",
"start": 4486.99,
"duration": 2.25
},
{
"text": "So first we need to set\nup an event handler,",
"start": 4489.24,
"duration": 4.83
},
{
"text": "so when that button is\nclicked, what should it do?",
"start": 4494.07,
"duration": 3.71
},
{
"text": "Well, it should do this dot add to do.",
"start": 4497.78,
"duration": 4.935
},
{
"text": "So what is wrong with this code?",
"start": 4502.715,
"duration": 1.9
},
{
"text": "Why isn't it working?",
"start": 4508.89,
"duration": 3.05
},
{
"text": "Yeah, this is not bound\nto what it should be.",
"start": 4511.94,
"duration": 1.94
},
{
"text": "And so how are we going to fix that?",
"start": 4513.88,
"duration": 1.94
},
{
"text": "Well, the easiest way is\nto just declare an arrow",
"start": 4515.82,
"duration": 2.34
},
{
"text": "function in line here, which will\nlexically bind what we want it to do.",
"start": 4518.16,
"duration": 7.75
},
{
"text": "And so now we have another\nbug, so we declared",
"start": 4525.91,
"duration": 4.84
},
{
"text": "to do to be a list item with\nan input, a button, and a span.",
"start": 4530.75,
"duration": 4.49
},
{
"text": "And we see the input in the\nbutton, but we don't see any spans.",
"start": 4535.24,
"duration": 3.67
},
{
"text": "So does anybody spot exactly\nwhy that's happening?",
"start": 4538.91,
"duration": 4.049
},
{
"text": "So what text are we\npopulating that span with?",
"start": 4551.77,
"duration": 2.916
},
{
"text": "Props dot text.",
"start": 4561.87,
"duration": 1.79
},
{
"text": "And so what does the\nobject called props--",
"start": 4563.66,
"duration": 3.84
},
{
"text": "what values are in there?",
"start": 4567.5,
"duration": 2.43
},
{
"text": "They're key value pairs that are\nmapped to what are passed in here.",
"start": 4569.93,
"duration": 4.41
},
{
"text": "Are we passing a key\ncalled text into to do?",
"start": 4574.34,
"duration": 3.42
},
{
"text": "No, we're not.",
"start": 4580.177,
"duration": 0.583
},
{
"text": "We're only passing something\ncalled to do, right?",
"start": 4580.76,
"duration": 2.041
},
{
"text": "To do may have a value\ncalled text, but in order",
"start": 4585.3,
"duration": 3.69
},
{
"text": "to actually access that, what we would\nhave to do is to do props.todo.text.",
"start": 4588.99,
"duration": 4.2
},
{
"text": "And that will allow us to\npull up the correct field.",
"start": 4596.58,
"duration": 3.84
},
{
"text": "So now we have a way to add to dos.",
"start": 4604.41,
"duration": 3.66
},
{
"text": "Great.",
"start": 4608.07,
"duration": 1.05
},
{
"text": "That doesn't make much of a to do app.",
"start": 4609.12,
"duration": 3.15
},
{
"text": "We should be able to check\nand uncheck, which we can.",
"start": 4612.27,
"duration": 3.3
},
{
"text": "But we're not really\nhandling that in state.",
"start": 4615.57,
"duration": 2.07
},
{
"text": "We have no way of telling whether\nthese to dos are checked or not,",
"start": 4617.64,
"duration": 4.832
},
{
"text": "and we also have no\nway to delete to dos.",
"start": 4622.472,
"duration": 1.708
},
{
"text": "And so we need some sort of way\nto recognize which to do is which.",
"start": 4627.88,
"duration": 6.49
},
{
"text": "What are some strategies\nto go about doing that?",
"start": 4634.37,
"duration": 3.351
},
{
"text": "Can anybody think of one?",
"start": 4637.721,
"duration": 2.959
},
{
"text": "AUDIENCE: [INAUDIBLE]",
"start": 4640.68,
"duration": 1.43
},
{
"text": "JORDAN HAYASHI: Yeah, we\nshould give each one an ID.",
"start": 4642.11,
"duration": 2.166
},
{
"text": "That way, if we can guarantee\nthat those IDs are unique,",
"start": 4644.276,
"duration": 2.444
},
{
"text": "then we'll have a unique way\nof recognizing each to do.",
"start": 4646.72,
"duration": 3.94
},
{
"text": "And so let's go ahead\nand do that real quick.",
"start": 4650.66,
"duration": 3.13
},
{
"text": "So a very easy way to do that would\nbe to do something like this--",
"start": 4653.79,
"duration": 3.45
},
{
"text": "have some value called ID declared\noutside, initialized to 0.",
"start": 4661.73,
"duration": 6.18
},
{
"text": "And every time you create a new\none, you just increment that.",
"start": 4667.91,
"duration": 5.208
},
{
"text": "And so now every single time we\nadd a to do, it has an ID field.",
"start": 4677.38,
"duration": 3.099
},
{
"text": "And we're guaranteed that\nit's unique, since it's",
"start": 4680.479,
"duration": 2.041
},
{
"text": "a counter outside the state of our app.",
"start": 4682.52,
"duration": 4.17
},
{
"text": "Does that makes sense?",
"start": 4686.69,
"duration": 2.24
},
{
"text": "So how does that help us?",
"start": 4688.93,
"duration": 1.96
},
{
"text": "So now we have a way of\nrecognizing which to do is which.",
"start": 4704.12,
"duration": 3.23
},
{
"text": "And so say we wanted to get rid of one,\nit's now pretty easy to do that, right?",
"start": 4707.35,
"duration": 6.3
},
{
"text": "So if we want to now remove to\ndo, that should now do what?",
"start": 4713.65,
"duration": 11.92
},
{
"text": "Does it make sense to have a\nremove to do with no arguments?",
"start": 4725.57,
"duration": 4.341
},
{
"text": "Not really, right?",
"start": 4729.911,
"duration": 0.749
},
{
"text": "Because we need to know exactly\nwhich to do we want to delete.",
"start": 4730.66,
"duration": 2.583
},
{
"text": "It makes sense that we want to\nadd a to do with no arguments,",
"start": 4733.243,
"duration": 3.297
},
{
"text": "because there's no particular\nto do that we want to add.",
"start": 4736.54,
"duration": 2.37
},
{
"text": "We create that text later.",
"start": 4738.91,
"duration": 1.959
},
{
"text": "But when we want to\nremove a to do, we need",
"start": 4740.869,
"duration": 1.791
},
{
"text": "to know exactly which to do to remove.",
"start": 4742.66,
"duration": 1.75
},
{
"text": "And so now we should have remove\nto do actually taking argument.",
"start": 4744.41,
"duration": 4.741
},
{
"text": "And so what argument should it take?",
"start": 4749.151,
"duration": 1.499
},
{
"text": "Well, we created some sort\nof unique identifier field,",
"start": 4750.65,
"duration": 3.02
},
{
"text": "so we can just take the ID.",
"start": 4753.67,
"duration": 3.32
},
{
"text": "And so now how are we\ngoing to remove the to do?",
"start": 4756.99,
"duration": 3.78
},
{
"text": "Well, we could do this.setState, which\nis the only way to update the state.",
"start": 4760.77,
"duration": 5.8
},
{
"text": "And the to dos are what?",
"start": 4766.57,
"duration": 3.87
},
{
"text": "They can take the old state's to dos\nand just filter them, where for each",
"start": 4770.44,
"duration": 7.72
},
{
"text": "to do, what do we want to keep?",
"start": 4778.16,
"duration": 5.71
},
{
"text": "Well, as long as the to do dot ID is\nnot equal to the ID that we pass in,",
"start": 4783.87,
"duration": 6.71
},
{
"text": "those are the to dos\nthat we want to take.",
"start": 4790.58,
"duration": 2.81
},
{
"text": "Great.",
"start": 4796.27,
"duration": 0.96
},
{
"text": "But the Delete button is not working.",
"start": 4797.23,
"duration": 3.87
},
{
"text": "Why not?",
"start": 4801.1,
"duration": 1.17
},
{
"text": "Well, our component called\nto do is not doing anything",
"start": 4802.27,
"duration": 6.18
},
{
"text": "when you click on the button.",
"start": 4808.45,
"duration": 4.03
},
{
"text": "So how might we handle that?",
"start": 4812.48,
"duration": 1.4
},
{
"text": "Well, that's where props come in.",
"start": 4816.336,
"duration": 1.374
},
{
"text": "Since we can pass any\nJavaScript value as props,",
"start": 4817.71,
"duration": 3.84
},
{
"text": "what's to stop us from just\npassing a function down?",
"start": 4821.55,
"duration": 2.85
},
{
"text": "And so when we map over\nthe to dos like this,",
"start": 4824.4,
"duration": 4.004
},
{
"text": "we can actually also\npass a function down.",
"start": 4828.404,
"duration": 8.656
},
{
"text": "So we could pass something called\nonDelete and just pass a function.",
"start": 4837.06,
"duration": 6.31
},
{
"text": "And what would that function do?",
"start": 4843.37,
"duration": 2.65
},
{
"text": "Well, it's going to call\nthis.delete removeTodo.",
"start": 4846.02,
"duration": 4.68
},
{
"text": "And what are we going to\npass in when we invoke that?",
"start": 4850.7,
"duration": 2.962
},
{
"text": "Well, this todo.id.",
"start": 4853.662,
"duration": 0.808
},
{
"text": "Does that make sense?",
"start": 4871.516,
"duration": 0.874
},
{
"text": "So we're mapping over to dos,\nand for each to do in that array,",
"start": 4875.06,
"duration": 3.69
},
{
"text": "we're going to invoke that to\ndo function, that component.",
"start": 4878.75,
"duration": 4.89
},
{
"text": "And what are we passing as props?",
"start": 4883.64,
"duration": 1.8
},
{
"text": "We're passing it this\nthing called onDelete",
"start": 4885.44,
"duration": 2.67
},
{
"text": "which is a function that invokes\nremove to do with that to do's ID.",
"start": 4888.11,
"duration": 4.19
},
{
"text": "So for each to do that\nwe create, we're passing",
"start": 4892.3,
"duration": 2.74
},
{
"text": "a unique function that\nremoves that particular to dos",
"start": 4895.04,
"duration": 4.07
},
{
"text": "from our global state.",
"start": 4899.11,
"duration": 2.547
},
{
"text": "And of course, we're also\npassing down the to dos,",
"start": 4901.657,
"duration": 2.083
},
{
"text": "so it knows exactly\nwhich text to render.",
"start": 4903.74,
"duration": 4.73
},
{
"text": "And so how do we get that to fire\nwhen we click the delete button?",
"start": 4908.47,
"duration": 3.994
},
{
"text": "Well, that function's\njust a prop, right?",
"start": 4921.18,
"duration": 3.03
},
{
"text": "So when we click that\nbutton, what should we do?",
"start": 4924.21,
"duration": 4.02
},
{
"text": "Well, we should invoke\nprops dot on delete.",
"start": 4928.23,
"duration": 2.67
},
{
"text": "And so now when we add things,\nif we click that button,",
"start": 4936.38,
"duration": 10.82
},
{
"text": "the correct one is deleted.",
"start": 4947.2,
"duration": 3.12
},
{
"text": "Again, how does it know that that first\none is the one that should be deleted?",
"start": 4950.32,
"duration": 4.11
},
{
"text": "Well, it's because each\nto do that gets rendered,",
"start": 4954.43,
"duration": 2.28
},
{
"text": "so when we map over the to dos in\nour state, we pass a unique on delete",
"start": 4956.71,
"duration": 4.17
},
{
"text": "handler down.",
"start": 4960.88,
"duration": 2.1
},
{
"text": "It's a function that gets bound to\nthat particular to do's ID field.",
"start": 4962.98,
"duration": 5.14
},
{
"text": "And so when we click that\nparticular to do's delete button,",
"start": 4968.12,
"duration": 3.68
},
{
"text": "it invokes remove to do with\nthat ID, and so it knows",
"start": 4971.8,
"duration": 2.52
},
{
"text": "to filter based on that particular ID.",
"start": 4974.32,
"duration": 5.408
},
{
"text": "Does that make sense to people?",
"start": 4979.728,
"duration": 1.984
},
{
"text": "Great.",
"start": 4989.421,
"duration": 0.499
},
{
"text": "So we've handled the two big parts--",
"start": 4989.92,
"duration": 3.51
},
{
"text": "well, the add and the delete\npart of the assignment.",
"start": 4993.43,
"duration": 3.34
},
{
"text": "And so what are we missing?",
"start": 4996.77,
"duration": 1.34
},
{
"text": "We're missing some way of tracking\nhow many to dos we have total",
"start": 4998.11,
"duration": 3.612
},
{
"text": "and how many to dos that we still\nhave to complete-- or in other words,",
"start": 5001.722,
"duration": 2.958
},
{
"text": "to dos that are unchecked.",
"start": 5004.68,
"duration": 2.91
},
{
"text": "And so now we have a choice--\nhow are we going to handle this?",
"start": 5007.59,
"duration": 2.83
},
{
"text": "Well, if we were back in\nVanilla JavaScript world,",
"start": 5010.42,
"duration": 3.047
},
{
"text": "a way to do that would\njust be to keep track",
"start": 5013.467,
"duration": 1.833
},
{
"text": "of however many unchecked there are,\nkeep track of however many total",
"start": 5015.3,
"duration": 3.21
},
{
"text": "there are.",
"start": 5018.51,
"duration": 0.99
},
{
"text": "And just mess with those values every\nsingle time we add, delete, or check",
"start": 5019.5,
"duration": 5.22
},
{
"text": "or uncheck a to do.",
"start": 5024.72,
"duration": 2.41
},
{
"text": "But in React land, that seems a\nlittle imperative for our like,",
"start": 5027.13,
"duration": 3.18
},
{
"text": "and so there might be a more\ndeclarative way to do this.",
"start": 5030.31,
"duration": 3.84
},
{
"text": "But in order to do that, we also\nneed to track every single to do,",
"start": 5034.15,
"duration": 3.59
},
{
"text": "whether it's checked or not.",
"start": 5037.74,
"duration": 1.9
},
{
"text": "And so currently our to dos are\nan ID field and a text field.",
"start": 5039.64,
"duration": 7.34
},
{
"text": "But maybe it would be better to also add\na field of whether it's checked or not.",
"start": 5046.98,
"duration": 3.63
},
{
"text": "And so now when we add to dos, we\nadd an object with three keys--",
"start": 5054.52,
"duration": 3.18
},
{
"text": "an ID which is guaranteed to be\nunique, because we're incrementing",
"start": 5057.7,
"duration": 2.97
},
{
"text": "this ID counter, which lives outside of\nour app; the texts that we prompt for;",
"start": 5060.67,
"duration": 5.17
},
{
"text": "and lastly, a value called checked,\nwhich keeps track of whether that to do",
"start": 5065.84,
"duration": 4.04
},
{
"text": "is checked or not.",
"start": 5069.88,
"duration": 2.04
},
{
"text": "And we should just\ninitialize it with false,",
"start": 5071.92,
"duration": 1.95
},
{
"text": "because there's no point\nin creating a to do.",
"start": 5073.87,
"duration": 2.102
},
{
"text": "That's already done.",
"start": 5075.972,
"duration": 3.569
},
{
"text": "Cool.",
"start": 5079.541,
"duration": 0.499
},
{
"text": "And so now we're still\nmissing one thing.",
"start": 5080.04,
"duration": 6.73
},
{
"text": "So when we create to dos, we\njust create an empty checkbox.",
"start": 5086.77,
"duration": 4.97
},
{
"text": "And so no matter what,\nit's going to be unchecked.",
"start": 5091.74,
"duration": 2.52
},
{
"text": "And so we should set that check field\nto equal whether that particular to do",
"start": 5094.26,
"duration": 5.55
},
{
"text": "is checked or not.",
"start": 5099.81,
"duration": 0.83
},
{
"text": "And so how might we do that?",
"start": 5100.64,
"duration": 1.29
},
{
"text": "Well, with props.todo.checked.",
"start": 5101.93,
"duration": 1.62
},
{
"text": "And so now that keeps track of\nwhether a to do is checked or not.",
"start": 5109.9,
"duration": 5.39
},
{
"text": "But the problem is we're not actually\nupdating our app state every time",
"start": 5115.29,
"duration": 3.1
},
{
"text": "a to do is checked or not.",
"start": 5118.39,
"duration": 1.82
},
{
"text": "And so we're going to have to\nactually handle every single time",
"start": 5120.21,
"duration": 4.08
},
{
"text": "an input is clicked.",
"start": 5124.29,
"duration": 1.71
},
{
"text": "Every single time it changes, we need\nto update our app state accordingly,",
"start": 5126.0,
"duration": 4.35
},
{
"text": "because if you look here, I'm\nclicking but nothing is happening.",
"start": 5130.35,
"duration": 8.649
},
{
"text": "Why is nothing happening?",
"start": 5138.999,
"duration": 1.041
},
{
"text": "Well, we're initializing our\nto dos to be checked if false.",
"start": 5140.04,
"duration": 4.47
},
{
"text": "We're saying, for each to do, make sure\nthat checkbox, whether it's checked",
"start": 5144.51,
"duration": 4.14
},
{
"text": "or not, is whether that to do--",
"start": 5148.65,
"duration": 2.58
},
{
"text": "its check value is true or false.",
"start": 5151.23,
"duration": 1.619
},
{
"text": "But we have no way to\nchange that from not false,",
"start": 5152.849,
"duration": 2.041
},
{
"text": "and so no matter how\nmany times I click that,",
"start": 5154.89,
"duration": 2.76
},
{
"text": "it's not going to change the\nvalue of props.todo.checked.",
"start": 5157.65,
"duration": 4.49
},
{
"text": "And so we're going to actually\nhave to write some logic in order",
"start": 5162.14,
"duration": 3.127
},
{
"text": "to handle that.",
"start": 5165.267,
"duration": 0.938
},
{
"text": "And so now let's create a new\nmethod, called toggle to do.",
"start": 5170.9,
"duration": 4.59
},
{
"text": "We're obviously going to need to\ntake an argument here, because we",
"start": 5175.49,
"duration": 3.33
},
{
"text": "need to know which to do to toggle.",
"start": 5178.82,
"duration": 3.69
},
{
"text": "And so what are we going to do here--",
"start": 5182.51,
"duration": 2.92
},
{
"text": "so again, we're going to do\nthis.setState and we can to dos.",
"start": 5185.43,
"duration": 7.04
},
{
"text": "It's this.state.todos.map.",
"start": 5192.47,
"duration": 2.12
},
{
"text": "So if you remember back a few lectures\nago, when we talked about filter, map,",
"start": 5207.8,
"duration": 5.56
},
{
"text": "and reduce, we talked about how\nmap applies a function to each",
"start": 5213.36,
"duration": 5.67
},
{
"text": "of the values in an\narray and will return",
"start": 5219.03,
"duration": 3.9
},
{
"text": "a new array with those\nvalues swapped out",
"start": 5222.93,
"duration": 3.6
},
{
"text": "with whatever this function returns.",
"start": 5226.53,
"duration": 2.4
},
{
"text": "And so how might we use that map\nfunction to handle the toggling of a",
"start": 5228.93,
"duration": 5.04
},
{
"text": "to do?",
"start": 5233.97,
"duration": 2.64
},
{
"text": "Well, first, which to do\ndo we actually care about?",
"start": 5236.61,
"duration": 4.71
},
{
"text": "We only care about the\nto do with an ID that",
"start": 5241.32,
"duration": 4.166
},
{
"text": "matches the ID that were passed, right?",
"start": 5245.486,
"duration": 1.624
},
{
"text": "So we can immediately\noff the bat say, if to do",
"start": 5247.11,
"duration": 3.81
},
{
"text": "dot ID is not equal to the ID that\nwere passed, what should we do?",
"start": 5250.92,
"duration": 7.02
},
{
"text": "AUDIENCE: [INAUDIBLE]",
"start": 5257.94,
"duration": 1.44
},
{
"text": "JORDAN HAYASHI: Yeah, we\nshouldn't do anything, right?",
"start": 5259.38,
"duration": 2.25
},
{
"text": "We should just return that to do.",
"start": 5261.63,
"duration": 1.65
},
{
"text": "OK, great.",
"start": 5266.02,
"duration": 0.5
},
{
"text": "We have one condition, but what happens\nwith the to do that we want replaced?",
"start": 5266.52,
"duration": 4.767
},
{
"text": "We should flip its checked value, right?",
"start": 5275.4,
"duration": 3.35
},
{
"text": "And so we're going to want\nto return a new object,",
"start": 5278.75,
"duration": 4.76
},
{
"text": "and what should those objects' field be?",
"start": 5283.51,
"duration": 1.7
},
{
"text": "Well, we can say, ID is to do dot ID.",
"start": 5285.21,
"duration": 5.94
},
{
"text": "We can do text is to do dot text.",
"start": 5294.79,
"duration": 4.25
},
{
"text": "And what do we want the\nchecked value to be?",
"start": 5299.04,
"duration": 2.11
},
{
"text": "AUDIENCE: [INAUDIBLE]",
"start": 5304.15,
"duration": 1.055
},
{
"text": "JORDAN HAYASHI: Yeah, the\nopposite of what it was before.",
"start": 5305.205,
"duration": 2.375
},
{
"text": "And now it looks like we should\nhave something that works.",
"start": 5313.4,
"duration": 2.93
},
{
"text": "The problem is it doesn't.",
"start": 5319.217,
"duration": 1.083
},
{
"text": "It still doesn't change anything\nwhen we click this value.",
"start": 5323.68,
"duration": 4.45
},
{
"text": "Anybody have any ideas why not?",
"start": 5328.13,
"duration": 2.97
},
{
"text": "It looks like our toggle\nto do function should work.",
"start": 5331.1,
"duration": 3.9
},
{
"text": "We're setting state,\nwe're updating the to dos,",
"start": 5335.0,
"duration": 2.829
},
{
"text": "we're mapping over this\nwith this function,",
"start": 5337.829,
"duration": 1.791
},
{
"text": "where if the to do's ID doesn't\nmatch, we don't change it.",
"start": 5339.62,
"duration": 2.62
},
{
"text": "But if it does, we flip\nthe to check value.",
"start": 5342.24,
"duration": 2.85
},
{
"text": "But when we click over here,\nnothing actually happens.",
"start": 5345.09,
"duration": 3.47
},
{
"text": "Why might that be?",
"start": 5348.56,
"duration": 1.238
},
{
"text": "Yeah?",
"start": 5349.798,
"duration": 0.5
},
{
"text": "AUDIENCE: [INAUDIBLE]",
"start": 5350.298,
"duration": 10.187
},
{
"text": "JORDAN HAYASHI: Yeah.",
"start": 5360.485,
"duration": 0.875
},
{
"text": "Exactly.",
"start": 5361.36,
"duration": 0.5
},
{
"text": "We defined this function, but we're\nnot actually calling it anywhere.",
"start": 5361.86,
"duration": 3.34
},
{
"text": "And so just like we have this\nonDelete that passes a new onDelete",
"start": 5365.2,
"duration": 3.36
},
{
"text": "function to every single\nto do, we should also",
"start": 5368.56,
"duration": 2.37
},
{
"text": "have this thing called\nonCheck or onToggle that",
"start": 5370.93,
"duration": 5.91
},
{
"text": "passes the unique function\nto each to do that we",
"start": 5376.84,
"duration": 2.82
},
{
"text": "should invoke when we toggle it.",
"start": 5379.66,
"duration": 1.56
},
{
"text": "And so let's have an arrow\nfunction here so we don't",
"start": 5381.22,
"duration": 3.6
},
{
"text": "have to worry about the this binding.",
"start": 5384.82,
"duration": 2.73
},
{
"text": "And when on toggle's invoked, we should\ndo this.toggleTodo with the todos.id.",
"start": 5387.55,
"duration": 5.43
},
{
"text": "And so now since we're\npassing a new prop here,",
"start": 5397.822,
"duration": 1.958
},
{
"text": "we should probably invoke\nthat prop at some point.",
"start": 5399.78,
"duration": 2.083
},
{
"text": "And so where are we going to do it?",
"start": 5404.202,
"duration": 1.458
},
{
"text": "Well, we can do it in the input.",
"start": 5405.66,
"duration": 2.77
},
{
"text": "We can do something like\nonChange = props.onToggle.",
"start": 5408.43,
"duration": 5.99
},
{
"text": "And how do I know that this\non change attribute exists?",
"start": 5418.44,
"duration": 2.44
},
{
"text": "Well, you can read the\ndocs and it'll be there.",
"start": 5420.88,
"duration": 2.06
},
{
"text": "Or you can wait till\nthe next lecture, when",
"start": 5422.94,
"duration": 1.95
},
{
"text": "we talk about event handling in React.",
"start": 5424.89,
"duration": 2.19
},
{
"text": "And so now we can create a to do.",
"start": 5427.08,
"duration": 3.24
},
{
"text": "And when we click it,\nlook at that-- it works.",
"start": 5430.32,
"duration": 3.22
},
{
"text": "Cool.",
"start": 5436.635,
"duration": 0.5
},
{
"text": "So now we're tracking every single\nto do, whether it's checked or not.",
"start": 5437.135,
"duration": 4.28
},
{
"text": "But that wasn't really the goal, right?",
"start": 5441.415,
"duration": 1.625
},
{
"text": "The underlying goal was to keep\ntrack of how many to dos there were,",
"start": 5443.04,
"duration": 2.833
},
{
"text": "and how many unchecked to dos there are.",
"start": 5445.873,
"duration": 2.277
},
{
"text": "And so how might we do that?",
"start": 5448.15,
"duration": 1.88
},
{
"text": "Well, first let's create some divs.",
"start": 5453.79,
"duration": 1.816
},
{
"text": "This div says to do count.",
"start": 5460.7,
"duration": 4.41
},
{
"text": "And then we're going to have\nsome JavaScript in line there,",
"start": 5467.72,
"duration": 3.57
},
{
"text": "and then maybe a div for\nunchecked to do count.",
"start": 5471.29,
"duration": 6.99
},
{
"text": "And again, some JavaScript.",
"start": 5478.28,
"duration": 3.535
},
{
"text": "Cool.",
"start": 5481.815,
"duration": 0.5
},
{
"text": "And so what is a quick and easy way\nto calculate how many to dos we have?",
"start": 5482.315,
"duration": 5.265
},
{
"text": "We're tracking all of\nour to dos in memory.",
"start": 5492.39,
"duration": 2.14
},
{
"text": "What are we using to track them?",
"start": 5494.53,
"duration": 2.23
},
{
"text": "Yeah?",
"start": 5496.76,
"duration": 0.5
},
{
"text": "AUDIENCE: [INAUDIBLE]",
"start": 5497.26,
"duration": 1.715
},
{
"text": "JORDAN HAYASHI: Yeah.",
"start": 5498.975,
"duration": 0.875
},
{
"text": "It's just an array that\nhas a length attribute,",
"start": 5499.85,
"duration": 1.958
},
{
"text": "so we can do this dot state dot\nto dos, which grabs that list,",
"start": 5501.808,
"duration": 3.572
},
{
"text": "and just do dot length.",
"start": 5505.38,
"duration": 2.61
},
{
"text": "And now we have a 0.",
"start": 5507.99,
"duration": 1.53
},
{
"text": "And if we add a to do, it\ngoes ahead and increases.",
"start": 5509.52,
"duration": 6.46
},
{
"text": "Cool.",
"start": 5515.98,
"duration": 0.5
},
{
"text": "And so how are we going to keep\ntrack of the unchecked to dos?",
"start": 5516.48,
"duration": 3.39
},
{
"text": "Well, we know we're tracking every\nsingle to do and whether it's checked",
"start": 5527.55,
"duration": 3.78
},
{
"text": "or not.",
"start": 5531.33,
"duration": 1.37
},
{
"text": "And so one strategy might\nbe to get all of the to dos,",
"start": 5532.7,
"duration": 3.13
},
{
"text": "remove all of the ones that are checked,\nand then count the number remaining.",
"start": 5535.83,
"duration": 4.77
},
{
"text": "So what functions might we use\nto implement that algorithm?",
"start": 5540.6,
"duration": 3.67
},
{
"text": "Yeah, we can use the filter function.",
"start": 5547.889,
"duration": 1.541
},
{
"text": "So we can do this dot state\ndot to dos dot filter.",
"start": 5549.43,
"duration": 4.812
},
{
"text": "And what are we going to filter by?",
"start": 5554.242,
"duration": 1.458
},
{
"text": "Well, for each to do,\nreturn if it's not checked.",
"start": 5555.7,
"duration": 5.46
},
{
"text": "And then we're left with a new array,\nand we can just grab the length.",
"start": 5566.744,
"duration": 2.916
},
{
"text": "And so if we do a test,\nnow we have it unchecked.",
"start": 5572.19,
"duration": 2.67
},
{
"text": "What happens when we check it?",
"start": 5574.86,
"duration": 1.84
},
{
"text": "Well, that's going to get filtered out\nhere, and what's left is an empty list.",
"start": 5576.7,
"duration": 5.37
},
{
"text": "And you grab the length\nof that, and we have 0.",
"start": 5582.07,
"duration": 2.54
},
{
"text": "What happens when we delete it?",
"start": 5584.61,
"duration": 1.769
},
{
"text": "Well, we don't actually\nhave to update any logic,",
"start": 5586.379,
"duration": 2.041
},
{
"text": "because everything is handled\ncompletely declaratively.",
"start": 5588.42,
"duration": 3.627
},
{
"text": "Is there anything here\nthat says, every time",
"start": 5606.74,
"duration": 2.37
},
{
"text": "we change something, make sure to go and\nchange this count that we're keeping?",
"start": 5609.11,
"duration": 4.74
},
{
"text": "Not really, right?",
"start": 5613.85,
"duration": 1.02
},
{
"text": "We just assume that we have some to\ndos and some data that's coming down,",
"start": 5614.87,
"duration": 4.06
},
{
"text": "and we just run calculations\nbased on that data.",
"start": 5618.93,
"duration": 2.51
},
{
"text": "And so we're just declaring\nwhat we want, based",
"start": 5621.44,
"duration": 2.28
},
{
"text": "on any data that's passed into us.",
"start": 5623.72,
"duration": 2.38
},
{
"text": "And so React, in that\nway, is very declarative.",
"start": 5626.1,
"duration": 3.515
},
{
"text": "And so this is great.",
"start": 5632.61,
"duration": 0.96
},
{
"text": "If we want to add any new features, we\ndon't really have to worry about, oh,",
"start": 5633.57,
"duration": 3.17
},
{
"text": "what is every single\npossible method that we",
"start": 5636.74,
"duration": 2.56
},
{
"text": "need to update this new feature with.",
"start": 5639.3,
"duration": 1.8
},
{
"text": "Whereas if we were doing\nthis in more Vanilla JS",
"start": 5641.1,
"duration": 2.427
},
{
"text": "and more an imperative\nnature, we would have",
"start": 5643.527,
"duration": 1.833
},
{
"text": "to make sure, oh, if we want to also\nkeep track of the count of checks",
"start": 5645.36,
"duration": 4.21
},
{
"text": "to dos, we're going to have to\ncheck every single method where",
"start": 5649.57,
"duration": 3.59
},
{
"text": "that number might be affected\nand go ahead and update it there.",
"start": 5653.16,
"duration": 2.91
},
{
"text": "And so keeping track of\nall that in your mind",
"start": 5656.07,
"duration": 3.18
},
{
"text": "might create bugs that\nyou might not otherwise",
"start": 5659.25,
"duration": 3.54
},
{
"text": "if the paradigm were more simple.",
"start": 5662.79,
"duration": 1.99
},
{
"text": "And so this is a much more simple way\nof saying, hey, given some app state,",
"start": 5664.78,
"duration": 4.94
},
{
"text": "go ahead and render an\napp based on that state.",
"start": 5669.72,
"duration": 3.23
},
{
"text": "Any questions?",
"start": 5675.95,
"duration": 2.42
},
{
"text": "Yeah?",
"start": 5678.37,
"duration": 0.5
},
{
"text": "AUDIENCE: [INAUDIBLE]",
"start": 5678.87,
"duration": 18.795
},
{
"text": "JORDAN HAYASHI: Can you\nrepeat your question?",
"start": 5697.665,
"duration": 1.875
},
{
"text": "AUDIENCE: [INAUDIBLE]",
"start": 5699.54,
"duration": 12.51
},
{
"text": "JORDAN HAYASHI: So the\nquestion was if we,",
"start": 5712.05,
"duration": 5.1
},
{
"text": "rather than replacing the old\nstate.todos over here with a new array,",
"start": 5717.15,
"duration": 5.88
},
{
"text": "would it still work, if we\nwere to mutate the array?",
"start": 5723.03,
"duration": 3.72
},
{
"text": "And the answer to that is, it depends.",
"start": 5726.75,
"duration": 2.144
},
{
"text": "It depends on the React\ncomponent that you're using,",
"start": 5728.894,
"duration": 2.166
},
{
"text": "and it depends on some\nother methods that you",
"start": 5731.06,
"duration": 3.07
},
{
"text": "could write that we'll talk\nabout in future lectures.",
"start": 5734.13,
"duration": 4.2
},
{
"text": "But the fact that it\ndepends is a bad thing,",
"start": 5738.33,
"duration": 2.46
},
{
"text": "because then you don't really know--",
"start": 5740.79,
"duration": 1.877
},
{
"text": "it depends on what else you've\nwritten, whether it will work or not.",
"start": 5742.667,
"duration": 2.833
},
{
"text": "But if you do it like this, where\nit's a completely new array,",
"start": 5745.5,
"duration": 2.89
},
{
"text": "it will always work.",
"start": 5748.39,
"duration": 2.37
},
{
"text": "And we'll talk about\nthis in future lectures,",
"start": 5750.76,
"duration": 3.14
},
{
"text": "but the React paradigm is\nalways to do things immutably,",
"start": 5753.9,
"duration": 4.53
},
{
"text": "which means if you ever\nchange something, replace it",
"start": 5758.43,
"duration": 2.25
},
{
"text": "with something completely new.",
"start": 5760.68,
"duration": 1.41
},
{
"text": "And that-- be a reference to that.",
"start": 5762.09,
"duration": 2.22
},
{
"text": "That way, if you're\never comparing values,",
"start": 5764.31,
"duration": 2.82
},
{
"text": "you don't have any bugs that can\nappear there if you've mutated a value.",
"start": 5767.13,
"duration": 4.23
},
{
"text": "So technically it's changed,\nbut the reference to that value",
"start": 5771.36,
"duration": 3.21
},
{
"text": "has not changed.",
"start": 5774.57,
"duration": 3.16
},
{
"text": "Does that make sense?",
"start": 5777.73,
"duration": 3.93
},
{
"text": "Cool.",
"start": 5781.66,
"duration": 0.5
},
{
"text": "Any other questions about\nReact, props, state, or anything",
"start": 5782.16,
"duration": 3.47
},
{
"text": "that we've seen today?",
"start": 5785.63,
"duration": 1.47
},
{
"text": "All right.",
"start": 5792.0,
"duration": 0.78
},
{
"text": "So we talked about to do app.JS\nand using React on the web.",
"start": 5792.78,
"duration": 8.94
},
{
"text": "But why limit React just to the web?",
"start": 5801.72,
"duration": 3.0
},
{
"text": "So there exists this thing\ncalled React Native, which",
"start": 5804.72,
"duration": 2.31
},
{
"text": "is a framework that relies on React\nCore and the core algorithms implemented",
"start": 5807.03,
"duration": 3.6
},
{
"text": "by the React library.",
"start": 5810.63,
"duration": 1.48
},
{
"text": "But it's actually a framework that\nallows you to do much, much more.",
"start": 5812.11,
"duration": 3.65
},
{
"text": "It allows us to build mobile\napps using only JavaScript.",
"start": 5815.76,
"duration": 4.56
},
{
"text": "And so when Facebook released this\nframework called React Native,",
"start": 5820.32,
"duration": 5.07
},
{
"text": "they released it with the tagline,\n\"learn once, write anywhere.\"",
"start": 5825.39,
"duration": 2.86
},
{
"text": "So you only have to\nlearn JavaScript once.",
"start": 5828.25,
"duration": 1.924
},
{
"text": "You only have to learn React once.",
"start": 5830.174,
"duration": 1.416
},
{
"text": "But you can actually write\nit, and it'll run anywhere.",
"start": 5831.59,
"duration": 3.53
},
{
"text": "And this supports both iOS and Android.",
"start": 5835.12,
"duration": 3.365
},
{
"text": "And so I'll leave you there for\nthis lecture, and next lecture,",
"start": 5838.485,
"duration": 2.625
},
{
"text": "we'll look at this thing\ncalled React Native",
"start": 5841.11,
"duration": 1.833
},
{
"text": "and how to run JavaScript\nfor mobile apps.",
"start": 5842.943,
"duration": 4.487
}
]