Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,120 --> 00:00:04,410
Let's say we wanna utilize
2
00:00:04,410 --> 00:00:08,500
this provided store not in the app component
3
00:00:08,500 --> 00:00:12,100
but in the counter component which I rendered there.
4
00:00:12,100 --> 00:00:16,300
That's this counter component in the components folder.
5
00:00:16,300 --> 00:00:19,320
We got more components there we'll use them later
6
00:00:19,320 --> 00:00:21,400
for the moment it's the counter component
7
00:00:21,400 --> 00:00:23,390
that matters to us.
8
00:00:23,390 --> 00:00:25,890
Now that's the component you're seeing on the screen here
9
00:00:25,890 --> 00:00:28,150
when you're running your Def server.
10
00:00:28,150 --> 00:00:31,050
It shows us basically a container
11
00:00:31,050 --> 00:00:34,210
where we wanna output the counter value.
12
00:00:34,210 --> 00:00:37,300
We got a toggle counter button for hiding
13
00:00:37,300 --> 00:00:38,853
or showing the counter.
14
00:00:39,930 --> 00:00:44,540
Now here I wanna start by outputting the current counter.
15
00:00:44,540 --> 00:00:48,670
And for this we need to get access to our Redux store
16
00:00:48,670 --> 00:00:50,170
and to the data in there.
17
00:00:50,170 --> 00:00:55,170
And we again do that with help off the React Redux Library.
18
00:00:56,830 --> 00:01:00,360
So from React Redux we again wanna import something
19
00:01:00,360 --> 00:01:05,360
and the something which we are importing is a React Hook.
20
00:01:06,230 --> 00:01:10,330
A custom hook made by the React Redux team.
21
00:01:10,330 --> 00:01:13,333
The use selector hook.
22
00:01:14,210 --> 00:01:18,480
There also is use store hook, which we could use as well
23
00:01:18,480 --> 00:01:21,370
which gives us direct access to the store
24
00:01:21,370 --> 00:01:24,900
but you as selector is a bit more convenient to use
25
00:01:24,900 --> 00:01:29,550
because that allows us to then automatically select a part
26
00:01:29,550 --> 00:01:33,230
of our state managed by the store.
27
00:01:33,230 --> 00:01:35,683
So I will use use selector here.
28
00:01:36,620 --> 00:01:39,980
Now if we would be using a class-based component
29
00:01:39,980 --> 00:01:43,300
and not a functional component as we are here
30
00:01:43,300 --> 00:01:45,990
and as we are in the majority of the course,
31
00:01:45,990 --> 00:01:49,620
then there also is a connect function
32
00:01:49,620 --> 00:01:51,650
which we could use instead.
33
00:01:51,650 --> 00:01:55,210
This function can be used as a wrapper
34
00:01:55,210 --> 00:01:59,100
around our class component to connect that class component
35
00:01:59,100 --> 00:02:00,070
to the store.
36
00:02:00,070 --> 00:02:02,300
I'll come back to that later for the moment
37
00:02:02,300 --> 00:02:04,593
we'll focus on use selector.
38
00:02:05,710 --> 00:02:09,120
So here in this functional component we can now get access
39
00:02:09,120 --> 00:02:13,853
to the data managed in our store by using use selector.
40
00:02:15,290 --> 00:02:19,270
We call this and now we need to pass a function
41
00:02:19,270 --> 00:02:21,200
to use selector.
42
00:02:21,200 --> 00:02:25,340
A function which will be executed by a React Redux
43
00:02:25,340 --> 00:02:28,780
of function which then basically determines which piece
44
00:02:28,780 --> 00:02:31,593
of data we wanna extract from our store.
45
00:02:32,680 --> 00:02:35,480
Of course at the moment we have a very simple state.
46
00:02:35,480 --> 00:02:38,910
Just an object with a counter property.
47
00:02:38,910 --> 00:02:40,730
But in bigger applications,
48
00:02:40,730 --> 00:02:44,520
you will have more complex states with tons
49
00:02:44,520 --> 00:02:48,950
of different properties maybe nested objects and arrays
50
00:02:48,950 --> 00:02:52,270
and therefore being able to just get a slice
51
00:02:52,270 --> 00:02:55,670
just a tiny part of that overall state object
52
00:02:55,670 --> 00:02:58,250
in a easy way is worth a lot.
53
00:02:58,250 --> 00:03:00,760
And that's what use selector allows us to do.
54
00:03:00,760 --> 00:03:02,910
For this we should pass a function to it,
55
00:03:02,910 --> 00:03:05,990
a function of which we'll receive the state managed
56
00:03:05,990 --> 00:03:10,490
by Redux and then we return the part of the state
57
00:03:10,490 --> 00:03:11,800
which you wanna extract.
58
00:03:11,800 --> 00:03:14,053
So here for example, state stock.counter.
59
00:03:15,190 --> 00:03:19,750
Again this function will be executed for us by React Redux.
60
00:03:19,750 --> 00:03:21,940
It will then pass the Redux state
61
00:03:21,940 --> 00:03:24,660
so the manage the data into this function
62
00:03:24,660 --> 00:03:27,488
when it executes it and then basically executes
63
00:03:27,488 --> 00:03:30,730
this code to retrieve the part of the state
64
00:03:30,730 --> 00:03:32,890
which you need in this component.
65
00:03:32,890 --> 00:03:36,544
And then use select or therefore overall gives us
66
00:03:36,544 --> 00:03:38,270
that returned value.
67
00:03:38,270 --> 00:03:40,330
So we get a counter constant
68
00:03:40,330 --> 00:03:43,653
which is the counter managed by Redux.
69
00:03:44,540 --> 00:03:48,600
Now the great thing is that when you use use selector,
70
00:03:48,600 --> 00:03:52,510
React Redux will automatically set up a subscription
71
00:03:52,510 --> 00:03:56,330
to the Redux store for this component.
72
00:03:56,330 --> 00:03:58,690
So your component will be updated
73
00:03:58,690 --> 00:04:01,920
and will receive the latest counter automatically
74
00:04:01,920 --> 00:04:05,690
whenever that data changes in the Redux store.
75
00:04:05,690 --> 00:04:07,730
So it's an automatically reactive
76
00:04:07,730 --> 00:04:10,560
and changes to the Redux store will cause
77
00:04:10,560 --> 00:04:13,830
this component function to be re executed.
78
00:04:13,830 --> 00:04:16,279
So you always have the latest counter.
79
00:04:16,279 --> 00:04:19,480
That's why use selector is a very useful hook
80
00:04:19,480 --> 00:04:21,970
and why it is the hook we use for getting data
81
00:04:21,970 --> 00:04:22,973
out of the store.
82
00:04:24,070 --> 00:04:27,260
If you ever would unmount this component
83
00:04:27,260 --> 00:04:30,440
if it would be removed from the DOM for whatever reason,
84
00:04:30,440 --> 00:04:34,020
React Redux would also automatically clear
85
00:04:34,020 --> 00:04:35,880
the subscription for you.
86
00:04:35,880 --> 00:04:39,623
So it manages that subscription for you behind the scenes.
87
00:04:41,000 --> 00:04:44,220
Now that we got this counter, we can use it down there,
88
00:04:44,220 --> 00:04:48,673
to output the counter value like this.
89
00:04:49,650 --> 00:04:54,650
And if we now save this, we therefore now see zero here.
90
00:04:55,620 --> 00:05:00,490
And that's how we can get access to data managed by Redux.
91
00:05:00,490 --> 00:05:04,240
Of course the question now is how we can change that data?
92
00:05:04,240 --> 00:05:06,583
How can we dispatch actions?
7460
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.