All language subtitles for 008 Using useEffect with Redux_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
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 Download
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:01,303 --> 00:00:03,400 Now one possible, 2 00:00:03,400 --> 00:00:06,620 better solution is actually totally obvious 3 00:00:06,620 --> 00:00:08,200 once you see it. 4 00:00:08,200 --> 00:00:10,860 Instead of using this approach, 5 00:00:10,860 --> 00:00:13,560 which I implemented in the last lecture 6 00:00:13,560 --> 00:00:15,400 and which I'll therefore now get rid of, 7 00:00:15,400 --> 00:00:17,620 instead of doing that, 8 00:00:17,620 --> 00:00:21,150 and hence I can get rid of this cart selector 9 00:00:21,150 --> 00:00:23,450 and of the useSelector import. 10 00:00:23,450 --> 00:00:24,870 Instead of doing that 11 00:00:24,870 --> 00:00:28,900 we wanna stick to the approach from before. 12 00:00:28,900 --> 00:00:32,060 We wanna dispatch the addItemToCart action 13 00:00:32,060 --> 00:00:36,530 and do all this heavy work inside of the Reducer function. 14 00:00:36,530 --> 00:00:41,530 But if we now wanna sync our new state to the server 15 00:00:42,150 --> 00:00:44,010 so if we wanna update the server 16 00:00:44,010 --> 00:00:45,360 with that new state 17 00:00:45,360 --> 00:00:47,350 which we derived on the front end 18 00:00:47,350 --> 00:00:49,560 we can simply switch the order. 19 00:00:49,560 --> 00:00:52,370 We can first do the work on the front end 20 00:00:52,370 --> 00:00:54,970 and let Redux update its store. 21 00:00:54,970 --> 00:00:57,330 And then in a second step thereafter 22 00:00:57,330 --> 00:00:59,730 we send the request to the server 23 00:00:59,730 --> 00:01:01,440 but we don't necessarily need 24 00:01:01,440 --> 00:01:04,340 to do that here inside of the Reducer function 25 00:01:04,340 --> 00:01:07,130 where we wouldn't be allowed to do it. 26 00:01:07,130 --> 00:01:08,740 Instead, we can, for example 27 00:01:08,740 --> 00:01:11,740 do it in the ProductItem.js file 28 00:01:11,740 --> 00:01:13,640 or in a totally different file. 29 00:01:13,640 --> 00:01:15,930 Let's say in App.js 30 00:01:16,900 --> 00:01:19,010 as our root component. 31 00:01:19,010 --> 00:01:21,130 There we can simply get hold 32 00:01:21,130 --> 00:01:23,660 of our overall cart 33 00:01:23,660 --> 00:01:26,080 by basically using useSelector 34 00:01:26,080 --> 00:01:29,210 and listening to changes to our cart state. 35 00:01:29,210 --> 00:01:32,080 And whenever our cart state does change 36 00:01:32,080 --> 00:01:34,570 we can send the Http request. 37 00:01:34,570 --> 00:01:36,120 So that's what I'll do here. 38 00:01:36,120 --> 00:01:37,510 I'll use useSelector, 39 00:01:37,510 --> 00:01:39,360 which we're already importing 40 00:01:39,360 --> 00:01:41,980 to get hold of my overall cart 41 00:01:43,780 --> 00:01:46,363 and then I'll store it in that constant like this. 42 00:01:47,260 --> 00:01:49,140 And now we can use useEffect 43 00:01:50,090 --> 00:01:52,040 which we import from React 44 00:01:52,040 --> 00:01:55,930 to watch for changes in our cart state, 45 00:01:55,930 --> 00:01:56,763 because you learned 46 00:01:56,763 --> 00:01:59,630 that useEffect allows you to run side effects. 47 00:01:59,630 --> 00:02:01,900 So it sounds like a good choice here, 48 00:02:01,900 --> 00:02:03,850 and it allows you to run an effect 49 00:02:03,850 --> 00:02:07,810 whenever some dependency changes. 50 00:02:07,810 --> 00:02:12,740 So we import useEffect from React 51 00:02:12,740 --> 00:02:14,740 and we can then call useEffect here 52 00:02:14,740 --> 00:02:17,060 in the app component, for example 53 00:02:17,060 --> 00:02:19,560 and we could be doing this in any component. 54 00:02:19,560 --> 00:02:21,900 I'm just picking my route component here 55 00:02:21,900 --> 00:02:24,700 but that's not a must to do. 56 00:02:24,700 --> 00:02:29,030 So here we then define our Effect function 57 00:02:29,030 --> 00:02:30,973 and our array of dependencies. 58 00:02:32,410 --> 00:02:34,550 Now, inside of the Effect function, 59 00:02:34,550 --> 00:02:39,070 I wanna send a Http request with the Fetch API, let's say 60 00:02:39,070 --> 00:02:41,090 and I wanna send it to Firebase. 61 00:02:41,090 --> 00:02:44,283 So we grab that URL from Firebase, 62 00:02:45,200 --> 00:02:46,220 add that here 63 00:02:46,220 --> 00:02:50,090 and maybe target a cart.json node. 64 00:02:50,090 --> 00:02:52,710 The .json is Firebase specific. 65 00:02:52,710 --> 00:02:55,530 This will create a new cart Node in the database 66 00:02:55,530 --> 00:02:57,590 and then store the data there. 67 00:02:57,590 --> 00:02:59,760 And we wanna send a POST request 68 00:02:59,760 --> 00:03:03,690 because that will tell Firebase to store new data 69 00:03:04,570 --> 00:03:05,740 or to be precise, 70 00:03:05,740 --> 00:03:10,740 actually here, I wanna send a PUT request. 71 00:03:10,860 --> 00:03:13,200 That's also allowed by a Firebase. 72 00:03:13,200 --> 00:03:14,890 And if we send a PUT request 73 00:03:14,890 --> 00:03:18,070 we also do store data on Firebase. 74 00:03:18,070 --> 00:03:20,140 But the difference to POST is 75 00:03:20,140 --> 00:03:21,900 that the new data will not be added 76 00:03:21,900 --> 00:03:24,200 in a list of data so to say, 77 00:03:24,200 --> 00:03:26,930 but that it will override existing data. 78 00:03:26,930 --> 00:03:28,630 So when sending a PUT request, 79 00:03:28,630 --> 00:03:30,870 will override the existing cart 80 00:03:30,870 --> 00:03:32,600 with the incoming data 81 00:03:32,600 --> 00:03:35,000 and that's exactly what we want here. 82 00:03:35,000 --> 00:03:36,930 So we can send a PUT request 83 00:03:36,930 --> 00:03:41,490 and then set our request body to JSON.stringify 84 00:03:42,500 --> 00:03:45,150 And now here, I wanna send my cart. 85 00:03:45,150 --> 00:03:47,430 So this cart, which I get from Redux, 86 00:03:47,430 --> 00:03:49,820 I convert this to JSON data 87 00:03:49,820 --> 00:03:52,600 and send it as part of the request. 88 00:03:52,600 --> 00:03:54,430 Now, since we're using cart in here 89 00:03:54,430 --> 00:03:57,890 we should add it as a dependency to useEffect 90 00:03:57,890 --> 00:04:01,820 so that this Effect function re-executes 91 00:04:01,820 --> 00:04:03,910 whenever our cart changes, 92 00:04:03,910 --> 00:04:05,620 which is exactly what we want. 93 00:04:05,620 --> 00:04:06,940 Now, the great thing is 94 00:04:06,940 --> 00:04:10,570 that useSelector sets up a subscription to Redux. 95 00:04:10,570 --> 00:04:13,410 So whenever our Redux store does change 96 00:04:13,410 --> 00:04:16,399 this component function will be re-executed 97 00:04:16,399 --> 00:04:18,320 and we will get to the latest state. 98 00:04:18,320 --> 00:04:20,800 So in this case, the latest cart. 99 00:04:20,800 --> 00:04:24,120 So that means that effect will also be re-evaluated 100 00:04:24,120 --> 00:04:27,430 and it will re-execute if our carts did change 101 00:04:27,430 --> 00:04:29,710 and that is exactly what we need. 102 00:04:29,710 --> 00:04:32,310 So with this simple addition here 103 00:04:32,310 --> 00:04:37,310 we will send this Http request whenever our cart changes 104 00:04:37,550 --> 00:04:41,720 and we can keep our logic for updating the cart 105 00:04:41,720 --> 00:04:43,320 inside of the reducer, 106 00:04:43,320 --> 00:04:45,880 because we simply switched the order. 107 00:04:45,880 --> 00:04:49,570 We first update our Redux store 108 00:04:49,570 --> 00:04:51,110 and we're done with that. 109 00:04:51,110 --> 00:04:55,340 And then we select the updated store to send the request 110 00:04:55,340 --> 00:04:59,070 and that allows us to keep lean components 111 00:04:59,070 --> 00:05:02,260 create a fat Reducer with all the logic 112 00:05:02,260 --> 00:05:04,690 and then perform any side effects 113 00:05:04,690 --> 00:05:08,720 that might depend on our Redux state. 114 00:05:08,720 --> 00:05:10,500 So if you save this 115 00:05:10,500 --> 00:05:11,950 and we go back here, 116 00:05:11,950 --> 00:05:13,700 if I reload 117 00:05:13,700 --> 00:05:15,430 if I open up the network tab 118 00:05:15,430 --> 00:05:18,280 so that we can see Http requests, 119 00:05:18,280 --> 00:05:19,900 if I add something to my cart, 120 00:05:19,900 --> 00:05:22,670 you see a Http request is sent 121 00:05:22,670 --> 00:05:25,310 and that happens whenever I update my cart. 122 00:05:25,310 --> 00:05:26,750 And if we go to Firebase, 123 00:05:26,750 --> 00:05:28,597 we therefore see the cart's there. 124 00:05:28,597 --> 00:05:32,683 And we see that here we have the correct cart reflected. 125 00:05:33,750 --> 00:05:36,490 If I add my other item to the cart 126 00:05:36,490 --> 00:05:38,350 and never request the send 127 00:05:38,350 --> 00:05:40,780 and the cart was updated again. 128 00:05:40,780 --> 00:05:43,010 So that is a very, very nice 129 00:05:43,010 --> 00:05:44,920 and that's a very good way 130 00:05:44,920 --> 00:05:49,560 of having our side effect logic in a component 131 00:05:49,560 --> 00:05:53,050 and keeping all our data transformation logic 132 00:05:53,050 --> 00:05:55,120 inside of a Reducer, 133 00:05:55,120 --> 00:05:57,350 which is where we typically wanna have it 134 00:05:57,350 --> 00:05:58,813 when working with Redux. 9793

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