Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,180 --> 00:00:05,980
So let's start using our context.
2
00:00:05,980 --> 00:00:08,039
For this in this function component
3
00:00:08,039 --> 00:00:12,030
we use the use context React hook from React
4
00:00:12,030 --> 00:00:13,300
as you learned.
5
00:00:13,300 --> 00:00:16,343
And we use it here in the Header Cart Button for a start.
6
00:00:17,300 --> 00:00:20,340
Now we will also need to import the context itself.
7
00:00:20,340 --> 00:00:23,370
So we need to import cart context,
8
00:00:23,370 --> 00:00:27,700
not the cart provider but the cart context object
9
00:00:27,700 --> 00:00:32,700
from going up, going up store cart context.
10
00:00:34,230 --> 00:00:36,000
We don't need the provider component
11
00:00:36,000 --> 00:00:38,510
but really the context itself.
12
00:00:38,510 --> 00:00:40,450
And then we can call use context
13
00:00:40,450 --> 00:00:43,400
and pass this cart context to it.
14
00:00:43,400 --> 00:00:45,960
To get access to this cart context
15
00:00:45,960 --> 00:00:48,940
which is managed by the closest provider
16
00:00:48,940 --> 00:00:52,550
which is this cart provider used in the app component
17
00:00:52,550 --> 00:00:54,243
inside of the Header Cart Button.
18
00:00:56,096 --> 00:00:58,610
And we simply can store this axis,
19
00:00:58,610 --> 00:01:02,820
this object, this context object in a cart Ctx constant
20
00:01:02,820 --> 00:01:04,720
but this name is up to you, of course.
21
00:01:05,580 --> 00:01:08,130
Now by using use context here,
22
00:01:08,130 --> 00:01:12,550
the header cart button component will be a re evaluated
23
00:01:12,550 --> 00:01:16,620
by react whenever the context changes.
24
00:01:16,620 --> 00:01:19,330
And it of course changes when we do update it
25
00:01:19,330 --> 00:01:21,640
in the cart provider a component.
26
00:01:21,640 --> 00:01:24,150
So now we established this connection
27
00:01:25,140 --> 00:01:30,140
and we can now use this to output our number of cart items.
28
00:01:31,840 --> 00:01:34,963
For this we can add another constant here.
29
00:01:35,960 --> 00:01:40,960
Number of cart items sounds like a descriptive name
30
00:01:41,740 --> 00:01:46,740
and here we want to get access to cart context dot items.
31
00:01:47,980 --> 00:01:50,890
And you could no think that you simply use the length
32
00:01:50,890 --> 00:01:54,433
of that array to get the number of cart items.
33
00:01:55,280 --> 00:02:00,250
This could work depending on how you write your cart logic
34
00:02:00,250 --> 00:02:03,720
but I don't want to add every item
35
00:02:03,720 --> 00:02:06,073
as a new entry in that list.
36
00:02:07,030 --> 00:02:10,860
Instead if we add, let's say three pieces of sushi
37
00:02:10,860 --> 00:02:15,180
I want to add one item to the cart items array
38
00:02:15,180 --> 00:02:18,183
but set the amount for that item to free.
39
00:02:19,160 --> 00:02:21,100
So then the length would be wrong
40
00:02:21,100 --> 00:02:24,440
because it's just one item and the cart items array
41
00:02:24,440 --> 00:02:27,750
but there are actually free meals inside of it
42
00:02:27,750 --> 00:02:30,320
simply because inside of our cart items
43
00:02:30,320 --> 00:02:32,560
array will have objects where
44
00:02:32,560 --> 00:02:36,080
for every kind of meal we stored
45
00:02:36,080 --> 00:02:39,753
the amount of meals of that type is part of the cart.
46
00:02:41,010 --> 00:02:44,300
So therefore the proper way to derive
47
00:02:44,300 --> 00:02:49,300
that number of items is to axis cart items.
48
00:02:50,490 --> 00:02:52,410
So cart contexts on items,
49
00:02:52,410 --> 00:02:55,860
but then use the reduced method on that
50
00:02:55,860 --> 00:02:58,480
reduces a built in method in the end
51
00:02:58,480 --> 00:03:01,600
that is a method which allows us to transform an array
52
00:03:01,600 --> 00:03:05,000
of data into a single value
53
00:03:05,000 --> 00:03:07,333
into a single number in this case.
54
00:03:09,020 --> 00:03:10,820
For data takes two arguments.
55
00:03:10,820 --> 00:03:13,810
The first argument is a function
56
00:03:13,810 --> 00:03:15,423
which will be called for you.
57
00:03:16,460 --> 00:03:19,960
The second argument is a starting value
58
00:03:19,960 --> 00:03:23,100
and you'll see why it's a starting value in a second.
59
00:03:23,100 --> 00:03:28,100
Here we use CRO, this function which has passed
60
00:03:28,180 --> 00:03:32,990
and as a first argument then receives two arguments itself
61
00:03:32,990 --> 00:03:34,900
automatically by JavaScript
62
00:03:34,900 --> 00:03:36,500
which is calling that function
63
00:03:36,500 --> 00:03:40,563
for every item in that array on what you're calling reduce.
64
00:03:41,700 --> 00:03:44,210
That's the current number.
65
00:03:44,210 --> 00:03:45,890
However you want to name it.
66
00:03:45,890 --> 00:03:48,890
And then the item at which it's currently having a look
67
00:03:49,750 --> 00:03:51,650
and current number will be different
68
00:03:51,650 --> 00:03:53,580
for every item it's calling it.
69
00:03:53,580 --> 00:03:54,920
Item will also be different,
70
00:03:54,920 --> 00:03:57,287
but this is basically a number of value
71
00:03:57,287 --> 00:04:00,970
which has carried on across executions.
72
00:04:00,970 --> 00:04:02,760
Initially it's CRO,
73
00:04:02,760 --> 00:04:06,410
but then after the first time disfunction has been executed
74
00:04:06,410 --> 00:04:09,650
it will be the result which you returned
75
00:04:09,650 --> 00:04:11,593
in that previous execution.
76
00:04:12,460 --> 00:04:15,730
And here I want to return current number
77
00:04:15,730 --> 00:04:20,730
plus item dot amount because my cart items objects
78
00:04:21,560 --> 00:04:23,540
will have an amount field
79
00:04:23,540 --> 00:04:27,593
which stores the number of items per item type.
80
00:04:28,710 --> 00:04:31,820
And this will now reduce this array to a single number.
81
00:04:31,820 --> 00:04:34,160
And it's this number of cart items
82
00:04:34,160 --> 00:04:36,103
which will be output down there.
83
00:04:38,340 --> 00:04:41,150
Now, if we saved as we see CRO here
84
00:04:41,150 --> 00:04:44,023
in our or on our cart button,
85
00:04:45,180 --> 00:04:46,720
therefore as a next step
86
00:04:46,720 --> 00:04:50,193
let's make sure we can actually add cart items.
6754
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.