Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,140 --> 00:00:08,170
Now that we had a thorough look at you, state use of fact use reducer y'all to use to use callback.
2
00:00:09,390 --> 00:00:15,540
Let's have a look at how we can use to context API, which is also an exciting feature about REACT together
3
00:00:15,540 --> 00:00:18,510
with functional components and Hooke's.
4
00:00:19,440 --> 00:00:24,360
And for that, I got my all for Jaspal, which we haven't used thus far, which is a very simple login
5
00:00:24,360 --> 00:00:25,180
form.
6
00:00:25,200 --> 00:00:29,280
Well, it doesn't even have inputs, but it has a login button, at least, which we can click.
7
00:00:29,970 --> 00:00:35,010
And I want to show that if you're not logged in yet and alternatively show my ingredients if we are
8
00:00:35,010 --> 00:00:37,640
locked in, that's where the ABC's fall will come into play.
9
00:00:37,890 --> 00:00:39,630
Right now, it always returns ingredients.
10
00:00:39,840 --> 00:00:45,360
The goal is that now this does not always return ingredients, but instead it returns either ingredients
11
00:00:45,360 --> 00:00:46,740
or the off screen.
12
00:00:47,660 --> 00:00:56,300
So here in the U.S., I'll import all from components off, and while we could not always return off,
13
00:00:56,300 --> 00:01:02,500
of course, which means we would always see this, but now we got no way off going to our ingredients.
14
00:01:02,510 --> 00:01:04,989
So the goal will be that we switch between those.
15
00:01:05,510 --> 00:01:11,240
Now, of course, for that, we can manage all the off logic here in app, use a step in to render different
16
00:01:11,240 --> 00:01:11,660
content.
17
00:01:11,840 --> 00:01:12,730
We could do that.
18
00:01:12,740 --> 00:01:18,080
But in a more complex app, of course, you might be interested in whether the user is authenticated
19
00:01:18,080 --> 00:01:24,950
or not in a couple of components, maybe deeply nested into other components and therefore passing data
20
00:01:24,950 --> 00:01:30,530
about the authentication status via props might not be what you want to do because you build very long
21
00:01:30,530 --> 00:01:32,660
chains of props, which you have to pass around.
22
00:01:34,220 --> 00:01:39,530
That's what context can help us, Fridell, create a new fuller context, which is optional, but I
23
00:01:39,530 --> 00:01:45,820
like to store my context in there and named as off context, just as the filename in there.
24
00:01:46,670 --> 00:01:51,830
Now, in this off context file, we can create context by importing react from react.
25
00:01:52,400 --> 00:01:55,130
And then we create our, let's say, off context.
26
00:01:55,130 --> 00:02:01,430
Name is totally up to you, but should start of the capital character actually by calling react create
27
00:02:01,430 --> 00:02:02,060
context.
28
00:02:02,060 --> 00:02:04,530
And this is also something I covered earlier in the course already.
29
00:02:04,550 --> 00:02:07,010
This is the context API offered by react.
30
00:02:07,250 --> 00:02:09,919
So go back to that component deep dive section.
31
00:02:09,919 --> 00:02:16,490
If you're not entirely sure what this means now we can initializes with a value and Deira want to manage
32
00:02:16,490 --> 00:02:18,080
whether we're authentic or not.
33
00:02:18,590 --> 00:02:24,290
And I want to provide a login function which here is just an empty function but which I still adhere
34
00:02:24,290 --> 00:02:24,980
to get better.
35
00:02:24,980 --> 00:02:28,130
All the completion will overwrite this with a concrete value soon.
36
00:02:29,060 --> 00:02:36,080
I will also export this context object, but I will also use it in that same file to create a component
37
00:02:36,080 --> 00:02:36,380
there.
38
00:02:36,950 --> 00:02:39,920
I'll named a component of context provider.
39
00:02:41,010 --> 00:02:47,130
It's a normal react component, a functional component, as you can tell, and in there I return.
40
00:02:48,490 --> 00:02:55,630
Off context dot provider, so I'm using off context up there and this automatically is a valid react
41
00:02:55,630 --> 00:03:00,060
component, or at least with dot provider, it gives you a valid react component.
42
00:03:00,580 --> 00:03:01,810
And between their.
43
00:03:02,720 --> 00:03:08,490
I want to allow anything, so I'll add props children there so that we can use the off context provider,
44
00:03:08,510 --> 00:03:12,830
pass anything as a child to it, and it will be wrapped by our context.
45
00:03:13,790 --> 00:03:19,110
Now it's in here where I'm going to manage to log in status, and I'll do this with the help of U.S
46
00:03:19,130 --> 00:03:22,610
state now use state because it's a really simple state.
47
00:03:23,270 --> 00:03:28,070
It's a simple state because I only want to track whoever the user is, authenticate it, and I want
48
00:03:28,070 --> 00:03:31,670
to be able to set is authenticate it.
49
00:03:32,960 --> 00:03:38,300
Or not, and initially, the user is not authenticated, which is why initializes with a false value.
50
00:03:40,430 --> 00:03:48,320
Now, all that aFunction year log user in or whatever you want to name it or log in handler to name
51
00:03:48,320 --> 00:03:49,080
is totally up to you.
52
00:03:49,760 --> 00:03:55,190
This takes no arguments here because it's just about showing how to work with context, not about really
53
00:03:55,190 --> 00:03:57,260
taking inputs here and validating that.
54
00:03:57,710 --> 00:03:59,870
And then I'll call set is authenticated.
55
00:04:00,350 --> 00:04:01,350
And that is true.
56
00:04:01,370 --> 00:04:02,270
That's what I do here.
57
00:04:02,270 --> 00:04:03,510
Very trivial function.
58
00:04:04,250 --> 00:04:11,090
Now the off context provider gets a value which will automatically be distributed to everyone listening
59
00:04:11,450 --> 00:04:14,900
if if it changes, which describes our context.
60
00:04:14,900 --> 00:04:20,480
Initially we have this starting value, but now it will immediately overwrite it with a new object where
61
00:04:20,480 --> 00:04:25,240
I have log in and that's my log in handler pointing at dysfunction.
62
00:04:25,310 --> 00:04:32,680
So now we have a real function behind it and is off where we have is authenticated.
63
00:04:32,690 --> 00:04:34,370
So it is state value.
64
00:04:35,450 --> 00:04:40,220
And again, if this changes, so, for example, if you call set as sophisticated, everyone listening
65
00:04:40,220 --> 00:04:41,840
will get this updated value.
66
00:04:42,980 --> 00:04:49,370
So now we just need to export maybe as a default dose of context providers.
67
00:04:49,370 --> 00:04:51,140
So just component, which we built here.
68
00:04:51,560 --> 00:04:55,310
And now I have this default export and here we have a named export.
69
00:04:56,160 --> 00:05:01,370
And now we can use that and we can use it in the index today as follows the root of our application
70
00:05:01,890 --> 00:05:05,040
there, we can import you off context provider from.
71
00:05:06,700 --> 00:05:15,250
Context and context of context and wrap our entire app with it, so wrap the entire app with off context
72
00:05:15,250 --> 00:05:21,150
provider like this so that the entire app receives our context or is able to receive it.
73
00:05:21,160 --> 00:05:23,920
You can listen to the context from everywhere, the app.
74
00:05:23,920 --> 00:05:25,180
That's the right way of putting it.
75
00:05:26,080 --> 00:05:30,570
So all context provider is wrapped around our app and app as I'm already interested.
76
00:05:30,580 --> 00:05:34,720
And that's the thing which could be problematic with functional props.
77
00:05:35,290 --> 00:05:38,200
We can of course, import our off context.
78
00:05:38,290 --> 00:05:45,490
So not the provider component, but the context object itself from context of context.
79
00:05:46,670 --> 00:05:52,280
However, here, I mean, the named export, so we have to wrap this in curly braces and what we can
80
00:05:52,280 --> 00:05:59,720
do is we can wrap this year with off context dot consumer now to set up does this render function in
81
00:05:59,720 --> 00:06:00,460
the end in there?
82
00:06:01,250 --> 00:06:04,520
This would be how you can use context and functional components.
83
00:06:04,940 --> 00:06:10,760
This means that if you need data from the context anywhere else but in your J as X3, you have no way
84
00:06:10,760 --> 00:06:11,460
of getting it.
85
00:06:11,510 --> 00:06:18,530
You would need to transform this into a class based component so that you can add to static context
86
00:06:18,530 --> 00:06:21,370
type thing, which you might remember from earlier and the course.
87
00:06:22,070 --> 00:06:25,450
Well, thankfully, there is a hook to also fix this.
88
00:06:25,850 --> 00:06:27,290
The U.S. context hook.
89
00:06:27,800 --> 00:06:33,320
You use it like this, you pass in the context you want to listen to, so to say, off context in my
90
00:06:33,320 --> 00:06:39,410
case, and you get a handle to that context, which is then stored in this constant and you can name
91
00:06:39,410 --> 00:06:41,810
it whatever you want here and name it off context.
92
00:06:42,620 --> 00:06:45,860
And now here in Objets.
93
00:06:48,980 --> 00:06:56,510
I can listen to you off context, dot is off or not listen to it, but check it and this will be rebuilt
94
00:06:56,510 --> 00:06:59,950
whenever the context changes, whenever value in their changes.
95
00:06:59,960 --> 00:07:03,280
So whenever we changes all, for example, then this will rebuild.
96
00:07:03,500 --> 00:07:04,730
So here a listen to it.
97
00:07:05,150 --> 00:07:07,010
And then I want to output a different content.
98
00:07:07,340 --> 00:07:11,420
Let's say my default content here is off component like this.
99
00:07:12,020 --> 00:07:15,980
But if we are authenticated, then content of course, is the ingredients component.
100
00:07:16,310 --> 00:07:22,010
And then down there, instead of hard coding context, I return content, which is change depending
101
00:07:22,010 --> 00:07:23,450
on the value in context.
102
00:07:24,560 --> 00:07:31,130
Now, we just need to make sure that clicking on the login button also does reach the well log in handler
103
00:07:31,130 --> 00:07:31,910
function here.
104
00:07:33,070 --> 00:07:38,560
Keep in mind that a pointer at this function is stored in log in in the context, so all they have to
105
00:07:38,560 --> 00:07:45,100
do is I have to go to the place where the button is, which is in the off component, and also import
106
00:07:45,460 --> 00:07:50,320
the off context to dare off context named import from.
107
00:07:52,110 --> 00:08:00,060
The context folder off that context and import the use context, Hoak here as well, and now here.
108
00:08:01,830 --> 00:08:06,780
I can get access to my off contacts with you as context, pointing out of context.
109
00:08:08,380 --> 00:08:13,480
And the Log-in handler, I'll call off context dot log-in as a function.
110
00:08:14,760 --> 00:08:20,070
And now with that, if you click that button, we're locked in, we see the ingredients, so this is
111
00:08:20,070 --> 00:08:24,660
how we can work with context, with functional components and the U.S. context hook.
11766
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.