【发布时间】:2021-10-26 21:10:10
【问题描述】:
我是新手,我正在尝试为客户存储状态值,我获取下拉列表值的代码是这样的:
handleChangeCC = customercode => {
this.setState({ customercode });
// var customer = customercode.map(o => o.label);
// this.setState({ customer });
var customer1 = customercode.label;
this.setState({ customer: customer1 });
console.log(`Option selected1:`, this.state.customer);
this.getCustomerName();
// console.log("Rec2:" + document.getElementById("CustomerCode").innerText);
};
我在这个函数中运行函数 this.getCustomerName(),代码是这样的:
getCustomerName = () => {
var Items = [];
var code = this.state.customer;
console.log('CustomerCode:' + code);
axios.post('https://localhost:44303/api/observation/GetCustomerName?' + '&code=' + code).then(response => {
//console.log('Value=' + response.data);
console.log('Response=' + response.data);
response.data.map(item => {
var obj = new Object();
// obj.label = desc;
obj.label = item;
Items.push(obj);
//this.setState({ locations: response.data });
});
this.setState({ customerName: Items });
console.log('customerName:' + this.state.customerName);
});
};
我在函数 getCustomerName 中设置 code= this.state.customer,我在 this.setState({ customer: customer1 }); 之后运行它在 handleChangeCC 函数中。
但是由于 this.setState 是一个异步函数,它不会立即更新状态,因此我将代码设为 null 并且我的 getCustomerName 函数不起作用
我该如何解决这个问题?有没有办法将一个函数的变量值传递给另一个函数。请帮忙
【问题讨论】:
标签: javascript reactjs async-await