Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,220 --> 00:00:04,160
Now what about context
2
00:00:04,160 --> 00:00:06,510
and class space components?
3
00:00:06,510 --> 00:00:09,240
Well the first important takeaway is that you can still
4
00:00:09,240 --> 00:00:14,240
define it and provide it as you learned it.
5
00:00:14,250 --> 00:00:18,120
You define it by calling create context, maybe with some
6
00:00:18,120 --> 00:00:22,713
initial value and you then provide it by using the provider
7
00:00:22,713 --> 00:00:27,010
component which is part of that creative context object.
8
00:00:27,010 --> 00:00:30,380
And then you can pass a value to that and here it's actually
9
00:00:30,380 --> 00:00:34,890
a static value which never changes, but it also may change
10
00:00:34,890 --> 00:00:39,070
with state updates and that all doesn't change only the way
11
00:00:39,070 --> 00:00:42,390
you update state of course changes if you use a class
12
00:00:42,390 --> 00:00:45,820
based component, as explained over the last lectures.
13
00:00:45,820 --> 00:00:48,840
As a side note, if you want to follow along, attached you'll
14
00:00:48,840 --> 00:00:53,010
find the updated app component and this users context JS
15
00:00:53,010 --> 00:00:57,920
file which you can add into a newly created store folder.
16
00:00:57,920 --> 00:01:01,680
But back to that, here we provide as we learned it.
17
00:01:01,680 --> 00:01:05,150
The only part which changes when you're working with context
18
00:01:05,150 --> 00:01:10,150
is related to how you use it in your class based components.
19
00:01:10,770 --> 00:01:13,421
Now I haven't edit any code for using context up until
20
00:01:13,421 --> 00:01:18,350
now so therefore let's go to the user finder and think
21
00:01:18,350 --> 00:01:20,445
about how we could add it there.
22
00:01:20,445 --> 00:01:23,966
There let's say I no longer wanna work with the dummy
23
00:01:23,966 --> 00:01:28,966
users defined in this file, I no longer wanna get my data
24
00:01:29,600 --> 00:01:33,880
from there, but instead I want to get my data
25
00:01:33,880 --> 00:01:38,880
from context, because it's managed application wide.
26
00:01:39,790 --> 00:01:43,640
If this would be a functional component, we would of course
27
00:01:43,640 --> 00:01:48,640
used the use context hook for that.
28
00:01:48,680 --> 00:01:51,650
That's what we would do, but this is not an option here
29
00:01:51,650 --> 00:01:53,997
in the class based component because we can't
30
00:01:53,997 --> 00:01:55,920
use hooks there.
31
00:01:55,920 --> 00:01:59,511
Instead here we got two ways of using context.
32
00:01:59,511 --> 00:02:04,511
One approach was shown to you early in the course already.
33
00:02:05,091 --> 00:02:10,091
The context consumer component can be used in both
34
00:02:10,720 --> 00:02:13,405
functional and class based components.
35
00:02:13,405 --> 00:02:18,108
So in your render method you could import the users
36
00:02:18,108 --> 00:02:22,100
context, so add it here and of course import it
37
00:02:22,100 --> 00:02:24,759
from the users context file.
38
00:02:24,759 --> 00:02:29,759
And then simply add that consumer or access this consumer
39
00:02:30,160 --> 00:02:33,590
component and then use it as showed to you early
40
00:02:33,590 --> 00:02:35,090
in the course.
41
00:02:35,090 --> 00:02:39,410
Since this is used in JSX only it works in both functional
42
00:02:39,410 --> 00:02:41,053
and class based components.
43
00:02:41,980 --> 00:02:45,890
But the user hook is used so that we don't have to use
44
00:02:45,890 --> 00:02:47,430
this consumer component.
45
00:02:47,430 --> 00:02:49,840
So what's the equivalent to this?
46
00:02:49,840 --> 00:02:54,354
There is no perfect equivalent, I will say that right away.
47
00:02:54,354 --> 00:02:59,354
With use context, you can listen to multiple context
48
00:02:59,719 --> 00:03:03,910
in one of the same component by calling use context
49
00:03:03,910 --> 00:03:08,070
multiple times and pointing at different contexts.
50
00:03:08,070 --> 00:03:11,870
This will not be possible with class based components,
51
00:03:11,870 --> 00:03:16,000
because there you can only connect a class based component
52
00:03:16,000 --> 00:03:18,430
to one context.
53
00:03:18,430 --> 00:03:22,150
And you do that by adding a static property,
54
00:03:22,150 --> 00:03:27,110
by adding the static keyword here and then adding
55
00:03:27,110 --> 00:03:31,890
the property named context type written like this.
56
00:03:31,890 --> 00:03:36,370
And then you assign a value of users context here.
57
00:03:36,370 --> 00:03:40,300
With that you're telling React hey this component
58
00:03:40,300 --> 00:03:43,874
should have access to the user's context context,
59
00:03:43,874 --> 00:03:47,970
but you can only set the static context type property
60
00:03:47,970 --> 00:03:51,620
once so if there are two contexts which should be
61
00:03:51,620 --> 00:03:55,100
connected to one at the same component, this would simply
62
00:03:55,100 --> 00:03:57,680
not be an option, you would have to find some other
63
00:03:57,680 --> 00:04:00,457
work around like wrapping it in a number component
64
00:04:00,457 --> 00:04:02,413
or anything like that.
65
00:04:02,413 --> 00:04:07,079
With that said however, you can then simply access
66
00:04:07,079 --> 00:04:12,079
this context users in this case,
67
00:04:14,120 --> 00:04:17,882
in all the places where you need access to the users.
68
00:04:21,240 --> 00:04:25,850
And with that, it will again work as before as you can
69
00:04:25,850 --> 00:04:28,750
tell but now with context.
70
00:04:28,750 --> 00:04:32,498
So it is all quite straightforward to use but a bit less
71
00:04:32,498 --> 00:04:37,070
flexible than in the functional component world.
72
00:04:37,070 --> 00:04:40,550
Of course you will not always need more than one context
73
00:04:40,550 --> 00:04:45,050
per component, often one context is enough but if you do
74
00:04:45,050 --> 00:04:48,460
need more contexts connected to one component,
75
00:04:48,460 --> 00:04:51,493
then this restriction can of course hurt.
6481
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.