<?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: Fredrick Mgbeoma</title>
    <description>The latest articles on Forem by Fredrick Mgbeoma (@fuchodeveloper).</description>
    <link>https://forem.com/fuchodeveloper</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%2F69240%2Fef6a7c39-d9c7-40c9-af55-3885432c1b69.jpg</url>
      <title>Forem: Fredrick Mgbeoma</title>
      <link>https://forem.com/fuchodeveloper</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/fuchodeveloper"/>
    <language>en</language>
    <item>
      <title>How to Create a Simple Blog in 4 Minutes</title>
      <dc:creator>Fredrick Mgbeoma</dc:creator>
      <pubDate>Fri, 15 May 2020 22:28:10 +0000</pubDate>
      <link>https://forem.com/fuchodeveloper/how-to-create-a-simple-blog-in-4-minutes-56hh</link>
      <guid>https://forem.com/fuchodeveloper/how-to-create-a-simple-blog-in-4-minutes-56hh</guid>
      <description>&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;GitHub account&lt;/li&gt;
&lt;li&gt;Netlify account&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/dEga2O1JUQo"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Creating a blog to share your ideas with the rest of the world might sound like fun at first, but you might be wondering how to actually do it. With free resources available via platforms such as Gatsby, GitHub and Netlify, you can create, host and deploy your blog in just a few minutes.&lt;/p&gt;

&lt;h4&gt;
  
  
  How To:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Search for &lt;code&gt;gatsby blog starter&lt;/code&gt; on Google, or simply use the following direct link - &lt;a href="https://www.gatsbyjs.org/starters/gatsbyjs/gatsby-starter-blog"&gt;https://www.gatsbyjs.org/starters/gatsbyjs/gatsby-starter-blog&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Click on the Netlify button at the bottom of the image preview:
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C5VOSVLj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.codeisbae.com/static/bab7521a2a0895b79aefde9d623c5e05/6a03e/netlify.png" alt="Netlify button"&gt;
&lt;/li&gt;
&lt;li&gt;You will be redirected to Netlify site, click on the &lt;code&gt;Connect to GitHub&lt;/code&gt; button to authenticate via GitHub and give Netlify the permissions needed to create a public repository where your blog will be hosted.&lt;/li&gt;
&lt;li&gt;Change the repository name as desired and click on the &lt;code&gt;Save &amp;amp; Deploy&lt;/code&gt; button.&lt;/li&gt;
&lt;li&gt;After the blog is created by Netlify, you can visit it using the generated link.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pro-Tip: You can personalise the generic website name generated by Netlify by changing it under &lt;code&gt;Site settings&lt;/code&gt; on your Netlify profile or purchase a custom domain and use that if desired.&lt;/p&gt;

&lt;p&gt;If you could this post helpful, go ahead to share it! Your feedback is highly welcome.&lt;/p&gt;

</description>
      <category>gatsby</category>
      <category>github</category>
      <category>netlify</category>
    </item>
    <item>
      <title>Dynamic Form Fields in React</title>
      <dc:creator>Fredrick Mgbeoma</dc:creator>
      <pubDate>Sun, 03 Nov 2019 09:58:10 +0000</pubDate>
      <link>https://forem.com/fuchodeveloper/dynamic-form-fields-in-react-1h6c</link>
      <guid>https://forem.com/fuchodeveloper/dynamic-form-fields-in-react-1h6c</guid>
      <description>&lt;p&gt;&lt;a href="https://codeisbae.com/static/39116b007223b9c8c7fa08654d611496/02744/dynamic-forms-thumbnail.png"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xXU2TCB2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://codeisbae.com/static/39116b007223b9c8c7fa08654d611496/02744/dynamic-forms-thumbnail.png" alt="Dynamic Forms Thumbnail" title="Dynamic Forms Banner"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you’ve been building applications that accept user input via forms, you might have come across the need to make some form fields dynamic. This could be for various reasons, for instance, to add multiple members as part of a team during creation. If you’ve never been in this situation, this post might also be helpful for you at some point.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Working knowledge of React Hooks&lt;/li&gt;
