Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,190 --> 00:00:03,810
Now, with fragments and portals,
2
00:00:03,810 --> 00:00:05,530
we had two cool features
3
00:00:05,530 --> 00:00:08,490
that help us write cleaner HTML code.
4
00:00:08,490 --> 00:00:11,240
The app worked in exactly the same way
5
00:00:11,240 --> 00:00:13,710
before we added those features too
6
00:00:13,710 --> 00:00:15,290
but with those features,
7
00:00:15,290 --> 00:00:18,770
we end up with semantically more correct HTML code
8
00:00:18,770 --> 00:00:20,410
and that's never a bad thing.
9
00:00:20,410 --> 00:00:22,920
It makes your app more accessible,
10
00:00:22,920 --> 00:00:26,450
makes sure you don't render unnecessarily many divs
11
00:00:26,450 --> 00:00:28,820
and in general, shows that you are a developer
12
00:00:28,820 --> 00:00:30,503
who knows what he's doing.
13
00:00:31,520 --> 00:00:34,830
Now let me move on to one last key feature,
14
00:00:34,830 --> 00:00:36,380
which I also wanna show you,
15
00:00:36,380 --> 00:00:37,950
which does something different
16
00:00:37,950 --> 00:00:39,960
than fragments and portals though.
17
00:00:39,960 --> 00:00:42,170
And that would be refs.
18
00:00:42,170 --> 00:00:45,110
References but the name in React is just ref,
19
00:00:45,110 --> 00:00:47,620
so the short form of reference.
20
00:00:47,620 --> 00:00:49,760
What do refs do?
21
00:00:49,760 --> 00:00:52,150
Refs are actually quite powerful
22
00:00:52,150 --> 00:00:53,860
as we will see throughout the course
23
00:00:53,860 --> 00:00:55,890
but in their most basic form,
24
00:00:55,890 --> 00:00:59,590
they allow us to get access to other DOM elements
25
00:00:59,590 --> 00:01:01,250
and work with them.
26
00:01:01,250 --> 00:01:03,180
Now, what do I mean with that?
27
00:01:03,180 --> 00:01:05,890
Let's go to the AddUser component again.
28
00:01:05,890 --> 00:01:07,530
There we have our inputs
29
00:01:07,530 --> 00:01:10,030
and we manage what the user enters
30
00:01:10,030 --> 00:01:13,010
by simply keeping track of it.
31
00:01:13,010 --> 00:01:14,720
We simply have our state
32
00:01:14,720 --> 00:01:16,160
and with every keystroke,
33
00:01:16,160 --> 00:01:17,960
we update our state.
34
00:01:17,960 --> 00:01:21,150
So with every keystroke, we update the value we get
35
00:01:21,150 --> 00:01:23,970
by the user and we store it in our state
36
00:01:23,970 --> 00:01:27,430
and we feed that state back into the input
37
00:01:27,430 --> 00:01:30,060
and we then use that state later on
38
00:01:30,060 --> 00:01:33,930
to reset the inputs but also to send it
39
00:01:33,930 --> 00:01:36,223
to the place where we need the data.
40
00:01:37,860 --> 00:01:41,290
So that is a perfectly fine way of managing this.
41
00:01:41,290 --> 00:01:43,880
But updating the state with every keystroke
42
00:01:43,880 --> 00:01:46,750
when we only need it when we submit the form
43
00:01:46,750 --> 00:01:49,560
sounds a bit redundant to me.
44
00:01:49,560 --> 00:01:52,410
And that's a scenario where refs could help us,
45
00:01:52,410 --> 00:01:54,210
though they're not limited to that.
46
00:01:54,210 --> 00:01:56,390
I will already say that.
47
00:01:56,390 --> 00:01:58,040
How do refs work?
48
00:01:58,040 --> 00:02:00,430
With refs, we can set up a connection
49
00:02:00,430 --> 00:02:04,080
between a HTML element which is being rendered in the end
50
00:02:04,080 --> 00:02:06,640
and our other JavaScript code.
51
00:02:06,640 --> 00:02:09,320
And for that, we first of all, need to create a ref,
52
00:02:09,320 --> 00:02:12,410
which we do with the help of another React hook.
53
00:02:12,410 --> 00:02:14,570
We use the useRef hook here
54
00:02:14,570 --> 00:02:19,570
and we then simply call useRef here in our code,
55
00:02:20,510 --> 00:02:23,340
in our functional component.
56
00:02:23,340 --> 00:02:27,470
And like all React hooks, useRef is only usable
57
00:02:27,470 --> 00:02:29,673
inside of functional components.
58
00:02:30,730 --> 00:02:32,660
Now, what does useRef return
59
00:02:32,660 --> 00:02:35,380
and which value does it take?
60
00:02:35,380 --> 00:02:38,440
It takes a default value you wanna initialize it to
61
00:02:38,440 --> 00:02:39,980
but we don't need this here
62
00:02:39,980 --> 00:02:42,710
but I'm very interested in what it returns
63
00:02:42,710 --> 00:02:45,380
because it returns a value
64
00:02:45,380 --> 00:02:47,920
which allows us to work with that ref later,
65
00:02:47,920 --> 00:02:49,970
so which allows us to work with that element
66
00:02:49,970 --> 00:02:52,450
to which we're going to connect it.
67
00:02:52,450 --> 00:02:55,093
And therefore, here I'll name this nameInputRef
68
00:02:56,410 --> 00:02:59,360
because I plan on connecting this ref
69
00:02:59,360 --> 00:03:00,850
with that first input,
70
00:03:00,850 --> 00:03:02,973
which allows us to enter a username.
71
00:03:04,390 --> 00:03:06,960
So here we have the nameInputRef.
72
00:03:06,960 --> 00:03:09,540
Now I'll duplicate this and create another ref
73
00:03:09,540 --> 00:03:11,270
by calling useRef again
74
00:03:11,270 --> 00:03:12,870
and that will be my ageInputRef.
75
00:03:15,220 --> 00:03:16,630
So now I've got two refs
76
00:03:16,630 --> 00:03:19,550
and they're just sitting there doing nothing.
77
00:03:19,550 --> 00:03:21,750
They're initialized to be undefined
78
00:03:21,750 --> 00:03:23,380
because that's the default
79
00:03:23,380 --> 00:03:24,533
and they're useless.
80
00:03:25,690 --> 00:03:27,630
Now, we can let React know
81
00:03:27,630 --> 00:03:29,200
that we wanna connect a ref
82
00:03:29,200 --> 00:03:32,470
to a HTML element by going to that element
83
00:03:32,470 --> 00:03:34,400
to which we wanna connect the ref
84
00:03:34,400 --> 00:03:38,500
and adding a special prop there, the ref prop.
85
00:03:38,500 --> 00:03:41,240
Just like the key prop, that's a built-in prop,
86
00:03:41,240 --> 00:03:44,060
which you can add to any HTML element
87
00:03:44,920 --> 00:03:46,890
because, and that's important,
88
00:03:46,890 --> 00:03:51,320
you can connect any HTML element to one of your references.
89
00:03:51,320 --> 00:03:53,610
You will very often do that for inputs
90
00:03:53,610 --> 00:03:56,140
because you wanna fetch input data, for example,
91
00:03:56,140 --> 00:03:58,293
but you can do with any element.
92
00:03:59,370 --> 00:04:01,220
So here I've got my ref
93
00:04:01,220 --> 00:04:06,183
and now I will pass nameInputRef as a value here.
94
00:04:07,490 --> 00:04:09,780
And nameInputRef is just that constant,
95
00:04:09,780 --> 00:04:11,563
which stores this first ref.
96
00:04:12,496 --> 00:04:14,890
So with that, I'm connecting this ref,
97
00:04:14,890 --> 00:04:16,680
which I created in this component
98
00:04:16,680 --> 00:04:18,760
with that JSX code that is rendered
99
00:04:18,760 --> 00:04:20,123
by that same component.
100
00:04:21,060 --> 00:04:23,910
And now a connection will be established.
101
00:04:23,910 --> 00:04:27,620
The first time React reaches this code
102
00:04:27,620 --> 00:04:29,220
and renders this code,
103
00:04:29,220 --> 00:04:32,350
it will actually set the values stored
104
00:04:32,350 --> 00:04:36,280
in nameInputRef to the native DOM element
105
00:04:36,280 --> 00:04:38,990
that is rendered based on this input.
106
00:04:38,990 --> 00:04:40,000
And that's important.
107
00:04:40,000 --> 00:04:43,180
What will end up inside of nameInputRef in the end
108
00:04:43,180 --> 00:04:46,123
will really be a real DOM element later.
109
00:04:47,740 --> 00:04:49,980
So let's do the same for the age
110
00:04:49,980 --> 00:04:52,880
and here I'll connect this to ageInputRef
111
00:04:56,330 --> 00:04:58,820
and that's now connected as well.
112
00:04:58,820 --> 00:05:01,680
So now the refs are connected.
113
00:05:01,680 --> 00:05:02,993
What's the benefit?
114
00:05:04,030 --> 00:05:07,370
Let's go to our addUserHandler function
115
00:05:07,370 --> 00:05:09,530
and after event.preventDefault,
116
00:05:09,530 --> 00:05:12,313
I wanna console.log my refs.
117
00:05:13,200 --> 00:05:15,640
Let's start with the nameInputRef.
118
00:05:15,640 --> 00:05:18,830
If I log this here and I go back
119
00:05:18,830 --> 00:05:20,420
and I go to the console
120
00:05:20,420 --> 00:05:22,230
so that I can see the log,
121
00:05:22,230 --> 00:05:23,760
if I submit this,
122
00:05:23,760 --> 00:05:26,283
you see there's an object being output here.
123
00:05:27,620 --> 00:05:29,450
There is a object being output here
124
00:05:29,450 --> 00:05:32,620
and that object has a current property.
125
00:05:32,620 --> 00:05:34,000
Now, that's important.
126
00:05:34,000 --> 00:05:36,930
This ref value, which is being generated here
127
00:05:36,930 --> 00:05:38,760
always is an object,
128
00:05:38,760 --> 00:05:41,070
which always has a current prop
129
00:05:41,070 --> 00:05:44,310
and the current prop holds the actual value
130
00:05:44,310 --> 00:05:46,784
that ref is connected with.
131
00:05:46,784 --> 00:05:49,730
Now, by default, it's undefined
132
00:05:49,730 --> 00:05:52,140
but as soon as this code ran,
133
00:05:52,140 --> 00:05:53,740
because of this ref prop,
134
00:05:53,740 --> 00:05:56,280
the nameInputRef is connected to that input
135
00:05:56,280 --> 00:05:58,600
and hence, it's actually the input
136
00:05:58,600 --> 00:06:02,113
which is being stored as a value in the current prop.
137
00:06:03,120 --> 00:06:05,290
And what's being stored here really
138
00:06:05,290 --> 00:06:07,593
is the actual DOM node.
139
00:06:08,450 --> 00:06:12,270
So not some theoretical value or anything like that
140
00:06:12,270 --> 00:06:13,980
but the real DOM node,
141
00:06:13,980 --> 00:06:16,020
which you could now manipulate
142
00:06:16,020 --> 00:06:18,794
and do all kinds of things with.
143
00:06:18,794 --> 00:06:21,790
Now, it is recommended that you don't manipulate it.
144
00:06:21,790 --> 00:06:25,600
Your DOM should really only be manipulated by React.
145
00:06:25,600 --> 00:06:29,165
You're using React to let it do all the heavy lifting
146
00:06:29,165 --> 00:06:33,500
but reading data from the input doesn't sound too bad
147
00:06:33,500 --> 00:06:35,540
because you're not changing anything with that.
148
00:06:35,540 --> 00:06:37,123
You're just reading data.
149
00:06:38,150 --> 00:06:40,660
So here instead of logging this here,
150
00:06:40,660 --> 00:06:45,660
we can, of course, read current.value.
151
00:06:45,670 --> 00:06:47,970
Now, current refers to the value stored
152
00:06:47,970 --> 00:06:50,130
and the value stored is the input element
153
00:06:50,130 --> 00:06:54,150
and every input element has a value property in JavaScript.
154
00:06:54,150 --> 00:06:55,333
We can actually see this here.
155
00:06:55,333 --> 00:06:57,163
There's the value property.
156
00:06:58,920 --> 00:07:00,470
So if I now save this
157
00:07:00,470 --> 00:07:03,630
and I log this value, if I enter Max here,
158
00:07:03,630 --> 00:07:06,340
you see MAX being logged here.
159
00:07:06,340 --> 00:07:07,610
And that's pretty cool
160
00:07:07,610 --> 00:07:10,200
because that means we can get access
161
00:07:10,200 --> 00:07:13,430
to the values stored in the element
162
00:07:13,430 --> 00:07:15,980
without having to log every keystroke.
163
00:07:15,980 --> 00:07:17,530
We don't need state for this.
164
00:07:17,530 --> 00:07:20,393
We can just read it when the submit button is pressed.
165
00:07:21,780 --> 00:07:25,390
So that means we could now replace console.log
166
00:07:25,390 --> 00:07:27,140
maybe with a helper constant,
167
00:07:27,140 --> 00:07:29,720
which we use inside of addUserHandler.
168
00:07:29,720 --> 00:07:31,980
I'll name that constant enteredName
169
00:07:34,430 --> 00:07:39,430
and simply store my current.value in that.
170
00:07:40,440 --> 00:07:41,960
And I do the same for the age.
171
00:07:41,960 --> 00:07:46,960
I add my enteredAge and store ageInputRef.current.value.
172
00:07:52,310 --> 00:07:56,430
Now, enteredAge clashes with my state here,
173
00:07:56,430 --> 00:07:59,400
so therefore, I'll pick a slightly different name,
174
00:07:59,400 --> 00:08:01,453
I'll use enteredUserAge here.
175
00:08:02,480 --> 00:08:05,300
Now we can check enteredName here
176
00:08:06,148 --> 00:08:08,740
and enteredUserAge here
177
00:08:08,740 --> 00:08:11,870
and also enteredUserAge here in this condition.
178
00:08:11,870 --> 00:08:14,963
So always the values we retrieved from the refs.
179
00:08:16,650 --> 00:08:20,338
And I also use enteredName here
180
00:08:20,338 --> 00:08:23,057
and enteredUserAge to submit it to onAddUser.
181
00:08:25,160 --> 00:08:26,430
Now, with that, of course,
182
00:08:26,430 --> 00:08:29,560
we should no longer reset these inputs
183
00:08:29,560 --> 00:08:31,940
by resetting the state here though
184
00:08:31,940 --> 00:08:34,039
because we're not using the state
185
00:08:34,039 --> 00:08:36,070
to get our values anymore.
186
00:08:36,070 --> 00:08:38,270
We currently still have those listeners
187
00:08:38,270 --> 00:08:41,510
but we actually now don't really need those values anymore.
188
00:08:41,510 --> 00:08:43,592
We're using refs to get the values.
189
00:08:44,700 --> 00:08:45,950
So what we could do
190
00:08:45,950 --> 00:08:48,523
is we could get rid of these states here.
191
00:08:49,530 --> 00:08:52,920
So of these states where we listen to every keystroke.
192
00:08:52,920 --> 00:08:55,940
Therefore, get rid of these state updating functions
193
00:08:55,940 --> 00:08:58,903
and get rid of these two handlers.
194
00:09:00,440 --> 00:09:04,160
And also, get rid of the value property
195
00:09:04,160 --> 00:09:07,123
and the onChange property on the inputs.
196
00:09:08,510 --> 00:09:11,280
With that, we shortened that code quite a bit
197
00:09:11,280 --> 00:09:14,420
and we're relying on refs to read the values.
198
00:09:14,420 --> 00:09:16,960
Hence if I add Max, 31,
199
00:09:16,960 --> 00:09:20,090
you see we still can add that user.
200
00:09:20,090 --> 00:09:21,900
However, during the process,
201
00:09:21,900 --> 00:09:24,433
we lost our resetting logic.
202
00:09:25,410 --> 00:09:28,120
Now, to bring it back, we've got two options.
203
00:09:28,120 --> 00:09:30,680
We can switch back to the state-based solution,
204
00:09:30,680 --> 00:09:33,730
which is not bad but I wanna show refs here.
205
00:09:33,730 --> 00:09:37,090
Or we do something which you should rarely do
206
00:09:37,090 --> 00:09:39,600
but which actually is okay here
207
00:09:39,600 --> 00:09:42,320
in the context of a input field value
208
00:09:42,320 --> 00:09:43,443
which you wanna reset.
209
00:09:44,300 --> 00:09:47,160
You can manipulate the DOM without React
210
00:09:47,160 --> 00:09:48,717
and yes, I said you shouldn't do that
211
00:09:48,717 --> 00:09:50,780
and you typically shouldn't do that
212
00:09:50,780 --> 00:09:53,720
but if you just wanna reset the value entered by a user,
213
00:09:53,720 --> 00:09:56,390
it is something you can consider doing.
214
00:09:56,390 --> 00:09:59,820
So we could use the nameInputRef.current.value
215
00:10:02,320 --> 00:10:04,950
and now set it to an empty string
216
00:10:04,950 --> 00:10:07,733
and repeat this for the ageInputRef.
217
00:10:09,092 --> 00:10:11,860
And with these two lines added here,
218
00:10:11,860 --> 00:10:13,523
if we now check this again,
219
00:10:15,090 --> 00:10:16,850
you see now it's cleared.
220
00:10:16,850 --> 00:10:19,440
Again, rarely do that.
221
00:10:19,440 --> 00:10:22,910
Rarely use refs to manipulate the DOM.
222
00:10:22,910 --> 00:10:25,470
Here we're not really manipulating the DOM,
223
00:10:25,470 --> 00:10:27,220
we're not adding a new element
224
00:10:27,220 --> 00:10:29,210
or changing a CSS class,
225
00:10:29,210 --> 00:10:31,730
I'm just changing what the user entered.
226
00:10:31,730 --> 00:10:34,170
You could argue that you don't even wanna do that
227
00:10:34,170 --> 00:10:36,440
and in that case, you can always return back
228
00:10:36,440 --> 00:10:38,060
to the state-based solution
229
00:10:38,060 --> 00:10:40,150
but I think it's fine.
230
00:10:40,150 --> 00:10:42,840
Now, in general, the question is not whether refs
231
00:10:42,840 --> 00:10:44,210
or state is better.
232
00:10:44,210 --> 00:10:45,948
You can use either of the two.
233
00:10:45,948 --> 00:10:48,582
You will sometimes have use cases
234
00:10:48,582 --> 00:10:52,520
where you just want to quickly read a value, for example.
235
00:10:52,520 --> 00:10:54,327
And if you only want to read a value
236
00:10:54,327 --> 00:10:56,826
and you never plan on changing anything,
237
00:10:56,826 --> 00:10:59,035
well, then you don't really need state
238
00:10:59,035 --> 00:11:02,630
because just to use state as a keylogger
239
00:11:02,630 --> 00:11:03,690
is not that great.
240
00:11:03,690 --> 00:11:06,440
It's a lot of unnecessary code and work.
241
00:11:06,440 --> 00:11:08,510
So if you just want to read a value,
242
00:11:08,510 --> 00:11:10,283
refs are probably better.
243
00:11:11,150 --> 00:11:14,350
In scenarios like this, it's up to you what you prefer.
244
00:11:14,350 --> 00:11:17,000
Refs, which are a little bit less code
245
00:11:17,000 --> 00:11:20,500
but you have this edge case of manipulating the DOM,
246
00:11:20,500 --> 00:11:23,250
or a state, which is definitely cleaner
247
00:11:23,250 --> 00:11:24,990
but is a bit more code.
248
00:11:24,990 --> 00:11:28,290
It's up to you but we will see refs throughout this course
249
00:11:28,290 --> 00:11:30,610
and you will see them in a lot of projects,
250
00:11:30,610 --> 00:11:32,460
in a lot of React projects
251
00:11:32,460 --> 00:11:36,420
because getting access to elements is quite convenient
252
00:11:36,420 --> 00:11:39,240
and as I said, they can even do a bit more than that
253
00:11:39,240 --> 00:11:41,430
but we'll see them again throughout the course.
254
00:11:41,430 --> 00:11:42,780
This is just an introduction
255
00:11:42,780 --> 00:11:45,480
and this is all you need to know about refs right now.
19316
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.