Skip to content

Forms in React: Controlled Inputs, onChange, onSubmit, Selects, Checkboxes and Validation

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

Forms are where users give your app data: a search term, a delivery address, a quantity, a booking date. In the JavaScript course you read input values when the form was submitted. React usually goes one step further: the value of each input lives in state, the input shows that state, and every keystroke updates it. These are called controlled inputs, and they let you validate as people type, disable the submit button until the form is complete, and show live totals. This lesson covers text inputs, numbers, selects, checkboxes and radio buttons, submitting, and showing helpful error messages.

Quick answer

For a controlled input, keep the value in state and connect both directions: <input value={name} onChange={handle} />, where the handler calls setName(e.target.value). Checkboxes use checked and e.target.checked instead. Input values are always strings, so convert numbers with Number(...). Handle sending on the form, not the button: <form onSubmit={handleSubmit}>, and call e.preventDefault() first so the page does not reload. Validate in the handler or during render, store error messages in state or derive them, and show them next to the fields.

35 min read and practice · Content checked on 22 November 2026

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.

Try it: A live tip calculator with controlled inputs
Code
ResultUpdates as you type
Console
  1. 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
Sign in or create a free account

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.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.