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

95
Views
Dynamically add input field React

I am trying to add a new row of input directly under the current row. However, I can only add the new input at the bottom of the array instead of directly after the object within the array

  const initialState = [
    { name: "John", Age: 0, height: 0, weight: 0 },
    { name: "Mary", Age: 0, height: 0, weight: 0 },
    { name: "Darren", Age: 0, height: 0, weight: 0 },
    { name: "Eli", Age: 0, height: 0, weight: 0 },

  ];

  const [infoValues, setInfoValues] = useState(initialState);

// I want to add the new row directly under the current one instead at the last

  const addInputRow = () => {
    setInfoValues([
      ...infoValues,
      { name: "Eli", Age: 0, height: 0, weight: 0},
    ]);
  };

  const removeInputRow = (index) => {
    const valueList = [...infoValues];
    valueList.splice(index, 1);
    setInfoValues(valueList);
  };
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You're almost there. You're addInputRow method should accept an index, and you would splice the value similar to how you remove:

Note the second splice parameter is 0 - this means "remove 0 items during the splice". The third parameter means "insert this item at the specified index".

  const addInputRow = (index) => {
    const valueList = [...infoValues];
    valueList.splice(index, 0, { name: "Eli", Age: 0, height: 0, weight: 0})
    setInfoValues(valueList);
  };
about 4 years ago · Juan Pablo Isaza Report

0

I bet that the reason why Ryan's answer is not working for you, is that you are not correctly passing the index to the function, because when splice get undefined for the starting point, it will behave like if it is a 0, and add it to the first place.

Check this out, maybe can give you some clarity:

import React, { useState } from "react";

const initialState = [
  { name: "John", Age: 0, height: 0, weight: 0 },
  { name: "Mary", Age: 0, height: 0, weight: 0 },
  { name: "Darren", Age: 0, height: 0, weight: 0 },
  { name: "Eli", Age: 0, height: 0, weight: 0 }
];

export default function App() {
  const [infoValues, setInfoValues] = useState(initialState);

  const addInputRow = (index) => {
    const newInfoValues = [...infoValues];
    newInfoValues.splice(index + 1, 0, { name: "Eli", Age: 0, height: 0, weight: 0 });

    setInfoValues(newInfoValues);
  };

  return (
    <div className="App">
      <ul>
        {infoValues.map((value, index) => (
          <li key={index} onClick={() => addInputRow(index)}>
            {value.name}
          </li>
        ))}
      </ul>
    </div>
  );
}
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!