Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda