By using the Dynamic Form Creation approach, we are going to create a new form component for the post actions and modal components to display success and error messages. We are going to reuse them in every parent component that needs those modal components. Furthermore, we are going to create all the input fields from the config file dynamically, since we want to make our code reusable. The reusability part is one of the greatest advantages of the Dynamic Form Creation approach.

So, let’s start.

If you want to see all the basic instructions and complete navigation for the .NET Core series, check out the following link: Introduction to the .NET Core series.

For the complete navigation and all the basic instructions of the React series, check out: Introduction to the React series.

For the previous part check out: React Error Handling

The source code is available at GitHub: React series – react-series-part6-end branch

This post is divided into several sections:

Success and Error Modal Window Components

Let’s start by creating the following folder structure in the component folder:

Modal folder structre - Dynamic Form Creation

First, we are going to modify the SuccessModal.js file:

This code is pretty straightforward. We use the react-bootstrap components for the modal inside the functional component. Through the props object, we send different parameters to our modal and one event as well. This event is going to close this modal once we click on the button.

Let’s modify the ErrorModal.js file in the same manner:

Finally, we need to modify the ModalStyles.css file:


Now we have our modal components which we can use for the Create, Update or any other component. We have deliberately created two modal components, even though they are almost the same because it is much more readable when we use modal components inside the parent components. You can distinguish right away what is the purpose of the modal just by looking at its name.

Creating Input Configuration

Inside the src folder, we are going to create the Utility folder and inside it a new file InputConfiguration.js. In this file, we are going to store all of our configuration settings for the input elements (name, address, dateOfBirth) which we are going to use in the create and update forms.

We have already installed the moment library, in the previous post, but if you haven’t done it, please do it (we need it for the datepicker control) with the following command:

Now let’s modify the InputConfiguration.js file:

Later on, in our create component, we are going to transform this object into an array of objects and send it to the Input component to create all of the input fields we need on the form. That array will consist of the objects (key-value pairs) where the key is going to be the name or the address or the dateOfBirth (properties from the above object) and the value is going to be the complete configuration part of the same object (type, value, element…).

Creating Inputs Elements Dynamically

The first thing we need to do is to install the react-datepicker library because we are going to use it for the dateOfBirth control.

Let’s execute the command:

React datepicker - Dynamic Form Creation

In the src folder let’s create the UI folder. Inside it, we are going to create a new folder Input and inside it the Input.js and the Input.css files:

UI input folder structure - Dymanic Form Creation

The Input.js file is going to be a functional component so let’s modify it accordingly:

For now, we are just going to import all the resources we need, initialize the input field and the error message and set up the value for that message if the control is invalid and if it should be validated and if it is touched. We don’t want to show the error if the user didn’t place the mouse cursor inside that component at all. Lastly, we are returning that input field (which is null for now) to the parent component.

Below the if statement and above the return block, we are going to add the code for populating the inputField property:

So, we switch through the element type and if it is the input type we create the input field with all the properties and events it needs. We are doing the same thing for the datePicker control. For our forms, those two input types are going to be enough, but if you for any of your projects need more controls, just simply add additional case statement.

One thing left to do is to modify the Input.css file:

In this class, we are overriding some of the datePicker native classes and adding one custom class (.datePickerControl).

That’s it, we can now continue to the CreateOwner component.

CreateOwner Component

In the containers folder and then inside the Owner folder, let’s create a new folder and name it CreateOwner. Inside create a new file CreateOwner.js.

Let’s start modifying that file:

In the code above, we import all the necessary react-bootstrap components and the returnInputConfiguration function from the InputConfiguration.js file. This component is a class component or a stateful component and in the componentWillMount lifecycle hook, we update our local state with all the form configuration. The compnentWillMount hook is going to trigger immediately before the component mounts.

Let’s add another line of code between the render and return blocks:

In the convertStateToArrayOfFormObjectsfuncition, we want to convert the ownerForm object into an array of objects to send it to the Input component. So, let’s add that function in a separate file.

Inside the Utility folder, create a new file FormUtility.js. Modify that file by adding the function for the object transformation:

Now we need to import this function inside CreateOwner.js file:

We are going to have more actions inside FormUtility.js file therefore, we are importing all of those actions in the CreateOwner.js component.

We have populated the formElementsArray, so let’s use it to send all the properties towards the Input component.

Form Elements Configuration Object

Let’s add this code to the Well tag:

In this code, we are looping through all the objects (input configurations) inside the formElementsArray and returning the Input component with all the necessary properties and events it requires. There is a function handleChangeEvent, and we are going to create this function to enable validation and two-way binding. But more on that a little bit later.

To see the result of our current actions, let’s modify the App.js file by adding another route to the CreateOwner component below the OwnerDetails route. We shouldn’t forget the import statement as well:

If we navigate to the CreateOwner page we are going to see this result:

Create from started - Dynamic Form Creation


We have created our controls, but we still can’t do anything with them yet. But as soon as we implement the handleChangeEvent function, we will be able to modify and validate our inputs.

Finalizing the CreateOwner View

Let’s add the buttons to our component, to finalize the view part.

Inside the Form tag and below the <br> tag add this code:

We add two buttons, and the Create button is disabled as long as the form is invalid.


Now our form looks like this:

Create form completed - Dynamic Form Creation

Everything is working as it’s supposed to.


By reading this post, you’ve learned:

  • How to use the Bootstrap modal elements to create reusable modal window components
  • To use the simple configuration object for form creation and how to transfer it into an array of objects
  • How to transfer a configuration array by using a simple JSX code to a Form View

Thank you for reading the article and I hope you found something useful in it.

In the next part of the series, we are going to learn how to validate our input elements. Furthermore, we are going to use the CreateComponent to send a POST request towards our .NET Core Web API server.

If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published Web Development content we encourage you to subscribe to our blog.