Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,020 --> 00:00:03,630
To use the context API
2
00:00:03,630 --> 00:00:05,460
I'll create a new folder,
3
00:00:05,460 --> 00:00:06,790
which you don't have to do,
4
00:00:06,790 --> 00:00:10,810
but I wanna store my context related files
5
00:00:10,810 --> 00:00:13,620
my application wide state related files
6
00:00:13,620 --> 00:00:15,820
in a separate folder.
7
00:00:15,820 --> 00:00:18,170
And I'll name the folder store.
8
00:00:18,170 --> 00:00:20,970
Since this contains my state store
9
00:00:20,970 --> 00:00:23,720
for the entire application.
10
00:00:23,720 --> 00:00:26,180
And in store I'll add a new file, which I'll name
11
00:00:26,180 --> 00:00:30,570
notification-context.JS.
12
00:00:30,570 --> 00:00:33,653
This file is all is up to you though the file name.
13
00:00:34,520 --> 00:00:37,470
Now in here we wanna import from react
14
00:00:37,470 --> 00:00:41,620
and we wanna import the create context function
15
00:00:41,620 --> 00:00:44,990
because the goal here is to create a new context,
16
00:00:44,990 --> 00:00:48,710
a new context for managing our notifications.
17
00:00:48,710 --> 00:00:52,890
Now we can provide an initialization object here,
18
00:00:52,890 --> 00:00:56,950
which defines the structure of this context
19
00:00:56,950 --> 00:00:58,964
and the context so does object,
20
00:00:58,964 --> 00:01:03,350
which will also be exposed to the different components
21
00:01:03,350 --> 00:01:05,209
in our application later,
22
00:01:05,209 --> 00:01:10,210
this context should actually have a notification
23
00:01:11,890 --> 00:01:13,540
property let's say,
24
00:01:13,540 --> 00:01:14,930
which is null initially,
25
00:01:14,930 --> 00:01:18,950
but which eventually will be an object with a title,
26
00:01:18,950 --> 00:01:21,400
a message and a status property.
27
00:01:21,400 --> 00:01:22,823
So a nested object.
28
00:01:24,020 --> 00:01:28,170
And besides that, besides that notification property,
29
00:01:28,170 --> 00:01:32,470
I also want to have a method for showing a notification,
30
00:01:32,470 --> 00:01:36,440
the show notification method.
31
00:01:36,440 --> 00:01:40,190
So a property which holds a function,
32
00:01:40,190 --> 00:01:41,890
which initially may be empty.
33
00:01:41,890 --> 00:01:43,720
So which isn't doing anything here
34
00:01:43,720 --> 00:01:46,980
because we'll replace it with a different function
35
00:01:46,980 --> 00:01:49,190
in a different place anyways,
36
00:01:49,190 --> 00:01:51,600
I'm just setting up my base context here
37
00:01:51,600 --> 00:01:53,600
to get better auto completion.
38
00:01:53,600 --> 00:01:58,320
So the data here is really just dummy data in the end.
39
00:01:58,320 --> 00:02:01,710
Still I wanna have a show notification function
40
00:02:01,710 --> 00:02:02,920
and then let's say also
41
00:02:02,920 --> 00:02:06,533
a hide notification function like this.
42
00:02:07,640 --> 00:02:09,600
That could be our context.
43
00:02:09,600 --> 00:02:12,700
And with that all store it in a constant,
44
00:02:12,700 --> 00:02:16,323
the notification context.
45
00:02:17,320 --> 00:02:21,730
And I'll then export this notification context here
46
00:02:21,730 --> 00:02:25,120
to make it a way level outside of this file as well.
47
00:02:25,120 --> 00:02:27,420
So that we can tap into this context
48
00:02:27,420 --> 00:02:32,020
and use this context with the use context react hook
49
00:02:32,020 --> 00:02:34,163
in our react components.
50
00:02:35,610 --> 00:02:39,100
Now you know that the context which we create here
51
00:02:39,100 --> 00:02:42,290
actually allows us to create a component,
52
00:02:42,290 --> 00:02:44,910
a provider a component which we can wrap
53
00:02:44,910 --> 00:02:48,710
around the components and their child components
54
00:02:48,710 --> 00:02:52,430
that should be able to tap into this context.
55
00:02:52,430 --> 00:02:54,130
And therefore I'll actually change this
56
00:02:54,130 --> 00:02:56,023
to an upper case character here.
57
00:02:57,025 --> 00:02:59,970
The N to make it clear that we can get component
58
00:03:00,955 --> 00:03:02,403
out of this thing here.
59
00:03:03,440 --> 00:03:06,995
Now the goal will be to wrap this overall,
60
00:03:06,995 --> 00:03:10,750
content here with our context later.
61
00:03:10,750 --> 00:03:14,630
So to wrap all these components with our context.
62
00:03:14,630 --> 00:03:18,470
However I don't just wanna provide my context like this.
63
00:03:18,470 --> 00:03:21,240
Instead I wanna create a separate component,
64
00:03:21,240 --> 00:03:24,960
which also manages all the context related state.
65
00:03:24,960 --> 00:03:28,040
So back end is notification context
66
00:03:28,040 --> 00:03:30,260
file in the store folder.
67
00:03:30,260 --> 00:03:34,140
I'll not just create a context here and export it.
68
00:03:34,140 --> 00:03:37,340
Instead I'll also create a component in here,
69
00:03:37,340 --> 00:03:41,623
the notification context provider component,
70
00:03:42,680 --> 00:03:45,710
and this component will return
71
00:03:45,710 --> 00:03:49,290
notification context.Provider.
72
00:03:49,290 --> 00:03:53,000
So this component which we can get out of this context
73
00:03:53,960 --> 00:03:57,030
wrapped around props children.
74
00:03:57,030 --> 00:04:01,230
So here I'll wrap it around props children,
75
00:04:01,230 --> 00:04:02,701
and I'm doing that so that
76
00:04:02,701 --> 00:04:06,417
we can use the notification context provider component
77
00:04:06,417 --> 00:04:09,470
to wrap it around our components,
78
00:04:09,470 --> 00:04:12,010
which will then automatically have access
79
00:04:12,010 --> 00:04:14,350
to all our context.
80
00:04:14,350 --> 00:04:17,200
Hence I'll also exports this function.
81
00:04:17,200 --> 00:04:21,029
And now it's this notification context per wider function,
82
00:04:21,029 --> 00:04:23,370
which I wanna wrap around my components,
83
00:04:23,370 --> 00:04:26,450
which should have access to this context.
84
00:04:26,450 --> 00:04:30,150
And in this case that's in the underscore app JS file.
85
00:04:30,150 --> 00:04:33,720
I wanna wrap basically all that content here
86
00:04:33,720 --> 00:04:36,030
with my context.
87
00:04:36,030 --> 00:04:38,190
So here we can wrap everything
88
00:04:38,190 --> 00:04:42,490
with notification context provider
89
00:04:42,490 --> 00:04:46,350
and make sure that you import notification context provider
90
00:04:46,350 --> 00:04:50,740
from this notification context file into store folder.
91
00:04:50,740 --> 00:04:54,450
And then wrap that around all this content,
92
00:04:54,450 --> 00:04:56,520
which means that all these components,
93
00:04:56,520 --> 00:04:58,600
which are wrapped by the provider
94
00:04:58,600 --> 00:05:02,410
will be able to utilize our context later.
95
00:05:02,410 --> 00:05:04,820
And I'm creating this separate wrapper
96
00:05:04,820 --> 00:05:08,700
because this notification context provider function here
97
00:05:08,700 --> 00:05:11,990
will not just wrap props children.
98
00:05:11,990 --> 00:05:13,530
We wouldn't need it for that,
99
00:05:13,530 --> 00:05:15,500
if that would be all it's doing,
100
00:05:15,500 --> 00:05:17,820
but this function should also manage
101
00:05:17,820 --> 00:05:20,700
all the context related state.
102
00:05:20,700 --> 00:05:23,210
So here we will work with use state
103
00:05:23,210 --> 00:05:26,450
and so on to manage the notification state
104
00:05:26,450 --> 00:05:30,240
and to create the show and hide notification functions
105
00:05:30,240 --> 00:05:32,890
inside of this component.
106
00:05:32,890 --> 00:05:35,173
And that's what we'll do as a next step.
8232
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.