reactjs - 如何更新 React 选择列表
问题描述
我正在尝试从 React select 更新列表,但无法更新,我不知道为什么会发生,我认为我的 handleChange() 方法有问题,但我没有得到什么问题。谢谢。!!
代码:-
import React, { Component } from 'react';
import Select from 'react-select';
class extends countryComponent {
constructor(props) {
super(props);
this.state = {
country:[],
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
const { name, value } = event.target;
this.setState({
[name]: value
});
}
async componentDidMount(){
await API.get("/country").then(response=>{
console.log(response);
this.setState({country:response.data.data.country});
})
Country=()=>{
return (this.state.country.map(data => ({ label: data.name, value: data.id })))
}
render() {
return ( <>
<Form.Group>
<Form.Label id="formLabel">Country</Form.Label>
<Select options={this.Country()}/>
</Form.Group>
</>)
}
export default Country;
解决方案
您正在异步加载选项。应该改用“react-select/async”。请参考https://react-select.com/async
推荐阅读
- jsf - 单击第一个复选框时如何检查另一个复选框?我的复选框列表从数据文件中检索
- google-cloud-platform - 为 Pubsub 到 Bigquery 流式传输构建此 GCP 数据流示例时出错
- python - 将 \n 替换为 " "
- java - [HttpURLConnection]如何发送带有表单数据的post请求
- python - 修复字典中的排序
- javascript - 为字符串中的每个句子添加 html 标签
- redis - 为从 redis-cli 中的外部 redis 模块加载的新命令添加帮助文档
- unity3d - Unity中没有导入包的选项
- mysql - sql查询是否正确?
- python - 如何获取列表中每个项目的第一项和最后一项