Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,250 --> 00:00:10,210
The way we use use state is correct, we're passing in a state, we're getting back this array which
2
00:00:10,210 --> 00:00:15,540
restoring in this constant but the way we update the state is not correct.
3
00:00:16,140 --> 00:00:22,680
There's one superimportant difference when you compare use state and how you work with state and functional
4
00:00:22,680 --> 00:00:27,060
components to how you worked with state, state and to state and Class-Based components.
5
00:00:27,420 --> 00:00:32,250
There's an important difference between these two concepts for use state.
6
00:00:32,250 --> 00:00:34,560
As I mentioned earlier, you can manage any state.
7
00:00:34,560 --> 00:00:35,940
It doesn't have to be an object.
8
00:00:35,940 --> 00:00:37,980
You could just manage some text here.
9
00:00:38,670 --> 00:00:44,580
But here I decided to work with an object because I have, well, two types of state or two input fields
10
00:00:44,580 --> 00:00:48,930
which I want to manage now in class based components, you always had to use an object.
11
00:00:48,930 --> 00:00:56,880
But there whenever you call set state, whichever object you passed into said state was merged with
12
00:00:56,880 --> 00:00:58,010
your existing state.
13
00:00:58,140 --> 00:01:03,540
So there if we passed it an object, the title property, it would be merged with our current state
14
00:01:03,540 --> 00:01:09,600
and diamond property would not be lost, but simply title property would be updated and all old state
15
00:01:09,600 --> 00:01:12,390
would be kept with U.S. state.
16
00:01:12,390 --> 00:01:14,680
That's different, this updating function.
17
00:01:14,700 --> 00:01:21,810
The second element of our array does update the state, but it does so by replacing the current state
18
00:01:21,810 --> 00:01:27,690
with this, which in this case means if we type into the title, our new state simply is an object which
19
00:01:27,690 --> 00:01:31,200
only has a title property and we drop the amount property.
20
00:01:31,200 --> 00:01:35,170
Our new state is an object that simply doesn't have an amount property anymore.
21
00:01:35,760 --> 00:01:39,790
That's why I'm getting this warning that the input of type number is uncontrolled.
22
00:01:39,960 --> 00:01:45,120
That's our second input and I'm getting a warning regarding that input when we type into the first input,
23
00:01:45,420 --> 00:01:48,980
because the second input does amount input here, which is of type no.
24
00:01:49,350 --> 00:01:52,740
This, of course, refers to the amount property in our state.
25
00:01:53,160 --> 00:01:58,920
Now, if we type into the first input, we update the state to now be an object which only has a title
26
00:01:58,920 --> 00:01:59,490
property.
27
00:01:59,730 --> 00:02:05,220
Hence the property we're trying to access on a second input, the amount property simply doesn't exist
28
00:02:05,220 --> 00:02:05,610
anymore.
29
00:02:05,610 --> 00:02:07,600
And that's exactly what's causing this error.
30
00:02:08,490 --> 00:02:10,500
The solution is simple.
31
00:02:10,860 --> 00:02:15,810
When we update the state, we have to ensure that we don't lose any old data.
32
00:02:15,820 --> 00:02:20,900
That's not our responsibility because Riak doesn't merge that automatically for us.
33
00:02:21,240 --> 00:02:27,720
And whilst this might sound like a bad change, whilst this might sound like it should do that for us,
34
00:02:27,870 --> 00:02:33,990
you'll later learn why React actually doesn't merge does for us anymore because now we actually have
35
00:02:33,990 --> 00:02:36,690
more flexibility regarding how we manage state.
36
00:02:37,350 --> 00:02:43,530
If we decide to manage some object state, though, which very well might be the case in many situations,
37
00:02:43,860 --> 00:02:47,790
we have to ensure that whenever we updated, we don't lose any old data.
38
00:02:48,180 --> 00:02:54,000
So when we update the title here, we also have to set the amount property and simply set it to our
39
00:02:54,000 --> 00:03:00,460
will current amount states or to the unchanged amount state by accessing input states zero.
40
00:03:00,510 --> 00:03:03,570
So the current state snapshot data amount, for example.
41
00:03:04,380 --> 00:03:08,370
On the other hand, when we update the amount, we should also make sure that we don't lose the title
42
00:03:08,370 --> 00:03:12,990
by also setting this to input state zero DOT title.
43
00:03:14,710 --> 00:03:20,320
Now, if you do that, if you make sure that when you update the state, you don't lose any information.
44
00:03:20,770 --> 00:03:24,460
Now, if I type here, you see I get no warnings anymore.
45
00:03:24,650 --> 00:03:28,380
And of course, this button doesn't do anything because we haven't connected it any more.
46
00:03:28,480 --> 00:03:31,180
But these inputs are now working correctly.
47
00:03:32,410 --> 00:03:40,120
Nonetheless, our code here, the updating code is not 100 percent correct yet reason for that is that
48
00:03:40,630 --> 00:03:44,260
I'm accessing the current state snapshot on the first array element.
49
00:03:44,260 --> 00:03:49,110
And of course, that does hold the current state snapshot for this renderer cycle of the component.
50
00:03:49,510 --> 00:03:57,400
But the way react updates to state, we are not having a 100 percent guarantee here that our current
51
00:03:57,400 --> 00:04:01,300
state snapshot actually is the latest state we committed.
52
00:04:01,300 --> 00:04:07,300
And this might sound strange, but due to the way we react, renders components upon state updates and
53
00:04:07,300 --> 00:04:12,880
so on, you could theoretically have a case where there's a lot going on on your page, let's say a
54
00:04:12,880 --> 00:04:21,010
lot of animations or a lot of stuff that react decides to defer a state update until there's less work
55
00:04:21,010 --> 00:04:21,640
going on.
56
00:04:21,880 --> 00:04:28,360
And therefore, a user might be typing here into the title input before the latest amount input because
57
00:04:28,360 --> 00:04:35,290
the user also typed into the amount input field a second ago before that state change has been rendered
58
00:04:35,290 --> 00:04:35,920
to the screen.
59
00:04:35,950 --> 00:04:38,260
Now, it's very unlikely for this to happen.
60
00:04:38,260 --> 00:04:42,310
And in this simple application here, it will never happen, actually.
61
00:04:42,700 --> 00:04:48,820
But in more complex apps, you could have a case where you're changing some state and your latest state
62
00:04:48,820 --> 00:04:53,290
change hasn't been committed yet, hasn't been rendered to the screen yet.
63
00:04:53,530 --> 00:05:00,550
Hence, if you have a new state change that also depends on your latest state snapshot, then you don't
64
00:05:00,550 --> 00:05:05,440
have a guarantee that this state snapshot has already been well committed or created.
65
00:05:05,590 --> 00:05:11,370
And therefore, this year, accessing our latest state value like this is not the ideal way.
66
00:05:11,770 --> 00:05:13,800
Instead, does updating function?
67
00:05:13,810 --> 00:05:21,340
So this function, stored and stored as a second element in your array, has an alternative forum which
68
00:05:21,340 --> 00:05:26,890
you can use instead of just passing a value to that update function.
69
00:05:26,920 --> 00:05:32,980
So to that function that sets a new state, you can also pass a function to that function and anonymous
70
00:05:32,980 --> 00:05:34,210
function, for example.
71
00:05:34,210 --> 00:05:35,710
But it could of course, also be a named one.
72
00:05:36,250 --> 00:05:43,240
And if you pass a function here, which of course has to return your new state value in the end, and
73
00:05:43,240 --> 00:05:48,760
I'm wrapping it in brackets here so that I can use this inline notation here, does inline error function
74
00:05:48,760 --> 00:05:54,190
notation that after the Arrow business Disvalue, we're returning and we're returning an object here
75
00:05:54,190 --> 00:05:58,570
without the extra brackets, this would be treated as the function body and this would simply be an
76
00:05:58,570 --> 00:05:59,770
incorrect function body.
77
00:06:00,100 --> 00:06:06,850
Now we indicate to JavaScript that this year is our function body and this is actually the return value,
78
00:06:06,850 --> 00:06:08,000
which is just an object.
79
00:06:08,500 --> 00:06:10,360
So now we're returning that object.
80
00:06:10,360 --> 00:06:14,300
But in this function, which we're passing to this set state function here.
81
00:06:14,320 --> 00:06:18,880
So to say there we get an argument passed in automatically by react.
82
00:06:19,180 --> 00:06:20,020
And that's our.
83
00:06:21,000 --> 00:06:27,660
Previous input state or whatever you want to call it, and wikinomics these brackets here, so here,
84
00:06:27,660 --> 00:06:34,500
this function will actually tell react that we want to get the latest state that we set, even if it
85
00:06:34,500 --> 00:06:37,770
hasn't been fully committed yet for this renderer cycle.
86
00:06:38,430 --> 00:06:44,810
So now previous input state allows us to access this previous input state like this.
87
00:06:45,390 --> 00:06:50,640
And the difference to using input state zero is that we're now not using the latest state, which has
88
00:06:50,640 --> 00:06:56,010
been fully committed and which is used in this render cycle of this component, but that we're using
89
00:06:56,010 --> 00:07:03,030
the latest state we set with an updating function, no matter if react already recreated the function
90
00:07:03,030 --> 00:07:05,070
or not based on that last update.
91
00:07:05,460 --> 00:07:10,980
So now here we have to guarantee that we get the latest state at all times.
92
00:07:11,520 --> 00:07:13,080
That's, of course, what we want here.
93
00:07:14,690 --> 00:07:19,290
And here are missing an extra bracket, by the way, to close that input, state one function.
94
00:07:19,880 --> 00:07:26,750
So now this is the approach I always want to use down there for the amount of input we get, our previous
95
00:07:26,750 --> 00:07:28,030
input state.
96
00:07:28,460 --> 00:07:32,120
And then here we're returning a JavaScript object.
97
00:07:32,120 --> 00:07:36,950
And that object can use that previous input state to set the title or the amount.
98
00:07:37,890 --> 00:07:42,180
Now, is that have we safe that if we go back, you'll actually notice a strange behavior, though?
99
00:07:42,600 --> 00:07:48,960
If you start typing, you'll get an error, and that error tells you that you reused an event, essentially.
100
00:07:49,950 --> 00:07:56,910
Now, the problem here is that when we're passing a function to this function here, to this sad state
101
00:07:56,910 --> 00:08:00,240
function in the end, then we're having this anonymous function here.
102
00:08:00,240 --> 00:08:01,640
And that's that's a closure.
103
00:08:01,650 --> 00:08:05,310
So that's simply a function that closes over surrounding values.
104
00:08:05,310 --> 00:08:08,280
And in this case, it closes over our event here.
105
00:08:08,610 --> 00:08:12,570
The event is fed into this on change anonymous function.
106
00:08:12,810 --> 00:08:18,870
And in that function, which we have here upon a change, we're calling these sad state functions who
107
00:08:18,870 --> 00:08:19,800
are calling a function.
108
00:08:19,800 --> 00:08:22,880
But then I'm also defining yet another nested function here.
109
00:08:23,070 --> 00:08:28,470
So we have a function, one which is our event function and function to which is our updating function.
110
00:08:29,220 --> 00:08:35,100
Now, the problem with that is that if in this inner function here I use something from the outer function,
111
00:08:35,100 --> 00:08:41,010
which I'm doing, I'm using the event, then this event will be locked in for the first keystroke,
112
00:08:41,010 --> 00:08:46,200
essentially, which means that for subsequent keystrokes we don't use the new keystroke event, but
113
00:08:46,200 --> 00:08:50,250
the previous one, which of course is then reused and which caused this error.
114
00:08:50,730 --> 00:08:56,130
Now, normally that wouldn't be a problem, of course, because, yes, this inner function is state
115
00:08:56,130 --> 00:09:00,840
updating function functions, a closure, and therefore it closes over a DEVANT object, which means
116
00:09:00,840 --> 00:09:06,900
it saves this event object for its execution so that when does interstate updating function runs, which
117
00:09:06,900 --> 00:09:12,500
happens asynchronously, we're guaranteed to use the event that was triggered for that keystrokes or
118
00:09:12,510 --> 00:09:14,290
it was created for the keystroke.
119
00:09:14,790 --> 00:09:21,510
The problem with events and that's now really exclusive to events and react just is that react events
120
00:09:21,510 --> 00:09:28,080
are not the native theme events, but special synthetic events created by REACT, which basically replicate
121
00:09:28,080 --> 00:09:30,060
the native DOM events he would normally get.
122
00:09:30,060 --> 00:09:33,450
But react adds a special factor to that.
123
00:09:33,450 --> 00:09:38,070
It pulls these event objects, which simply means it uses event objects.
124
00:09:38,370 --> 00:09:44,040
So instead of creating a new event object for every keystroke, it instead reuses the previous object.
125
00:09:44,190 --> 00:09:48,900
And the consequence of this is that for the second keystroke, since we have a closure and we locked
126
00:09:48,900 --> 00:09:56,100
in the event for the first keystroke, for the second keystroke, we still reuse that same object we
127
00:09:56,100 --> 00:09:59,260
had for the first keystroke and dead Sibley's.
128
00:09:59,280 --> 00:10:05,790
The problem here, we're reusing the wrong event object because of the way react handles event objects.
129
00:10:06,030 --> 00:10:09,210
Now I'm aware of this sounding very confusing.
130
00:10:09,420 --> 00:10:15,270
In the end, this is really a special case because of the way React handles this event object.
131
00:10:15,660 --> 00:10:21,600
The solution is that outside of this sets that function, we create a constant, for example.
132
00:10:22,500 --> 00:10:30,100
Which we hear on the title could call new title and their iStore event target value.
133
00:10:30,240 --> 00:10:34,800
Now, this will be recreated whenever we're typing.
134
00:10:35,010 --> 00:10:37,280
And now we need a real function body, though.
135
00:10:37,290 --> 00:10:43,920
So we need to wrap this into curly braces, because now we actually have more than one line.
136
00:10:43,920 --> 00:10:45,330
We're not just for turning something.
137
00:10:45,330 --> 00:10:46,830
We're not just executing one expression.
138
00:10:46,840 --> 00:10:47,990
What we have two statements here.
139
00:10:48,300 --> 00:10:52,610
So now here I'm storing the new title for every keystroke and now this here.
140
00:10:53,010 --> 00:10:57,690
This sets that function with this nested inner function can use new title.
141
00:10:57,990 --> 00:11:04,740
And this will therefore now close over this new extract, a title for every keystroke, instead of using
142
00:11:04,740 --> 00:11:08,430
that same event all the time, that same event object.
143
00:11:09,880 --> 00:11:15,910
Of course, we do the same for the amount they are, we now need curly braces around this entire function
144
00:11:15,910 --> 00:11:17,770
expression year then.
145
00:11:18,700 --> 00:11:24,100
We got our new amount from event target, not title value here.
146
00:11:25,600 --> 00:11:33,010
And the amount now is simply a new amount, again, simply because we're using a closure here and we
147
00:11:33,010 --> 00:11:38,800
want to make sure that we're generating a new amount for every keystroke, and then this will automatically
148
00:11:38,800 --> 00:11:44,140
be considered by this inner function instead of reusing that same event object all the time.
149
00:11:44,800 --> 00:11:51,640
Now that we can save this, go back and now start typing here and that now all works.
16869
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.