All language subtitles for 028 Getting Started with Fetching Data_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,190 --> 00:00:05,300 So let's now make sure that we load the data 2 00:00:05,300 --> 00:00:09,020 we store here on Firebase into our application 3 00:00:09,020 --> 00:00:10,590 into our website here 4 00:00:10,590 --> 00:00:13,900 when we load this starting page. 5 00:00:13,900 --> 00:00:16,800 Because at the moment here in the All Meetups page, 6 00:00:16,800 --> 00:00:18,600 we're still using this dummy data. 7 00:00:18,600 --> 00:00:21,020 And that's of course not the goal. 8 00:00:21,020 --> 00:00:24,870 Now to load data from our back and from our API, 9 00:00:24,870 --> 00:00:28,180 we need to send a HTTP request again 10 00:00:28,180 --> 00:00:32,270 just as we send one when we store data. 11 00:00:32,270 --> 00:00:34,610 So in the AllMeetups js file 12 00:00:34,610 --> 00:00:36,950 in the "AllMeetups Page" component 13 00:00:36,950 --> 00:00:38,560 we want to send the request. 14 00:00:38,560 --> 00:00:42,220 The question is, when do we wanna to send the request? 15 00:00:42,220 --> 00:00:46,430 And the answer is whenever this component is being rendered. 16 00:00:46,430 --> 00:00:49,163 So whenever we visit this page. 17 00:00:50,060 --> 00:00:53,300 So what you could do, what could be your first idea, 18 00:00:53,300 --> 00:00:56,690 is that directly in this component function 19 00:00:56,690 --> 00:00:59,810 before we returned to JSX code, 20 00:00:59,810 --> 00:01:02,370 we can "fetch" our data. 21 00:01:02,370 --> 00:01:06,940 We can use the same URL as we used for storing the data. 22 00:01:06,940 --> 00:01:08,350 Since you want to "fetch" data 23 00:01:08,350 --> 00:01:10,523 from the same API and node, 24 00:01:11,510 --> 00:01:14,263 and hence, we can use the same URL here. 25 00:01:15,820 --> 00:01:19,250 Now, we don't need to configure this request here 26 00:01:19,250 --> 00:01:21,500 with this second argument, 27 00:01:21,500 --> 00:01:25,900 because the default is that a "get" request is sent 28 00:01:25,900 --> 00:01:29,090 and that's exactly the kind of request we need here. 29 00:01:29,090 --> 00:01:32,840 We want to send a request to get our data. 30 00:01:32,840 --> 00:01:35,200 So we can call ""fetch"" just like this. 31 00:01:35,200 --> 00:01:38,000 And this will "fetch" this data. 32 00:01:38,000 --> 00:01:41,430 But now, unlike as with the "POST" request 33 00:01:41,430 --> 00:01:45,040 when we sent data, now, when we get data 34 00:01:45,040 --> 00:01:49,110 we are definitely also interested in the response. 35 00:01:49,110 --> 00:01:51,780 Now since "fetch" returns a promise, 36 00:01:51,780 --> 00:01:55,460 we can of course use the "then" method here 37 00:01:55,460 --> 00:02:00,460 and change this after "then" to handle this response. 38 00:02:00,620 --> 00:02:02,960 So we pass an anonymous function to "then" 39 00:02:02,960 --> 00:02:05,190 and here we'll get the response object 40 00:02:05,190 --> 00:02:07,310 as an argument automatically 41 00:02:07,310 --> 00:02:09,900 because that's how the ""fetch"" function works. 42 00:02:09,900 --> 00:02:13,610 It returns a promise, which resolves to the actual response, 43 00:02:13,610 --> 00:02:14,993 at some point in time. 44 00:02:15,850 --> 00:02:18,680 Now, from that response, we want to read the body. 45 00:02:18,680 --> 00:02:20,340 We want to read the data 46 00:02:20,340 --> 00:02:23,230 and we can do that with the "json" method. 47 00:02:23,230 --> 00:02:24,810 That's a method which exists 48 00:02:24,810 --> 00:02:27,510 on this response object out of the box. 49 00:02:27,510 --> 00:02:28,900 And this will give us access 50 00:02:28,900 --> 00:02:32,800 to that data automatically converted from "json" 51 00:02:32,800 --> 00:02:35,410 to a plain JavaScript object. 52 00:02:35,410 --> 00:02:36,577 But here's a gotcha, 53 00:02:36,577 --> 00:02:40,200 "json" will actually return a promise as well. 54 00:02:40,200 --> 00:02:43,520 So we also need to wait for this promise to resolve. 55 00:02:43,520 --> 00:02:47,320 So I actually will return "response,Json" here 56 00:02:47,320 --> 00:02:49,480 and add another "then" block, 57 00:02:49,480 --> 00:02:52,390 where we then get the actual data. 58 00:02:52,390 --> 00:02:54,197 And it's now in this second 59 00:02:54,197 --> 00:02:58,750 "then" block where we can work on that data. 60 00:02:58,750 --> 00:03:01,320 We could also add error handling 61 00:03:01,320 --> 00:03:03,740 and we do that in the complete guide course. 62 00:03:03,740 --> 00:03:05,940 But for the moment here, let's focus 63 00:03:05,940 --> 00:03:09,193 on just getting that data and using that data. 64 00:03:10,030 --> 00:03:13,150 So here in this "then" block, we get the data. 65 00:03:13,150 --> 00:03:15,960 What do we want to do with this data now? 66 00:03:15,960 --> 00:03:18,580 In the end, we want to extract an array 67 00:03:18,580 --> 00:03:22,530 of meetups from that response data and pass that 68 00:03:22,530 --> 00:03:25,913 as a value to the meetups prop on the meetup list. 69 00:03:26,750 --> 00:03:28,970 Now we'll have a problem here though. 70 00:03:28,970 --> 00:03:31,070 Since ""fetch"" returns a promise 71 00:03:31,070 --> 00:03:35,940 and we're in this promise chain, JavaScript does not wait 72 00:03:35,940 --> 00:03:40,570 for this promise to complete before we return here. 73 00:03:40,570 --> 00:03:43,750 Now you could think that we can use "async await" 74 00:03:43,750 --> 00:03:46,420 here to wait until this is done, 75 00:03:46,420 --> 00:03:49,060 but that would be bad because that would mean 76 00:03:49,060 --> 00:03:53,510 that the entire component function now returns a promise 77 00:03:53,510 --> 00:03:56,130 because that's what the "async" keyword does. 78 00:03:56,130 --> 00:03:57,580 It converts this function 79 00:03:57,580 --> 00:03:59,850 to a function that returns a promise 80 00:03:59,850 --> 00:04:03,330 and that then no longer qualifies 81 00:04:03,330 --> 00:04:05,970 as a valid React component 82 00:04:05,970 --> 00:04:09,940 because React component functions must be synchronous 83 00:04:09,940 --> 00:04:12,080 and must not return a promise 84 00:04:12,080 --> 00:04:16,350 but instead they have to directly return JSX. 85 00:04:16,350 --> 00:04:19,350 So we can't use "async, await" weight here. 86 00:04:19,350 --> 00:04:23,090 Hence we can't defer returning a value 87 00:04:23,090 --> 00:04:25,270 until we have a response. 88 00:04:25,270 --> 00:04:28,350 Instead, what we need to do is we need to return 89 00:04:28,350 --> 00:04:32,860 some temporary JSX code, for example, a loading spinner 90 00:04:32,860 --> 00:04:34,460 or something like this. 91 00:04:34,460 --> 00:04:37,120 And then once we have the response, we want to 92 00:04:37,120 --> 00:04:40,180 update the returned JSX code. 93 00:04:40,180 --> 00:04:43,790 And how do we change what's visible on the screen? 94 00:04:43,790 --> 00:04:45,000 With "state". 95 00:04:45,000 --> 00:04:48,150 That's what we learned earlier already. 96 00:04:48,150 --> 00:04:52,720 We can use "state" to change what's displayed on the screen 97 00:04:52,720 --> 00:04:55,470 when certain conditions change. 98 00:04:55,470 --> 00:04:58,730 Before we did that, when the user clicked a button 99 00:04:58,730 --> 00:05:01,350 and we wanted to show an overlay. 100 00:05:01,350 --> 00:05:04,893 Now I want to change the "state" once we have response data. 101 00:05:05,750 --> 00:05:09,560 So for that here in this AllMeetups JS file, 102 00:05:09,560 --> 00:05:13,610 we can import "useState" from React. 103 00:05:13,610 --> 00:05:16,120 So this "useState" hook. 104 00:05:16,120 --> 00:05:19,907 And then register some state here with "useState". 105 00:05:21,400 --> 00:05:24,693 And here we could, for example, set up some loading state. 106 00:05:25,660 --> 00:05:29,530 So name the constant "loading", or "isLoading" 107 00:05:29,530 --> 00:05:33,610 and add a "setIsloading" state updating function. 108 00:05:33,610 --> 00:05:35,970 Because you learned that "useState" 109 00:05:35,970 --> 00:05:39,490 always returns an array with exactly two elements 110 00:05:39,490 --> 00:05:42,930 where the first element is the current state snapshot. 111 00:05:42,930 --> 00:05:47,270 And the second element is a function for updating the state. 112 00:05:47,270 --> 00:05:50,660 And when we have data we want to set "isLoading" 113 00:05:50,660 --> 00:05:53,850 to "false" again, because we're not loading anymore 114 00:05:53,850 --> 00:05:56,770 but actually the initial state here should be "true", 115 00:05:56,770 --> 00:05:58,090 not "false". 116 00:05:58,090 --> 00:06:00,240 So we start in a loading state 117 00:06:00,240 --> 00:06:04,520 and then we set it to false, once we have the data. 118 00:06:04,520 --> 00:06:09,520 And then with that, we could check if we are loading here 119 00:06:09,840 --> 00:06:13,310 in the component function, before we return, I check 120 00:06:13,310 --> 00:06:16,450 if we are loading, and if we are, 121 00:06:16,450 --> 00:06:21,090 then I want to return another piece of JSX code, 122 00:06:21,090 --> 00:06:22,670 where I, for example, 123 00:06:22,670 --> 00:06:26,320 have a section with a paragraph where I say, "loading", 124 00:06:26,320 --> 00:06:27,623 something like this. 125 00:06:28,480 --> 00:06:31,810 So that's my fallback content, which I want to return 126 00:06:31,810 --> 00:06:34,570 in this component whilst we are loading. 127 00:06:34,570 --> 00:06:37,490 And we only return the actual meetup list 128 00:06:37,490 --> 00:06:40,373 if we're done loading, so if we have the data. 129 00:06:41,300 --> 00:06:42,500 But speaking of that, 130 00:06:42,500 --> 00:06:45,090 there we're missing an important point. 131 00:06:45,090 --> 00:06:47,830 At the moment, we're just managing the loading state. 132 00:06:47,830 --> 00:06:49,270 We're not doing anything 133 00:06:49,270 --> 00:06:52,053 with the actual data that is being fetched. 134 00:06:53,160 --> 00:06:57,810 For this we could add another "state" to this component 135 00:06:57,810 --> 00:07:00,420 and that is something we haven't done before 136 00:07:00,420 --> 00:07:04,050 but you can have multiple pieces of "state" in one 137 00:07:04,050 --> 00:07:05,460 and the same component. 138 00:07:05,460 --> 00:07:06,640 As many as you need. 139 00:07:06,640 --> 00:07:08,800 There is no restriction here. 140 00:07:08,800 --> 00:07:12,770 And this could be our array of meetups 141 00:07:12,770 --> 00:07:15,080 and it could be an empty array initially 142 00:07:15,080 --> 00:07:17,260 but eventually we want to override this 143 00:07:17,260 --> 00:07:20,040 once we've "fetched our data from the backend, 144 00:07:20,040 --> 00:07:21,453 from the API. 145 00:07:22,340 --> 00:07:25,064 So here, I'll name this "loadedMeetups" 146 00:07:25,064 --> 00:07:25,897 and "setLoadedMeetups". 147 00:07:27,969 --> 00:07:29,353 And here we want to set "loadedMeetups" to "data" let's say. 148 00:07:32,600 --> 00:07:35,010 That will not be the final step, 149 00:07:35,010 --> 00:07:38,150 but for the moment we can do it like this. 150 00:07:38,150 --> 00:07:40,430 And then we could pass the "loadedMeetups" 151 00:07:40,430 --> 00:07:45,300 instead of the dummy data to our meetups prop down there. 152 00:07:45,300 --> 00:07:46,970 And that means that now 153 00:07:46,970 --> 00:07:50,160 we can also get rid of that dummy data. 154 00:07:50,160 --> 00:07:52,823 So I will delete this dummy data constant. 155 00:07:53,840 --> 00:07:58,790 Now don't save this file yet though, because this code 156 00:07:58,790 --> 00:08:01,350 will cause an infinite loop. 157 00:08:01,350 --> 00:08:03,000 What's the problem here. 158 00:08:03,000 --> 00:08:08,000 The problem is related to "state" and our HTTP request. 159 00:08:08,600 --> 00:08:11,270 You might recall that I mentioned, 160 00:08:11,270 --> 00:08:13,560 when I introduced state, 161 00:08:13,560 --> 00:08:16,940 that when we call this state updating function, 162 00:08:16,940 --> 00:08:19,730 we tell react that it should 163 00:08:19,730 --> 00:08:24,730 re-execute this component function and re-evaluate it 164 00:08:24,730 --> 00:08:28,130 and then return the updated JSX code. 165 00:08:28,130 --> 00:08:29,910 That's the idea of "state". 166 00:08:29,910 --> 00:08:33,840 It allows us to reevaluate a component 167 00:08:33,840 --> 00:08:37,429 and possibly render different content on the screen 168 00:08:37,429 --> 00:08:39,980 whenever state changes. 169 00:08:39,980 --> 00:08:43,299 The problem here is, that we change "state" 170 00:08:43,299 --> 00:08:45,760 once we made our requests. 171 00:08:45,760 --> 00:08:48,823 We change the loading and the loaded meetup state. 172 00:08:49,920 --> 00:08:53,090 Then this component function will execute again. 173 00:08:53,090 --> 00:08:54,380 What does this mean? 174 00:08:54,380 --> 00:08:57,920 It means that this "fetch" function will run again 175 00:08:57,920 --> 00:09:00,220 and we send another request. 176 00:09:00,220 --> 00:09:03,910 And once that's done, we update the state again 177 00:09:03,910 --> 00:09:06,430 which means the component function executes again 178 00:09:06,430 --> 00:09:10,290 and "fetch" is called again and so on and so on. 179 00:09:10,290 --> 00:09:15,180 So we have an infinite loop and that breaks our application 180 00:09:15,180 --> 00:09:18,280 and spams our API with requests. 181 00:09:18,280 --> 00:09:20,990 And it's not something we want. 182 00:09:20,990 --> 00:09:22,823 How can we prevent this? 14421

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