【发布时间】:2020-04-14 06:23:46
【问题描述】:
我使用反应钩子进行选择。目前只能取id,取不了名字。
选择代码:
const [countries, setCountries] = useState([]);
const [selectedCountry, setSelectedCountry] = useState(0);
useEffect(() => {
CountryGet().then(result => { setCountries(result); });
}, []);
const onChangeCountry = e => {
setSelectedCountry(e.target.value);
}
<FormControl className={classes.formControl}>
<InputLabel id="country_label">Country</InputLabel>
<Select id="country" value={selectedCountry} onChange={onChangeCountry}>
{countries.map((country) => (
<MenuItem key={country.id} value={country.id}>
{country.countryName}
</MenuItem>
))}
</Select>
</FormControl>
我希望在 selectCountry 中拥有 ID 和名称,但到目前为止我只获得了 ID。如何从选择中获取名称?
【问题讨论】: