Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

351
Views
Handle multiple input boxes rendered using map function javascript

I am working on a react where I am dealing with dynamic number of input fields as I am rendering them using arr.map function. But How can I handle the input onChange method with so many input fields?

Here's my component:

this.props.setsList.map((codeset, index) => (
     <Table.Row key={codeset.code_system_id}>
       <Table.Cell>{index + 1}</Table.Cell>
       <Table.Cell>{codeset.name}</Table.Cell>
       <Table.Cell>
         <Input
           type='text'
           className='form-control'
           value={codeset.code}
           placeholder={translateText('Code')}
           onChange={() => this.handleCodeChange(event, index)}
           style={{ height: '70%' }}
         />
       </Table.Cell>
       <Table.Cell>
         <Input
           type='text'
           className='form-control'
           value={codeset.description}
           placeholder={translateText('Code Description')}
           onChange={() => this.handleCodeDescriptionChange(event, index)}
           style={{ height: '70%' }}
         />
       </Table.Cell>
     </Table.Row>

All the input fields may have existing fields or may be empty and one can edit it them semd the edit fields to the API. How can I handle such a case with just 1 handleFunction. Is there a way? Any leads will be appreciated.

In the above code, 2 input boxes are there with initial values from the api.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Data passed into your component should go directly into state. Then each field sends the array index, field name and new value to the handleChange callback.

import { useState } from "react";

// passed in as a prop from the parent component
const data = [
  {
    name: "foo",
    code: "gdfgsd"
  },
  {
    name: "bar",
    code: "gfdsgsdfgfd"
  }
];

const App = ({ setsList = data }) => {
  const [state, setState] = useState(setsList);

  const handleChange = (e, i) => {
    const { value, name } = e.target;

    const newState = [...state];
    newState[i] = {
      ...newState[i],
      [name]: value
    };

    console.log(newState);
    setState(newState);
  };

  return (
    <div className="App">
      {state.map(({ name, code }, index) => {
        return (
          <div key={index}>
            <label>
              name
              {":  "}
              <input
                name="name"
                value={name}
                onChange={(e) => handleChange(e, index)}
              />
            </label>
            <label>
              code
              {":  "}
              <input
                name="code"
                value={code}
                onChange={(e) => handleChange(e, index)}
              />
            </label>
          </div>
        );
      })}
    </div>
  );
};

export default App;

Edit purple-worker-hrcl4


There's also a way that you can render each field dynamically by mapping over the array, and then inner mapping over that array item's keys.

import { useState } from "react";
import { data } from "./data";

const App = ({ setsList = data }) => {
  const [state, setState] = useState(setsList);

  const handleChange = (e, i) => {
    const { value, name } = e.target;
    const newState = [...state];
    newState[i] = {
      ...newState[i],
      [name]: value
    };
    console.log(newState);
    setState(newState);
  };

  return (
    <table className="App">
      <tbody>
        {state.map((item, index) => (
          <tr key={index}>
            {Object.keys(item).map((key) => (
              <td key={`${index}-${key}`}>
                <label>
                  {key}
                  {":  "}
                  <input
                    name={key}
                    value={item[key]}
                    onChange={(e) => handleChange(e, index)}
                  />
                </label>
              </td>
            ))}
          </tr>
        ))}
      </tbody>
    </table>
  );
};

export default App;

Edit naughty-haibt-ngzqv

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!