create dynamic fields in react

import React, { useState } from "react";

function App() {
  const [inputList, setInputList] = useState([{ firstName: "", lastName: "" }]);

  // handle input change
  const handleInputChange = (e, index) => {
    const { name, value } = e.target;
    const list = [...inputList];
    list[index][name] = value;
    setInputList(list);
  };

  // handle click event of the Remove button
  const handleRemoveClick = index => {
    const list = [...inputList];
    list.splice(index, 1);
    setInputList(list);
  };

  // handle click event of the Add button
  const handleAddClick = () => {
    setInputList([...inputList, { firstName: "", lastName: "" }]);
  };

  return (
    <div className="App">
      <h3><a href="https://cluemediator.com">Clue Mediator</a></h3>
      {inputList.map((x, i) => {
        return (
          <div className="box">
            <input
              name="firstName"
              placeholder="Enter First Name"
              value={x.firstName}
              onChange={e => handleInputChange(e, i)}
            />
            <input
              className="ml10"
              name="lastName"
              placeholder="Enter Last Name"
              value={x.lastName}
              onChange={e => handleInputChange(e, i)}
            />
            <div className="btn-box">
              {inputList.length !== 1 && <button
                className="mr10"
                onClick={() => handleRemoveClick(i)}>Remove</button>}
              {inputList.length - 1 === i && <button onClick={handleAddClick}>Add</button>}
            </div>
          </div>
        );
      })}
      <div style={{ marginTop: 20 }}>{JSON.stringify(inputList)}</div>
    </div>
  );
}

export default App;

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
create dynamic field in reactjs component create dynamic field in reactjs react add dynamic form fields Create Dynamic Form Fields in React react dynamic form fields create dynamic form fields using react react dynamiclaly creating fields dynamic react fields react dynamic field how build dynamic content in react add dynamic fields to form in reactjs dynamic field for react how to add dynamic fields in form using react js dynamic fields react how to generate a dynamic field using react Making dynamic form inputs with React react create dynamic inputs submit a dynamic field in react submit dynamic form in react dynamics build a react component to display on FORM dynamic form react component dynamic form react react dyanamic adding form react dynamic input how to set dynamic input values in react react control value input field dynamic number of inputs how to create dynamic input fields in reactjs in class components create dynamic fields in react create dynamic form in react js react dynamic input list how to add edit functionality on dynmic field in react js how to get dynamic input value in react dynamic form in reactjs react class component dynamic form dynamic input field react class dynamic input field react dynamiccally pass input component in object in react dynamic pass input component react how to create dynamic forms in react js implementing dynamic forms in react edit form with pluss button in reactjs add dynamic fields to form in multiple section with reactjs functional add dynamic fields to forms with reactjs implement dynamic input field in react POST form dynamically react post input fields dynamically with reactjs building a dynamic controlled form with react create additional form reactjs dynamic input fields react js react object add input plus order dynamically generated inputs react use dynamic input names react dynamic input fields in react js create dynamic input fields react adding dynamic input fields react js react dynamically add new form react dynamic form react bootstrap add dynamic form dynamic forms react js
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source