If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
00:00
Now let me know how to work with various form fields within the browser. The next step is to collect the values from these fields and then submit them off to, for example, some backend. And that's exactly what we will do in this lesson. And along the way, also look at best practices for the form component within React and the browser. So let's go. Let's create some basic controlled fields within the ui. We create a state for a username and a separate state for the password. Now to create a form within the browser, we use the form element, and within this form we create the various input elements. We create one for the username,
00:31
and then we create one for the password. And of course, as we are already familiar, we'll be controlling them with the state variables of username and password. All of the properties that we are using right now should be familiar to you. The only new one here is the name field, which we are using on the different inputs. And the use case for this will become evident once we see the browser native behavior for the form element. Now within a form element, you can actually have a special button of type submit, and this is the button that will get triggered if the user tries to, for example, of course, click the button or press an enter key on any of the inputs
01:04
to trigger a form submission. Finally, just to help us debug our application a bit better, we will display the currently stored state values of username and password. So let's jump into the browser and look at our application. In practice, it's going to be pretty simple. You can provide a username, you can provide a password, and you can see that these values are being controlled from React right now. Now let's try and submit the form by pressing the submit button and you see that the page actually reloads all of our status lost. So let's try and debug this and see the default behavior
01:36
of the form component within the browser. One thing that you can immediately notice is that the URL of the page has changed. It now contains the URL parameters of username is equal to basar and password is equal to unsafe. The names, username and password are actually the name props that we passed to the individual input elements. Let's go full screen and open up the developer tools to discuss this behavior of the form component a bit more. Again, we provide a username and we provide a password. And now let's just submit the form again. And you can see that the browser actually does a full reload
02:11
with the same URL, but passes in the parameters, which is of course, username and the password. Now with React, you do want all of the accessibility features of the form element. For example, the default enter key handling, but you most likely do not want this reloading of the whole page With React, you will most likely want to execute some custom JavaScript when the user attempts to submit a form. And then within that JavaScript do something like make an HGTP request off to some backend API. To submit that, Tara, now I have a very simple H GT P server running over here on local host 80 80, and this is an Echo server.
02:45
So whatever data you send to it is exactly what it sends back. So if I send in the string hello, it replies with the string. Hello. So let's use this local host 80 80 API to send some data from a form within React. Third. Jumping back to our application, we are going to remove the name props because they're only really used for the default behavior and there's not much needed for them. For input elements in a React application to handle a form submission within React, we use the on submit prop on a form element, the callback function that we provide gets passed back an
03:19
event and we can actually prevent the default behavior of the browser of reloading the page by invoking prevent default. On this event with the browser default behavior prevented, let's implement our own requirement of making an HTT P request to local host 80 80 with the method post to providing in the payload of a username and password. And for this purpose, we are using the browser built-in fetch API. Once the request completes, we get back the response and then passe it as Jason and log it to the console. With these changes in place, let's jump back
03:51
to our application, provide a username and a password, and then submit the form again by pressing the enter key. You can see that this time the page doesn't reload. We still have our state with us and a new HTDP call has been made to local host. And if you look at the request, you can see that it contains the payload that we provided, which is the username and the password. And if you look at the response, you can see the server echoes back the data to us. So we know that the correct data did make its way to the server. And additionally we can see the response being logged on the console as well.
04:22
Now at this point, pretty much the sky is the limit. You can do whatever you want within the form submission. This form is now completely within your react control. Now that we know how the browser form component works, it's a good idea to wrap it up into a custom component and we'll create a very basic one that I've seen being used in a number of projects. So we create a new module called form.sx, and let's just define the props for our form component. And of course, the first prop that we will have will be on submit. And a neat feature of this form component will be that it'll automatically prevent the browser default
04:55
behavior of reload. So you don't need to worry about that if you are going to use this component. The other prop that this component takes is the children. And then within the rendering of the form component, it is going to be pretty simple. It's going to take the props and then render out a browser native form element, and on submit, make sure that the default behavior is prevented, then invoke our props on submit. And the final piece of the puzzle is that it'll render out the children that are passed in. Additional advantages of building your own component can be that you can add additional logic over here and of course add some common styling for the layout
05:28
of the children if you wanted to. Now with this simple form component created, let's jump into our main application and replace the form element with this form component. And now on submit, we no longer need the event or prevent default using the event. This might not feel like a big saving right now, but of course when you do this again and again, it does become a bit annoying. So it's good to have a component that takes care of that for us. A common issue I find in applications out there is a missing type attribute on buttons. And although it's fine for simple examples, it breaks badly when used in a form component.
06:03
To demonstrate this issue, consider a simple UI with a single username field. Within the rendering of our application, we have a form and on submit, we will simply log to the console that a form has been submitted. We display a label and an input for the username. And then we have three simple buttons within a div, and they're designed to modify the username to some default values, for example, alpha, PTA and Kama. Finally, we have the form submit button and we make sure that this is a submit button. By saying that this is of type submit, it might not seem like it,
06:35
but at this point our application has a pretty critical bug. You can jump into the ui, provide a username, and then of course submit the form or pressing the submit button. But what you might not realize is that a submission will also happen as a side effect of clicking the three buttons, which were designed to only change the username to default values. And we can see these submissions being logged to the console. And the reason why this is happening is because buttons by default have the behavior of type submit. So all of these buttons are actually also submit buttons. So in addition of invoking on click,
07:09
they will also submit any parent form. The fix of course, is pretty simple. Make sure that we provide a type button for button elements if we don't want them to turn into form submissions. And now with this simple change in place when we jump to the ui, the button clicks on alpha, beta and gamma only trigger the on click and only the submit button triggers to submission. As we can see in the console, it's a common interaction to disable the form in the presence of validation errors. So let's take a look at some best practices for doing so to demonstrate some validation.
07:41
Consider a simple field for a message, and we want to make sure that the message is not empty. And if it is empty, people will display the error message is required. A UI will consist of a simple form where we will log the message value on form submission and then render out a label and an input via to the message. And if there is an error, we will also display a simple dev of color purple containing the error message. Finally, we have a trusted submit button for the form. If we jump to the ui, of course it's going
08:13
to start off in an error state because we haven't provided a message. So let's just fix that by providing a message and then press the enter key to submit the form. And you can see that the submission succeeds. However, if there is an error, we can still submit the form by pressing the enter key. And the reason, of course is that we haven't disabled the form in the presence of validation errors. Now unfortunately, there is no disabled attribute on a form within the browser, but there is one on a button and that is how you disable a form. You disable the submit button within the form,
08:47
and then none of the interactions like pressing an enter key on an input or clicking the button will trigger a form submission. In Our case, we want to disable the form if the error is toothy. So let's demonstrate that by jumping back into the ui, we can provide a value for the message string. And of course, now we can submit the form by pressing the enter key, but if we get rid of the message, we get that error display. And of course the submit button is disabled. And additionally, an enter key on the input doesn't trigger a submission either. And that's it for this lesson on using the form
09:19
component within React. Thank you for joining me and I'll see you in the next one.