<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Antonio Maina</title>
    <description>The latest articles on Forem by Antonio Maina (@r0b0tt).</description>
    <link>https://forem.com/r0b0tt</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F96189%2F3fd4e4c7-8ecb-486e-9331-5a73ff8af143.jpg</url>
      <title>Forem: Antonio Maina</title>
      <link>https://forem.com/r0b0tt</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/r0b0tt"/>
    <language>en</language>
    <item>
      <title>Handling forms in React</title>
      <dc:creator>Antonio Maina</dc:creator>
      <pubDate>Mon, 19 Oct 2020 07:06:38 +0000</pubDate>
      <link>https://forem.com/r0b0tt/handling-forms-in-react-4cb7</link>
      <guid>https://forem.com/r0b0tt/handling-forms-in-react-4cb7</guid>
      <description>&lt;p&gt;As a web developer, you have probably interacted with HTML &lt;a href="https://www.w3schools.com/html/html_forms.asp" rel="noopener noreferrer"&gt;forms&lt;/a&gt;. Forms provide an interface for us to collect data from users and then perform various actions using that data. For example, a signup form collects registration data such as email, name and password from users and creates accounts for them.&lt;/p&gt;

&lt;p&gt;In this article, we will learn how to handle forms using  &lt;a href="https://reactjs.org/" rel="noopener noreferrer"&gt;ReactJs&lt;/a&gt;. This article assumes that you have basic knowledge of  &lt;a href="https://reactjs.org/tutorial/tutorial.html" rel="noopener noreferrer"&gt;ReactJs&lt;/a&gt; and  &lt;a href="https://reactjs.org/docs/hooks-intro.html" rel="noopener noreferrer"&gt;React Hooks&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Here is the source code for this article. &lt;a href="https://github.com/r0b0tt/react-form-handling" rel="noopener noreferrer"&gt;https://github.com/r0b0tt/react-form-handling&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to React Forms
&lt;/h2&gt;

&lt;p&gt;Basically, ReactJs has two types of form elements,  &lt;a href="https://reactjs.org/docs/forms.html#controlled-components" rel="noopener noreferrer"&gt;controlled&lt;/a&gt; and  &lt;a href="https://reactjs.org/docs/uncontrolled-components.html" rel="noopener noreferrer"&gt;uncontrolled&lt;/a&gt;. According to the official documentation, controlled elements control their own state and update them based on user input. Uncontrolled elements, on the other hand, rely on the  &lt;a href="https://www.w3schools.com/js/js_htmldom.asp" rel="noopener noreferrer"&gt;DOM&lt;/a&gt; to handle the form data. The recommended way, however, is to use controlled components.&lt;/p&gt;

&lt;h2&gt;
  
  
  Form Setup
&lt;/h2&gt;

&lt;p&gt;For this article, we will be building a basic registration form for a careers website. Here is the final design. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602945331382%2FC6oTByOqv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602945331382%2FC6oTByOqv.png" alt="localhost_3000_.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling Form Input
&lt;/h2&gt;

&lt;p&gt;Whenever a user enters information in our form elements' fields, we need to store it in some sort of state. If you're unfamiliar with state management in ReactJs you can checkout out  &lt;a href="https://kentcdodds.com/blog/application-state-management-with-react" rel="noopener noreferrer"&gt;this&lt;/a&gt; article by Kent C. Dodds who explains it in a very simple manner. &lt;br&gt;
In this article, we will be using the  &lt;a href="https://reactjs.org/docs/hooks-reference.html#usestate" rel="noopener noreferrer"&gt;useState&lt;/a&gt; hook to store the various user details.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602949223390%2Fqrw1i4U0B.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602949223390%2Fqrw1i4U0B.png" alt="carbon.png"&gt;&lt;/a&gt;&lt;br&gt;
From the image above, you can see that we have stored all registration details using the &lt;code&gt;useState&lt;/code&gt; hook. We have initialized some fields so as to give default values to some of the form elements. After setting up our state, we can now get user input from our form elements and store it in our &lt;code&gt;userDetails&lt;/code&gt; state.&lt;/p&gt;

