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

152
Views
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 answers
Answer question

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 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!