Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 5.1 — Controlled Inputs
React · Example 1 of 3

A controlled input and a textarea

Type in the Name box and watch the console: every keystroke is one render. The greeting, the counter and the Save button all read the same state as the fields.

A controlled input gets its value from React state and reports every keystroke through onChange, so the state is the one source of truth and the field always shows exactly what the state says.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Make a controlled <input> for a username that removes spaces as you type and shows @username below it.

02

Make a controlled <textarea> with a counter N characters left that starts at 100 and stops the user typing past 100.

Explain it without notes

01

What makes an input controlled? Walk through what happens on one keystroke.

02

Why does React warn when you pass value without onChange, and what are the three fixes?

03

How is React's onChange different from the DOM change event?

04

What does "single source of truth" mean for a form field, and why does it help?

A controlled input and a textarea

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

render: name = ""