All language subtitles for 002 Listening to Events & Working with Event Handlers_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
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
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
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:02,090 --> 00:00:04,360 So this is our demo application 2 00:00:04,360 --> 00:00:07,520 at the moment, and, of course, it's fairly static. 3 00:00:07,520 --> 00:00:09,880 We have this output here on the screen, 4 00:00:09,880 --> 00:00:13,570 and we reached that output with help of our components. 5 00:00:13,570 --> 00:00:16,880 But as I mentioned, this never changes, it's static. 6 00:00:16,880 --> 00:00:19,870 We only have one state in this application, 7 00:00:19,870 --> 00:00:22,573 and that's this initial state which we see here. 8 00:00:23,460 --> 00:00:27,350 Now, let's dive into how we can react to user events 9 00:00:27,350 --> 00:00:30,340 before we then have a look how such events 10 00:00:30,340 --> 00:00:32,780 could trigger a state change. 11 00:00:32,780 --> 00:00:35,080 And when we wanna start with events, 12 00:00:35,080 --> 00:00:37,580 I wanna start fairly simple. 13 00:00:37,580 --> 00:00:40,020 I wanna start with clicks on a button 14 00:00:40,020 --> 00:00:41,580 to which you wanna react. 15 00:00:41,580 --> 00:00:43,820 And to get started with all of that, 16 00:00:43,820 --> 00:00:45,830 I'll go to the ExpenseItem, 17 00:00:45,830 --> 00:00:49,770 where we output the title, the amount, and so on. 18 00:00:49,770 --> 00:00:54,330 And in there, I will also add a button below that div 19 00:00:54,330 --> 00:00:56,483 right before we close the card. 20 00:00:57,600 --> 00:01:01,200 And there, I will just say Change Title. 21 00:01:01,200 --> 00:01:05,290 And if we now save that, we will see this button here. 22 00:01:05,290 --> 00:01:08,070 Now the styling is definitely not great, 23 00:01:08,070 --> 00:01:10,810 but this is also just a temporary button. 24 00:01:10,810 --> 00:01:12,590 We're going to remove it later. 25 00:01:12,590 --> 00:01:13,790 I'm just using it here 26 00:01:13,790 --> 00:01:17,100 so that we can start diving into events and state. 27 00:01:17,100 --> 00:01:20,570 Later we'll use what we learned in better places, 28 00:01:20,570 --> 00:01:24,410 in places that really help our application. 29 00:01:24,410 --> 00:01:26,740 But for a start, we now have these buttons, 30 00:01:26,740 --> 00:01:29,230 one per expense item component. 31 00:01:29,230 --> 00:01:31,840 And my goal now is simple. 32 00:01:31,840 --> 00:01:34,250 When this button here is clicked, 33 00:01:34,250 --> 00:01:37,970 I wanna change the title which is being output here. 34 00:01:37,970 --> 00:01:40,320 Initially, the title which is being output 35 00:01:40,320 --> 00:01:43,310 is the title we get via props. 36 00:01:43,310 --> 00:01:46,690 But I wanna change it whenever this button is clicked. 37 00:01:46,690 --> 00:01:49,060 And therefore, we, of course, first of all, 38 00:01:49,060 --> 00:01:52,820 need to find out how exactly we can react 39 00:01:52,820 --> 00:01:54,820 to button clicks in React. 40 00:01:54,820 --> 00:01:58,180 And it turns out that this is rather simple. 41 00:01:58,180 --> 00:02:03,030 On all built-in HTML elements, like divs and h2, 42 00:02:03,030 --> 00:02:05,560 and especially all the buttons and so on, 43 00:02:05,560 --> 00:02:10,560 we have full access to all these native DOM events 44 00:02:11,170 --> 00:02:13,150 which we can listen to. 45 00:02:13,150 --> 00:02:17,410 So if you search for HTML button element, for example, 46 00:02:17,410 --> 00:02:19,920 you'll find an article about this button. 47 00:02:19,920 --> 00:02:23,410 And there, if you click on the DOM Interface here, 48 00:02:23,410 --> 00:02:27,500 this HTML button element, you see from which other elements 49 00:02:27,500 --> 00:02:30,410 or from whichever classes this inherits in the end. 50 00:02:30,410 --> 00:02:33,420 And here you see that the HTML button element 51 00:02:33,420 --> 00:02:36,750 is a more specific form of the HTML element, 52 00:02:36,750 --> 00:02:39,060 which is a more specific form of the element, 53 00:02:39,060 --> 00:02:42,680 which is a more specific form of the node and so on. 54 00:02:42,680 --> 00:02:46,460 And if we click on Element, for example, 55 00:02:46,460 --> 00:02:50,580 we actually will see that there are a bunch of events 56 00:02:50,580 --> 00:02:53,503 to which we can listen, for example, click and blur. 57 00:02:54,380 --> 00:02:57,440 And basically, for all these default events, 58 00:02:57,440 --> 00:03:01,060 there is a prop equivalent in React, 59 00:03:01,060 --> 00:03:04,400 which we can add to these built-in HTML elements 60 00:03:04,400 --> 00:03:06,110 to listen to these events. 61 00:03:06,110 --> 00:03:10,550 So instead of adding a listener as we would normally do it, 62 00:03:10,550 --> 00:03:14,240 by for example selecting an element by ID 63 00:03:14,240 --> 00:03:16,980 and then adding an event listener like this, 64 00:03:16,980 --> 00:03:19,970 which would be the imperative way of doing that, 65 00:03:19,970 --> 00:03:22,630 in React, we add an event listener 66 00:03:22,630 --> 00:03:26,370 by going to the JSX element, like this button. 67 00:03:26,370 --> 00:03:28,880 And there we add a special prop. 68 00:03:28,880 --> 00:03:32,960 But now it's not a prop which sets some value 69 00:03:32,960 --> 00:03:36,320 for this button, but instead it's a prop which starts 70 00:03:36,320 --> 00:03:38,160 with on. 71 00:03:38,160 --> 00:03:42,450 Because React exposes all these default events 72 00:03:42,450 --> 00:03:45,230 as props which start with on. 73 00:03:45,230 --> 00:03:47,610 And for example, here we can add onClick. 74 00:03:49,170 --> 00:03:51,940 Now, what this does is it adds an event listener 75 00:03:51,940 --> 00:03:54,313 for click events to this button. 76 00:03:55,350 --> 00:03:57,460 Now we just need to define what should happen 77 00:03:57,460 --> 00:03:59,110 when such a click occurs. 78 00:03:59,110 --> 00:04:01,410 And we do that by assigning a value 79 00:04:01,410 --> 00:04:03,090 to this click event. 80 00:04:03,090 --> 00:04:06,490 And the value here, and that's now important, 81 00:04:06,490 --> 00:04:08,040 of course, has to be code 82 00:04:08,040 --> 00:04:11,740 which should be executed when that click occurs. 83 00:04:11,740 --> 00:04:14,480 And how do we define codes that should eventually 84 00:04:14,480 --> 00:04:17,350 be executed in programming? 85 00:04:17,350 --> 00:04:20,250 Well, we create a function. 86 00:04:20,250 --> 00:04:22,860 So all these on props, 87 00:04:22,860 --> 00:04:27,090 all these event handler props, want a function 88 00:04:27,090 --> 00:04:31,620 as a value, a function passed as a value for onClick 89 00:04:31,620 --> 00:04:33,650 and all these other on props 90 00:04:33,650 --> 00:04:36,803 which then is executed when that event occurs. 91 00:04:37,940 --> 00:04:41,480 Now, here, we can either create this function on the fly. 92 00:04:41,480 --> 00:04:42,650 For example, like this, 93 00:04:42,650 --> 00:04:46,230 we can create an anonymous arrow function like this. 94 00:04:46,230 --> 00:04:49,300 And then here we could console log Clicked, 95 00:04:49,300 --> 00:04:50,393 if we wanted to. 96 00:04:51,420 --> 00:04:54,160 And if we save that and we go back 97 00:04:54,160 --> 00:04:57,230 and open the developer tools here in the browser, 98 00:04:57,230 --> 00:05:00,080 and there we go to the console part, 99 00:05:00,080 --> 00:05:02,103 if I click the title, we see clicked. 100 00:05:03,150 --> 00:05:04,690 So this is how that works. 101 00:05:04,690 --> 00:05:08,240 This is how simple it is to add an event listener 102 00:05:08,240 --> 00:05:10,333 to a React element. 103 00:05:11,500 --> 00:05:14,180 Now, typically, you don't just wanna work 104 00:05:14,180 --> 00:05:18,360 with such anonymous inline arrow functions though. 105 00:05:18,360 --> 00:05:21,160 By the way, it doesn't have to be an arrow function. 106 00:05:21,160 --> 00:05:24,310 You could also create a function with the function keyword. 107 00:05:24,310 --> 00:05:26,650 You don't wanna overdo it at least. 108 00:05:26,650 --> 00:05:29,830 Because it would mean that you put a lot of code here 109 00:05:29,830 --> 00:05:34,100 into your JSX block, into your JSX code, 110 00:05:34,100 --> 00:05:37,320 and that's generally not a good idea. 111 00:05:37,320 --> 00:05:38,820 You wanna keep that lean 112 00:05:38,820 --> 00:05:43,200 and not put too much logic into your JSX code. 113 00:05:43,200 --> 00:05:46,710 Instead, you typically wanna define a function 114 00:05:46,710 --> 00:05:48,550 before you return. 115 00:05:48,550 --> 00:05:51,490 And you can do that either with the function keyword 116 00:05:51,490 --> 00:05:55,020 or, again, by creating a const or a variable 117 00:05:55,020 --> 00:05:57,410 which holds an arrow function. 118 00:05:57,410 --> 00:06:01,720 So here we could add a clickHandler const, 119 00:06:01,720 --> 00:06:05,270 the name is up to you, which holds an arrow function, 120 00:06:05,270 --> 00:06:09,450 like this, and in there, I can also say Clicked 121 00:06:09,450 --> 00:06:11,970 with a couple of exclamation marks. 122 00:06:11,970 --> 00:06:14,080 And then here, for this button, 123 00:06:14,080 --> 00:06:16,523 we just point at clickHandler. 124 00:06:17,490 --> 00:06:21,170 And that's important, by the way, we just point at it. 125 00:06:21,170 --> 00:06:22,660 We don't execute it here. 126 00:06:22,660 --> 00:06:25,660 You don't add parentheses here. 127 00:06:25,660 --> 00:06:28,850 Instead, you just repeat the name of the function. 128 00:06:28,850 --> 00:06:32,630 No matter if you created that function like this 129 00:06:32,630 --> 00:06:37,150 or with the function keyword, this does not matter. 130 00:06:37,150 --> 00:06:38,980 No matter how you defined it, 131 00:06:38,980 --> 00:06:42,070 you just point at it like this. 132 00:06:42,070 --> 00:06:43,280 Why? 133 00:06:43,280 --> 00:06:46,040 Because if you would add parentheses here, 134 00:06:46,040 --> 00:06:48,460 JavaScript would execute this 135 00:06:48,460 --> 00:06:51,040 when this line of code is being parsed. 136 00:06:51,040 --> 00:06:53,110 And this line of code is being parsed 137 00:06:53,110 --> 00:06:55,490 when the JSX code is returned. 138 00:06:55,490 --> 00:06:57,970 So it's then not executing clickHandler 139 00:06:57,970 --> 00:07:02,220 when the click occurs but when this JSX code is evaluated, 140 00:07:02,220 --> 00:07:04,830 and that would be too early. 141 00:07:04,830 --> 00:07:08,320 That's why instead we just point at the clickHandler. 142 00:07:08,320 --> 00:07:10,440 We just point at the function. 143 00:07:10,440 --> 00:07:14,980 We pass a pointer at this function as a value to onClick, 144 00:07:14,980 --> 00:07:18,000 and then React basically memorizes this 145 00:07:18,000 --> 00:07:23,000 and executes the function for us whenever the click occurs 146 00:07:23,110 --> 00:07:26,280 so that it's not executed when this is evaluated 147 00:07:26,280 --> 00:07:29,343 but when the click occurs, which is exactly what we want. 148 00:07:30,910 --> 00:07:34,113 So now when I save that and I reload here, 149 00:07:35,440 --> 00:07:36,700 you'll see now we have clicked 150 00:07:36,700 --> 00:07:38,570 with multiple exclamation marks, 151 00:07:38,570 --> 00:07:41,263 which is the console log from this const. 152 00:07:42,400 --> 00:07:46,600 And that is how we can add event listeners to our elements. 153 00:07:46,600 --> 00:07:49,800 To all these built-in HTML elements, 154 00:07:49,800 --> 00:07:53,720 we can add supported event listeners basically. 155 00:07:53,720 --> 00:07:55,910 OnClick, for example, basically is available 156 00:07:55,910 --> 00:07:57,340 on every element. 157 00:07:57,340 --> 00:08:01,110 Some events are only available on specific elements. 158 00:08:01,110 --> 00:08:05,140 But that is all based on the default DOM behavior. 159 00:08:05,140 --> 00:08:08,760 If an element supports an event, then you can add a listener 160 00:08:08,760 --> 00:08:11,290 with React by adding such an on 161 00:08:11,290 --> 00:08:13,600 and then the event name prop. 162 00:08:13,600 --> 00:08:15,510 Just make sure that the event name 163 00:08:15,510 --> 00:08:17,610 starts with a capital character, 164 00:08:17,610 --> 00:08:19,533 like in the case of onClick here. 165 00:08:20,440 --> 00:08:22,910 And then you just point at a function, 166 00:08:22,910 --> 00:08:27,910 either defined in line here or, better, defined upfront, 167 00:08:27,980 --> 00:08:30,870 and React will execute that function for you 168 00:08:30,870 --> 00:08:32,500 when that event occurs. 169 00:08:32,500 --> 00:08:34,082 That's how you add events. 170 00:08:35,429 --> 00:08:38,549 Now, one word about naming these functions. 171 00:08:38,549 --> 00:08:41,270 It is a convention which you don't have to follow 172 00:08:41,270 --> 00:08:43,840 but which you might consider following 173 00:08:43,840 --> 00:08:46,820 that you name your functions like this. 174 00:08:46,820 --> 00:08:49,340 If they are triggered upon an event, 175 00:08:49,340 --> 00:08:51,870 that you end with handler. 176 00:08:51,870 --> 00:08:53,500 This is not a must-do, 177 00:08:53,500 --> 00:08:56,040 and not every React developer does it, 178 00:08:56,040 --> 00:08:58,650 but it is a preference I personally have. 179 00:08:58,650 --> 00:09:02,070 And you will see it in a couple of projects out there 180 00:09:02,070 --> 00:09:04,470 so that it's clear that this is a function 181 00:09:04,470 --> 00:09:08,270 which is not called by us, somewhere else in our code, 182 00:09:08,270 --> 00:09:10,300 but that it is a function which we attached 183 00:09:10,300 --> 00:09:13,160 to an event listener so that it's eventually called 184 00:09:13,160 --> 00:09:15,810 by React when that event occurs. 185 00:09:15,810 --> 00:09:19,510 That's why I'm naming my event handler functions like this, 186 00:09:19,510 --> 00:09:21,180 ending with handler. 187 00:09:21,180 --> 00:09:23,570 But again, this is just my preference. 188 00:09:23,570 --> 00:09:25,620 You see it in a couple of projects. 189 00:09:25,620 --> 00:09:27,530 You might wanna consider doing that, 190 00:09:27,530 --> 00:09:28,763 but you don't have to. 191 00:09:29,650 --> 00:09:33,540 But now with that, we know how we can react to events. 192 00:09:33,540 --> 00:09:35,030 How can we now change 193 00:09:35,030 --> 00:09:37,163 what is showing up on the screen though? 15319

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