Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,302 --> 00:00:04,008
So we're using useContext
2
00:00:04,008 --> 00:00:06,140
to pass the isLoggedIn state
3
00:00:06,140 --> 00:00:08,020
to my other components here.
4
00:00:08,020 --> 00:00:08,920
That's great.
5
00:00:08,920 --> 00:00:10,680
But of course you will notice
6
00:00:10,680 --> 00:00:14,070
that we're still forwarding the onLogout prop
7
00:00:14,070 --> 00:00:17,340
or the logoutHandler function through props
8
00:00:17,340 --> 00:00:19,900
and that's of course still not ideal.
9
00:00:19,900 --> 00:00:23,230
It would be nicer if we could use the MainHeader like this.
10
00:00:23,230 --> 00:00:25,840
So without passing any data to it
11
00:00:25,840 --> 00:00:28,760
because we don't need to forward onLogout.
12
00:00:28,760 --> 00:00:31,050
But of course at the moment this would not work.
13
00:00:31,050 --> 00:00:32,930
This would mean at the moment
14
00:00:32,930 --> 00:00:35,110
that we're simply not able to log out
15
00:00:35,110 --> 00:00:37,800
because we're not passing our logoutHandler.
16
00:00:37,800 --> 00:00:39,610
So hammering the log out button here
17
00:00:39,610 --> 00:00:41,560
in the navigation wouldn't do anything.
18
00:00:42,870 --> 00:00:46,330
The good thing is we can set up a dynamic context
19
00:00:46,330 --> 00:00:48,480
where we don't just pass data
20
00:00:48,480 --> 00:00:52,300
to our components but also functions,
21
00:00:52,300 --> 00:00:54,610
which technically of course owes this data,
22
00:00:54,610 --> 00:00:56,750
but still you get my point.
23
00:00:56,750 --> 00:00:59,075
Here all we need to do is
24
00:00:59,075 --> 00:01:01,680
in our AuthContext.Provider
25
00:01:01,680 --> 00:01:02,820
where we set up the value
26
00:01:02,820 --> 00:01:05,850
besides passing down isLoggedIn.
27
00:01:05,850 --> 00:01:07,700
We can also pass down,
28
00:01:07,700 --> 00:01:09,330
let's say onLogout
29
00:01:09,330 --> 00:01:11,380
where we point at the logoutHandler
30
00:01:13,130 --> 00:01:14,113
just like this.
31
00:01:15,020 --> 00:01:16,700
If I do that
32
00:01:16,700 --> 00:01:18,340
and make sure you don't execute it
33
00:01:18,340 --> 00:01:20,780
you just wanna point at the function.
34
00:01:20,780 --> 00:01:22,070
If I do that,
35
00:01:22,070 --> 00:01:24,640
then every listening component
36
00:01:24,640 --> 00:01:27,350
every component that listens to Auth context
37
00:01:27,350 --> 00:01:31,230
we'll be able to utilize logoutHandler here
38
00:01:32,970 --> 00:01:36,193
simply through the onLogout context value.
39
00:01:38,270 --> 00:01:40,880
So therefore back in Navigation
40
00:01:40,880 --> 00:01:43,670
where we are referring to props.onLogout
41
00:01:43,670 --> 00:01:46,393
We can now simply do ctx.onLogout.
42
00:01:48,030 --> 00:01:49,400
And then this will work,
43
00:01:49,400 --> 00:01:51,430
because on the context object
44
00:01:51,430 --> 00:01:54,740
we now have the onLogout value.
45
00:01:54,740 --> 00:01:56,360
So if you save that,
46
00:01:56,360 --> 00:01:59,380
now logging out works again.
47
00:01:59,380 --> 00:02:02,770
And of course the rest of the app also works just fine,
48
00:02:02,770 --> 00:02:04,430
as you can tell.
49
00:02:04,430 --> 00:02:06,680
So this now all works again
50
00:02:06,680 --> 00:02:08,380
with the help of context
51
00:02:08,380 --> 00:02:11,190
where you cannot just pass down values
52
00:02:11,190 --> 00:02:12,920
like strings objects and so on
53
00:02:12,920 --> 00:02:16,180
but where you can also pass down functions
54
00:02:16,180 --> 00:02:19,380
and therefore now, we're using this app-wide
55
00:02:19,380 --> 00:02:22,350
or component-wide context object
56
00:02:22,350 --> 00:02:25,080
to manage our state and to manage the function
57
00:02:25,080 --> 00:02:27,020
that changes the state.
58
00:02:27,020 --> 00:02:28,430
Hence we don't need props
59
00:02:28,430 --> 00:02:30,320
in the navigation component anymore
60
00:02:31,710 --> 00:02:33,180
and we can remove it.
61
00:02:33,180 --> 00:02:37,073
And you now have a great example for using context.
62
00:02:38,110 --> 00:02:42,930
Now here in my main section here,
63
00:02:42,930 --> 00:02:47,630
I will still pass down the logoutHandler through onLogout
64
00:02:47,630 --> 00:02:50,610
and the loginHandler through onLogin
65
00:02:50,610 --> 00:02:53,670
because I directly use those handlers
66
00:02:53,670 --> 00:02:56,420
in the Login and Home components.
67
00:02:56,420 --> 00:02:57,910
You see in the Login component
68
00:02:57,910 --> 00:03:00,257
we directly refer props.onLogin
69
00:03:02,656 --> 00:03:04,000
where we're not forwarding it
70
00:03:04,000 --> 00:03:05,510
We're using it here.
71
00:03:05,510 --> 00:03:07,050
And the same into Home component.
72
00:03:07,050 --> 00:03:09,730
I'm directly using onLogout here.
73
00:03:09,730 --> 00:03:11,630
Sure, we're technically forwarding it
74
00:03:11,630 --> 00:03:13,430
to our own Button component
75
00:03:13,430 --> 00:03:16,280
but this is a pure presentational component.
76
00:03:16,280 --> 00:03:17,970
And this here is actually fine.
77
00:03:17,970 --> 00:03:21,380
I don't wanna use context inside of my Button
78
00:03:21,380 --> 00:03:24,960
to always bind Button onclicks to onLogout
79
00:03:24,960 --> 00:03:27,810
because whilst this would remove the need
80
00:03:27,810 --> 00:03:31,370
to pass onLogout like this,
81
00:03:31,370 --> 00:03:32,210
it would also mean
82
00:03:32,210 --> 00:03:35,200
that our Button is now always logging the user out
83
00:03:35,200 --> 00:03:38,360
and not able to do anything else upon a click.
84
00:03:38,360 --> 00:03:41,240
And this all just shows you when to use props.
85
00:03:41,240 --> 00:03:42,890
And when to use context.
86
00:03:42,890 --> 00:03:44,330
In most cases,
87
00:03:44,330 --> 00:03:47,880
you will use props to pass data to components
88
00:03:47,880 --> 00:03:51,490
because props are your mechanism to configure components
89
00:03:51,490 --> 00:03:53,630
and to make them reusable.
90
00:03:53,630 --> 00:03:56,720
Only if you have something which you would forward
91
00:03:56,720 --> 00:03:58,460
through a lot of components
92
00:03:58,460 --> 00:04:01,080
and you're forwarding it to a component
93
00:04:01,080 --> 00:04:03,160
that does something very specific.
94
00:04:03,160 --> 00:04:04,900
Like for example, the Navigation
95
00:04:04,900 --> 00:04:08,080
where this button will always log the user out.
96
00:04:08,080 --> 00:04:11,250
In such cases, you wanna consider context.
97
00:04:11,250 --> 00:04:13,620
And as with everything in programming
98
00:04:13,620 --> 00:04:15,720
with more experience it will also
99
00:04:15,720 --> 00:04:18,779
of course become clearer and more natural
100
00:04:18,779 --> 00:04:20,163
when to use what.
101
00:04:21,089 --> 00:04:23,810
You can of course always rely on prop chains
102
00:04:23,810 --> 00:04:25,120
if you prefer that,
103
00:04:25,120 --> 00:04:28,140
but context allows you to write more concise code
104
00:04:28,140 --> 00:04:29,780
and it often makes managing
105
00:04:29,780 --> 00:04:33,143
such app-wide state simply a bit easier.
7661
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.