Command Palette

Search for a command to run...

Hectal
Phase 5: Forms and User Input
React · Exercise 3 of 5 PreviousNext
Intermediate

One state object, one handler

Rewrite this form so all its values live in one useState object: { name: "", email: "", city: "", newsletter: false }.

Use one change handler for all four fields. Each field has a name attribute matching its key (name, email, city, newsletter); the newsletter field is a checkbox.

Show the state under the form in <pre id="json">{JSON.stringify(form)}</pre>.

What Check tests

  1. #json shows the starting object
  2. Typing a name changes only name
  3. Email and city are stored too, and the name is kept
  4. The checkbox stores true, not a string
  5. One useState and a computed key [name]

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.