All language subtitles for 004 Getting Started with useState()_en

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
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
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian Download
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:02,460 --> 00:00:07,530 Now, it is worth mentioning that I also touched on Hooke's throughout this course, so some of the 2 00:00:07,530 --> 00:00:10,530 hooks all dive deeper into you in this module. 3 00:00:10,560 --> 00:00:15,690 Are Hoxha already mentioned throughout this course, but then I always mentioned that we will dive into 4 00:00:15,690 --> 00:00:16,239 it later. 5 00:00:16,260 --> 00:00:18,430 Well, now is dead later. 6 00:00:18,540 --> 00:00:20,570 Now we will, David, in more detail. 7 00:00:21,210 --> 00:00:24,420 And let's start with the ingredient foreign Jaspal there. 8 00:00:24,420 --> 00:00:25,710 We have a couple of inputs. 9 00:00:26,220 --> 00:00:35,330 And one thing we typically would do here is that we binde input the user enters to some state, to some 10 00:00:35,340 --> 00:00:41,310 state property, and we update that state on every keystroke and then sent the value back into the input. 11 00:00:41,850 --> 00:00:44,490 That's that two where binding you'll learn about in this course. 12 00:00:44,490 --> 00:00:46,870 And we did this multiple times when we work with inputs. 13 00:00:47,490 --> 00:00:53,700 I want to do the same here because ultimately it all starts in disappear with us gathering the user 14 00:00:53,700 --> 00:00:59,580 input so that we can make does add ingredient button work so that we do something useful when there's 15 00:00:59,580 --> 00:01:00,390 buttons pressed. 16 00:01:01,080 --> 00:01:07,140 Now, if you wouldn't know about Hooke's or if you would use react sixteen point seven or earlier, 17 00:01:07,530 --> 00:01:12,510 then you would have to transform this to a Class-Based component and use state and set state there. 18 00:01:13,230 --> 00:01:15,470 Now with react hooks, you don't have to do that. 19 00:01:15,480 --> 00:01:20,910 Instead, you can import something new from react by not just importing the default export, which is 20 00:01:20,910 --> 00:01:27,350 that react object, but by importing a named export and that is use state. 21 00:01:27,960 --> 00:01:32,550 I mentioned that all these hooks start with use and then they have various names. 22 00:01:32,700 --> 00:01:39,900 So here it's the use state and this is a crucial probably the core hook provided by REACT. 23 00:01:40,230 --> 00:01:43,500 It allows us to manage state and functional components. 24 00:01:43,890 --> 00:01:49,690 It works a bit different then state and Class-Based components, but we'll see the difference in a second. 25 00:01:50,430 --> 00:01:54,210 So how do we use it then when we go into our functional component? 26 00:01:54,210 --> 00:01:55,020 And that's important. 27 00:01:55,020 --> 00:02:01,800 You have to use it in your functional component body, let's say here and we call it we call use state. 28 00:02:02,220 --> 00:02:03,990 Now, that alone doesn't do much, though. 29 00:02:04,620 --> 00:02:11,550 Use state can be initialized with a default state and debt state can be anything, can be an object, 30 00:02:11,550 --> 00:02:16,740 can be an array, can be a number, it can be a string, a boolean can be any value. 31 00:02:16,740 --> 00:02:18,180 It doesn't have to be an object. 32 00:02:18,180 --> 00:02:19,540 It can be any value. 33 00:02:20,310 --> 00:02:26,930 So here we could create a new state that should manage to input for the title and the amount. 34 00:02:27,630 --> 00:02:29,910 So let's create an object here again. 35 00:02:29,910 --> 00:02:31,290 Doesn't have to be an object. 36 00:02:31,290 --> 00:02:37,140 I choose to use one here because I have two values which I want to manage and that would be title. 37 00:02:37,680 --> 00:02:42,810 And initially it's an empty string and amount and let's set amount also to an empty string. 38 00:02:42,900 --> 00:02:47,640 Now, you could argue that this should store a number and hence you want to set it to zero or something 39 00:02:47,640 --> 00:02:48,630 like that initially. 40 00:02:49,080 --> 00:02:55,560 But actually managing this will be easier if we said to a string because input elements, the values 41 00:02:55,560 --> 00:02:59,010 of input elements are always strings, even if it's of type. 42 00:02:59,010 --> 00:03:01,260 No, we'll always get a string. 43 00:03:01,260 --> 00:03:06,480 If we would want to work with a number in our app, then we would simply have to convert that value 44 00:03:06,480 --> 00:03:07,620 whenever we use it. 45 00:03:07,860 --> 00:03:13,650 But when we fatchett when we manage it, managing it as a string avoids some strange side effects where 46 00:03:13,650 --> 00:03:16,860 we might not be able to edit this value in the way we want to. 47 00:03:17,250 --> 00:03:19,920 So both values here are strings. 48 00:03:20,250 --> 00:03:24,300 And now, of course, the question is what does this get us? 49 00:03:24,300 --> 00:03:26,790 What does this use state call do? 50 00:03:26,910 --> 00:03:32,790 Because just that alone, of course, won't set a connection between, well, our state we're trying 51 00:03:32,790 --> 00:03:38,190 to manage here and our inputs now use state actually we return something. 52 00:03:38,820 --> 00:03:44,460 This here might be complicated to read, but this in the end describes how does function to use state 53 00:03:44,460 --> 00:03:45,940 function is defined. 54 00:03:46,200 --> 00:03:52,860 And after this colon here we see the return value and we see that the return value actually is an array 55 00:03:53,280 --> 00:03:56,160 and it's an array with exactly two elements. 56 00:03:56,160 --> 00:04:04,740 And that's important use state is built in hook, always returns an array with exactly two elements. 57 00:04:05,730 --> 00:04:13,260 The first element then always is your current state snapshot, and whenever your state updates to which 58 00:04:13,260 --> 00:04:16,810 I'll come back in a second, this component will rebuild. 59 00:04:16,829 --> 00:04:23,460 So this component function is really executed and use state, of course, there for all those executed 60 00:04:23,460 --> 00:04:23,880 again. 61 00:04:23,880 --> 00:04:31,200 But react internally saves that you already configured a state with the help of use state for this component 62 00:04:31,200 --> 00:04:37,770 and will not reinitialize it, but instead use state manages this state detached from your component, 63 00:04:37,770 --> 00:04:43,530 so to say so independent from your component, so that the state survives renders of this component, 64 00:04:43,650 --> 00:04:49,060 so that the state survives when component function gets executed again and therefore use state does 65 00:04:49,080 --> 00:04:50,340 first value which is returned. 66 00:04:50,340 --> 00:04:57,090 Here is your current state snapshot and it's a current state snapshot for this Rivendell cycle of this 67 00:04:57,090 --> 00:04:57,570 component. 68 00:04:58,170 --> 00:05:01,740 This means that when you update the state, you'll get the updated state here. 69 00:05:01,920 --> 00:05:05,610 So you get back in the rain, you state and Dederer has two elements in. 70 00:05:05,610 --> 00:05:10,950 The first element is always your current state snapshot ever the initial state or if you changed it, 71 00:05:10,950 --> 00:05:12,000 the updated state. 72 00:05:12,510 --> 00:05:15,810 That, of course, leads us to the question, how do you change the state? 73 00:05:15,810 --> 00:05:17,070 How do you update the state? 74 00:05:17,550 --> 00:05:21,030 And that's where the second element in disarray is helpful. 75 00:05:21,060 --> 00:05:24,870 As I mentioned, this array always has exactly two elements. 76 00:05:25,200 --> 00:05:27,410 First element is your current state snapshot. 77 00:05:27,600 --> 00:05:32,360 Second element is a function that allows you to update your current state. 78 00:05:32,670 --> 00:05:39,300 So this year, this strange looking thing, this dispatch thing here, that's a function that will always 79 00:05:39,300 --> 00:05:46,380 be a function which you can call with a new data for your state to update that state. 80 00:05:47,160 --> 00:05:53,160 So therefore, what we can do here, we can store this array with exactly two elements in a variable 81 00:05:53,160 --> 00:05:57,630 or in a constant here, actually, because for this render cycle, it's not going to change. 82 00:05:57,630 --> 00:06:01,500 Hence it can be a constant and all named as input state. 83 00:06:01,500 --> 00:06:02,640 The name is totally up to you. 84 00:06:02,640 --> 00:06:09,480 But in the end here we manage everything we need to manage for our well state for these two inputs here, 85 00:06:09,480 --> 00:06:10,950 hence the name input state. 86 00:06:11,890 --> 00:06:18,310 Now, did we store our array with the state snapshot and the state updating function here in input state, 87 00:06:18,610 --> 00:06:23,860 we can connect it to our inputs and there I'll use a similar approach I showed you earlier in the course 88 00:06:23,860 --> 00:06:30,040 with two way binding in the end, where we bind to value, often input to the current state snapshot 89 00:06:30,040 --> 00:06:34,180 so that we pass the current state back into the input, so to say. 90 00:06:34,540 --> 00:06:37,210 And I do this by simply referring to input state. 91 00:06:37,360 --> 00:06:42,940 And then the first element, remember, input state is an array with exactly two elements and the first 92 00:06:42,940 --> 00:06:46,470 element at Index zero will be a current state snapshot. 93 00:06:46,810 --> 00:06:50,500 So that will be well, what we want to feed back into the input. 94 00:06:50,710 --> 00:06:53,760 Now, of course, our state is an object, though not a string. 95 00:06:54,070 --> 00:07:01,450 So for the title input here, I want to access input state zero DOT title because our state is an object 96 00:07:01,450 --> 00:07:09,280 with a title and an amount property to manage while input for these two inputs and for the amount input 97 00:07:09,280 --> 00:07:15,190 here all day of Forcett value equal to input state for the first element, which is our current state 98 00:07:15,190 --> 00:07:16,420 snapshot amount. 99 00:07:17,330 --> 00:07:22,640 Now we feed the current state for the respective inputs back into the input elements. 100 00:07:23,030 --> 00:07:27,260 Of course, you all want to update the state when we type into these inputs, though. 101 00:07:27,920 --> 00:07:31,750 And for that, just as we do, it always falls in Class-Based components. 102 00:07:32,180 --> 00:07:36,190 We can listen to the on change event, which fires for every keystroke. 103 00:07:36,590 --> 00:07:41,690 And there we get this event object, some hyrum executing an anonymous function. 104 00:07:41,690 --> 00:07:46,840 In the end, we get the event object automatically and we want to update our state. 105 00:07:46,850 --> 00:07:50,900 And as I mentioned, we do this with the second element in this array. 106 00:07:51,140 --> 00:07:52,840 Use state gives us back. 107 00:07:53,240 --> 00:07:57,140 So here we access the element index one, which is the second element. 108 00:07:57,290 --> 00:08:01,440 And this is a function so we can execute it as a function by adding parentheses here. 109 00:08:02,150 --> 00:08:05,530 Now, here I'm on the title input, so I want to update the title. 110 00:08:05,540 --> 00:08:13,910 So what we do here is we simply pass a new object to input state to update our current state with this 111 00:08:13,910 --> 00:08:15,140 new state object. 112 00:08:15,650 --> 00:08:23,450 And there we can set the title property to event target value because event density change events. 113 00:08:23,450 --> 00:08:28,100 The target is our input field and the value property then holds the value the user entered. 114 00:08:28,820 --> 00:08:35,090 So let me reformat that and now repeat that same logic here for the amount and put down there. 115 00:08:35,150 --> 00:08:39,679 So there we also add on change, but of course we update the amount field. 116 00:08:41,780 --> 00:08:49,680 So with that, if we save this, that we go back to our program here and now enter Appel's five year. 117 00:08:50,330 --> 00:08:52,280 Well, we see this generally works. 118 00:08:52,280 --> 00:08:57,320 We can type here, which is a good sign, which means we're feeding the state correctly back into these 119 00:08:57,320 --> 00:08:58,010 inputs and so on. 120 00:08:58,190 --> 00:09:04,010 But we always get a warning that a component is changing a controlled input of type number to be uncontrolled. 121 00:09:04,610 --> 00:09:06,460 So what's the reason for this warning? 122 00:09:06,470 --> 00:09:10,490 What's wrong about the way we're saving our state here? 123 00:09:10,640 --> 00:09:16,280 And why am I getting a warning about the input of type number, which is our second input if I type 124 00:09:16,280 --> 00:09:17,180 into the first one? 13449

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