Skip to content
Rung

09/12

No. 09Medium15 min

Forms: The Single Source of Truth

Mission

Manage several inputs with one state object and turn plain inputs into controlled components.

Tasks

  1. 1

    Give formData its initial value: a single object with name, email and role.

  2. 2

    Bind the value of every field (inputs and the select) to the matching key of formData.

  3. 3

    Write one generic handleChange that uses the input's name attribute.

  4. 4

    Update the state immutably with the spread operator.

  5. 5

    Stop the browser from reloading the page on submit with e.preventDefault().