Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Routing and Application Architecture
Stage 5: Specialization (pick ONE)

Part 05 / Topic 2 of 7

Multi-field state: one object vs separate useState calls

A form with one field needs one useState call. A form with several has a real decision to make, and both answers are correct React. examples/02_single_object_vs_separate_state.jsx builds the exact same two fields - name and email - both ways:

// examples/02_single_object_vs_separate_state.jsx (trimmed - the real file
// also has a <label> per input and a <p>Preview: ...</p> line, left out
// here for brevity)
export function SignupDetailsObjectState() {
  const [details, setDetails] = useState({ name: "", email: "" });

  function handleChange(event) {
    const { name, value } = event.target;
    setDetails((current) => ({ ...current, [name]: value }));
  }

  return (
    <form>
      <input name="name" value={details.name} onChange={handleChange} />
      <input name="email" value={details.email} onChange={handleChange} />
    </form>
  );
}

export function SignupDetailsSeparateState() {
  const [name, setName] = useState("");
  const [email, setEmail] = useState("");

  return (
    <form>
      <input value={name} onChange={(event) => setName(event.target.value)} />
      <input value={email} onChange={(event) => setEmail(event.target.value)} />
    </form>
  );
}

Both really behave identically from the outside - typing "Asha" then "[email protected]" into either version really ends with the same preview text, "Preview: Asha / [email protected]" (examples/02_single_object_vs_separate_state.test.jsx). The object version needs exactly one handleChange for every field, no matter how many fields the form grows to - the trade is that every input now needs a name attribute that matches a key in the state object exactly (name="email" must line up with details.email, or [name]: value quietly writes a brand-new, wrong key into the object instead of updating the right one). The separate-state version needs one useState call and one inline handler per field - more to type as the form grows, but each field's state and update logic stands completely on its own, with nothing to keep in sync by name. A short, rarely-changing form (two or three fields) reads fine either way; a longer form, or one this course's later parts will keep growing (the sign-up form by the end of this part), is where the single-object version's one-handler-for-everything starts to pay for itself.

Done here? The practice set for Part 5 — including the solutions — is on the part page.

Search the guide