Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,130 --> 00:00:05,840
How can we clear those inputs?
2
00:00:05,840 --> 00:00:08,260
Well, that's part of the reason why I wanted
3
00:00:08,260 --> 00:00:12,740
to use state for storing the entered values.
4
00:00:12,740 --> 00:00:15,100
We could've also used the global variables
5
00:00:15,100 --> 00:00:16,850
outside of the component function.
6
00:00:16,850 --> 00:00:20,670
Otherwise, if we just wanted to persist the values,
7
00:00:20,670 --> 00:00:23,800
but by using state, we have an advantage.
8
00:00:23,800 --> 00:00:25,710
We can now implement something
9
00:00:25,710 --> 00:00:30,130
which is called two-way binding, which simply means
10
00:00:30,130 --> 00:00:34,690
that for inputs we don't just listen to changes,
11
00:00:34,690 --> 00:00:39,530
but we can also pass a new value back into the input.
12
00:00:39,530 --> 00:00:44,160
So that we can reset or change the input programmatically.
13
00:00:44,160 --> 00:00:47,170
And how do we do that? Well, it's very simple.
14
00:00:47,170 --> 00:00:50,830
All we have to do is add the value attribute,
15
00:00:50,830 --> 00:00:54,763
which is a default attribute, to this input element.
16
00:00:55,860 --> 00:00:58,640
This will set the internal value property,
17
00:00:58,640 --> 00:01:00,710
which every input element has.
18
00:01:00,710 --> 00:01:02,863
And we can set it to a new value.
19
00:01:03,820 --> 00:01:07,087
And here, I will bind this to enteredTitle.
20
00:01:08,570 --> 00:01:10,470
So now it is this two-way binding
21
00:01:10,470 --> 00:01:13,010
because now we don't just listen to changes
22
00:01:13,010 --> 00:01:15,650
in the input to update our state.
23
00:01:15,650 --> 00:01:19,010
But we also feed the state back into the input
24
00:01:19,010 --> 00:01:22,920
so that when we change the state, we also change input.
25
00:01:22,920 --> 00:01:24,760
This might sound like an infinite loop,
26
00:01:24,760 --> 00:01:25,800
but it actually isn't.
27
00:01:25,800 --> 00:01:28,590
So we won't have a problem there.
28
00:01:28,590 --> 00:01:31,130
But the advantage is that when the form is submitted
29
00:01:31,130 --> 00:01:35,237
for example, we can call setEnteredTitle.
30
00:01:36,440 --> 00:01:38,363
And set this back to an empty string,
31
00:01:39,410 --> 00:01:41,573
which also was our initial state.
32
00:01:42,680 --> 00:01:46,350
And by doing that, we override what the user entered
33
00:01:46,350 --> 00:01:48,280
after the form was submitted
34
00:01:48,280 --> 00:01:50,630
and therefore cleared the input.
35
00:01:50,630 --> 00:01:52,840
And we can do this for all inputs.
36
00:01:52,840 --> 00:01:55,950
We can set the entered amount to an empty string,
37
00:01:55,950 --> 00:02:00,950
and set the entered date to an empty string,
38
00:02:01,730 --> 00:02:05,810
and then just add to the value prop to all these inputs.
39
00:02:05,810 --> 00:02:09,479
So here I add value and point at enteredAmount
40
00:02:09,479 --> 00:02:12,460
to pass that back into the amount input.
41
00:02:12,460 --> 00:02:14,560
And do the same here for the date,
42
00:02:14,560 --> 00:02:17,293
pass the entered date back into the input.
43
00:02:19,290 --> 00:02:24,290
If we now save that and I reload, this is looking good.
44
00:02:24,960 --> 00:02:29,730
Now, we can still enter something here as it seems,
45
00:02:29,730 --> 00:02:31,790
but if I click Add Expense,
46
00:02:31,790 --> 00:02:34,240
you'll see it's all cleared again.
47
00:02:34,240 --> 00:02:36,160
And we still have our data collected
48
00:02:36,160 --> 00:02:38,490
and stored here in this object.
49
00:02:38,490 --> 00:02:41,330
So this is a neat behavior, but more importantly,
50
00:02:41,330 --> 00:02:44,570
this is also another key concept in React.
51
00:02:44,570 --> 00:02:47,060
Two-way binding is very useful
52
00:02:47,060 --> 00:02:49,050
when you're working with forms
53
00:02:49,050 --> 00:02:51,740
because it allows you to gather user input,
54
00:02:51,740 --> 00:02:53,280
but then also change it.
55
00:02:53,280 --> 00:02:55,803
For example, upon form of mission.
4363
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.