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

177
Vistas
React Dynamic Components and Create Object
import React, { useState } from "react";

function Form() {
  const [components, setComponents] = useState(["1"]);

  function addComponent() {
    setComponents([...components, 1]);
    
  }

  const defaultState = [
    {
      title: "",
      desc: "",
    },
  ];

  const [form, setForm] = useState(defaultState);

  const handleChangeTitle = (e) => {
    setForm({
      title: e.target.value,
      desc: form.desc,
    });
  };

  const handleChangeDesc = (e) => {
    setForm({
      title: form.title,
      desc: e.target.value,
    });
  };
  return (
    <div>
      <div style={{ display: "flex" }}>
        <button onClick={addComponent}>+</button>
        {components.map((item, i) => (
          <>
            <div style={{ display: "flex" }}>
              <label>
                Title:
                <input
                  type="text"
                  name="title"
                  onChange={(e) => handleChangeTitle(e)}
                />
              </label>
              <label>
                Description:
                <input
                  type="text"
                  desc="desc"
                  onChange={(e) => handleChangeDesc(e)}
                />
              </label>
            </div>
          </>
        ))}
      </div>
    </div>
  );
}

export default Form;

Hi, I have a code like the one at the top. Pressing the "+" button on the page extracts a text input for title and description on the screen. This means that input numbers are increasing according to the user's request. I want to print these inputs, which have more than one goal, in a single object (defaultState in form state). For example, if you want to use the user pressed the plus button 2 times and the view of the object should be as follows: {title:'Title:'1st title', desc:'1.content', title:'2.title', desc:'2.content', title:'3.title', desc:'3.content'}

about 4 years ago · Juan Pablo Isaza
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