React Reference ยท Built-in DOM Components
The React option element
Reference for the React option element: set value, label and disabled, group choices with optgroup, and select options through the parent select.
<option> describes one choice inside a <select>, a <datalist> or an <optgroup>. Its value is what the form submits and what event.target.value reports; its children (plain text) are what the user reads. If you leave value out, the HTML rule applies: the value is the text content.
In React you never mark an option as chosen yourself. The parent <select> has the value or defaultValue, and React sets the matching option's selected state. Use <optgroup label="..."> to put related options under a heading.
<select defaultValue="fr"> <option value="de">Germany</option> <option value="fr">France</option> </select>
Try it
Syntax
<option value="string" disabled={boolean} label="string">Text</option> <optgroup label="Group name" disabled={boolean}>{options}</optgroup>
| Name | Description |
|---|---|
value |
The value reported on selection and submitted with a form. Defaults to the text content. |
children |
The visible text: a string, a number or a mix of both. Not elements. |
disabled |
The option cannot be chosen. |
label |
An alternative text shown instead of the children by some browsers. Usually omitted. |
selected |
Do not use in React. Set value or defaultValue on the select. |
For <optgroup>: label (required in practice) and disabled.
Returns: an HTMLOptionElement (or HTMLOptGroupElement).
Edge cases: React warns if you set selected, and warns ("Cannot infer the option value of complex children") if the children contain elements and there is no value.
Usage notes
- Children are text only. An option can contain only text, not
<b>or<span>. To add structure, build a custom listbox instead of a native select. - Text pieces are fine.
<option>{name} ({count})</option>works, because React joins plain text and numbers. Only element children (such as<b>) are a problem. - Keys. Options made with
mapneed a stablekey, usually the value. - Disabled does not remove it from the data. A disabled option can still be the
valueof the select if you set it from code. - Empty first option.
<option value="">Choose...</option>is the usual way to allow no choice. Adddisabledto forbid choosing it again. - Large lists. Thousands of options are slow in every browser. Filter or use a search field.
- Fragments are fine. You can return
<>...</>with several options from a component, because fragments leave no DOM node. - Datalist.
<datalist>takes the same<option value>children for autocomplete suggestions of an<input list="id">.
Related
<select>,<input>,<form>- Lessons: Forms and inputs, Lists and keys