Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
How to manage dynamic form data using useState?

I have a form that gets it's input fields dynamically, it can have hundreds of fields so i can't create a state individually, I was planning on doing something like using an object using the unique key of a form field but need some help.

Suppose the form has fields like this.

<form>
 {inputFields.map((i) => {
    <input type={i.type} />
  })}
</form>

Now i would need a state like the one below

 inputState = {
  "INPUT_FIELD_NAME1": "INPUT FIELD VALUE 1",
  "INPUT_FIELD_NAME2": "INPUT FIELD VALUE 2",
  "INPUT_FIELD_NAME3": "INPUT FIELD VALUE 3",
}

I need help with this, how do i set values in such a manner in my input onChange and how do i access the values from the state and use them for the matching input field?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

As per my understanding and knowledge, you have to update your dynamic structure like given as below

 <form>
    {inputFields.map((i) => (
      <input
        type={i.type}
        name={`INPUT_FIELD_NAME${i.id}`}
        onChange={handleChange}
      />
    ))}
  </form>

Also have to update your react state on input change like

const [inputState, setinputState] = useState({});
const handleChange = (e) => {
  const { name, value } = e.target;
  setinputState({
    ...inputState,
    [name]: value
  });
};

I hope it will work for you! Thanks.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I would do something like this:

const [form, setForm] = useState({})

const onChange = (event) => {
   const { id, value } = event.target
   setForm((prev) => ({ ...prev, [id]: value }))
}

<form>
   {inputFields.map((i) => {
      <input key={i.id} onChange={onChange} id={i.id} type={i.type} />
   })}
</form>

Id must be unique, also you can use "name" property instead of "id"

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda