【发布时间】:2021-11-04 18:36:03
【问题描述】:
我正在使用来自 reactstrap 的 CustomInput。我正在设置要选择的类型。在我的 CustomInput type="select" 中,我正在映射数组中的选项。我需要将选择中的默认值设置为正在映射的特定选项。例如。我希望默认为美国或德国,现在我正在映射一系列不同的国家/地区。
下面是我的代码
<CustomInput
type="select"
id="country"
name="country"
className="mb-3"
value={country.countryCode}
onChange={handleChange}
>
{numberCountries.map(({countryCode, name}) => (
<Fragment>
<option key={countryCode} value={countryCode}>{name}</option> <--- how can i set a one of these options to be the default value rendered on the screen?
</Fragment>
))}
</CustomInput>
这是我正在映射的对象
[
{
name: 'United States',
countryCode: 'US'
},
{
name: 'Germany',
countryCode: 'DE'
}
]
【问题讨论】:
-
您可以在父
CustomInput上将其设置为value用于动态/受控选择,或defaultValue用于加载初始值。
标签: javascript html reactjs reactstrap