create dynamic fields in react

Solutions on MaxInterview for create dynamic fields in react by the best coders in the world

showing results for - "create dynamic fields in react"
Danika
04 Aug 2016
1import React, { useState } from "react";
2
3function App() {
4  const [inputList, setInputList] = useState([{ firstName: "", lastName: "" }]);
5
6  // handle input change
7  const handleInputChange = (e, index) => {
8    const { name, value } = e.target;
9    const list = [...inputList];
10    list[index][name] = value;
11    setInputList(list);
12  };
13
14  // handle click event of the Remove button
15  const handleRemoveClick = index => {
16    const list = [...inputList];
17    list.splice(index, 1);
18    setInputList(list);
19  };
20
21  // handle click event of the Add button
22  const handleAddClick = () => {
23    setInputList([...inputList, { firstName: "", lastName: "" }]);
24  };
25
26  return (
27    <div className="App">
28      <h3><a href="https://cluemediator.com">Clue Mediator</a></h3>
29      {inputList.map((x, i) => {
30        return (
31          <div className="box">
32            <input
33              name="firstName"
34              placeholder="Enter First Name"
35              value={x.firstName}
36              onChange={e => handleInputChange(e, i)}
37            />
38            <input
39              className="ml10"
40              name="lastName"
41              placeholder="Enter Last Name"
42              value={x.lastName}
43              onChange={e => handleInputChange(e, i)}
44            />
45            <div className="btn-box">
46              {inputList.length !== 1 && <button
47                className="mr10"
48                onClick={() => handleRemoveClick(i)}>Remove</button>}
49              {inputList.length - 1 === i && <button onClick={handleAddClick}>Add</button>}
50            </div>
51          </div>
52        );
53      })}
54      <div style={{ marginTop: 20 }}>{JSON.stringify(inputList)}</div>
55    </div>
56  );
57}
58
59export default App;
60
queries leading to this page
edit form with pluss button in reactjsreact class component dynamic form dynamic form reactreact dynamically add new formcreate dynamic field in reactjs componenthow to set dynamic input values in reactmaking dynamic form inputs with reactreact dynamic fieldsreact control value input field dynamic number of inputshow to add edit functionality on dynmic field in react jsreact dynamiclaly creating fieldsreact bootstrap add dynamic formreact dyanamic adding formreact dynamic fielddynamic fields reactcreate additional form reactjsimplementing dynamic forms in reactcreate dynamic input fields reactdynamic form in reactjsreact dynamic form fieldshow build dynamic content in reactreact object add input pluscreate dynamic form fields in reactreact dynamic formdynamic field for reactreact add dynamic form fieldsreact create dynamic inputscreate dynamic field in reactjscreate dynamic form fields using reactdynamic input field reactdynamiccally pass input component in object in reactorder dynamically generated inputs reactdynamic input fields react jshow to add dynamic fields in form using react jsadd dynamic fields to form in reactjssubmit dynamic form in reactcreate dynamic form in react jsdynamic pass input component reactreact dynamic inputdynamic input field react classreact dynamic input listpost input fields dynamically with reactjsdynamic field in reactimplement dynamic input field in reacthow to generate a dynamic field using reacthow to get dynamic input value in reactadd dynamic fields to form in multiple section with reactjs functionalcreate dynamic fields in reactadd dynamic fields to forms with reactjsuse dynamic input names reactpost form dynamically reacthow to create dynamic input fields in reactjs in class componentsadding dynamic input fields react jssubmit a dynamic field in reactdynamic form react componentdynamic react fieldsdynamic input fields in react jshow to create dynamic forms in react jsbuilding a dynamic controlled form with reactdynamic forms react jsdynamics build a react component to display on formcreate dynamic fields in react