Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,160 --> 00:00:02,993
So as I mentioned,
2
00:00:02,993 --> 00:00:06,020
we have this React Context Concept,
3
00:00:06,020 --> 00:00:08,260
which allows us to manage State
4
00:00:08,260 --> 00:00:10,920
kind of behind the scenes in React,
5
00:00:10,920 --> 00:00:14,100
such that we actually are able to directly change it
6
00:00:14,100 --> 00:00:15,810
from any component in our App
7
00:00:15,810 --> 00:00:18,850
and directly pass it to any component in our App
8
00:00:18,850 --> 00:00:20,853
without building such a prop chain.
9
00:00:22,010 --> 00:00:23,970
For that here in my source folder,
10
00:00:23,970 --> 00:00:26,170
next to my components folder,
11
00:00:26,170 --> 00:00:28,820
I will add a new folder, Context
12
00:00:28,820 --> 00:00:30,560
and you don't have to name it Context.
13
00:00:30,560 --> 00:00:33,260
You could also name it State or store
14
00:00:33,260 --> 00:00:36,400
often such App by it's State is also named store.
15
00:00:36,400 --> 00:00:38,010
So maybe let's go with that.
16
00:00:38,010 --> 00:00:40,730
And in there, I'll add a new file
17
00:00:40,730 --> 00:00:44,380
and since we're going to manage some authentication State
18
00:00:44,380 --> 00:00:48,770
I will add Auth-Context in there.
19
00:00:48,770 --> 00:00:51,800
So hence I will name the file Auth-Context.js,
20
00:00:51,800 --> 00:00:53,880
the exact naming is up to you though.
21
00:00:53,880 --> 00:00:56,710
You could also name it AuthContext like this,
22
00:00:56,710 --> 00:00:59,530
So in this Pascal case notation
23
00:00:59,530 --> 00:01:00,720
but this would imply that
24
00:01:00,720 --> 00:01:03,000
we're going to store a component in there
25
00:01:03,000 --> 00:01:05,430
and that's not directly what we were doing.
26
00:01:05,430 --> 00:01:08,463
Hence, I'll go for this naming using Kebab case.
27
00:01:09,400 --> 00:01:12,660
So Auth-Context.js and I can say right away,
28
00:01:12,660 --> 00:01:14,360
you can have multiple Contexts
29
00:01:14,360 --> 00:01:16,940
for multiple global States in your app
30
00:01:16,940 --> 00:01:19,450
and of course, you can also use just one Context
31
00:01:19,450 --> 00:01:22,000
for a bigger State, that's all up to you
32
00:01:22,000 --> 00:01:25,020
but let's maybe just learn how Context works.
33
00:01:25,020 --> 00:01:27,290
So we've get to Auth-Context.js file
34
00:01:27,290 --> 00:01:30,330
and in there we import React from React
35
00:01:30,330 --> 00:01:32,410
as if we would build a component
36
00:01:32,410 --> 00:01:34,170
but we're not going to do that.
37
00:01:34,170 --> 00:01:38,770
Instead on React, I'm going to call, create Context
38
00:01:38,770 --> 00:01:41,010
and does does what the name implies.
39
00:01:41,010 --> 00:01:44,953
It creates such a Context object in the end.
40
00:01:45,840 --> 00:01:49,570
Now create Context takes a default Context
41
00:01:49,570 --> 00:01:53,060
and Context here really is just your app
42
00:01:53,060 --> 00:01:55,220
or component white State.
43
00:01:55,220 --> 00:01:57,380
So it's up to you what that State should be.
44
00:01:57,380 --> 00:02:01,880
It could be something simple like just a string, my State.
45
00:02:01,880 --> 00:02:04,480
if your app white or a component white State
46
00:02:04,480 --> 00:02:05,710
is just some texts.
47
00:02:05,710 --> 00:02:09,416
You could have some well, text as your default State.
48
00:02:09,416 --> 00:02:12,310
Often though it will be an object,
49
00:02:12,310 --> 00:02:16,000
it will be an object where you, for example, in our case,
50
00:02:16,000 --> 00:02:18,320
managed IsLoggedIn State
51
00:02:18,320 --> 00:02:20,703
and set this to false initially, let's say.
52
00:02:21,970 --> 00:02:24,330
Now what we get back from create Context
53
00:02:24,330 --> 00:02:26,500
is something interesting though.
54
00:02:26,500 --> 00:02:30,650
It will be a component or it will be an object
55
00:02:30,650 --> 00:02:33,743
that also contains components, let's put it like this.
56
00:02:34,630 --> 00:02:36,650
So we get back AuthContext
57
00:02:36,650 --> 00:02:38,360
and I'll name it like this because
58
00:02:38,360 --> 00:02:41,730
whilst Auth-Context itself is not a component,
59
00:02:41,730 --> 00:02:44,883
it is an object that will contain a component.
60
00:02:46,270 --> 00:02:49,090
Now we don't need this object here though,
61
00:02:49,090 --> 00:02:51,740
instead we'll need it in the other components.
62
00:02:51,740 --> 00:02:53,910
So I'll export it as a default
63
00:02:53,910 --> 00:02:55,863
to make it available in our files.
64
00:02:57,020 --> 00:03:00,350
So now we can import this specific Context object
65
00:03:00,350 --> 00:03:03,030
and to then use Context in your app
66
00:03:03,030 --> 00:03:04,680
you need to do two things.
67
00:03:04,680 --> 00:03:07,987
You need to provide it, which basically tells React,
68
00:03:07,987 --> 00:03:09,700
"Hey, here's my Context."
69
00:03:09,700 --> 00:03:12,030
All components that are wrapped by it
70
00:03:12,030 --> 00:03:13,780
should have access to it
71
00:03:13,780 --> 00:03:16,720
and I'll come back to that wrapping part in a second.
72
00:03:16,720 --> 00:03:19,780
And besides providing, you then need to consume it.
73
00:03:19,780 --> 00:03:22,760
You need to hook into it, you need to listen to it,
74
00:03:22,760 --> 00:03:24,310
if you wanna call it like this.
75
00:03:25,350 --> 00:03:28,040
Now, providing is always the first step.
76
00:03:28,040 --> 00:03:31,640
Providing means that you wrap in JSX code
77
00:03:31,640 --> 00:03:33,960
all the components that should be able to tap
78
00:03:33,960 --> 00:03:35,060
into that Context.
79
00:03:35,060 --> 00:03:38,070
So that should be able to listen to that Context.
80
00:03:38,070 --> 00:03:41,560
Any component that's not wrapped will not be able to listen.
81
00:03:41,560 --> 00:03:44,310
So here, if we know that we need this Context
82
00:03:44,310 --> 00:03:46,910
everywhere in the entire application.
83
00:03:46,910 --> 00:03:49,760
So in all components possibly,
84
00:03:49,760 --> 00:03:53,860
we wanna wrap everything in app component with it.
85
00:03:53,860 --> 00:03:55,610
If we knew that we only needed,
86
00:03:55,610 --> 00:03:59,340
let's say, in the log in component and its child components,
87
00:03:59,340 --> 00:04:01,780
we could only wrap to login component.
88
00:04:01,780 --> 00:04:03,760
But here we'll need it in the main header,
89
00:04:03,760 --> 00:04:06,260
we'll need a down there, we'll need it everywhere.
90
00:04:07,220 --> 00:04:08,253
So therefore,
91
00:04:09,210 --> 00:04:11,760
we can wrap
92
00:04:11,760 --> 00:04:13,260
our main header
93
00:04:13,260 --> 00:04:16,950
and this main section with Auth-Context
94
00:04:16,950 --> 00:04:19,170
and for that we will need to import it.
95
00:04:19,170 --> 00:04:24,170
So make sure you add this import from store Auth-Context
96
00:04:24,400 --> 00:04:26,463
to import this Auth-Context object.
97
00:04:27,320 --> 00:04:30,460
And I said that this itself wouldn't be a component
98
00:04:30,460 --> 00:04:32,870
in JSX we need a component though,
99
00:04:32,870 --> 00:04:35,000
but with a dot we can access a property
100
00:04:35,000 --> 00:04:38,080
on this Auth-Context object that contains a component
101
00:04:38,080 --> 00:04:40,163
and that's the provider.
102
00:04:41,996 --> 00:04:45,070
Auth-Context provider is a component
103
00:04:45,070 --> 00:04:46,840
we can use in our JSX code,
104
00:04:46,840 --> 00:04:49,670
and we can wrap it around other components
105
00:04:49,670 --> 00:04:51,580
and those other components
106
00:04:51,580 --> 00:04:54,690
and all their descendant components.
107
00:04:54,690 --> 00:04:56,420
So all their children
108
00:04:56,420 --> 00:04:58,770
and their children's children and so on,
109
00:04:58,770 --> 00:05:01,773
all those components will now have access to that Context.
110
00:05:02,742 --> 00:05:05,900
Now, since we use Auth-Context as a wrapper here,
111
00:05:05,900 --> 00:05:08,730
we can now technically also use React Fragment
112
00:05:08,730 --> 00:05:10,880
because we got this wrapping component
113
00:05:10,880 --> 00:05:13,830
as a root level component I returned here
114
00:05:13,830 --> 00:05:15,770
and with that, the main header,
115
00:05:15,770 --> 00:05:17,800
but also to log in and the home component
116
00:05:17,800 --> 00:05:21,513
and all their children will have access to Auth-Context.
117
00:05:22,690 --> 00:05:24,420
So now we provide this.
118
00:05:24,420 --> 00:05:27,000
Now, we have the second part
119
00:05:27,000 --> 00:05:28,740
and that is the listening part
120
00:05:29,820 --> 00:05:31,950
to get access to our value,
121
00:05:31,950 --> 00:05:34,830
which at the moment is always just this default value,
122
00:05:34,830 --> 00:05:36,130
which never changes.
123
00:05:36,130 --> 00:05:38,390
We're going to work on that too, of course,
124
00:05:38,390 --> 00:05:41,360
but to have access to that value, we need to listen
125
00:05:41,360 --> 00:05:44,250
and we can listen in two ways.
126
00:05:44,250 --> 00:05:48,210
We can listen by using Auth-Context consumer
127
00:05:48,210 --> 00:05:50,720
or by using a React Hook.
128
00:05:50,720 --> 00:05:52,630
Now we'll typically use the React Hook,
129
00:05:52,630 --> 00:05:54,440
but let me show you the other alternative
130
00:05:54,440 --> 00:05:56,510
to consumer as well.
131
00:05:56,510 --> 00:06:00,860
Let's say here in navigation, we're interested in knowing
132
00:06:00,860 --> 00:06:03,223
whether the user isAuthenticated or not.
133
00:06:04,420 --> 00:06:08,070
For that we can also use Auth-Context steer
134
00:06:08,070 --> 00:06:10,980
and wrap everything where we need data from it
135
00:06:10,980 --> 00:06:12,820
with that consumer.
136
00:06:12,820 --> 00:06:17,060
So here, for example, we could wrap the entire component
137
00:06:17,060 --> 00:06:19,020
with Auth-Context.
138
00:06:19,020 --> 00:06:21,050
Again, make sure to import it, of course
139
00:06:22,060 --> 00:06:23,903
dot consumer now.
140
00:06:24,840 --> 00:06:28,913
Previously we had the provider, now we have the consumer.
141
00:06:30,230 --> 00:06:32,410
The consumer works a bit differently though.
142
00:06:32,410 --> 00:06:35,540
The consumer takes a child
143
00:06:35,540 --> 00:06:37,570
which actually should be a function
144
00:06:37,570 --> 00:06:40,070
between curly braces like this
145
00:06:40,070 --> 00:06:43,460
and as argument, you'll get your Context data.
146
00:06:43,460 --> 00:06:46,113
So in our case, you'll get this object here.
147
00:06:49,480 --> 00:06:50,803
And then,
148
00:06:53,210 --> 00:06:57,200
in this function you should return your JSX code,
149
00:06:57,200 --> 00:06:59,040
which should have access to that data.
150
00:06:59,040 --> 00:07:03,050
So here I'm grabbing all that NAV code, I'm returning that
151
00:07:03,050 --> 00:07:04,960
and in there, I now for example,
152
00:07:04,960 --> 00:07:08,700
have access to ctx.isLockedIn
153
00:07:08,700 --> 00:07:11,850
because my Context object is this object here
154
00:07:11,850 --> 00:07:14,183
and there I have it IsLockedIn prop.
155
00:07:15,450 --> 00:07:18,750
Now, if you save that, you will however see
156
00:07:18,750 --> 00:07:21,223
if you reload that this crashes.
157
00:07:22,070 --> 00:07:22,903
And the reason for that,
158
00:07:22,903 --> 00:07:25,290
is that we do have a default value here,
159
00:07:25,290 --> 00:07:28,340
but this default value will actually only be used
160
00:07:28,340 --> 00:07:32,600
if we would consume here without having a provider.
161
00:07:32,600 --> 00:07:34,790
So technically the provider is not even needed,
162
00:07:34,790 --> 00:07:36,970
however, you should memorize this pattern
163
00:07:36,970 --> 00:07:39,680
I explained earlier, where you need the provider.
164
00:07:39,680 --> 00:07:43,090
Technically, you don't need it if you have a default value,
165
00:07:43,090 --> 00:07:45,620
but in reality, you will use Context
166
00:07:45,620 --> 00:07:47,670
to have a value which can change
167
00:07:47,670 --> 00:07:50,740
and that will only be possible with a provider.
168
00:07:50,740 --> 00:07:53,990
So therefore, to make sure that this does not crash,
169
00:07:53,990 --> 00:07:55,820
let's go back to the provider
170
00:07:55,820 --> 00:07:58,130
and they're on the provider component
171
00:07:58,130 --> 00:08:00,270
you can add a value prop,
172
00:08:00,270 --> 00:08:02,020
and this has to be named value
173
00:08:02,020 --> 00:08:04,050
because this is not a component by you
174
00:08:04,050 --> 00:08:07,620
and to do that you can also pass your object.
175
00:08:07,620 --> 00:08:10,610
So in this case, I will just repeat my default object here,
176
00:08:10,610 --> 00:08:13,570
but now you would be able to change that object,
177
00:08:13,570 --> 00:08:16,380
for example, through State and the app component.
178
00:08:16,380 --> 00:08:19,820
And whenever it changes, the new value will be passed down
179
00:08:19,820 --> 00:08:21,930
to all consuming components,
180
00:08:21,930 --> 00:08:24,880
but that is something I will also come back to in a second.
181
00:08:25,930 --> 00:08:27,220
So here, let me save this
182
00:08:27,220 --> 00:08:29,313
and now it reloads without problems.
183
00:08:30,550 --> 00:08:32,773
Now of course, if I now log in,
184
00:08:34,330 --> 00:08:37,309
you will see that we only have one link here
185
00:08:37,309 --> 00:08:40,419
so that first link, the user's link is missing
186
00:08:40,419 --> 00:08:43,289
because there I'm pulling data from my Context
187
00:08:43,289 --> 00:08:45,853
and that at the moment is never changing.
188
00:08:46,780 --> 00:08:48,540
So let's make sure we can change it
189
00:08:48,540 --> 00:08:51,313
before I then show you the alternative consumer.
190
00:08:52,240 --> 00:08:54,000
Here changing it is easy,
191
00:08:54,000 --> 00:08:56,810
we have set up the here in the app component
192
00:08:56,810 --> 00:09:00,823
and there we manage to IsLockedIn State anyways.
193
00:09:01,720 --> 00:09:05,550
So instead of hard coding our IsLockedIn value,
194
00:09:05,550 --> 00:09:07,970
in the value we pass to the provider,
195
00:09:07,970 --> 00:09:10,543
we can set it to IsLockedIn, of course.
196
00:09:11,440 --> 00:09:14,070
And if we do that, this value object
197
00:09:14,070 --> 00:09:18,430
will be updated by React whenever IsLoggedIn changed.
198
00:09:18,430 --> 00:09:21,940
And that new object, that new Context object
199
00:09:21,940 --> 00:09:26,030
will be passed down to all listening components.
200
00:09:26,030 --> 00:09:29,343
So to all components where we consume this Context.
201
00:09:30,270 --> 00:09:32,000
The difference to before is
202
00:09:32,000 --> 00:09:35,980
that we don't need to use a prop to forward that therefore.
203
00:09:35,980 --> 00:09:37,970
Instead we just set it on the provider
204
00:09:37,970 --> 00:09:41,030
and then everywhere in all child components,
205
00:09:41,030 --> 00:09:42,343
we can listen to that.
206
00:09:43,190 --> 00:09:46,450
So here we could remove the isAuthenticated prop
207
00:09:46,450 --> 00:09:47,620
on the main header
208
00:09:48,610 --> 00:09:51,800
and in the main header therefore, stop forwarding it
209
00:09:51,800 --> 00:09:53,820
to the navigation.
210
00:09:53,820 --> 00:09:58,570
and in navigation, we could now use Context.IsLoggedIn
211
00:09:58,570 --> 00:10:02,970
everywhere where we need the IsLockedIn information.
212
00:10:02,970 --> 00:10:06,760
And that's now where we break up that props chain,
213
00:10:06,760 --> 00:10:09,150
where we stop forwarding that data,
214
00:10:09,150 --> 00:10:11,160
which we don't need in the main header
215
00:10:11,160 --> 00:10:13,553
and where we instead leverage Context.
216
00:10:14,410 --> 00:10:16,760
So now if I reload you see that still works
217
00:10:16,760 --> 00:10:18,330
and now the link is back,
218
00:10:18,330 --> 00:10:21,323
but now we're using Context here.
219
00:10:22,350 --> 00:10:25,070
Now I did mention, however that
220
00:10:25,070 --> 00:10:28,480
the consumer is just one way of consuming our Context.
221
00:10:28,480 --> 00:10:32,550
It's an okay-ish way, but I don't like that syntax too much
222
00:10:32,550 --> 00:10:35,678
having that function that then returns our code.
223
00:10:35,678 --> 00:10:39,530
You can do that, but there actually is a more elegant way
224
00:10:39,530 --> 00:10:42,883
and more elegant solution using the use Context Hook.
17677
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.