&lt;p&gt;Right now, our form elements are uncontrolled components and we need to convert them to controlled components. We do this by adding the &lt;code&gt;value&lt;/code&gt; and &lt;code&gt;onChange&lt;/code&gt; properties to the elements. The images below show how we add the attributes to the input, select and checkbox elements respectively. &lt;strong&gt;Note that the values are being fetched from our userDetails state.&lt;/strong&gt; You can view the other elements in the  &lt;a href="https://github.com/r0b0tt/react-form-handling" rel="noopener noreferrer"&gt;source code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602949827399%2F9SKrfbbui.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602949827399%2F9SKrfbbui.png" alt="input.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602949832786%2FQseaWUwNP.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602949832786%2FQseaWUwNP.png" alt="select.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602949944834%2FixV6ZD9ZI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602949944834%2FixV6ZD9ZI.png" alt="checkbox.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you might have noticed, we have added an extra attribute on the checkbox elements called &lt;code&gt;checked&lt;/code&gt;. This attribute is used to set the checked state of the checkbox. In our case, it checks whether the current value exists in our opportunities array that is in our userDetails state. Another important attribute here is the &lt;code&gt;name&lt;/code&gt; attribute. It is used to reference that particular element. You can learn more about it  &lt;a href="https://www.w3schools.com/tags/att_name.asp#:~:text=The%20name%20attribute%20specifies%20a,to%20target%20a%20form%20submission" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;After converting our elements to controlled components, we can now implement our &lt;code&gt;onchange&lt;/code&gt; handler. An  &lt;a href="https://www.w3schools.com/jsref/event_onchange.asp" rel="noopener noreferrer"&gt;onchange&lt;/a&gt; event occurs when the value of an element is changed. ReactJs exposes an &lt;code&gt;onChange&lt;/code&gt; property on all form elements whereby we pass a function to handle input change. &lt;br&gt;
Here is the function we have implemented.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602951420473%2FOOZdnr1Hd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602951420473%2FOOZdnr1Hd.png" alt="handleInputChange.png"&gt;&lt;/a&gt;&lt;br&gt;
Our &lt;code&gt;handleInputChange&lt;/code&gt; function takes the &lt;code&gt;onchange&lt;/code&gt; event as a parameter. We then use object  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment" rel="noopener noreferrer"&gt;destructuring&lt;/a&gt; to get the &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;value&lt;/code&gt; of the current form element from the &lt;code&gt;onchange&lt;/code&gt; event.&lt;/p&gt;

&lt;p&gt;The default behaviour will be to update our state using the &lt;code&gt;setUserDetails&lt;/code&gt; function exposed by our &lt;code&gt;useState&lt;/code&gt; hook. As you might have noticed, the &lt;code&gt;name&lt;/code&gt; attributes of our form elements match the keys in our &lt;code&gt;userDetails&lt;/code&gt; state. This allows easy and seamless updating of our state using the  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax" rel="noopener noreferrer"&gt;spread operator&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;One special case here is our opportunities' checkbox elements. We have three values namely contract, partTime and fullTime. We have hardcoded these values to the elements. &lt;/p&gt;

&lt;p&gt;In this case, we check whether the current checkbox is checked. If so, we add its value to our opportunities array, else, we find its index in the opportunities array using the  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex" rel="noopener noreferrer"&gt;findIndex&lt;/a&gt; method and remove it from the array using the  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice" rel="noopener noreferrer"&gt;splice&lt;/a&gt; method. Finally, we use the &lt;code&gt;spread operator&lt;/code&gt; to update our state with the new opportunities selected.&lt;br&gt;
In our checkbox elements, we display the checked status by checking whether the value of the checkbox is contained in our opportunities array using the  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes" rel="noopener noreferrer"&gt;includes&lt;/a&gt; method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling Form Submission
&lt;/h2&gt;

&lt;p&gt;After storing user input from our registration form, we should now handle what happens when the user clicks the create account button. &lt;/p&gt;

&lt;p&gt;When the user clicks the button, the form details are submitted. This emits an  &lt;a href="https://www.w3schools.com/jsref/event_onsubmit.asp" rel="noopener noreferrer"&gt;onsubmit&lt;/a&gt; event from the form. We will implement a function to handle our form submission.&lt;/p&gt;

&lt;p&gt;First of all, we add the &lt;code&gt;onSubmit&lt;/code&gt; attribute exposed by ReactJs to our form element.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602951999012%2FqjtuHO_fA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602951999012%2FqjtuHO_fA.png" alt="carbon (7).png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then we implement the function we have passed.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602952186992%2FMsDyVacKX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1602952186992%2FMsDyVacKX.png" alt="carbon (8).png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our function above, we prevent the default behaviour when our form is submitted using the  &lt;a href="https://www.w3schools.com/jsref/event_preventdefault.asp" rel="noopener noreferrer"&gt;preventDefault&lt;/a&gt; method and then log our user details to the console. &lt;/p&gt;

&lt;p&gt;In real-life scenarios, you would probably send the data to your servers so as to create that users account.&lt;/p&gt;




&lt;p&gt;I hope this article was educative.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>forms</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