&lt;li&gt;npm installed in machine&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We’ll be illustrating dynamic form fields in React using a simple form that has two input fields. One for the first name and another for the last name, these would form the user data. These two fields would be dynamically duplicated to create more fields that are unique and can accept new user data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;To quickly get started, we would be using the Create React App package, but the steps we would go over can be seamlessly integrated into your existing application.&lt;/p&gt;

&lt;p&gt;Follow the steps below to create a new React app, navigate into the app directory and start it up in development mode.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app my-app
cd my-app
npm start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Open &lt;a href="http://localhost:3000"&gt;http://localhost:3000&lt;/a&gt; to view it in the browser.&lt;/p&gt;

&lt;p&gt;For some styling, we would install Bootstrap. This step is optional and only included here to give the form some good user interface. Knowledge of Bootstrap is not required.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i bootstrap
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Deep Dive
&lt;/h2&gt;

&lt;p&gt;Open the project in your favourite text editor. Navigate to &lt;code&gt;App.js&lt;/code&gt; and replace the content with the code snippet below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, Fragment } from "react";

import "bootstrap/dist/css/bootstrap.css";

const App = () =&amp;gt; {

  return (
    &amp;lt;&amp;gt;
      &amp;lt;h1&amp;gt;Dynamic Form Fields in React&amp;lt;/h1&amp;gt;
    &amp;lt;/&amp;gt;
  )
}

export default App;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This gives us a basic template on which to build our form component.&lt;/p&gt;

&lt;p&gt;Using React Hooks, initialise the form input fields like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...

const App = () =&amp;gt; {
  const [inputFields, setInputFields] = useState([
    { firstName: '', lastName: '' }
  ]);

...

}

...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the above snippet, &lt;code&gt;inputFields&lt;/code&gt; refers to the form fields, while &lt;code&gt;setInputFields&lt;/code&gt; is the used to set the value for these form fields&lt;/p&gt;

&lt;p&gt;Proceed to setup the form by updating the &lt;code&gt;App&lt;/code&gt; component as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const App = () =&amp;gt; {
...

const handleSubmit = e =&amp;gt; {
    e.preventDefault();
    console.log("inputFields", inputFields);
  };

return (
    &amp;lt;&amp;gt;
      &amp;lt;h1&amp;gt;Dynamic Form Fields in React&amp;lt;/h1&amp;gt;
      &amp;lt;form onSubmit={handleSubmit}&amp;gt;
        &amp;lt;div className="form-row"&amp;gt;
          {inputFields.map((inputField, index) =&amp;gt; (
            &amp;lt;Fragment key={`${inputField}~${index}`}&amp;gt;
              &amp;lt;div className="form-group col-sm-6"&amp;gt;
                &amp;lt;label htmlFor="firstName"&amp;gt;First Name&amp;lt;/label&amp;gt;
                &amp;lt;input
                  type="text"
                  className="form-control"
                  id="firstName"
                  name="firstName"
                  value={inputField.firstName}
                /&amp;gt;
              &amp;lt;/div&amp;gt;
              &amp;lt;div className="form-group col-sm-4"&amp;gt;
                &amp;lt;label htmlFor="lastName"&amp;gt;Last Name&amp;lt;/label&amp;gt;
                &amp;lt;input
                  type="text"
                  className="form-control"
                  id="lastName"
                  name="lastName"
                  value={inputField.lastName}
                /&amp;gt;
              &amp;lt;/div&amp;gt;
              &amp;lt;div className="form-group col-sm-2"&amp;gt;
                &amp;lt;button
                  className="btn btn-link"
                  type="button"
                &amp;gt;
                  -
                &amp;lt;/button&amp;gt;
                &amp;lt;button
                  className="btn btn-link"
                  type="button"
                &amp;gt;
                  +
                &amp;lt;/button&amp;gt;
              &amp;lt;/div&amp;gt;
            &amp;lt;/Fragment&amp;gt;
          ))}
        &amp;lt;/div&amp;gt;
        &amp;lt;div className="submit-button"&amp;gt;
          &amp;lt;button
            className="btn btn-primary mr-2"
            type="submit"
            onSubmit={handleSubmit}
          &amp;gt;
            Save
          &amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/form&amp;gt;
    &amp;lt;/&amp;gt;
  )

}

