Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,066 --> 00:00:05,800
Instructor: How can we clear those inputs?
2
00:00:05,800 --> 00:00:08,200
Well, that's part of the reason why I wanted
3
00:00:08,200 --> 00:00:12,700
to use state for storing the entered values.
4
00:00:12,700 --> 00:00:15,066
We could've also used the global variables
5
00:00:15,066 --> 00:00:16,800
outside of the component function.
6
00:00:16,800 --> 00:00:20,633
Otherwise, if we just wanted to persist the values,
7
00:00:20,633 --> 00:00:23,766
but by using state, we have an advantage.
8
00:00:23,766 --> 00:00:25,666
We can now implement something
9
00:00:25,666 --> 00:00:28,833
which is called two-way binding, which simply means
10
00:00:28,833 --> 00:00:30,600
which is called two-way binding, which simply means
11
00:00:30,600 --> 00:00:35,166
that for inputs we don't just listen to changes,
12
00:00:35,166 --> 00:00:40,000
but we can also pass a new value back into the input.
13
00:00:40,000 --> 00:00:44,633
So that we can reset or change the input programmatically.
14
00:00:44,633 --> 00:00:47,666
And how do we do that? Well, it's very simple.
15
00:00:47,666 --> 00:00:51,300
All we have to do is add the value attribute,
16
00:00:51,300 --> 00:00:56,333
which is a default attribute, to this input element.
17
00:00:56,333 --> 00:00:59,133
This will set the internal value property,
18
00:00:59,133 --> 00:01:01,200
which every input element has.
19
00:01:01,200 --> 00:01:04,300
And we can set it to a new value.
20
00:01:04,300 --> 00:01:09,066
And here, I will bind this to enteredTitle.
21
00:01:09,066 --> 00:01:11,166
So now it is this two-way binding
22
00:01:11,166 --> 00:01:11,200
So now it is this two-way binding
23
00:01:11,200 --> 00:01:13,733
because now we don't just listen to changes
24
00:01:13,733 --> 00:01:16,366
in the input to update our state.
25
00:01:16,366 --> 00:01:19,733
But we also feed the state back into the input
26
00:01:19,733 --> 00:01:23,633
so that when we change the state, we also change input.
27
00:01:23,633 --> 00:01:25,466
This might sound like an infinite loop,
28
00:01:25,466 --> 00:01:26,533
but it actually isn't.
29
00:01:26,533 --> 00:01:29,300
So we won't have a problem there.
30
00:01:29,300 --> 00:01:31,833
But the advantage is that when the form is submitted
31
00:01:31,833 --> 00:01:33,500
for example, we can call setEnteredTitle.
32
00:01:33,500 --> 00:01:37,600
for example, we can call setEnteredTitle.
33
00:01:37,600 --> 00:01:40,566
And set this back to an empty string,
34
00:01:40,566 --> 00:01:43,833
which also was our initial state.
35
00:01:43,833 --> 00:01:47,500
And by doing that, we override what the user entered
36
00:01:47,500 --> 00:01:49,433
after the form was submitted
37
00:01:49,433 --> 00:01:51,733
and therefore cleared the input.
38
00:01:51,733 --> 00:01:52,366
and therefore cleared the input.
39
00:01:52,366 --> 00:01:54,600
And we can do this for all inputs.
40
00:01:54,600 --> 00:01:57,700
We can set the entered amount to an empty string,
41
00:01:57,700 --> 00:02:03,466
and set the entered date to an empty string,
42
00:02:03,466 --> 00:02:08,199
and then just add to the value prop to all these inputs.
43
00:02:08,199 --> 00:02:08,400
and then just add to the value prop to all these inputs.
44
00:02:08,400 --> 00:02:12,066
So here I add value and point at enteredAmount
45
00:02:12,066 --> 00:02:15,033
to pass that back into the amount input.
46
00:02:15,033 --> 00:02:17,900
And do the same here for the date,
47
00:02:17,900 --> 00:02:18,033
And do the same here for the date,
48
00:02:18,033 --> 00:02:22,766
pass the entered date back into the input.
49
00:02:22,766 --> 00:02:28,433
If we now save that and I reload, this is looking good.
50
00:02:28,433 --> 00:02:33,200
Now, we can still enter something here as it seems,
51
00:02:33,200 --> 00:02:35,266
but if I click Add Expense,
52
00:02:35,266 --> 00:02:37,733
you'll see it's all cleared again.
53
00:02:37,733 --> 00:02:39,633
And we still have our data collected
54
00:02:39,633 --> 00:02:41,966
and stored here in this object.
55
00:02:41,966 --> 00:02:44,800
So this is a neat behavior, but more importantly,
56
00:02:44,800 --> 00:02:48,033
this is also another key concept in React.
57
00:02:48,033 --> 00:02:48,166
this is also another key concept in React.
58
00:02:48,166 --> 00:02:50,633
Two-way binding is very useful
59
00:02:50,633 --> 00:02:52,633
when you're working with forms
60
00:02:52,633 --> 00:02:55,333
because it allows you to gather user input,
61
00:02:55,333 --> 00:02:56,866
but then also change it.
62
00:02:56,866 --> 00:02:59,400
For example, upon form of mission.
4937
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.