Controlled components
In vanilla JavaScript, form inputs manage their own values. In React, you’ll often manage input values with state. This is called a controlled component — React state is the single source of truth.value— set from state (React controls what’s displayed)onChange— updates state when the user types
All input types
Thevalue + onChange pattern works the same way for most common input types:
Checkboxes use
checked and e.target.checked. Most other inputs use value and e.target.value. (File inputs are a special case and are usually handled as uncontrolled inputs.)Handling form submission
UseonSubmit on the <form> element, not onClick on the button:
Managing multiple fields with one state object
When forms have many fields, use a single state object instead of separateuseState calls:
[name]: value — a computed property name. The input’s name attribute matches the key in state, so one handleChange function works for all fields.
Every input needs a
name attribute that matches the state key. handleChange reads e.target.name to know which field to update. This is the standard pattern for multi-field forms in React.Basic validation
Show errors per field and validate on submit:&&, clear errors when the user starts fixing them.
Edit forms — pre-populated from data
Edit forms start with existing data passed via props:onSave callback lets the parent component know the data was updated — this is the child-to-parent communication pattern you’ll learn in the next lesson.
What’s next?
You can build forms and handle user input. The last piece: how to structure your components together — which components own the data, which ones just display it, and how they communicate.Component composition
Structure your React app with smart and presentational components