Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,210 --> 00:00:04,930
Now for exploring the Redux basics,
2
00:00:04,930 --> 00:00:06,710
I have a brand new project
3
00:00:06,710 --> 00:00:10,430
and this isn't even a React project.
4
00:00:10,430 --> 00:00:12,220
It's an empty folder in the end,
5
00:00:12,220 --> 00:00:15,210
just have some settings for my IDE here.
6
00:00:15,210 --> 00:00:16,290
Now you can therefore also
7
00:00:16,290 --> 00:00:18,880
just create a brand new empty folder.
8
00:00:18,880 --> 00:00:21,850
We don't need a React App yet,
9
00:00:21,850 --> 00:00:24,870
of course, we will add one later, but for the moment,
10
00:00:24,870 --> 00:00:27,050
an empty folder we'll do.
11
00:00:27,050 --> 00:00:32,049
And in that folder, you can create a new JavaScript file.
12
00:00:32,049 --> 00:00:33,650
You can give it any name you want,
13
00:00:33,650 --> 00:00:37,093
I will name it Redux-demo.JS.
14
00:00:37,930 --> 00:00:41,780
Now we will execute this file with node JS
15
00:00:41,780 --> 00:00:45,130
because node JS allows us to run JavaScript
16
00:00:45,130 --> 00:00:47,570
outside of the browser.
17
00:00:47,570 --> 00:00:50,740
And of course, we already have node JS installed
18
00:00:50,740 --> 00:00:53,290
because we needed that for creating
19
00:00:53,290 --> 00:00:57,660
our react apps for installing our third party packages
20
00:00:57,660 --> 00:01:01,610
with NPM and also for running our development server.
21
00:01:01,610 --> 00:01:05,430
So we already do have node JS installed.
22
00:01:05,430 --> 00:01:10,430
And now here, in this empty folder, I will run NPM in it
23
00:01:11,570 --> 00:01:13,210
and you should do this too.
24
00:01:13,210 --> 00:01:15,640
Open a terminal or a command prompt,
25
00:01:15,640 --> 00:01:17,830
navigate into this empty folder
26
00:01:17,830 --> 00:01:22,293
and run NPM in it to be precise, run NPM in it -y,
27
00:01:23,340 --> 00:01:27,200
which simply answers all these default questions with yes,
28
00:01:27,200 --> 00:01:29,410
if you don't run it with -y,
29
00:01:29,410 --> 00:01:31,963
just answer them manually with yes.
30
00:01:32,870 --> 00:01:35,300
Now this gives us a package.json file,
31
00:01:35,300 --> 00:01:37,460
which is not too interesting for us,
32
00:01:37,460 --> 00:01:41,010
but we need it to install third-party packages.
33
00:01:41,010 --> 00:01:43,963
Because now we wanna install Redux.
34
00:01:44,820 --> 00:01:49,400
For this we can now run NPM install Redux.
35
00:01:49,400 --> 00:01:52,510
That's the name of that package and when we run this,
36
00:01:52,510 --> 00:01:55,160
this package gets installed into this
37
00:01:55,160 --> 00:01:57,720
otherwise empty project folder.
38
00:01:57,720 --> 00:02:00,160
We therefore node modules folder,
39
00:02:00,160 --> 00:02:04,510
which now contains Redux and all the dependencies of Redux.
40
00:02:04,510 --> 00:02:07,180
Now we're ready to use Redux here.
41
00:02:07,180 --> 00:02:11,520
So in the Redux-demo.js file, we can now use it.
42
00:02:11,520 --> 00:02:15,060
Now for that we need to import Redux.
43
00:02:15,060 --> 00:02:19,050
And since we were going to execute this file with node JS,
44
00:02:19,050 --> 00:02:21,720
the import will look a little bit different
45
00:02:21,720 --> 00:02:24,190
than what we're used to.
46
00:02:24,190 --> 00:02:27,790
We import Redux like this.
47
00:02:27,790 --> 00:02:29,200
This might look strange,
48
00:02:29,200 --> 00:02:32,820
but that is the default node JS import syntax
49
00:02:32,820 --> 00:02:35,910
for importing a third party package.
50
00:02:35,910 --> 00:02:40,910
And we import Redux from the Redux package with this syntax.
51
00:02:41,330 --> 00:02:44,220
Now we need to do a couple of things.
52
00:02:44,220 --> 00:02:49,220
If you recall that slide, we will need to create that store,
53
00:02:50,290 --> 00:02:53,620
we will need to create a reducer function
54
00:02:53,620 --> 00:02:57,900
which changes that store, we will then also need an action
55
00:02:57,900 --> 00:03:02,780
and a component or since we're not using a React app yet,
56
00:03:02,780 --> 00:03:07,410
any other code that sets up a subscription to the store.
57
00:03:07,410 --> 00:03:10,570
So I think it makes sense to start with the store
58
00:03:10,570 --> 00:03:14,783
since that is the central piece and concept of Redux.
59
00:03:15,840 --> 00:03:17,920
Now we can create such a store
60
00:03:17,920 --> 00:03:20,920
by using that imported Redux object.
61
00:03:20,920 --> 00:03:24,590
And then on that object, we can call, createStore.
62
00:03:24,590 --> 00:03:27,930
That's a method exposed by the Redux library,
63
00:03:27,930 --> 00:03:31,533
which does what the name implies, it creates a store.
64
00:03:32,680 --> 00:03:35,820
Now let's store that store in a constant,
65
00:03:35,820 --> 00:03:38,950
which you of course don't have to name store,
66
00:03:38,950 --> 00:03:40,920
but I think it's a name that makes sense,
67
00:03:40,920 --> 00:03:43,620
given that we create a store here.
68
00:03:43,620 --> 00:03:46,650
And then the question is, how do we proceed?
69
00:03:46,650 --> 00:03:49,000
What do we do with that store?
70
00:03:49,000 --> 00:03:52,240
Well, that store should manage some data
71
00:03:52,240 --> 00:03:55,530
and the data which it manages is in the end
72
00:03:55,530 --> 00:03:58,040
determined by the reducer function,
73
00:03:58,040 --> 00:04:01,640
because it's the reducer function which will produce
74
00:04:01,640 --> 00:04:03,880
new state snapshots.
75
00:04:03,880 --> 00:04:08,880
The reducer has to go of spitting out a new state snapshot
76
00:04:09,060 --> 00:04:11,590
whenever an action reaches it.
77
00:04:11,590 --> 00:04:14,610
And when we run this code for the first time,
78
00:04:14,610 --> 00:04:18,740
the reducer will also be executed with a default action,
79
00:04:18,740 --> 00:04:22,390
so to say, that should spit out the initial state.
80
00:04:22,390 --> 00:04:24,600
So therefore the next thing we need to add
81
00:04:24,600 --> 00:04:26,860
is a reducer function.
82
00:04:26,860 --> 00:04:30,540
And I will name this counter reducer,
83
00:04:30,540 --> 00:04:33,933
but the name is up to you, and that stores a function.
84
00:04:34,970 --> 00:04:38,320
Here I'm using this anonymous arrow function syntax.
85
00:04:38,320 --> 00:04:43,100
Of course you could also create that function like this.
86
00:04:43,100 --> 00:04:44,780
That would also be fine.
87
00:04:44,780 --> 00:04:47,000
But I'll stick to the Arbor syntax.
88
00:04:47,000 --> 00:04:50,250
And that's now our reducer function.
89
00:04:50,250 --> 00:04:52,250
It's not finished though.
90
00:04:52,250 --> 00:04:56,860
A reducer function is a standard JavaScript function,
91
00:04:56,860 --> 00:05:01,010
but it will it be called by the Redux library
92
00:05:01,010 --> 00:05:04,700
and it will then always receive two pieces of input,
93
00:05:04,700 --> 00:05:09,050
two parameters, the old or existing state
94
00:05:09,050 --> 00:05:12,160
and the action that was dispatched.
95
00:05:12,160 --> 00:05:17,120
And then this reducer function must return a certain output.
96
00:05:17,120 --> 00:05:22,050
It must always return a new state object.
97
00:05:22,050 --> 00:05:26,140
And therefore a reducer function should be a pure function.
98
00:05:26,140 --> 00:05:28,490
Which basically means that the same inputs,
99
00:05:28,490 --> 00:05:32,660
the same values for inputs always should produce
100
00:05:32,660 --> 00:05:34,810
exactly the same output.
101
00:05:34,810 --> 00:05:39,230
And there should be no side effects inside of that function.
102
00:05:39,230 --> 00:05:42,760
So you must not send a HTTP request
103
00:05:42,760 --> 00:05:45,490
or write something to local storage
104
00:05:45,490 --> 00:05:48,750
or fetch something from local storage there.
105
00:05:48,750 --> 00:05:52,240
Instead, a reducer should really just be a function
106
00:05:52,240 --> 00:05:56,260
that takes the given inputs, which are provided by Redux
107
00:05:56,260 --> 00:06:01,023
and then produces the expected output, a new state object.
108
00:06:02,240 --> 00:06:04,040
So therefore back in code here,
109
00:06:04,040 --> 00:06:07,760
this reducer function will receive the state,
110
00:06:07,760 --> 00:06:11,160
the current state and the action.
111
00:06:11,160 --> 00:06:13,260
It receives that by default
112
00:06:13,260 --> 00:06:16,620
because the reducer will ultimately be executed
113
00:06:16,620 --> 00:06:18,960
by the Redux library.
114
00:06:18,960 --> 00:06:22,500
And then we should return the new state,
115
00:06:22,500 --> 00:06:25,780
which will replace the existing state.
116
00:06:25,780 --> 00:06:26,980
Now here on the slide,
117
00:06:26,980 --> 00:06:30,410
I said that the output is a state object,
118
00:06:30,410 --> 00:06:33,780
it typically will be because in most applications,
119
00:06:33,780 --> 00:06:37,070
state is more than just one single of value
120
00:06:37,070 --> 00:06:41,230
but theoretically, it can be any kind of value type.
121
00:06:41,230 --> 00:06:44,440
State can also just be a number or a string.
122
00:06:44,440 --> 00:06:48,420
But in reality, it will most often be an object.
123
00:06:48,420 --> 00:06:51,640
So here we then wanna return, let's say a new object
124
00:06:52,520 --> 00:06:56,860
and then this object can have any structure we want.
125
00:06:56,860 --> 00:07:01,010
This is totally up to us, the developer of this page.
126
00:07:01,010 --> 00:07:04,210
So here we could have a counter field in this object,
127
00:07:04,210 --> 00:07:07,290
which is zero for example.
128
00:07:07,290 --> 00:07:10,300
Now this word user would always return an object
129
00:07:10,300 --> 00:07:12,790
with a counter set to zero.
130
00:07:12,790 --> 00:07:15,560
Now that might not be too realistic though.
131
00:07:15,560 --> 00:07:18,180
Instead, the counter should probably be
132
00:07:18,180 --> 00:07:20,720
the existing counter and then let's say
133
00:07:20,720 --> 00:07:23,380
the existing counter plus one.
134
00:07:23,380 --> 00:07:28,080
So state.counter + 1, referring to the existing state,
135
00:07:28,080 --> 00:07:32,160
which we get automatically accessing the old counter value,
136
00:07:32,160 --> 00:07:35,700
which was stored in a state and adding one to it
137
00:07:35,700 --> 00:07:40,160
for the new counter value of the newly returned state.
138
00:07:40,160 --> 00:07:44,010
That's now how a basic reducer could look like.
139
00:07:44,010 --> 00:07:46,670
Now it's this counter reducer,
140
00:07:46,670 --> 00:07:50,000
which we passed to the create store function.
141
00:07:50,000 --> 00:07:53,460
Because the store needs to know which reducer
142
00:07:53,460 --> 00:07:56,950
is responsible for changing that store.
143
00:07:56,950 --> 00:07:59,190
Because keep in mind, it's the reducer
144
00:07:59,190 --> 00:08:01,470
that works together with the store.
145
00:08:01,470 --> 00:08:05,350
So the store wants to know who the reducer function is
146
00:08:05,350 --> 00:08:07,670
that will manipulate the data.
147
00:08:07,670 --> 00:08:11,910
So we now got our reducer function and we got our store.
148
00:08:11,910 --> 00:08:15,490
Now we also need someone who subscribes to that store,
149
00:08:15,490 --> 00:08:19,270
and then we need an action that can be dispatched.
150
00:08:19,270 --> 00:08:22,180
Now let's start with the subscription.
151
00:08:22,180 --> 00:08:24,330
For this, I'll add a new constant here
152
00:08:24,330 --> 00:08:26,990
and I'll name it, counter subscriber,
153
00:08:26,990 --> 00:08:28,920
and this will hold a function.
154
00:08:28,920 --> 00:08:31,210
And again, you could, of course, overwrite this
155
00:08:31,210 --> 00:08:35,820
with the function keyword, if you preferred that.
156
00:08:35,820 --> 00:08:38,549
So does subscriber is a function here,
157
00:08:38,549 --> 00:08:41,559
which does not get any parameters,
158
00:08:41,559 --> 00:08:44,240
but where we then inside of the function
159
00:08:44,240 --> 00:08:48,690
can reach out to the store and call, getS state.
160
00:08:48,690 --> 00:08:53,280
Get state is a method which is available on the store
161
00:08:53,280 --> 00:08:55,430
created with create store.
162
00:08:55,430 --> 00:08:58,591
And it will give us the latest state snapshot
163
00:08:58,591 --> 00:09:00,690
after it was updated.
164
00:09:00,690 --> 00:09:04,270
So this subscription function will soon be triggered
165
00:09:04,270 --> 00:09:06,090
whenever the state changes.
166
00:09:06,090 --> 00:09:07,610
And then when it is triggered,
167
00:09:07,610 --> 00:09:11,170
we can get to that latest state after it was changed
168
00:09:11,170 --> 00:09:13,843
with the get state method here.
169
00:09:14,680 --> 00:09:17,490
So then we have that latest state here
170
00:09:17,490 --> 00:09:20,230
and we can just console log here,
171
00:09:20,230 --> 00:09:21,793
console log that state here.
172
00:09:23,070 --> 00:09:24,970
Now we just need to make Redux
173
00:09:24,970 --> 00:09:27,700
aware of this subscriber function
174
00:09:27,700 --> 00:09:31,070
and tell it that disfunction should be executed
175
00:09:31,070 --> 00:09:33,110
whenever our state changes.
176
00:09:33,110 --> 00:09:36,050
And we do that by reaching out to the store
177
00:09:36,050 --> 00:09:38,871
and calling the subscribe method on the store,
178
00:09:38,871 --> 00:09:41,043
another method which exists there.
179
00:09:42,120 --> 00:09:47,040
The subscribe method then wants such a subscriber function.
180
00:09:47,040 --> 00:09:49,860
So to subscribe method expects a function
181
00:09:49,860 --> 00:09:52,700
which Redux will then execute for us
182
00:09:52,700 --> 00:09:55,680
whenever the data and the store changed.
183
00:09:55,680 --> 00:10:00,350
So here we now pass the counter subscriber to subscribe.
184
00:10:00,350 --> 00:10:04,380
Important, we don't execute counter subscriber here,
185
00:10:04,380 --> 00:10:08,290
we just point at it just as we did not execute
186
00:10:08,290 --> 00:10:11,840
counter reducer here, but just point at it.
187
00:10:11,840 --> 00:10:16,360
Because both the reducer, as well as the subscriber function
188
00:10:16,360 --> 00:10:19,393
will be executed by Redux.
189
00:10:20,800 --> 00:10:24,470
Now we've got a subscriber and we've got our reducer,
190
00:10:24,470 --> 00:10:26,960
what we don't have is an action.
191
00:10:26,960 --> 00:10:29,600
Nonetheless, let's save that file
192
00:10:29,600 --> 00:10:33,010
and then open up the terminal or a command prompt,
193
00:10:33,010 --> 00:10:35,310
here I'm using the integrated one.
194
00:10:35,310 --> 00:10:40,310
And then you can run node and then your file name,
195
00:10:41,100 --> 00:10:44,633
in my case, redux-demo.JS.
196
00:10:45,510 --> 00:10:47,510
And this will execute this file
197
00:10:47,510 --> 00:10:51,053
and the code inside of the file with node JS.
198
00:10:52,030 --> 00:10:54,560
And if you do that, you should see an error.
199
00:10:54,560 --> 00:10:55,490
You will get an error
200
00:10:55,490 --> 00:10:59,120
that you cannot read property counter of undefined.
201
00:10:59,120 --> 00:11:02,913
Now this is an expected error and why are we getting it?
202
00:11:03,830 --> 00:11:05,860
Well, we are creating a story here
203
00:11:05,860 --> 00:11:09,290
and that store has a counter reducer as it needs to,
204
00:11:09,290 --> 00:11:14,180
and in that reducer, we return a state object,
205
00:11:14,180 --> 00:11:17,610
which sets the counter to the old state counter plus one.
206
00:11:17,610 --> 00:11:20,703
The problem is that when that store is initialized,
207
00:11:21,630 --> 00:11:25,660
Redux will execute this reducer for the first time.
208
00:11:25,660 --> 00:11:28,970
So this code will then run when the store is created.
209
00:11:28,970 --> 00:11:32,140
The problem is that at this point of time,
210
00:11:32,140 --> 00:11:33,950
state is undefined.
211
00:11:33,950 --> 00:11:37,730
We have no existing state because it's the first time
212
00:11:37,730 --> 00:11:38,873
this is executing.
213
00:11:39,760 --> 00:11:43,770
That's why we should give state this state parameter,
214
00:11:43,770 --> 00:11:47,010
a default value, which is assumed
215
00:11:47,010 --> 00:11:49,560
if it would otherwise be undefined.
216
00:11:49,560 --> 00:11:54,320
So that for the first time this runs, it does have a value.
217
00:11:54,320 --> 00:11:57,980
If it then runs thereafter and we do have an existing state,
218
00:11:57,980 --> 00:12:00,640
the default value will not be used,
219
00:12:00,640 --> 00:12:02,700
but here for the first time,
220
00:12:02,700 --> 00:12:06,823
we wanna have a fallback default value which will be used.
221
00:12:07,720 --> 00:12:10,050
And here I'll set my default value
222
00:12:10,050 --> 00:12:13,120
to an object which has a counter property,
223
00:12:13,120 --> 00:12:15,453
which is zero initially.
224
00:12:16,320 --> 00:12:19,240
With that change made, if we saved that
225
00:12:19,240 --> 00:12:24,240
and we now try this again, we again run node Redux-demo.JS,
226
00:12:26,440 --> 00:12:28,840
now this works.
227
00:12:28,840 --> 00:12:31,810
But we don't see anything in the terminal,
228
00:12:31,810 --> 00:12:33,740
we don't see any output.
229
00:12:33,740 --> 00:12:37,620
The reason for this is that we do have a subscription here,
230
00:12:37,620 --> 00:12:41,010
but we haven't dispatched any actions yet.
231
00:12:41,010 --> 00:12:44,880
Yes, there is this initial initialization action
232
00:12:44,880 --> 00:12:47,160
kind of dispatched by Redux,
233
00:12:47,160 --> 00:12:51,110
but that does not trigger our subscription here.
234
00:12:51,110 --> 00:12:55,020
We could, of course still get our initial state
235
00:12:55,020 --> 00:12:59,860
by just console logging, store.getState here,
236
00:12:59,860 --> 00:13:02,590
if we needed that initial state as well.
237
00:13:02,590 --> 00:13:05,720
If I logged us here after the store is created,
238
00:13:05,720 --> 00:13:08,410
then we see the initial state being output here.
239
00:13:08,410 --> 00:13:13,070
It's an object with a counter value of one, why one?
240
00:13:13,070 --> 00:13:14,700
Because the default is zero
241
00:13:14,700 --> 00:13:16,650
and when this runs for the first time,
242
00:13:16,650 --> 00:13:18,370
we incremented by one.
243
00:13:18,370 --> 00:13:21,050
That's why this is our new states thereafter,
244
00:13:21,050 --> 00:13:22,473
after the initialization.
245
00:13:23,510 --> 00:13:26,460
We could do that, but I don't even wanna do that here.
246
00:13:26,460 --> 00:13:30,020
Instead, let's now create and dispatch an action.
247
00:13:30,020 --> 00:13:33,080
For this we again, use that store object
248
00:13:33,080 --> 00:13:37,160
and on that object besides get state and subscribe,
249
00:13:37,160 --> 00:13:39,490
we can call dispatch.
250
00:13:39,490 --> 00:13:44,490
And dispatch is a method which dispatches an action.
251
00:13:44,810 --> 00:13:48,310
Now an action is a JavaScript object.
252
00:13:48,310 --> 00:13:52,080
It's a JavaScript object with a type property,
253
00:13:52,080 --> 00:13:54,740
which acts as an identifier.
254
00:13:54,740 --> 00:13:56,920
Typically you use a string here
255
00:13:56,920 --> 00:13:59,440
and then this should be a unique string
256
00:13:59,440 --> 00:14:03,220
so that every action, every distinct action,
257
00:14:03,220 --> 00:14:06,120
which you dispatch leads to different things
258
00:14:06,120 --> 00:14:08,530
being done in the reducer.
259
00:14:08,530 --> 00:14:11,750
Currently, we have no code to do different things,
260
00:14:11,750 --> 00:14:13,583
but we're going to add that soon.
261
00:14:14,430 --> 00:14:16,410
So we dispatch such a object
262
00:14:16,410 --> 00:14:21,030
and here we could give does a type of increment
263
00:14:21,030 --> 00:14:23,193
for incrementing the counter.
264
00:14:24,060 --> 00:14:28,250
In its simplest form, that is all we need.
265
00:14:28,250 --> 00:14:32,560
Now with this, if we save that without doing anything else,
266
00:14:32,560 --> 00:14:36,260
if I execute the Redux-demo.JS file again,
267
00:14:36,260 --> 00:14:39,260
we get this output, counter two
268
00:14:39,260 --> 00:14:41,850
We get this output because it is one
269
00:14:41,850 --> 00:14:45,400
after the initialization, as we saw a couple of seconds ago,
270
00:14:45,400 --> 00:14:48,370
and now we dispatched a new action,
271
00:14:48,370 --> 00:14:51,920
which caused this reducer function to run again
272
00:14:51,920 --> 00:14:55,580
and therefore the counter was again, incremented by one,
273
00:14:55,580 --> 00:14:57,983
which is why we now see counter two here.
274
00:14:59,040 --> 00:15:02,220
That is in a nutshell how Redux works.
275
00:15:02,220 --> 00:15:04,920
But of course that's not all it's able to do
276
00:15:04,920 --> 00:15:08,530
because currently the type of action we're dispatching here
277
00:15:08,530 --> 00:15:10,330
has no effect at all.
278
00:15:10,330 --> 00:15:12,623
So let's take a closer look at that as well.
22532
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.