Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,230 --> 00:00:08,830
To mark a product as a favorite on back in the context folder, in the products context last fall and
2
00:00:08,830 --> 00:00:15,040
in there we manage our product list with use state right now in that same component, we can add a function
3
00:00:15,040 --> 00:00:17,710
stored in a constant, maybe named toggle favorite.
4
00:00:18,550 --> 00:00:19,320
It's a function.
5
00:00:19,330 --> 00:00:23,950
So I'll use the arrow function here where I expect to get the product ID.
6
00:00:23,960 --> 00:00:29,950
So the idea of the product for which I want to toggle the favorite status, the logic in here is pretty
7
00:00:29,950 --> 00:00:34,120
similar to what I did in my producer before year.
8
00:00:34,120 --> 00:00:43,240
I want to call set products list and I will use the function form of this hook to get my current product
9
00:00:43,240 --> 00:00:47,290
list and return a new one based on that current one.
10
00:00:47,290 --> 00:00:52,660
And I'm using that function form to be guaranteed that I'm working with the most recent state.
11
00:00:53,440 --> 00:00:55,300
So here I want to return a new list.
12
00:00:55,450 --> 00:00:57,740
That new list should be based on the old list, of course.
13
00:00:58,090 --> 00:01:02,500
Now, again, for dad, we can simply grab the logic from our products producer here.
14
00:01:03,430 --> 00:01:10,180
This logic here, solid copy all that logic, move it into the context folder, into this update function
15
00:01:10,180 --> 00:01:10,470
here.
16
00:01:10,480 --> 00:01:18,730
So in this nested arrow function and in here, states now simply state products now simply as the current
17
00:01:18,730 --> 00:01:21,070
product list action product.
18
00:01:21,070 --> 00:01:25,030
It doesn't exist, but we get the product here and everywhere.
19
00:01:25,150 --> 00:01:26,290
I have state products.
20
00:01:26,290 --> 00:01:33,790
I just use current prod list to get my product index, get the new favorite status, create an updated
21
00:01:33,790 --> 00:01:40,150
products list based on the old list, and then swap the one item we're updating by copying all the properties
22
00:01:40,150 --> 00:01:42,460
of the old item and replacing the favorite status.
23
00:01:42,640 --> 00:01:45,160
And then here I return my updated products.
24
00:01:45,460 --> 00:01:50,230
So now we have to toggle favorite method, which we can call to toggled of favorite status of a single
25
00:01:50,230 --> 00:01:50,770
product.
26
00:01:51,890 --> 00:01:58,280
Now, in my context, I'm trading up there besides the products in this object, and that's why I'm
27
00:01:58,280 --> 00:02:04,490
creating an object, I'll not only have that list of products, I'll also have that toggle FEV function,
28
00:02:04,490 --> 00:02:06,140
which here is an empty function.
29
00:02:06,260 --> 00:02:12,320
And I'm just adding it here to this initial value so that we get better I.T. support and auto completion.
30
00:02:12,740 --> 00:02:18,710
But I will also add toggle Faf to the object we're passing here as a value for products context provider
31
00:02:19,250 --> 00:02:19,830
there.
32
00:02:19,850 --> 00:02:24,650
I can now also add toggle faf and set does equal equal to toggle favorite.
33
00:02:25,490 --> 00:02:28,290
Toggle favorite of course is that function we just created.
34
00:02:28,850 --> 00:02:35,120
So now this function, a pointer at this function is also stored in our context value and now they are
35
00:02:35,120 --> 00:02:41,030
for everywhere where we tap into our context, we can call this function and then dysfunction will change.
36
00:02:41,030 --> 00:02:49,160
Something in Deadfall will change our state and Dhafer emit a new state, a new value to all components
37
00:02:49,160 --> 00:02:50,100
that are listening.
38
00:02:50,630 --> 00:02:56,390
So now let's go back to the product item, jass file into components folder.
39
00:02:56,870 --> 00:03:00,080
And in there, let's now also import.
40
00:03:01,810 --> 00:03:04,630
Products context from.
41
00:03:07,380 --> 00:03:12,350
The context folder, of course, and they are from products context, get rid of that actually import,
42
00:03:12,360 --> 00:03:19,560
which we don't need anymore, and with products context imported here, we can also import U.S. context
43
00:03:19,620 --> 00:03:29,490
is hooked from react and then here simply get access to our products context or to the toggle thatthe.
44
00:03:31,240 --> 00:03:39,220
Function by using U.S. context, products, context, dot toggle faf, and that's the pointer at this
45
00:03:39,220 --> 00:03:39,680
function.
46
00:03:39,940 --> 00:03:46,450
And now here in the toggle FAF handler, instead of dispatching this, I can just call toggle Faf and
47
00:03:46,450 --> 00:03:53,560
forwarded now to get the best IDE completion, I'll go back to the product's context file and here dysthymia
48
00:03:53,560 --> 00:03:54,760
function on the finding up here.
49
00:03:54,910 --> 00:04:01,150
I'll actually add the ideas a parameter so that my idy knows that an ID should be passed here when I
50
00:04:01,150 --> 00:04:01,660
call it.
51
00:04:02,470 --> 00:04:06,040
And the idea when I pass here of course is proper ID.
52
00:04:06,040 --> 00:04:12,850
So the idea of the item we're rendering here now, we should actually be able to mark something as a
53
00:04:12,850 --> 00:04:15,340
favorite snuffly safe distance we go back.
54
00:04:16,390 --> 00:04:17,959
Favorite Ding works again.
55
00:04:18,610 --> 00:04:23,560
Now, let's also make your favorites page work again, because right now that is broken by going to
56
00:04:23,560 --> 00:04:25,600
favorites Jestina Container's folder.
57
00:04:25,810 --> 00:04:29,410
And there, let's get rid of the use selecter thing here instead.
58
00:04:29,410 --> 00:04:33,940
Import the products context here from.
59
00:04:34,940 --> 00:04:42,320
Context, products, context, that that file here and then here, our favorite products can be selected
60
00:04:42,320 --> 00:04:44,850
with the help of the U.S. context.
61
00:04:45,050 --> 00:04:49,520
Again, by using U.S. context here and by then.
62
00:04:52,110 --> 00:04:54,060
Selecting products context.
63
00:04:54,920 --> 00:05:02,270
DOT products, and then here we can call, filter and filter every four product by checking if it is
64
00:05:02,270 --> 00:05:07,790
a favorite and only if Despard turns true, it will be included in the newly returned array, which
65
00:05:07,790 --> 00:05:09,230
is stored in favorite products.
66
00:05:09,980 --> 00:05:11,780
So now this page should work again.
67
00:05:12,080 --> 00:05:15,650
Got no favorites initially, but now if I add, let's say these two favorites.
68
00:05:16,800 --> 00:05:22,770
We see them here, so now we're managing our state with the context API and on first look, that looks
69
00:05:22,770 --> 00:05:23,250
amazing.
70
00:05:23,250 --> 00:05:26,420
We're not using Redux and it argued wasn't too hard.
71
00:05:26,610 --> 00:05:31,530
We basically now have our story here in the context folder, in the product context to a file here.
72
00:05:31,890 --> 00:05:38,100
And we're using react only features to pass that data around and to change it from anywhere and to read
73
00:05:38,100 --> 00:05:39,060
it from anywhere.
74
00:05:39,300 --> 00:05:41,160
Now, what's the downside of this approach?
7742
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.