Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,240 --> 00:00:04,010
Now to handle the form submission,
2
00:00:04,010 --> 00:00:06,820
we got to do two main things,
3
00:00:06,820 --> 00:00:09,780
we have to listen to the form submission.
4
00:00:09,780 --> 00:00:13,400
And we then actually have to prevent the browser default,
5
00:00:13,400 --> 00:00:17,170
which is that it sends an HTTP request automatically,
6
00:00:17,170 --> 00:00:19,210
and hence it would reload the page.
7
00:00:19,210 --> 00:00:22,950
We wanna prevent this and instead handle the submission
8
00:00:22,950 --> 00:00:26,170
with JavaScript with react.
9
00:00:26,170 --> 00:00:28,120
And in addition to that, we of course,
10
00:00:28,120 --> 00:00:30,800
need to read the entered values,
11
00:00:30,800 --> 00:00:34,193
we need to get the actual input the user entered.
12
00:00:35,300 --> 00:00:37,890
Let's start with the form submission.
13
00:00:37,890 --> 00:00:39,380
To listen for that submission,
14
00:00:39,380 --> 00:00:42,730
we can add the on submit prop on the form,
15
00:00:42,730 --> 00:00:46,450
because by default, a submit event will be emitted
16
00:00:46,450 --> 00:00:49,730
if you have a button in a form and that button is clicked.
17
00:00:49,730 --> 00:00:52,230
And we can then catch this submit event here
18
00:00:52,230 --> 00:00:56,960
and run our own logic with help of that on submit prop.
19
00:00:56,960 --> 00:00:59,200
Now I will add another nested function
20
00:00:59,200 --> 00:01:02,010
in the new meetup forum component function,
21
00:01:02,010 --> 00:01:05,930
let's say the submit handler, which should be triggered
22
00:01:05,930 --> 00:01:10,250
when the submit event occurs by connecting function
23
00:01:10,250 --> 00:01:12,133
and on submit like this.
24
00:01:13,300 --> 00:01:15,540
But as I mentioned, a couple of seconds ago,
25
00:01:15,540 --> 00:01:19,510
the default behavior of the browser would actually be
26
00:01:19,510 --> 00:01:20,960
that it sends a request
27
00:01:20,960 --> 00:01:24,870
to the server serving this page automatically.
28
00:01:24,870 --> 00:01:28,460
And we don't want that we don't want that browser default.
29
00:01:28,460 --> 00:01:31,170
Instead, we wanna prevent that default,
30
00:01:31,170 --> 00:01:34,360
and run our own JavaScript logic.
31
00:01:34,360 --> 00:01:36,370
That might and still involve
32
00:01:36,370 --> 00:01:38,940
that we send that HTTP request later.
33
00:01:38,940 --> 00:01:42,620
But we wanna send that request behind the scenes
34
00:01:42,620 --> 00:01:44,760
without reloading the page,
35
00:01:44,760 --> 00:01:46,330
which is what would happen
36
00:01:46,330 --> 00:01:49,160
if the browser does its default thing.
37
00:01:49,160 --> 00:01:53,650
Now, thankfully, preventing that browser default is simple.
38
00:01:53,650 --> 00:01:57,650
We will get an event object automatically here.
39
00:01:57,650 --> 00:02:01,420
Because all those built in events like on click, on submit
40
00:02:01,420 --> 00:02:04,170
and all the other events to which we can listen,
41
00:02:04,170 --> 00:02:05,610
for all those events,
42
00:02:05,610 --> 00:02:09,530
react will automatically pass an event argument
43
00:02:09,530 --> 00:02:13,350
into the function that is executed for those events.
44
00:02:13,350 --> 00:02:16,890
This argument which we can accept here as a parameter,
45
00:02:16,890 --> 00:02:21,890
and this event object will have a prevent default method,
46
00:02:22,400 --> 00:02:25,873
which we can call to prevent the browser default.
47
00:02:26,740 --> 00:02:29,390
That's actually not even react specific,
48
00:02:29,390 --> 00:02:31,810
this event object and this method,
49
00:02:31,810 --> 00:02:34,130
that is a Vanilla JavaScript,
50
00:02:34,130 --> 00:02:36,773
which is fully supported by react.
51
00:02:37,870 --> 00:02:40,550
So that will prevent that browser default,
52
00:02:40,550 --> 00:02:43,970
and will allow us to fully handle the submission
53
00:02:43,970 --> 00:02:46,833
with just JavaScript and react.
54
00:02:48,080 --> 00:02:49,960
So that's step one.
55
00:02:49,960 --> 00:02:54,453
Now we need to find a way of reading those entered values.
56
00:02:55,290 --> 00:02:58,040
For reading the entered values,
57
00:02:58,040 --> 00:03:01,380
we got two main ways of handling this,
58
00:03:01,380 --> 00:03:05,070
we can use use state again,
59
00:03:05,070 --> 00:03:10,070
and add the on change event listener to all those inputs,
60
00:03:10,290 --> 00:03:12,330
which will in the end trigger a function
61
00:03:12,330 --> 00:03:17,330
for every keystroke, and then we can extract that value,
62
00:03:17,340 --> 00:03:19,560
the user entered from the event object
63
00:03:19,560 --> 00:03:21,520
which will receive for that event,
64
00:03:21,520 --> 00:03:24,260
and update our state for that given input
65
00:03:24,260 --> 00:03:26,470
with the entered value.
66
00:03:26,470 --> 00:03:28,170
That would allow us to keep track
67
00:03:28,170 --> 00:03:31,900
of what the user entered with every keystroke.
68
00:03:31,900 --> 00:03:35,800
But here I'm actually only interested in the user input once
69
00:03:35,800 --> 00:03:37,650
when the form is submitted.
70
00:03:37,650 --> 00:03:38,483
And for this,
71
00:03:38,483 --> 00:03:41,160
we can use a number a concept built into react,
72
00:03:41,160 --> 00:03:43,390
the concept of refs.
73
00:03:43,390 --> 00:03:45,880
Now ref stands for reference.
74
00:03:45,880 --> 00:03:47,770
And react simply allows us
75
00:03:47,770 --> 00:03:52,220
to set up references cue DOM elements,
76
00:03:52,220 --> 00:03:56,640
so we can get direct access to DOM elements.
77
00:03:56,640 --> 00:04:00,040
To set up such a reference such a connection,
78
00:04:00,040 --> 00:04:05,040
we first of all have to import the use ref hook from react.
79
00:04:05,450 --> 00:04:07,260
So just like use state,
80
00:04:07,260 --> 00:04:11,590
that's another built in special function offered by react
81
00:04:11,590 --> 00:04:16,040
which we can execute in our functional components.
82
00:04:16,040 --> 00:04:18,810
And we can execute use ref here.
83
00:04:18,810 --> 00:04:23,697
And with that, we create a ref object, a reference object
84
00:04:23,697 --> 00:04:27,340
and we can store this in a constant and we could name it,
85
00:04:27,340 --> 00:04:31,610
title input ref, because my first ref here should be
86
00:04:31,610 --> 00:04:33,443
for this title input.
87
00:04:34,440 --> 00:04:37,710
Now to connect this created object to this input,
88
00:04:37,710 --> 00:04:41,780
we add another special prop to this HTML element.
89
00:04:41,780 --> 00:04:45,220
And now very special prop besides the key prop,
90
00:04:45,220 --> 00:04:46,800
which is built into react
91
00:04:46,800 --> 00:04:51,070
and supported on all elements out of the box,
92
00:04:51,070 --> 00:04:53,890
and that's the ref prop.
93
00:04:53,890 --> 00:04:56,800
We add the ref prop to this input
94
00:04:56,800 --> 00:05:00,663
and as a value, we point at this title input ref,
95
00:05:01,550 --> 00:05:05,090
this will establish a connection and will give us access
96
00:05:05,090 --> 00:05:08,573
to this input element through this ref object.
97
00:05:09,590 --> 00:05:12,290
So here in this submit handler,
98
00:05:12,290 --> 00:05:15,140
we can then get the entered title.
99
00:05:15,140 --> 00:05:18,320
So the concrete value the user entered
100
00:05:18,320 --> 00:05:20,693
by using that title input ref.
101
00:05:21,640 --> 00:05:24,510
And then they're actually dot current,
102
00:05:24,510 --> 00:05:29,010
all those ref objects created with the use ref, were x such
103
00:05:29,010 --> 00:05:31,030
that they have a current property,
104
00:05:31,030 --> 00:05:34,530
which then holds the actual connected value.
105
00:05:34,530 --> 00:05:35,870
And therefore it's not current,
106
00:05:35,870 --> 00:05:38,530
which then holds this input element object,
107
00:05:38,530 --> 00:05:42,780
the JavaScript representation of that input element.
108
00:05:42,780 --> 00:05:47,250
And all those input elements have a value property.
109
00:05:47,250 --> 00:05:49,510
That's just how JavaScript works.
110
00:05:49,510 --> 00:05:53,470
The JavaScript object representing an input element
111
00:05:53,470 --> 00:05:55,430
has a value property.
112
00:05:55,430 --> 00:06:00,020
And that value property holds the currently entered value
113
00:06:00,020 --> 00:06:02,870
of that input.
114
00:06:02,870 --> 00:06:05,713
So that's how we can extract what the user entered.
115
00:06:06,790 --> 00:06:09,350
We could also change it,
116
00:06:09,350 --> 00:06:13,700
we could set value to some new value.
117
00:06:13,700 --> 00:06:16,270
But we shouldn't really do that.
118
00:06:16,270 --> 00:06:18,820
If you wanna change what's output on the screen,
119
00:06:18,820 --> 00:06:21,480
use state for data and stat.
120
00:06:21,480 --> 00:06:25,540
But for reading input, for reading values,
121
00:06:25,540 --> 00:06:28,340
refs can be a very useful tool.
122
00:06:28,340 --> 00:06:31,823
And here we are reading what the user entered.
123
00:06:33,280 --> 00:06:36,370
Now, we can repeat that for the other inputs,
124
00:06:36,370 --> 00:06:38,390
simply by creating more reps,
125
00:06:38,390 --> 00:06:42,270
you can have more than one ref per component.
126
00:06:42,270 --> 00:06:47,270
So here, I also create the image, input ref, like this,
127
00:06:50,430 --> 00:06:54,370
and I'll already copy that a couple of times,
128
00:06:54,370 --> 00:06:57,710
and also create the address input ref
129
00:06:57,710 --> 00:07:02,070
and the description, input ref.
130
00:07:02,070 --> 00:07:06,340
And then we connect those refs with the ref property.
131
00:07:06,340 --> 00:07:10,380
Here on the image, we connect this to image input ref.
132
00:07:10,380 --> 00:07:14,300
For the address, we connect us to address input ref.
133
00:07:14,300 --> 00:07:18,400
And on the text area, which is there to enter a description,
134
00:07:18,400 --> 00:07:20,883
we connect the description input ref.
135
00:07:23,200 --> 00:07:25,500
Now with that, we can extract all
136
00:07:25,500 --> 00:07:28,720
those entered values in the submit handler.
137
00:07:28,720 --> 00:07:32,730
So we get the entered image with whips
138
00:07:32,730 --> 00:07:35,913
with image input ref.current.value,
139
00:07:37,270 --> 00:07:40,440
we got the entered address
140
00:07:40,440 --> 00:07:43,713
with the address input ref.current.value.
141
00:07:44,570 --> 00:07:47,890
And we got the entered description
142
00:07:47,890 --> 00:07:52,163
by using the description input ref current.value.
143
00:07:53,730 --> 00:07:57,430
And that then allows us to create a new object,
144
00:07:57,430 --> 00:08:01,803
let's say, the meetup data object,
145
00:08:02,860 --> 00:08:06,240
which then has a couple of keys, like let's say title,
146
00:08:06,240 --> 00:08:09,630
and stores those entered values as values.
147
00:08:09,630 --> 00:08:13,270
So the entered title for the title key, for the image key,
148
00:08:13,270 --> 00:08:15,640
I store the entered image,
149
00:08:15,640 --> 00:08:19,550
for the address key, let's say we store the entered address.
150
00:08:19,550 --> 00:08:21,330
And for the description key,
151
00:08:21,330 --> 00:08:23,683
we stored the entered description.
152
00:08:24,770 --> 00:08:28,770
And now for the moment, I'll simply lock that meetup data
153
00:08:28,770 --> 00:08:33,453
with console log, soon, we will send it to a server instead.
154
00:08:34,400 --> 00:08:37,210
But for the moment, let's just see if that works.
155
00:08:37,210 --> 00:08:38,610
And hence if I now go back
156
00:08:38,610 --> 00:08:41,893
and open up the dev tools here and reload,
157
00:08:42,850 --> 00:08:46,293
we can enter something here like test.
158
00:08:47,200 --> 00:08:49,490
And then here's some URL.
159
00:08:49,490 --> 00:08:53,150
Let me just grab that dummy image URL
160
00:08:53,150 --> 00:08:57,480
from my dummy meetup data in the old meetups JS file,
161
00:08:57,480 --> 00:09:00,930
and insert data here without quotes,
162
00:09:00,930 --> 00:09:05,303
some test address 512345 test city
163
00:09:07,370 --> 00:09:09,850
and then this is a test.
164
00:09:09,850 --> 00:09:12,220
And if we now click add meetup,
165
00:09:12,220 --> 00:09:14,860
we see this object here on the right,
166
00:09:14,860 --> 00:09:18,420
which is the object with all the entered data.
167
00:09:18,420 --> 00:09:21,530
So submitting the form and handling the submission,
168
00:09:21,530 --> 00:09:23,530
preventing the browser default
169
00:09:23,530 --> 00:09:26,210
and gathering all the user input.
170
00:09:26,210 --> 00:09:28,240
All of that works now.
171
00:09:28,240 --> 00:09:32,500
And that is how we can handle forms with react.
172
00:09:32,500 --> 00:09:33,333
Now of course,
173
00:09:33,333 --> 00:09:36,520
the goal is not to just lock the data here though.
174
00:09:36,520 --> 00:09:39,520
Instead, we now want to send it
175
00:09:39,520 --> 00:09:43,563
to a server which then stores it in a database.
13985
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.