reactjs - 反应选择值落后一步
问题描述
我正在使用 react-select 并将值传递给后端节点脚本 - 但传递的值始终是选择框的前一个值 -
所以在这个例子中,当它加载并且我将下拉列表更改为 Lancaster 时,警报是空白的。如果我然后选择伦敦,警报会显示兰开斯特,那么无论我选择什么警报都会显示伦敦等。
有任何想法吗?
import React from 'react';
import axios from 'axios';
import Select from 'react-select';
export default class PersonList extends React.Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
this.state = {
cines: [],
};
}
handleSubmit(c){
axios.get(`/api/groups/`, {
params: {
City: c
}
})
.then(res => {
const cines = res.data;
this.setState({ cines });
})
.catch((err) => {
console.log(err);
})
}
handleChange = (event) => {
event.persist();
this.setState({ value: event.target.value});
alert(this.state.value);
this.handleSubmit(this.state.value);
}
render() {
const { options, value } = this.state;
return (
<div>
<select id="city" value={this.state.value} onChange={this.handleChange.bind(this)} >
<option value="">City</option>
<option value="Lancaster">Lancaster</option>
<option value="London">London</option>
</select>
<ul>
{ this.state.cines.map(cine => <li>* {cine.Name}</li>)}
</ul>
</div>
)
}
}
解决方案
推荐阅读
- reactjs - 在 Office Fabric UI 中最大化或最小化对话框
- apache-spark-sql - ALTER TABLE table ADD IF NOT EXISTS PARTITION (state = '34' , city = '123') 没有在 temp/local 文件夹中添加分区
- php - Wordpress 小部件错误 - 未捕获的错误:在 null 上调用成员函数 get_header_transparency()
- mysql - 从 BigQuery 中的查询中提取/获取视图中使用的表名及其各自的列
- anylogic - 如何基于 AnyLogic 模型的输出构建数据库
- python - 有没有办法恢复我的 conda 命令?
- vue.js - NuxtJS 静态站点常规选项
- logstash - Logstash 中的配置错误
- html - 按钮拒绝留在滚动 div
- json - 如何在pyspark中读取json字符串(json字符串有双双引号