Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 5.3 — Uncontrolled Inputs
React · Example 2 of 3

defaultValue is read once; a key starts over

The state changed to Ravi, but the first field still says Asha: defaultValue only mattered when the field was created. The second field got a new key, so React made a new input that started from the new default.

An uncontrolled input keeps its own value in the DOM: you give it a starting value with defaultValue or defaultChecked and read it when you need it, with a ref or with FormData; file inputs are always uncontrolled.

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

Build an uncontrolled search form with defaultValue="pizza" that logs the query on submit using FormData.

02

Build an uncontrolled Remember me checkbox that starts ticked and logs true or false when a button is clicked, using a ref.

Explain it without notes

01

What is an uncontrolled input, and how do you set its starting value?

02

Compare reading an uncontrolled field with a ref and with FormData.

03

Why is a file input always uncontrolled, and how do you work with it in React?

04

What causes the "changing an uncontrolled input to be controlled" warning and how do you fix it?

05

When would you choose controlled over uncontrolled, and the other way round?

defaultValue is read once; a key starts over

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

plain: Asha
keyed: Ravi