All language subtitles for 011 Using an Action Creator Thunk_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,459 --> 00:00:04,430 Now before we start fetching data, 2 00:00:04,430 --> 00:00:06,960 let's have a look at the alternative, 3 00:00:06,960 --> 00:00:08,940 of putting all that side effect logic 4 00:00:08,940 --> 00:00:10,690 into our component. 5 00:00:10,690 --> 00:00:12,710 That is perfectly fine, 6 00:00:12,710 --> 00:00:14,060 but we learned that it's only one 7 00:00:14,060 --> 00:00:15,410 of the two options. 8 00:00:15,410 --> 00:00:17,330 The other option would be the usage 9 00:00:17,330 --> 00:00:19,650 of an action creator. 10 00:00:19,650 --> 00:00:21,440 And that's something we haven't done up 11 00:00:21,440 --> 00:00:22,750 to this point. 12 00:00:22,750 --> 00:00:25,250 Or actually we kind of did. 13 00:00:25,250 --> 00:00:28,276 We are using action creators all the time. 14 00:00:28,276 --> 00:00:29,870 Something like this here, 15 00:00:29,870 --> 00:00:31,720 is an action creator. 16 00:00:31,720 --> 00:00:34,440 We get those action creators automatically 17 00:00:34,440 --> 00:00:35,690 by Redux toolkit. 18 00:00:35,690 --> 00:00:37,030 And we call them, 19 00:00:37,030 --> 00:00:40,830 to create the action objects which we dispatch. 20 00:00:40,830 --> 00:00:45,200 So these are these automatically created action creators. 21 00:00:45,200 --> 00:00:48,410 Now we can also write our own action creators 22 00:00:48,410 --> 00:00:50,070 and we can write them 23 00:00:50,070 --> 00:00:52,387 to create so-called thunks. 24 00:00:53,230 --> 00:00:54,380 Now what is a thunk? 25 00:00:54,380 --> 00:00:56,990 And why might we want to do that? 26 00:00:56,990 --> 00:00:59,420 A thunk is simply a function, 27 00:00:59,420 --> 00:01:02,160 that delays an action until later, 28 00:01:02,160 --> 00:01:04,129 until something else finished. 29 00:01:04,129 --> 00:01:07,200 And we could write an action creator as a thunk, 30 00:01:07,200 --> 00:01:08,920 to write an action creator, 31 00:01:08,920 --> 00:01:12,790 which does not immediately return the action object, 32 00:01:12,790 --> 00:01:14,760 but which instead, 33 00:01:14,760 --> 00:01:16,520 returns another function 34 00:01:16,520 --> 00:01:19,460 which eventually returns the action. 35 00:01:19,460 --> 00:01:21,390 So that we can run some other code 36 00:01:21,390 --> 00:01:24,880 before we then dispatch the actual action object 37 00:01:24,880 --> 00:01:27,390 that we did want to create. 38 00:01:27,390 --> 00:01:29,020 This might sound cryptic, 39 00:01:29,020 --> 00:01:30,820 but that's a very common pattern 40 00:01:30,820 --> 00:01:33,250 and it is easy to implement. 41 00:01:33,250 --> 00:01:35,870 So let me show you how it would work. 42 00:01:35,870 --> 00:01:39,170 And let's dive into these custom action creators, 43 00:01:39,170 --> 00:01:41,660 into these action creator thunks 44 00:01:41,660 --> 00:01:43,130 by moving that code 45 00:01:43,130 --> 00:01:45,140 for sending carts data, 46 00:01:45,140 --> 00:01:47,110 out of the component, 47 00:01:47,110 --> 00:01:48,940 though it would be fine there, 48 00:01:48,940 --> 00:01:50,250 but to show the alternative, 49 00:01:50,250 --> 00:01:51,750 let's move it out of there, 50 00:01:51,750 --> 00:01:54,393 and let's move it into an action creator. 51 00:01:55,520 --> 00:01:57,650 Now for this, I'll go to cart slice, 52 00:01:57,650 --> 00:02:00,500 because I wanna write this custom action creator 53 00:02:00,500 --> 00:02:01,960 in that same file, 54 00:02:01,960 --> 00:02:03,330 because that is my file 55 00:02:03,330 --> 00:02:05,450 for managing the cart. 56 00:02:05,450 --> 00:02:07,570 Now to create our own action creator, 57 00:02:07,570 --> 00:02:08,500 we can go to the end 58 00:02:08,500 --> 00:02:09,729 of that file, 59 00:02:09,729 --> 00:02:11,770 after this slice, that's important. 60 00:02:11,770 --> 00:02:14,680 So outside of this slice object, 61 00:02:14,680 --> 00:02:16,703 and there we can create a new function. 62 00:02:17,730 --> 00:02:20,890 Let's maybe name it, send carts data. 63 00:02:20,890 --> 00:02:22,930 Now that is a regular function, 64 00:02:22,930 --> 00:02:23,920 and therefore, of course, 65 00:02:23,920 --> 00:02:24,852 as always you could create it 66 00:02:24,852 --> 00:02:27,500 with the function keyword as well. 67 00:02:27,500 --> 00:02:31,260 And here let's say we expect the carts data. 68 00:02:31,260 --> 00:02:32,500 Now we could write this 69 00:02:32,500 --> 00:02:33,960 as a action creator, 70 00:02:33,960 --> 00:02:36,650 by now returning an action object in here. 71 00:02:36,650 --> 00:02:39,850 So object with a type of, whatever, 72 00:02:39,850 --> 00:02:42,590 and maybe some payload. 73 00:02:42,590 --> 00:02:44,660 We never did this ourselves, 74 00:02:44,660 --> 00:02:46,030 because Redux toolkit, 75 00:02:46,030 --> 00:02:49,400 creates these action creators automatically for us, 76 00:02:49,400 --> 00:02:51,760 for all those reducer methods. 77 00:02:51,760 --> 00:02:54,410 Every method in the reducers object, 78 00:02:54,410 --> 00:02:56,670 receives such an action creator, 79 00:02:56,670 --> 00:02:57,690 which is called, 80 00:02:57,690 --> 00:03:00,060 by using that reducer function name. 81 00:03:00,060 --> 00:03:01,010 That's what's inside 82 00:03:01,010 --> 00:03:03,010 of these actions here. 83 00:03:03,010 --> 00:03:04,350 So we don't need to write, 84 00:03:04,350 --> 00:03:07,063 such simple action creators on our own. 85 00:03:08,000 --> 00:03:09,380 But we could create, 86 00:03:09,380 --> 00:03:11,440 action creator which does not return, 87 00:03:11,440 --> 00:03:13,460 such a action object here, 88 00:03:13,460 --> 00:03:16,180 but which instead returns another function. 89 00:03:16,180 --> 00:03:17,890 And of course in JavaScript, 90 00:03:17,890 --> 00:03:19,540 you can write functions 91 00:03:19,540 --> 00:03:22,040 that return other functions. 92 00:03:22,040 --> 00:03:23,460 Now that would be a function 93 00:03:23,460 --> 00:03:26,080 that should receive the dispatch function 94 00:03:26,080 --> 00:03:27,500 as a argument. 95 00:03:27,500 --> 00:03:29,360 And we'll see where this is coming 96 00:03:29,360 --> 00:03:31,000 from in a second. 97 00:03:31,000 --> 00:03:32,760 And where inside of this function, 98 00:03:32,760 --> 00:03:35,630 we can then therefore, dispatch, 99 00:03:35,630 --> 00:03:38,343 the actual action we wanna perform. 100 00:03:39,400 --> 00:03:40,310 Like for example, 101 00:03:40,310 --> 00:03:41,900 showing a notification 102 00:03:41,900 --> 00:03:44,060 or adding a cart item, 103 00:03:44,060 --> 00:03:46,770 but before we call dispatch, 104 00:03:46,770 --> 00:03:48,630 we can of course do other things, 105 00:03:48,630 --> 00:03:50,800 before we call dispatch, 106 00:03:50,800 --> 00:03:53,660 we can perform any asynchronous code, 107 00:03:53,660 --> 00:03:55,150 any side effects, 108 00:03:55,150 --> 00:03:56,860 because we will not yet, 109 00:03:56,860 --> 00:03:58,760 have reached our reducer. 110 00:03:58,760 --> 00:04:00,930 We're not running this code in a reducer. 111 00:04:00,930 --> 00:04:04,590 It's a separate standalone JavaScript function instead. 112 00:04:04,590 --> 00:04:06,230 So in our case here, 113 00:04:06,230 --> 00:04:08,200 we could go to app JS, 114 00:04:08,200 --> 00:04:12,090 and actually grabbed this dispatch action, 115 00:04:12,090 --> 00:04:13,270 where we dispatch, 116 00:04:13,270 --> 00:04:15,670 show notification, copy it, 117 00:04:15,670 --> 00:04:17,790 go to the cart slice, 118 00:04:17,790 --> 00:04:20,350 and execute this in here. 119 00:04:20,350 --> 00:04:21,550 If were this need to get access 120 00:04:21,550 --> 00:04:22,620 to the UI actions, 121 00:04:22,620 --> 00:04:24,000 so I will import them, 122 00:04:24,000 --> 00:04:26,410 I'll import UI actions 123 00:04:28,020 --> 00:04:31,683 from UI slice, from that folder. 124 00:04:32,800 --> 00:04:33,633 If we do that, 125 00:04:33,633 --> 00:04:37,630 we can dispatch this action from in here. 126 00:04:37,630 --> 00:04:40,920 We can then also, copy all that, 127 00:04:40,920 --> 00:04:43,010 code here for sending the request 128 00:04:43,010 --> 00:04:44,700 and handling the response. 129 00:04:44,700 --> 00:04:45,890 And I'll not just copy it. 130 00:04:45,890 --> 00:04:47,010 I'll cut it. 131 00:04:47,010 --> 00:04:49,100 And I'll remove this, dispatch code 132 00:04:49,100 --> 00:04:50,160 because we all did copy 133 00:04:50,160 --> 00:04:51,283 to that already. 134 00:04:52,430 --> 00:04:57,390 So now here we can execute this down here, 135 00:04:57,390 --> 00:04:59,623 after dispatching that first notification. 136 00:05:00,600 --> 00:05:02,210 Now I'm using await here. 137 00:05:02,210 --> 00:05:03,240 And the great thing is, 138 00:05:03,240 --> 00:05:06,000 we can convert this into async function, 139 00:05:06,000 --> 00:05:07,790 this function which we returned here, 140 00:05:07,790 --> 00:05:08,733 that is fine. 141 00:05:09,610 --> 00:05:11,160 Now here we need the cart, 142 00:05:11,160 --> 00:05:12,750 here I called it carts data. 143 00:05:12,750 --> 00:05:13,583 Let me name, 144 00:05:13,583 --> 00:05:15,240 let me call it card here as well, 145 00:05:15,240 --> 00:05:16,180 as a parameter. 146 00:05:16,180 --> 00:05:17,930 So that we do get that card 147 00:05:17,930 --> 00:05:19,043 that should be sent. 148 00:05:20,330 --> 00:05:21,280 Now in app JS, 149 00:05:21,280 --> 00:05:23,690 we can now also grab the Abra code, 150 00:05:23,690 --> 00:05:26,600 like dispatching, this action here, 151 00:05:26,600 --> 00:05:29,480 go to cart slice and dispatch. 152 00:05:29,480 --> 00:05:32,150 That if we make it past this, if check, 153 00:05:32,150 --> 00:05:33,410 and now when it comes 154 00:05:33,410 --> 00:05:36,850 to handling the potential errors, 155 00:05:36,850 --> 00:05:40,090 I will actually create a new function, 156 00:05:40,090 --> 00:05:41,683 send request here, 157 00:05:42,580 --> 00:05:43,663 which is async, 158 00:05:46,080 --> 00:05:48,240 and put my code 159 00:05:48,240 --> 00:05:49,540 or sending the response 160 00:05:49,540 --> 00:05:51,130 and handling the response 161 00:05:51,130 --> 00:05:52,150 for sending the request 162 00:05:52,150 --> 00:05:53,300 and handling the response, 163 00:05:53,300 --> 00:05:54,680 inside of this function, 164 00:05:54,680 --> 00:05:56,310 so that I can, 165 00:05:56,310 --> 00:05:58,350 call, send request here, 166 00:05:58,350 --> 00:05:59,910 and await that as well, 167 00:05:59,910 --> 00:06:01,530 because it's a async function. 168 00:06:01,530 --> 00:06:03,180 It returns a promise. 169 00:06:03,180 --> 00:06:04,420 And since we're still inside 170 00:06:04,420 --> 00:06:05,910 of this outer function, 171 00:06:05,910 --> 00:06:07,720 where we also use async, 172 00:06:07,720 --> 00:06:09,940 we can use await here as well. 173 00:06:09,940 --> 00:06:12,600 And I know that it's quite some nesting of functions, 174 00:06:12,600 --> 00:06:14,820 but this extra nesting here is required 175 00:06:14,820 --> 00:06:17,230 because of how to fetch API works. 176 00:06:17,230 --> 00:06:18,810 Because now we can, 177 00:06:18,810 --> 00:06:21,970 wrap, try catch around this, 178 00:06:21,970 --> 00:06:23,563 await block here, 179 00:06:25,560 --> 00:06:26,760 and catch any errors 180 00:06:26,760 --> 00:06:28,530 that might be frozen from in, 181 00:06:28,530 --> 00:06:31,200 from anywhere inside of this function. 182 00:06:31,200 --> 00:06:34,050 And then dispatch our error 183 00:06:34,050 --> 00:06:35,513 and notification as well. 184 00:06:36,590 --> 00:06:38,490 So here all, first of all, 185 00:06:38,490 --> 00:06:40,190 dispatch, success ends out 186 00:06:40,190 --> 00:06:42,300 of the try block if this succeeds, 187 00:06:42,300 --> 00:06:43,133 but if we make it 188 00:06:43,133 --> 00:06:45,170 into this catch block instead, 189 00:06:45,170 --> 00:06:47,120 I wanna dispatch, 190 00:06:47,120 --> 00:06:49,563 this error case instead. 191 00:06:50,550 --> 00:06:51,640 So we'll copy that 192 00:06:51,640 --> 00:06:52,853 into this catch block. 193 00:06:54,120 --> 00:06:55,580 So with that what we're doing, 194 00:06:55,580 --> 00:06:57,040 is we're creating a function, 195 00:06:57,040 --> 00:06:58,370 send carts data, 196 00:06:58,370 --> 00:07:01,710 which immediately, without doing anything else, 197 00:07:01,710 --> 00:07:03,644 returns another function, 198 00:07:03,644 --> 00:07:05,050 a async function. 199 00:07:05,050 --> 00:07:08,110 We don't know yet who will execute that function, 200 00:07:08,110 --> 00:07:10,170 but we will soon to know it. 201 00:07:10,170 --> 00:07:11,370 But in that function, 202 00:07:11,370 --> 00:07:15,230 we then dispatch a notification, action. 203 00:07:15,230 --> 00:07:17,440 Then we create a new function, 204 00:07:17,440 --> 00:07:19,192 so yet a never nested function, 205 00:07:19,192 --> 00:07:20,920 which we create on the fly, 206 00:07:20,920 --> 00:07:22,720 which has also async, 207 00:07:22,720 --> 00:07:24,920 in there we send the request. 208 00:07:24,920 --> 00:07:27,250 This function is then called by us, 209 00:07:27,250 --> 00:07:28,890 instead of try-catch, 210 00:07:28,890 --> 00:07:31,440 simply to handle any errors. 211 00:07:31,440 --> 00:07:32,740 If we don't have an error, 212 00:07:32,740 --> 00:07:35,070 we dispatch the success notification. 213 00:07:35,070 --> 00:07:36,490 If we do have an error, 214 00:07:36,490 --> 00:07:39,053 we dispatch the error notification. 215 00:07:40,060 --> 00:07:43,283 So now we created this sent carts data function. 216 00:07:44,180 --> 00:07:45,250 How do we now call 217 00:07:45,250 --> 00:07:47,780 that function and where. 218 00:07:47,780 --> 00:07:48,850 We could of course call it 219 00:07:48,850 --> 00:07:50,020 from inside a component, 220 00:07:50,020 --> 00:07:51,670 but that's not the idea. 221 00:07:51,670 --> 00:07:53,910 Instead in app JS in there, 222 00:07:53,910 --> 00:07:55,410 I will clean up the content 223 00:07:55,410 --> 00:07:56,780 and use effect. 224 00:07:56,780 --> 00:07:58,470 I'll keep, you could use effect though 225 00:07:58,470 --> 00:08:00,470 but I'll clean up all the code in there, 226 00:08:01,360 --> 00:08:02,193 and I'll get rid 227 00:08:02,193 --> 00:08:04,580 of my UI actions import. 228 00:08:04,580 --> 00:08:06,970 I actually will keep that, 229 00:08:06,970 --> 00:08:08,913 is initial code though, 230 00:08:10,300 --> 00:08:11,810 where I return 231 00:08:11,810 --> 00:08:14,120 and set his initial to false. 232 00:08:14,120 --> 00:08:15,380 I'll keep that. 233 00:08:15,380 --> 00:08:16,710 But other than that, 234 00:08:16,710 --> 00:08:18,130 I cleaned this up, 235 00:08:18,130 --> 00:08:19,990 because now I wanna do something 236 00:08:19,990 --> 00:08:22,490 which might look weird at first. 237 00:08:22,490 --> 00:08:24,930 I wanna use sent carts data 238 00:08:24,930 --> 00:08:26,770 as a action creator. 239 00:08:26,770 --> 00:08:30,060 So in app JS, I still wanna dispatch, 240 00:08:30,060 --> 00:08:32,179 after this initial check, 241 00:08:32,179 --> 00:08:33,703 and I wanna dispatch, 242 00:08:35,549 --> 00:08:37,140 this send carts data, 243 00:08:37,140 --> 00:08:38,610 action so to say. 244 00:08:38,610 --> 00:08:42,030 Therefore I'll export it in cart slice, 245 00:08:42,030 --> 00:08:43,953 and then app JS hold an import. 246 00:08:44,800 --> 00:08:47,900 I'll import, send cart data 247 00:08:47,900 --> 00:08:50,320 so that function which we just exported, 248 00:08:50,320 --> 00:08:53,930 from store cart slice, 249 00:08:53,930 --> 00:08:55,780 from that file. 250 00:08:55,780 --> 00:08:57,290 And then here use effect, 251 00:08:57,290 --> 00:09:00,160 I dispatch sent cart data, 252 00:09:00,160 --> 00:09:01,130 and I'll execute it 253 00:09:01,130 --> 00:09:02,973 and pass my cart as an argument. 254 00:09:04,160 --> 00:09:05,800 Now this might look weird. 255 00:09:05,800 --> 00:09:07,590 What we dispatched before, 256 00:09:07,590 --> 00:09:10,100 always were action creators. 257 00:09:10,100 --> 00:09:13,260 So functions that return an action object 258 00:09:13,260 --> 00:09:15,400 with a type and so on. 259 00:09:15,400 --> 00:09:17,100 Now in cart slice, 260 00:09:17,100 --> 00:09:19,360 we are instead dispatching a function 261 00:09:19,360 --> 00:09:21,330 that returns another function. 262 00:09:21,330 --> 00:09:23,270 But the great thing about Redux, 263 00:09:23,270 --> 00:09:25,320 when using Redux toolkit, 264 00:09:25,320 --> 00:09:27,160 is that it's prepared for that. 265 00:09:27,160 --> 00:09:30,550 It does not just accept action objects 266 00:09:30,550 --> 00:09:32,060 with a type property. 267 00:09:32,060 --> 00:09:34,410 Instead it also does accept, 268 00:09:34,410 --> 00:09:37,160 action creators that return functions. 269 00:09:37,160 --> 00:09:38,550 And if it sees, 270 00:09:38,550 --> 00:09:40,050 that you're dispatching, 271 00:09:40,050 --> 00:09:42,640 a action which is actually a function, 272 00:09:42,640 --> 00:09:44,530 instead of action object, 273 00:09:44,530 --> 00:09:45,790 it will execute 274 00:09:45,790 --> 00:09:47,520 that function for you. 275 00:09:47,520 --> 00:09:49,100 So Redux will execute 276 00:09:49,100 --> 00:09:50,580 that function for you. 277 00:09:50,580 --> 00:09:51,890 And with that function, 278 00:09:51,890 --> 00:09:54,050 I mean this function here, 279 00:09:54,050 --> 00:09:55,120 it will give us 280 00:09:55,120 --> 00:09:58,070 that dispatch argument automatically. 281 00:09:58,070 --> 00:10:00,880 So that in that executed function, 282 00:10:00,880 --> 00:10:03,070 we can dispatch again, 283 00:10:03,070 --> 00:10:05,350 because there's a such a common pattern 284 00:10:05,350 --> 00:10:07,370 that we wanna have action creators 285 00:10:07,370 --> 00:10:09,490 that can perform side effects. 286 00:10:09,490 --> 00:10:12,540 And that can then dispatch Abra actions, 287 00:10:12,540 --> 00:10:14,940 which eventually reached the reducers, 288 00:10:14,940 --> 00:10:18,030 as part of a flow off side-effects, 289 00:10:18,030 --> 00:10:19,810 or as a flow of steps 290 00:10:19,810 --> 00:10:20,920 that should be taken. 291 00:10:20,920 --> 00:10:22,960 And that's what we have here. 292 00:10:22,960 --> 00:10:24,400 So we can use a function 293 00:10:24,400 --> 00:10:26,230 that returns another function, 294 00:10:26,230 --> 00:10:28,000 as a action as well. 295 00:10:28,000 --> 00:10:30,190 That is built into Redux 296 00:10:30,190 --> 00:10:32,810 when using Redux toolkit. 297 00:10:32,810 --> 00:10:35,150 So this patching this here will work. 298 00:10:35,150 --> 00:10:36,780 And when we dispatch, 299 00:10:36,780 --> 00:10:38,410 Redux will go ahead, 300 00:10:38,410 --> 00:10:40,750 and it will execute this function for us. 301 00:10:40,750 --> 00:10:43,930 And therefore all our other actions will be dispatched, 302 00:10:43,930 --> 00:10:47,120 and the HTTP request will be sent. 303 00:10:47,120 --> 00:10:49,210 So if you save all of that, 304 00:10:49,210 --> 00:10:50,570 if we reload here, 305 00:10:50,570 --> 00:10:52,510 if I add something to my cart, 306 00:10:52,510 --> 00:10:54,870 it works just as before, 307 00:10:54,870 --> 00:10:56,911 that still works and firebase, 308 00:10:56,911 --> 00:10:58,810 the staff were still being hit, 309 00:10:58,810 --> 00:10:59,970 and all the data, 310 00:10:59,970 --> 00:11:02,710 is still being stored in there. 311 00:11:02,710 --> 00:11:04,190 That still works, 312 00:11:04,190 --> 00:11:05,600 because that is a support 313 00:11:05,600 --> 00:11:08,140 that pattern by Redux. 314 00:11:08,140 --> 00:11:10,660 And why would we wanna use that pattern? 315 00:11:10,660 --> 00:11:12,720 Well, it's simply an alternative 316 00:11:12,720 --> 00:11:15,740 to having that logic in your component. 317 00:11:15,740 --> 00:11:18,670 You can add that logic in your components. 318 00:11:18,670 --> 00:11:21,720 You can stick to the approach we had before, 319 00:11:21,720 --> 00:11:23,860 but it's also not a bad idea 320 00:11:23,860 --> 00:11:25,990 to keep your components lean, 321 00:11:25,990 --> 00:11:28,820 to not have too much logic in them. 322 00:11:28,820 --> 00:11:30,180 And at the moment, 323 00:11:30,180 --> 00:11:31,430 by moving that logic 324 00:11:31,430 --> 00:11:34,010 to this action creator function, 325 00:11:34,010 --> 00:11:35,910 we did achieve this. 326 00:11:35,910 --> 00:11:38,040 This component is now leaner. 327 00:11:38,040 --> 00:11:40,160 It only dispatches one action, 328 00:11:40,160 --> 00:11:41,520 not multiple actions. 329 00:11:41,520 --> 00:11:42,510 It doesn't care 330 00:11:42,510 --> 00:11:44,900 about sending the HTTP request, 331 00:11:44,900 --> 00:11:46,600 and all the hard work, 332 00:11:46,600 --> 00:11:50,150 happens inside of our custom action creator function, 333 00:11:50,150 --> 00:11:52,560 in our Redux files. 334 00:11:52,560 --> 00:11:54,260 And splitting our recode like this, 335 00:11:54,260 --> 00:11:55,890 could be considered good, 336 00:11:55,890 --> 00:11:58,570 because it keeps our components lean. 337 00:11:58,570 --> 00:11:59,510 That does not mean 338 00:11:59,510 --> 00:12:01,210 that the other approach is bad. 339 00:12:01,210 --> 00:12:02,960 Both options are viable 340 00:12:02,960 --> 00:12:05,463 and that's why I am showing both here. 23092

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