In this lesson you will
- Build controlled text, number, select and checkbox inputs
- Manage many fields with one state object and one change handler
- Handle submit with preventDefault and reset the form
- Validate input and show clear, accessible error messages
Controlled inputs: value plus onChange
A controlled input has two connections. value={name} makes the input show whatever is in state. onChange={e => setName(e.target.value)} copies every edit back into state. In React, onChange fires on every keystroke, like the DOM input event, not only when the field loses focus. If you set value without onChange, the field becomes read-only and React warns you. Because the value is in state, the rest of the component can use it immediately: show a greeting as the name is typed, count characters, or filter a list.
The value of a text or number input is always a string, even with type="number". If you store e.target.value and later add it to something, you get text joining instead of arithmetic: "2" plus 1 is "21". Convert when you use the value, Number(qty), and check the result with Number.isFinite before trusting it. Keeping the raw string in state and converting when needed is the simplest approach, because it lets the user clear the field or type "1." on the way to "1.5" without the input fighting back.
Console
- Console messages, errors, form data and clicked links appear here.
The result updates on every keystroke, and the select is controlled the same way as the input, with value and onChange. The last line shows "1205": the bill is a string. Clear the bill field to see the message instead of NaN.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
What is the difference between controlled and uncontrolled inputs in React?
A controlled input gets its value from state through value and reports changes through onChange. An uncontrolled input keeps its own value in the DOM, starts from defaultValue, and you read it when needed, usually with FormData on submit.
Why does React warn that a component is changing an uncontrolled input to be controlled?
The value was undefined at first, so React treated the input as uncontrolled, and later it became a string. Start the state as an empty string, useState(""), so the input is controlled from the first render.
Should I use a form library such as React Hook Form?
For large forms with many fields and rules, a library saves time and handles details such as touched fields and schema validation. Learn the plain approach in this lesson first; libraries build on the same ideas.
How do I handle a textarea or a select in React?
Exactly like an input: <textarea value={note} onChange={...} /> and <select value={city} onChange={...}>. In React the selected option is set with value on the select, not with selected on an option.
What are form actions in React 19?
React 19 lets you pass a function to <form action={fn}>; React calls it with the FormData and resets uncontrolled fields after it finishes. It is most useful with frameworks such as Next.js. The onSubmit approach in this lesson works everywhere and is still common.
Sources
Facts in this lesson were checked against these official pages on 22 November 2026.
- React reference: input (controlled inputs)
- React reference: select
- React reference: form (actions)
- React docs: Responding to Events
- MDN: Client-side form validation
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.