All language subtitles for 013 Introducing React Context (Context API)_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,400 --> 00:00:04,090 [Maximilian Schwarzm�ller] So with useReducer 2 00:00:04,090 --> 00:00:05,200 out of the way, 3 00:00:05,200 --> 00:00:08,170 let's dive into a different problem you might face 4 00:00:08,170 --> 00:00:10,870 in bigger React applications specifically, 5 00:00:10,870 --> 00:00:14,270 but even in this tiny demo here, we already have it, 6 00:00:14,270 --> 00:00:16,683 that can be solved in a more elegant way. 7 00:00:17,630 --> 00:00:20,670 I'm talking about a problem, in quotes, 8 00:00:20,670 --> 00:00:23,730 where you are passing a lot of data 9 00:00:23,730 --> 00:00:26,447 through a lot of components via props. 10 00:00:26,447 --> 00:00:30,300 And a good example here is our isLoggedIn state 11 00:00:30,300 --> 00:00:33,020 and the login functions. 12 00:00:33,020 --> 00:00:35,940 We're managing this state in the app component 13 00:00:35,940 --> 00:00:39,020 because I lifted this state up to that component 14 00:00:39,020 --> 00:00:41,130 because we basically need this state, 15 00:00:41,130 --> 00:00:44,018 this isLoggedIn state and the functions to change it 16 00:00:44,018 --> 00:00:46,283 everywhere in this application. 17 00:00:47,280 --> 00:00:51,120 We use the logged in state in the header 18 00:00:51,120 --> 00:00:54,240 to which I'm passing it through the isAuthenticated prop. 19 00:00:54,240 --> 00:00:56,460 From there, we are able to logout. 20 00:00:56,460 --> 00:00:59,200 So I passed the pointer at the logoutHandler 21 00:00:59,200 --> 00:01:02,770 to my header on the onLogout prop. 22 00:01:02,770 --> 00:01:04,860 And we also need the login state 23 00:01:04,860 --> 00:01:07,100 to render different content here, 24 00:01:07,100 --> 00:01:09,350 either the login or the home component, 25 00:01:09,350 --> 00:01:11,040 and to those components, 26 00:01:11,040 --> 00:01:15,690 I also passed the logoutHandler or the loginHandler. 27 00:01:15,690 --> 00:01:19,110 Now the logoutHandler being passed to the home component, 28 00:01:19,110 --> 00:01:20,940 that's something I'm doing here 29 00:01:20,940 --> 00:01:24,310 so that we theoretically could add a button there 30 00:01:25,150 --> 00:01:27,350 with our button component, for example, 31 00:01:27,350 --> 00:01:29,900 to allow the user to logout there as well. 32 00:01:29,900 --> 00:01:31,240 Not something we need in this app, 33 00:01:31,240 --> 00:01:35,010 but something you could add here with props on logout 34 00:01:35,010 --> 00:01:38,240 since we're getting a pointer to this handler anyways. 35 00:01:38,240 --> 00:01:40,773 And if you do that, once you login here, 36 00:01:42,258 --> 00:01:44,690 you all have the logout button here. 37 00:01:44,690 --> 00:01:48,580 So long story short, we need, or we use this login state 38 00:01:48,580 --> 00:01:51,260 in a lot of different places in the app. 39 00:01:51,260 --> 00:01:54,910 Now, obviously this is a very, very, very simple app, 40 00:01:54,910 --> 00:01:56,320 a very simple demo, 41 00:01:56,320 --> 00:01:58,880 and here passing our state around like this 42 00:01:58,880 --> 00:02:00,553 is not a huge problem. 43 00:02:01,390 --> 00:02:03,740 Now it is quite common that you pass data 44 00:02:03,740 --> 00:02:05,600 to components through props, 45 00:02:05,600 --> 00:02:08,800 but it's always a problem if you forward state 46 00:02:08,800 --> 00:02:10,900 through multiple components. 47 00:02:10,900 --> 00:02:14,160 So if you basically just leverage props to forward data 48 00:02:14,160 --> 00:02:15,860 to another component. 49 00:02:15,860 --> 00:02:17,500 And that's actually something we're doing 50 00:02:17,500 --> 00:02:19,110 in the main header. 51 00:02:19,110 --> 00:02:20,890 Here in the app component, 52 00:02:20,890 --> 00:02:24,840 I'm passing isLoggedIn through the isAuthenticated prop 53 00:02:24,840 --> 00:02:26,010 to main header. 54 00:02:26,010 --> 00:02:27,800 And I'm passing the logoutHandler 55 00:02:27,800 --> 00:02:31,930 through the onLogout prop to the main header as well. 56 00:02:31,930 --> 00:02:33,770 Now in the main header though, 57 00:02:33,770 --> 00:02:37,220 I'm using neither of those two props. 58 00:02:37,220 --> 00:02:40,170 I'm using them only to forward them again 59 00:02:40,170 --> 00:02:42,480 to the navigation component 60 00:02:42,480 --> 00:02:45,140 because it's the navigation component in the end, 61 00:02:45,140 --> 00:02:47,720 which needs the isLoggedIn data 62 00:02:47,720 --> 00:02:50,928 to render different links and which needs the 63 00:02:50,928 --> 00:02:53,610 onLogout prop and the logoutHandler therefore, 64 00:02:53,610 --> 00:02:56,163 to do something when the logout button is clicked. 65 00:02:57,110 --> 00:03:00,930 So essentially what I'm doing here, is in the main header 66 00:03:00,930 --> 00:03:02,640 I'm receiving props, 67 00:03:02,640 --> 00:03:05,070 I'm receiving data through props 68 00:03:05,070 --> 00:03:08,480 which I'm not actually using primarily in the main header. 69 00:03:08,480 --> 00:03:11,430 Instead, I just forward that data. 70 00:03:11,430 --> 00:03:13,790 That's not necessarily a problem, 71 00:03:13,790 --> 00:03:17,100 but in big apps, that chain of forwarding 72 00:03:17,100 --> 00:03:19,033 might become longer and longer. 73 00:03:20,200 --> 00:03:23,350 Consider this example of a fictional app. 74 00:03:23,350 --> 00:03:25,620 We could have an app with an app component, 75 00:03:25,620 --> 00:03:27,490 with, let's say, an auth section, 76 00:03:27,490 --> 00:03:28,890 with a login form, 77 00:03:28,890 --> 00:03:32,020 with a shop section where we have a list of products 78 00:03:32,020 --> 00:03:34,890 in which we render individual product items, 79 00:03:34,890 --> 00:03:36,113 and with a cart. 80 00:03:37,140 --> 00:03:38,560 Now in the login form, 81 00:03:38,560 --> 00:03:41,630 we might have the login event when the user fills 82 00:03:41,630 --> 00:03:43,270 out the form and submits the form, 83 00:03:43,270 --> 00:03:45,780 and we validate the data and find out 84 00:03:45,780 --> 00:03:47,720 that the user is logged in. 85 00:03:47,720 --> 00:03:49,790 And we might need that login data, 86 00:03:49,790 --> 00:03:51,320 So the login status, 87 00:03:51,320 --> 00:03:53,690 the information that the user is logged in 88 00:03:53,690 --> 00:03:55,670 in different parts of the app. 89 00:03:55,670 --> 00:03:57,100 We might need it in the shop. 90 00:03:57,100 --> 00:03:58,990 We might need it in the cart, 91 00:03:58,990 --> 00:04:02,740 to show the cart to avoid that the user can go there 92 00:04:02,740 --> 00:04:04,690 if he or she is not logged in. 93 00:04:04,690 --> 00:04:07,150 It's easy to imagine that we need information 94 00:04:07,150 --> 00:04:10,083 about the login status in different parts of the app. 95 00:04:11,110 --> 00:04:12,670 And we have something similar. 96 00:04:12,670 --> 00:04:15,060 Let's say the user adds a product to the cart. 97 00:04:15,060 --> 00:04:18,360 We need that information in the cart component 98 00:04:18,360 --> 00:04:21,220 to show the product there, for example. 99 00:04:21,220 --> 00:04:24,800 But since the product is a direct child of products, 100 00:04:24,800 --> 00:04:27,850 which is a child of shop, which is a child of app. 101 00:04:27,850 --> 00:04:31,290 And since the login event happens in the login form, 102 00:04:31,290 --> 00:04:34,460 which is a child of auth, which is a child of app, 103 00:04:34,460 --> 00:04:36,014 we don't have those direct connections 104 00:04:36,014 --> 00:04:39,423 to the other components where the data might be needed. 105 00:04:40,610 --> 00:04:43,250 If a user clicks a button in the product component 106 00:04:43,250 --> 00:04:45,460 to add a product to the cart, 107 00:04:45,460 --> 00:04:48,980 the information needs to reach the cart component 108 00:04:48,980 --> 00:04:50,740 but there is no direct connection 109 00:04:50,740 --> 00:04:54,190 between the product component and the cart component. 110 00:04:54,190 --> 00:04:56,100 And the same for the login. 111 00:04:56,100 --> 00:04:58,960 And therefore, what we typically would do is 112 00:04:58,960 --> 00:05:03,270 we would use props to pass a function handler 113 00:05:03,270 --> 00:05:05,400 to the login form, for example, 114 00:05:05,400 --> 00:05:07,500 so that in there we can call it. 115 00:05:07,500 --> 00:05:08,760 But that essentially means 116 00:05:08,760 --> 00:05:12,570 that we're forwarding data through our different components. 117 00:05:12,570 --> 00:05:15,580 And the same, of course, if we add something to the cart. 118 00:05:15,580 --> 00:05:19,180 Now, that means that this function that should be triggered 119 00:05:19,180 --> 00:05:21,610 when the user adds something to the cart 120 00:05:21,610 --> 00:05:24,460 would probably be managed in the app component, 121 00:05:24,460 --> 00:05:27,280 because we lifted the state up to the level 122 00:05:27,280 --> 00:05:30,170 where we have access to both the product component, 123 00:05:30,170 --> 00:05:31,003 indirectly, 124 00:05:31,003 --> 00:05:32,431 and the card component. 125 00:05:32,431 --> 00:05:34,900 And that's only the case in the app component. 126 00:05:34,900 --> 00:05:37,980 Only this component has at least indirect connections 127 00:05:37,980 --> 00:05:40,590 to both components, product, and cart. 128 00:05:40,590 --> 00:05:43,390 And therefore we would forward a pointer 129 00:05:43,390 --> 00:05:45,610 to the add to cart function 130 00:05:45,610 --> 00:05:47,700 through all those other components, 131 00:05:47,700 --> 00:05:50,080 the shop component and the products component, 132 00:05:50,080 --> 00:05:53,050 even though those components probably don't need 133 00:05:53,050 --> 00:05:54,260 that function. 134 00:05:54,260 --> 00:05:56,920 They only need it so that they can forward it 135 00:05:56,920 --> 00:05:58,733 to the product component in the end. 136 00:05:59,700 --> 00:06:00,600 So in the end here, 137 00:06:00,600 --> 00:06:02,800 we have the same problem I described here 138 00:06:02,800 --> 00:06:04,660 just on a larger scale. 139 00:06:04,660 --> 00:06:06,250 Here, it's a very simple app 140 00:06:06,250 --> 00:06:08,950 but even here we are forwarding data 141 00:06:08,950 --> 00:06:12,070 through the main header to the navigation component 142 00:06:12,070 --> 00:06:14,520 on props we receive in the main header 143 00:06:14,520 --> 00:06:17,500 even though we don't really need the data there. 144 00:06:17,500 --> 00:06:19,020 So therefore, in bigger apps, 145 00:06:19,020 --> 00:06:21,890 you could easily build such prop chains, 146 00:06:21,890 --> 00:06:24,930 which are used to forward data through components 147 00:06:24,930 --> 00:06:26,650 to other components. 148 00:06:26,650 --> 00:06:29,830 That's something you can do, and that's not necessarily bad 149 00:06:29,830 --> 00:06:31,540 but the bigger your app gets, 150 00:06:31,540 --> 00:06:34,140 the more inconvenient it is to do that. 151 00:06:34,140 --> 00:06:37,450 Instead, it would be nicer if we could use props 152 00:06:37,450 --> 00:06:40,050 in a component to only receive the data 153 00:06:40,050 --> 00:06:43,021 this component really needs from its parent, 154 00:06:43,021 --> 00:06:47,010 and not to forward our data through the parent 155 00:06:47,010 --> 00:06:50,250 which the parent doesn't ever manage nor need itself. 156 00:06:50,250 --> 00:06:52,517 And for that we got a component-wide, 157 00:06:52,517 --> 00:06:57,000 "behind the scenes", State Storage, built into React. 158 00:06:57,000 --> 00:06:59,850 We have a concept called React Context, 159 00:06:59,850 --> 00:07:04,180 and this then allows us to, for example, trigger a action 160 00:07:04,180 --> 00:07:06,580 in that component-wide State Storage, 161 00:07:06,580 --> 00:07:09,220 and then directly pass that to the component 162 00:07:09,220 --> 00:07:13,670 that is interested without building such a long prop chain. 163 00:07:13,670 --> 00:07:15,930 And that allows us to solve our problem 164 00:07:15,930 --> 00:07:17,910 in a more elegant way here. 165 00:07:17,910 --> 00:07:19,910 So let's take a closer look at this now. 13003

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