QuizNot attempted
Spot the bug: option shape mismatch
1–4 to choose, Enter to submit
Show answer & explanation
Correct answer:
- The label text: an option without a value attribute uses its text, so it works only by accident
- React drops an attribute whose value is
undefined, and an<option>with novalueattribute reports its text content as its value. - So it works while label and id happen to be equal, and breaks the moment they differ ("Virtual Machines" vs
vm, or a translated label). - TypeScript didn't catch it:
useState([])infersnever[]andres.json()returnsany, so both shapes compile. - Fix: one shared
Optiontype, map to{ value: r, label: r }, and type the stateuseState<Option[]>([]). - Not undefined: the DOM falls back to the text.
- Not the index:
keyis React-only and never reaches the DOM. - Not an empty string: only the placeholder option has
value="".