← React Foundations
13 / 25
Lesson 13 — Controlled input
You decide what gets stored
Because a controlled input shows whatever is in state, your onChange handler can change the value before saving it: trim it, uppercase it, or cut it to a maximum length. The input then shows your version, not the raw keystrokes.
function onChange(e) {
setCode(e.target.value.toUpperCase()); // always shows capitals
}
Why it matters: this is how apps enforce limits like "max 280 characters" or "digits only" while the user types.
Want it another way?
Task
Store at most 10 characters. Typing "hello world!" should leave "hello worl" in the box and show 10/10.
Test your knowledge
A quick 3-question quiz on this lesson — instant feedback.