Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,240 --> 00:00:04,010
So how can we now ensure
2
00:00:04,010 --> 00:00:05,920
that the Cart is only showing up
3
00:00:05,920 --> 00:00:08,470
when the your Cart button is clicked,
4
00:00:08,470 --> 00:00:11,130
and can be removed if we click on the backdrop
5
00:00:11,130 --> 00:00:12,513
or the close button?
6
00:00:13,960 --> 00:00:16,690
Well, we need to manage some state
7
00:00:16,690 --> 00:00:19,010
because we have different states now.
8
00:00:19,010 --> 00:00:22,160
One state where the Cart is visible
9
00:00:22,160 --> 00:00:24,920
and one state where it's not visible.
10
00:00:24,920 --> 00:00:26,350
These are two states.
11
00:00:26,350 --> 00:00:29,140
And whenever you have more than one state,
12
00:00:29,140 --> 00:00:32,520
you need to manage that state with React.
13
00:00:32,520 --> 00:00:35,310
And in function components as we have it here,
14
00:00:35,310 --> 00:00:38,080
we use use state for that.
15
00:00:38,080 --> 00:00:42,590
So we import use State from react in the App.js file.
16
00:00:42,590 --> 00:00:44,490
Why in this file?
17
00:00:44,490 --> 00:00:46,840
Because data's where we do render the Cart.
18
00:00:46,840 --> 00:00:48,750
So that is where I wanna manage
19
00:00:48,750 --> 00:00:50,373
the visibility of the Cart.
20
00:00:51,370 --> 00:00:53,990
Of course, we could also do it in some parents component
21
00:00:53,990 --> 00:00:56,900
and pass down the state's data from props,
22
00:00:56,900 --> 00:01:01,300
but the only parent component to the app component is index.
23
00:01:01,300 --> 00:01:02,700
And that's not even a component,
24
00:01:02,700 --> 00:01:04,800
that's just where we initially render it,
25
00:01:04,800 --> 00:01:07,780
and therefore we actually have no parent component.
26
00:01:07,780 --> 00:01:09,320
And even if we would have,
27
00:01:09,320 --> 00:01:11,653
it makes a lot of sense to manage it here.
28
00:01:12,720 --> 00:01:15,940
So, inside of the App function,
29
00:01:15,940 --> 00:01:20,410
I will use the structuring to pull out the two elements
30
00:01:20,410 --> 00:01:23,370
I get back from calling use state.
31
00:01:23,370 --> 00:01:25,080
My initial state is false
32
00:01:25,080 --> 00:01:28,450
because dad is my cartIsShown state,
33
00:01:28,450 --> 00:01:32,650
and we also have a set card is shown function.
34
00:01:32,650 --> 00:01:35,790
And here we are just using useState,
35
00:01:35,790 --> 00:01:38,000
as you learned it from this course.
36
00:01:38,000 --> 00:01:39,900
Registering it with react,
37
00:01:39,900 --> 00:01:42,830
so that react can manage this state for us
38
00:01:42,830 --> 00:01:44,050
for this component.
39
00:01:44,050 --> 00:01:47,260
And then getting access to the latest state snapshot,
40
00:01:47,260 --> 00:01:51,270
whenever this component is re-evaluated and getting access
41
00:01:51,270 --> 00:01:54,883
to this function, which allows us to update the state.
42
00:01:56,850 --> 00:01:59,800
And then here we can add a couple of functions,
43
00:01:59,800 --> 00:02:01,450
cue that functions.
44
00:02:01,450 --> 00:02:03,370
Functions which should be called
45
00:02:03,370 --> 00:02:05,500
when this button is clicked for example,
46
00:02:05,500 --> 00:02:07,223
or when the backdrop is clicked.
47
00:02:08,289 --> 00:02:13,290
Because I'm talking about a showCartHandler function.
48
00:02:13,410 --> 00:02:17,320
Which is a function defined here inside of the App function,
49
00:02:17,320 --> 00:02:21,193
in which I then call setCartIsShown and set this to true.
50
00:02:22,620 --> 00:02:27,620
And then I'll also add a hideCardHandler function like this,
51
00:02:29,460 --> 00:02:33,683
where I setCardIsShown to false.
52
00:02:36,390 --> 00:02:40,240
And now we just need to use that cartIsShown state
53
00:02:40,240 --> 00:02:42,850
and make sure that these two functions
54
00:02:42,850 --> 00:02:45,173
which are just added are getting called.
55
00:02:46,330 --> 00:02:50,150
Now, let's start with using the cartIsShown state.
56
00:02:50,150 --> 00:02:50,983
In the end,
57
00:02:50,983 --> 00:02:54,340
we wanna render the Cart component conditionally.
58
00:02:54,340 --> 00:02:56,540
And you'll learn how that works.
59
00:02:56,540 --> 00:02:59,600
You can simply wrap this in curly braces
60
00:02:59,600 --> 00:03:02,850
to evaluate a dynamic expression.
61
00:03:02,850 --> 00:03:05,896
And then axes cartIsShown and use stat shortcut
62
00:03:05,896 --> 00:03:10,896
with double and operator to render a Cart,
63
00:03:11,560 --> 00:03:15,823
if this is truthy and not render it, if this is falsy.
64
00:03:17,740 --> 00:03:18,600
And with that,
65
00:03:18,600 --> 00:03:22,853
the Cart disappears because initially it's false.
66
00:03:24,070 --> 00:03:26,440
Now showCartHandlers should be called
67
00:03:26,440 --> 00:03:28,370
whenever this button is clicked.
68
00:03:28,370 --> 00:03:31,363
This button however, is part of my Header.
69
00:03:32,580 --> 00:03:35,040
So therefore, we should probably expect
70
00:03:35,040 --> 00:03:39,860
to get that function, that showCardHandler function called
71
00:03:39,860 --> 00:03:41,770
from inside the Header.
72
00:03:41,770 --> 00:03:44,940
And for the header, to call this function,
73
00:03:44,940 --> 00:03:47,300
we need to pass a pointer at the function
74
00:03:47,300 --> 00:03:48,930
down through props.
75
00:03:48,930 --> 00:03:51,130
That's also a pattern you learn about
76
00:03:51,130 --> 00:03:52,330
through out this course.
77
00:03:53,600 --> 00:03:56,130
Now it's up to you, how you name that prop,
78
00:03:56,130 --> 00:03:59,420
but the convention for these props which hold functions,
79
00:03:59,420 --> 00:04:02,040
is to start with on.
80
00:04:02,040 --> 00:04:04,830
Since these are kind of like custom events
81
00:04:04,830 --> 00:04:08,620
which triggered this function, if you look at it like this.
82
00:04:08,620 --> 00:04:11,600
And therefore here I'll name it onShowCart,
83
00:04:11,600 --> 00:04:13,490
again, name is up to you,
84
00:04:13,490 --> 00:04:16,870
and I point at the showCartHandler.
85
00:04:16,870 --> 00:04:20,430
Important, don't execute it here, just point at it,
86
00:04:20,430 --> 00:04:24,130
so that it can be executed from inside the Header
87
00:04:24,130 --> 00:04:26,563
through that onShowCart prop.
88
00:04:27,530 --> 00:04:29,890
So insides that Header component,
89
00:04:29,890 --> 00:04:32,440
we now wanna execute that function,
90
00:04:32,440 --> 00:04:35,540
and we wanna execute it whenever this HeaderCartButton
91
00:04:35,540 --> 00:04:36,423
is being clicked.
92
00:04:37,330 --> 00:04:40,460
Now, this of course, is a number of custom component,
93
00:04:40,460 --> 00:04:44,190
and therefore here, let's say we expect that we have
94
00:04:44,190 --> 00:04:47,120
a onClick prop, again, this is up to you,
95
00:04:47,120 --> 00:04:48,610
I'm just using this name,
96
00:04:48,610 --> 00:04:50,950
which looks like the built in onClick,
97
00:04:50,950 --> 00:04:55,100
because indent one React to the button being clicked.
98
00:04:55,100 --> 00:04:58,150
But technically you can use any name you want here,
99
00:04:58,150 --> 00:04:59,960
because you will have to write code
100
00:04:59,960 --> 00:05:04,890
inside of HeaderCartButton to handle this prop anyways.
101
00:05:04,890 --> 00:05:09,640
But to this prop, I wanna pass a pointer, to that function,
102
00:05:09,640 --> 00:05:12,963
I'm receiving on props onShowCart here.
103
00:05:13,930 --> 00:05:18,930
So in the Header component, we received the onShowCart prop,
104
00:05:19,300 --> 00:05:21,870
which points at showCartHandler.
105
00:05:21,870 --> 00:05:25,810
And now I'm forwarding that point at that function
106
00:05:25,810 --> 00:05:28,563
to the onClick prop on the HeaderCartButton.
107
00:05:30,060 --> 00:05:32,083
And now in the HeaderCartButton we therefore
108
00:05:32,083 --> 00:05:36,080
need to handle that onClick prop which we receive.
109
00:05:36,080 --> 00:05:37,880
Here on that built in button,
110
00:05:37,880 --> 00:05:40,190
there is a built in onClick prop,
111
00:05:40,190 --> 00:05:42,830
so here the name is not up to you.
112
00:05:42,830 --> 00:05:46,040
And I now wanna point at props onClick.
113
00:05:46,040 --> 00:05:48,460
So that the function I receive on
114
00:05:48,460 --> 00:05:51,090
onClick in the HeaderCartButton,
115
00:05:51,090 --> 00:05:54,780
is forwarded to the enclave prop on the button.
116
00:05:54,780 --> 00:05:56,550
This can be confusing,
117
00:05:56,550 --> 00:05:59,720
but in the end we're just passing down a pointer
118
00:05:59,720 --> 00:06:04,373
at this showCartHandler button to that HeaderCartButton.
119
00:06:05,810 --> 00:06:09,830
Now, you learn that if you have a prop chain like this,
120
00:06:09,830 --> 00:06:14,600
where you pass a prop through multiple levels of components,
121
00:06:14,600 --> 00:06:17,220
could be replaced with context.
122
00:06:17,220 --> 00:06:19,890
And indeed we could use context here,
123
00:06:19,890 --> 00:06:23,190
for handling this Cart visibility state.
124
00:06:23,190 --> 00:06:24,980
I will not do it here though,
125
00:06:24,980 --> 00:06:28,950
because I think here it is okay to have these two levels
126
00:06:28,950 --> 00:06:32,400
of components through which I'm passing props.
127
00:06:32,400 --> 00:06:34,610
But you could implement context here as well
128
00:06:34,610 --> 00:06:35,653
if you wanted to.
129
00:06:36,560 --> 00:06:40,400
We are going to use context in this demo application as well
130
00:06:40,400 --> 00:06:42,450
but not for this Cart button.
131
00:06:42,450 --> 00:06:45,920
Here I will instead go for this prop drilling,
132
00:06:45,920 --> 00:06:47,863
so for this chain of props.
133
00:06:48,900 --> 00:06:51,350
With that however, if you save everything,
134
00:06:51,350 --> 00:06:53,973
if you click your Cart, it should open up.
135
00:06:55,900 --> 00:06:58,960
Now to make it closable, we wanna make sure
136
00:06:58,960 --> 00:07:01,000
that both that close button
137
00:07:01,000 --> 00:07:04,793
as well as a click on the backdrop closes the Cart.
138
00:07:06,210 --> 00:07:11,210
So they for in App.js, where we use the Cart component,
139
00:07:12,220 --> 00:07:16,340
we should probably expect a onClosed prop
140
00:07:16,340 --> 00:07:18,020
or something like this.
141
00:07:18,020 --> 00:07:20,120
As always the name is up to you,
142
00:07:20,120 --> 00:07:22,840
to which we pass the hideCardHandler.
143
00:07:25,020 --> 00:07:28,180
And now it's inside of that cart component,
144
00:07:28,180 --> 00:07:32,340
where we need to handle this onClosed prop.
145
00:07:32,340 --> 00:07:35,960
For one, I wanna go to that Close button
146
00:07:35,960 --> 00:07:39,180
and dare at onClick prop,
147
00:07:39,180 --> 00:07:41,590
and point at prop start onClose.
148
00:07:43,490 --> 00:07:46,250
OnClose because that's the name I just chose
149
00:07:46,250 --> 00:07:48,430
in the App.js file.
150
00:07:48,430 --> 00:07:50,940
It's the onClose prop on which we received
151
00:07:50,940 --> 00:07:54,440
the hideCardHandler and they offer it's dysfunction,
152
00:07:54,440 --> 00:07:56,880
which I now wanna forward to the button.
153
00:07:56,880 --> 00:07:58,700
So that, when I click on the button,
154
00:07:58,700 --> 00:08:01,833
that hideCardHandler function is being called.
155
00:08:03,120 --> 00:08:05,220
Now that's one way of closing the Modal.
156
00:08:05,220 --> 00:08:08,490
It should always be closed, If the backdrop is clicked.
157
00:08:08,490 --> 00:08:10,100
And to make that happen,
158
00:08:10,100 --> 00:08:12,570
we should go to the Modal component.
159
00:08:12,570 --> 00:08:15,210
And on the backdrop there,
160
00:08:15,210 --> 00:08:18,943
we should add a onClick prop to the div,
161
00:08:20,010 --> 00:08:22,370
so using that built in onClick prop,
162
00:08:22,370 --> 00:08:26,200
which exists on basically all HTML elements,
163
00:08:26,200 --> 00:08:30,420
and there, forward the value we expect to get
164
00:08:30,420 --> 00:08:32,900
on whatever prop you want.
165
00:08:32,900 --> 00:08:35,982
So here let's say, I expect to get a onClose prop,
166
00:08:36,950 --> 00:08:38,990
now here, when we use the backdrop,
167
00:08:38,990 --> 00:08:41,520
we need to set that onClose prop,
168
00:08:41,520 --> 00:08:45,250
because we just added it inside of the backdrop.
169
00:08:45,250 --> 00:08:48,740
And here I expect to get another onClose prop
170
00:08:48,740 --> 00:08:51,510
but now on the Modal component itself.
171
00:08:51,510 --> 00:08:54,120
So again, here I'll be passing a value
172
00:08:54,120 --> 00:08:58,900
through multiple levels, down to the backdrop.
173
00:08:58,900 --> 00:09:01,700
You could use context, but I also wanna highlight,
174
00:09:01,700 --> 00:09:05,950
that if you are using context and you always bind a Click
175
00:09:05,950 --> 00:09:09,620
on the backdrop to closing the Cart,
176
00:09:09,620 --> 00:09:12,760
then you would make the spectrum very specific
177
00:09:12,760 --> 00:09:15,780
and you would not be able to use the Modal for ever
178
00:09:15,780 --> 00:09:17,420
kinds of content.
179
00:09:17,420 --> 00:09:20,760
Because clicking the backdrop would always close the Cart.
180
00:09:20,760 --> 00:09:23,740
So if you had our content, in Modals as well
181
00:09:23,740 --> 00:09:26,090
then you would be too specific.
182
00:09:26,090 --> 00:09:29,220
So here, using multiple levels of props,
183
00:09:29,220 --> 00:09:30,750
could even be better,
184
00:09:30,750 --> 00:09:34,260
because in this case, it makes the Modal more reusable
185
00:09:34,260 --> 00:09:38,750
and doesn't tie it to one specific use case.
186
00:09:38,750 --> 00:09:40,270
That's just a side note.
187
00:09:40,270 --> 00:09:41,210
Why here?
188
00:09:41,210 --> 00:09:44,760
I think using props instead of context is good,
189
00:09:44,760 --> 00:09:48,500
even though we are passing data or to be precise
190
00:09:48,500 --> 00:09:50,930
a function through multiple levels
191
00:09:50,930 --> 00:09:52,593
of components in this case.
192
00:09:54,090 --> 00:09:58,220
And they are for now in the Cart component,
193
00:09:58,220 --> 00:10:01,800
on Modal, we can add the onClose prop as well
194
00:10:01,800 --> 00:10:03,833
and forward props onClose.
195
00:10:04,930 --> 00:10:08,500
And that's now the onClose prop for the Cart component
196
00:10:08,500 --> 00:10:11,460
which is being set in the App component.
197
00:10:11,460 --> 00:10:14,823
And that is then this hideCartHandler function.
198
00:10:16,030 --> 00:10:18,920
With that, We can open the Cart,
199
00:10:18,920 --> 00:10:21,180
click on the backdrop to close it
200
00:10:21,180 --> 00:10:23,750
or click on the Close button to close it.
201
00:10:23,750 --> 00:10:25,083
So that's working.
202
00:10:26,030 --> 00:10:29,820
And with that, we implemented the Cart,
203
00:10:29,820 --> 00:10:32,440
at least with some dummy data here.
204
00:10:32,440 --> 00:10:35,890
It's not finished at all at the moment though,
205
00:10:35,890 --> 00:10:40,120
as a next step, I want to make sure that we can really
206
00:10:40,120 --> 00:10:45,100
add Cart items and then we'll also refine the Cart overall,
207
00:10:45,100 --> 00:10:46,153
and so on.
16338
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.