Working with User Input and Submission
Handling user input in a React form means keeping track of what the user types and doing something useful when they press submit. The standard approach is the controlled input: each field's value lives in component state, an onChange handler updates that state on every keystroke, and an onSubmit handler on the form collects the values.
Controlled inputs with useState
An input becomes "controlled" when you give it both a value prop from state and an onChange handler that updates that state. React is now the single source of truth for the field. You can read the value anywhere in the component, reset it, or transform it as the user types.
import { useState } from "react";
function AddTaskForm({ onAddTask }) {
const [title, setTitle] = useState("");
const [priority, setPriority] = useState("medium");
function handleSubmit(event) {
event.preventDefault();
const trimmed = title.trim();
if (!trimmed) return;
onAddTask({ id: crypto.randomUUID(), title: trimmed, priority });
setTitle("");
setPriority("medium");
}
return (
<form onSubmit={handleSubmit}>
<label htmlFor="title">Task</label>
<input
id="title"
value={title}
onChange={(e) => setTitle(e.target.value)}
/>
<label htmlFor="priority">Priority</label>
<select
id="priority"
value={priority}
onChange={(e) => setPriority(e.target.value)}
>
<option value="low">Low</option>
<option value="medium">Medium</option>
<option value="high">High</option>
</select>
<button type="submit">Add task</button>
</form>
);
}
Submitting the right way
Attach the submit handler to the form, not to the button's onClick. A form's submit event fires when the user clicks the submit button and also when they press Enter inside a field, so keyboard users get the expected behaviour for free.
The first line of the handler, event.preventDefault(), stops the browser's default action. Without it, the browser sends a traditional request and reloads the page, wiping out your React state before you can use it.
Passing data up to the parent
A form component rarely stores the final list itself. Instead, the parent passes a function as a prop, here onAddTask, and the form calls it with the new item. The parent then updates its own state. This "lifting state up" pattern keeps the data in one place, so other components such as a task list or counter can use it too.
Handling many fields at once
For larger forms, separate useState calls become repetitive. You can keep one object in state and use the input's name attribute to decide which key to update: setForm((prev) => ({ ...prev, [e.target.name]: e.target.value })). Using the function form of the setter guarantees you always copy the latest state.
Bugs beginners hit
Setting value without an onChange makes the field read-only: React keeps forcing the old value back, and typing does nothing. React also warns about this in the console.
Starting state as undefined and later setting a string triggers the warning about switching from uncontrolled to controlled. Always initialise text fields with an empty string.
Checkboxes are different: read e.target.checked, not e.target.value, and pass checked instead of value. Reading value on a checkbox returns the string "on" regardless of whether it is ticked.
Finally, remember that inputs always produce strings. A number field gives you "5", not 5, so convert with Number() before doing maths.
Takeaway: bind each field's value to state, update it in onChange, handle submission on the form with preventDefault, pass the result to the parent, and reset the fields afterwards.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.