Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,070 --> 00:00:04,600
So now we already learned quite a bit
2
00:00:04,600 --> 00:00:06,910
about Redux but up to this point,
3
00:00:06,910 --> 00:00:10,960
we always just worked with our single counter value.
4
00:00:10,960 --> 00:00:13,600
Now I got this Toggle Counter button here
5
00:00:13,600 --> 00:00:14,710
and when it's clicked,
6
00:00:14,710 --> 00:00:17,090
I wanna make sure that the counter is hidden
7
00:00:17,090 --> 00:00:19,710
and when it's clicked again, it should be shown again.
8
00:00:19,710 --> 00:00:23,253
Now for this, of course, we could use useState.
9
00:00:24,113 --> 00:00:27,420
So we could set up some local state in this component
10
00:00:27,420 --> 00:00:31,400
which we manage with useState, not with Redux.
11
00:00:31,400 --> 00:00:34,090
And that would be the proper way of doing it
12
00:00:34,090 --> 00:00:37,180
because showing or hiding the counter
13
00:00:37,180 --> 00:00:40,900
is something which only is interesting to this component,
14
00:00:40,900 --> 00:00:44,080
not to any other part of the application
15
00:00:44,080 --> 00:00:46,990
but the same could be set about our counter.
16
00:00:46,990 --> 00:00:50,410
We are only using the counter in this component here.
17
00:00:50,410 --> 00:00:53,660
So the counter technically also is local state
18
00:00:54,560 --> 00:00:57,400
but this is just a simple demo to get started.
19
00:00:57,400 --> 00:01:01,540
So let's assume that both the counter as well as the state
20
00:01:01,540 --> 00:01:03,930
wherever the counter should be visible or not,
21
00:01:03,930 --> 00:01:06,940
is a global state which is also the interesting
22
00:01:06,940 --> 00:01:10,540
to other components even though that's not the case here.
23
00:01:10,540 --> 00:01:13,200
As we progress once we know these basics,
24
00:01:13,200 --> 00:01:16,530
we will, of course, also work on demos and projects
25
00:01:16,530 --> 00:01:19,270
where this then really is the case.
26
00:01:19,270 --> 00:01:21,820
But for the moment, let's assume it.
27
00:01:21,820 --> 00:01:23,560
So when we click this button,
28
00:01:23,560 --> 00:01:26,520
the toggleCounterHandler is fired.
29
00:01:26,520 --> 00:01:29,900
And then here we wanna dispatch an action
30
00:01:29,900 --> 00:01:32,720
which changes some state in Redux
31
00:01:32,720 --> 00:01:36,263
which controls whether this counter div is shown or not.
32
00:01:37,910 --> 00:01:41,080
For this, we need to add a new state
33
00:01:41,080 --> 00:01:45,400
a new piece of data to our Redux store.
34
00:01:45,400 --> 00:01:48,310
And how do we now do that?
35
00:01:48,310 --> 00:01:50,750
Well, to add a new piece of data,
36
00:01:50,750 --> 00:01:54,910
we need to go to our reducer in the end and just add it
37
00:01:54,910 --> 00:01:59,910
to all these state snapshots which we are producing.
38
00:02:00,050 --> 00:02:02,750
Let's start with the initial state snapshot.
39
00:02:02,750 --> 00:02:05,370
When we start besides having a counter
40
00:02:05,370 --> 00:02:09,500
which has a value of zero, I wanna have a showCounter field
41
00:02:09,500 --> 00:02:14,230
which has a value of true or false, that's up to you.
42
00:02:14,230 --> 00:02:15,980
And to keep this a bit more readable,
43
00:02:15,980 --> 00:02:18,970
I'll extract that and store that in a constant
44
00:02:18,970 --> 00:02:22,520
named initialState like this
45
00:02:22,520 --> 00:02:25,080
and assign initialState here then
46
00:02:25,080 --> 00:02:27,223
just to make this a bit easier to read.
47
00:02:28,370 --> 00:02:32,790
Now when we increment, we are changing the counter,
48
00:02:32,790 --> 00:02:35,490
we don't care about showCounter.
49
00:02:35,490 --> 00:02:40,490
We still need to set the showCounter property here though
50
00:02:40,550 --> 00:02:44,490
because we are returning the overall state object
51
00:02:44,490 --> 00:02:48,720
and Redux won't merge your changes with the existing state.
52
00:02:48,720 --> 00:02:50,990
It instead takes what you return
53
00:02:50,990 --> 00:02:54,360
and replaces the existing state with it.
54
00:02:54,360 --> 00:02:56,350
More on that in a second.
55
00:02:56,350 --> 00:02:58,540
But for the moment that's just accept it
56
00:02:58,540 --> 00:03:03,080
and set showCounter here to state.showCounter.
57
00:03:03,080 --> 00:03:05,970
So to the value we currently have in our state
58
00:03:05,970 --> 00:03:08,480
because for increment, we don't wanna change it,
59
00:03:08,480 --> 00:03:12,263
so we will just take the existing showCounter value.
60
00:03:13,670 --> 00:03:16,510
And then we can do the same here for increase
61
00:03:16,510 --> 00:03:18,330
because there we also wanna keep
62
00:03:18,330 --> 00:03:21,230
the existing showCounter value
63
00:03:21,230 --> 00:03:24,173
and do the same for decrement here.
64
00:03:25,780 --> 00:03:29,650
But now I will also handle a new action type
65
00:03:29,650 --> 00:03:32,700
and by the way, since we have many if statements here,
66
00:03:32,700 --> 00:03:35,670
we could also use a switch case statement,
67
00:03:35,670 --> 00:03:38,900
but I will stick to multiple if statements here for now
68
00:03:38,900 --> 00:03:41,310
and in this new action type,
69
00:03:41,310 --> 00:03:44,700
I will check for, let's say toggle.
70
00:03:44,700 --> 00:03:47,700
Now the identifier just like all these identifiers
71
00:03:47,700 --> 00:03:48,950
is up to you.
72
00:03:48,950 --> 00:03:53,180
I'll go with toggle and then return a new object.
73
00:03:53,180 --> 00:03:56,540
Here, we now wanna change showCounter
74
00:03:56,540 --> 00:04:00,100
and set it to the opposite of what it was before.
75
00:04:00,100 --> 00:04:03,040
If it was true, we wanna set it to false,
76
00:04:03,040 --> 00:04:05,960
if it was false, we wanna set it to true.
77
00:04:05,960 --> 00:04:09,080
And we can do this by simply adding an exclamation mark
78
00:04:09,080 --> 00:04:11,900
and then accessing state.showCounter.
79
00:04:11,900 --> 00:04:14,023
This will invert the value.
80
00:04:14,890 --> 00:04:18,010
Now for the counter itself, we wanna keep the existing state
81
00:04:18,010 --> 00:04:21,010
because we don't wanna change this here for this action.
82
00:04:21,010 --> 00:04:23,593
So we just set counter to state.counter.
83
00:04:25,660 --> 00:04:29,520
Now we also support this new toggle action type
84
00:04:29,520 --> 00:04:34,290
and hence back in counter.js in the toggleCounterHandler,
85
00:04:34,290 --> 00:04:36,920
we should dispatch such an action.
86
00:04:36,920 --> 00:04:41,193
We dispatch a new action object where the type is toggle.
87
00:04:43,840 --> 00:04:47,910
Now we can take advantage of this toggle state,
88
00:04:47,910 --> 00:04:50,360
so of the show counter.state,
89
00:04:50,360 --> 00:04:52,630
and for that we need to get access to it.
90
00:04:52,630 --> 00:04:57,300
So all to in the counter, we again call useSelector.
91
00:04:57,300 --> 00:05:00,910
We can use this multiple times to retrieve different pieces
92
00:05:00,910 --> 00:05:02,940
of data from the state.
93
00:05:02,940 --> 00:05:04,810
And here I'm then interested
94
00:05:04,810 --> 00:05:08,173
in my showCounter piece of data,
95
00:05:09,170 --> 00:05:12,570
and I'll store that in a show constant.
96
00:05:12,570 --> 00:05:14,620
The constant name of course is up to you.
97
00:05:15,750 --> 00:05:18,190
Now again, that will then always update
98
00:05:18,190 --> 00:05:20,710
and the component will be re-evaluated
99
00:05:20,710 --> 00:05:24,400
whenever that data which we're accessing here changes.
100
00:05:24,400 --> 00:05:27,760
So now here with show extracted,
101
00:05:27,760 --> 00:05:31,900
we now can render this div here conditionally
102
00:05:31,900 --> 00:05:35,130
by checking if show and only rendering the div
103
00:05:35,130 --> 00:05:38,980
if show is truthy, like this.
104
00:05:38,980 --> 00:05:41,270
If we now save this and reload,
105
00:05:41,270 --> 00:05:43,340
if we click Toggle Counter, it's gone,
106
00:05:43,340 --> 00:05:45,730
if I click this again, it's there again.
107
00:05:45,730 --> 00:05:48,520
I can still increase it even if it's hidden
108
00:05:48,520 --> 00:05:50,320
but it only shows up when,
109
00:05:50,320 --> 00:05:52,690
well, when I click Toggle Counter.
110
00:05:52,690 --> 00:05:56,200
So that now also works and that is how we can manage
111
00:05:56,200 --> 00:06:01,030
multiple different pieces of data in our state.
112
00:06:01,030 --> 00:06:04,370
Of course, this data is still kind of connected,
113
00:06:04,370 --> 00:06:05,853
we have the counter and then the state
114
00:06:05,853 --> 00:06:08,060
whether we wanna show it or not
115
00:06:08,060 --> 00:06:10,520
but it's too totally different values
116
00:06:10,520 --> 00:06:13,470
which are changed in totally different ways.
117
00:06:13,470 --> 00:06:15,570
So therefore, that is how we can manage
118
00:06:15,570 --> 00:06:17,463
multiple pieces of data.
9563
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.