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

155
Vistas
Setting formData with an object

Currently, i have this state with a formData.

Upon typing some text, instead to change the fullName.firstName. its making another property and just setting a single (as in single letter) value.

enter image description here

const [formData, setFormData] = useState({
    fullName: {
      firstName: "",
      lastName: "",
    },
    email: "",
    password: "",
    confirmPassword: "",
  });

This is how i set the formData.

 const handleChange = (event) => {
    const { value, name } = event.target;
    console.log(name, value);

    setFormData((prevFormData) => ({
      ...prevFormData,
      [name]: value,
    }));
  };

This is my JSX, you may check the "name" attribute in input for some reference.

<div className="App">
      <form onSubmit={onSubmit}>
        <h1>Submit Form Nested Object UseState</h1>

        <input
          text="text"
          placeholder="First Name"
          name="firstName"
          value={formData.fullName.firstName}
          onChange={handleChange}
        />
        <input
          text="text"
          placeholder="Last Name"
          name="lastName"
          value={formData.fullName.lastName}
          onChange={handleChange}
        />
        <input
          text="email"
          placeholder="email"
          name="email"
          value={formData.email}
          onChange={handleChange}
        />
        <input
          text="password"
          placeholder="password"
          name="password"
          value={formData.password}
          onChange={handleChange}
        />
        <input
          text="password"
          placeholder="confirm Password"
          name="confirmPassword"
          value={formData.confirmPassword}
          onChange={handleChange}
        />
        <button>Submit</button>
      </form>
      {JSON.stringify(formData, null, 2)}
    </div>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Change the form names like so:

      <form onSubmit={onSubmit}>
        <h1>Submit Form Nested Object UseState</h1>

        <input
          placeholder="First Name"
          name="fullName.firstName"
          value={formData.fullName.firstName}
          onChange={handleChange}
        />
        <input

          placeholder="Last Name"
          name="fullName.lastName"
          value={formData.fullName.lastName}
          onChange={handleChange}
        />
        <input

          placeholder="email"
          name="email"
          value={formData.email}
          onChange={handleChange}
        />
        <input

          placeholder="password"
          name="password"
          value={formData.password}
          onChange={handleChange}
        />
        <input

          placeholder="confirm Password"
          name="confirmPassword"
          value={formData.confirmPassword}
          onChange={handleChange}
        />
        <button>Submit</button>
      </form>

then change your handleChange function to this:

  const handleChange = (event) => {
    const { value } = event.target;
    const [key,subkey] = event.target.name.split('.');

    setFormData((prevFormData) => ({
      ...prevFormData,
      [key]: subkey ? {
        ...prevFormData[key],
        [subkey]: value,
      } : value
    }));
  };
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