...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If that looks like a lot of code thrown right at you, don’t worry, we’ll go over it. We have the function &lt;code&gt;handleSubmit&lt;/code&gt; to log the value returned when the form is submitted. Here we are simply logging to the console, but you could use the data here for whatever you need depending on your specific use case. The JavaScript &lt;code&gt;map&lt;/code&gt; function is used to create an array of the form fields. &lt;code&gt;Fragment&lt;/code&gt; from React allows us to group a list of children without adding a new node to the DOM.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Functionality to the Form
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Handling Changes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At this point, the basic UI is done that means we are getting closer to our goal, but there is no functionality yet. Roll up your sleeves, let’s get to work on the functionality!&lt;/p&gt;

&lt;p&gt;Update the input fields to include a change handler to cater for user input action. See the change below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...

  &amp;lt;input
...
   onChange={event =&amp;gt; handleInputChange(index, event)}
  /&amp;gt;

...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;handleInputChange&lt;/code&gt; function does not exist. Let’s create it in the &lt;code&gt;App&lt;/code&gt; component right after &lt;code&gt;handleSubmit&lt;/code&gt;. See below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
const handleInputChange = (index, event) =&amp;gt; {
    const values = [...inputFields];
    if (event.target.name === "firstName") {
      values[index].firstName = event.target.value;
    } else {
      values[index].lastName = event.target.value;
    }

    setInputFields(values);
  };
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the above code snippet, we spread &lt;code&gt;inputFields&lt;/code&gt; and do a simple check for the input field based on the &lt;code&gt;name&lt;/code&gt; attribute of that field. Then we supply the value for the given index. The index is derived from the &lt;code&gt;map&lt;/code&gt; function we used previously.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adding and Removing Form Fields&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now comes the interesting part. Right after the &lt;code&gt;handleInputChange&lt;/code&gt;, add the following code snippet to handling adding and removing form fields:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...

  const handleAddFields = () =&amp;gt; {
    const values = [...inputFields];
    values.push({ firstName: '', lastName: '' });
    setInputFields(values);
  };

  const handleRemoveFields = index =&amp;gt; {
    const values = [...inputFields];
    values.splice(index, 1);
    setInputFields(values);
  };

...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To make use of these two new functions, update the &lt;code&gt;-&lt;/code&gt; and &lt;code&gt;+&lt;/code&gt; buttons to include click handlers to add and remove form fields.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
              &amp;lt;div className="form-group col-sm-2"&amp;gt;
                &amp;lt;button
                  className="btn btn-link"
                  type="button"
                  onClick={() =&amp;gt; handleRemoveFields(index)}
                &amp;gt;
                  -
                &amp;lt;/button&amp;gt;
                &amp;lt;button
                  className="btn btn-link"
                  type="button"
                  onClick={() =&amp;gt; handleAddFields()}
                &amp;gt;
                  +
                &amp;lt;/button&amp;gt;
              &amp;lt;/div&amp;gt;
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Aside:&lt;/em&gt; To see a preview of the form input values, add the following code snippet before the closing form tag &lt;code&gt;&amp;lt;/form&amp;gt;:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
&amp;lt;br/&amp;gt;
&amp;lt;pre&amp;gt;
 {JSON.stringify(inputFields, null, 2)}
&amp;lt;/pre&amp;gt;
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There you go! To add more form fields, click on &lt;code&gt;+&lt;/code&gt;, to remove click on &lt;code&gt;-&lt;/code&gt;. You can implement this functionality in your existing applications or new ones.&lt;/p&gt;

&lt;p&gt;Preview:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/epic-fire-3fjbd"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Free free to share this with your team!&lt;/p&gt;

&lt;p&gt;&lt;small&gt;Originally published at: &lt;a href="https://codeisbae.com/dynamic-form-fields-in-react/"&gt;https://codeisbae.com/dynamic-form-fields-in-react/&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>webforms</category>
      <category>webdev</category>
      <category>reacthooks</category>
    </item>
  </channel>
</rss>
