Home > other >  Setting formData with an object
Setting formData with an object

Time:06-18

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>

CodePudding user response:

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
    }));
  };
  • Related