Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,930 --> 00:00:09,090
Welcome back in this video, I want to cover the topic of state and especially the idea of state updates
2
00:00:09,090 --> 00:00:10,260
being asynchronous.
3
00:00:10,500 --> 00:00:11,160
Let's have a look.
4
00:00:12,520 --> 00:00:18,490
I've just started a base creative reactor project for us, and the only thing I've done up until now
5
00:00:18,490 --> 00:00:22,330
is just update this from a function to a class component.
6
00:00:23,400 --> 00:00:29,130
Now, what I want to do is just simply change this attack to a button and we'll do the same over here.
7
00:00:30,550 --> 00:00:37,240
And we don't need any of this, all we need is, well, let's just say here, update state.
8
00:00:38,820 --> 00:00:46,170
So that we have an update stateman and then finally will create some sort of state that will display
9
00:00:46,170 --> 00:00:51,030
here and let's just say that we want to create, again, using constructor.
10
00:00:52,960 --> 00:00:54,970
I'm going to call super, as we always do.
11
00:00:56,030 --> 00:01:03,950
And then finally, we'll say this state is going to equal and let's just say meaning of life.
12
00:01:05,230 --> 00:01:06,580
It's going to be.
13
00:01:07,540 --> 00:01:15,880
Well, 47 and make sure I have the proper syntax and all we're going to do is just display this state
14
00:01:15,880 --> 00:01:20,140
up here as this dot state.
15
00:01:21,340 --> 00:01:22,820
Dot meaning of life.
16
00:01:23,560 --> 00:01:29,800
So if we save this and we run our server, we should have the meaning of light, meaning of life, but
17
00:01:29,800 --> 00:01:31,860
we also want to update the state.
18
00:01:31,900 --> 00:01:36,330
So again, in this one, we'll just create and this is something we've already seen before.
19
00:01:36,340 --> 00:01:36,580
Right.
20
00:01:37,270 --> 00:01:39,530
So this should become second nature to us by now.
21
00:01:39,610 --> 00:01:47,260
So we create an unclick will say it's going to be this dot handle click and we're create this method
22
00:01:47,260 --> 00:01:49,570
on this component right here.
23
00:01:50,600 --> 00:01:56,360
We'll say handle click, we'll use an arrow function so that we don't have that issue with the this
24
00:01:56,360 --> 00:01:56,840
keyword.
25
00:01:57,770 --> 00:02:04,970
And then in here, we'll simply say this dot set state to update our meaning of life.
26
00:02:05,720 --> 00:02:07,830
Well, let's just incremented by one.
27
00:02:07,850 --> 00:02:16,280
So what I'm going to do is say meaning of life is going to equal this dot state, dot meaning of life
28
00:02:16,910 --> 00:02:17,780
plus one.
29
00:02:18,650 --> 00:02:19,040
That's it.
30
00:02:19,850 --> 00:02:24,920
So if I save this and go back and I click on Update State.
31
00:02:26,140 --> 00:02:29,170
I see that my state is updated every time I click.
32
00:02:30,370 --> 00:02:31,210
Now, here's the thing.
33
00:02:32,670 --> 00:02:33,900
As I've mentioned before.
34
00:02:35,120 --> 00:02:43,190
The React Library helps us manipulate the dumb, but to be efficient and to be fast.
35
00:02:44,130 --> 00:02:51,770
It usually matches multiple sets, state calls, because sometimes we can have in our app multiple sets
36
00:02:51,780 --> 00:02:54,690
take also or multiple locations where we change.
37
00:02:54,730 --> 00:02:55,070
Adam?
38
00:02:56,210 --> 00:03:02,690
Instead of calling set state every single time, it sometimes gets smart and says, oh, don't worry,
39
00:03:02,690 --> 00:03:09,200
if there's a lot of states happening, I'll bastion together and update them all into one single update
40
00:03:09,440 --> 00:03:10,280
for performance.
41
00:03:11,930 --> 00:03:20,540
Now, in our case, this worked, but react actually does not guarantee that when we call this and we
42
00:03:20,540 --> 00:03:22,940
update state that this is going to work.
43
00:03:23,900 --> 00:03:24,620
Why is that?
44
00:03:25,460 --> 00:03:30,860
Well, because this call of said state is asynchronous and what does that mean?
45
00:03:31,820 --> 00:03:33,470
It means that when we click.
46
00:03:34,600 --> 00:03:42,100
On the button, this doesn't happen immediately when we call this sad state, we give control to the
47
00:03:42,100 --> 00:03:47,950
React Library and we say, hey, you take care of this, you know the best time to update the state
48
00:03:47,950 --> 00:03:48,430
for me.
49
00:03:48,640 --> 00:03:49,180
I don't care.
50
00:03:49,190 --> 00:03:52,060
Just do it for me and I'll keep doing something else.
51
00:03:53,330 --> 00:03:59,600
So unlike a Synchronoss goal that this happens immediately when we call it asynchronous, just happens
52
00:03:59,750 --> 00:04:00,920
sometime in the future.
53
00:04:01,970 --> 00:04:08,570
And we actually saw this when you mentioned the fact that if we ever want to check the state, let's
54
00:04:08,570 --> 00:04:15,200
say after we make this call, let's say we do a console dialogue, this dot state dot meaning of life,
55
00:04:15,500 --> 00:04:23,540
if we did this, will notice that this state of meaning of life won't get updated, even though it's
56
00:04:23,540 --> 00:04:25,030
called after the state.
57
00:04:25,520 --> 00:04:32,120
Again, if I save and go back and open up the developer tools, you'll see here that when I click this.
58
00:04:33,530 --> 00:04:39,680
My state is 48, but it council logged 47 and it's always one behind.
59
00:04:40,870 --> 00:04:47,890
And that is because this line runs, we give control to react, to update the state for us, but it
60
00:04:47,890 --> 00:04:50,350
doesn't do it immediately, it does it asynchronously.
61
00:04:50,630 --> 00:04:52,990
And by the time we call this line.
62
00:04:54,480 --> 00:05:01,680
The state had it hasn't been updated and we saw a way to get around this right, we can give a second
63
00:05:01,680 --> 00:05:09,330
parameter to our set state goal, which is our callback, and the callback simply takes a function so
64
00:05:09,330 --> 00:05:14,640
we can just have simple our function here that console logs for us.
65
00:05:15,270 --> 00:05:19,050
So if I move this here now and I save.
66
00:05:19,950 --> 00:05:21,510
Go back and I click.
67
00:05:22,490 --> 00:05:24,710
You see that now everything is in sync.
68
00:05:26,050 --> 00:05:28,810
OK, so that works, but here's the thing.
69
00:05:29,530 --> 00:05:31,910
This is actually bad practice.
70
00:05:32,440 --> 00:05:33,220
Why is that?
71
00:05:34,320 --> 00:05:43,020
Well, because our app is simple right now, this is working, however, because of the way South State
72
00:05:43,020 --> 00:05:46,100
batches work into a single update.
73
00:05:46,110 --> 00:05:52,920
Like I mentioned, if we had multiple sets, state calls, it doesn't guarantee that when we increment
74
00:05:52,920 --> 00:05:57,750
meaning of life that this part meaning of life will be the latest version.
75
00:05:58,200 --> 00:06:02,380
Maybe another part of the app modifies meaning of life to be something else.
76
00:06:03,210 --> 00:06:07,950
So because there is no guarantee there's a rule in react that we have to follow.
77
00:06:08,880 --> 00:06:17,460
And the rule is this, if in your set stakes, you ever use this state directly like this, so instead
78
00:06:17,460 --> 00:06:20,670
of meaning of life being something that well, let's say.
79
00:06:21,880 --> 00:06:29,680
Dolphins where we don't use the state, that's completely fine and we can just give it an object, but
80
00:06:29,680 --> 00:06:38,080
if we ever want to use this state or this dog props in our updates to calculate something, then we
81
00:06:38,080 --> 00:06:42,580
should do something instead of what we do here, which is giving it an object.
82
00:06:42,790 --> 00:06:48,730
So let's just remove all of this or we can keep the callback function over here because this could just
83
00:06:48,730 --> 00:06:49,160
stay there.
84
00:06:49,960 --> 00:06:51,850
The important part is the first parameter.
85
00:06:52,270 --> 00:06:56,830
This object, instead of an object, can also be a function.
86
00:06:58,030 --> 00:07:06,490
And it's a function that receives two things one is the state or to be more descriptive, the previous
87
00:07:06,820 --> 00:07:07,300
state.
88
00:07:08,290 --> 00:07:11,350
And then the second parameter is the previous props.
89
00:07:12,520 --> 00:07:19,690
So if I just wrap this in brackets, since these are the parameters and we call this function and in
90
00:07:19,690 --> 00:07:23,800
here I do the exact same thing where we return an object.
91
00:07:25,040 --> 00:07:29,960
So I'm adding the brackets here so that I don't have to just type in return so that we can actually
92
00:07:29,990 --> 00:07:31,210
return the object here.
93
00:07:32,170 --> 00:07:41,500
If I do this, I can say the meaning of life and say this dot state, dot meaning of life plus one just
94
00:07:41,500 --> 00:07:41,860
like that.
95
00:07:44,380 --> 00:07:48,130
You know, let's put this on a new line just so it's cleaner, so I'll remove the brackets here.
96
00:07:49,680 --> 00:07:50,940
And I'll simply say.
97
00:07:52,100 --> 00:07:52,730
Return.
98
00:07:54,020 --> 00:07:57,530
The object meaning of life just like this.
99
00:07:59,310 --> 00:08:05,670
So this whole function is our first parameter and our second parameter is right here.
100
00:08:07,160 --> 00:08:08,420
So if I do this.
101
00:08:09,730 --> 00:08:16,940
I now can use this previous state to guarantee that when I update this, this is the latest previous
102
00:08:16,940 --> 00:08:18,080
state before the update.
103
00:08:18,220 --> 00:08:22,690
So this is just best practice that you have to remember any time you want to update state.
104
00:08:22,990 --> 00:08:30,400
And there is props or state that you need to use within the update, then you should use the syntax
105
00:08:30,400 --> 00:08:32,740
of using a function instead of an object.
106
00:08:33,039 --> 00:08:38,740
So in here, instead of calling this DOT state directly, I simply just say previous.
107
00:08:39,840 --> 00:08:41,669
State, so if I save this.
108
00:08:42,830 --> 00:08:48,080
And go back and I click update, you see that everything is still working properly.
109
00:08:48,270 --> 00:08:48,680
Awesome.
110
00:08:51,530 --> 00:08:53,660
Now, what about this previous props?
111
00:08:54,840 --> 00:09:01,400
In order for us to play with this, we need to have some sort of props or properties on this app component,
112
00:09:01,920 --> 00:09:09,320
but up until now we've only seen App be the parent or the biggest higher level component.
113
00:09:09,330 --> 00:09:10,500
It's the top of the tree.
114
00:09:11,280 --> 00:09:14,580
But as I mentioned previously, state can live anywhere.
115
00:09:15,090 --> 00:09:18,000
We don't need to have state in just one component.
116
00:09:18,030 --> 00:09:20,760
We can bring it down the tree to another node.
117
00:09:21,480 --> 00:09:23,040
We can mix and match.
118
00:09:23,040 --> 00:09:25,110
And that's something we'll explore throughout the course.
119
00:09:25,470 --> 00:09:29,720
But let me show you how we can get props to the app component.
120
00:09:30,210 --> 00:09:36,840
We simply go to index SJS, where the app component is, and let's say we'll add a increment.
121
00:09:37,900 --> 00:09:44,410
Attribute and this increment attribute will be well, while we increment by now, if I do hear one,
122
00:09:45,160 --> 00:09:46,570
this is going to give me an air.
123
00:09:47,520 --> 00:09:52,620
It's going to say, well, I'm trying to do just sex and I have no idea what this means because remember,
124
00:09:53,040 --> 00:09:59,190
we need to pass it some sort of a value in JavaScript, which means wrapping it in brackets.
125
00:09:59,500 --> 00:10:03,690
So this evaluates to a JavaScript expression, if I say this.
126
00:10:04,970 --> 00:10:06,350
I now have these props.
127
00:10:07,700 --> 00:10:10,260
Now, let's say I wanted to use the props here.
128
00:10:10,820 --> 00:10:12,800
So now I'm going to get the incremental prop.
129
00:10:13,010 --> 00:10:14,660
So I'll just say this.
130
00:10:15,670 --> 00:10:17,930
Got props, Dot.
131
00:10:19,050 --> 00:10:19,530
Inkerman.
132
00:10:21,260 --> 00:10:22,190
If I see this.
133
00:10:24,070 --> 00:10:26,320
All right, it seems to be working, but let's check.
134
00:10:27,980 --> 00:10:28,940
Fi UPD state.
135
00:10:31,590 --> 00:10:34,110
All right, this is working nicely, perfect.
136
00:10:35,710 --> 00:10:41,710
And again, if I wanted to just use previous props, I can just make sure that it's the latest and nothing
137
00:10:41,710 --> 00:10:44,650
has changed it by using previous props if I save it.
138
00:10:46,070 --> 00:10:46,730
Once again.
139
00:10:47,720 --> 00:10:49,460
Everything is working great.
140
00:10:51,570 --> 00:10:53,550
Now, one last thing.
141
00:10:54,830 --> 00:11:03,050
Usually it's good practice to put in here in the constructor and super to put in the props like this.
142
00:11:04,370 --> 00:11:05,930
Now, what does this do?
143
00:11:06,710 --> 00:11:12,830
This allows you to actually use the props in the constructor.
144
00:11:14,070 --> 00:11:17,400
What do I mean, if I do hear this, Dr..
145
00:11:18,640 --> 00:11:19,270
Props.
146
00:11:20,670 --> 00:11:28,650
I can now use this as props inside of the constructor, so if you ever need to use this stop props inside
147
00:11:28,650 --> 00:11:30,990
of the constructor, you need to pass this year.
148
00:11:32,100 --> 00:11:40,290
So, for example, if I just simply do this top Prof's equals props, I save it and I remove the props
149
00:11:40,290 --> 00:11:40,780
from here.
150
00:11:41,250 --> 00:11:43,110
You'll notice that I'll get an air.
151
00:11:43,710 --> 00:11:48,130
Props is not defined because I can I use this props this way.
152
00:11:48,870 --> 00:11:52,080
Now, there are occasions when that needs to happen.
153
00:11:52,270 --> 00:11:57,530
So it's always recommended that you just add props in case you ever need to do this.
154
00:11:57,710 --> 00:11:58,440
Props in here.
155
00:12:00,270 --> 00:12:06,090
So let's say, for example, in our state calculation, we want to say that meaning of life is going
156
00:12:06,090 --> 00:12:11,550
to equal forty seven plus this prop start increment.
157
00:12:12,210 --> 00:12:15,090
So I'll originally be forty eight.
158
00:12:16,800 --> 00:12:23,820
If we safe here, we have previous props here for the increment, if I go back, you see that it's now
159
00:12:23,820 --> 00:12:29,010
forty eight as the default so that the next update will be forty nine.
160
00:12:30,130 --> 00:12:36,940
So this is why we passed props in the constructor and super now the final thing I want to show you is
161
00:12:36,940 --> 00:12:41,100
that we act up has something called the alternate class syntax.
162
00:12:41,290 --> 00:12:44,080
Now, in regular JavaScript, you can do this.
163
00:12:44,080 --> 00:12:49,390
And there are proposals that are going to allow future versions of JavaScript to do this, but you may
164
00:12:49,390 --> 00:12:51,370
see them in common react cabezas.
165
00:12:51,370 --> 00:12:57,520
So I want to show you, because some people don't like this constructor having so many lines of code,
166
00:12:58,000 --> 00:13:00,940
sometimes a state is just a simple thing.
167
00:13:01,970 --> 00:13:06,080
So, for example, if we didn't have any of this increment and we just had the.
168
00:13:07,630 --> 00:13:09,580
Plus one here.
169
00:13:10,850 --> 00:13:12,000
And let's say no props.
170
00:13:12,020 --> 00:13:15,170
Well, we can just remove all of this.
171
00:13:17,670 --> 00:13:20,820
Like, so in just simply say that St..
172
00:13:22,340 --> 00:13:27,260
Is equal meaning of life, which equals forty seven, and that's it.
173
00:13:29,190 --> 00:13:36,810
And if I do this, it leaves out the constructor and initializes the state as a class field declaration.
174
00:13:36,990 --> 00:13:43,320
Again, this is something because of the way we act up and the bable compiler that it has underneath,
175
00:13:43,530 --> 00:13:49,410
but you are going to see this in some code bases where you just want to set state really easily, but
176
00:13:49,560 --> 00:13:55,470
you won't have access to the props inside of here, as we did with the constructor, so that if I save
177
00:13:55,470 --> 00:13:58,020
this, this should still work.
178
00:13:58,170 --> 00:13:58,860
And it does.
179
00:14:00,120 --> 00:14:05,430
Keep in mind, state updates are asynchronous and we want to be careful.
180
00:14:05,550 --> 00:14:13,410
The rule is this do you ever want to manipulate or use the state somehow after the update, then add
181
00:14:13,410 --> 00:14:19,650
in a second parameter, which will be a function where you can use that updated state.
182
00:14:20,680 --> 00:14:26,500
Maybe in your update, in your state update, you have to use the state as part of the calculation or
183
00:14:26,500 --> 00:14:31,990
the props as part of the calculation, then you should pass this function form to update the state.
184
00:14:32,590 --> 00:14:37,090
Otherwise, you can just use the simple object notation that we've seen up until now.
185
00:14:38,210 --> 00:14:44,860
If you get this concept, this is going to take you very far in your career, so hope that makes sense
186
00:14:44,870 --> 00:14:46,340
and I'll see you in the next video.
17587
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.