reactjs - 来自ajax的反应选择选项数据
问题描述
使用
<Select
native
value={item.resolution}
onChange={this.handleChange}
inputProps={{
name: 'student',
id: 'student',
data_id: index
}}
>
{ this.state.students ? this.state.students.map((res, i) => (
<option key={index+i} value={res.id}>{res.name}</option>
)): ''}
</Select>
在构造函数中
constructor(props) {
super(props);
this.state = {
students: []
}
}
componentDidMount() {
this.getOptions()
}
async getOptions(){
const res = await axios.get('https://jsonplaceholder.typicode.com/users')
const data = res.data
const options = data.map(d => ({
"value" : d.id,
"label" : d.name
}))
this.setState({students: options})
}
一切正常,但选中显示如下
怎么修?
解决方案
尝试并设置选定的值
<Select
native
value={item.resolution}
onChange={this.handleChange}
inputProps={{
name: 'student',
id: 'student',
data_id: index
}}
>
{ this.state.students ? this.state.students.map((res, i) => (
<option key={index+i} value={res.id} selected={i === 0}>{res.name}</option>
)): ''}
</Select>
推荐阅读
- android - 即使我设置布局宽度匹配父项,RecyclerView 也仅显示半屏
- javascript - 通过 WebAPI + jquery (ASP.NET core) 删除元素
- python - 如何使用空格作为分隔符来获取字符串中的子字符串?
- javascript - 在javascript ajax请求中获取特定的数组键
- laravel - 如何在 phpartisan 服务中自定义 url
- c# - 如何在 WPF MVVM 运行时从文本框值调整图像大小?
- python - 我可以仅使用 flow() 而不是 flow_from_directory 在 Keras 中增加图像和蒙版吗?
- ios - 应用渐变后获取透明 UIButton
- shiny - R Shiny Radiobuttons 几个列表
- vba - 带有 Application.ScreenUpdating 的 Excel VBA 不起作用