As you already know that the created above form is static. A static form is isolated and not pretty much useful. Our goal is to create a dynamic form. Let’s make the above form dynamic using React state management. To make the form dynamic I’ll create a React state, and this state will have an object that contains age and salary properties in it. Let’s do it. `const [inputFields, setInputFields] = useState([ {name: '', salary: ''} ])`. Here, the `inputFields` is a React state. Now, we can use this state to map our form fields. To use this state we must import the `useState` hook from the library. ``` import { useState } from 'react'; import './App.css'; function App() { const [inputFields, setInputFields] = useState([ { name: '', salary: '' } ]) return (
inputFields
state to the input fields by doing–
```
```
And the process is the same for the “Salary” property.
Let's imagine a real-time situation where we are in need of adding more fields to our form since our existing input fields are not sufficient. To solve the issue we may create a button named “Add More” that will create a new pair of input fields while clicking. Let’s make the button first.
```
```
Now we need a function that will work beneath this button. This function will trigger only when someone clicks the “Add More” button.
```
const addFields = () => {
}
```
And now, let’s connect the function to our button by simply adding an `onClick` event.
`<button onClick={addFields}>Add More</button>`.
So far so good! Our physical structure is ready. Now, we need to create a function that will create a new empty field every time we click the “**Add More**” button. Therefore, we need to make an object inside the `addFields` function, which will cause the result. This object will go back to the `inputFields` state while clicking and creating a new input field.
```
const addFields = () => {
let newField = { name: '', salary: '' }
}
```
Now, we can put this `newField` object inside the `inputFields` state so that it can automate the process.
```
const addFields = () => {
let newField = { name: '', salary: '' }
setInputFields([...inputFields, newfield])
}
```
Note: The spread operator (three dots **…**) set up the new fields with existing values from the first input field. Now, we can have more input fields by clicking the “**Add More**” button. Each click will add an extra pair of input fields with “Name” and “Salary” properties. Next, we want to get another feature of a dynamic form, which is the “**Submit**” button. Let’s imagine a situation when we want to provide our information and send or submit it to the concerned person/company. In this scenario must have to have a submit button below our dynamic form. In this case, we need to create a “**Submit**” button like the previous one. Let’s get a “**Submit**” ``` ``` Then again, we need to have a function, which helps to get the button triggered when we click it. This function will save the data in the console, from the input fields. It also contains a method called
e.preventDefault()
that will prevent the page from getting refreshed. So that, the information becomes safe.
```
const submit = (e) => {
e.preventDefault();
console.log(inputFields)
}
```
Afterward, integrate this function with the “**Submit**” button so that it can send the data and log it in.
```
```
Finally, add this to the form tag.
```
Mauricio has been at the forefront of technology for +15 years. He is constantly integrating new technologies including frameworks, CMS, and standard industry models. He is a pragmatic problem-solver and customizes solutions based on the best schema/language/application for each project. As the CTO at Jobsity, he ensures that his team is always up to date with the latest advances in software development by researching the software ecosystem, implementing professional development initiatives, and coordinating with new and existing clients about their needs.