Skip to content

Controlled Inputs

The Course
•
20 min study

Today: controlled inputs

Forms are where users give your app data. In React, the usual pattern is a controlled input: state holds the field's value, and React shows that value in the field.

Two props make a field controlled

JSX
<input type="text" value={goal} onChange={(event) => setGoal(event.target.value)} />
  • value={goal} says "show what's in state".
  • onChange runs on every keystroke. event.target is the input, and event.target.value is the text it would now hold. The handler copies it into state, React re-renders, and the field shows the new value.

Type in the field below. The character count and the preview follow every keystroke, because they're computed from the same goal state during render.

A goal with a live preview React · 3 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { useState } from "react"; const LIMIT = 40; export default function App() { const [goal, setGoal] = useState(""); return ( <main> <h1>Today's goal</h1> <label> Goal{" "} <input type="text" value={goal} onChange={(event) => setGoal(event.target.value)} /> </label> <p className="muted">{goal.length} of {LIMIT} characters</p> <section className="card"> <h2>Preview</h2> <p>{goal === "" ? "Type a goal above." : `Today I will ${goal}.`}</p> </section> </main> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.75rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } .card h2 { margin: 0 0 0.25rem; font-size: 1.1rem; } .muted { color: #6b7785; } label { display: block; margin: 0.5rem 0; } input, select, button { font: inherit; } input[type="text"], input[type="number"], select { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } .error { color: #b42318; margin: 0.25rem 0; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.

Predict: value without onChange

Delete the onChange prop and run. Then try typing in the field.

Check your prediction

Nothing you type appears. value={goal} still says "show what's in state", and nothing changes the state any more. The console explains: You provided a value prop to a form field without an onChange handler. This will render a read-only field. Put onChange back.

Why control the field?

Because the value lives in state, you can use it anywhere while the user types: a live preview, a character count, a disabled button, or a corrected value. Change the handler to keep the goal within the limit:

JavaScript
onChange={(event) => setGoal(event.target.value.slice(0, LIMIT))}

Now the field refuses to grow past 40 characters. The state decides what the field shows.

Your turn

Exercise

Warn near the limit

Show the character count in the error class (red) once there are 35 or more characters, and add (almost full) after it. Keep the slice from above so the goal can't pass 40.

Your working space React · 3 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { useState } from "react"; const LIMIT = 40; export default function App() { const [goal, setGoal] = useState(""); return ( <main> <h1>Today's goal</h1> <label> Goal{" "} <input type="text" value={goal} onChange={(event) => setGoal(event.target.value)} /> </label> <p className="muted">{goal.length} of {LIMIT} characters</p> <section className="card"> <h2>Preview</h2> <p>{goal === "" ? "Type a goal above." : `Today I will ${goal}.`}</p> </section> </main> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.75rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } .card h2 { margin: 0 0 0.25rem; font-size: 1.1rem; } .muted { color: #6b7785; } label { display: block; margin: 0.5rem 0; } input, select, button { font: inherit; } input[type="text"], input[type="number"], select { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } .error { color: #b42318; margin: 0.25rem 0; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea

Whether the count is near the limit follows from goal.length, so work it out during render. It doesn't need its own state.

Hint 2: a first step

const nearLimit = goal.length >= 35;, then a ternary for the class name and && for the extra words.

Show one solution
JavaScript
const nearLimit = goal.length >= 35;
JSX
<p className={nearLimit ? "error" : "muted"}>
  {goal.length} of {LIMIT} characters{nearLimit && " (almost full)"}
</p>

Was this article helpful?

Your response is saved on this device.