All language subtitles for 010 Advantages of the Composition API_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French Download
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,800 --> 00:00:05,900 In this lecture, we're going to learn about the advantages of the Composition API. 2 00:00:05,930 --> 00:00:10,220 We've become familiar with the API in these past few lectures. 3 00:00:10,220 --> 00:00:13,280 You have all the knowledge you need to start using it. 4 00:00:13,280 --> 00:00:17,960 I want to discuss one of the main advantages of using the composition API. 5 00:00:17,990 --> 00:00:21,140 We'll see how it matches up against Nixons. 6 00:00:21,380 --> 00:00:27,920 In the very first lecture, I briefly went over the reasons why you'd want to use the composition API. 7 00:00:27,950 --> 00:00:31,040 Firstly, it has better TypeScript support. 8 00:00:31,040 --> 00:00:34,640 If you're not familiar with TypeScript, that's perfectly fine. 9 00:00:34,640 --> 00:00:41,420 As you saw in the past few lectures, it isn't necessary to use TypeScript with the composition API. 10 00:00:41,690 --> 00:00:44,840 Secondly, it allows for a better organization. 11 00:00:44,840 --> 00:00:47,570 We saw firsthand how that works. 12 00:00:47,570 --> 00:00:52,100 We were able to define variables and functions in any order we like. 13 00:00:52,460 --> 00:00:58,610 Unlike the Options API, we weren't bound to defining everything in specific locations. 14 00:00:58,610 --> 00:01:04,519 If we want, we can move everything to random places in the setup function, the component will still 15 00:01:04,519 --> 00:01:05,150 work. 16 00:01:05,420 --> 00:01:08,480 Thirdly, it makes reusability easier. 17 00:01:08,480 --> 00:01:13,520 It opens up an interesting pattern where we're using code that's better than using mix ins. 18 00:01:13,520 --> 00:01:19,070 In this lecture, we're going to specifically look at how we can create reusable code. 19 00:01:19,370 --> 00:01:25,430 Before we dive into some code, there's a concept I want to discuss called a composition function. 20 00:01:25,580 --> 00:01:30,170 The definition of the word composition is the action of putting things together. 21 00:01:30,170 --> 00:01:35,870 In that regard, a composition function is a function made up of other functions. 22 00:01:35,870 --> 00:01:40,850 It's common practice to want to combine things together for reusability reasons. 23 00:01:40,850 --> 00:01:47,030 If you have common logic among components, you will most likely outsource the code in a separate file, 24 00:01:47,030 --> 00:01:49,880 import it and then load it into the component. 25 00:01:51,800 --> 00:01:57,280 Currently I'm working on the mix INS project we worked on in Lecture two of this section. 26 00:01:57,290 --> 00:02:02,210 Towards the end of the lecture, I showed you one of the biggest problems with Nixons. 27 00:02:02,210 --> 00:02:05,840 In the data object, we have a property called Offset. 28 00:02:08,360 --> 00:02:14,330 If we were to look at the mix and file, we'd see that it also has a data property called Offset. 29 00:02:16,730 --> 00:02:21,350 Despite that, switching back to the component, we aren't receiving errors. 30 00:02:21,350 --> 00:02:25,120 We won't get errors in the browser nor in the editor. 31 00:02:25,130 --> 00:02:30,410 We aren't aware of the problem this will cause because the environment we're working in won't throw 32 00:02:30,410 --> 00:02:31,160 an error. 33 00:02:31,310 --> 00:02:36,710 This will mean we'll have to investigate by looking around our code and files to find the issue. 34 00:02:36,740 --> 00:02:41,720 For something small like this, it won't take long to find the culprit of the issue. 35 00:02:41,750 --> 00:02:46,160 However, in larger projects, this can quickly become a nightmare. 36 00:02:46,550 --> 00:02:49,490 We can't even rename the properties in the component. 37 00:02:49,490 --> 00:02:55,400 If we found a conflicting name, we would have to open the mix and file and rename the property from 38 00:02:55,400 --> 00:02:55,940 there. 39 00:02:55,970 --> 00:03:01,610 Then we would have to go to every component that uses the mix and to reflect the change. 40 00:03:01,640 --> 00:03:04,370 It's not an efficient way to go about things. 41 00:03:04,700 --> 00:03:08,960 There are workaround patterns available such as high order components. 42 00:03:08,990 --> 00:03:14,090 The View team has decided to provide a solution that's much easier to work with. 43 00:03:14,120 --> 00:03:17,780 The composition API is the solution they came up with. 44 00:03:19,290 --> 00:03:23,340 Let's go back to the composition API project we were working on. 45 00:03:23,370 --> 00:03:26,390 The setup function has become quite large. 46 00:03:26,400 --> 00:03:28,470 It's starting to become unreadable. 47 00:03:28,470 --> 00:03:31,710 Let's split the function into multiple functions. 48 00:03:31,830 --> 00:03:37,920 There are two things we'll put into separate functions the code for the number, example and phrases. 49 00:03:37,920 --> 00:03:43,560 Example, we'll create a directory for holding our code in the source directory. 50 00:03:43,560 --> 00:03:45,900 Create a folder called Hooks. 51 00:03:48,180 --> 00:03:54,960 The name Hooks is a common naming convention for composition function files inside the newly created 52 00:03:54,960 --> 00:03:55,840 directory. 53 00:03:55,860 --> 00:03:58,980 Create a file called Number JS. 54 00:04:01,320 --> 00:04:04,560 We're going to export a function called use number. 55 00:04:06,790 --> 00:04:14,830 Next back in the app component will cut the num variable increment function and double computed property. 56 00:04:17,160 --> 00:04:22,170 I will paste this into the number function we exported in the number JS file. 57 00:04:24,500 --> 00:04:29,460 The ref and computed functions will need to be imported from the view package. 58 00:04:29,480 --> 00:04:31,040 We'll do that at the top. 59 00:04:33,440 --> 00:04:38,450 We'll return the num increment and double values in an object. 60 00:04:40,650 --> 00:04:46,710 We've successfully created our first composition function, just like mix ins by themselves. 61 00:04:46,710 --> 00:04:49,910 Composition functions aren't meant to be used alone. 62 00:04:49,920 --> 00:04:52,320 They're meant to be merged with a component. 63 00:04:52,560 --> 00:04:58,020 We gave our composition function a unique name by adding the word used before the name. 64 00:04:58,020 --> 00:04:59,910 It's not required to do this. 65 00:04:59,910 --> 00:05:03,930 It's common practice to prefix functions with the word use. 66 00:05:03,930 --> 00:05:08,400 It helps developers identify if the function is a composition function. 67 00:05:08,400 --> 00:05:11,310 It's a function meant to be merged with a component. 68 00:05:11,700 --> 00:05:14,640 The naming convention is inspired by React. 69 00:05:14,640 --> 00:05:19,470 If you worked with React, you're probably familiar with this naming convention. 70 00:05:19,470 --> 00:05:24,030 It's a great way to help developers to identify composition functions. 71 00:05:24,030 --> 00:05:26,100 We'll be following this practice. 72 00:05:26,370 --> 00:05:30,510 Let's merge it with the component back in the app component file. 73 00:05:30,510 --> 00:05:34,530 We'll import the use number function from the number file. 74 00:05:36,770 --> 00:05:43,550 Then right before the return statement we'll create a variable that de structures the object returned 75 00:05:43,550 --> 00:05:45,500 by the user number of function. 76 00:05:47,990 --> 00:05:50,000 In relation to reusability. 77 00:05:50,000 --> 00:05:53,600 Here's one advantage the composition API has over MCS. 78 00:05:53,600 --> 00:06:00,260 Since we don't have to retrieve every property and method from the composition function, we have the 79 00:06:00,260 --> 00:06:03,560 option of selecting what we want to grab from the function. 80 00:06:03,590 --> 00:06:09,370 When we're working with, Nixon's view will merge the entire mix in object with the component. 81 00:06:09,380 --> 00:06:11,210 We don't have a say in the matter. 82 00:06:11,210 --> 00:06:17,960 By using the composition API, we'll have an idea of what we can retrieve because of IntelliSense and 83 00:06:17,960 --> 00:06:18,290 tell us. 84 00:06:18,290 --> 00:06:21,020 Sense is a feature in Visual Studio code. 85 00:06:21,050 --> 00:06:24,080 You may have an editor that has something similar. 86 00:06:24,080 --> 00:06:27,260 It's the ability to perform code completion. 87 00:06:27,290 --> 00:06:31,070 The editor is able to detect what the function is returning. 88 00:06:31,070 --> 00:06:33,810 It'll output a list of possible values. 89 00:06:33,830 --> 00:06:39,350 This feature helps us greatly because we won't have to switch back to the file to check what's being 90 00:06:39,350 --> 00:06:40,190 returned. 91 00:06:40,310 --> 00:06:43,160 This feature isn't available with Nixon's. 92 00:06:43,340 --> 00:06:48,020 We're going to grab the NUM increments and composition properties. 93 00:06:50,200 --> 00:06:55,750 We won't need to update the return statement because the values are using the same names. 94 00:06:55,930 --> 00:06:59,360 We're finished with creating our first composition function. 95 00:06:59,380 --> 00:07:01,350 Let's work on creating another. 96 00:07:01,360 --> 00:07:07,510 We have two variables for containing the phrase We'll move these into a composition function. 97 00:07:07,510 --> 00:07:14,860 The watcher will be moved to create a file called phrase JS inside the Hooks Directory. 98 00:07:17,170 --> 00:07:20,290 We'll return a function called use phrase. 99 00:07:25,230 --> 00:07:31,980 Switch back to the app component file, we'll cut the phrase variable, reversed phrase variable and 100 00:07:31,980 --> 00:07:36,030 watch function paste them into the use phrase function. 101 00:07:38,310 --> 00:07:44,580 We'll add a return statement to return the phrase and reversed phrase variables in an object. 102 00:07:46,800 --> 00:07:48,360 At the top of the file. 103 00:07:48,360 --> 00:07:52,620 We'll import the ref and watch function from the view package. 104 00:07:54,910 --> 00:08:01,570 The composition function is ready back and the component will import the use phrase function from the 105 00:08:01,570 --> 00:08:02,680 phrase file. 106 00:08:04,830 --> 00:08:10,800 While we're here, we can update the import statement from the view package to remove the computed and 107 00:08:10,800 --> 00:08:11,930 watch functions. 108 00:08:11,940 --> 00:08:13,530 We don't need them anymore. 109 00:08:15,950 --> 00:08:22,070 Back down in the setup function, we'll create a variable that de structures the object returned by 110 00:08:22,070 --> 00:08:23,710 the use phrase function. 111 00:08:23,720 --> 00:08:27,980 We'll want to grab the phrase and reversed phrase properties. 112 00:08:30,230 --> 00:08:35,510 We don't need to update the return statement in the setup function because the names are the same. 113 00:08:35,539 --> 00:08:38,130 Let's give things a test in the browser. 114 00:08:38,150 --> 00:08:39,590 Refresh the page. 115 00:08:41,960 --> 00:08:45,900 If we press the increment button, the numbers will continue to work. 116 00:08:45,920 --> 00:08:51,020 The form for inputting a phrase works to we'll even get the phrase reversed. 117 00:08:53,190 --> 00:08:54,210 Fantastic. 118 00:08:54,210 --> 00:08:56,880 We've created two composition functions. 119 00:08:56,880 --> 00:08:58,550 They're highly reusable. 120 00:08:58,560 --> 00:09:03,930 We can use them in any component without experiencing the same issues we had before. 121 00:09:04,170 --> 00:09:06,570 We can even rename the variables. 122 00:09:06,570 --> 00:09:10,560 This flexibility isn't something we were able to do with Nixons. 123 00:09:10,560 --> 00:09:12,180 Let me show you what I mean. 124 00:09:12,180 --> 00:09:15,990 Back in the editor, we're going to modify the phrase file. 125 00:09:18,240 --> 00:09:20,900 We'll create another reactive reference. 126 00:09:20,910 --> 00:09:22,260 It'll be called NUM. 127 00:09:22,260 --> 00:09:24,540 Its value will be an empty string. 128 00:09:26,720 --> 00:09:29,030 We'll add it to the return statement. 129 00:09:31,330 --> 00:09:36,910 Back in the component file, we'll add it to the list of properties to extract from the object. 130 00:09:39,410 --> 00:09:41,340 Our editor will throw an error. 131 00:09:41,360 --> 00:09:44,900 It's saying that we already have an identifier called NUM. 132 00:09:44,900 --> 00:09:50,120 The number composition function is already returning a variable with the same name. 133 00:09:50,120 --> 00:09:53,180 We have two identifiers with the same name. 134 00:09:53,180 --> 00:09:56,720 Previously view would use whatever mix and it wanted. 135 00:09:56,720 --> 00:10:00,680 If there were conflicting names, it didn't throw an error at us. 136 00:10:00,800 --> 00:10:03,710 Things are different with the composition API. 137 00:10:03,740 --> 00:10:07,340 At the end of the day, we're writing JavaScript functions. 138 00:10:07,340 --> 00:10:09,950 This will not slide with JavaScript. 139 00:10:09,980 --> 00:10:12,950 It does not like it when there are conflicting names. 140 00:10:12,950 --> 00:10:18,230 Luckily, we can resolve this issue by renaming the variables in the component. 141 00:10:18,230 --> 00:10:22,490 We don't have to rename them in the composition functions we created. 142 00:10:22,880 --> 00:10:26,930 JavaScript allows us to rename properties we'd structure. 143 00:10:26,930 --> 00:10:31,460 We'll assign the NUM property, the name of phrase num. 144 00:10:33,780 --> 00:10:37,380 Next, we'll add it to the list of values to return. 145 00:10:39,740 --> 00:10:42,680 This reassignment will fix the issues we had. 146 00:10:42,710 --> 00:10:49,500 We can continue to use properties and methods with the same names across multiple composition functions. 147 00:10:49,520 --> 00:10:54,420 We'll know when there's an error because our editors will let us know when names clash. 148 00:10:54,440 --> 00:10:58,430 It's not like last time with Nixons where everything was up in the air. 149 00:10:58,880 --> 00:11:03,770 In terms of reusability, the composition API is far superior. 150 00:11:03,800 --> 00:11:10,370 This is one of the reasons why you may want to opt to use the composition API over the options API. 151 00:11:10,400 --> 00:11:14,450 It allows us to compose a component with little to no hassle. 152 00:11:14,750 --> 00:11:17,660 That wraps it up for the composition API. 153 00:11:17,690 --> 00:11:21,630 It's a very powerful API for composing a component. 154 00:11:21,650 --> 00:11:23,750 I want to repeat myself again. 155 00:11:23,750 --> 00:11:28,730 The composition API is not a replacement for the options API. 156 00:11:28,760 --> 00:11:34,070 The primary purpose of the composition API is to help with reusability. 157 00:11:34,400 --> 00:11:40,910 If you find yourself in a situation where you need to reuse logic, the composition API may be what 158 00:11:40,910 --> 00:11:42,050 you're looking for. 159 00:11:42,080 --> 00:11:45,440 Otherwise the options API will suffice. 160 00:11:45,650 --> 00:11:50,660 We'll continue our discussion of the composition API in the next lecture. 15953

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.