Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,040 --> 00:00:04,870
So now that we know what Redux is,
2
00:00:04,870 --> 00:00:08,220
basically an alternative to react context,
3
00:00:08,220 --> 00:00:11,520
and now that we also know why we might wanna use it,
4
00:00:11,520 --> 00:00:14,640
over react context in some apps,
5
00:00:14,640 --> 00:00:16,670
the big question that remains,
6
00:00:16,670 --> 00:00:19,660
is how does Redux work.
7
00:00:19,660 --> 00:00:21,500
Well, Redux is all
8
00:00:21,500 --> 00:00:24,970
about having one Central Data Store,
9
00:00:24,970 --> 00:00:27,120
in your application.
10
00:00:27,120 --> 00:00:29,150
And with data I mean state.
11
00:00:29,150 --> 00:00:30,850
And that is super important.
12
00:00:30,850 --> 00:00:33,270
You have exactly one store.
13
00:00:33,270 --> 00:00:36,100
You never have more than one store.
14
00:00:36,100 --> 00:00:37,560
It's one store,
15
00:00:37,560 --> 00:00:41,470
for all your state for your entire application.
16
00:00:41,470 --> 00:00:43,210
So in this store,
17
00:00:43,210 --> 00:00:46,690
you would store authentication state theming,
18
00:00:46,690 --> 00:00:49,100
maybe some user input state you wanna save,
19
00:00:49,100 --> 00:00:50,410
whatever it is.
20
00:00:50,410 --> 00:00:53,930
Whichever cross component or app wide state you have,
21
00:00:53,930 --> 00:00:56,320
it goes into this one store.
22
00:00:56,320 --> 00:00:58,786
Now this might sound unmaintainable,
23
00:00:58,786 --> 00:01:00,350
but the good thing is
24
00:01:00,350 --> 00:01:03,020
that we don't directly need to manage,
25
00:01:03,020 --> 00:01:05,129
the entire store all the time.
26
00:01:05,129 --> 00:01:09,250
As you will see from this module and the next modules.
27
00:01:09,250 --> 00:01:11,810
So we have this Central Data Store.
28
00:01:11,810 --> 00:01:13,230
What do we do with it?
29
00:01:13,230 --> 00:01:16,100
Ultimately, we have data in that store,
30
00:01:16,100 --> 00:01:19,800
so that we can use it from inside our components.
31
00:01:19,800 --> 00:01:22,390
Because if some data there changes,
32
00:01:22,390 --> 00:01:25,450
if the authentication status of a user changes
33
00:01:25,450 --> 00:01:29,180
for example, we wanna know about that in a component,
34
00:01:29,180 --> 00:01:31,150
so that we can react accordingly,
35
00:01:31,150 --> 00:01:32,423
and update the UI.
36
00:01:33,320 --> 00:01:37,090
For this components set up subscriptions,
37
00:01:37,090 --> 00:01:39,610
to our Central Store.
38
00:01:39,610 --> 00:01:41,430
They subscribe to the store,
39
00:01:41,430 --> 00:01:43,270
and whenever the data changes,
40
00:01:43,270 --> 00:01:45,640
the store notifies components,
41
00:01:45,640 --> 00:01:49,070
and then components can get the data they need,
42
00:01:49,070 --> 00:01:52,200
for example the current authentication status,
43
00:01:52,200 --> 00:01:55,560
so they get a slice of the Redux store,
44
00:01:55,560 --> 00:01:57,430
and they can then use it.
45
00:01:57,430 --> 00:01:59,510
And of course we're going to see all of that,
46
00:01:59,510 --> 00:02:02,543
in action, in detail, in this course.
47
00:02:03,470 --> 00:02:06,150
So that's how we get data out of the store.
48
00:02:06,150 --> 00:02:06,983
But of course,
49
00:02:06,983 --> 00:02:08,630
since this is state,
50
00:02:08,630 --> 00:02:12,000
so data which also changes from time to time.
51
00:02:12,000 --> 00:02:14,900
We need a way of changing that data.
52
00:02:14,900 --> 00:02:17,970
So how do we change data into stored in?
53
00:02:17,970 --> 00:02:19,850
Here's one very important rule.
54
00:02:19,850 --> 00:02:23,050
Components, never important.
55
00:02:23,050 --> 00:02:27,870
Never, directly manipulate the store data.
56
00:02:27,870 --> 00:02:29,670
So we have that subscription,
57
00:02:29,670 --> 00:02:33,050
but we don't have a data flow in the other direction.
58
00:02:33,050 --> 00:02:35,860
At least not a direct data flow.
59
00:02:35,860 --> 00:02:40,053
Components don't directly manipulate that data in the store.
60
00:02:41,040 --> 00:02:44,970
Instead for debt, we use a concept called reducers.
61
00:02:44,970 --> 00:02:47,930
We have a reducer function.
62
00:02:47,930 --> 00:02:51,010
This reducer function which we have to set up.
63
00:02:51,010 --> 00:02:54,720
This function is responsible, for mutating.
64
00:02:54,720 --> 00:02:57,610
So for changing the store data.
65
00:02:57,610 --> 00:02:59,450
Now One important note here,
66
00:02:59,450 --> 00:03:01,170
the term reducer,
67
00:03:01,170 --> 00:03:03,890
is of course something we have heard before,
68
00:03:03,890 --> 00:03:08,073
with use reducer and the reducer function here is not
69
00:03:08,073 --> 00:03:11,340
that use reducer hook we learned about.
70
00:03:11,340 --> 00:03:16,340
Reducer functions in general, are just a general concept.
71
00:03:16,770 --> 00:03:20,870
Reducer functions are functions, which takes some input,
72
00:03:20,870 --> 00:03:23,300
and then transform that input,
73
00:03:23,300 --> 00:03:25,040
they reduce it,
74
00:03:25,040 --> 00:03:28,510
for example they can reduce a list of numbers to the sum
75
00:03:28,510 --> 00:03:31,330
of that number and that's just one example.
76
00:03:31,330 --> 00:03:34,570
But they in general transform, inputs
77
00:03:34,570 --> 00:03:37,750
and spit out a new output a new result.
78
00:03:37,750 --> 00:03:40,350
So that's a general programming concept,
79
00:03:40,350 --> 00:03:42,630
which use reducer, does hook uses
80
00:03:42,630 --> 00:03:46,993
and which this reducer function Redux will use also uses.
81
00:03:48,030 --> 00:03:49,870
Okay so we have a reducer function,
82
00:03:49,870 --> 00:03:53,740
which is responsible for updating the store data.
83
00:03:53,740 --> 00:03:57,300
And we have components that subscribe to that data.
84
00:03:57,300 --> 00:04:01,410
How do we now connect components and that reducer function?
85
00:04:01,410 --> 00:04:02,840
Because ultimately,
86
00:04:02,840 --> 00:04:05,300
it will of course be the components
87
00:04:05,300 --> 00:04:08,160
that should trigger a data change.
88
00:04:08,160 --> 00:04:11,230
It is for example a click on a button in a component
89
00:04:11,230 --> 00:04:14,050
that should trigger some data change.
90
00:04:14,050 --> 00:04:15,563
So how do we connect that.
91
00:04:16,399 --> 00:04:18,959
Trigger is a good word for that.
92
00:04:18,959 --> 00:04:21,370
Because we have a third concept here.
93
00:04:21,370 --> 00:04:26,370
We have actions and components dispatch actions.
94
00:04:26,400 --> 00:04:27,960
And therefore we could also say
95
00:04:27,960 --> 00:04:31,670
that components trigger, certain actions.
96
00:04:31,670 --> 00:04:35,970
Now an action is really just a simple JavaScript object,
97
00:04:35,970 --> 00:04:38,740
which describes the kind of operation,
98
00:04:38,740 --> 00:04:41,410
the reducers should perform.
99
00:04:41,410 --> 00:04:45,770
Therefore, Redux then forwards actions to the reducer,
100
00:04:45,770 --> 00:04:49,980
reads that description of the desired operation,
101
00:04:49,980 --> 00:04:51,400
if you wanna to call it like this,
102
00:04:51,400 --> 00:04:56,060
and then this operation is performed by the reducer.
103
00:04:56,060 --> 00:04:58,590
So components dispatch actions,
104
00:04:58,590 --> 00:05:00,770
which describe what should be done,
105
00:05:00,770 --> 00:05:02,770
but don't do it directly,
106
00:05:02,770 --> 00:05:05,940
then these actions are forwarded to the reducer,
107
00:05:05,940 --> 00:05:08,800
the reducer then does what the action wants,
108
00:05:08,800 --> 00:05:10,050
the reducer to do.
109
00:05:10,050 --> 00:05:13,970
And then the reducer, spits out a new state,
110
00:05:13,970 --> 00:05:17,980
which effectively will replace the existing state in
111
00:05:17,980 --> 00:05:19,970
that Central Data Store.
112
00:05:19,970 --> 00:05:21,560
And when that happens,
113
00:05:21,560 --> 00:05:24,560
when that state in that data store is updated,
114
00:05:24,560 --> 00:05:27,580
subscribing components are notified,
115
00:05:27,580 --> 00:05:29,723
so that they can update their UI.
116
00:05:31,340 --> 00:05:34,080
That's how Redux works.
117
00:05:34,080 --> 00:05:35,750
Now of course that's all nice.
118
00:05:35,750 --> 00:05:37,550
That's great theory.
119
00:05:37,550 --> 00:05:41,220
But how does that look like in reality then.
120
00:05:41,220 --> 00:05:44,450
How does the code we have to write look like?
121
00:05:44,450 --> 00:05:46,650
Well that's what we're going to explore now.
9172
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.